2023-03-01 11:09:08 +01:00
use crate ::{
collections ::* ,
2023-05-10 14:14:29 +08:00
indexed_tx_graph ::Indexer ,
2023-03-01 11:09:08 +01:00
miniscript ::{ Descriptor , DescriptorPublicKey } ,
2023-03-22 17:00:08 +08:00
spk_iter ::BIP32_MAX_INDEX ,
2024-01-15 18:52:03 +01:00
DescriptorExt , DescriptorId , SpkIterator , SpkTxOutIndex ,
2023-03-01 11:09:08 +01:00
} ;
2024-01-15 18:52:03 +01:00
use bitcoin ::{ hashes ::Hash , Amount , OutPoint , Script , SignedAmount , Transaction , TxOut , Txid } ;
2024-01-18 14:30:29 +08:00
use core ::{
fmt ::Debug ,
ops ::{ Bound , RangeBounds } ,
} ;
2023-03-01 11:09:08 +01:00
2023-04-05 17:29:20 +08:00
use crate ::Append ;
2023-10-12 00:03:18 +08:00
/// Represents updates to the derivation index of a [`KeychainTxOutIndex`].
2024-01-15 18:52:03 +01:00
/// It maps each keychain `K` to a descriptor and its last revealed index.
2023-10-12 00:03:18 +08:00
///
/// It can be applied to [`KeychainTxOutIndex`] with [`apply_changeset`]. [`ChangeSet] are
/// monotone in that they will never decrease the revealed derivation index.
///
/// [`KeychainTxOutIndex`]: crate::keychain::KeychainTxOutIndex
/// [`apply_changeset`]: crate::keychain::KeychainTxOutIndex::apply_changeset
#[ derive(Clone, Debug, PartialEq) ]
#[ cfg_attr(
feature = " serde " ,
derive ( serde ::Deserialize , serde ::Serialize ) ,
serde (
crate = " serde_crate " ,
bound (
deserialize = " K: Ord + serde::Deserialize<'de> " ,
serialize = " K: Ord + serde::Serialize "
)
)
) ]
#[ must_use ]
2024-01-15 18:52:03 +01:00
pub struct ChangeSet < K > {
/// Contains the keychains that have been added and their respective descriptor
pub keychains_added : BTreeMap < K , Descriptor < DescriptorPublicKey > > ,
/// Contains for each descriptor_id the last revealed index of derivation
pub last_revealed : BTreeMap < DescriptorId , u32 > ,
2023-10-12 00:03:18 +08:00
}
impl < K : Ord > Append for ChangeSet < K > {
/// Append another [`ChangeSet`] into self.
///
2024-01-15 18:52:03 +01:00
/// For each keychain in `keychains_added` in the given [`ChangeSet`]:
/// If the keychain already exist with a different descriptor, we overwrite the old descriptor.
///
/// For each `last_revealed` in the given [`ChangeSet`]:
2023-10-12 00:03:18 +08:00
/// If the keychain already exists, increase the index when the other's index > self's index.
2024-05-04 23:37:42 +08:00
fn append ( & mut self , other : Self ) {
2023-10-12 00:03:18 +08:00
// We use `extend` instead of `BTreeMap::append` due to performance issues with `append`.
// Refer to https://github.com/rust-lang/rust/issues/34666#issuecomment-675658420
2024-01-15 18:52:03 +01:00
self . keychains_added . extend ( other . keychains_added ) ;
2024-05-04 23:37:42 +08:00
// for `last_revealed`, entries of `other` will take precedence ONLY if it is greater than
// what was originally in `self`.
for ( desc_id , index ) in other . last_revealed {
use crate ::collections ::btree_map ::Entry ;
match self . last_revealed . entry ( desc_id ) {
Entry ::Vacant ( entry ) = > {
entry . insert ( index ) ;
}
Entry ::Occupied ( mut entry ) = > {
if * entry . get ( ) < index {
entry . insert ( index ) ;
}
}
}
}
2023-10-12 00:03:18 +08:00
}
/// Returns whether the changeset are empty.
fn is_empty ( & self ) -> bool {
2024-01-15 18:52:03 +01:00
self . last_revealed . is_empty ( ) & & self . keychains_added . is_empty ( )
2023-10-12 00:03:18 +08:00
}
}
impl < K > Default for ChangeSet < K > {
fn default ( ) -> Self {
2024-01-15 18:52:03 +01:00
Self {
last_revealed : BTreeMap ::default ( ) ,
keychains_added : BTreeMap ::default ( ) ,
}
2023-10-12 00:03:18 +08:00
}
}
2024-01-23 09:44:38 +01:00
const DEFAULT_LOOKAHEAD : u32 = 25 ;
2023-11-28 18:08:49 +01:00
2024-01-17 13:30:28 +08:00
/// [`KeychainTxOutIndex`] controls how script pubkeys are revealed for multiple keychains, and
/// indexes [`TxOut`]s with them.
2023-03-01 11:09:08 +01:00
///
2024-01-17 13:30:28 +08:00
/// A single keychain is a chain of script pubkeys derived from a single [`Descriptor`]. Keychains
/// are identified using the `K` generic. Script pubkeys are identified by the keychain that they
/// are derived from `K`, as well as the derivation index `u32`.
2023-03-01 11:09:08 +01:00
///
2024-01-17 13:30:28 +08:00
/// # Revealed script pubkeys
2023-03-01 11:09:08 +01:00
///
2024-01-17 13:30:28 +08:00
/// Tracking how script pubkeys are revealed is useful for collecting chain data. For example, if
/// the user has requested 5 script pubkeys (to receive money with), we only need to use those
/// script pubkeys to scan for chain data.
///
/// Call [`reveal_to_target`] or [`reveal_next_spk`] to reveal more script pubkeys.
/// Call [`revealed_keychain_spks`] or [`revealed_spks`] to iterate through revealed script pubkeys.
///
/// # Lookahead script pubkeys
///
/// When an user first recovers a wallet (i.e. from a recovery phrase and/or descriptor), we will
/// NOT have knowledge of which script pubkeys are revealed. So when we index a transaction or
/// txout (using [`index_tx`]/[`index_txout`]) we scan the txouts against script pubkeys derived
/// above the last revealed index. These additionally-derived script pubkeys are called the
/// lookahead.
///
/// The [`KeychainTxOutIndex`] is constructed with the `lookahead` and cannot be altered. The
/// default `lookahead` count is 1000. Use [`new`] to set a custom `lookahead`.
///
/// # Unbounded script pubkey iterator
///
/// For script-pubkey-based chain sources (such as Electrum/Esplora), an initial scan is best done
/// by iterating though derived script pubkeys one by one and requesting transaction histories for
/// each script pubkey. We will stop after x-number of script pubkeys have empty histories. An
/// unbounded script pubkey iterator is useful to pass to such a chain source.
///
/// Call [`unbounded_spk_iter`] to get an unbounded script pubkey iterator for a given keychain.
/// Call [`all_unbounded_spk_iters`] to get unbounded script pubkey iterators for all keychains.
///
/// # Change sets
///
2024-01-15 18:52:03 +01:00
/// Methods that can update the last revealed index or add keychains will return [`super::ChangeSet`] to report
2023-03-01 11:09:08 +01:00
/// these changes. This can be persisted for future recovery.
///
/// ## Synopsis
///
/// ```
/// use bdk_chain::keychain::KeychainTxOutIndex;
/// # use bdk_chain::{ miniscript::{Descriptor, DescriptorPublicKey} };
/// # use core::str::FromStr;
///
/// // imagine our service has internal and external addresses but also addresses for users
/// #[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd)]
/// enum MyKeychain {
/// External,
/// Internal,
/// MyAppUser {
/// user_id: u32
/// }
/// }
///
/// let mut txout_index = KeychainTxOutIndex::<MyKeychain>::default();
///
/// # let secp = bdk_chain::bitcoin::secp256k1::Secp256k1::signing_only();
/// # let (external_descriptor,_) = Descriptor::<DescriptorPublicKey>::parse_descriptor(&secp, "tr([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/0/*)").unwrap();
/// # let (internal_descriptor,_) = Descriptor::<DescriptorPublicKey>::parse_descriptor(&secp, "tr([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/1/*)").unwrap();
2024-01-15 18:52:03 +01:00
/// # let (descriptor_42, _) = Descriptor::<DescriptorPublicKey>::parse_descriptor(&secp, "tr([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/2/*)").unwrap();
/// let _ = txout_index.insert_descriptor(MyKeychain::External, external_descriptor);
/// let _ = txout_index.insert_descriptor(MyKeychain::Internal, internal_descriptor);
/// let _ = txout_index.insert_descriptor(MyKeychain::MyAppUser { user_id: 42 }, descriptor_42);
2023-03-01 11:09:08 +01:00
///
/// let new_spk_for_user = txout_index.reveal_next_spk(&MyKeychain::MyAppUser{ user_id: 42 });
/// ```
///
/// [`Ord`]: core::cmp::Ord
/// [`SpkTxOutIndex`]: crate::spk_txout_index::SpkTxOutIndex
/// [`Descriptor`]: crate::miniscript::Descriptor
2024-01-17 13:30:28 +08:00
/// [`reveal_to_target`]: KeychainTxOutIndex::reveal_to_target
/// [`reveal_next_spk`]: KeychainTxOutIndex::reveal_next_spk
/// [`revealed_keychain_spks`]: KeychainTxOutIndex::revealed_keychain_spks
/// [`revealed_spks`]: KeychainTxOutIndex::revealed_spks
/// [`index_tx`]: KeychainTxOutIndex::index_tx
/// [`index_txout`]: KeychainTxOutIndex::index_txout
/// [`new`]: KeychainTxOutIndex::new
/// [`unbounded_spk_iter`]: KeychainTxOutIndex::unbounded_spk_iter
/// [`all_unbounded_spk_iters`]: KeychainTxOutIndex::all_unbounded_spk_iters
2024-01-15 18:52:03 +01:00
// Under the hood, the KeychainTxOutIndex uses a SpkTxOutIndex that keeps track of spks, indexed by
// descriptors. Users then assign or unassign keychains to those descriptors. It's important to
// note that descriptors, once added, never get removed from the SpkTxOutIndex; this is useful in
// case a user unassigns a keychain from a descriptor and after some time assigns it again.
2023-03-01 11:09:08 +01:00
#[ derive(Clone, Debug) ]
pub struct KeychainTxOutIndex < K > {
2024-01-15 18:52:03 +01:00
inner : SpkTxOutIndex < ( DescriptorId , u32 ) > ,
// keychain -> (descriptor, descriptor id) map
keychains_to_descriptors : BTreeMap < K , ( DescriptorId , Descriptor < DescriptorPublicKey > ) > ,
// descriptor id -> keychain map
descriptor_ids_to_keychain : BTreeMap < DescriptorId , ( K , Descriptor < DescriptorPublicKey > ) > ,
// descriptor_id -> descriptor map
// This is a "monotone" map, meaning that its size keeps growing, i.e., we never delete
// descriptors from it. This is useful for revealing spks for descriptors that don't have
// keychains associated.
descriptor_ids_to_descriptors : BTreeMap < DescriptorId , Descriptor < DescriptorPublicKey > > ,
2023-03-15 15:38:25 +08:00
// last revealed indexes
2024-01-15 18:52:03 +01:00
last_revealed : BTreeMap < DescriptorId , u32 > ,
2023-03-01 11:09:08 +01:00
// lookahead settings for each keychain
2023-11-28 18:08:49 +01:00
lookahead : u32 ,
2023-03-01 11:09:08 +01:00
}
impl < K > Default for KeychainTxOutIndex < K > {
fn default ( ) -> Self {
2023-11-28 18:08:49 +01:00
Self ::new ( DEFAULT_LOOKAHEAD )
2023-03-01 11:09:08 +01:00
}
}
2023-05-03 15:20:49 +08:00
impl < K : Clone + Ord + Debug > Indexer for KeychainTxOutIndex < K > {
2023-08-07 17:43:17 +02:00
type ChangeSet = super ::ChangeSet < K > ;
2023-03-24 15:47:39 +08:00
2023-08-07 17:43:17 +02:00
fn index_txout ( & mut self , outpoint : OutPoint , txout : & TxOut ) -> Self ::ChangeSet {
2023-09-06 09:47:45 +03:00
match self . inner . scan_txout ( outpoint , txout ) . cloned ( ) {
2024-01-15 18:52:03 +01:00
Some ( ( descriptor_id , index ) ) = > {
// We want to reveal spks for descriptors that aren't tracked by any keychain, and
// so we call reveal with descriptor_id
if let Some ( ( _ , changeset ) ) = self . reveal_to_target_with_id ( descriptor_id , index ) {
changeset
} else {
super ::ChangeSet ::default ( )
}
}
2023-09-06 09:47:45 +03:00
None = > super ::ChangeSet ::default ( ) ,
2023-08-25 12:52:09 +03:00
}
2023-03-24 15:47:39 +08:00
}
2023-08-07 17:43:17 +02:00
fn index_tx ( & mut self , tx : & bitcoin ::Transaction ) -> Self ::ChangeSet {
2023-08-25 12:52:09 +03:00
let mut changeset = super ::ChangeSet ::< K > ::default ( ) ;
for ( op , txout ) in tx . output . iter ( ) . enumerate ( ) {
changeset . append ( self . index_txout ( OutPoint ::new ( tx . txid ( ) , op as u32 ) , txout ) ) ;
}
changeset
2023-03-24 15:47:39 +08:00
}
2023-08-16 17:39:35 +02:00
fn initial_changeset ( & self ) -> Self ::ChangeSet {
2024-01-15 18:52:03 +01:00
super ::ChangeSet {
keychains_added : self
. keychains ( )
. map ( | ( k , v ) | ( k . clone ( ) , v . clone ( ) ) )
. collect ( ) ,
last_revealed : self . last_revealed . clone ( ) ,
}
2023-08-16 17:39:35 +02:00
}
2023-08-07 17:43:17 +02:00
fn apply_changeset ( & mut self , changeset : Self ::ChangeSet ) {
self . apply_changeset ( changeset )
2023-03-27 15:36:37 +08:00
}
2023-04-05 18:17:08 +08:00
fn is_tx_relevant ( & self , tx : & bitcoin ::Transaction ) -> bool {
2024-01-13 20:04:49 +08:00
self . inner . is_relevant ( tx )
2023-03-26 11:24:30 +08:00
}
2023-03-24 15:47:39 +08:00
}
2023-11-28 18:08:49 +01:00
impl < K > KeychainTxOutIndex < K > {
/// Construct a [`KeychainTxOutIndex`] with the given `lookahead`.
///
2023-12-29 16:40:48 +11:00
/// The `lookahead` is the number of script pubkeys to derive and cache from the internal
/// descriptors over and above the last revealed script index. Without a lookahead the index
/// will miss outputs you own when processing transactions whose output script pubkeys lie
/// beyond the last revealed index. In certain situations, such as when performing an initial
/// scan of the blockchain during wallet import, it may be uncertain or unknown what the index
/// of the last revealed script pubkey actually is.
2024-01-17 13:30:28 +08:00
///
/// Refer to [struct-level docs](KeychainTxOutIndex) for more about `lookahead`.
2023-11-28 18:08:49 +01:00
pub fn new ( lookahead : u32 ) -> Self {
Self {
inner : SpkTxOutIndex ::default ( ) ,
2024-01-15 18:52:03 +01:00
descriptor_ids_to_keychain : BTreeMap ::new ( ) ,
descriptor_ids_to_descriptors : BTreeMap ::new ( ) ,
keychains_to_descriptors : BTreeMap ::new ( ) ,
2023-11-28 18:08:49 +01:00
last_revealed : BTreeMap ::new ( ) ,
lookahead ,
}
}
}
2024-01-13 20:04:49 +08:00
/// Methods that are *re-exposed* from the internal [`SpkTxOutIndex`].
2023-03-01 11:09:08 +01:00
impl < K : Clone + Ord + Debug > KeychainTxOutIndex < K > {
/// Return a reference to the internal [`SpkTxOutIndex`].
2024-01-13 20:04:49 +08:00
///
2024-01-17 13:30:28 +08:00
/// **WARNING:** The internal index will contain lookahead spks. Refer to
/// [struct-level docs](KeychainTxOutIndex) for more about `lookahead`.
2024-01-15 18:52:03 +01:00
pub fn inner ( & self ) -> & SpkTxOutIndex < ( DescriptorId , u32 ) > {
2023-03-01 11:09:08 +01:00
& self . inner
}
2024-01-15 18:52:03 +01:00
/// Get the set of indexed outpoints, corresponding to tracked keychains.
pub fn outpoints ( & self ) -> impl DoubleEndedIterator < Item = ( ( K , u32 ) , OutPoint ) > + '_ {
self . inner
. outpoints ( )
. iter ( )
. filter_map ( | ( ( desc_id , index ) , op ) | {
self . descriptor_ids_to_keychain
. get ( desc_id )
. map ( | ( k , _ ) | ( ( k . clone ( ) , * index ) , * op ) )
} )
2023-05-10 14:14:29 +08:00
}
2024-01-18 14:30:29 +08:00
/// Iterate over known txouts that spend to tracked script pubkeys.
2024-01-15 18:52:03 +01:00
pub fn txouts ( & self ) -> impl DoubleEndedIterator < Item = ( K , u32 , OutPoint , & TxOut ) > + '_ {
self . inner . txouts ( ) . filter_map ( | ( ( desc_id , i ) , op , txo ) | {
self . descriptor_ids_to_keychain
. get ( desc_id )
. map ( | ( k , _ ) | ( k . clone ( ) , * i , op , txo ) )
} )
2024-01-18 14:30:29 +08:00
}
/// Finds all txouts on a transaction that has previously been scanned and indexed.
pub fn txouts_in_tx (
& self ,
txid : Txid ,
) -> impl DoubleEndedIterator < Item = ( K , u32 , OutPoint , & TxOut ) > {
self . inner
. txouts_in_tx ( txid )
2024-01-15 18:52:03 +01:00
. filter_map ( | ( ( desc_id , i ) , op , txo ) | {
self . descriptor_ids_to_keychain
. get ( desc_id )
. map ( | ( k , _ ) | ( k . clone ( ) , * i , op , txo ) )
} )
2024-01-18 14:30:29 +08:00
}
2024-01-15 18:52:03 +01:00
/// Return the [`TxOut`] of `outpoint` if it has been indexed, and if it corresponds to a
/// tracked keychain.
2024-01-13 20:04:49 +08:00
///
/// The associated keychain and keychain index of the txout's spk is also returned.
///
/// This calls [`SpkTxOutIndex::txout`] internally.
pub fn txout ( & self , outpoint : OutPoint ) -> Option < ( K , u32 , & TxOut ) > {
2024-01-15 18:52:03 +01:00
let ( ( descriptor_id , index ) , txo ) = self . inner . txout ( outpoint ) ? ;
let ( keychain , _ ) = self . descriptor_ids_to_keychain . get ( descriptor_id ) ? ;
Some ( ( keychain . clone ( ) , * index , txo ) )
2024-01-13 20:04:49 +08:00
}
/// Return the script that exists under the given `keychain`'s `index`.
///
/// This calls [`SpkTxOutIndex::spk_at_index`] internally.
pub fn spk_at_index ( & self , keychain : K , index : u32 ) -> Option < & Script > {
2024-01-15 18:52:03 +01:00
let descriptor_id = self . keychains_to_descriptors . get ( & keychain ) ? . 0 ;
self . inner . spk_at_index ( & ( descriptor_id , index ) )
2024-01-13 20:04:49 +08:00
}
/// Returns the keychain and keychain index associated with the spk.
///
/// This calls [`SpkTxOutIndex::index_of_spk`] internally.
pub fn index_of_spk ( & self , script : & Script ) -> Option < ( K , u32 ) > {
2024-01-15 18:52:03 +01:00
let ( desc_id , last_index ) = self . inner . index_of_spk ( script ) ? ;
self . descriptor_ids_to_keychain
. get ( desc_id )
. map ( | ( k , _ ) | ( k . clone ( ) , * last_index ) )
2024-01-13 20:04:49 +08:00
}
/// Returns whether the spk under the `keychain`'s `index` has been used.
///
/// Here, "unused" means that after the script pubkey was stored in the index, the index has
/// never scanned a transaction output with it.
///
/// This calls [`SpkTxOutIndex::is_used`] internally.
pub fn is_used ( & self , keychain : K , index : u32 ) -> bool {
2024-01-15 18:52:03 +01:00
let descriptor_id = self . keychains_to_descriptors . get ( & keychain ) . map ( | k | k . 0 ) ;
match descriptor_id {
Some ( descriptor_id ) = > self . inner . is_used ( & ( descriptor_id , index ) ) ,
None = > false ,
}
2024-01-13 20:04:49 +08:00
}
/// Marks the script pubkey at `index` as used even though the tracker hasn't seen an output
/// with it.
///
/// This only has an effect when the `index` had been added to `self` already and was unused.
///
2024-01-15 18:52:03 +01:00
/// Returns whether the spk under the given `keychain` and `index` is successfully
/// marked as used. Returns false either when there is no descriptor under the given
/// keychain, or when the spk is already marked as used.
2024-01-13 20:04:49 +08:00
///
/// This is useful when you want to reserve a script pubkey for something but don't want to add
/// the transaction output using it to the index yet. Other callers will consider `index` on
/// `keychain` used until you call [`unmark_used`].
///
/// This calls [`SpkTxOutIndex::mark_used`] internally.
///
/// [`unmark_used`]: Self::unmark_used
pub fn mark_used ( & mut self , keychain : K , index : u32 ) -> bool {
2024-01-15 18:52:03 +01:00
let descriptor_id = self . keychains_to_descriptors . get ( & keychain ) . map ( | k | k . 0 ) ;
match descriptor_id {
Some ( descriptor_id ) = > self . inner . mark_used ( & ( descriptor_id , index ) ) ,
None = > false ,
}
2024-01-13 20:04:49 +08:00
}
/// Undoes the effect of [`mark_used`]. Returns whether the `index` is inserted back into
/// `unused`.
///
/// Note that if `self` has scanned an output with this script pubkey, then this will have no
/// effect.
///
/// This calls [`SpkTxOutIndex::unmark_used`] internally.
///
/// [`mark_used`]: Self::mark_used
pub fn unmark_used ( & mut self , keychain : K , index : u32 ) -> bool {
2024-01-15 18:52:03 +01:00
let descriptor_id = self . keychains_to_descriptors . get ( & keychain ) . map ( | k | k . 0 ) ;
match descriptor_id {
Some ( descriptor_id ) = > self . inner . unmark_used ( & ( descriptor_id , index ) ) ,
None = > false ,
}
2024-01-13 20:04:49 +08:00
}
2024-02-06 17:31:22 +11:00
/// Computes the total value transfer effect `tx` has on the script pubkeys belonging to the
/// keychains in `range`. Value is *sent* when a script pubkey in the `range` is on an input and
/// *received* when it is on an output. For `sent` to be computed correctly, the output being
/// spent must have already been scanned by the index. Calculating received just uses the
/// [`Transaction`] outputs directly, so it will be correct even if it has not been scanned.
2024-04-26 11:17:23 -03:00
pub fn sent_and_received (
& self ,
tx : & Transaction ,
range : impl RangeBounds < K > ,
) -> ( Amount , Amount ) {
2024-02-06 17:31:22 +11:00
self . inner
2024-01-15 18:52:03 +01:00
. sent_and_received ( tx , self . map_to_inner_bounds ( range ) )
2024-01-13 20:04:49 +08:00
}
/// Computes the net value that this transaction gives to the script pubkeys in the index and
/// *takes* from the transaction outputs in the index. Shorthand for calling
/// [`sent_and_received`] and subtracting sent from received.
///
/// This calls [`SpkTxOutIndex::net_value`] internally.
///
/// [`sent_and_received`]: Self::sent_and_received
2024-04-26 11:17:23 -03:00
pub fn net_value ( & self , tx : & Transaction , range : impl RangeBounds < K > ) -> SignedAmount {
2024-01-15 18:52:03 +01:00
self . inner . net_value ( tx , self . map_to_inner_bounds ( range ) )
2024-01-13 20:04:49 +08:00
}
}
impl < K : Clone + Ord + Debug > KeychainTxOutIndex < K > {
2024-01-15 18:52:03 +01:00
/// Return the map of the keychain to descriptors.
pub fn keychains (
& self ,
) -> impl DoubleEndedIterator < Item = ( & K , & Descriptor < DescriptorPublicKey > ) > + ExactSizeIterator + '_
{
self . keychains_to_descriptors
. iter ( )
. map ( | ( k , ( _ , d ) ) | ( k , d ) )
2023-03-01 11:09:08 +01:00
}
2024-01-15 18:52:03 +01:00
/// Insert a descriptor with a keychain associated to it.
2023-03-01 11:09:08 +01:00
///
2024-01-15 18:52:03 +01:00
/// Adding a descriptor means you will be able to derive new script pubkeys under it
2023-03-01 11:09:08 +01:00
/// and the txout index will discover transaction outputs with those script pubkeys.
///
2024-01-15 18:52:03 +01:00
/// When trying to add a keychain that already existed under a different descriptor, or a descriptor
/// that already existed with a different keychain, the old keychain (or descriptor) will be
/// overwritten.
pub fn insert_descriptor (
& mut self ,
keychain : K ,
descriptor : Descriptor < DescriptorPublicKey > ,
) -> super ::ChangeSet < K > {
let descriptor_id = descriptor . descriptor_id ( ) ;
// First, we fill the keychain -> (desc_id, descriptor) map
let old_descriptor_opt = self
. keychains_to_descriptors
. insert ( keychain . clone ( ) , ( descriptor_id , descriptor . clone ( ) ) ) ;
// Then, we fill the descriptor_id -> (keychain, descriptor) map
let old_keychain_opt = self
. descriptor_ids_to_keychain
. insert ( descriptor_id , ( keychain . clone ( ) , descriptor . clone ( ) ) ) ;
// If `keychain` already had a `descriptor` associated, different from the `descriptor`
// passed in, we remove it from the descriptor -> keychain map
if let Some ( ( old_desc_id , _ ) ) = old_descriptor_opt {
if old_desc_id ! = descriptor_id {
self . descriptor_ids_to_keychain . remove ( & old_desc_id ) ;
}
}
// Lastly, we fill the desc_id -> desc map
self . descriptor_ids_to_descriptors
. insert ( descriptor_id , descriptor . clone ( ) ) ;
2023-11-28 18:08:49 +01:00
self . replenish_lookahead ( & keychain , self . lookahead ) ;
2024-01-15 18:52:03 +01:00
// If both the keychain and descriptor were already inserted and associated, the
// keychains_added changeset must be empty
let keychains_added = if old_keychain_opt . map ( | ( k , _ ) | k ) = = Some ( keychain . clone ( ) )
& & old_descriptor_opt . map ( | ( _ , d ) | d ) = = Some ( descriptor . clone ( ) )
{
[ ] . into ( )
} else {
[ ( keychain , descriptor ) ] . into ( )
} ;
super ::ChangeSet {
keychains_added ,
last_revealed : [ ] . into ( ) ,
}
}
/// Gets the descriptor associated with the keychain. Returns `None` if the keychain doesn't
/// have a descriptor associated with it.
pub fn get_descriptor ( & self , keychain : & K ) -> Option < & Descriptor < DescriptorPublicKey > > {
self . keychains_to_descriptors . get ( keychain ) . map ( | ( _ , d ) | d )
2023-03-01 11:09:08 +01:00
}
2023-11-28 18:08:49 +01:00
/// Get the lookahead setting.
2023-03-01 11:09:08 +01:00
///
2023-11-28 18:08:49 +01:00
/// Refer to [`new`] for more information on the `lookahead`.
2023-03-01 11:09:08 +01:00
///
2023-11-28 18:08:49 +01:00
/// [`new`]: Self::new
pub fn lookahead ( & self ) -> u32 {
self . lookahead
2023-03-01 11:09:08 +01:00
}
2024-02-17 02:20:44 +08:00
/// Store lookahead scripts until `target_index` (inclusive).
2023-03-01 11:09:08 +01:00
///
2024-02-17 02:20:44 +08:00
/// This does not change the global `lookahead` setting.
2023-03-01 11:09:08 +01:00
pub fn lookahead_to_target ( & mut self , keychain : & K , target_index : u32 ) {
2024-01-15 18:52:03 +01:00
if let Some ( ( next_index , _ ) ) = self . next_index ( keychain ) {
let temp_lookahead = ( target_index + 1 )
. checked_sub ( next_index )
. filter ( | & index | index > 0 ) ;
2024-02-17 02:20:44 +08:00
2024-01-15 18:52:03 +01:00
if let Some ( temp_lookahead ) = temp_lookahead {
self . replenish_lookahead ( keychain , temp_lookahead ) ;
}
2023-03-01 11:09:08 +01:00
}
}
2023-11-28 18:08:49 +01:00
fn replenish_lookahead ( & mut self , keychain : & K , lookahead : u32 ) {
2024-01-15 18:52:03 +01:00
let descriptor_opt = self . keychains_to_descriptors . get ( keychain ) . cloned ( ) ;
if let Some ( ( descriptor_id , descriptor ) ) = descriptor_opt {
let next_store_index = self . next_store_index ( descriptor_id ) ;
let next_reveal_index = self . last_revealed . get ( & descriptor_id ) . map_or ( 0 , | v | * v + 1 ) ;
for ( new_index , new_spk ) in SpkIterator ::new_with_range (
descriptor ,
next_store_index .. next_reveal_index + lookahead ,
) {
let _inserted = self . inner . insert_spk ( ( descriptor_id , new_index ) , new_spk ) ;
debug_assert! ( _inserted , " replenish lookahead: must not have existing spk: keychain={:?}, lookahead={}, next_store_index={}, next_reveal_index={} " , keychain , lookahead , next_store_index , next_reveal_index ) ;
}
2023-03-01 11:09:08 +01:00
}
}
2024-01-15 18:52:03 +01:00
fn next_store_index ( & self , descriptor_id : DescriptorId ) -> u32 {
2023-03-01 11:09:08 +01:00
self . inner ( )
. all_spks ( )
2024-01-15 18:52:03 +01:00
// This range is keeping only the spks with descriptor_id equal to
// `descriptor_id`. We don't use filter here as range is more optimized.
. range ( ( descriptor_id , u32 ::MIN ) .. ( descriptor_id , u32 ::MAX ) )
2023-03-01 11:09:08 +01:00
. last ( )
2024-01-08 14:46:25 +01:00
. map_or ( 0 , | ( ( _ , index ) , _ ) | * index + 1 )
2023-03-01 11:09:08 +01:00
}
2024-01-15 18:52:03 +01:00
/// Get an unbounded spk iterator over a given `keychain`. Returns `None` if the provided
/// keychain doesn't exist
pub fn unbounded_spk_iter (
& self ,
keychain : & K ,
) -> Option < SpkIterator < Descriptor < DescriptorPublicKey > > > {
let descriptor = self . keychains_to_descriptors . get ( keychain ) ? . 1. clone ( ) ;
Some ( SpkIterator ::new ( descriptor ) )
2024-01-13 20:04:49 +08:00
}
/// Get unbounded spk iterators for all keychains.
pub fn all_unbounded_spk_iters (
2023-03-01 11:09:08 +01:00
& self ,
2023-03-22 17:00:08 +08:00
) -> BTreeMap < K , SpkIterator < Descriptor < DescriptorPublicKey > > > {
2024-01-15 18:52:03 +01:00
self . keychains_to_descriptors
2023-03-01 11:09:08 +01:00
. iter ( )
2024-01-15 18:52:03 +01:00
. map ( | ( k , ( _ , descriptor ) ) | ( k . clone ( ) , SpkIterator ::new ( descriptor . clone ( ) ) ) )
2023-03-01 11:09:08 +01:00
. collect ( )
}
2024-02-06 17:31:22 +11:00
/// Iterate over revealed spks of keychains in `range`
pub fn revealed_spks (
& self ,
range : impl RangeBounds < K > ,
) -> impl DoubleEndedIterator < Item = ( & K , u32 , & Script ) > + Clone {
2024-01-15 18:52:03 +01:00
self . keychains_to_descriptors
. range ( range )
. flat_map ( | ( _ , ( descriptor_id , _ ) ) | {
let start = Bound ::Included ( ( * descriptor_id , u32 ::MIN ) ) ;
let end = match self . last_revealed . get ( descriptor_id ) {
Some ( last_revealed ) = > Bound ::Included ( ( * descriptor_id , * last_revealed ) ) ,
None = > Bound ::Excluded ( ( * descriptor_id , u32 ::MIN ) ) ,
} ;
self . inner
. all_spks ( )
. range ( ( start , end ) )
. map ( | ( ( descriptor_id , i ) , spk ) | {
(
& self
. descriptor_ids_to_keychain
. get ( descriptor_id )
. expect ( " Must be here " )
. 0 ,
* i ,
spk . as_script ( ) ,
)
} )
} )
2023-03-01 11:09:08 +01:00
}
2024-01-13 20:04:49 +08:00
/// Iterate over revealed spks of the given `keychain`.
2024-02-06 17:31:22 +11:00
pub fn revealed_keychain_spks < ' a > (
& ' a self ,
keychain : & ' a K ,
) -> impl DoubleEndedIterator < Item = ( u32 , & Script ) > + ' a {
self . revealed_spks ( keychain ..= keychain )
. map ( | ( _ , i , spk ) | ( i , spk ) )
2023-03-01 11:09:08 +01:00
}
2024-01-13 20:04:49 +08:00
/// Iterate over revealed, but unused, spks of all keychains.
pub fn unused_spks ( & self ) -> impl DoubleEndedIterator < Item = ( K , u32 , & Script ) > + Clone {
2024-01-15 18:52:03 +01:00
self . keychains_to_descriptors . keys ( ) . flat_map ( | keychain | {
2024-01-13 20:04:49 +08:00
self . unused_keychain_spks ( keychain )
. map ( | ( i , spk ) | ( keychain . clone ( ) , i , spk ) )
} )
}
/// Iterate over revealed, but unused, spks of the given `keychain`.
2024-01-15 18:52:03 +01:00
/// Returns an empty iterator if the provided keychain doesn't exist.
2024-01-13 20:04:49 +08:00
pub fn unused_keychain_spks (
2023-03-01 11:09:08 +01:00
& self ,
keychain : & K ,
) -> impl DoubleEndedIterator < Item = ( u32 , & Script ) > + Clone {
2024-01-15 18:52:03 +01:00
let desc_id = self
. keychains_to_descriptors
. get ( keychain )
. map ( | ( desc_id , _ ) | * desc_id )
// We use a dummy desc id if we can't find the real one in our map. In this way,
// if this method was to be called with a non-existent keychain, we would return an
// empty iterator
. unwrap_or_else ( | | DescriptorId ::from_byte_array ( [ 0 ; 32 ] ) ) ;
let next_i = self . last_revealed . get ( & desc_id ) . map_or ( 0 , | & i | i + 1 ) ;
2023-03-01 11:09:08 +01:00
self . inner
2024-01-15 18:52:03 +01:00
. unused_spks ( ( desc_id , u32 ::MIN ) .. ( desc_id , next_i ) )
2024-01-13 20:04:49 +08:00
. map ( | ( ( _ , i ) , spk ) | ( * i , spk ) )
2023-03-01 11:09:08 +01:00
}
2023-03-10 23:23:29 +05:30
/// Get the next derivation index for `keychain`. The next index is the index after the last revealed
2023-03-01 11:09:08 +01:00
/// derivation index.
///
/// The second field in the returned tuple represents whether the next derivation index is new.
/// There are two scenarios where the next derivation index is reused (not new):
///
/// 1. The keychain's descriptor has no wildcard, and a script has already been revealed.
/// 2. The number of revealed scripts has already reached 2^31 (refer to BIP-32).
///
/// Not checking the second field of the tuple may result in address reuse.
///
2024-01-15 18:52:03 +01:00
/// Returns None if the provided `keychain` doesn't exist.
pub fn next_index ( & self , keychain : & K ) -> Option < ( u32 , bool ) > {
let ( descriptor_id , descriptor ) = self . keychains_to_descriptors . get ( keychain ) ? ;
let last_index = self . last_revealed . get ( descriptor_id ) . cloned ( ) ;
2023-03-01 11:09:08 +01:00
2023-03-10 23:23:29 +05:30
// we can only get the next index if the wildcard exists.
2023-03-01 11:09:08 +01:00
let has_wildcard = descriptor . has_wildcard ( ) ;
2024-01-15 18:52:03 +01:00
Some ( match last_index {
2023-03-10 23:23:29 +05:30
// if there is no index, next_index is always 0.
2023-03-01 11:09:08 +01:00
None = > ( 0 , true ) ,
2023-03-10 23:23:29 +05:30
// descriptors without wildcards can only have one index.
2023-03-01 11:09:08 +01:00
Some ( _ ) if ! has_wildcard = > ( 0 , false ) ,
2023-03-10 23:23:29 +05:30
// derivation index must be < 2^31 (BIP-32).
2023-03-01 11:09:08 +01:00
Some ( index ) if index > BIP32_MAX_INDEX = > {
unreachable! ( " index is out of bounds " )
}
Some ( index ) if index = = BIP32_MAX_INDEX = > ( index , false ) ,
2023-03-10 23:23:29 +05:30
// get the next derivation index.
2023-03-01 11:09:08 +01:00
Some ( index ) = > ( index + 1 , true ) ,
2024-01-15 18:52:03 +01:00
} )
2023-03-01 11:09:08 +01:00
}
/// Get the last derivation index that is revealed for each keychain.
///
/// Keychains with no revealed indices will not be included in the returned [`BTreeMap`].
2024-01-15 18:52:03 +01:00
pub fn last_revealed_indices ( & self ) -> BTreeMap < K , u32 > {
self . last_revealed
. iter ( )
. filter_map ( | ( descriptor_id , index ) | {
self . descriptor_ids_to_keychain
. get ( descriptor_id )
. map ( | ( k , _ ) | ( k . clone ( ) , * index ) )
} )
. collect ( )
2023-03-01 11:09:08 +01:00
}
2024-01-15 18:52:03 +01:00
/// Get the last derivation index revealed for `keychain`. Returns None if the keychain doesn't
/// exist, or if the keychain doesn't have any revealed scripts.
2023-03-01 11:09:08 +01:00
pub fn last_revealed_index ( & self , keychain : & K ) -> Option < u32 > {
2024-01-15 18:52:03 +01:00
let descriptor_id = self . keychains_to_descriptors . get ( keychain ) ? . 0 ;
self . last_revealed . get ( & descriptor_id ) . cloned ( )
2023-03-01 11:09:08 +01:00
}
/// Convenience method to call [`Self::reveal_to_target`] on multiple keychains.
pub fn reveal_to_target_multi (
& mut self ,
keychains : & BTreeMap < K , u32 > ,
) -> (
2023-03-22 17:00:08 +08:00
BTreeMap < K , SpkIterator < Descriptor < DescriptorPublicKey > > > ,
2023-08-07 17:43:17 +02:00
super ::ChangeSet < K > ,
2023-03-01 11:09:08 +01:00
) {
2023-08-07 17:43:17 +02:00
let mut changeset = super ::ChangeSet ::default ( ) ;
2023-03-01 11:09:08 +01:00
let mut spks = BTreeMap ::new ( ) ;
for ( keychain , & index ) in keychains {
2024-01-15 18:52:03 +01:00
if let Some ( ( new_spks , new_changeset ) ) = self . reveal_to_target ( keychain , index ) {
if ! new_changeset . is_empty ( ) {
spks . insert ( keychain . clone ( ) , new_spks ) ;
changeset . append ( new_changeset . clone ( ) ) ;
}
2023-03-01 11:09:08 +01:00
}
}
2023-08-07 17:43:17 +02:00
( spks , changeset )
2023-03-01 11:09:08 +01:00
}
2024-01-15 18:52:03 +01:00
/// Convenience method to call `reveal_to_target` with a descriptor_id instead of a keychain.
/// This is useful for revealing spks of descriptors for which we don't have a keychain
/// tracked.
/// Refer to the `reveal_to_target` documentation for more.
2023-03-01 11:09:08 +01:00
///
2024-01-15 18:52:03 +01:00
/// Returns None if the provided `descriptor_id` doesn't correspond to a tracked descriptor.
fn reveal_to_target_with_id (
2023-03-01 11:09:08 +01:00
& mut self ,
2024-01-15 18:52:03 +01:00
descriptor_id : DescriptorId ,
2023-03-01 11:09:08 +01:00
target_index : u32 ,
2024-01-15 18:52:03 +01:00
) -> Option < (
2023-03-22 17:00:08 +08:00
SpkIterator < Descriptor < DescriptorPublicKey > > ,
2023-08-07 17:43:17 +02:00
super ::ChangeSet < K > ,
2024-01-15 18:52:03 +01:00
) > {
let descriptor = self
. descriptor_ids_to_descriptors
. get ( & descriptor_id ) ?
. clone ( ) ;
2023-03-01 11:09:08 +01:00
let has_wildcard = descriptor . has_wildcard ( ) ;
let target_index = if has_wildcard { target_index } else { 0 } ;
2024-01-08 14:47:28 +01:00
let next_reveal_index = self
. last_revealed
2024-01-15 18:52:03 +01:00
. get ( & descriptor_id )
2024-01-08 14:47:28 +01:00
. map_or ( 0 , | index | * index + 1 ) ;
2023-03-01 11:09:08 +01:00
2024-01-15 18:52:03 +01:00
debug_assert! ( next_reveal_index + self . lookahead > = self . next_store_index ( descriptor_id ) ) ;
2023-03-15 15:38:25 +08:00
2024-01-08 14:47:28 +01:00
// If the target_index is already revealed, we are done
if next_reveal_index > target_index {
2024-01-15 18:52:03 +01:00
return Some ( (
SpkIterator ::new_with_range ( descriptor , next_reveal_index .. next_reveal_index ) ,
2024-01-08 14:47:28 +01:00
super ::ChangeSet ::default ( ) ,
2024-01-15 18:52:03 +01:00
) ) ;
2023-03-01 11:09:08 +01:00
}
2024-01-08 14:47:28 +01:00
// We range over the indexes that are not stored and insert their spks in the index.
// Indexes from next_reveal_index to next_reveal_index + lookahead are already stored (due
// to lookahead), so we only range from next_reveal_index + lookahead to target + lookahead
2023-11-28 18:08:49 +01:00
let range = next_reveal_index + self . lookahead ..= target_index + self . lookahead ;
2024-01-15 18:52:03 +01:00
for ( new_index , new_spk ) in SpkIterator ::new_with_range ( descriptor . clone ( ) , range ) {
let _inserted = self . inner . insert_spk ( ( descriptor_id , new_index ) , new_spk ) ;
2024-01-09 09:44:06 +08:00
debug_assert! ( _inserted , " must not have existing spk " ) ;
debug_assert! (
has_wildcard | | new_index = = 0 ,
" non-wildcard descriptors must not iterate past index 0 "
) ;
2023-03-01 11:09:08 +01:00
}
2024-01-15 18:52:03 +01:00
let _old_index = self . last_revealed . insert ( descriptor_id , target_index ) ;
2024-01-08 14:47:28 +01:00
debug_assert! ( _old_index < Some ( target_index ) ) ;
2024-01-15 18:52:03 +01:00
Some ( (
SpkIterator ::new_with_range ( descriptor , next_reveal_index .. target_index + 1 ) ,
super ::ChangeSet {
keychains_added : BTreeMap ::new ( ) ,
last_revealed : core ::iter ::once ( ( descriptor_id , target_index ) ) . collect ( ) ,
} ,
) )
}
/// Reveals script pubkeys of the `keychain`'s descriptor **up to and including** the
/// `target_index`.
///
/// If the `target_index` cannot be reached (due to the descriptor having no wildcard and/or
/// the `target_index` is in the hardened index range), this method will make a best-effort and
/// reveal up to the last possible index.
///
/// This returns an iterator of newly revealed indices (alongside their scripts) and a
/// [`super::ChangeSet`], which reports updates to the latest revealed index. If no new script
/// pubkeys are revealed, then both of these will be empty.
///
/// Returns None if the provided `keychain` doesn't exist.
pub fn reveal_to_target (
& mut self ,
keychain : & K ,
target_index : u32 ,
) -> Option < (
SpkIterator < Descriptor < DescriptorPublicKey > > ,
super ::ChangeSet < K > ,
) > {
let descriptor_id = self . keychains_to_descriptors . get ( keychain ) ? . 0 ;
self . reveal_to_target_with_id ( descriptor_id , target_index )
2023-03-01 11:09:08 +01:00
}
/// Attempts to reveal the next script pubkey for `keychain`.
///
/// Returns the derivation index of the revealed script pubkey, the revealed script pubkey and a
2023-08-07 17:43:17 +02:00
/// [`super::ChangeSet`] which represents changes in the last revealed index (if any).
2024-01-15 18:52:03 +01:00
/// Returns None if the provided keychain doesn't exist.
2023-03-01 11:09:08 +01:00
///
/// When a new script cannot be revealed, we return the last revealed script and an empty
2023-08-07 17:43:17 +02:00
/// [`super::ChangeSet`]. There are two scenarios when a new script pubkey cannot be derived:
2023-03-01 11:09:08 +01:00
///
/// 1. The descriptor has no wildcard and already has one script revealed.
/// 2. The descriptor has already revealed scripts up to the numeric bound.
2024-01-15 18:52:03 +01:00
/// 3. There is no descriptor associated with the given keychain.
pub fn reveal_next_spk (
& mut self ,
keychain : & K ,
) -> Option < ( ( u32 , & Script ) , super ::ChangeSet < K > ) > {
let descriptor_id = self . keychains_to_descriptors . get ( keychain ) ? . 0 ;
let ( next_index , _ ) = self . next_index ( keychain ) . expect ( " We know keychain exists " ) ;
let changeset = self
. reveal_to_target ( keychain , next_index )
. expect ( " We know keychain exists " )
. 1 ;
2023-03-01 11:09:08 +01:00
let script = self
. inner
2024-01-15 18:52:03 +01:00
. spk_at_index ( & ( descriptor_id , next_index ) )
2023-03-01 11:09:08 +01:00
. expect ( " script must already be stored " ) ;
2024-01-15 18:52:03 +01:00
Some ( ( ( next_index , script ) , changeset ) )
2023-03-01 11:09:08 +01:00
}
2023-03-10 23:23:29 +05:30
/// Gets the next unused script pubkey in the keychain. I.e., the script pubkey with the lowest
2023-03-01 11:09:08 +01:00
/// index that has not been used yet.
///
/// This will derive and reveal a new script pubkey if no more unused script pubkeys exist.
///
2023-03-10 23:23:29 +05:30
/// If the descriptor has no wildcard and already has a used script pubkey or if a descriptor
/// has used all scripts up to the derivation bounds, then the last derived script pubkey will be
2023-03-01 11:09:08 +01:00
/// returned.
///
2024-01-15 18:52:03 +01:00
/// Returns None if the provided keychain doesn't exist.
pub fn next_unused_spk (
& mut self ,
keychain : & K ,
) -> Option < ( ( u32 , & Script ) , super ::ChangeSet < K > ) > {
2024-01-13 20:04:49 +08:00
let need_new = self . unused_keychain_spks ( keychain ) . next ( ) . is_none ( ) ;
2023-03-01 11:09:08 +01:00
// this rather strange branch is needed because of some lifetime issues
if need_new {
self . reveal_next_spk ( keychain )
} else {
2024-01-15 18:52:03 +01:00
Some ( (
2024-01-13 20:04:49 +08:00
self . unused_keychain_spks ( keychain )
2023-03-01 11:09:08 +01:00
. next ( )
. expect ( " we already know next exists " ) ,
2023-08-07 17:43:17 +02:00
super ::ChangeSet ::default ( ) ,
2024-01-15 18:52:03 +01:00
) )
2023-03-01 11:09:08 +01:00
}
}
2024-02-06 17:31:22 +11:00
/// Iterate over all [`OutPoint`]s that have `TxOut`s with script pubkeys derived from
2023-03-01 11:09:08 +01:00
/// `keychain`.
2024-02-06 17:31:22 +11:00
pub fn keychain_outpoints < ' a > (
& ' a self ,
keychain : & ' a K ,
) -> impl DoubleEndedIterator < Item = ( u32 , OutPoint ) > + ' a {
self . keychain_outpoints_in_range ( keychain ..= keychain )
. map ( move | ( _ , i , op ) | ( i , op ) )
}
/// Iterate over [`OutPoint`]s that have script pubkeys derived from keychains in `range`.
pub fn keychain_outpoints_in_range < ' a > (
& ' a self ,
range : impl RangeBounds < K > + ' a ,
) -> impl DoubleEndedIterator < Item = ( & ' a K , u32 , OutPoint ) > + ' a {
2024-01-15 18:52:03 +01:00
let bounds = self . map_to_inner_bounds ( range ) ;
2024-02-06 17:31:22 +11:00
self . inner
. outputs_in_range ( bounds )
2024-01-15 18:52:03 +01:00
. map ( move | ( ( descriptor_id , i ) , op ) | {
(
& self
. descriptor_ids_to_keychain
. get ( descriptor_id )
. expect ( " must be here " )
. 0 ,
* i ,
op ,
)
} )
2024-01-18 14:30:29 +08:00
}
2024-01-15 18:52:03 +01:00
fn map_to_inner_bounds (
& self ,
bound : impl RangeBounds < K > ,
) -> impl RangeBounds < ( DescriptorId , u32 ) > {
let get_desc_id = | keychain | {
self . keychains_to_descriptors
. get ( keychain )
. map ( | ( desc_id , _ ) | * desc_id )
. unwrap_or_else ( | | DescriptorId ::from_byte_array ( [ 0 ; 32 ] ) )
} ;
2024-02-06 17:31:22 +11:00
let start = match bound . start_bound ( ) {
2024-01-15 18:52:03 +01:00
Bound ::Included ( keychain ) = > Bound ::Included ( ( get_desc_id ( keychain ) , u32 ::MIN ) ) ,
Bound ::Excluded ( keychain ) = > Bound ::Excluded ( ( get_desc_id ( keychain ) , u32 ::MAX ) ) ,
2024-01-18 14:30:29 +08:00
Bound ::Unbounded = > Bound ::Unbounded ,
} ;
2024-02-06 17:31:22 +11:00
let end = match bound . end_bound ( ) {
2024-01-15 18:52:03 +01:00
Bound ::Included ( keychain ) = > Bound ::Included ( ( get_desc_id ( keychain ) , u32 ::MAX ) ) ,
Bound ::Excluded ( keychain ) = > Bound ::Excluded ( ( get_desc_id ( keychain ) , u32 ::MIN ) ) ,
2024-01-18 14:30:29 +08:00
Bound ::Unbounded = > Bound ::Unbounded ,
} ;
2024-02-06 17:31:22 +11:00
( start , end )
2023-03-01 11:09:08 +01:00
}
/// Returns the highest derivation index of the `keychain` where [`KeychainTxOutIndex`] has
/// found a [`TxOut`] with it's script pubkey.
pub fn last_used_index ( & self , keychain : & K ) -> Option < u32 > {
2024-01-13 20:04:49 +08:00
self . keychain_outpoints ( keychain ) . last ( ) . map ( | ( i , _ ) | i )
2023-03-01 11:09:08 +01:00
}
/// Returns the highest derivation index of each keychain that [`KeychainTxOutIndex`] has found
/// a [`TxOut`] with it's script pubkey.
pub fn last_used_indices ( & self ) -> BTreeMap < K , u32 > {
2024-01-15 18:52:03 +01:00
self . keychains_to_descriptors
2023-03-01 11:09:08 +01:00
. iter ( )
. filter_map ( | ( keychain , _ ) | {
self . last_used_index ( keychain )
. map ( | index | ( keychain . clone ( ) , index ) )
} )
. collect ( )
}
2024-01-15 18:52:03 +01:00
/// Applies the derivation changeset to the [`KeychainTxOutIndex`], as specified in the
/// [`ChangeSet::append`] documentation:
/// - Extends the number of derived scripts per keychain
/// - Adds new descriptors introduced
/// - If a descriptor is introduced for a keychain that already had a descriptor, overwrites
/// the old descriptor
2023-08-07 17:43:17 +02:00
pub fn apply_changeset ( & mut self , changeset : super ::ChangeSet < K > ) {
2024-01-15 18:52:03 +01:00
let ChangeSet {
keychains_added ,
last_revealed ,
} = changeset ;
for ( keychain , descriptor ) in keychains_added {
let _ = self . insert_descriptor ( keychain , descriptor ) ;
}
let last_revealed = last_revealed
. into_iter ( )
. filter_map ( | ( descriptor_id , index ) | {
self . descriptor_ids_to_keychain
. get ( & descriptor_id )
. map ( | ( k , _ ) | ( k . clone ( ) , index ) )
} )
. collect ( ) ;
let _ = self . reveal_to_target_multi ( & last_revealed ) ;
2023-03-01 11:09:08 +01:00
}
}