Skip to content

Commit 77f9977

Browse files
committed
feat(wallet): Add infallible Wallet get_address(), get_internal_address functions
refactor!(wallet)!: rename fallible Wallet try_get_address(), try_get_internal_address functions
1 parent 9e7d99e commit 77f9977

File tree

4 files changed

+40
-7
lines changed

4 files changed

+40
-7
lines changed

crates/bdk/src/wallet/mod.rs

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,29 @@ impl Wallet {
259259
}
260260
}
261261

262+
impl<D> Wallet<D>
263+
where
264+
D: PersistBackend<ChangeSet, WriteError = core::convert::Infallible>,
265+
{
266+
/// Infallibly return a derived address using the external descriptor, see [`AddressIndex`] for
267+
/// available address index selection strategies. If none of the keys in the descriptor are derivable
268+
/// (i.e. does not end with /*) then the same address will always be returned for any [`AddressIndex`].
269+
pub fn get_address(&mut self, address_index: AddressIndex) -> AddressInfo {
270+
self.try_get_address(address_index).unwrap()
271+
}
272+
273+
/// Infallibly return a derived address using the internal (change) descriptor.
274+
///
275+
/// If the wallet doesn't have an internal descriptor it will use the external descriptor.
276+
///
277+
/// see [`AddressIndex`] for available address index selection strategies. If none of the keys
278+
/// in the descriptor are derivable (i.e. does not end with /*) then the same address will always
279+
/// be returned for any [`AddressIndex`].
280+
pub fn get_internal_address(&mut self, address_index: AddressIndex) -> AddressInfo {
281+
self.try_get_internal_address(address_index).unwrap()
282+
}
283+
}
284+
262285
/// The error type when constructing a fresh [`Wallet`].
263286
///
264287
/// Methods [`new`] and [`new_with_genesis_hash`] may return this error.
@@ -611,27 +634,37 @@ impl<D> Wallet<D> {
611634
/// Return a derived address using the external descriptor, see [`AddressIndex`] for
612635
/// available address index selection strategies. If none of the keys in the descriptor are derivable
613636
/// (i.e. does not end with /*) then the same address will always be returned for any [`AddressIndex`].
614-
pub fn get_address(&mut self, address_index: AddressIndex) -> AddressInfo
637+
///
638+
/// A `PersistBackend<ChangeSet>::WriteError` will result if unable to persist the new address
639+
/// to the `PersistBackend`.
640+
pub fn try_get_address(
641+
&mut self,
642+
address_index: AddressIndex,
643+
) -> Result<AddressInfo, D::WriteError>
615644
where
616645
D: PersistBackend<ChangeSet>,
617646
{
618647
self._get_address(KeychainKind::External, address_index)
619-
.expect("persistence backend must not fail")
620648
}
621649

622650
/// Return a derived address using the internal (change) descriptor.
623651
///
624652
/// If the wallet doesn't have an internal descriptor it will use the external descriptor.
625653
///
654+
/// A `PersistBackend<ChangeSet>::WriteError` will result if unable to persist the new address
655+
/// to the `PersistBackend`.
656+
///
626657
/// see [`AddressIndex`] for available address index selection strategies. If none of the keys
627658
/// in the descriptor are derivable (i.e. does not end with /*) then the same address will always
628659
/// be returned for any [`AddressIndex`].
629-
pub fn get_internal_address(&mut self, address_index: AddressIndex) -> AddressInfo
660+
pub fn try_get_internal_address(
661+
&mut self,
662+
address_index: AddressIndex,
663+
) -> Result<AddressInfo, D::WriteError>
630664
where
631665
D: PersistBackend<ChangeSet>,
632666
{
633667
self._get_address(KeychainKind::Internal, address_index)
634-
.expect("persistence backend must not fail")
635668
}
636669

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

example-crates/wallet_electrum/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ fn main() -> Result<(), anyhow::Error> {
2929
Network::Testnet,
3030
)?;
3131

32-
let address = wallet.get_address(bdk::wallet::AddressIndex::New);
32+
let address = wallet.try_get_address(bdk::wallet::AddressIndex::New)?;
3333
println!("Generated Address: {}", address);
3434

3535
let balance = wallet.get_balance();

example-crates/wallet_esplora_async/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ async fn main() -> Result<(), anyhow::Error> {
2727
Network::Testnet,
2828
)?;
2929

30-
let address = wallet.get_address(AddressIndex::New);
30+
let address = wallet.try_get_address(AddressIndex::New)?;
3131
println!("Generated Address: {}", address);
3232

3333
let balance = wallet.get_balance();

example-crates/wallet_esplora_blocking/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn main() -> Result<(), anyhow::Error> {
2626
Network::Testnet,
2727
)?;
2828

29-
let address = wallet.get_address(AddressIndex::New);
29+
let address = wallet.try_get_address(AddressIndex::New)?;
3030
println!("Generated Address: {}", address);
3131

3232
let balance = wallet.get_balance();

0 commit comments

Comments
 (0)