๐ BIP39 - Mnemonic Code for Cryptocurrency Wallets
A comprehensive, production-ready Rust implementation of the BIP39 standard for generating deterministic keys in cryptocurrency wallets.
๐ Overview
BIP39 (Bitcoin Improvement Proposal 39) defines a method for creating mnemonic phrases (12-24 words) that can be used to generate deterministic cryptocurrency wallet keys. This implementation provides a safe, ergonomic, and fully-tested Rust API.
โจ Features
- โ Full BIP39 Compliance - Implements the complete BIP39 specification
- โ Multi-Language Support - 9 languages (English, Japanese, Korean, Spanish, French, Italian, Czech, Portuguese, Chinese Simplified)
- โ Type-Safe API - Leverages Rust's type system for safety
- โ Comprehensive Testing - 149 tests including unit, doc, and integration tests
- โ Cryptographically Secure - Uses system CSPRNG for entropy generation
- โ Zero Unsafe Code - Pure safe Rust implementation
- โ Well Documented - Extensive documentation and examples
๐ Quick Start
Installation
Add to your Cargo.toml:
[]
= "0.2.0"
Or via cargo:
Basic Usage
use ;
// Generate a new 12-word mnemonic
let mnemonic = generate?;
// Display the phrase to the user (they should write it down!)
println!;
// Generate a cryptographic seed for key derivation
let seed = mnemonic.to_seed?;
// Use seed for BIP32 key derivation...
๐ Usage Examples
Creating a New Wallet
use ;
// Generate a new mnemonic with 24 words (highest security)
let mnemonic = generate?;
// Show the phrase to the user
println!;
println!;
// Generate seed with passphrase for additional security
let seed = mnemonic.to_seed?;
println!;
Recovering a Wallet
use ;
// User enters their recovery phrase
let phrase = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
// Parse and validate the phrase
let mnemonic = from_phrase?;
// Regenerate the seed (must use same passphrase!)
let seed = mnemonic.to_seed?;
// Now derive keys from the seed...
Creating from Known Entropy
use ;
// From hardware wallet or external entropy source
let entropy = ; // 256 bits = 24 words
// Create mnemonic from entropy
let mnemonic = new?;
println!;
println!;
Multi-Language Support
use ;
// Generate Japanese mnemonic
let mnemonic_ja = generate?;
println!;
// Generate Spanish mnemonic
let mnemonic_es = generate?;
println!;
All Word Count Options
use ;
// 12 words = 128 bits entropy (standard)
let m12 = generate?;
// 15 words = 160 bits entropy
let m15 = generate?;
// 18 words = 192 bits entropy
let m18 = generate?;
// 21 words = 224 bits entropy
let m21 = generate?;
// 24 words = 256 bits entropy (maximum security)
let m24 = generate?;
Validating a Phrase
use ;
let phrase = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
// Validate the phrase
match validate_phrase_in_language
Using Utility Functions
use ;
// Generate a phrase directly
let phrase = generate_mnemonic?;
// Validate it
validate_phrase?;
// Generate seed from phrase
let seed = phrase_to_seed?;
๐๏ธ Architecture
Core Types
-
Mnemonic- Main struct representing a BIP39 mnemonicnew(entropy, language)- Create from raw entropyfrom_phrase(phrase, language)- Parse existing phrasegenerate(word_count, language)- Generate random mnemonicphrase()- Get the mnemonic phraseentropy()- Get the entropy bytesto_seed(passphrase)- Generate cryptographic seed
-
WordCount- Type-safe word count enum (12, 15, 18, 21, 24) -
Language- Supported languages enum -
Error- Comprehensive error types with helpful messages
Module Structure
bip39/
โโโ src/
โ โโโ lib.rs # Public API exports
โ โโโ error.rs # Error types
โ โโโ language.rs # Language enum
โ โโโ word_count.rs # WordCount enum
โ โโโ mnemonic.rs # Core Mnemonic struct
โ โโโ utils.rs # Utility functions
โโโ tests/
โ โโโ integration_tests.rs # Integration tests
โโโ benches/
โโโ benchmarks.rs # Performance benchmarks
๐ Security Considerations
โ ๏ธ Important Security Notes
-
Entropy Generation: This library uses the system's cryptographically secure random number generator (
rand::thread_rng()). Ensure your system's RNG is properly seeded. -
Mnemonic Storage:
- Never store mnemonics in plain text
- Never log or transmit mnemonics over insecure channels
- Users should write down phrases on paper and store securely
-
Passphrase Security:
- Passphrases add a "25th word" for additional security
- If lost, the wallet cannot be recovered even with correct mnemonic
- Use strong, memorable passphrases
-
Memory Safety:
- Seeds and entropy should be zeroed after use in production
- Consider using
zeroizecrate for sensitive data
๐ก๏ธ Best Practices
use ;
// โ DO: Generate with maximum entropy
let mnemonic = generate?;
// โ DO: Use passphrases for additional security
let seed = mnemonic.to_seed?;
// โ DON'T: Use weak entropy sources
// โ DON'T: Store mnemonics in application state
// โ DON'T: Log or transmit mnemonics
๐งช Testing
The crate includes comprehensive test coverage:
# Run all tests
# Run with output
# Run only unit tests
# Run only integration tests
# Run only doc tests
# Run benchmarks
Test Statistics
- Unit Tests: 138 tests
- Doc Tests: 35 tests
- Integration Tests: 11 tests
- Total: 184 tests, all passing โ
โก Performance
Benchmarks on Apple M1 (example):
generate_mnemonic_12_words ~500 ยตs
generate_mnemonic_24_words ~800 ยตs
from_phrase ~100 ยตs
to_seed (2048 iterations) ~15 ms
validate_phrase ~50 ยตs
Run benchmarks yourself:
๐ค Contributing
Contributions are welcome! Please ensure:
- All tests pass:
cargo test - Code is formatted:
cargo fmt - No clippy warnings:
cargo clippy - Documentation is updated
- Add tests for new features
๐ License
This project is dual-licensed under:
- MIT License (LICENSE-MIT or http://opensource.org/licenses/MIT)
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
You may choose either license for your use.
๐ References
๐ฎ Support
For bugs, questions, or feature requests, please open an issue on the repository.
โ ๏ธ Disclaimer: This library handles sensitive cryptographic material. Use at your own risk. Always audit cryptographic code before using in production.