A flexible, robust implementation of Bitcoin functionality with dual-support for Python and Rust, designed for smooth migration between implementations while maintaining a consistent interface.
Current Version: 0.1.0
Last Updated: March 1, 2025
OPSource provides a complete Bitcoin integration framework with the following key features:
- Dual Implementation: Side-by-side support for both Python (python-bitcoinlib) and Rust (rust-bitcoin/BDK) implementations
- Common Interface: Consistent API across implementations enabling seamless transitions
- Migration Path: Structured approach for transitioning from Python to Rust
- Flexible Configuration: Runtime selection of implementation based on configuration or feature flags
- Comprehensive Testing: Identical behavior validation across implementations
This project follows hexagonal architecture principles, with a clear separation between:
- Core Domain Interface (
src/bitcoin/interface.rs
): Defines the contract that all implementations must fulfill - Implementations:
- Python Implementation (
src/bitcoin/python.py
): Using python-bitcoinlib - Rust Implementation (
src/bitcoin/rust.rs
): Using rust-bitcoin and BDK
- Python Implementation (
- Adapter Module (
src/bitcoin/mod.rs
): Exposes a consistent interface and handles implementation selection - Configuration (
src/config.rs
): Manages environment settings and implementation preferences - Testing Framework (
src/bitcoin/test.rs
): Validates identical behavior across implementations
src/
├── bitcoin/
│ ├── interface.rs # Common interface definition
│ ├── mod.rs # Module and factory implementations
│ ├── python.py # Python implementation
│ ├── rust.rs # Rust implementation
│ └── test.rs # Test framework
├── config.rs # Configuration management
├── lib.rs # Library exports
└── main.rs # Test driver
- Rust (1.60+)
- Python 3.8+
- python-bitcoinlib (
pip install python-bitcoinlib
)
# Clone the repository
git clone https://github.com/your-org/opsource.git
cd opsource
# Build with both implementations enabled
cargo build --features full
The implementation can be configured through environment variables:
# Select implementation (python or rust)
export USE_RUST_BITCOIN=1 # Use Rust implementation
export BITCOIN_NETWORK=testnet # Network selection
Or programmatically:
let mut config = Config::default();
config.use_rust_bitcoin = true;
config.bitcoin_network = Some("testnet".to_string());
use opsource::{bitcoin, config};
// Initialize
let config = config::Config::default();
let bitcoin_interface = bitcoin::create_bitcoin_interface(
bitcoin::interface::BitcoinImplementationType::Rust,
&config
);
// Generate address
let address = bitcoin_interface.generate_address(
bitcoin::interface::AddressType::P2WPKH
).unwrap();
println!("Generated address: {}", address.address);
// Check balance
let balance = bitcoin_interface.get_balance().unwrap();
println!("Wallet balance: {} satoshis", balance);
// Create transaction
let tx = bitcoin_interface.create_transaction(
vec![("tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx".to_string(), 10000)],
5 // fee rate in sat/vB
).unwrap();
println!("Created transaction: {}", tx.txid);
Use the provided script to test both implementations:
# Run tests on both implementations
./scripts/test_implementations.sh
This project implements a controlled migration strategy from Python to Rust:
-
Phase 1 (Complete): Dual implementation with Python as default
- Common interface definition
- Basic functionality in both implementations
- Test framework to validate behavior
-
Phase 2 (In Progress): Enhanced Rust implementation
- Complete BDK wallet integration
- Comprehensive error handling
- Performance optimization
-
Phase 3 (Upcoming): Rust as default implementation
- Switch default to Rust implementation
- Python as fallback only
- Additional Bitcoin features (Lightning, etc.)
-
Phase 4 (Future): Python removal
- Complete removal of Python implementation
- Full Rust implementation
- Uses python-bitcoinlib for core functionality
- Connects to Bitcoin network via RPC
- Provides all interface functionality with consistent error handling
- Uses rust-bitcoin and BDK (Bitcoin Dev Kit)
- Wallet functionality via BDK with descriptor-based wallet support
- Electrum server connection for blockchain data
- Support for SegWit addresses and transactions
The project includes a comprehensive test suite that ensures both implementations provide identical behavior:
# Run specific tests
cargo test
# Run the test suite comparing both implementations
cargo run -- test
- 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
This project is licensed under the MIT License - see the LICENSE file for details.
- Bitcoin Dev Kit (BDK) team
- python-bitcoinlib maintainers
- Bitcoin Core developers