Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo-minimal.lock
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ name = "bdk_bitcoind_client"
version = "0.1.0"
dependencies = [
"anyhow",
"bdk_bitcoind_client",
"bitcoind",
"corepc-types",
"filetime",
Expand Down
1 change: 1 addition & 0 deletions Cargo-recent.lock
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ name = "bdk_bitcoind_client"
version = "0.1.0"
dependencies = [
"anyhow",
"bdk_bitcoind_client",
"bitcoind",
"corepc-types",
"filetime",
Expand Down
22 changes: 13 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,38 @@ readme = "README.md"
edition = "2024"
rust-version = "1.85.0"

[features]
default = ["30_0"]
30_0 = []
29_0 = []
28_0 = []

[dependencies]
corepc-types = { version = "0.12.0", features = ["default"]}
jsonrpc = { version = "0.20.0", features = ["bitreq_http"] }
corepc-types = { version = "0.12.0", features = ["default"], optional = true }
jsonrpc = { version = "0.20.0", default-features = false }

# These pins are needed for `Cargo-minimal.lock`:
hex-conservative = { version = "0.2.1" } # blame: corepc-node

[dev-dependencies]
anyhow = { version = "1.0.66" }
bdk_bitcoind_client = { path = ".", default-features = false, features = ["bitreq", "29_0"] }
bitcoind = { version = "0.38.0", features = ["download", "29_0"] }
bitreq = { version = "0.3.5", features = ["async"] }
tokio = { version = "1", features = ["full"] }

# These pins are needed for `Cargo-minimal.lock`:
tar = { version = "0.4.43" } # blame: corepc-node
filetime = { version = "0.2.8" } # blame: corepc-node
log = { version = "0.4.14" } # blame: corepc-node

[features]
default = ["28_0", "bitreq"]
bitreq = ["dep:corepc-types", "jsonrpc/bitreq_http"]
30_0 = ["29_0"]
29_0 = ["28_0"]
28_0 = []

[package.metadata.rbmt.toolchains]
stable = "1.95.0"
nightly = "nightly"

[package.metadata.rbmt.test]
#exclude_features = ["default"]
exclude_features = ["default"]

# Allow multiple versions of the same package in the dependency tree.
[package.metadata.rbmt.lint]
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# bdk-bitcoind-client

<p>
<!-- <a href="https://crates.io/crates/bdk-bitcoind-client"><img src="https://img.shields.io/crates/v/bdk-bitcoind-client.svg"/></a> -->
<!-- <a href="https://docs.rs/bdk-bitcoind-client"><img src="https://img.shields.io/badge/docs.rs-bdk-bitcoind-client-orange"/></a> -->
<a href="https://crates.io/crates/bdk_bitcoind_client"><img src="https://img.shields.io/crates/v/bdk_bitcoind_client.svg"/></a>
<a href="https://docs.rs/bdk_bitcoind_client"><img src="https://img.shields.io/badge/docs.rs-bdk_bitcoind_client-orange.svg"/></a>
<a href="https://blog.rust-lang.org/2025/02/20/Rust-1.85.0/"><img src="https://img.shields.io/badge/rustc-1.85.0%2B-orange.svg"/></a>
<a href="https://github.com/bitcoindevkit/bdk-bitcoind-client/blob/master/LICENSE"><img src="https://img.shields.io/badge/License-MIT%2FApache--2.0-red.svg"/></a>
<a href="https://github.com/bitcoindevkit/bdk-bitcoind-client/actions/workflows/cont_integration.yml"><img src="https://github.com/bitcoindevkit/bdk-bitcoind-client/actions/workflows/cont_integration.yml/badge.svg"></a>
Expand Down Expand Up @@ -53,9 +53,10 @@ fn main() -> anyhow::Result<()> {
// Define how to authenticate with `bitcoind` (Cookie File or User/Pass)
let auth = Auth::CookieFile(PathBuf::from("/path/to/regtest/.cookie"));
let auth = Auth::UserPass("user".to_string(), "pass".to_string());
let timeout = std::time::Duration::from_secs(15);

// Instantiate a JSON-RPC `Client`
let client = Client::with_auth("http://127.0.0.1:18443", auth)?;
let client = Client::with_auth_timeout("http://127.0.0.1:18443", auth, timeout)?;

// Perform blockchain queries to `bitcoind` using the `Client`
let block_count = client.get_block_count()?;
Expand Down
50 changes: 50 additions & 0 deletions examples/call_async.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// async client example

use corepc_types::v29;
use jsonrpc::base64::Engine;
use jsonrpc::base64::engine::general_purpose::STANDARD as BASE64;
use jsonrpc::bitreq;
use jsonrpc::serde_json;

const URL: &str = "http://127.0.0.1:18443";
const RPC_COOKIE_PATH: &str = ".bitcoin/regtest/.cookie";

#[tokio::main]
async fn main() -> Result<(), Box<dyn core::error::Error>> {
// Create cookie authentication
let cookie_file = std::env::var("RPC_COOKIE").unwrap_or(RPC_COOKIE_PATH.to_string());
let cookie = std::fs::read_to_string(cookie_file)?;
let auth_header = format!("Basic {}", BASE64.encode(cookie.as_bytes()));

// The RPC method to call
let rpc = bdk_bitcoind_client::Rpc::GetBestBlockHash;
let method = rpc.to_string();

// Create RPC client
let client = bdk_bitcoind_client::Client::new();

// The `send_fn` takes the request as a JSON value, sends it asynchronously,
// and parses the response as a `jsonrpc::Response`.
let send_fn = |value: serde_json::Value| {
let auth_header = auth_header.clone();
async move {
bitreq::post(URL)
.with_header("Authorization", auth_header)
.with_json(&value)?
.send_async()
.await?
.json::<jsonrpc::Response>()
}
};

// Execute the RPC
let block_hash = client
.call_async::<v29::GetBestBlockHash, bitreq::Error, _>(&method, &[], send_fn)
.await?
.into_model()?
.0;

println!("{}", block_hash);

Ok(())
}
Loading
Loading