From 28a9b302d593a04c75dd6a0ec415746079d5739a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zoe=20Faltib=C3=A0?= Date: Sat, 18 Jun 2022 15:38:39 +0200 Subject: [PATCH] Post review changes --- src/bdk.udl | 1 + src/lib.rs | 35 ++++++++++++++++------------------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/bdk.udl b/src/bdk.udl index e9d1d24..54952ef 100644 --- a/src/bdk.udl +++ b/src/bdk.udl @@ -168,6 +168,7 @@ dictionary LocalUtxo { OutPoint outpoint; TxOut txout; KeychainKind keychain; + boolean is_spent; }; interface Wallet { diff --git a/src/lib.rs b/src/lib.rs index 54b749d..fd2a7ed 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,7 +15,8 @@ use bdk::miniscript::BareCtx; use bdk::wallet::AddressIndex as BdkAddressIndex; use bdk::wallet::AddressInfo as BdkAddressInfo; use bdk::{ - BlockTime, Error, FeeRate, SignOptions, SyncOptions as BdkSyncOptions, Wallet as BdkWallet, + BlockTime, Error, FeeRate, KeychainKind, SignOptions, SyncOptions as BdkSyncOptions, + Wallet as BdkWallet, }; use std::convert::{From, TryFrom}; use std::fmt; @@ -182,27 +183,15 @@ pub struct TxOut { address: String, } -pub enum KeychainKind { - External = 0, - Internal = 1, -} - -impl From for KeychainKind { - fn from(x: bdk::KeychainKind) -> KeychainKind { - match x { - bdk::KeychainKind::External => KeychainKind::External, - bdk::KeychainKind::Internal => KeychainKind::Internal, - } - } -} - pub struct LocalUtxo { outpoint: OutPoint, txout: TxOut, keychain: KeychainKind, + is_spent: bool, } -trait NetworkLocalUtxo: { +/// Trait used to convert a script to an address using the wallet network +trait NetworkLocalUtxo { fn from_utxo(x: &bdk::LocalUtxo, network: Network) -> LocalUtxo; } @@ -216,9 +205,14 @@ impl NetworkLocalUtxo for LocalUtxo { txout: TxOut { value: x.txout.value, address: bdk::bitcoin::util::address::Address::from_script( - &x.txout.script_pubkey, network).unwrap().to_string(), + &x.txout.script_pubkey, + network, + ) + .unwrap() + .to_string(), }, - keychain: KeychainKind::from(x.keychain), + keychain: x.keychain, + is_spent: x.is_spent, } } } @@ -337,7 +331,10 @@ impl Wallet { fn list_unspent(&self) -> Result, Error> { let unspents = self.get_wallet().list_unspent()?; - Ok(unspents.iter().map(|u| LocalUtxo::from_utxo(u, self.get_network())).collect()) + Ok(unspents + .iter() + .map(|u| LocalUtxo::from_utxo(u, self.get_network())) + .collect()) } }