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
19 changes: 18 additions & 1 deletion src/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::str::FromStr;
use bitcoin::consensus::{deserialize, serialize};
use bitcoin::hashes::hex::{FromHex, ToHex};
use bitcoin::hashes::{sha256, Hash};
use bitcoin::{Block, BlockHash, BlockHeader, Script, Transaction, Txid};
use bitcoin::{Block, BlockHash, BlockHeader, MerkleBlock, Script, Transaction, Txid};

#[allow(unused_imports)]
use log::{debug, error, info, trace};
Expand Down Expand Up @@ -176,6 +176,23 @@ impl AsyncClient {
Ok(Some(resp.error_for_status()?.json().await?))
}

/// Get a [`MerkleBlock`] inclusion proof for a [`Transaction`] with the given [`Txid`].
pub async fn get_merkle_block(&self, tx_hash: &Txid) -> Result<Option<MerkleBlock>, Error> {
let resp = self
.client
.get(&format!("{}/tx/{}/merkleblock-proof", self.url, tx_hash))
.send()
.await?;

if let StatusCode::NOT_FOUND = resp.status() {
return Ok(None);
}

let merkle_block = deserialize(&Vec::from_hex(&resp.text().await?)?)?;

Ok(Some(merkle_block))
}

/// Get the spending status of an output given a [`Txid`] and the output index.
pub async fn get_output_status(
&self,
Expand Down
21 changes: 20 additions & 1 deletion src/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use ureq::{Agent, Proxy, Response};
use bitcoin::consensus::{deserialize, serialize};
use bitcoin::hashes::hex::{FromHex, ToHex};
use bitcoin::hashes::{sha256, Hash};
use bitcoin::{Block, BlockHash, BlockHeader, Script, Transaction, Txid};
use bitcoin::{Block, BlockHash, BlockHeader, MerkleBlock, Script, Transaction, Txid};

use crate::{BlockStatus, Builder, Error, MerkleProof, OutputStatus, Tx, TxStatus};

Expand Down Expand Up @@ -202,6 +202,25 @@ impl BlockingClient {
}
}

/// Get a [`MerkleBlock`] inclusion proof for a [`Transaction`] with the given [`Txid`].
pub fn get_merkle_block(&self, txid: &Txid) -> Result<Option<MerkleBlock>, Error> {
let resp = self
.agent
.get(&format!("{}/tx/{}/merkleblock-proof", self.url, txid))
.call();

match resp {
Ok(resp) => Ok(Some(deserialize(&Vec::from_hex(&resp.into_string()?)?)?)),
Err(ureq::Error::Status(code, _)) => {
if is_status_not_found(code) {
return Ok(None);
}
Err(Error::HttpResponse(code))
}
Err(e) => Err(Error::Ureq(e)),
}
}

/// Get the spending status of an output given a [`Txid`] and the output index.
pub fn get_output_status(
&self,
Expand Down
43 changes: 43 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
//! * `async-https` enables [`reqwest`], the async client with support for proxying and TLS (SSL).
//!
//!

#![allow(clippy::result_large_err)]

use std::collections::HashMap;
use std::fmt;
use std::io;
Expand Down Expand Up @@ -568,6 +571,46 @@ mod test {
assert!(merkle_proof.pos > 0);
}

#[cfg(all(feature = "blocking", any(feature = "async", feature = "async-https")))]
#[tokio::test]
async fn test_get_merkle_block() {
let (blocking_client, async_client) = setup_clients().await;

let address = BITCOIND
.client
.get_new_address(Some("test"), Some(AddressType::Legacy))
.unwrap();
let txid = BITCOIND
.client
.send_to_address(
&address,
Amount::from_sat(1000),
None,
None,
None,
None,
None,
None,
)
.unwrap();
let _miner = MINER.lock().await;
generate_blocks_and_wait(1);

let merkle_block = blocking_client.get_merkle_block(&txid).unwrap().unwrap();
let merkle_block_async = async_client.get_merkle_block(&txid).await.unwrap().unwrap();
assert_eq!(merkle_block, merkle_block_async);

let mut matches = vec![txid];
let mut indexes = vec![];
let root = merkle_block
.txn
.extract_matches(&mut matches, &mut indexes)
.unwrap();
assert_eq!(root, merkle_block.header.merkle_root);
assert_eq!(indexes.len(), 1);
assert!(indexes[0] > 0);
}

#[cfg(all(feature = "blocking", any(feature = "async", feature = "async-https")))]
#[tokio::test]
async fn test_get_output_status() {
Expand Down