From 806e29b93ce0c7934993428363337e136a6fcf95 Mon Sep 17 00:00:00 2001 From: thunderbiscuit Date: Wed, 10 Apr 2024 12:12:37 -0400 Subject: [PATCH] feat: expose get_tx method on wallet type --- bdk-ffi/src/bdk.udl | 8 ++++++++ bdk-ffi/src/error.rs | 12 ++++++------ bdk-ffi/src/lib.rs | 1 + bdk-ffi/src/wallet.rs | 10 +++++++++- 4 files changed, 24 insertions(+), 7 deletions(-) diff --git a/bdk-ffi/src/bdk.udl b/bdk-ffi/src/bdk.udl index 3c9211d..d0371ca 100644 --- a/bdk-ffi/src/bdk.udl +++ b/bdk-ffi/src/bdk.udl @@ -104,6 +104,11 @@ interface DescriptorError { Hex(string e); }; +[Error] +interface TxidParseError { + InvalidTxid(string txid); +}; + // ------------------------------------------------------------------------ // bdk crate - types module // ------------------------------------------------------------------------ @@ -212,6 +217,9 @@ interface Wallet { sequence transactions(); + [Throws=TxidParseError] + CanonicalTx? get_tx(string txid); + [Throws=CalculateFeeError] u64 calculate_fee([ByRef] Transaction tx); diff --git a/bdk-ffi/src/error.rs b/bdk-ffi/src/error.rs index 18aba8c..8c34f2d 100644 --- a/bdk-ffi/src/error.rs +++ b/bdk-ffi/src/error.rs @@ -109,6 +109,12 @@ pub enum FeeRateError { ArithmeticOverflow, } +#[derive(Debug, thiserror::Error)] +pub enum TxidParseError { + #[error("invalid txid: {txid}")] + InvalidTxid { txid: String }, +} + #[derive(Debug, thiserror::Error)] pub enum AddressError { #[error("base58 address encoding error")] @@ -318,12 +324,6 @@ impl From for PersistenceError { } } -// impl From for Alpha3Error { -// fn from(_: DescriptorError) -> Self { -// Alpha3Error::Generic -// } -// } - impl From for Alpha3Error { fn from(_: AllowShrinkingError) -> Self { Alpha3Error::Generic diff --git a/bdk-ffi/src/lib.rs b/bdk-ffi/src/lib.rs index 369e422..78795b7 100644 --- a/bdk-ffi/src/lib.rs +++ b/bdk-ffi/src/lib.rs @@ -22,6 +22,7 @@ use crate::error::FeeRateError; use crate::error::PersistenceError; use crate::error::PsbtParseError; use crate::error::TransactionError; +use crate::error::TxidParseError; use crate::error::WalletCreationError; use crate::esplora::EsploraClient; use crate::keys::DerivationPath; diff --git a/bdk-ffi/src/wallet.rs b/bdk-ffi/src/wallet.rs index 49b0133..987d7be 100644 --- a/bdk-ffi/src/wallet.rs +++ b/bdk-ffi/src/wallet.rs @@ -1,6 +1,8 @@ use crate::bitcoin::{OutPoint, PartiallySignedTransaction, Script, Transaction}; use crate::descriptor::Descriptor; -use crate::error::{Alpha3Error, CalculateFeeError, PersistenceError, WalletCreationError}; +use crate::error::{ + Alpha3Error, CalculateFeeError, PersistenceError, TxidParseError, WalletCreationError, +}; use crate::types::{AddressIndex, AddressInfo, Balance, CanonicalTx, FeeRate, ScriptAmount}; use bdk::bitcoin::blockdata::script::ScriptBuf as BdkScriptBuf; @@ -108,6 +110,12 @@ impl Wallet { .collect() } + pub fn get_tx(&self, txid: String) -> Result, TxidParseError> { + let txid = + Txid::from_str(txid.as_str()).map_err(|_| TxidParseError::InvalidTxid { txid })?; + Ok(self.get_wallet().get_tx(txid).map(|tx| tx.into())) + } + pub fn calculate_fee(&self, tx: &Transaction) -> Result { self.get_wallet() .calculate_fee(&tx.into())