Skip to content

Commit

Permalink
feat: esplora client get_tx
Browse files Browse the repository at this point in the history
  • Loading branch information
reez committed Sep 26, 2024
1 parent bdb5174 commit f3e80b1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions bdk-ffi/src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,9 @@ interface EsploraClient {

[Throws=EsploraError]
void broadcast([ByRef] Transaction transaction);

[Throws=EsploraError]
Transaction? get_tx(string txid);
};

// ------------------------------------------------------------------------
Expand Down
9 changes: 9 additions & 0 deletions bdk-ffi/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use bdk_wallet::bitcoin::address::FromScriptError as BdkFromScriptError;
use bdk_wallet::bitcoin::address::ParseError as BdkParseError;
use bdk_wallet::bitcoin::bip32::Error as BdkBip32Error;
use bdk_wallet::bitcoin::consensus::encode::Error as BdkEncodeError;
use bdk_wallet::bitcoin::hashes::hex::HexToArrayError as BdkHexToArrayError;
use bdk_wallet::bitcoin::hex::DisplayHex;
use bdk_wallet::bitcoin::psbt::Error as BdkPsbtError;
use bdk_wallet::bitcoin::psbt::ExtractTxError as BdkExtractTxError;
Expand Down Expand Up @@ -1013,6 +1014,14 @@ impl From<Box<BdkEsploraError>> for EsploraError {
}
}

impl From<BdkHexToArrayError> for EsploraError {
fn from(error: BdkHexToArrayError) -> Self {
EsploraError::Parsing {
error_message: format!("Invalid txid: {}", error),
}
}
}

impl From<BdkExtractTxError> for ExtractTxError {
fn from(error: BdkExtractTxError) -> Self {
match error {
Expand Down
9 changes: 9 additions & 0 deletions bdk-ffi/src/esplora.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ use bdk_wallet::chain::spk_client::SyncResult as BdkSyncResult;
use bdk_wallet::KeychainKind;
use bdk_wallet::Update as BdkUpdate;

use bitcoin_ffi::Txid;

use std::collections::BTreeMap;
use std::str::FromStr;
use std::sync::Arc;

pub struct EsploraClient(BlockingClient);
Expand Down Expand Up @@ -81,4 +84,10 @@ impl EsploraClient {
.broadcast(&bdk_transaction)
.map_err(EsploraError::from)
}

pub fn get_tx(&self, txid: String) -> Result<Option<Arc<Transaction>>, EsploraError> {
let txid = Txid::from_str(&txid)?;
let tx_opt = self.0.get_tx(&txid)?;
Ok(tx_opt.map(|inner| Arc::new(Transaction::from(inner))))
}
}

0 comments on commit f3e80b1

Please sign in to comment.