fix(wallet): use efficient peek address logic
Changes the peek address logic to use the optimized `Iterator::nth` implementation of `SpkIterator`. Additionally, docs are added for panics that will occur when the caller requests for addresses with out-of-bound derivation indices (BIP32).
This commit is contained in:
parent
81aeaba48a
commit
b74c2e2622
@ -262,6 +262,11 @@ where
|
|||||||
/// Infallibly return a derived address using the external descriptor, see [`AddressIndex`] for
|
/// 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
|
/// 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`].
|
/// (i.e. does not end with /*) then the same address will always be returned for any [`AddressIndex`].
|
||||||
|
///
|
||||||
|
/// # Panics
|
||||||
|
///
|
||||||
|
/// This panics when the caller requests for an address of derivation index greater than the
|
||||||
|
/// BIP32 max index.
|
||||||
pub fn get_address(&mut self, address_index: AddressIndex) -> AddressInfo {
|
pub fn get_address(&mut self, address_index: AddressIndex) -> AddressInfo {
|
||||||
self.try_get_address(address_index).unwrap()
|
self.try_get_address(address_index).unwrap()
|
||||||
}
|
}
|
||||||
@ -273,6 +278,11 @@ where
|
|||||||
/// see [`AddressIndex`] for available address index selection strategies. If none of the keys
|
/// 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
|
/// in the descriptor are derivable (i.e. does not end with /*) then the same address will always
|
||||||
/// be returned for any [`AddressIndex`].
|
/// be returned for any [`AddressIndex`].
|
||||||
|
///
|
||||||
|
/// # Panics
|
||||||
|
///
|
||||||
|
/// This panics when the caller requests for an address of derivation index greater than the
|
||||||
|
/// BIP32 max index.
|
||||||
pub fn get_internal_address(&mut self, address_index: AddressIndex) -> AddressInfo {
|
pub fn get_internal_address(&mut self, address_index: AddressIndex) -> AddressInfo {
|
||||||
self.try_get_internal_address(address_index).unwrap()
|
self.try_get_internal_address(address_index).unwrap()
|
||||||
}
|
}
|
||||||
@ -649,6 +659,11 @@ impl<D> Wallet<D> {
|
|||||||
///
|
///
|
||||||
/// A `PersistBackend<ChangeSet>::WriteError` will result if unable to persist the new address
|
/// A `PersistBackend<ChangeSet>::WriteError` will result if unable to persist the new address
|
||||||
/// to the `PersistBackend`.
|
/// to the `PersistBackend`.
|
||||||
|
///
|
||||||
|
/// # Panics
|
||||||
|
///
|
||||||
|
/// This panics when the caller requests for an address of derivation index greater than the
|
||||||
|
/// BIP32 max index.
|
||||||
pub fn try_get_address(
|
pub fn try_get_address(
|
||||||
&mut self,
|
&mut self,
|
||||||
address_index: AddressIndex,
|
address_index: AddressIndex,
|
||||||
@ -669,6 +684,11 @@ impl<D> Wallet<D> {
|
|||||||
/// see [`AddressIndex`] for available address index selection strategies. If none of the keys
|
/// 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
|
/// in the descriptor are derivable (i.e. does not end with /*) then the same address will always
|
||||||
/// be returned for any [`AddressIndex`].
|
/// be returned for any [`AddressIndex`].
|
||||||
|
///
|
||||||
|
/// # Panics
|
||||||
|
///
|
||||||
|
/// This panics when the caller requests for an address of derivation index greater than the
|
||||||
|
/// BIP32 max index.
|
||||||
pub fn try_get_internal_address(
|
pub fn try_get_internal_address(
|
||||||
&mut self,
|
&mut self,
|
||||||
address_index: AddressIndex,
|
address_index: AddressIndex,
|
||||||
@ -691,6 +711,11 @@ impl<D> Wallet<D> {
|
|||||||
/// See [`AddressIndex`] for available address index selection strategies. If none of the keys
|
/// 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
|
/// in the descriptor are derivable (i.e. does not end with /*) then the same address will
|
||||||
/// always be returned for any [`AddressIndex`].
|
/// always be returned for any [`AddressIndex`].
|
||||||
|
///
|
||||||
|
/// # Panics
|
||||||
|
///
|
||||||
|
/// This panics when the caller requests for an address of derivation index greater than the
|
||||||
|
/// BIP32 max index.
|
||||||
fn _get_address(
|
fn _get_address(
|
||||||
&mut self,
|
&mut self,
|
||||||
keychain: KeychainKind,
|
keychain: KeychainKind,
|
||||||
@ -710,12 +735,14 @@ impl<D> Wallet<D> {
|
|||||||
let ((index, spk), index_changeset) = txout_index.next_unused_spk(&keychain);
|
let ((index, spk), index_changeset) = txout_index.next_unused_spk(&keychain);
|
||||||
(index, spk.into(), Some(index_changeset))
|
(index, spk.into(), Some(index_changeset))
|
||||||
}
|
}
|
||||||
AddressIndex::Peek(index) => {
|
AddressIndex::Peek(mut peek_index) => {
|
||||||
let (index, spk) = txout_index
|
let mut spk_iter = txout_index.unbounded_spk_iter(&keychain);
|
||||||
.unbounded_spk_iter(&keychain)
|
if !spk_iter.descriptor().has_wildcard() {
|
||||||
.take(index as usize + 1)
|
peek_index = 0;
|
||||||
.last()
|
}
|
||||||
.unwrap();
|
let (index, spk) = spk_iter
|
||||||
|
.nth(peek_index as usize)
|
||||||
|
.expect("derivation index is out of bounds");
|
||||||
(index, spk, None)
|
(index, spk, None)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user