@@ -5,8 +5,11 @@ use crate::{
5
5
spk_iter:: BIP32_MAX_INDEX ,
6
6
SpkIterator , SpkTxOutIndex ,
7
7
} ;
8
- use bitcoin:: { OutPoint , Script , Transaction , TxOut } ;
9
- use core:: fmt:: Debug ;
8
+ use bitcoin:: { OutPoint , Script , Transaction , TxOut , Txid } ;
9
+ use core:: {
10
+ fmt:: Debug ,
11
+ ops:: { Bound , RangeBounds } ,
12
+ } ;
10
13
11
14
use crate :: Append ;
12
15
@@ -180,6 +183,25 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
180
183
self . inner . outpoints ( )
181
184
}
182
185
186
+ /// Iterate over known txouts that spend to tracked script pubkeys.
187
+ pub fn txouts (
188
+ & self ,
189
+ ) -> impl DoubleEndedIterator < Item = ( K , u32 , OutPoint , & TxOut ) > + ExactSizeIterator {
190
+ self . inner
191
+ . txouts ( )
192
+ . map ( |( ( k, i) , op, txo) | ( k. clone ( ) , * i, op, txo) )
193
+ }
194
+
195
+ /// Finds all txouts on a transaction that has previously been scanned and indexed.
196
+ pub fn txouts_in_tx (
197
+ & self ,
198
+ txid : Txid ,
199
+ ) -> impl DoubleEndedIterator < Item = ( K , u32 , OutPoint , & TxOut ) > {
200
+ self . inner
201
+ . txouts_in_tx ( txid)
202
+ . map ( |( ( k, i) , op, txo) | ( k. clone ( ) , * i, op, txo) )
203
+ }
204
+
183
205
/// Return the [`TxOut`] of `outpoint` if it has been indexed.
184
206
///
185
207
/// The associated keychain and keychain index of the txout's spk is also returned.
@@ -590,14 +612,37 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
590
612
}
591
613
}
592
614
593
- /// Iterates over all the [`OutPoint`] that have a `TxOut` with a script pubkey derived from
615
+ /// Iterate over all [`OutPoint`]s that point to `TxOut`s with script pubkeys derived from
594
616
/// `keychain`.
617
+ ///
618
+ /// Use [`keychain_outpoints_in_range`](KeychainTxOutIndex::keychain_outpoints_in_range) to
619
+ /// iterate over a specific derivation range.
595
620
pub fn keychain_outpoints (
596
621
& self ,
597
622
keychain : & K ,
598
623
) -> impl DoubleEndedIterator < Item = ( u32 , OutPoint ) > + ' _ {
624
+ self . keychain_outpoints_in_range ( keychain, ..)
625
+ }
626
+
627
+ /// Iterate over [`OutPoint`]s that point to `TxOut`s with script pubkeys derived from
628
+ /// `keychain` in a given derivation `range`.
629
+ pub fn keychain_outpoints_in_range (
630
+ & self ,
631
+ keychain : & K ,
632
+ range : impl RangeBounds < u32 > ,
633
+ ) -> impl DoubleEndedIterator < Item = ( u32 , OutPoint ) > + ' _ {
634
+ let start = match range. start_bound ( ) {
635
+ Bound :: Included ( i) => Bound :: Included ( ( keychain. clone ( ) , * i) ) ,
636
+ Bound :: Excluded ( i) => Bound :: Excluded ( ( keychain. clone ( ) , * i) ) ,
637
+ Bound :: Unbounded => Bound :: Unbounded ,
638
+ } ;
639
+ let end = match range. end_bound ( ) {
640
+ Bound :: Included ( i) => Bound :: Included ( ( keychain. clone ( ) , * i) ) ,
641
+ Bound :: Excluded ( i) => Bound :: Excluded ( ( keychain. clone ( ) , * i) ) ,
642
+ Bound :: Unbounded => Bound :: Unbounded ,
643
+ } ;
599
644
self . inner
600
- . outputs_in_range ( ( keychain . clone ( ) , u32 :: MIN ) .. ( keychain . clone ( ) , u32 :: MAX ) )
645
+ . outputs_in_range ( ( start , end ) )
601
646
. map ( |( ( _, i) , op) | ( * i, op) )
602
647
}
603
648
0 commit comments