diff --git a/crates/bdk/examples/compiler.rs b/crates/bdk/examples/compiler.rs index f8918895ce..4a66356d7e 100644 --- a/crates/bdk/examples/compiler.rs +++ b/crates/bdk/examples/compiler.rs @@ -58,7 +58,7 @@ fn main() -> Result<(), Box> { info!( "First derived address from the descriptor: \n{}", - wallet.get_address(New) + wallet.get_address(New).expect("new address") ); // BDK also has it's own `Policy` structure to represent the spending condition in a more diff --git a/crates/bdk/src/wallet/mod.rs b/crates/bdk/src/wallet/mod.rs index 0b6d2a5205..caa8e6afdf 100644 --- a/crates/bdk/src/wallet/mod.rs +++ b/crates/bdk/src/wallet/mod.rs @@ -160,6 +160,24 @@ impl Wallet { NewError::Persist(_) => unreachable!("no persistence so it can't fail"), }) } + + /// 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() + } } #[derive(Debug)] @@ -330,27 +348,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 db80f106d1..dfc5e58486 100644 --- a/example-crates/wallet_electrum/src/main.rs +++ b/example-crates/wallet_electrum/src/main.rs @@ -26,7 +26,9 @@ fn main() -> Result<(), Box> { Network::Testnet, )?; - let address = wallet.get_address(bdk::wallet::AddressIndex::New); + let address = wallet + .try_get_address(bdk::wallet::AddressIndex::New) + .expect("new_address"); println!("Generated Address: {}", address); let balance = wallet.get_balance(); diff --git a/example-crates/wallet_esplora/src/main.rs b/example-crates/wallet_esplora/src/main.rs index 119d9cbd79..8efea3a8a8 100644 --- a/example-crates/wallet_esplora/src/main.rs +++ b/example-crates/wallet_esplora/src/main.rs @@ -26,7 +26,9 @@ fn main() -> Result<(), Box> { Network::Testnet, )?; - let address = wallet.get_address(AddressIndex::New); + let address = wallet + .try_get_address(AddressIndex::New) + .expect("new address"); 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 7cb218ec24..2d05b78bed 100644 --- a/example-crates/wallet_esplora_async/src/main.rs +++ b/example-crates/wallet_esplora_async/src/main.rs @@ -27,7 +27,9 @@ async fn main() -> Result<(), Box> { Network::Testnet, )?; - let address = wallet.get_address(AddressIndex::New); + let address = wallet + .try_get_address(AddressIndex::New) + .expect("new address"); println!("Generated Address: {}", address); let balance = wallet.get_balance();