From 5784a95e48cb37e8386bfff3e5d55d4d0174336a Mon Sep 17 00:00:00 2001 From: LLFourn Date: Tue, 3 Nov 2020 16:03:04 +1100 Subject: [PATCH 1/3] Remove redundant Box around address validators --- examples/address_validator.rs | 2 +- src/wallet/address_validator.rs | 6 +++--- src/wallet/mod.rs | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/address_validator.rs b/examples/address_validator.rs index f5b3d3cc..d37049c6 100644 --- a/examples/address_validator.rs +++ b/examples/address_validator.rs @@ -62,7 +62,7 @@ fn main() -> Result<(), bdk::Error> { let mut wallet: OfflineWallet<_> = Wallet::new_offline(descriptor, None, Network::Regtest, MemoryDatabase::new())?; - wallet.add_address_validator(Arc::new(Box::new(DummyValidator))); + wallet.add_address_validator(Arc::new(DummyValidator)); wallet.get_new_address()?; wallet.get_new_address()?; diff --git a/src/wallet/address_validator.rs b/src/wallet/address_validator.rs index 385977d7..1a76feb9 100644 --- a/src/wallet/address_validator.rs +++ b/src/wallet/address_validator.rs @@ -67,7 +67,7 @@ //! //! let descriptor = "wpkh(tpubD6NzVbkrYhZ4Xferm7Pz4VnjdcDPFyjVu5K4iZXQ4pVN8Cks4pHVowTBXBKRhX64pkRyJZJN5xAKj4UDNnLPb5p2sSKXhewoYx5GbTdUFWq/*)"; //! let mut wallet: OfflineWallet<_> = Wallet::new_offline(descriptor, None, Network::Testnet, MemoryDatabase::default())?; -//! wallet.add_address_validator(Arc::new(Box::new(PrintAddressAndContinue))); +//! wallet.add_address_validator(Arc::new(PrintAddressAndContinue)); //! //! let address = wallet.get_new_address()?; //! println!("Address: {}", address); @@ -140,7 +140,7 @@ mod test { #[should_panic(expected = "InvalidScript")] fn test_address_validator_external() { let (mut wallet, _, _) = get_funded_wallet(get_test_wpkh()); - wallet.add_address_validator(Arc::new(Box::new(TestValidator))); + wallet.add_address_validator(Arc::new(TestValidator)); wallet.get_new_address().unwrap(); } @@ -149,7 +149,7 @@ mod test { #[should_panic(expected = "InvalidScript")] fn test_address_validator_internal() { let (mut wallet, descriptors, _) = get_funded_wallet(get_test_wpkh()); - wallet.add_address_validator(Arc::new(Box::new(TestValidator))); + wallet.add_address_validator(Arc::new(TestValidator)); let addr = testutils!(@external descriptors, 10); wallet diff --git a/src/wallet/mod.rs b/src/wallet/mod.rs index 881a2710..16b9d88b 100644 --- a/src/wallet/mod.rs +++ b/src/wallet/mod.rs @@ -89,7 +89,7 @@ pub struct Wallet { signers: Arc, change_signers: Arc, - address_validators: Vec>>, + address_validators: Vec>, network: Network, @@ -218,7 +218,7 @@ where /// Add an address validator /// /// See [the `address_validator` module](address_validator) for an example. - pub fn add_address_validator(&mut self, validator: Arc>) { + pub fn add_address_validator(&mut self, validator: Arc) { self.address_validators.push(validator); } From 3b3659fc0c401ead8643c62d3a82684580cef744 Mon Sep 17 00:00:00 2001 From: LLFourn Date: Tue, 3 Nov 2020 16:03:44 +1100 Subject: [PATCH 2/3] Remove redundant Box around signers --- src/wallet/mod.rs | 2 +- src/wallet/signer.rs | 22 +++++++++------------- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/src/wallet/mod.rs b/src/wallet/mod.rs index 16b9d88b..0152fcfc 100644 --- a/src/wallet/mod.rs +++ b/src/wallet/mod.rs @@ -205,7 +205,7 @@ where script_type: ScriptType, id: SignerId, ordering: SignerOrdering, - signer: Arc>, + signer: Arc, ) { let signers = match script_type { ScriptType::External => Arc::make_mut(&mut self.signers), diff --git a/src/wallet/signer.rs b/src/wallet/signer.rs index 5ac1ce7a..f0392a80 100644 --- a/src/wallet/signer.rs +++ b/src/wallet/signer.rs @@ -82,7 +82,7 @@ //! ScriptType::External, //! Fingerprint::from_str("e30f11b8").unwrap().into(), //! SignerOrdering(200), -//! Arc::new(Box::new(custom_signer)) +//! Arc::new(custom_signer) //! ); //! //! # Ok::<_, bdk::Error>(()) @@ -320,7 +320,7 @@ impl From<(SignerId, SignerOrdering)> for SignersContainerKey { /// Container for multiple signers #[derive(Debug, Default, Clone)] -pub struct SignersContainer(BTreeMap>>); +pub struct SignersContainer(BTreeMap>); impl SignersContainer { pub fn as_key_map(&self) -> KeyMap { @@ -346,12 +346,12 @@ impl From for SignersContainer { .to_pubkeyhash(), ), SignerOrdering::default(), - Arc::new(Box::new(private_key.key)), + Arc::new(private_key.key), ), DescriptorSecretKey::XPrv(xprv) => container.add_external( SignerId::from(xprv.root_fingerprint()), SignerOrdering::default(), - Arc::new(Box::new(xprv)), + Arc::new(xprv), ), }; } @@ -372,17 +372,13 @@ impl SignersContainer { &mut self, id: SignerId, ordering: SignerOrdering, - signer: Arc>, - ) -> Option>> { + signer: Arc, + ) -> Option> { self.0.insert((id, ordering).into(), signer) } /// Removes a signer from the container and returns it - pub fn remove( - &mut self, - id: SignerId, - ordering: SignerOrdering, - ) -> Option>> { + pub fn remove(&mut self, id: SignerId, ordering: SignerOrdering) -> Option> { self.0.remove(&(id, ordering).into()) } @@ -395,12 +391,12 @@ impl SignersContainer { } /// Returns the list of signers in the container, sorted by lowest to highest `ordering` - pub fn signers(&self) -> Vec<&Arc>> { + pub fn signers(&self) -> Vec<&Arc> { self.0.values().collect() } /// Finds the signer with lowest ordering for a given id in the container. - pub fn find(&self, id: SignerId) -> Option<&Arc>> { + pub fn find(&self, id: SignerId) -> Option<&Arc> { self.0 .range(( Included(&(id.clone(), SignerOrdering(0)).into()), From 796f9f5a70a9579df3ecec4941d104e3466f5df0 Mon Sep 17 00:00:00 2001 From: LLFourn Date: Tue, 3 Nov 2020 16:06:03 +1100 Subject: [PATCH 3/3] Make Signer and AddressValidator Send and Sync --- src/wallet/address_validator.rs | 2 +- src/wallet/signer.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wallet/address_validator.rs b/src/wallet/address_validator.rs index 1a76feb9..cb4087aa 100644 --- a/src/wallet/address_validator.rs +++ b/src/wallet/address_validator.rs @@ -106,7 +106,7 @@ impl std::error::Error for AddressValidatorError {} /// validator will be propagated up to the original caller that triggered the address generation. /// /// For a usage example see [this module](crate::address_validator)'s documentation. -pub trait AddressValidator { +pub trait AddressValidator: Send + Sync { /// Validate or inspect an address fn validate( &self, diff --git a/src/wallet/signer.rs b/src/wallet/signer.rs index f0392a80..24f08aef 100644 --- a/src/wallet/signer.rs +++ b/src/wallet/signer.rs @@ -162,7 +162,7 @@ impl std::error::Error for SignerError {} /// /// This trait can be implemented to provide customized signers to the wallet. For an example see /// [`this module`](crate::wallet::signer)'s documentation. -pub trait Signer: fmt::Debug { +pub trait Signer: fmt::Debug + Send + Sync { /// Sign a PSBT /// /// The `input_index` argument is only provided if the wallet doesn't declare to sign the whole