From a7a1d9b2fb9fda4d86fe17cf90d9c0d46c4d8609 Mon Sep 17 00:00:00 2001 From: Daniela Brozzoni Date: Thu, 31 Aug 2023 17:48:56 +0200 Subject: [PATCH] fix: non-wildcard descriptors should return an.. ..spk only if index is equal to 0 --- crates/chain/src/spk_iter.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/crates/chain/src/spk_iter.rs b/crates/chain/src/spk_iter.rs index bce436ed..a7331ff9 100644 --- a/crates/chain/src/spk_iter.rs +++ b/crates/chain/src/spk_iter.rs @@ -52,6 +52,8 @@ where // 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 // 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(descriptor: D, range: R) -> Self where R: RangeBounds, @@ -100,6 +102,11 @@ where 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 .descriptor .borrow() @@ -220,6 +227,24 @@ mod test { assert_eq!(external_spk.nth(0).unwrap(), (0, external_spk_0)); 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.