khodpay_bip32/
lib.rs

1//! # BIP32 - Hierarchical Deterministic Wallets
2//!
3//! A production-ready Rust implementation of the BIP32 standard for hierarchical deterministic
4//! wallets in cryptocurrency applications.
5//!
6//! ## Overview
7//!
8//! BIP32 (Bitcoin Improvement Proposal 32) defines the standard for creating hierarchical
9//! deterministic (HD) wallets. This allows generating a tree of key pairs from a single seed,
10//! enabling backup and recovery of unlimited keys using just the initial seed.
11//!
12//! ## Features
13//!
14//! - **Full BIP32 Compliance** - Implements the complete BIP32 specification
15//! - **Type-Safe API** - Leverages Rust's type system for safety
16//! - **BIP39 Integration** - Seamlessly works with BIP39 mnemonics
17//! - **Hardened & Normal Derivation** - Supports both derivation types
18//! - **Network Support** - Bitcoin mainnet and testnet
19//! - **Zero Unsafe Code** - Pure safe Rust implementation
20//! - **Production Ready** - Validated against official test vectors
21//! - **Cross-Compatible** - Interoperable with major wallet implementations
22//!
23//! ## Quick Start
24//!
25//! ### Basic Usage
26//!
27//! ```rust
28//! use khodpay_bip32::{ExtendedPrivateKey, Network, DerivationPath};
29//! use khodpay_bip39::{Mnemonic, WordCount, Language};
30//! use std::str::FromStr;
31//!
32//! // Generate a mnemonic (using BIP39)
33//! let mnemonic = Mnemonic::generate(WordCount::Twelve, Language::English)?;
34//!
35//! // Create master extended private key directly from mnemonic
36//! let master_key = ExtendedPrivateKey::from_mnemonic(
37//!     &mnemonic,
38//!     None,  // Optional passphrase
39//!     Network::BitcoinMainnet
40//! )?;
41//!
42//! // Derive child keys using a BIP-44 path
43//! let path = DerivationPath::from_str("m/44'/0'/0'")?;
44//! let account_key = master_key.derive_path(&path)?;
45//!
46//! assert_eq!(account_key.depth(), 3);
47//! # Ok::<(), Box<dyn std::error::Error>>(())
48//! ```
49//!
50//! ### Derive from Seed
51//!
52//! ```rust
53//! use khodpay_bip32::{ExtendedPrivateKey, Network};
54//!
55//! // Use a seed directly (typically from BIP39)
56//! let seed = b"your-secure-seed-bytes-here-at-least-16-bytes-long";
57//! let master = ExtendedPrivateKey::from_seed(seed, Network::BitcoinMainnet)?;
58//!
59//! // Get the extended public key
60//! let master_pub = master.to_extended_public_key();
61//! println!("Master xpub: {}", master_pub);
62//! # Ok::<(), khodpay_bip32::Error>(())
63//! ```
64//!
65//! ### Watch-Only Wallets (Public Key Derivation)
66//!
67//! ```rust
68//! use khodpay_bip32::{ExtendedPrivateKey, Network, DerivationPath, ChildNumber};
69//! use std::str::FromStr;
70//!
71//! # let seed = b"your-secure-seed-bytes-here-at-least-16-bytes-long";
72//! # let master = ExtendedPrivateKey::from_seed(seed, Network::BitcoinMainnet)?;
73//! // Derive account-level key (with hardened derivation)
74//! let account_path = DerivationPath::from_str("m/44'/0'/0'")?;
75//! let account_key = master.derive_path(&account_path)?;
76//!
77//! // Get the extended public key for watch-only wallet
78//! let account_pub = account_key.to_extended_public_key();
79//!
80//! // Now derive receive addresses from public key only (no hardened)
81//! let first_address = account_pub.derive_child(ChildNumber::Normal(0))?;
82//! println!("First receive address xpub: {}", first_address);
83//! # Ok::<(), khodpay_bip32::Error>(())
84//! ```
85//!
86//! ## Common Derivation Paths
87//!
88//! - **BIP44** - `m/44'/0'/0'` - Multi-account hierarchy for Bitcoin
89//! - **BIP49** - `m/49'/0'/0'` - SegWit (P2WPKH-nested-in-P2SH)
90//! - **BIP84** - `m/84'/0'/0'` - Native SegWit (P2WPKH)
91//!
92//! ## Security Considerations
93//!
94//! - Always use cryptographically secure random seeds
95//! - Protect private keys and seeds with appropriate security measures
96//! - Use hardened derivation (`'` or `H`) for account-level keys
97//! - Never expose private keys or seeds over insecure channels
98//! - The library uses `zeroize` to securely clear sensitive data from memory
99//!
100//! ## Compatibility
101//!
102//! This implementation is fully compatible with:
103//! - Hardware wallets (Trezor, Ledger)
104//! - Software wallets (Electrum, Bitcoin Core)
105//! - All BIP32/44/49/84 compliant implementations
106
107// Module declarations
108mod chain_code;
109mod child_number;
110mod derivation_path;
111mod error;
112mod extended_private_key;
113mod extended_public_key;
114mod network;
115mod private_key;
116mod public_key;
117
118/// Utility functions and convenience methods for common BIP32 operations.
119///
120/// This module provides ergonomic wrappers around common patterns to reduce
121/// boilerplate in application code.
122pub mod utils;
123
124// Public re-exports
125pub use chain_code::ChainCode;
126pub use child_number::ChildNumber;
127pub use derivation_path::DerivationPath;
128pub use error::{Error, Result};
129pub use extended_private_key::ExtendedPrivateKey;
130pub use extended_public_key::ExtendedPublicKey;
131pub use network::{KeyType, Network};
132pub use private_key::PrivateKey;
133pub use public_key::PublicKey;