bitcoin-address-generator
A Rust library for Bitcoin key derivation and address generation.
Features
- 🔐 Secure Memory Handling: Sensitive data like private keys and seeds are automatically zeroized when no longer needed
- 🔑 BIP39 Mnemonic Generation: Create new mnemonic phrases with various word counts (12, 15, 18, 21, 24 words)
- 💼 Multiple Address Types:
- Legacy addresses (P2PKH) via BIP44
- Nested SegWit addresses (P2SH-WPKH) via BIP49
- Native SegWit addresses (P2WPKH) via BIP84
- Taproot addresses (P2TR) via BIP86
- 🌐 Network Support: Works with Bitcoin mainnet, testnet, regtest, and signet
- 🛠️ Script Hash Calculation: Calculate script hashes for Bitcoin addresses (compatible with Electrum servers)
- 📋 Private Key Export: Derive private keys in WIF format
- 🧩 BIP32 HD Wallet: Supports custom derivation paths
- 📚 Multi-language Support: Generate mnemonics in different languages
Installation
Add this to your Cargo.toml
:
[]
= "0.1.0"
Usage Examples
Generate a New Mnemonic Phrase
use ;
use Language;
Derive Different Address Types from a Mnemonic
use derive_bitcoin_address;
use Network;
Derive Multiple Addresses at Once
use derive_bitcoin_addresses;
use Network;
Calculate a Script Hash for an Address
use calculate_script_hash;
use Network;
Derive a Private Key in WIF Format
use derive_private_key;
use Network;
Using a BIP39 Passphrase (Optional Extra Security)
use derive_bitcoin_address;
use Network;
API Documentation
Main Functions
generate_mnemonic(word_count: Option<WordCount>, language: Option<Language>) -> Result<String, DerivationError>
Generates a new BIP39 mnemonic phrase.
word_count
: Optional. Number of words (12, 15, 18, 21, or 24). Defaults to 12.language
: Optional. Mnemonic language. Defaults to English.- Returns: The mnemonic phrase as a string, or an error.
derive_bitcoin_address(mnemonic_phrase: &str, derivation_path_str: Option<&str>, network: Option<Network>, bip39_passphrase: Option<&str>) -> Result<GetAddressResponse, DerivationError>
Derives a Bitcoin address from a mnemonic phrase.
mnemonic_phrase
: The BIP39 mnemonic phrase.derivation_path_str
: Optional. BIP32 derivation path. Defaults to "m/84'/0'/0'/0/0".network
: Optional. Bitcoin network. Defaults to Bitcoin mainnet.bip39_passphrase
: Optional. BIP39 passphrase. Defaults to empty string.- Returns: A
GetAddressResponse
containing address, derivation path, and public key, or an error.
derive_bitcoin_addresses(mnemonic_phrase: &str, derivation_path_str: Option<&str>, network: Option<Network>, bip39_passphrase: Option<&str>, is_change: Option<bool>, start_index: Option<u32>, count: Option<u32>) -> Result<GetAddressesResponse, DerivationError>
Derives multiple Bitcoin addresses from a single mnemonic by iterating through a range of indices.
mnemonic_phrase
: The BIP39 mnemonic phrase.derivation_path_str
: Optional. Base path up to the account level (e.g., "m/84'/0'/0'"). Defaults to "m/84'/0'/0'".network
: Optional. Bitcoin network. Defaults to Bitcoin mainnet.bip39_passphrase
: Optional. BIP39 passphrase. Defaults to empty string.is_change
: Optional. Whether to derive change addresses (true) or receiving addresses (false). Defaults to false.start_index
: Optional. Starting index for address derivation. Defaults to 0.count
: Optional. Number of addresses to generate. Defaults to 1. Returns: AGetAddressesResponse
containing a collection of addresses, or an error.
calculate_script_hash(address: &str, network: Option<Network>) -> Result<String, DerivationError>
Calculates a script hash for a Bitcoin address.
address
: The Bitcoin address.network
: Optional. Bitcoin network. Defaults to Bitcoin mainnet.- Returns: The script hash as a hex string, or an error.
derive_private_key(mnemonic_phrase: &str, derivation_path_str: Option<&str>, network: Option<Network>, bip39_passphrase: Option<&str>) -> Result<String, DerivationError>
Derives a private key in WIF format.
mnemonic_phrase
: The BIP39 mnemonic phrase.derivation_path_str
: Optional. BIP32 derivation path. Defaults to "m/84'/0'/0'/0/0".network
: Optional. Bitcoin network. Defaults to Bitcoin mainnet.bip39_passphrase
: Optional. BIP39 passphrase. Defaults to empty string.- Returns: The private key in WIF format, or an error.
Data Structures
GetAddressResponse
Holds the result of address derivation.
GetAddressesResponse
Holds the result of multiple address derivation.
WordCount
Enum representing the possible number of words in a mnemonic phrase.
DerivationError
Error type for key derivation operations.
Security Considerations
-
Protecting Private Keys: This library uses the
zeroize
crate to automatically clear sensitive data from memory when it's no longer needed. -
Mnemonic Phrases: Treat mnemonic phrases with the same level of security as private keys. They should never be stored unencrypted or shared.
-
BIP39 Passphrases: Using a BIP39 passphrase adds an extra layer of security, but also means you must remember both the mnemonic and the passphrase to recover your wallet.
-
Network Validation: The library validates that the derivation path's coin type matches the requested network to prevent address derivation on the wrong network.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
Run tests and show logs
cargo test -- --nocapture
Run a specific test
cargo test <test_name> -- --nocapture
cargo test test_generate_mnemonic -- --nocapture