Skip to content

Commit c171cab

Browse files
ItoroDthunderbiscuit
authored andcommitted
feat(esplora): Add get_merkle_proof`and get_output_status
1 parent 86aea5d commit c171cab

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

bdk-ffi/src/esplora.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::error::EsploraError;
88
use crate::types::Tx;
99
use crate::types::TxStatus;
1010
use crate::types::Update;
11-
use crate::types::{FullScanRequest, SyncRequest};
11+
use crate::types::{FullScanRequest, MerkleProof, OutputStatus, SyncRequest};
1212

1313
use bdk_esplora::esplora_client::{BlockingClient, Builder};
1414
use bdk_esplora::EsploraExt;
@@ -240,4 +240,26 @@ impl EsploraClient {
240240

241241
Ok(txs.into_iter().map(Tx::from).collect())
242242
}
243+
244+
/// Get a merkle inclusion proof for a [`Transaction`] with the given
245+
/// [`Txid`].
246+
pub fn get_merkle_proof(&self, txid: &Txid) -> Result<Option<MerkleProof>, EsploraError> {
247+
self.0
248+
.get_merkle_proof(&txid.0)
249+
.map(|proof| proof.map(MerkleProof::from))
250+
.map_err(EsploraError::from)
251+
}
252+
253+
/// Get the spending status of an output given a `Txid` and the output
254+
/// index.
255+
pub fn get_output_status(
256+
&self,
257+
txid: Arc<Txid>,
258+
vout: u64,
259+
) -> Result<Option<OutputStatus>, EsploraError> {
260+
self.0
261+
.get_output_status(&txid.0, vout)
262+
.map(|status| status.map(OutputStatus::from))
263+
.map_err(EsploraError::from)
264+
}
243265
}

bdk-ffi/src/types.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ use std::fmt::Display;
3737
use std::sync::{Arc, Mutex};
3838

3939
use crate::{impl_from_core_type, impl_into_core_type};
40+
use bdk_esplora::esplora_client::api::MerkleProof as BdkMerkleProof;
41+
use bdk_esplora::esplora_client::api::OutputStatus as BdkOutputStatus;
4042
use bdk_esplora::esplora_client::api::Tx as BdkTx;
4143
use bdk_esplora::esplora_client::api::TxStatus as BdkTxStatus;
4244

@@ -892,6 +894,46 @@ impl From<BdkTxStatus> for TxStatus {
892894
}
893895
}
894896

897+
#[derive(uniffi::Record, Debug)]
898+
pub struct MerkleProof {
899+
pub block_height: u32,
900+
pub merkle: Vec<Arc<Txid>>,
901+
pub pos: u64,
902+
}
903+
904+
impl From<BdkMerkleProof> for MerkleProof {
905+
fn from(proof: BdkMerkleProof) -> Self {
906+
MerkleProof {
907+
block_height: proof.block_height,
908+
merkle: proof
909+
.merkle
910+
.into_iter()
911+
.map(|h| Arc::new(Txid(h)))
912+
.collect(),
913+
pos: proof.pos as u64,
914+
}
915+
}
916+
}
917+
918+
#[derive(uniffi::Record, Debug)]
919+
pub struct OutputStatus {
920+
pub spent: bool,
921+
pub txid: Option<Arc<Txid>>,
922+
pub vin: Option<u64>,
923+
pub status: Option<TxStatus>,
924+
}
925+
926+
impl From<BdkOutputStatus> for OutputStatus {
927+
fn from(status: BdkOutputStatus) -> Self {
928+
OutputStatus {
929+
spent: status.spent,
930+
txid: status.txid.map(|h| Arc::new(Txid(h))),
931+
vin: status.vin,
932+
status: status.status.map(|s| s.into()),
933+
}
934+
}
935+
}
936+
895937
/// Bitcoin transaction metadata.
896938
#[derive(Debug, uniffi::Record)]
897939
pub struct Tx {

0 commit comments

Comments
 (0)