Skip to content

Commit a7a1d9b

Browse files
fix: non-wildcard descriptors should return an..
..spk only if index is equal to 0
1 parent cc1a43c commit a7a1d9b

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

crates/chain/src/spk_iter.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ where
5252
// If the descriptor doesn't have a wildcard, we shorten whichever range you pass in
5353
// to have length <= 1. This means that if you pass in 0..0 or 0..1 the range will
5454
// 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.
5557
pub(crate) fn new_with_range<R>(descriptor: D, range: R) -> Self
5658
where
5759
R: RangeBounds<u32>,
@@ -100,6 +102,11 @@ where
100102
return None;
101103
}
102104

105+
// If the descriptor is non-wildcard, only index 0 will return an spk.
106+
if !self.descriptor.borrow().has_wildcard() && self.next_index != 0 {
107+
return None;
108+
}
109+
103110
let script = self
104111
.descriptor
105112
.borrow()
@@ -220,6 +227,24 @@ mod test {
220227

221228
assert_eq!(external_spk.nth(0).unwrap(), (0, external_spk_0));
222229
assert_eq!(external_spk.nth(0), None);
230+
231+
// non index-0 should NOT return an spk
232+
assert_eq!(
233+
SpkIterator::new_with_range(&no_wildcard_descriptor, 1..1).next(),
234+
None
235+
);
236+
assert_eq!(
237+
SpkIterator::new_with_range(&no_wildcard_descriptor, 1..=1).next(),
238+
None
239+
);
240+
assert_eq!(
241+
SpkIterator::new_with_range(&no_wildcard_descriptor, 1..2).next(),
242+
None
243+
);
244+
assert_eq!(
245+
SpkIterator::new_with_range(&no_wildcard_descriptor, 1..=2).next(),
246+
None
247+
);
223248
}
224249

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

0 commit comments

Comments
 (0)