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

    Constructors

    Methods

    Constructors

    Methods

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