Skip to content

Commit 71fff16

Browse files
committed
feat(chain): add txout methods to KeychainTxOutIndex
`txouts` and `txouts_in_tx` are exposed from `SpkTxOutIndex`, but modified to remove nested unions. Add `keychain_outpoints_in_range` that iterates over outpoints of a given keychain derivation range.
1 parent 83e7b7e commit 71fff16

File tree

1 file changed

+49
-4
lines changed

1 file changed

+49
-4
lines changed

crates/chain/src/keychain/txout_index.rs

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ use crate::{
55
spk_iter::BIP32_MAX_INDEX,
66
SpkIterator, SpkTxOutIndex,
77
};
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+
};
1013

1114
use crate::Append;
1215

@@ -180,6 +183,25 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
180183
self.inner.outpoints()
181184
}
182185

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+
183205
/// Return the [`TxOut`] of `outpoint` if it has been indexed.
184206
///
185207
/// 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> {
590612
}
591613
}
592614

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
594616
/// `keychain`.
617+
///
618+
/// Use [`keychain_outpoints_in_range`](KeychainTxOutIndex::keychain_outpoints_in_range) to
619+
/// iterate over a specific derivation range.
595620
pub fn keychain_outpoints(
596621
&self,
597622
keychain: &K,
598623
) -> 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+
};
599644
self.inner
600-
.outputs_in_range((keychain.clone(), u32::MIN)..(keychain.clone(), u32::MAX))
645+
.outputs_in_range((start, end))
601646
.map(|((_, i), op)| (*i, op))
602647
}
603648

0 commit comments

Comments
 (0)