Source: bip39.zigTests: bip39.test.ts
Try it Live
Run BIP-39 examples in the interactive playground
Overview
BIP39 is a mnemonic seed phrase standard (Bitcoin Improvement Proposal 39) that encodes cryptographic entropy as human-readable words for deterministic wallet key generation. Ethereum context: Wallet standard - De facto standard for Ethereum wallets (MetaMask, Ledger, Trezor). Not part of Ethereum protocol itself, but critical for key management UX. Key operations:- Generate entropy: 128/160/192/224/256 bits of cryptographically secure randomness
- Entropy → mnemonic: Convert to 12/15/18/21/24 words from BIP39 wordlist
- Mnemonic → seed: Derive 512-bit seed via PBKDF2-HMAC-SHA512 (2048 iterations)
- Optional passphrase: Additional security layer for plausible deniability
Quick Start
API Reference
Generation
generateMnemonic(strength?: 128 | 160 | 192 | 224 | 256, wordlist?: string[]): string
Generates a cryptographically secure mnemonic phrase.
Parameters:
strength- Entropy bits (default: 256)- 128 bits = 12 words
- 160 bits = 15 words
- 192 bits = 18 words
- 224 bits = 21 words
- 256 bits = 24 words
wordlist- Optional custom wordlist (default: English)
entropyToMnemonic(entropy: Uint8Array, wordlist?: string[]): string
Converts raw entropy to mnemonic phrase.
Validation
validateMnemonic(mnemonic: string, wordlist?: string[]): boolean
Validates mnemonic phrase (checksum + word existence).
assertValidMnemonic(mnemonic: string, wordlist?: string[]): void
Validates and throws on invalid mnemonic.
Seed Derivation
mnemonicToSeed(mnemonic: string, passphrase?: string): Promise<Uint8Array>
Converts mnemonic to 64-byte seed using PBKDF2 (async, 2048 iterations).
mnemonicToSeedSync(mnemonic: string, passphrase?: string): Uint8Array
Synchronous version of mnemonicToSeed.
Utilities
getWordCount(entropyBits: number): number
Returns word count for given entropy bits.
getEntropyBits(wordCount: number): number
Returns entropy bits for given word count.
Constants
Error Handling
All BIP-39 functions throw typed errors that extendCryptoError:
Error Types
Error Properties
All errors include:name- Error class name (e.g.,"InvalidMnemonicError")message- Human-readable descriptioncode- Machine-readable error codedocsPath- Link to relevant documentationcause- Original error if wrapping another error
Entropy and Word Count
BIP-39 uses entropy + checksum to generate mnemonics:
Formula:
words = (entropy_bits + checksum_bits) / 11
The checksum ensures the last word validates the entire phrase.
Wordlist
BIP-39 uses a standardized 2048-word English wordlist by default:- All words are 3-8 characters
- First 4 letters are unique
- No similar-looking words
- Common, easy-to-spell words
@scure/bip39):
- Chinese (Simplified/Traditional)
- Czech
- French
- Italian
- Japanese
- Korean
- Portuguese
- Spanish
Passphrase (BIP-39 Extension)
An optional passphrase adds an additional security layer:- Plausible deniability: Different passphrases unlock different wallets from same mnemonic
- Additional security: Attacker needs both mnemonic AND passphrase
- Two-factor: Store mnemonic and passphrase separately
PBKDF2 Derivation
BIP-39 uses PBKDF2-HMAC-SHA512 to derive seed from mnemonic:- Slow derivation resists brute-force attacks
- Standardized, widely supported
- 2048 iterations balance security vs performance
Security Considerations
Critical Requirements
1. Mnemonic must be from official BIP39 wordlistBest Practices
1. Never transmit mnemonics unencrypted- Write mnemonic on paper, store in fireproof safe
- Consider metal backups (fire/water resistant)
- Split storage for high-value wallets
- Verify backups by restoring test wallet
- Back up passphrases separately from mnemonic
- Warning: Forgetting passphrase means permanent loss of funds
- No recovery possible without correct passphrase
Common Errors
Invalid Mnemonic
Entropy Length
Integration with HD Wallets
BIP-39 is typically used with BIP-32 (HD Wallets) for deterministic key generation:- Mnemonic (human-readable backup)
- Seed (64 bytes via PBKDF2)
- Root key (master private key)
- Derived keys (unlimited addresses from root)
Implementation Notes
- Uses
@scure/bip39by Paul Miller (audited, widely-used) - PBKDF2-HMAC-SHA512 with 2048 iterations
- NFKD normalization for mnemonic and passphrase
- Constant-time checksum verification
- Support for multiple wordlists
Test Vectors
BIP-39 test vectors for verification:Examples
Comprehensive examples demonstrating BIP-39 functionality:- Generate Mnemonic - Generate mnemonics with different entropy levels (12-24 words)
- Validate Mnemonic - Validate mnemonics and handle errors
- Mnemonic to Seed - Async seed derivation with PBKDF2
- Entropy to Mnemonic - Convert raw entropy to mnemonic
- Sync Derivation - Synchronous seed derivation
- Passphrase Usage - Plausible deniability with passphrases
- Utilities - Word count and entropy calculations
- Full Workflow - Complete wallet creation workflow
Related
- BIP-39 (Effect) - Effect.ts integration with Schema validation
References
- BIP-39 Specification
- English Wordlist
- @scure/bip39 Library
- Ian Coleman BIP-39 Tool (for testing, NOT for real funds)

