BIP32 - Hierarchical Deterministic Wallets
A production-ready Rust implementation of the BIP32 standard for hierarchical deterministic wallets in cryptocurrency applications.
Features
- ✅ Full BIP32 Compliance - Implements the complete BIP32 specification
- ✅ Type-Safe API - Leverages Rust's type system for compile-time safety
- ✅ BIP39 Integration - Seamlessly works with BIP39 mnemonics
- ✅ Hardened & Normal Derivation - Supports both derivation types
- ✅ Network Support - Bitcoin mainnet and testnet
- ✅ Zero Unsafe Code - Pure safe Rust implementation
- ✅ Production Ready - Validated against official test vectors
- ✅ Cross-Compatible - Interoperable with major wallet implementations (Trezor, Ledger, Electrum, Bitcoin Core)
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
= "0.1.0" # For mnemonic support
Quick Start
Basic Usage
use ;
use ;
use FromStr;
// Generate a mnemonic (using BIP39)
let mnemonic = generate?;
// Create master extended private key from mnemonic
let master_key = from_mnemonic?;
// Derive child keys using a BIP-44 path
let path = from_str?;
let account_key = master_key.derive_path?;
println!;
println!;
Derive from Seed
use ;
// Use a seed directly (typically from BIP39)
let seed = b"your-secure-seed-bytes-here-at-least-16-bytes-long";
let master = from_seed?;
// Get the extended public key
let master_pub = master.to_extended_public_key;
println!;
Watch-Only Wallets (Public Key Derivation)
use ;
use FromStr;
let seed = b"your-secure-seed-bytes-here-at-least-16-bytes-long";
let master = from_seed?;
// Derive account-level key (with hardened derivation)
let account_path = from_str?;
let account_key = master.derive_path?;
// Get the extended public key for watch-only wallet
let account_pub = account_key.to_extended_public_key;
// Now derive receive addresses from public key only (no private key needed)
let first_address = account_pub.derive_child?;
println!;
Generate Multiple Addresses
use ;
use FromStr;
let seed = b"your-secure-seed-bytes-here-at-least-16-bytes-long";
let master = from_seed?;
// BIP-44 Bitcoin account 0, external chain (receive addresses)
let path = from_str?;
let receive_chain = master.derive_path?;
// Generate first 5 receiving addresses
for i in 0..5
Common Derivation Paths
Following standard BIP specifications:
| Standard | Path | Purpose |
|---|---|---|
| BIP44 | m/44'/0'/0' |
Multi-account hierarchy for Bitcoin |
| BIP49 | m/49'/0'/0' |
SegWit (P2WPKH-nested-in-P2SH) |
| BIP84 | m/84'/0'/0' |
Native SegWit (P2WPKH) |
BIP-44 Structure
m / purpose' / coin_type' / account' / change / address_index
- purpose' - 44' for BIP-44
- coin_type' - 0' for Bitcoin, 1' for Bitcoin Testnet
- account' - Account index (0' for first account)
- change - 0 for external (receive), 1 for internal (change)
- address_index - Address index (0, 1, 2, ...)
Example: m/44'/0'/0'/0/0 = First receiving address of the first account
API Overview
Core Types
ExtendedPrivateKey- Extended private key with derivation capabilitiesExtendedPublicKey- Extended public key for watch-only walletsDerivationPath- Path specification for key derivationChildNumber- Individual derivation step (normal or hardened)Network- Bitcoin mainnet or testnet configuration
Key Methods
// Master key generation
from_seed?
from_mnemonic?
// Key derivation
master_key.derive_path?
master_key.derive_child?
// Public key conversion
private_key.to_extended_public_key
// Serialization
key.to_string // Base58Check format (xprv/xpub)
from_str?
Security Considerations
🔐 Important Security Guidelines:
- Use Cryptographically Secure Seeds - Always use proper random number generators
- Protect Private Keys - Store private keys and seeds securely
- Use Hardened Derivation - Use hardened derivation (
'orH) for account-level keys - Never Expose Seeds - Never transmit seeds or private keys over insecure channels
- Zeroize Sensitive Data - This library uses
zeroizeto clear sensitive data from memory
Hardened vs Normal Derivation
- Hardened (
m/44'/0'/0'): More secure, requires private key, prevents parent key exposure - Normal (
m/44'/0'/0'/0/0): Allows public key derivation, useful for watch-only wallets
Best Practice: Use hardened derivation for upper levels (purpose, coin type, account) and normal derivation for address generation.
Compatibility
This implementation is fully compatible with:
- ✅ Hardware Wallets: Trezor, Ledger
- ✅ Software Wallets: Electrum, Bitcoin Core, Bitpay/Bitcore
- ✅ Standards: BIP32, BIP39, BIP44, BIP49, BIP84
- ✅ Libraries: btcsuite/btcutil (Go), bitcoinjs-lib (JavaScript)
All keys are interoperable and can be imported/exported across different wallet implementations.
Testing
This library includes comprehensive test coverage:
# Run all tests
# Run with output
# Run specific test
# Run doc tests
Test coverage includes:
- ✅ Official BIP32 test vectors (all 4 vectors)
- ✅ Cross-compatibility with major implementations
- ✅ Edge cases (leading zeros, maximum indices)
- ✅ Error handling and validation
Documentation
Full API documentation is available at docs.rs/bip32.
Build documentation locally:
Examples
Additional examples can be found in the examples/ directory:
wallet_creation.rs- Complete wallet creation workflowkey_derivation.rs- Various derivation patternswatch_only.rs- Public key derivation for watch-only wallets
Run examples:
Performance
The library is optimized for both performance and security:
- Efficient key derivation using
secp256k1 - Minimal allocations
- Zero-copy operations where possible
- Constant-time operations for sensitive data
Contributing
Contributions are welcome! Please ensure:
- All tests pass:
cargo test - Code is formatted:
cargo fmt - No clippy warnings:
cargo clippy - Documentation is updated
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
Made with ❤️ for the Bitcoin and cryptocurrency community