]> Untitled Git - bdk/commitdiff
fix: non-wildcard descriptors should return an..
authorDaniela Brozzoni <danielabrozzoni@protonmail.com>
Thu, 31 Aug 2023 15:48:56 +0000 (17:48 +0200)
committerDaniela Brozzoni <danielabrozzoni@protonmail.com>
Fri, 1 Sep 2023 07:12:44 +0000 (09:12 +0200)
..spk only if index is equal to 0

crates/chain/src/spk_iter.rs

index bce436ed3515872c40ee449ffe7803f8cded4e4e..a7331ff95269262a401de93d8b47e9fdb30aafec 100644 (file)
@@ -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<R>(descriptor: D, range: R) -> Self
     where
         R: RangeBounds<u32>,
@@ -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.