Class BaseWalletAbstract

    Abstract base wallet implementation providing common functionality

    BaseWallet implements the core wallet interface and provides shared functionality for all wallet types. It handles:

    • Provider management
    • Event system (connect, disconnect, accountChanged)
    • Balance and nonce queries
    • Transaction broadcasting
    • Message verification

    Design Pattern: This class follows the Template Method pattern, where concrete implementations (NodeWallet, BrowserWallet) provide specific implementations for:

    • connect() - How to establish wallet connection
    • disconnect() - How to clean up connection
    • signMessage() - How to sign arbitrary messages
    • signTransaction() - How to sign blockchain transactions

    Common Functionality: All wallets inherit these capabilities:

    • getBalance() - Query wallet balance
    • getNonce() - Get transaction nonce
    • verifyMessage() - Verify message signatures
    • broadcastTransaction() - Send signed transactions to blockchain
    • Event handling (on/off/emit)

    Do Not Instantiate Directly: This is an abstract class. Use concrete implementations:

    • NodeWallet for server-side Node.js applications
    • BrowserWallet for browser/dApp applications
    // Don't do this (abstract class):
    // const wallet = new BaseWallet(provider) // ❌ Error

    // Do this instead:
    import { NodeWallet } from '@klever/connect-wallet'
    const wallet = new NodeWallet(provider, privateKey) // ✅ Correct
    Implements
    • Wallet
    Hierarchy

    Constructors

    Properties

    _address: string = ''
    _connected: boolean = false
    _events: Map<WalletEvent, Set<WalletEventHandler>> = ...
    _provider: IProvider
    _publicKey: string = ''

    Accessors

    Methods

    • Disconnect from the wallet

      Parameters

      • OptionalclearPrivateKey: boolean

      Returns Promise<void>

    • Unregister an event handler

      Parameters

      • event: WalletEvent

        Event name

      • handler: WalletEventHandler

        Event handler function to remove

      Returns void

    • Register an event handler

      Parameters

      • event: WalletEvent

        Event name ('connect', 'disconnect', 'accountChanged')

      • handler: WalletEventHandler

        Event handler function

      Returns void

    • Remove all event listeners for a specific event or all events

      Parameters

      • Optionalevent: WalletEvent

        Optional event name. If not provided, removes all listeners for all events

      Returns void

    • 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>

    • Sign a message with the wallet's private key

      Parameters

      • message: string | Uint8Array<ArrayBufferLike>

      Returns Promise<Signature>

    • 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

      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)