Wallets serve as the gateway for many users entering the world of Ethereum or other virtual currencies. Whether through mobile or browser-based wallets, most encounter a series of unfamiliar yet crucial words (often requiring backup). These terms originate from Bitcoin's wallet design, with such wallets typically referred to as HD Wallets. This article briefly explains the architecture of HD Wallets and demonstrates how to create an Ethereum HD Wallet from scratch using JavaScript libraries.
What Is a Virtual Currency Wallet?
While traditional wallets store physical cash, virtual currency wallets operate differently. Account information (such as balances) resides on the blockchain, while the wallet stores the key associated with the account. This key allows users to:
- Prove identity in the virtual currency ecosystem.
- Modify account state (e.g., sending funds to others).
Thus, a virtual currency wallet is essentially a tool for managing and storing cryptographic keys. The private key secures the account, while the public key derives the account address.
Core Protocols: BIP32, BIP39, BIP44
BIP (Bitcoin Improvement Proposals) are documents proposing new features or improvements for Bitcoin. Anyone can submit them, with approved versions published on bitcoin/bips. BIPs relate to Bitcoin as RFCs do to the internet.
BIP32, BIP39, and BIP44 collectively define the widely used HD Wallet standard, covering design principles, implementation methods, and use cases.
1. BIP32: Hierarchical Deterministic Wallets
- Generates a tree-like structure of keypairs (private/public keys) from a single seed.
Benefits:
- Easy backup/transfer (seed-only requirement).
- Hierarchical access control.
2. BIP39: Mnemonic Phrases
- Represents the seed as a human-readable phrase (12 or 24 words).
- Example:
rose rocket invest real refuse margin festival danger anger border idle brown
3. BIP44: Multi-Currency and Multi-Account Support
Extends BIP32 with standardized paths:
m / purpose' / coin_type' / account' / change / address_indexpurpose'is fixed as44'for BIP44.coin_type'identifies currencies (e.g.,0'for Bitcoin,60'for Ethereum).
Ethereum HD Wallet Paths
Ethereum adopts Bitcoin’s HD Wallet framework, assigning coin_type' as 60' (discussed in EIPs #84). For example:
- First account’s first keypair path:
m/44'/60'/0'/0/0.
Creating an Ethereum HD Wallet
JavaScript Libraries Used:
- bip39: Implements BIP39 (mnemonic generation).
- ethereumjs-wallet: Manages keypairs (HD Wallet support via
hdkey). - ethereumjs-util: Ethereum utility functions.
Step-by-Step Implementation
1. Install Libraries
npm install bip39 ethereumjs-wallet ethereumjs-util --save2. Initialize
const bip39 = require('bip39');
const hdkey = require('ethereumjs-wallet/hdkey');
const util = require('ethereumjs-util');3. Generate Mnemonic
const mnemonic = bip39.generateMnemonic();
// Example output: "rose rocket invest real refuse margin festival danger anger border idle brown"4. Create HD Wallet
const seed = bip39.mnemonicToSeedSync(mnemonic);
const hdWallet = hdkey.fromMasterSeed(seed);5. Derive First Ethereum Address
const path = "m/44'/60'/0'/0/0";
const key1 = hdWallet.derivePath(path);
const address1 = util.pubToAddress(key1._hdkey._publicKey, true);
// Apply EIP55 checksum encoding
const checksumAddress = util.toChecksumAddress(address1.toString('hex'));
// Output: "0x685ce4CbDd5c19b64CA008cB85b83947e5318EFA"6. Verify with Mnemonic Code Converter
Using Ethereum HD Wallets
Store the mnemonic securely to create a cold wallet (offline storage). Supported wallets include:
- Browser: MyEtherWallet, MetaMask.
- Mobile: imToken.
👉 Explore secure wallet options
FAQ
Q1: Is the mnemonic phrase enough to recover all keys?
A1: Yes! The phrase generates the seed, which derives all hierarchical keys.
Q2: How does MetaMask store mnemonics securely?
A2: MetaMask encrypts the mnemonic using a user-defined password and stores it in localStorage. Decryption requires the password (Open-source tool).
Q3: Can I use the same HD Wallet for multiple cryptocurrencies?
A3: Yes, via BIP44’s coin_type (e.g., 60' for Ethereum).
References
Additional Ethereum JavaScript Libraries
Special thanks to Jiyi for cryptographic expertise.