Skip to content

Commit af2475e

Browse files
ItoroDthunderbiscuit
authored andcommitted
feat: Add method to get block by hash for esplora
1 parent 35c3d47 commit af2475e

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

bdk-ffi/src/bitcoin.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use std::collections::HashMap;
1111
use bdk_wallet::bitcoin::address::NetworkChecked;
1212
use bdk_wallet::bitcoin::address::NetworkUnchecked;
1313
use bdk_wallet::bitcoin::address::{Address as BdkAddress, AddressData as BdkAddressData};
14+
use bdk_wallet::bitcoin::blockdata::block::Block as BdkBlock;
1415
use bdk_wallet::bitcoin::blockdata::block::Header as BdkHeader;
1516
use bdk_wallet::bitcoin::consensus::encode::deserialize;
1617
use bdk_wallet::bitcoin::consensus::encode::serialize;
@@ -299,6 +300,27 @@ impl From<BdkHeader> for Header {
299300
}
300301
}
301302

303+
/// Bitcoin block.
304+
/// A collection of transactions with an attached proof of work.
305+
#[derive(uniffi::Record)]
306+
pub struct Block {
307+
pub header: Header,
308+
pub txdata: Vec<Arc<Transaction>>,
309+
}
310+
311+
impl From<BdkBlock> for Block {
312+
fn from(bdk_block: BdkBlock) -> Self {
313+
Block {
314+
header: bdk_block.header.into(),
315+
txdata: bdk_block
316+
.txdata
317+
.into_iter()
318+
.map(|tx| Arc::new(tx.into()))
319+
.collect(),
320+
}
321+
}
322+
}
323+
302324
/// The type of address.
303325
#[derive(Debug, uniffi::Enum)]
304326
pub enum AddressData {

bdk-ffi/src/esplora.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::bitcoin::Address;
2+
use crate::bitcoin::Block;
23
use crate::bitcoin::BlockHash;
34
use crate::bitcoin::Header;
45
use crate::bitcoin::Transaction;
@@ -178,6 +179,17 @@ impl EsploraClient {
178179
.map_err(EsploraError::from)
179180
}
180181

182+
/// Get a Block given a particular BlockHash.
183+
pub fn get_block_by_hash(
184+
&self,
185+
block_hash: Arc<BlockHash>,
186+
) -> Result<Option<Block>, EsploraError> {
187+
self.0
188+
.get_block_by_hash(&block_hash.0)
189+
.map(|block| block.map(|block| block.into()))
190+
.map_err(EsploraError::from)
191+
}
192+
181193
/// Get a `Txid` of a transaction given its index in a block with a given hash.
182194
pub fn get_txid_at_block_index(
183195
&self,

0 commit comments

Comments
 (0)