| Crates.io | khodpay-bip32 |
| lib.rs | khodpay-bip32 |
| version | 0.2.0 |
| created_at | 2025-10-16 10:09:53.743625+00 |
| updated_at | 2025-10-16 10:09:53.743625+00 |
| description | Production-ready Rust implementation of BIP32 hierarchical deterministic wallets |
| homepage | https://github.com/khodpay/rust-wallet |
| repository | https://github.com/khodpay/rust-wallet |
| max_upload_size | |
| id | 1885709 |
| size | 540,179 |
A production-ready Rust implementation of the BIP32 standard for hierarchical deterministic wallets in cryptocurrency applications.
Add this to your Cargo.toml:
[dependencies]
khodpay-bip32 = "0.1.0"
khodpay-bip39 = "0.1.0" # For mnemonic support
use khodpay_bip32::{ExtendedPrivateKey, Network, DerivationPath};
use khodpay_bip39::{Mnemonic, WordCount, Language};
use std::str::FromStr;
// Generate a mnemonic (using BIP39)
let mnemonic = Mnemonic::generate(WordCount::Twelve, Language::English)?;
// Create master extended private key from mnemonic
let master_key = ExtendedPrivateKey::from_mnemonic(
&mnemonic,
None, // Optional passphrase
Network::BitcoinMainnet
)?;
// Derive child keys using a BIP-44 path
let path = DerivationPath::from_str("m/44'/0'/0'")?;
let account_key = master_key.derive_path(&path)?;
println!("Account xprv: {}", account_key);
println!("Account xpub: {}", account_key.to_extended_public_key());
use khodpay_bip32::{ExtendedPrivateKey, Network};
// Use a seed directly (typically from BIP39)
let seed = b"your-secure-seed-bytes-here-at-least-16-bytes-long";
let master = ExtendedPrivateKey::from_seed(seed, Network::BitcoinMainnet)?;
// Get the extended public key
let master_pub = master.to_extended_public_key();
println!("Master xpub: {}", master_pub);
use khodpay_bip32::{ExtendedPrivateKey, Network, DerivationPath, ChildNumber};
use std::str::FromStr;
let seed = b"your-secure-seed-bytes-here-at-least-16-bytes-long";
let master = ExtendedPrivateKey::from_seed(seed, Network::BitcoinMainnet)?;
// Derive account-level key (with hardened derivation)
let account_path = DerivationPath::from_str("m/44'/0'/0'")?;
let account_key = master.derive_path(&account_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(ChildNumber::Normal(0))?;
println!("First receive address xpub: {}", first_address);
use khodpay_bip32::{ExtendedPrivateKey, Network, DerivationPath, ChildNumber};
use std::str::FromStr;
let seed = b"your-secure-seed-bytes-here-at-least-16-bytes-long";
let master = ExtendedPrivateKey::from_seed(seed, Network::BitcoinMainnet)?;
// BIP-44 Bitcoin account 0, external chain (receive addresses)
let path = DerivationPath::from_str("m/44'/0'/0'/0")?;
let receive_chain = master.derive_path(&path)?;
// Generate first 5 receiving addresses
for i in 0..5 {
let address_key = receive_chain.derive_child(ChildNumber::Normal(i))?;
let address_pub = address_key.to_extended_public_key();
println!("Address {}: {}", i, address_pub);
}
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) |
m / purpose' / coin_type' / account' / change / address_index
Example: m/44'/0'/0'/0/0 = First receiving address of the first account
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// Master key generation
ExtendedPrivateKey::from_seed(seed, network)?
ExtendedPrivateKey::from_mnemonic(mnemonic, passphrase, network)?
// Key derivation
master_key.derive_path(&path)?
master_key.derive_child(child_number)?
// Public key conversion
private_key.to_extended_public_key()
// Serialization
key.to_string() // Base58Check format (xprv/xpub)
ExtendedPrivateKey::from_str(xprv_string)?
🔐 Important Security Guidelines:
' or H) for account-level keyszeroize to clear sensitive data from memorym/44'/0'/0'): More secure, requires private key, prevents parent key exposurem/44'/0'/0'/0/0): Allows public key derivation, useful for watch-only walletsBest Practice: Use hardened derivation for upper levels (purpose, coin type, account) and normal derivation for address generation.
This implementation is fully compatible with:
All keys are interoperable and can be imported/exported across different wallet implementations.
This library includes comprehensive test coverage:
# Run all tests
cargo test
# Run with output
cargo test -- --nocapture
# Run specific test
cargo test test_bip44_derivation
# Run doc tests
cargo test --doc
Test coverage includes:
Full API documentation is available at docs.rs/bip32.
Build documentation locally:
cargo doc --open --no-deps --package khodpay-bip32
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 walletsRun examples:
cargo run --example wallet_creation
The library is optimized for both performance and security:
secp256k1Contributions are welcome! Please ensure:
cargo testcargo fmtcargo clippyThis project is dual-licensed under:
You may choose either license for your use.
Made with ❤️ for the Bitcoin and cryptocurrency community