From 9f76548d4475ed753b6ceb7bc96df920e92d460b Mon Sep 17 00:00:00 2001 From: rustaceanrob Date: Thu, 22 Jan 2026 14:08:07 +0000 Subject: [PATCH] Add `block` route --- README.md | 2 ++ src/lib.rs | 12 +++++++++++- tests/client.rs | 14 ++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3163c30..c96c522 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,8 @@ This is a client implementation for a subset of the [`block-dn`](https://github. `sp/tweak-data/`: Returns up to 2_000 blocks of BIP-352 partial secrets. +`block/`: Fetch a block by its hash. + # Getting Started Download all filters from height 700_000 up to the current height. diff --git a/src/lib.rs b/src/lib.rs index 7eb532b..5b49c1f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,7 @@ use core::time::Duration; use std::{borrow::Cow, io::Cursor, net::SocketAddr}; -use bitcoin::{bip158::BlockFilter, block::Header, consensus::Decodable}; +use bitcoin::{Block, BlockHash, bip158::BlockFilter, block::Header, consensus::Decodable}; use models::{Html, ServerStatus, TapTweaks}; /// Errors that may occur when querying. @@ -152,6 +152,16 @@ impl<'e> Client<'e> { .send()?; Ok(response.json::()?) } + + /// Fetch the block by its hash. + pub fn block(&self, block_hash: BlockHash) -> Result { + let route = self.endpoint.append_route(format!("block/{block_hash}")); + let response = bitreq::get(route) + .with_timeout(self.timeout.as_secs()) + .send()?; + let block = bitcoin::consensus::deserialize::(response.as_bytes())?; + Ok(block) + } } #[cfg(test)] diff --git a/tests/client.rs b/tests/client.rs index de431f4..9d636b9 100644 --- a/tests/client.rs +++ b/tests/client.rs @@ -38,3 +38,17 @@ fn test_tap_tweaks() { assert!(!tweaks.blocks.is_empty()); let _ = tweaks.fallible_into_iterator(); } + +#[test] +fn test_block() { + let client = default_client(); + assert!( + client + .block( + "0000000000000000000320283a032748cef8227873ff4872689bf23f1cda83a5" + .parse() + .unwrap() + ) + .is_ok() + ) +}