bitcoincore-rpc

repository·master·Indexed 18 days ago

https://github.com/rust-bitcoin/rust-bitcoincore-rpc

A type-safe Rust RPC client library for interacting with the Bitcoin Core JSON-RPC API. The package consists of the main bitcoincore-rpc client and bitcoincore-rpc-json for JSON-enabled data types. It supports Bitcoin Core versions 0.18.0 through 0.21.0 and requires Rust 1.56.1+. Note: This repository was archived as of 2025-11-25; users are recommended to migrate to corepc-client for blocking APIs.

Tokens
9.1K
Snippets
32
Records
51
Agent score
62%

What's inside bitcoincore-rpc

  1. Use bitcoincore-rpc for Bitcoin Core JSON-RPC interaction

    master

    The bitcoincore-rpc crate provides a Rust client library for interacting with the Bitcoin Core daemon's JSON-RPC API.

    Note that the data types used in the interface are provided by a separate crate, bitcoincore-rpc-json, which contains the JSON-enabled data types required for the RPC calls.

  2. Understand the bitcoincore-rpc crate structure

    master

    The rust-bitcoincore-rpc package compiles into two distinct crates:

    1. bitcoincore-rpc: Provides the RPC client implementation that exposes Bitcoin Core JSON-RPC APIs as native Rust functions.
    2. bitcoincore-rpc-json: Contains the Rust data structures representing the JSON responses from the Bitcoin Core JSON-RPC APIs. bitcoincore-rpc depends on this crate.
  3. Query Bitcoin Core entities using the Queryable trait

    master

    The Queryable trait allows you to retrieve specific Bitcoin Core entities (like Blocks or Transactions) directly from an RPC client using their unique identifiers. This abstraction maps a specific ID type to a corresponding RPC method call and handles the deserialization of the response.

    Supported implementations include:

    EntityID TypeUnderlying RPC Method
    bitcoin::block::Blockbitcoin::BlockHashgetblock
    bitcoin::transaction::Transactionbitcoin::Txidgetrawtransaction
    Option<crate::json::GetTxOutResult>bitcoin::OutPointgettxout (via get_tx_out)
    // Example: Querying a block by its hash
    let block_hash = ...; // bitcoin::BlockHash
    let block: bitcoin::block::Block = Block::query(&rpc_client, &block_hash)?;
    
    // Example: Querying a transaction by its txid
    let txid = ...; // bitcoin::Txid
    let tx: bitcoin::transaction::Transaction = Transaction::query(&rpc_client, &txid)?;
  4. Configure RPC authentication methods

    master

    The Auth enum defines how the client authenticates with the Bitcoin Core RPC server. You can use one of three methods:

    1. Auth::None: No authentication.
    2. Auth::UserPass(username, password): Standard HTTP Basic authentication.
    3. Auth::CookieFile(path): Uses a Bitcoin Core .cookie file to extract credentials. The client reads the first line of the file and splits it by the first colon to obtain the username and password.
    use rust_bitcoincore_rpc::Auth;
    use std::path::PathBuf;
    
    // Using username and password
    let auth = Auth::UserPass("myuser".to_string(), "mypass".to_string());
    
    // Using a cookie file
    let auth = Auth::CookieFile(PathBuf::from("/path/to/.bitcoin/.cookie"));
  5. Create and sign transactions

    master

    Workflow for constructing and signing transactions:

    1. Construct: Use create_raw_transaction(utxos, outs, locktime?, replaceable?) to create a new transaction from inputs and outputs.
    2. Fund: Use fund_raw_transaction(tx, options?, is_witness?) to automatically select UTXOs and calculate fees.
    3. Sign:
      • sign_raw_transaction_with_wallet(tx, utxos?, sighash_type?): Signs using the wallet's private keys.
      • sign_raw_transaction_with_key(tx, privkeys, prevtxs?, sighash_type?): Signs using provided private keys.
    4. PSBT: Alternatively, use create_psbt(inputs, outputs, locktime?, replaceable?) to create a Partially Signed Bitcoin Transaction.
  6. Connect to Bitcoin Core and fetch the best block hash

    master

    To use the client, connect to a Bitcoin Core node via its JSON-RPC interface. This example assumes the node is running on localhost:8332 with password authentication enabled.

    You will need to use Client::new with an Auth::UserPass credential and call methods provided by the RpcApi trait.

    extern crate bitcoincore_rpc;
    
    use bitcoincore_rpc::{Auth, Client, RpcApi};
    
    fn main() {
    
        let rpc = Client::new("http://localhost:8332",
                              Auth::UserPass("<FILL RPC USERNAME>".to_string(),
                                             "<FILL RPC PASSWORD>".to_string())).unwrap();
        let best_block_hash = rpc.get_best_block_hash().unwrap();
        println!("best block hash: {}", best_block_hash);
    }