Skip to content
Merged
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ This is a client implementation for a subset of the [`block-dn`](https://github.

`sp/tweak-data/<start_block>`: Returns up to 2_000 blocks of BIP-352 partial secrets.

`block/<block_hash>`: Fetch a block by its hash.

# Getting Started

Download all filters from height 700_000 up to the current height.
Expand Down
12 changes: 11 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -152,6 +152,16 @@ impl<'e> Client<'e> {
.send()?;
Ok(response.json::<TapTweaks>()?)
}

/// Fetch the block by its hash.
pub fn block(&self, block_hash: BlockHash) -> Result<Block, Error> {
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::<Block>(response.as_bytes())?;
Ok(block)
}
}

#[cfg(test)]
Expand Down
14 changes: 14 additions & 0 deletions tests/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)
}
Loading