A robust Bitcoin transaction builder written in Rust that handles UTXO selection, fee calculation, and PSBT generation with support for multiple script types and advanced features like RBF and locktime.
Coin Smith is a developer-friendly Bitcoin transaction construction tool that provides both CLI and web interfaces. It implements intelligent coin selection algorithms, accurate fee estimation, and generates industry-standard Partially Signed Bitcoin Transactions (PSBTs).
- πͺ Smart Coin Selection: Efficient UTXO selection with configurable strategies
- π° Accurate Fee Calculation: Precise virtual byte calculation for all script types
- π Multi-Script Support: P2WPKH, P2PKH, P2SH-P2WPKH, and P2TR (Taproot)
- β»οΈ RBF Support: Replace-By-Fee signaling for transaction replacement
- β±οΈ Locktime Handling: Both block height and Unix timestamp locktimes with anti-fee-sniping
- π¦ PSBT Generation: Standards-compliant Partially Signed Bitcoin Transaction output
- π Dual Interface: Command-line tool and web application
- π§ͺ Comprehensive Testing: Extensive fixture-based test suite
- Rust 1.70+ and Cargo
- Bash shell (for convenience scripts)
-
Clone the repository
git clone <repository-url> cd 2026-developer-challenge-2-coin-smith-EliteCoder18
-
Build the project
./setup.sh
This compiles both CLI and web binaries in release mode.
Process Bitcoin transaction fixtures from JSON files:
./cli.sh fixtures/basic_change_p2wpkh.jsonInput Format (JSON):
{
"network": "mainnet",
"utxos": [
{
"txid": "...",
"vout": 0,
"value_sats": 100000,
"script_pubkey_hex": "...",
"script_type": "p2wpkh",
"address": "bc1q..."
}
],
"payments": [
{
"address": "bc1q...",
"script_pubkey_hex": "...",
"script_type": "p2wpkh",
"value_sats": 70000
}
],
"change": {
"address": "bc1q...",
"script_pubkey_hex": "...",
"script_type": "p2wpkh"
},
"fee_rate_sat_vb": 5.0,
"rbf": true,
"locktime": 840000,
"current_height": 840000,
"policy": {
"max_inputs": 5
}
}Output Format:
{
"ok": true,
"network": "mainnet",
"strategy": "default",
"selected_inputs": [...],
"outputs": [...],
"change_index": 1,
"fee_sats": 560,
"fee_rate_sat_vb": 5.0,
"vbytes": 112,
"rbf_signaling": true,
"locktime": 839999,
"locktime_type": "block_height",
"psbt_base64": "cHNidP8B...",
"warnings": []
}Start the web server:
./web.shThen open your browser to http://127.0.0.1:3000 for an interactive transaction builder interface.
Environment Variables:
PORT: Web server port (default: 3000)
coin-smith/
βββ cli/ # Command-line interface
β βββ src/
β βββ main.rs # CLI entry point
βββ core/ # Core transaction logic
β βββ src/
β βββ lib.rs # Module exports
β βββ models.rs # Data structures
β βββ validation.rs # Input validation
β βββ coin_selection.rs # UTXO selection algorithm
β βββ builder.rs # Transaction construction
β βββ fee.rs # Fee calculation
β βββ locktime.rs # Locktime logic
β βββ psbt.rs # PSBT generation
β βββ error.rs # Error handling
βββ web/ # Web server
βββ src/
βββ main.rs # Server entry point
βββ routes/ # API endpoints
βββ templates/ # HTML templates
fixtures/ # Test fixtures
βββ basic_change_p2wpkh.json
βββ multi_payment_change.json
βββ rbf_basic.json
βββ locktime_block_height.json
βββ ...
Coin Smith implements a greedy coin selection strategy that:
- Filters UTXOs based on policy constraints (e.g.,
max_inputs) - Selects UTXOs in order until sufficient funds are available
- Calculates change output accounting for fee requirements
- Validates minimum output values (dust threshold)
Virtual bytes (vBytes) are calculated using accurate witness accounting:
| Script Type | Input vBytes | Output vBytes |
|---|---|---|
| P2WPKH | 68 | 31 |
| P2PKH | 148 | 34 |
| P2SH-P2WPKH | 91 | 32 |
| P2TR | 57.5 | 43 |
Base transaction overhead: 10.5 vBytes
When RBF is enabled, all inputs use sequence number 0xFFFFFFFD (4294967293) to signal replaceability per BIP 125.
- Anti-Fee-Sniping: By default, sets locktime to
current_height - 1 - Custom Locktime: Supports explicit block height or Unix timestamp
- Sequence Numbers: Adjusted based on locktime type and RBF signaling
- P2WPKH: Native SegWit (bech32)
- P2PKH: Legacy addresses
- P2SH-P2WPKH: Nested SegWit
- P2TR: Taproot (bech32m)
The project includes 26+ comprehensive test fixtures covering:
- Basic change creation
- Multiple payment outputs
- Mixed input/output script types
- RBF scenarios
- Locktime variants (block height, Unix timestamp)
- Large UTXO pools
- Edge cases (send-all, dust change)
- Anti-fee-sniping
Run all tests:
cd coin-smith
cargo testRun specific fixture:
./cli.sh fixtures/rbf_basic.json./cli.sh fixtures/basic_change_p2wpkh.json./cli.sh fixtures/multi_payment_change.json./cli.sh fixtures/rbf_basic.json./cli.sh fixtures/locktime_block_height.json./cli.sh fixtures/send_all_dust_change.jsoncd coin-smith
cargo buildcargo test -- --nocapturecargo check
cargo clippy
cargo fmt- bitcoin (0.32.2): Bitcoin primitives and transaction handling
- serde / serde_json: JSON serialization
- axum: Web framework (web interface)
- tokio: Async runtime (web interface)
Contributions are welcome! Please ensure:
- All tests pass
- Code is formatted with
cargo fmt - No clippy warnings
- New fixtures added for new features
See LICENSE file for details.
- Wallet Development: Integrate transaction building into Bitcoin wallets
- Payment Processing: Batch payment construction
- Testing: Generate test transactions for Bitcoin applications
- Education: Learn Bitcoin transaction structure and PSBT format
- Automation: Script-based transaction creation
Built with β€οΈ using Rust