From 77f9977c02683071748f5fd6d07bde4b2d1f4a12 Mon Sep 17 00:00:00 2001 From: Steve Myers Date: Thu, 16 Nov 2023 11:18:11 -0600 Subject: [PATCH] feat(wallet): Add infallible Wallet get_address(), get_internal_address functions refactor!(wallet)!: rename fallible Wallet try_get_address(), try_get_internal_address functions --- crates/bdk/src/wallet/mod.rs | 41 +++++++++++++++++-- example-crates/wallet_electrum/src/main.rs | 2 +- .../wallet_esplora_async/src/main.rs | 2 +- .../wallet_esplora_blocking/src/main.rs | 2 +- 4 files changed, 40 insertions(+), 7 deletions(-) diff --git a/crates/bdk/src/wallet/mod.rs b/crates/bdk/src/wallet/mod.rs index 005d75f9e..240cc1050 100644 --- a/crates/bdk/src/wallet/mod.rs +++ b/crates/bdk/src/wallet/mod.rs @@ -259,6 +259,29 @@ impl Wallet { } } +impl Wallet +where + D: PersistBackend, +{ + /// Infallibly return a derived address using the external descriptor, see [`AddressIndex`] for + /// available address index selection strategies. If none of the keys in the descriptor are derivable + /// (i.e. does not end with /*) then the same address will always be returned for any [`AddressIndex`]. + pub fn get_address(&mut self, address_index: AddressIndex) -> AddressInfo { + self.try_get_address(address_index).unwrap() + } + + /// Infallibly return a derived address using the internal (change) descriptor. + /// + /// If the wallet doesn't have an internal descriptor it will use the external descriptor. + /// + /// see [`AddressIndex`] for available address index selection strategies. If none of the keys + /// in the descriptor are derivable (i.e. does not end with /*) then the same address will always + /// be returned for any [`AddressIndex`]. + pub fn get_internal_address(&mut self, address_index: AddressIndex) -> AddressInfo { + self.try_get_internal_address(address_index).unwrap() + } +} + /// The error type when constructing a fresh [`Wallet`]. /// /// Methods [`new`] and [`new_with_genesis_hash`] may return this error. @@ -611,27 +634,37 @@ impl Wallet { /// Return a derived address using the external descriptor, see [`AddressIndex`] for /// available address index selection strategies. If none of the keys in the descriptor are derivable /// (i.e. does not end with /*) then the same address will always be returned for any [`AddressIndex`]. - pub fn get_address(&mut self, address_index: AddressIndex) -> AddressInfo + /// + /// A `PersistBackend::WriteError` will result if unable to persist the new address + /// to the `PersistBackend`. + pub fn try_get_address( + &mut self, + address_index: AddressIndex, + ) -> Result where D: PersistBackend, { self._get_address(KeychainKind::External, address_index) - .expect("persistence backend must not fail") } /// Return a derived address using the internal (change) descriptor. /// /// If the wallet doesn't have an internal descriptor it will use the external descriptor. /// + /// A `PersistBackend::WriteError` will result if unable to persist the new address + /// to the `PersistBackend`. + /// /// see [`AddressIndex`] for available address index selection strategies. If none of the keys /// in the descriptor are derivable (i.e. does not end with /*) then the same address will always /// be returned for any [`AddressIndex`]. - pub fn get_internal_address(&mut self, address_index: AddressIndex) -> AddressInfo + pub fn try_get_internal_address( + &mut self, + address_index: AddressIndex, + ) -> Result where D: PersistBackend, { self._get_address(KeychainKind::Internal, address_index) - .expect("persistence backend must not fail") } /// Return a derived address using the specified `keychain` (external/internal). diff --git a/example-crates/wallet_electrum/src/main.rs b/example-crates/wallet_electrum/src/main.rs index 6f9d93ea2..9d4c6c5a4 100644 --- a/example-crates/wallet_electrum/src/main.rs +++ b/example-crates/wallet_electrum/src/main.rs @@ -29,7 +29,7 @@ fn main() -> Result<(), anyhow::Error> { Network::Testnet, )?; - let address = wallet.get_address(bdk::wallet::AddressIndex::New); + let address = wallet.try_get_address(bdk::wallet::AddressIndex::New)?; println!("Generated Address: {}", address); let balance = wallet.get_balance(); diff --git a/example-crates/wallet_esplora_async/src/main.rs b/example-crates/wallet_esplora_async/src/main.rs index c438505c0..fb8f7b510 100644 --- a/example-crates/wallet_esplora_async/src/main.rs +++ b/example-crates/wallet_esplora_async/src/main.rs @@ -27,7 +27,7 @@ async fn main() -> Result<(), anyhow::Error> { Network::Testnet, )?; - let address = wallet.get_address(AddressIndex::New); + let address = wallet.try_get_address(AddressIndex::New)?; println!("Generated Address: {}", address); let balance = wallet.get_balance(); diff --git a/example-crates/wallet_esplora_blocking/src/main.rs b/example-crates/wallet_esplora_blocking/src/main.rs index b0c0a9382..09e7c3ad4 100644 --- a/example-crates/wallet_esplora_blocking/src/main.rs +++ b/example-crates/wallet_esplora_blocking/src/main.rs @@ -26,7 +26,7 @@ fn main() -> Result<(), anyhow::Error> { Network::Testnet, )?; - let address = wallet.get_address(AddressIndex::New); + let address = wallet.try_get_address(AddressIndex::New)?; println!("Generated Address: {}", address); let balance = wallet.get_balance();