WalletHelper API
Utilities for wallet and key management.
Import
import { WalletHelper } from 'cardano-devkit';
generateSeed()
Generate a new BIP-39 mnemonic seed phrase.
const seed = WalletHelper.generateSeed(wordCount);
Parameters
wordCount?: 12 | 15 | 18 | 21 | 24- Number of words (default: 24)
Returns
string - Space-separated mnemonic words
Example
// 24-word seed (default, most secure)
const seed24 = WalletHelper.generateSeed();
// "abandon abandon ... about"
// 12-word seed
const seed12 = WalletHelper.generateSeed(12);
validateSeed()
Validate a BIP-39 mnemonic seed phrase.
const isValid = WalletHelper.validateSeed(seed);
Parameters
seed: string- Mnemonic seed phrase
Returns
boolean - True if valid
Example
const valid = WalletHelper.validateSeed(
"abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"
);
console.log(valid); // true
const invalid = WalletHelper.validateSeed("not a valid seed");
console.log(invalid); // false
seedToPrivateKey()
Convert a seed phrase to a private key.
const privateKey = WalletHelper.seedToPrivateKey(seed);
Parameters
seed: string- BIP-39 mnemonic seed phrase
Returns
string - Ed25519 private key (bech32 encoded)
Example
const seed = WalletHelper.generateSeed();
const privateKey = WalletHelper.seedToPrivateKey(seed);
// "ed25519_sk1..."
// Use with Lucid
lucid.selectWallet.fromPrivateKey(privateKey);
privateKeyToPublicKey()
Derive a public key from a private key.
const publicKey = WalletHelper.privateKeyToPublicKey(privateKey);
Parameters
privateKey: string- Ed25519 private key
Returns
string - Ed25519 public key (bech32 encoded)
privateKeyToAddress()
Derive an address from a private key.
const address = await WalletHelper.privateKeyToAddress(privateKey, network);
Parameters
privateKey: string- Ed25519 private keynetwork: NetworkType- Target network
Returns
Promise<string> - Bech32 address
Example
const address = await WalletHelper.privateKeyToAddress(
privateKey,
'Preprod'
);
// "addr_test1qz..."
seedToAddress()
Derive an address directly from a seed phrase.
const address = await WalletHelper.seedToAddress(seed, network);
Parameters
seed: string- BIP-39 mnemonic seed phrasenetwork: NetworkType- Target network
Returns
Promise<string> - Bech32 address
Example
const seed = WalletHelper.generateSeed();
const address = await WalletHelper.seedToAddress(seed, 'Preprod');
createHDWallet()
Create an HD wallet for deriving multiple addresses.
const hdWallet = WalletHelper.createHDWallet(seed);
Parameters
seed: string- BIP-39 mnemonic seed phrase
Returns
HDWallet instance
Example
const hdWallet = WalletHelper.createHDWallet(seed);
// Derive addresses
const addr0 = await hdWallet.deriveAddress(0, 'Preprod');
const addr1 = await hdWallet.deriveAddress(1, 'Preprod');
const addr2 = await hdWallet.deriveAddress(2, 'Preprod');
formatLovelace()
Format lovelace amount to ADA string.
const formatted = WalletHelper.formatLovelace(lovelace, decimals);
Parameters
lovelace: bigint- Amount in lovelacedecimals?: number- Decimal places (default: 6)
Returns
string - Formatted ADA amount
Example
WalletHelper.formatLovelace(1_500_000n);
// "1.5"
WalletHelper.formatLovelace(1_500_000n, 2);
// "1.50"
WalletHelper.formatLovelace(1_234_567_890n);
// "1234.56789"
parseAda()
Parse ADA string to lovelace.
const lovelace = WalletHelper.parseAda(ada);
Parameters
ada: string | number- ADA amount
Returns
bigint - Amount in lovelace
Example
WalletHelper.parseAda('1.5');
// 1_500_000n
WalletHelper.parseAda(10);
// 10_000_000n
WalletHelper.parseAda('0.123456');
// 123_456n
Type Definitions
HDWallet
interface HDWallet {
deriveAddress: (index: number, network: NetworkType) => Promise<string>;
derivePrivateKey: (index: number) => string;
derivePublicKey: (index: number) => string;
}
Usage Patterns
Create and Fund a Wallet
import { createDevKit, WalletHelper, createDevnetManager } from 'cardano-devkit';
// Generate wallet
const seed = WalletHelper.generateSeed();
console.log('Save this seed:', seed);
// Get address
const devKit = createDevKit({ network: 'LocalDevnet' });
const lucid = await devKit.init();
lucid.selectWallet.fromSeed(seed);
const address = await lucid.wallet().address();
// Fund on local devnet
const devnet = createDevnetManager();
await devnet.topup(address, 100_000_000n);
Multiple Accounts
const seed = WalletHelper.generateSeed();
const hdWallet = WalletHelper.createHDWallet(seed);
// Create 5 addresses
const addresses = await Promise.all(
[0, 1, 2, 3, 4].map(i => hdWallet.deriveAddress(i, 'Preprod'))
);
console.log('Addresses:', addresses);
Secure Key Handling
// ✅ Good: Keep seeds in memory only when needed
async function signTransaction(seed: string, tx: Transaction) {
const privateKey = WalletHelper.seedToPrivateKey(seed);
lucid.selectWallet.fromPrivateKey(privateKey);
const signed = await tx.sign.withWallet().complete();
// privateKey goes out of scope
return signed;
}
// ❌ Bad: Don't store seeds in plain text
localStorage.setItem('seed', seed); // Never do this!