fix: non-wildcard descriptors should return an..

..spk only if index is equal to 0
This commit is contained in:
Daniela Brozzoni 2023-08-31 17:48:56 +02:00
parent cc1a43c495
commit a7a1d9b2fb
No known key found for this signature in database
GPG Key ID: 7DE4F1FDCED0AB87

View File

@ -52,6 +52,8 @@ where
// If the descriptor doesn't have a wildcard, we shorten whichever range you pass in // 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 // to have length <= 1. This means that if you pass in 0..0 or 0..1 the range will
// remain the same, but if you pass in 0..10, we'll shorten it to 0..1 // remain the same, but if you pass in 0..10, we'll shorten it to 0..1
// Also note that if the descriptor doesn't have a wildcard, passing in a range starting
// from n > 0, will return an empty iterator.
pub(crate) fn new_with_range<R>(descriptor: D, range: R) -> Self pub(crate) fn new_with_range<R>(descriptor: D, range: R) -> Self
where where
R: RangeBounds<u32>, R: RangeBounds<u32>,
@ -100,6 +102,11 @@ where
return None; return None;
} }
// If the descriptor is non-wildcard, only index 0 will return an spk.
if !self.descriptor.borrow().has_wildcard() && self.next_index != 0 {
return None;
}
let script = self let script = self
.descriptor .descriptor
.borrow() .borrow()
@ -220,6 +227,24 @@ mod test {
assert_eq!(external_spk.nth(0).unwrap(), (0, external_spk_0)); assert_eq!(external_spk.nth(0).unwrap(), (0, external_spk_0));
assert_eq!(external_spk.nth(0), None); assert_eq!(external_spk.nth(0), None);
// non index-0 should NOT return an spk
assert_eq!(
SpkIterator::new_with_range(&no_wildcard_descriptor, 1..1).next(),
None
);
assert_eq!(
SpkIterator::new_with_range(&no_wildcard_descriptor, 1..=1).next(),
None
);
assert_eq!(
SpkIterator::new_with_range(&no_wildcard_descriptor, 1..2).next(),
None
);
assert_eq!(
SpkIterator::new_with_range(&no_wildcard_descriptor, 1..=2).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.