Skip to content

Commit 48b28e3

Browse files
committed
Merge #1294: Expose SpkIterator::new_with_range
8305e64 feat(chain): expose `SpkIterator::new_with_range` (志宇) Pull request description: ### Description This method is useful, I'm not sure why it is not public. I've also updated the docs and tests. ### Changelog notice Added * Expose `SpkIterator::new_with_range` ### Checklists #### All Submissions: * [x] I've signed all my commits * [x] I followed the [contribution guidelines](https://github.com/bitcoindevkit/bdk/blob/master/CONTRIBUTING.md) * [x] I ran `cargo fmt` and `cargo clippy` before committing #### New Features: * [x] I've added tests for the new feature * [x] I've added docs for the new feature ACKs for top commit: danielabrozzoni: utACK 8305e64 LLFourn: ACK 8305e64 Tree-SHA512: 26389bf65bea74742ebb4d170f3d21aeb02e5be1b3fd20ef16eb318393145809d2b21b96767ebb42acfd8dd789a82bb2d7b29ded24249dabf06bd1e6d23cf0cf
2 parents fbd1d65 + 8305e64 commit 48b28e3

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

crates/chain/src/spk_iter.rs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,24 @@ impl<D> SpkIterator<D>
4343
where
4444
D: Borrow<Descriptor<DescriptorPublicKey>>,
4545
{
46-
/// Creates a new script pubkey iterator starting at 0 from a descriptor.
46+
/// Create a new script pubkey iterator from `descriptor`.
47+
///
48+
/// This iterates from derivation index 0 and stops at index 0x7FFFFFFF (as specified in
49+
/// BIP-32). Non-wildcard descriptors will only return one script pubkey at derivation index 0.
50+
///
51+
/// Use [`new_with_range`](SpkIterator::new_with_range) to create an iterator with a specified
52+
/// derivation index range.
4753
pub fn new(descriptor: D) -> Self {
4854
SpkIterator::new_with_range(descriptor, 0..=BIP32_MAX_INDEX)
4955
}
5056

51-
// Creates a new script pubkey iterator from a descriptor with a given range.
52-
// If the descriptor doesn't have a wildcard, we shorten whichever range you pass in
53-
// to have length <= 1. This means that if you pass in 0..0 or 0..1 the range will
54-
// remain the same, but if you pass in 0..10, we'll shorten it to 0..1
55-
// Also note that if the descriptor doesn't have a wildcard, passing in a range starting
56-
// from n > 0, will return an empty iterator.
57-
pub(crate) fn new_with_range<R>(descriptor: D, range: R) -> Self
57+
/// Create a new script pubkey iterator from `descriptor` and a given `range`.
58+
///
59+
/// Non-wildcard descriptors will only emit a single script pubkey (at derivation index 0).
60+
/// Wildcard descriptors have an end-bound of 0x7FFFFFFF (inclusive).
61+
///
62+
/// Refer to [`new`](SpkIterator::new) for more.
63+
pub fn new_with_range<R>(descriptor: D, range: R) -> Self
5864
where
5965
R: RangeBounds<u32>,
6066
{
@@ -73,13 +79,6 @@ where
7379
// Because `end` is exclusive, we want the maximum value to be BIP32_MAX_INDEX + 1.
7480
end = end.min(BIP32_MAX_INDEX + 1);
7581

76-
if !descriptor.borrow().has_wildcard() {
77-
// The length of the range should be at most 1
78-
if end != start {
79-
end = start + 1;
80-
}
81-
}
82-
8382
Self {
8483
next_index: start,
8584
end,
@@ -250,6 +249,14 @@ mod test {
250249
SpkIterator::new_with_range(&no_wildcard_descriptor, 1..=2).next(),
251250
None
252251
);
252+
assert_eq!(
253+
SpkIterator::new_with_range(&no_wildcard_descriptor, 10..11).next(),
254+
None
255+
);
256+
assert_eq!(
257+
SpkIterator::new_with_range(&no_wildcard_descriptor, 10..=10).next(),
258+
None
259+
);
253260
}
254261

255262
// The following dummy traits were created to test if SpkIterator is working properly.

0 commit comments

Comments
 (0)