Class WalletFactory

    Factory for creating environment-appropriate wallet instances

    WalletFactory automatically detects the runtime environment (Node.js, Browser, React Native) and creates the appropriate wallet implementation. This provides a unified API for wallet creation across different platforms.

    Automatic Environment Detection:

    • Node.js → Creates NodeWallet
    • Browser → Creates BrowserWallet
    • React Native → (Future support)

    Benefits:

    • Write once, run anywhere - same code works in Node.js and browser
    • Simplifies multi-platform dApp development
    • Handles environment-specific wallet initialization
    • Type-safe configuration
    import { WalletFactory } from '@klever/connect-wallet'
    import { KleverProvider } from '@klever/connect-provider'

    const provider = new KleverProvider({ network: 'mainnet' })
    const factory = new WalletFactory(provider)

    // In Node.js, this creates a NodeWallet
    // In Browser, this creates a BrowserWallet
    const wallet = await factory.createWallet({
    privateKey: process.env.PRIVATE_KEY, // Optional in browser (uses extension)
    network: 'mainnet',
    })

    await wallet.connect()
    console.log('Wallet address:', wallet.address)
    // Browser-specific configuration
    const wallet = await factory.createWallet({
    // No privateKey = uses extension in browser
    // With privateKey = uses private key mode
    privateKey: '0x123...', // Optional
    })
    // Use the convenience function for simpler code
    import { createWallet } from '@klever/connect-wallet'

    const wallet = await createWallet({
    network: 'testnet',
    privateKey: process.env.PRIVATE_KEY,
    })
    Implements
    • WalletFactory
    Index

    Constructors

    Methods

    • Create a new wallet with a randomly generated private key

      Creates a wallet appropriate for the current environment:

      • Node.js → NodeWallet with random key
      • Browser → BrowserWallet in private key mode with random key

      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

      • Optionalprovider: IProvider

        Optional custom provider (defaults to factory's provider)

      Returns Promise<Wallet>

      A promise that resolves to a new Wallet instance

      const factory = new WalletFactory()
      const wallet = await factory.createRandom()
      await wallet.connect()
      console.log('New address:', wallet.address)
      // With custom provider
      const testProvider = new KleverProvider({ network: 'testnet' })
      const wallet = await factory.createRandom(testProvider)
    • Create a wallet instance appropriate for the current environment

      Detects the runtime environment and creates:

      • NodeWallet in Node.js (requires privateKey)
      • BrowserWallet in browsers (optional privateKey, defaults to extension mode)
      • Throws error for React Native (not yet implemented)

      Parameters

      • Optionalconfig: WalletConfig

        Optional wallet configuration

        • privateKey

          Private key for wallet initialization (required for Node.js)

        • pemContent

          PEM file content (alternative to privateKey)

        • pemPassword

          Password for encrypted PEM files

        • network

          Network to connect to ('mainnet', 'testnet', etc.)

        • provider

          Override the factory's provider

      Returns Promise<Wallet>

      Wallet instance ready to connect

      If environment is React Native (not yet supported)

      If privateKey is not provided in Node.js environment

      // Node.js environment
      const wallet = await factory.createWallet({
      privateKey: process.env.PRIVATE_KEY, // Required
      network: 'mainnet',
      })
      // Browser environment - Extension mode
      const wallet = await factory.createWallet({
      // No privateKey = uses Klever Extension
      })
      // Browser environment - Private key mode
      const wallet = await factory.createWallet({
      privateKey: '0x123...', // Uses private key instead of extension
      })
    • Create a wallet from an encrypted keystore (Web3 Secret Storage)

      Creates a wallet appropriate for the current environment:

      • Node.js → NodeWallet with decrypted key
      • Browser → BrowserWallet in private key mode with decrypted key

      Parameters

      • json: string | Keystore

        The keystore object or JSON string

      • password: string

        The password to decrypt the keystore

      • Optionalprovider: IProvider

        Optional custom provider (defaults to factory's provider)

      Returns Promise<Wallet>

      A promise that resolves to a new Wallet instance

      Error if password is incorrect or keystore is invalid

      const factory = new WalletFactory()
      const wallet = await factory.fromEncryptedJson(keystore, 'my-password')
      await wallet.connect()
      console.log('Loaded wallet address:', wallet.address)
      // From JSON string
      const keystoreJson = await fs.readFile('keystore.json', 'utf-8')
      const wallet = await factory.fromEncryptedJson(keystoreJson, 'my-password')
    • Create a wallet from a BIP39 mnemonic phrase

      Creates a wallet appropriate for the current environment:

      • Node.js → NodeWallet with derived key
      • Browser → BrowserWallet in private key mode with derived key

      Parameters

      • mnemonic: string

        The BIP39 mnemonic phrase (12-24 words)

      • Optionalprovider: IProvider

        Optional custom provider (defaults to factory's provider)

      • Optionaloptions: MnemonicToKeyOptions

        Optional derivation path and passphrase

      Returns Promise<Wallet>

      A promise that resolves to a new Wallet instance

      const factory = new WalletFactory()
      const wallet = await factory.fromMnemonic('abandon abandon abandon...')
      await wallet.connect()
      console.log('Address:', wallet.address)
      // With custom derivation path
      const wallet = await factory.fromMnemonic('abandon abandon abandon...', undefined, {
      path: "m/44'/690'/0'/0'/1'"
      })
      // With passphrase
      const wallet = await factory.fromMnemonic('abandon abandon abandon...', undefined, {
      passphrase: 'my-secret-passphrase'
      })
    • Converts a BIP39 mnemonic phrase to a private key (hex string)

      This is a utility method that derives a private key from a mnemonic without creating a wallet instance. Useful when you only need the private key.

      Parameters

      • mnemonic: string

        The BIP39 mnemonic phrase (12-24 words)

      • Optionaloptions: MnemonicToKeyOptions

        Optional derivation path and passphrase

      Returns string

      The private key as a hexadecimal string

      const factory = new WalletFactory()
      const privateKey = factory.mnemonicToPrivateKey('abandon abandon abandon...')
      console.log('Private key:', privateKey)
      // With custom derivation path
      const privateKey = factory.mnemonicToPrivateKey('abandon abandon abandon...', {
      path: "m/44'/690'/0'/0'/1'"
      })