Skip to content

Commit 5398d15

Browse files
committed
feat(wallet): Add fallible Wallet try_get_address(), try_get_internal_address functions when a PersistBackend is used
1 parent 10e6d21 commit 5398d15

File tree

4 files changed

+41
-7
lines changed

4 files changed

+41
-7
lines changed

crates/bdk/src/wallet/mod.rs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,24 @@ impl Wallet {
225225
NewError::Persist(_) => unreachable!("no persistence so it can't fail"),
226226
})
227227
}
228+
229+
/// Infallibly return a derived address using the external descriptor, see [`AddressIndex`] for
230+
/// available address index selection strategies. If none of the keys in the descriptor are derivable
231+
/// (i.e. does not end with /*) then the same address will always be returned for any [`AddressIndex`].
232+
pub fn get_address(&mut self, address_index: AddressIndex) -> AddressInfo {
233+
self.try_get_address(address_index).unwrap()
234+
}
235+
236+
/// Infallibly return a derived address using the internal (change) descriptor.
237+
///
238+
/// If the wallet doesn't have an internal descriptor it will use the external descriptor.
239+
///
240+
/// see [`AddressIndex`] for available address index selection strategies. If none of the keys
241+
/// in the descriptor are derivable (i.e. does not end with /*) then the same address will always
242+
/// be returned for any [`AddressIndex`].
243+
pub fn get_internal_address(&mut self, address_index: AddressIndex) -> AddressInfo {
244+
self.try_get_internal_address(address_index).unwrap()
245+
}
228246
}
229247

230248
#[derive(Debug)]
@@ -395,27 +413,37 @@ impl<D> Wallet<D> {
395413
/// Return a derived address using the external descriptor, see [`AddressIndex`] for
396414
/// available address index selection strategies. If none of the keys in the descriptor are derivable
397415
/// (i.e. does not end with /*) then the same address will always be returned for any [`AddressIndex`].
398-
pub fn get_address(&mut self, address_index: AddressIndex) -> AddressInfo
416+
///
417+
/// A `PersistBackend<ChangeSet>::WriteError` will result if unable to persist the new address
418+
/// to the `PersistBackend`.
419+
pub fn try_get_address(
420+
&mut self,
421+
address_index: AddressIndex,
422+
) -> Result<AddressInfo, D::WriteError>
399423
where
400424
D: PersistBackend<ChangeSet>,
401425
{
402426
self._get_address(KeychainKind::External, address_index)
403-
.expect("persistence backend must not fail")
404427
}
405428

406429
/// Return a derived address using the internal (change) descriptor.
407430
///
408431
/// If the wallet doesn't have an internal descriptor it will use the external descriptor.
409432
///
433+
/// A `PersistBackend<ChangeSet>::WriteError` will result if unable to persist the new address
434+
/// to the `PersistBackend`.
435+
///
410436
/// see [`AddressIndex`] for available address index selection strategies. If none of the keys
411437
/// in the descriptor are derivable (i.e. does not end with /*) then the same address will always
412438
/// be returned for any [`AddressIndex`].
413-
pub fn get_internal_address(&mut self, address_index: AddressIndex) -> AddressInfo
439+
pub fn try_get_internal_address(
440+
&mut self,
441+
address_index: AddressIndex,
442+
) -> Result<AddressInfo, D::WriteError>
414443
where
415444
D: PersistBackend<ChangeSet>,
416445
{
417446
self._get_address(KeychainKind::Internal, address_index)
418-
.expect("persistence backend must not fail")
419447
}
420448

421449
/// Return a derived address using the specified `keychain` (external/internal).

example-crates/wallet_electrum/src/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
2929
Network::Testnet,
3030
)?;
3131

32-
let address = wallet.get_address(bdk::wallet::AddressIndex::New);
32+
let address = wallet
33+
.try_get_address(bdk::wallet::AddressIndex::New)
34+
.expect("new_address");
3335
println!("Generated Address: {}", address);
3436

3537
let balance = wallet.get_balance();

example-crates/wallet_esplora_async/src/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
2727
Network::Testnet,
2828
)?;
2929

30-
let address = wallet.get_address(AddressIndex::New);
30+
let address = wallet
31+
.try_get_address(AddressIndex::New)
32+
.expect("new address");
3133
println!("Generated Address: {}", address);
3234

3335
let balance = wallet.get_balance();

example-crates/wallet_esplora_blocking/src/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
2626
Network::Testnet,
2727
)?;
2828

29-
let address = wallet.get_address(AddressIndex::New);
29+
let address = wallet
30+
.try_get_address(AddressIndex::New)
31+
.expect("new address");
3032
println!("Generated Address: {}", address);
3133

3234
let balance = wallet.get_balance();

0 commit comments

Comments
 (0)