khodpay_bip39/lib.rs
1//! # BIP39 - Mnemonic Code for Cryptocurrency Wallets
2//!
3//! A production-ready Rust implementation of the BIP39 standard for generating deterministic keys
4//! in cryptocurrency wallets.
5//!
6//! ## Overview
7//!
8//! BIP39 (Bitcoin Improvement Proposal 39) defines a method for creating mnemonic phrases
9//! (12-24 words) that can be used to generate deterministic cryptocurrency wallet keys.
10//! This implementation provides a safe, ergonomic, and fully-tested Rust API.
11//!
12//! ## Features
13//!
14//! - **Full BIP39 Compliance** - Implements the complete BIP39 specification
15//! - **Multi-Language Support** - 9 languages supported
16//! - **Type-Safe API** - Leverages Rust's type system for safety
17//! - **Comprehensive Testing** - 184+ tests including unit, doc, and integration tests
18//! - **Cryptographically Secure** - Uses system CSPRNG for entropy generation
19//! - **Zero Unsafe Code** - Pure safe Rust implementation
20//!
21//! ## Quick Start
22//!
23//! ```rust
24//! use khodpay_bip39::{Mnemonic, WordCount, Language};
25//!
26//! // Generate a new 12-word mnemonic
27//! let mnemonic = Mnemonic::generate(WordCount::Twelve, Language::English)?;
28//!
29//! // Display the phrase (user should write this down!)
30//! println!("Recovery phrase: {}", mnemonic.phrase());
31//!
32//! // Generate a cryptographic seed for key derivation
33//! let seed = mnemonic.to_seed("optional passphrase")?;
34//! # Ok::<(), khodpay_bip39::Error>(())
35//! ```
36//!
37//! ## Core Types
38//!
39//! ### [`Mnemonic`]
40//!
41//! The main struct representing a BIP39 mnemonic phrase.
42//!
43//! **Constructors:**
44//! - [`Mnemonic::new(entropy, language)`](Mnemonic::new) - Create from raw entropy bytes
45//! - [`Mnemonic::from_phrase(phrase, language)`](Mnemonic::from_phrase) - Parse existing phrase
46//! - [`Mnemonic::generate(word_count, language)`](Mnemonic::generate) - Generate random mnemonic
47//!
48//! **Methods:**
49//! - [`phrase()`](Mnemonic::phrase) - Get the mnemonic phrase as a string
50//! - [`entropy()`](Mnemonic::entropy) - Get the entropy bytes
51//! - [`word_count()`](Mnemonic::word_count) - Get the word count
52//! - [`to_seed(passphrase)`](Mnemonic::to_seed) - Generate cryptographic seed
53//!
54//! ### [`WordCount`]
55//!
56//! Type-safe enum for BIP39 word counts (12, 15, 18, 21, or 24 words).
57//!
58//! ### [`Language`]
59//!
60//! Enum for supported languages (English, Japanese, Korean, Spanish, French, Italian, Czech, Portuguese, Chinese Simplified).
61//!
62//! ### [`Error`]
63//!
64//! Comprehensive error type for all BIP39 operations.
65//!
66//! ## Usage Examples
67//!
68//! ### Creating a New Wallet
69//!
70//! ```rust
71//! use khodpay_bip39::{Mnemonic, WordCount, Language};
72//!
73//! // Generate a new mnemonic with 24 words (highest security)
74//! let mnemonic = Mnemonic::generate(WordCount::TwentyFour, Language::English)?;
75//!
76//! // Show the phrase to the user (they should write it down!)
77//! println!("Your recovery phrase:");
78//! println!("{}", mnemonic.phrase());
79//!
80//! // Generate seed with passphrase for additional security
81//! let seed = mnemonic.to_seed("my secure passphrase")?;
82//! # Ok::<(), khodpay_bip39::Error>(())
83//! ```
84//!
85//! ### Recovering a Wallet
86//!
87//! ```rust
88//! use khodpay_bip39::{Mnemonic, Language};
89//!
90//! // User enters their recovery phrase
91//! let phrase = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
92//!
93//! // Parse and validate the phrase
94//! let mnemonic = Mnemonic::from_phrase(phrase, Language::English)?;
95//!
96//! // Regenerate the seed (must use same passphrase!)
97//! let seed = mnemonic.to_seed("my secure passphrase")?;
98//! # Ok::<(), khodpay_bip39::Error>(())
99//! ```
100//!
101//! ### Creating from Known Entropy
102//!
103//! ```rust
104//! use khodpay_bip39::{Mnemonic, Language};
105//!
106//! // From hardware wallet or external entropy source
107//! let entropy = [42u8; 32]; // 256 bits = 24 words
108//!
109//! // Create mnemonic from entropy
110//! let mnemonic = Mnemonic::new(&entropy, Language::English)?;
111//!
112//! println!("Mnemonic: {}", mnemonic.phrase());
113//! # Ok::<(), khodpay_bip39::Error>(())
114//! ```
115//!
116//! ### Using Utility Functions
117//!
118//! ```rust
119//! use khodpay_bip39::{generate_mnemonic, validate_phrase, phrase_to_seed, WordCount};
120//!
121//! // Generate a phrase directly
122//! let phrase = generate_mnemonic(WordCount::Twelve)?;
123//!
124//! // Validate it
125//! validate_phrase(&phrase)?;
126//!
127//! // Generate seed from phrase
128//! let seed = phrase_to_seed(&phrase, "passphrase")?;
129//! # Ok::<(), khodpay_bip39::Error>(())
130//! ```
131//!
132//! ## Security Considerations
133//!
134//! ### ⚠️ Important Security Notes
135//!
136//! 1. **Entropy Generation**: Uses system CSPRNG (`rand::thread_rng()`)
137//! 2. **Mnemonic Storage**: Never store mnemonics in plain text
138//! 3. **Passphrase Security**: Passphrases add a "25th word" for additional security
139//! 4. **Memory Safety**: Consider zeroing sensitive data after use
140//!
141//! ### Best Practices
142//!
143//! ```rust
144//! use khodpay_bip39::{Mnemonic, WordCount, Language};
145//!
146//! // ✓ DO: Generate with maximum entropy
147//! let mnemonic = Mnemonic::generate(WordCount::TwentyFour, Language::English)?;
148//!
149//! // ✓ DO: Use passphrases for additional security
150//! let seed = mnemonic.to_seed("strong passphrase")?;
151//!
152//! // ✗ DON'T: Store mnemonics in application state
153//! // ✗ DON'T: Log or transmit mnemonics
154//! # Ok::<(), khodpay_bip39::Error>(())
155//! ```
156//!
157//! ## Testing
158//!
159//! The crate includes comprehensive test coverage:
160//!
161//! - **Unit Tests**: 138 tests covering all modules
162//! - **Doc Tests**: 35 tests in documentation
163//! - **Integration Tests**: 11 end-to-end workflow tests
164//!
165//! Run tests with:
166//! ```bash
167//! cargo test
168//! ```
169//!
170//! ## References
171//!
172//! - [BIP39 Specification](https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki)
173//! - [BIP32 HD Wallets](https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki)
174
175// Module declarations
176mod error;
177mod language;
178mod mnemonic;
179mod utils;
180mod word_count;
181
182// Public re-exports
183pub use error::{Error, Result};
184pub use language::Language;
185pub use mnemonic::Mnemonic;
186pub use utils::{
187 generate_mnemonic, generate_mnemonic_in_language, phrase_to_seed, phrase_to_seed_in_language,
188 validate_phrase, validate_phrase_in_language,
189};
190pub use word_count::WordCount;