Relationship Between Seed Phrase, Private Key, BIP39, and BIP32
BIP39 and BIP32 work together to create a secure and efficient method for managing cryptocurrency wallets. Instead of directly using a private key, which is difficult to back up safely, BIP39 introduces a mnemonic seed phrase—a sequence of 12 or 24 words—that can regenerate the private key. This makes it easier for users to store and recover their wallets.
The mnemonic seed phrase is converted into a 512-bit seed, which serves as the foundation for generating a master private key using BIP32. From this master private key, a hierarchical deterministic (HD) wallet structure is created, allowing users to derive multiple child keys in a secure and organized manner. This structure enhances security and usability, enabling features like watch-only wallets and account segregation.
By understanding how BIP39 generates seed phrases and how BIP32 derives hierarchical keys from them, users can safely store and manage their cryptocurrency assets.
BIP39: Mnemonic Code for Generating Seed
Step 1: Generate Random Entropy
A secure random number is generated, typically 128 to 256 bits in length. This entropy forms the basis of the mnemonic phrase.
Example:
Suppose we generate a 128-bit entropy value:
1101101000110110111010001011011101110101010000101010111010101001
Step 2: Create a Checksum
A checksum is derived from the entropy by taking its SHA-256 hash and appending the first few bits (based on the entropy size) to the original entropy.
Example:
The SHA-256 hash of the entropy might be:
5f1d3c8b4a7d23f8...
Taking the first 4 bits 0101 (in case of 128-bit entropy) and appending it gives:
11011010001101101110100010110111011101010100001010101110101010010101
Step 3: Convert to Mnemonic Phrase
The resulting bit sequence is split into 11-bit chunks, and each chunk is mapped to a predefined wordlist of 2048 words. This produces a mnemonic phrase (e.g., 12 or 24 words), which acts as a human-readable backup.
Example:
Mapping our bits to words from the standard wordlist might give:
"kite zebra ladder boost tent guitar lunar fox apple rain drift solid"
Step 4: Derive the Seed
The mnemonic phrase is then used to generate a 512-bit seed using the PBKDF2 function with HMAC-SHA512. This seed can be optionally secured with a passphrase for additional protection.
Example:
Using the phrase above with no passphrase, we get a seed:
7df2b94e5a2f8d... (512-bit hexadecimal value)
BIP32: Hierarchical Deterministic (HD) Wallets
Step 1: Generate the Master Key Pair
The 512-bit seed from BIP39 is hashed using HMAC-SHA512 with the key "Bitcoin seed." The output is split into two 256-bit values:
Master Private Key
Master Chain Code (used for deterministic key derivation)
Example:
Applying HMAC-SHA512 gives:
Master Private Key: 123abc456def...
Master Chain Code: 789xyz101ghi...
Step 2: Derive Child Keys
BIP32 defines a tree-like structure where each node (key) can have child nodes. There are two types of derivation:
Normal (Non-Hardened) Keys: Derived using both the parent public key and chain code.
Hardened Keys: Derived using the parent private key and chain code, preventing exposure of the entire tree if a child private key is compromised.
Each child key is derived using HMAC-SHA512 with the parent key and chain code as inputs.
Example:
For a hardened child key at index 0x80000000
, we derive:
Child Private Key: abc789def456...
Step 3: Generate Extended Keys
Extended keys (xpub/xprv) contain both key material and the chain code, allowing wallets to derive new addresses deterministically. Public extended keys (xpub) enable generating receiving addresses without access to private keys, making them useful for watch-only wallets.
Example:
From our master key:
xprv: xprv9s21ZrQH143...
xpub: xpub661MyMwAqRz...
Conclusion
BIP39 and BIP32 together provide a powerful way to manage cryptocurrency wallets. BIP39 ensures easy backup and recovery, while BIP32 enables hierarchical, deterministic key generation. Understanding these processes helps enhance security and usability in crypto applications.
By following this step-by-step breakdown, you now have a solid grasp of how mnemonic phrases transform into deterministic wallet structures!