feat(chain): expose SpkIterator::new_with_range

Also update docs and add tests
This commit is contained in:
志宇 2024-01-22 14:21:20 +08:00
parent 8d916d7a10
commit 8305e64849
No known key found for this signature in database
GPG Key ID: F6345C9837C2BDE8

View File

@ -43,18 +43,24 @@ impl<D> SpkIterator<D>
where where
D: Borrow<Descriptor<DescriptorPublicKey>>, D: Borrow<Descriptor<DescriptorPublicKey>>,
{ {
/// Creates a new script pubkey iterator starting at 0 from a descriptor. /// Create a new script pubkey iterator from `descriptor`.
///
/// This iterates from derivation index 0 and stops at index 0x7FFFFFFF (as specified in
/// BIP-32). Non-wildcard descriptors will only return one script pubkey at derivation index 0.
///
/// Use [`new_with_range`](SpkIterator::new_with_range) to create an iterator with a specified
/// derivation index range.
pub fn new(descriptor: D) -> Self { pub fn new(descriptor: D) -> Self {
SpkIterator::new_with_range(descriptor, 0..=BIP32_MAX_INDEX) SpkIterator::new_with_range(descriptor, 0..=BIP32_MAX_INDEX)
} }
// Creates a new script pubkey iterator from a descriptor with a given range. /// Create a new script pubkey iterator from `descriptor` and a given `range`.
// If the descriptor doesn't have a wildcard, we shorten whichever range you pass in ///
// to have length <= 1. This means that if you pass in 0..0 or 0..1 the range will /// Non-wildcard descriptors will only emit a single script pubkey (at derivation index 0).
// remain the same, but if you pass in 0..10, we'll shorten it to 0..1 /// Wildcard descriptors have an end-bound of 0x7FFFFFFF (inclusive).
// Also note that if the descriptor doesn't have a wildcard, passing in a range starting ///
// from n > 0, will return an empty iterator. /// Refer to [`new`](SpkIterator::new) for more.
pub(crate) fn new_with_range<R>(descriptor: D, range: R) -> Self pub fn new_with_range<R>(descriptor: D, range: R) -> Self
where where
R: RangeBounds<u32>, R: RangeBounds<u32>,
{ {
@ -73,13 +79,6 @@ where
// Because `end` is exclusive, we want the maximum value to be BIP32_MAX_INDEX + 1. // Because `end` is exclusive, we want the maximum value to be BIP32_MAX_INDEX + 1.
end = end.min(BIP32_MAX_INDEX + 1); end = end.min(BIP32_MAX_INDEX + 1);
if !descriptor.borrow().has_wildcard() {
// The length of the range should be at most 1
if end != start {
end = start + 1;
}
}
Self { Self {
next_index: start, next_index: start,
end, end,
@ -250,6 +249,14 @@ mod test {
SpkIterator::new_with_range(&no_wildcard_descriptor, 1..=2).next(), SpkIterator::new_with_range(&no_wildcard_descriptor, 1..=2).next(),
None None
); );
assert_eq!(
SpkIterator::new_with_range(&no_wildcard_descriptor, 10..11).next(),
None
);
assert_eq!(
SpkIterator::new_with_range(&no_wildcard_descriptor, 10..=10).next(),
None
);
} }
// The following dummy traits were created to test if SpkIterator is working properly. // The following dummy traits were created to test if SpkIterator is working properly.