A secure, feature-rich Bitcoin wallet implementation in Rust with support for wallet creation, address generation, transaction signing, and UTXO management.
- BIP39 Mnemonic Support: Create and recover wallets using standard mnemonic phrases
- BIP44 HD Wallet: Hierarchical deterministic address generation
- Gap Limit Implementation: Efficient address scanning with configurable gap limits
- P2PKH Transaction Signing: Secure ECDSA signing for legacy Bitcoin addresses
- SegWit Support: Native P2WPKH (bech32) addresses with witness transaction signing
- Multi-signature Wallets: Create and manage 2-of-3, 3-of-5, etc. multi-signature wallets with native SegWit P2WSH
- Multi-signature Transaction Signing: Sign transactions with multiple cosigners and duplicate detection
- Transaction Broadcasting: Finalize and broadcast multi-signature transactions to the network
- UTXO Management: Real-time balance tracking and UTXO validation
- Blockchain Integration: Direct integration with Bitcoin testnet via Blockstream API
- Secure Key Management: Private keys stored in WIF format with proper cryptographic handling
- Lightning Node (Testnet): Start a Lightning node via LDK Node and print your Node ID
- Native SegWit (P2WPKH/P2WSH): Bech32 addresses (testnet:
tb1q.../tb1p...). Witness data goes in the witness; emptyscriptSig. - Wrapped SegWit (P2SH-P2WPKH): Base58 P2SH addresses (testnet:
2...). The segwit witness program (redeemScript) is placed inscriptSig; signatures are still carried in the witness. Useful for legacy compatibility.
- Cryptographic Security: Uses bitcoin-rs library for all cryptographic operations
- Memory Safety: Leverages Rust's ownership system for secure memory management
- Async/Await: Non-blocking blockchain API calls for better performance
- Error Handling: Comprehensive error handling with custom error types
- Serialization: JSON-based wallet persistence with serde
- Rust 1.70+ (install via rustup)
- Internet connection for blockchain API access
- Bitcoin testnet for safe testing
-
Clone the repository:
git clone https://github.com/leepl37/rust_cli_wallet.git cd rust_cli_wallet -
Build the project:
cargo build --release
-
Run the wallet:
cargo run
Start a Lightning node using LDK Node and print your Node ID:
cargo run
# In the menu, choose:
# 7) LDK Node Quickstart (Testnet)What happens:
- A testnet Lightning node starts using Esplora as the chain source (
mempool.spacetestnet API) - The node listens on port 9735 (local by default)
- Your Node ID (public key) is printed, e.g.
Node ID: 02abcd... - State is stored under
./ldk_data/so the node survives restarts
Tip: Keep the menu running to keep the node online. Share node_id@host:9735 with peers to connect or open channels (testnet).
- Wallet: Main wallet structure managing addresses and state
- WalletAddress: Individual address with private key and UTXOs
- UTXO: Unspent transaction output representation
- Transaction Signing: ECDSA signing with secp256k1 curve
The wallet implements BIP44 gap limit scanning to efficiently discover used addresses:
1. Scan addresses sequentially (index 0, 1, 2...)
2. Count consecutive unused addresses
3. Stop when gap limit (20) is reached
4. This prevents infinite scanning while finding all used addresses
Addresses are derived using BIP44 path: m/44'/1'/0'/0/{index}
44'- BIP44 standard1'- Bitcoin testnet coin type0'- Account number0- Change address type (receiving){index}- Sequential address index
- UTXO Selection: Smart coin selection algorithm
- Fee Calculation: Based on transaction size and fee rate (optimized for SegWit)
- Input Creation: References to spent UTXOs
- Output Creation: Destination + change addresses
- Signing: ECDSA signatures for each input (P2PKH) or witness data (P2WPKH)
The wallet supports native SegWit (P2WPKH) addresses with the following features:
- Address Format: Bech32 encoded addresses (tb1q... for testnet)
- Witness Data: Proper witness structure with signatures and public keys
- Fee Optimization: SegWit inputs use ~68 vbytes vs ~148 vbytes for legacy
- Mixed Transactions: Support for combining P2PKH and P2WPKH inputs
- Multi-signature: Native P2WSH multi-signature wallets
- Private keys stored in WIF (Wallet Import Format)
- Mnemonic phrases enable deterministic key generation
- All cryptographic operations use bitcoin-rs library
- Uses Bitcoin testnet for safe testing
- Validates all blockchain data before use
- Implements proper error handling for network failures
- Leverages Rust's ownership system
- No unsafe code blocks
- Automatic memory management prevents common vulnerabilities
Run the test suite:
cargo testRun with verbose output:
cargo test -- --nocaptureCurrently supports Bitcoin testnet. To switch to mainnet:
- Change
Network::TestnettoNetwork::Bitcoin - Update BIP44 coin type from
1'to0' - Use mainnet blockchain APIs
The gap limit is set to 20 by default. To modify:
const GAP_LIMIT: u32 = 20; // Change this value- SegWit support (P2WPKH) - β COMPLETED
- P2SH-P2WPKH support
- Lightning Node (Testnet) via LDK Node
- Advanced multi-signature features (time-locks, threshold signatures)
- β Duplicate Private Key Validation: Prevents accidental overwriting of existing private keys
- β Smart Key Management: Warns users when different private keys exist for the same public key
- β User Confirmation Prompts: Asks for explicit confirmation before overwriting existing keys
- β Data Integrity Protection: Maintains wallet consistency and prevents data loss
- β Enhanced User Feedback: Clear messaging for duplicate detection and key validation
- β Native SegWit P2WPKH: Full support for bech32 addresses (tb1q...)
- β Witness Transaction Signing: Proper SegWit transaction creation and signing
- β Mixed Input Support: Handle both P2PKH and P2WPKH inputs in same transaction
- β SegWit Fee Optimization: Accurate fee calculation for SegWit transactions
- β Multi-signature SegWit: Native P2WSH multi-signature wallets
- β Enhanced Address Generation: Automatic SegWit address creation
- β Multi-signature Wallet Creation: Create 2-of-3, 3-of-5, etc. multi-signature wallets
- β Transaction Signing: Sign transactions with multiple cosigners and duplicate detection
- β CLI Interface: Complete menu system for multi-signature operations
- β Transaction Broadcasting: Finalize and broadcast multi-signature transactions
- β File Management: Save/load transaction files for sharing between cosigners
- β Enhanced Error Handling: Clear user feedback and validation
- β Basic wallet creation and address generation
- β Transaction sending and receiving
- β UTXO management and balance tracking
- β BIP39 mnemonic support
This project is licensed under the MIT License - see the LICENSE file for details.
This is educational software. Use at your own risk. Never use this wallet for storing significant amounts of Bitcoin without thorough testing and security review.
- bitcoin-rs for the Bitcoin library
- BIP39 for mnemonic phrase support
- Blockstream for the blockchain API
For questions or issues:
- Open an issue on GitHub
- Check the documentation
- Review the test cases for usage examples