Remove unneeded WalletHolder and WalletOperations traits
This commit is contained in:
parent
b1d483463f
commit
a1b89adf84
92
src/lib.rs
92
src/lib.rs
@ -49,10 +49,6 @@ pub enum BlockchainConfig {
|
||||
Esplora { config: EsploraConfig },
|
||||
}
|
||||
|
||||
trait WalletHolder<B> {
|
||||
fn get_wallet(&self) -> MutexGuard<BdkWallet<B, AnyDatabase>>;
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Default)]
|
||||
pub struct TransactionDetails {
|
||||
pub fees: Option<u64>,
|
||||
@ -97,45 +93,6 @@ impl From<&bdk::TransactionDetails> for Transaction {
|
||||
}
|
||||
}
|
||||
|
||||
trait WalletOperations<B>: WalletHolder<B> {
|
||||
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<u64, Error> {
|
||||
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<Vec<Transaction>, Error> {
|
||||
let transactions = self.get_wallet().list_transactions(true)?;
|
||||
Ok(transactions.iter().map(Transaction::from).collect())
|
||||
}
|
||||
}
|
||||
|
||||
struct Wallet {
|
||||
wallet_mutex: Mutex<BdkWallet<AnyBlockchain, AnyDatabase>>,
|
||||
}
|
||||
@ -200,14 +157,6 @@ impl PartiallySignedBitcoinTransaction {
|
||||
}
|
||||
}
|
||||
|
||||
impl WalletHolder<AnyBlockchain> for Wallet {
|
||||
fn get_wallet(&self) -> MutexGuard<BdkWallet<AnyBlockchain, AnyDatabase>> {
|
||||
self.wallet_mutex.lock().expect("wallet")
|
||||
}
|
||||
}
|
||||
|
||||
impl WalletOperations<AnyBlockchain> for Wallet {}
|
||||
|
||||
impl Wallet {
|
||||
fn new(
|
||||
descriptor: String,
|
||||
@ -253,6 +202,10 @@ impl Wallet {
|
||||
Ok(Wallet { wallet_mutex })
|
||||
}
|
||||
|
||||
fn get_wallet(&self) -> MutexGuard<BdkWallet<AnyBlockchain, AnyDatabase>> {
|
||||
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<u64, Error> {
|
||||
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<Vec<Transaction>, Error> {
|
||||
let transactions = self.get_wallet().list_transactions(true)?;
|
||||
Ok(transactions.iter().map(Transaction::from).collect())
|
||||
}
|
||||
|
||||
fn broadcast(&self, psbt: &PartiallySignedBitcoinTransaction) -> Result<String, Error> {
|
||||
let tx = psbt.internal.lock().unwrap().clone().extract_tx();
|
||||
let txid = self.get_wallet().broadcast(&tx)?;
|
||||
|
Loading…
x
Reference in New Issue
Block a user