Understanding HD Wallets
An HD (Hierarchical Deterministic) Wallet is a widely adopted standard for cryptocurrency wallets. It generates all keys from a single root seed (a 128–256-bit random number). This means:
- Any compatible HD wallet can be recreated from its root seed.
- Owning the root seed grants access to all derived keys, simplifying backup and recovery.
Key Standards: BIP32, BIP39, BIP44
BIP32
Defines the structure for Hierarchical Deterministic Wallets, enabling a single seed to generate a tree of key pairs. Benefits include:
- Easy backup (only the seed is needed).
- Hierarchical access control.
BIP39
Converts the seed into a mnemonic phrase (12–24 words) for human-friendly storage. Example: "clump hurdle spatial aware solution trim dish frown miracle sword awkward milk"
BIP44
Extends BIP32 to support multi-currency accounts with this path format: m / purpose' / coin_type' / account' / change / address_index
- purpose': Fixed as
44'(for BIP44). - coin_type': Specifies the cryptocurrency (e.g.,
0'for BTC,60'for ETH). - change:
0for external addresses (receiving),1for internal (change).
Generating Mnemonic, Private Keys, and Addresses
1. HD Wallet Mnemonic
Use either ethers.js or bip39:
// Using ethers.js
const mnemonic = ethers.Wallet.createRandom().mnemonic.phrase;
// Using bip39
const mnemonic = bip39.generateMnemonic(); 2. BTC Wallet
Dependencies: bitcoinjs-lib, bip32, bip39
const network = bitcoin.networks.bitcoin;
const seed = bip39.mnemonicToSeedSync(mnemonic);
const root = bip32.fromSeed(seed, network);
const path = "m/44'/0'/0'/0/0"; // BIP44 path for BTC
const keyPair = root.derivePath(path);
// Private Key (WIF format)
const privateKey = keyPair.toWIF();
// Public Key
const publicKey = keyPair.publicKey.toString("hex");
// Addresses
const legacyAddress = bitcoin.payments.p2pkh({
pubkey: keyPair.publicKey,
network: network
}).address;
const segwitAddress = bitcoin.payments.p2sh({
redeem: bitcoin.payments.p2wpkh({ pubkey: keyPair.publicKey, network }),
network
}).address; 3. ETH Wallet
Dependencies: ethers.js or ethereumjs-wallet
// Using ethers.js
const wallet = ethers.Wallet.fromMnemonic(mnemonic);
console.log("ETH Private Key:", wallet.privateKey);
console.log("ETH Address:", wallet.address);
// Using ethereumjs-wallet
const hdwallet = hdkey.fromMasterSeed(seed);
const path = "m/44'/60'/0'/0/0"; // BIP44 path for ETH
const keyPair = hdwallet.derivePath(path);
const privateKey = keyPair.getWallet().getPrivateKeyString();
const address = keyPair.getWallet().getAddressString(); 4. EOS Wallet
Dependency: eosjs-ecc
const privateKey = eosjs_ecc.seedPrivate(mnemonic);
const publicKey = eosjs_ecc.privateToPublic(privateKey);
console.log("EOS Private Key:", privateKey);
console.log("EOS Public Key:", publicKey); FAQs
Q1: Why use an HD wallet?
HD wallets simplify key management—back up just the seed to restore all wallets. They also enhance security with hierarchical keys.
Q2: Are BIP39 mnemonics cross-compatible?
Yes! The same mnemonic can generate keys for BTC, ETH, EOS, and other BIP44-compliant blockchains.
Q3: What’s the difference between legacy and SegWit BTC addresses?
- Legacy: Starts with
1(e.g.,1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa). - SegWit: Starts with
3(e.g.,3FZbgi29cpjq2GjdwV8eyHuJJnkLtktZc5). SegWit reduces transaction fees.
👉 Learn more about advanced wallet security here