Class WalletFactory
Implements
- WalletFactory
Index
Constructors
Methods
Constructors
constructor
Create a new WalletFactory instance
Parameters
Optionalprovider: IProviderOptional provider instance. If not provided, creates a default KleverProvider
Returns WalletFactory
Methods
createRandom
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: IProviderOptional custom provider (defaults to factory's provider)
Returns Promise<Wallet>
A promise that resolves to a new Wallet instance
createWallet
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: WalletConfigOptional 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
fromEncryptedJson
- fromEncryptedJson(
json: string | Keystore,
password: string,
provider?: IProvider,
): Promise<Wallet>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: IProviderOptional custom provider (defaults to factory's provider)
Returns Promise<Wallet>
A promise that resolves to a new Wallet instance
fromMnemonic
- fromMnemonic(
mnemonic: string,
provider?: IProvider,
options?: MnemonicToKeyOptions,
): Promise<Wallet>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: IProviderOptional custom provider (defaults to factory's provider)
Optionaloptions: MnemonicToKeyOptionsOptional derivation path and passphrase
Returns Promise<Wallet>
A promise that resolves to a new Wallet instance
Example
const factory = new WalletFactory()
const wallet = await factory.fromMnemonic('abandon abandon abandon...')
await wallet.connect()
console.log('Address:', wallet.address)
mnemonicToPrivateKey
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: MnemonicToKeyOptionsOptional derivation path and passphrase
Returns string
The private key as a hexadecimal string
- mnemonic: string
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:
Benefits:
Example
Example
Example