Class NodeWallet
Hierarchy
- BaseWallet
- NodeWallet (View Summary)
Index
Constructors
Properties
Accessors
Methods
Constructors
constructor
Create a new NodeWallet instance
Parameters
- provider: IProvider
Provider instance for blockchain communication
OptionalprivateKey: stringOptional private key as hex string. Can be set later with setPrivateKey()
Returns NodeWallet
- provider: IProvider
Properties
Protected_address
Protected_connected
Protected_events
Protected_provider
Protected_publicKey
Accessors
address
- get address(): string
Wallet address (bech32 format)
Returns string
provider
Provider instance for blockchain queries
Returns IProvider
publicKey
- get publicKey(): string
Public key (hex format)
Returns string
Methods
broadcastTransaction
Broadcast a single signed transaction to the network Default implementation uses the provider's sendRawTransaction method Can be overridden by child classes for custom behavior (e.g., extension broadcasting)
Parameters
- tx: Transaction
Returns Promise<TransactionHash>
broadcastTransactions
Broadcast multiple signed transactions to the network in a single batch Default implementation uses the provider's sendRawTransactions method Can be overridden by child classes for custom behavior (e.g., extension broadcasting)
Parameters
- txs: Transaction[]
Returns Promise<TransactionHash[]>
connect
Connect the wallet
Derives the public key and address from the private key and marks the wallet as connected. This operation is performed locally without any network calls.
Returns Promise<void>
disconnect
Disconnect from the wallet
Clears the connection state and optionally removes the private key from memory.
Security Note:
- By default (clearPrivateKey=false), the private key remains in memory for quick reconnection
- For enhanced security, set clearPrivateKey=true to completely remove the key from memory
- After clearing the private key, you must create a new wallet instance to reconnect
Parameters
- clearPrivateKey: boolean = false
Whether to clear the private key from memory (default: false) - false: Keep key in memory, can reconnect with connect() - true: Remove key from memory, requires new wallet instance
Returns Promise<void>
Protectedemit
Parameters
- event: WalletEvent
Optionaldata: unknown
Returns void
getBalance
Get wallet balance
Returns Promise<bigint>
Balance in smallest units (KLV has 6 decimals)
getNonce
Get current nonce for the wallet Used for transaction ordering
Returns Promise<number>
Current nonce value
isConnected
Check if wallet is connected
Returns boolean
true if connected, false otherwise
off
Unregister an event handler
Parameters
- event: WalletEvent
Event name
- handler: WalletEventHandler
Event handler function to remove
Returns void
- event: WalletEvent
on
Register an event handler
Parameters
- event: WalletEvent
Event name ('connect', 'disconnect', 'accountChanged')
- handler: WalletEventHandler
Event handler function
Returns void
- event: WalletEvent
removeAllListeners
Remove all event listeners for a specific event or all events
Parameters
Optionalevent: WalletEventOptional event name. If not provided, removes all listeners for all events
Returns void
sendTransaction
Send a transaction with any contract type Builds, signs, and broadcasts the transaction Can be overridden by child classes (e.g., BrowserWallet uses extension)
Parameters
- contract: ContractRequestData
Returns Promise<TransactionSubmitResult>
setPrivateKey
Set or change the private key
Security Note: Can only be called when wallet is disconnected to prevent accidental key changes during active operations.
Parameters
- privateKey: string
Private key as hex string
Returns void
- privateKey: string
signMessage
Sign a message with the wallet's private key
SECURITY WARNING:
- Only sign messages from trusted sources
- Signing malicious messages can lead to phishing attacks
- Never sign messages that look like transactions or authorization requests
- Verify the message content before signing
The message is signed using the Ed25519 signature scheme. The resulting signature can be verified using the wallet's public key.
Parameters
- message: string | Uint8Array<ArrayBufferLike>
Message to sign (string or raw bytes)
Returns Promise<Signature>
Signature object with .toHex() and .toBase64() methods
Example
const message = "Hello, Klever!"
const signature = await wallet.signMessage(message)
// Get signature in different formats
console.log('Hex:', signature.toHex())
console.log('Base64:', signature.toBase64())
// Verify the signature
const isValid = await wallet.verifyMessage(message, signature)
console.log('Valid:', isValid) // true
signTransaction
Sign a transaction with the wallet's private key
SECURITY WARNING:
- Always verify transaction details before signing
- Check recipient address, amount, and contract type
- Signing a transaction authorizes it to be broadcasted to the blockchain
- Signed transactions cannot be reversed once broadcasted
The transaction is signed in-place, modifying the original transaction object with the signature. The same transaction object is returned for convenience.
Parameters
- tx: Transaction
Unsigned transaction to sign
Returns Promise<Transaction>
The same transaction object, now signed
Example
import { TransactionBuilder } from '@klever/connect-transactions'
// Build a transaction
const builder = new TransactionBuilder(provider)
const unsignedTx = await builder
.transfer({ receiver: 'klv1...', amount: 1000000 })
.sender(wallet.address)
.build()
// Sign it
const signedTx = await wallet.signTransaction(unsignedTx)
// Broadcast
const hash = await wallet.broadcastTransaction(signedTx)
console.log('Transaction hash:', hash)
transfer
Transfer tokens to another address Convenience method that uses sendTransaction internally
Parameters
- params: TransferRequest
Returns Promise<TransactionSubmitResult>
verifyMessage
- verifyMessage(
message: string | Uint8Array<ArrayBufferLike>,
signature: string | Signature,
): Promise<boolean>Verify a message signature
Parameters
- message: string | Uint8Array<ArrayBufferLike>
The message that was signed (string or bytes)
- signature: string | Signature
The signature to verify (Signature object, hex string, or base64 string)
Returns Promise<boolean>
true if signature is valid, false otherwise
Example
const message = "Hello, Klever!"
const signature = await wallet.signMessage(message)
// Verify with Signature object
const isValid = await wallet.verifyMessage(message, signature)
// Or verify with hex string
const isValidHex = await wallet.verifyMessage(message, signature.toHex())
// Or verify with base64 string
const isValidBase64 = await wallet.verifyMessage(message, signature.toBase64())
console.log('Signature valid:', isValid) - message: string | Uint8Array<ArrayBufferLike>
Staticgenerate
Generate a new wallet with a random private key
Creates a new wallet instance with a cryptographically secure random private key. The wallet is ready to connect and use immediately.
Important:
- Save the private key securely - it cannot be recovered if lost
- Never share the private key with anyone
- Use environment variables or secure key management in production
Parameters
- provider: IProvider
Provider instance for blockchain communication
Returns Promise<NodeWallet>
New NodeWallet instance with generated key pair
Example
import { KleverProvider } from '@klever/connect-provider'
import { NodeWallet } from '@klever/connect-wallet'
const provider = new KleverProvider({ network: 'testnet' })
const wallet = await NodeWallet.generate(provider)
await wallet.connect()
console.log('New wallet address:', wallet.address)
// IMPORTANT: Save the private key securely!
// You can extract it before connecting if needed for backup
Wallet implementation for Node.js environments
NodeWallet provides secure wallet operations in server-side Node.js applications. It uses private key-based signing and is designed for backend services, CLI tools, and automated systems.
Security Considerations:
disconnect(true)option to clear keys from memory when doneEnvironment:
Example
Example