rustywallet-mnemonic
BIP39 mnemonic (seed phrase) generation and management for cryptocurrency wallets with secure memory handling and comprehensive validation.
Features
- Mnemonic Generation: Generate cryptographically secure 12, 15, 18, 21, or 24-word mnemonics
- Validation: Complete mnemonic validation including checksum verification and wordlist compliance
- Seed Derivation: PBKDF2-HMAC-SHA512 seed derivation with optional passphrase support
- Passphrase Support: BIP39 "25th word" passphrase functionality for enhanced security
- Memory Security: Automatic zeroization of sensitive data on drop
- Standard Compliance: Full BIP39 specification compliance with English wordlist
- Integration Ready: Seamless integration with rustywallet-keys for key derivation
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Quick Start
use *;
// Generate a new 12-word mnemonic
let mnemonic = generate;
println!;
// Derive seed with passphrase
let seed = mnemonic.to_seed;
println!;
Mnemonic Generation
Generate Different Word Counts
use *;
// 12-word mnemonic (128-bit entropy)
let mnemonic_12 = generate;
// 24-word mnemonic (256-bit entropy) - recommended for maximum security
let mnemonic_24 = generate;
// Other supported lengths
let mnemonic_15 = generate;
let mnemonic_18 = generate;
let mnemonic_21 = generate;
Parse Existing Mnemonic
let phrase = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
let mnemonic = from_phrase?;
Seed Derivation
With Passphrase (BIP39 "25th word")
let mnemonic = generate;
// Derive seed with passphrase for enhanced security
let seed_with_passphrase = mnemonic.to_seed;
// Different passphrases produce different seeds
let seed_different = mnemonic.to_seed;
Without Passphrase
// Standard seed derivation without passphrase
let seed = mnemonic.to_seed;
// or use the convenience method
let seed_normalized = mnemonic.to_seed_normalized;
Passphrase Support
Passphrases provide an additional layer of security by acting as a "25th word":
let mnemonic = from_phrase?;
// Each passphrase creates a different wallet
let wallet_1 = mnemonic.to_seed;
let wallet_2 = mnemonic.to_seed;
let wallet_3 = mnemonic.to_seed; // No passphrase
// All three seeds will be completely different
assert_ne!;
assert_ne!;
Validation
Mnemonic Validation
// Validate mnemonic phrase
let valid_phrase = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
assert!;
// Invalid checksum
let invalid_phrase = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon";
assert!;
// Invalid word
let invalid_word = "invalid abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
assert!;
Word Count Validation
// Check if phrase has correct word count
let phrase = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
let word_count = phrase.split_whitespace.count;
assert_eq!;
API Reference
Core Types
Mnemonic
The main mnemonic type with secure memory handling.
WordCount
Supported mnemonic lengths.
Seed
Derived seed with secure memory handling.
BIP39 Specification Compliance
| Word Count | Entropy Bits | Checksum Bits | Total Bits | Security Level |
|---|---|---|---|---|
| 12 | 128 | 4 | 132 | Standard |
| 15 | 160 | 5 | 165 | Enhanced |
| 18 | 192 | 6 | 198 | High |
| 21 | 224 | 7 | 231 | Very High |
| 24 | 256 | 8 | 264 | Maximum |
Error Handling
use MnemonicError;
match from_phrase
Security Considerations
- Entropy Source: Uses
OsRngfor cryptographically secure random number generation - Memory Safety: All sensitive data (entropy, seeds) is automatically zeroized on drop
- Debug Protection: Debug output is masked to prevent accidental exposure in logs
- PBKDF2 Parameters: Uses 2048 iterations with HMAC-SHA512 as per BIP39 specification
- Constant-Time Operations: Checksum validation uses constant-time comparison
Integration with rustywallet-keys
use *;
use *;
// Generate mnemonic and derive keys
let mnemonic = generate;
let private_key = mnemonic.to_private_key?;
// Derive public key and address
let public_key = private_key.public_key;
let address = public_key.to_address;
println!;
Examples
See the examples/ directory for complete usage examples:
generate.rs- Basic mnemonic generationvalidation.rs- Mnemonic validation examplespassphrase.rs- Passphrase usage patternsintegration.rs- Integration with other rustywallet crates
Contributing
Contributions are welcome! Please read our Contributing Guidelines and Code of Conduct.
License
This project is licensed under the MIT License - see the LICENSE file for details.