From a1b89adf847a44b2631a23ae4ee646478cf8cfee Mon Sep 17 00:00:00 2001 From: Steve Myers Date: Sun, 20 Mar 2022 19:59:20 -0500 Subject: [PATCH] Remove unneeded WalletHolder and WalletOperations traits --- src/lib.rs | 92 ++++++++++++++++++++++++------------------------------ 1 file changed, 41 insertions(+), 51 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 9a133cc..7cd97e9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -49,10 +49,6 @@ pub enum BlockchainConfig { Esplora { config: EsploraConfig }, } -trait WalletHolder { - fn get_wallet(&self) -> MutexGuard>; -} - #[derive(Debug, Clone, PartialEq, Eq, Default)] pub struct TransactionDetails { pub fees: Option, @@ -97,45 +93,6 @@ impl From<&bdk::TransactionDetails> for Transaction { } } -trait WalletOperations: WalletHolder { - fn get_new_address(&self) -> String { - self.get_wallet() - .get_address(AddressIndex::New) - .unwrap() - .address - .to_string() - } - - fn get_last_unused_address(&self) -> String { - self.get_wallet() - .get_address(AddressIndex::LastUnused) - .unwrap() - .address - .to_string() - } - - fn get_balance(&self) -> Result { - self.get_wallet().get_balance() - } - - fn sign(&self, psbt: &PartiallySignedBitcoinTransaction) -> Result<(), Error> { - let mut psbt = psbt.internal.lock().unwrap(); - let finalized = self.get_wallet().sign(&mut psbt, SignOptions::default())?; - match finalized { - true => Ok(()), - false => Err(BdkError::Generic(format!( - "transaction signing not finalized {:?}", - psbt - ))), - } - } - - fn get_transactions(&self) -> Result, Error> { - let transactions = self.get_wallet().list_transactions(true)?; - Ok(transactions.iter().map(Transaction::from).collect()) - } -} - struct Wallet { wallet_mutex: Mutex>, } @@ -200,14 +157,6 @@ impl PartiallySignedBitcoinTransaction { } } -impl WalletHolder for Wallet { - fn get_wallet(&self) -> MutexGuard> { - self.wallet_mutex.lock().expect("wallet") - } -} - -impl WalletOperations for Wallet {} - impl Wallet { fn new( descriptor: String, @@ -253,6 +202,10 @@ impl Wallet { Ok(Wallet { wallet_mutex }) } + fn get_wallet(&self) -> MutexGuard> { + self.wallet_mutex.lock().expect("wallet") + } + fn get_network(&self) -> Network { self.get_wallet().network() } @@ -266,6 +219,43 @@ impl Wallet { .sync(BdkProgressHolder { progress_update }, max_address_param) } + fn get_new_address(&self) -> String { + self.get_wallet() + .get_address(AddressIndex::New) + .unwrap() + .address + .to_string() + } + + fn get_last_unused_address(&self) -> String { + self.get_wallet() + .get_address(AddressIndex::LastUnused) + .unwrap() + .address + .to_string() + } + + fn get_balance(&self) -> Result { + self.get_wallet().get_balance() + } + + fn sign(&self, psbt: &PartiallySignedBitcoinTransaction) -> Result<(), Error> { + let mut psbt = psbt.internal.lock().unwrap(); + let finalized = self.get_wallet().sign(&mut psbt, SignOptions::default())?; + match finalized { + true => Ok(()), + false => Err(BdkError::Generic(format!( + "transaction signing not finalized {:?}", + psbt + ))), + } + } + + fn get_transactions(&self) -> Result, Error> { + let transactions = self.get_wallet().list_transactions(true)?; + Ok(transactions.iter().map(Transaction::from).collect()) + } + fn broadcast(&self, psbt: &PartiallySignedBitcoinTransaction) -> Result { let tx = psbt.internal.lock().unwrap().clone().extract_tx(); let txid = self.get_wallet().broadcast(&tx)?;