khodpay-bip44
Production-ready Rust implementation of BIP-44 (Multi-Account Hierarchy for Deterministic Wallets).
Features
- ✅ Multi-Account Support - Manage multiple accounts per cryptocurrency
- ✅ Multi-Coin Support - Bitcoin, Ethereum, Litecoin, Dogecoin, and more
- ✅ BIP Standards - Support for BIP-44, BIP-49, BIP-84, and BIP-86
- ✅ Account Caching - Efficient account derivation with built-in caching
- ✅ Builder Pattern - Fluent API for wallet construction
- ✅ Type Safety - Strong typing for paths, chains, and coin types
- ✅ Serialization - Optional serde support for persistence
- ✅ No Unsafe Code - 100% safe Rust
- ✅ Comprehensive Tests - 400+ tests including integration and edge cases
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
= "0.2.0"
= "0.2.0"
For serialization support:
[]
= { = "0.1.0", = ["serde"] }
Quick Start
use ;
use Network;
// Create a wallet from a mnemonic
let mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
let mut wallet = from_mnemonic?;
// Get the first Bitcoin account
let account = wallet.get_account?;
// Derive receiving addresses
let addr0 = account.derive_external?; // m/44'/0'/0'/0/0
let addr1 = account.derive_external?; // m/44'/0'/0'/0/1
// Derive change addresses
let change0 = account.derive_internal?; // m/44'/0'/0'/1/0
BIP-44 Path Structure
BIP-44 defines a logical hierarchy for deterministic wallets:
m / purpose' / coin_type' / account' / change / address_index
Path Levels
| Level | Hardened | Description | Example |
|---|---|---|---|
| purpose | Yes (') | BIP standard (44, 49, 84, 86) | 44' |
| coin_type | Yes (') | Cryptocurrency type (SLIP-44) | 0' (Bitcoin) |
| account | Yes (') | Account index | 0' |
| change | No | 0=external (receiving), 1=internal (change) | 0 |
| address_index | No | Address index within chain | 0 |
Example Paths
- Bitcoin receiving:
m/44'/0'/0'/0/0 - Bitcoin change:
m/44'/0'/0'/1/0 - Ethereum receiving:
m/44'/60'/0'/0/0 - Bitcoin SegWit:
m/84'/0'/0'/0/0 - Bitcoin Taproot:
m/86'/0'/0'/0/0
Usage Examples
Multi-Coin Wallet
use ;
let mut wallet = from_english_mnemonic?;
// Bitcoin account
let btc = wallet.get_account?;
let btc_addr = btc.derive_external?;
// Ethereum account
let eth = wallet.get_account?;
let eth_addr = eth.derive_external?;
// Litecoin account
let ltc = wallet.get_account?;
let ltc_addr = ltc.derive_external?;
Builder Pattern
use WalletBuilder;
let mut wallet = new
.mnemonic
.password
.language
.network
.build?;
Batch Address Generation
use Chain;
let account = wallet.get_account?;
// Generate 20 receiving addresses at once
let addresses = account.derive_address_range?;
// Generate 10 change addresses
let change_addresses = account.derive_address_range?;
Path String Parsing
use Bip44Path;
// Parse a BIP-44 path
let path: Bip44Path = "m/44'/0'/0'/0/0".parse?;
// Access path components
println!;
println!;
println!;
println!;
println!;
// Convert back to string
assert_eq!;
SegWit and Taproot
// BIP-84: Native SegWit (bc1q... addresses)
let segwit = wallet.get_account?;
let segwit_addr = segwit.derive_external?;
// BIP-86: Taproot (bc1p... addresses)
let taproot = wallet.get_account?;
let taproot_addr = taproot.derive_external?;
Account Caching
// First access - derives and caches
let account1 = wallet.get_account?;
// Second access - uses cached account (faster)
let account2 = wallet.get_account?;
// Check cache size
println!;
// Clear cache if needed
wallet.clear_cache;
Address Iterator
use AddressIterator;
let account = wallet.get_account?;
// Iterate over addresses
for in new_external.take.enumerate
Supported BIP Standards
| Standard | Purpose | Address Type | Example |
|---|---|---|---|
| BIP-44 | 44' | Legacy P2PKH | 1... |
| BIP-49 | 49' | SegWit wrapped in P2SH | 3... |
| BIP-84 | 84' | Native SegWit (Bech32) | bc1q... |
| BIP-86 | 86' | Taproot (Bech32m) | bc1p... |
Supported Cryptocurrencies
This crate supports all SLIP-44 registered coin types:
| Cryptocurrency | Coin Type | Enum |
|---|---|---|
| Bitcoin | 0 | CoinType::Bitcoin |
| Bitcoin Testnet | 1 | CoinType::BitcoinTestnet |
| Litecoin | 2 | CoinType::Litecoin |
| Dogecoin | 3 | CoinType::Dogecoin |
| Dash | 5 | CoinType::Dash |
| Ethereum | 60 | CoinType::Ethereum |
| Ethereum Classic | 61 | CoinType::EthereumClassic |
| Custom | Any | CoinType::Custom(u32) |
Security Considerations
⚠️ Critical Security Guidelines
-
Mnemonic Storage
- ❌ Never store mnemonics in plain text
- ❌ Never log or print mnemonics
- ✅ Use secure storage (encrypted keystore, hardware wallet)
- ✅ Use strong passwords for BIP-39 passphrases
-
Key Material Handling
- ❌ Never expose private keys over network
- ❌ Never store private keys in databases
- ✅ Keep keys in secure memory
- ✅ Clear sensitive data after use
-
Password Protection
- ✅ Use BIP-39 passphrase for additional security
- ✅ Use strong, unique passwords
- ✅ Consider using a password manager
-
Gap Limit
- ✅ Follow BIP-44 gap limit (20 addresses)
- ✅ Stop scanning after 20 consecutive unused addresses
- ✅ Use
AccountDiscoverytrait for wallet recovery
-
Network Security
- ✅ Use testnet for development
- ✅ Verify addresses before sending funds
- ✅ Double-check network parameter
Best Practices
// ✅ Good: Use password protection
let wallet = from_mnemonic?;
// ✅ Good: Clear sensitive data
drop; // Clears cached accounts
// ✅ Good: Use testnet for development
let test_wallet = from_english_mnemonic?;
Account Discovery
For wallet recovery, use the gap limit to discover used accounts:
use ;
// Implement AccountDiscovery trait for your blockchain client
;
// Scan for used addresses
let blockchain = MyBlockchain;
let checker = new;
let account = wallet.get_account?;
let result = checker.scan_chain?;
println!;
println!;
Serialization
Enable the serde feature for serialization support:
use ;
// Serialize path
let path = new?;
let json = to_string?;
// Deserialize path
let parsed: Bip44Path = from_str?;
// Serialize account metadata (safe - no private keys)
let metadata = from_account;
let metadata_json = to_string?;
Error Handling
use Error;
match wallet.get_account
Testing
Run the test suite:
# Run all tests
# Run with serde feature
# Run doc tests
# Run integration tests
Performance
- Account Caching: Repeated access to the same account is O(1)
- Batch Derivation: More efficient than deriving addresses one by one
- Zero-Copy: Path parsing and formatting minimize allocations
Compatibility
This implementation is compatible with:
- ✅ Electrum - Standard BIP-44 paths
- ✅ Ledger - Hardware wallet paths
- ✅ Trezor - Hardware wallet paths
- ✅ MetaMask - Ethereum account derivation
- ✅ Trust Wallet - Multi-coin support
- ✅ Exodus - Standard BIP-44 implementation
Examples
See the examples directory for complete working examples:
basic.rs- Basic wallet usagemulti_coin.rs- Multi-cryptocurrency walletmulti_account.rs- Multiple accounts per coindiscovery.rs- Account discovery with gap limit
Contributing
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
References
- BIP-44 Specification
- BIP-32 Specification
- BIP-39 Specification
- SLIP-44 Coin Types
- BIP-49 Specification
- BIP-84 Specification
- BIP-86 Specification
Changelog
See CHANGELOG.md for version history.