An async Rust library for interfacing with Bitcoin SV nodes via their JSON-RPC API and REST interface. See the documentation at https://docs.rs/bitcoinsv-rpc/latest/bitcoinsv_rpc/.
- π Async/await support with Tokio
- π JSON-RPC interface for node commands with authentication support
- β‘ REST API interface for efficient binary block retrieval
- π§ Type-safe integration with the bitcoinsv crate
- β Tested with comprehensive unit and integration tests
Only a very limited number of functions have been implemented, to meet my immediate needs. If you need additional functions, please submit a pull request (preferred) or a ticket (I cant guarentee a quick response).
Add this to your Cargo.toml:
[dependencies]
bitcoinsv-rpc = "2.0"use bitcoinsv_rpc::{NodeClient, SvNodeClient};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a client connection to a Bitcoin SV node
let client = SvNodeClient::new(
"http://localhost:8332",
Some("your_rpc_user".to_string()),
Some("your_rpc_password".to_string()),
)?;
// Get the best block hash
let best_hash = client.get_best_block_hash().await?;
println!("Best block hash: {}", best_hash);
// Get the block header
let header = client.get_block_header(&best_hash).await?;
println!("Block version: {}", header.version());
println!("Block timestamp: {}", header.timestamp());
// Get the complete block (uses REST API for efficiency)
let block = client.get_block(&best_hash).await?;
println!("Number of transactions: {}", block.num_tx);
Ok(())
}Returns the hash of the best (tip) block in the longest blockchain.
Returns: Result<bitcoinsv::bitcoin::BlockHash>
Example:
let hash = client.get_best_block_hash().await?;
println!("Best block: {}", hash);Returns the block header for a specified block hash. Uses the JSON-RPC interface.
Parameters:
block_hash: The hash of the block to retrieve
Returns: Result<bitcoinsv::bitcoin::BlockHeader>
Example:
let hash = client.get_best_block_hash().await?;
let header = client.get_block_header(&hash).await?;
println!("Block difficulty: {}", header.difficulty());Returns the complete block data for a specified block hash. Uses the REST API in binary mode for efficient data transfer.
Parameters:
block_hash: The hash of the block to retrieve
Returns: Result<bitcoinsv::bitcoin::Block>
Example:
let hash = client.get_best_block_hash().await?;
let block = client.get_block(&hash).await?;
for tx in block.tx_iter() {
// Process transactions
}The client requires a node URL and optionally authentication credentials:
use bitcoinsv_rpc::{NodeClient, SvNodeClient};
// Without authentication (for nodes with rpcauth disabled)
let client = SvNodeClient::new("http://localhost:8332", None, None)?;
// With authentication
let client = SvNodeClient::new(
"http://localhost:8332",
Some("username".to_string()),
Some("password".to_string()),
)?;
// For testnet (default port 18332)
let client = SvNodeClient::new(
"http://localhost:18332",
Some("username".to_string()),
Some("password".to_string()),
)?;To use this library, you need a running Bitcoin SV node with:
- JSON-RPC enabled (enabled by default)
- REST interface enabled - Add
-restto your node configuration or command line - Authentication configured (if required) - Set
rpcuserandrpcpasswordin your bitcoin.conf
Example bitcoin.conf:
# Enable RPC server
server=1
# RPC credentials
rpcuser=your_username
rpcpassword=your_password
# RPC bind address (default: 127.0.0.1)
rpcbind=127.0.0.1
# RPC port (8332 for mainnet, 18332 for testnet)
rpcport=8332
# Enable REST API
rest=1Run the unit tests:
cargo testIntegration tests require a running Bitcoin SV node. Configure the connection using environment variables:
# Set node connection details
export BSV_NODE_URL=http://localhost:18332
export BSV_NODE_USER=your_username
export BSV_NODE_PASSWORD=your_password
# Run integration tests
cargo test --test integration -- --ignoredEnvironment Variables:
BSV_NODE_URL: URL of the Bitcoin SV node (default:http://localhost:18332)BSV_NODE_USER: RPC username (optional, omit if authentication is disabled)BSV_NODE_PASSWORD: RPC password (optional, omit if authentication is disabled)
Note: Integration tests are marked with #[ignore] by default and must be explicitly run with the --ignored flag.
The library provides detailed error types via the Error enum:
use bitcoinsv_rpc::{Error, NodeClient, SvNodeClient};
let hash = client.get_best_block_hash().await?;
match client.get_block(&hash).await {
Ok(block) => println!("Got block with {} transactions", block.num_tx),
Err(Error::Rpc { code, message }) => {
eprintln!("RPC error {}: {}", code, message);
}
Err(Error::Http(e)) => {
eprintln!("HTTP error: {}", e);
}
Err(e) => {
eprintln!("Other error: {}", e);
}
}The library is structured into several modules:
client: MainSvNodeClientstruct andNodeClienttraitrpc: JSON-RPC client implementation for RPC methodsrest: REST API client for efficient binary block retrievalerror: Error types and Result type alias
The NodeClient trait defines a common interface for communicating with Bitcoin nodes. This abstraction allows different client implementations (e.g., SV node, Teranode) to provide the same functionality, enabling easy switching between node types without changing your application code.
Note: You must import the NodeClient trait to use its methods on SvNodeClient:
use bitcoinsv_rpc::{NodeClient, SvNodeClient};The library uses the REST API (instead of JSON-RPC) for retrieving complete blocks because:
- Binary format is more efficient than JSON for large data structures
- Reduced overhead - no JSON encoding/decoding for block data
- Better performance for large blocks (Bitcoin SV supports very large blocks)
This library is under active development. Current implementation includes:
- β Connection management (JSON-RPC and REST)
- β
get_best_block_hash() - β
get_block_header() - β
get_block()via REST API - π§ Additional RPC methods (planned)
- π§ Teranode support (planned)
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.