diff --git a/crates/bdk/src/wallet/mod.rs b/crates/bdk/src/wallet/mod.rs index 4db035fb6c..43f9720eaf 100644 --- a/crates/bdk/src/wallet/mod.rs +++ b/crates/bdk/src/wallet/mod.rs @@ -342,6 +342,8 @@ pub enum LoadError { MissingNetwork, /// Data loaded from persistence is missing genesis hash. MissingGenesis, + /// Data loaded from persistence is missing descriptor. + MissingDescriptor, } impl fmt::Display for LoadError @@ -357,6 +359,7 @@ where } LoadError::MissingNetwork => write!(f, "loaded data is missing network type"), LoadError::MissingGenesis => write!(f, "loaded data is missing genesis hash"), + LoadError::MissingDescriptor => write!(f, "loaded data is missing descriptor"), } } } @@ -394,6 +397,13 @@ pub enum NewOrLoadError { /// The network type loaded from persistence. got: Option, }, + /// The loaded desccriptor does not match what was provided. + LoadedDescriptorDoesNotMatch { + /// The descriptor loaded from persistence. + got: Option, + /// The keychain of the descriptor not matching + keychain: KeychainKind, + }, } impl fmt::Display for NewOrLoadError @@ -415,6 +425,13 @@ where NewOrLoadError::LoadedNetworkDoesNotMatch { expected, got } => { write!(f, "loaded network type is not {}, got {:?}", expected, got) } + NewOrLoadError::LoadedDescriptorDoesNotMatch { got, keychain } => { + write!( + f, + "loaded descriptor is different from what was provided, got {:?} for keychain {:?}", + got, keychain + ) + } } } } @@ -553,11 +570,7 @@ impl Wallet { } /// Load [`Wallet`] from the given persistence backend. - pub fn load( - descriptor: E, - change_descriptor: Option, - mut db: D, - ) -> Result> + pub fn load(mut db: D) -> Result> where D: PersistBackend, { @@ -565,15 +578,10 @@ impl Wallet { .load_from_persistence() .map_err(LoadError::Load)? .ok_or(LoadError::NotInitialized)?; - Self::load_from_changeset(descriptor, change_descriptor, db, changeset) + Self::load_from_changeset(db, changeset) } - fn load_from_changeset( - descriptor: E, - change_descriptor: Option, - db: D, - changeset: ChangeSet, - ) -> Result> + fn load_from_changeset(db: D, changeset: ChangeSet) -> Result> where D: PersistBackend, { @@ -582,6 +590,19 @@ impl Wallet { let chain = LocalChain::from_changeset(changeset.chain).map_err(|_| LoadError::MissingGenesis)?; let mut index = KeychainTxOutIndex::::default(); + let descriptor = changeset + .indexed_tx_graph + .indexer + .keychains_added + .get(&KeychainKind::External) + .ok_or(LoadError::MissingDescriptor)? + .clone(); + let change_descriptor = changeset + .indexed_tx_graph + .indexer + .keychains_added + .get(&KeychainKind::Internal) + .cloned(); let (signers, change_signers) = create_signers(&mut index, &secp, descriptor, change_descriptor, network) @@ -625,8 +646,8 @@ impl Wallet { ) } - /// Either loads [`Wallet`] from persistence, or initializes it if it does not exist (with a - /// custom genesis hash). + /// Either loads [`Wallet`] from persistence, or initializes it if it does not exist, using the + /// provided descriptor, change descriptor, network, and custom genesis hash. /// /// This method will fail if the loaded [`Wallet`] has different parameters to those provided. /// This is like [`Wallet::new_or_load`] with an additional `genesis_hash` parameter. This is @@ -644,25 +665,23 @@ impl Wallet { let changeset = db.load_from_persistence().map_err(NewOrLoadError::Load)?; match changeset { Some(changeset) => { - let wallet = - Self::load_from_changeset(descriptor, change_descriptor, db, changeset) - .map_err(|e| match e { - LoadError::Descriptor(e) => NewOrLoadError::Descriptor(e), - LoadError::Load(e) => NewOrLoadError::Load(e), - LoadError::NotInitialized => NewOrLoadError::NotInitialized, - LoadError::MissingNetwork => { - NewOrLoadError::LoadedNetworkDoesNotMatch { - expected: network, - got: None, - } - } - LoadError::MissingGenesis => { - NewOrLoadError::LoadedGenesisDoesNotMatch { - expected: genesis_hash, - got: None, - } - } - })?; + let wallet = Self::load_from_changeset(db, changeset).map_err(|e| match e { + LoadError::Descriptor(e) => NewOrLoadError::Descriptor(e), + LoadError::Load(e) => NewOrLoadError::Load(e), + LoadError::NotInitialized => NewOrLoadError::NotInitialized, + LoadError::MissingNetwork => NewOrLoadError::LoadedNetworkDoesNotMatch { + expected: network, + got: None, + }, + LoadError::MissingGenesis => NewOrLoadError::LoadedGenesisDoesNotMatch { + expected: genesis_hash, + got: None, + }, + LoadError::MissingDescriptor => NewOrLoadError::LoadedDescriptorDoesNotMatch { + got: None, + keychain: KeychainKind::External, + }, + })?; if wallet.network != network { return Err(NewOrLoadError::LoadedNetworkDoesNotMatch { expected: network, @@ -675,6 +694,36 @@ impl Wallet { got: Some(wallet.chain.genesis_hash()), }); } + + let expected_descriptor = descriptor + .into_wallet_descriptor(&wallet.secp, network) + .map_err(|e| NewOrLoadError::Descriptor(e))? + .0; + let wallet_descriptor = wallet.public_descriptor(KeychainKind::External).cloned(); + if wallet_descriptor != Some(expected_descriptor) { + return Err(NewOrLoadError::LoadedDescriptorDoesNotMatch { + got: wallet_descriptor, + keychain: KeychainKind::External, + }); + } + + let expected_change_descriptor = if let Some(c) = change_descriptor { + Some( + c.into_wallet_descriptor(&wallet.secp, network) + .map_err(|e| NewOrLoadError::Descriptor(e))? + .0, + ) + } else { + None + }; + let wallet_change_descriptor = + wallet.public_descriptor(KeychainKind::Internal).cloned(); + if wallet_change_descriptor != expected_change_descriptor { + return Err(NewOrLoadError::LoadedDescriptorDoesNotMatch { + got: wallet_change_descriptor, + keychain: KeychainKind::Internal, + }); + } Ok(wallet) } None => Self::new_with_genesis_hash( @@ -700,7 +749,7 @@ impl Wallet { } /// Iterator over all keychains in this wallet - pub fn keychains(&self) -> &BTreeMap { + pub fn keychains(&self) -> impl Iterator { self.indexed_graph.index.keychains() } @@ -833,7 +882,7 @@ impl Wallet { .filter_chain_unspents( &self.chain, self.chain.tip().block_id(), - self.indexed_graph.index.outpoints().iter().cloned(), + self.indexed_graph.index.outpoints(), ) .map(|((k, i), full_txo)| new_local_utxo(k, i, full_txo)) } @@ -847,7 +896,7 @@ impl Wallet { .filter_chain_txouts( &self.chain, self.chain.tip().block_id(), - self.indexed_graph.index.outpoints().iter().cloned(), + self.indexed_graph.index.outpoints(), ) .map(|((k, i), full_txo)| new_local_utxo(k, i, full_txo)) } @@ -1181,7 +1230,7 @@ impl Wallet { self.indexed_graph.graph().balance( &self.chain, self.chain.tip().block_id(), - self.indexed_graph.index.outpoints().iter().cloned(), + self.indexed_graph.index.outpoints(), |&(k, _), _| k == KeychainKind::Internal, ) } @@ -1271,17 +1320,9 @@ impl Wallet { where D: PersistBackend, { - let external_descriptor = self - .indexed_graph - .index - .keychains() - .get(&KeychainKind::External) - .expect("must exist"); - let internal_descriptor = self - .indexed_graph - .index - .keychains() - .get(&KeychainKind::Internal); + let keychains: BTreeMap<_, _> = self.indexed_graph.index.keychains().collect(); + let external_descriptor = keychains.get(&KeychainKind::External).expect("must exist"); + let internal_descriptor = keychains.get(&KeychainKind::Internal); let external_policy = external_descriptor .extract_policy(&self.signers, BuildSatisfaction::None, &self.secp)? @@ -1892,7 +1933,11 @@ impl Wallet { /// /// This can be used to build a watch-only version of a wallet pub fn public_descriptor(&self, keychain: KeychainKind) -> Option<&ExtendedDescriptor> { - self.indexed_graph.index.keychains().get(&keychain) + self.indexed_graph + .index + .keychains() + .find(|(k, _)| *k == &keychain) + .map(|(_, d)| d) } /// Finalize a PSBT, i.e., for each input determine if sufficient data is available to pass @@ -1943,17 +1988,9 @@ impl Wallet { .get_utxo_for(n) .and_then(|txout| self.get_descriptor_for_txout(&txout)) .or_else(|| { - self.indexed_graph - .index - .keychains() - .iter() - .find_map(|(_, desc)| { - desc.derive_from_psbt_input( - psbt_input, - psbt.get_utxo_for(n), - &self.secp, - ) - }) + self.indexed_graph.index.keychains().find_map(|(_, desc)| { + desc.derive_from_psbt_input(psbt_input, psbt.get_utxo_for(n), &self.secp) + }) }); match desc { @@ -2176,7 +2213,6 @@ impl Wallet { if params.add_global_xpubs { let all_xpubs = self .keychains() - .iter() .flat_map(|(_, desc)| desc.get_extended_keys()) .collect::>(); @@ -2551,13 +2587,13 @@ fn create_signers( ) -> Result<(Arc, Arc), crate::descriptor::error::Error> { let (descriptor, keymap) = into_wallet_descriptor_checked(descriptor, secp, network)?; let signers = Arc::new(SignersContainer::build(keymap, &descriptor, secp)); - index.add_keychain(KeychainKind::External, descriptor); + let _ = index.insert_descriptor(descriptor, KeychainKind::External); let change_signers = match change_descriptor { Some(descriptor) => { let (descriptor, keymap) = into_wallet_descriptor_checked(descriptor, secp, network)?; let signers = Arc::new(SignersContainer::build(keymap, &descriptor, secp)); - index.add_keychain(KeychainKind::Internal, descriptor); + let _ = index.insert_descriptor(descriptor, KeychainKind::Internal); signers } None => Arc::new(SignersContainer::new()), diff --git a/crates/bdk/tests/wallet.rs b/crates/bdk/tests/wallet.rs index 271b871632..a984bcef99 100644 --- a/crates/bdk/tests/wallet.rs +++ b/crates/bdk/tests/wallet.rs @@ -1,7 +1,7 @@ use std::str::FromStr; use assert_matches::assert_matches; -use bdk::descriptor::calc_checksum; +use bdk::descriptor::{calc_checksum, IntoWalletDescriptor}; use bdk::psbt::PsbtUtils; use bdk::signer::{SignOptions, SignerError}; use bdk::wallet::coin_selection::{self, LargestFirstCoinSelection}; @@ -10,14 +10,15 @@ use bdk::wallet::tx_builder::AddForeignUtxoError; use bdk::wallet::{AddressIndex, AddressInfo, Balance, Wallet}; use bdk::wallet::{AddressIndex::*, NewError}; use bdk::{FeeRate, KeychainKind}; +use bdk_chain::collections::BTreeMap; use bdk_chain::COINBASE_MATURITY; use bdk_chain::{BlockId, ConfirmationTime}; use bitcoin::hashes::Hash; use bitcoin::sighash::{EcdsaSighashType, TapSighashType}; use bitcoin::ScriptBuf; use bitcoin::{ - absolute, script::PushBytesBuf, taproot::TapNodeHash, Address, OutPoint, Sequence, Transaction, - TxIn, TxOut, Weight, + absolute, script::PushBytesBuf, secp256k1::Secp256k1, taproot::TapNodeHash, Address, OutPoint, + Sequence, Transaction, TxIn, TxOut, Weight, }; use bitcoin::{psbt, Network}; use bitcoin::{BlockHash, Txid}; @@ -83,14 +84,24 @@ fn load_recovers_wallet() { // recover wallet { let db = bdk_file_store::Store::open(DB_MAGIC, &file_path).expect("must recover db"); - let wallet = - Wallet::load(get_test_tr_single_sig_xprv(), None, db).expect("must recover wallet"); + let wallet = Wallet::load(db).expect("must recover wallet"); assert_eq!(wallet.network(), Network::Testnet); - assert_eq!(wallet.spk_index().keychains(), wallet_spk_index.keychains()); + assert_eq!( + wallet.spk_index().keychains().collect::>(), + wallet_spk_index.keychains().collect::>() + ); assert_eq!( wallet.spk_index().last_revealed_indices(), wallet_spk_index.last_revealed_indices() ); + let secp = Secp256k1::new(); + assert_eq!( + *wallet.get_descriptor_for_keychain(KeychainKind::External), + get_test_tr_single_sig_xprv() + .into_wallet_descriptor(&secp, wallet.network()) + .unwrap() + .0 + ); } // `new` can only be called on empty db @@ -107,12 +118,12 @@ fn new_or_load() { let file_path = temp_dir.path().join("store.db"); // init wallet when non-existent - let wallet_keychains = { + let wallet_keychains: BTreeMap<_, _> = { let db = bdk_file_store::Store::open_or_create_new(DB_MAGIC, &file_path) .expect("must create db"); let wallet = Wallet::new_or_load(get_test_wpkh(), None, db, Network::Testnet) .expect("must init wallet"); - wallet.keychains().clone() + wallet.keychains().map(|(k, v)| (*k, v.clone())).collect() }; // wrong network @@ -161,6 +172,49 @@ fn new_or_load() { ); } + // wrong external descriptor + { + let exp_descriptor = get_test_tr_single_sig(); + let got_descriptor = get_test_wpkh() + .into_wallet_descriptor(&Secp256k1::new(), Network::Testnet) + .unwrap() + .0; + + let db = + bdk_file_store::Store::open_or_create_new(DB_MAGIC, &file_path).expect("must open db"); + let err = Wallet::new_or_load(exp_descriptor, None, db, Network::Testnet) + .expect_err("wrong external descriptor"); + assert!( + matches!( + err, + bdk::wallet::NewOrLoadError::LoadedDescriptorDoesNotMatch { ref got, keychain } + if got == &Some(got_descriptor) && keychain == KeychainKind::External + ), + "err: {}", + err, + ); + } + + // wrong internal descriptor + { + let exp_descriptor = Some(get_test_tr_single_sig()); + let got_descriptor = None; + + let db = + bdk_file_store::Store::open_or_create_new(DB_MAGIC, &file_path).expect("must open db"); + let err = Wallet::new_or_load(get_test_wpkh(), exp_descriptor, db, Network::Testnet) + .expect_err("wrong internal descriptor"); + assert!( + matches!( + err, + bdk::wallet::NewOrLoadError::LoadedDescriptorDoesNotMatch { ref got, keychain } + if got == &got_descriptor && keychain == KeychainKind::Internal + ), + "err: {}", + err, + ); + } + // all parameters match { let db = @@ -168,7 +222,13 @@ fn new_or_load() { let wallet = Wallet::new_or_load(get_test_wpkh(), None, db, Network::Testnet) .expect("must recover wallet"); assert_eq!(wallet.network(), Network::Testnet); - assert_eq!(wallet.keychains(), &wallet_keychains); + assert_eq!( + wallet + .keychains() + .map(|(k, v)| (*k, v.clone())) + .collect::>(), + wallet_keychains + ); } } @@ -180,7 +240,6 @@ fn test_descriptor_checksum() { let raw_descriptor = wallet .keychains() - .iter() .next() .unwrap() .1 diff --git a/crates/chain/Cargo.toml b/crates/chain/Cargo.toml index 46252730dd..1dab9026ad 100644 --- a/crates/chain/Cargo.toml +++ b/crates/chain/Cargo.toml @@ -28,4 +28,4 @@ rand = "0.8" [features] default = ["std"] std = ["bitcoin/std", "miniscript/std"] -serde = ["serde_crate", "bitcoin/serde"] +serde = ["serde_crate", "bitcoin/serde", "miniscript/serde"] diff --git a/crates/chain/src/descriptor_ext.rs b/crates/chain/src/descriptor_ext.rs index 4c77c160ba..de30423191 100644 --- a/crates/chain/src/descriptor_ext.rs +++ b/crates/chain/src/descriptor_ext.rs @@ -1,10 +1,18 @@ -use crate::miniscript::{Descriptor, DescriptorPublicKey}; +use crate::{ + alloc::{string::ToString, vec::Vec}, + keychain::DescriptorId, + miniscript::{Descriptor, DescriptorPublicKey}, +}; +use bitcoin::hashes::{sha256, Hash}; /// A trait to extend the functionality of a miniscript descriptor. pub trait DescriptorExt { /// Returns the minimum value (in satoshis) at which an output is broadcastable. /// Panics if the descriptor wildcard is hardened. fn dust_value(&self) -> u64; + + /// Returns the descriptor id, calculated as the sha256 of the descriptor, checksum included. + fn descriptor_id(&self) -> DescriptorId; } impl DescriptorExt for Descriptor { @@ -15,4 +23,9 @@ impl DescriptorExt for Descriptor { .dust_value() .to_sat() } + + fn descriptor_id(&self) -> DescriptorId { + let descriptor_bytes = >::from(self.to_string().as_bytes()); + DescriptorId(sha256::Hash::hash(&descriptor_bytes)) + } } diff --git a/crates/chain/src/keychain/txout_index.rs b/crates/chain/src/keychain/txout_index.rs index e2c5d94ddb..62660b1b06 100644 --- a/crates/chain/src/keychain/txout_index.rs +++ b/crates/chain/src/keychain/txout_index.rs @@ -3,9 +3,12 @@ use crate::{ indexed_tx_graph::Indexer, miniscript::{Descriptor, DescriptorPublicKey}, spk_iter::BIP32_MAX_INDEX, - SpkIterator, SpkTxOutIndex, + DescriptorExt, SpkIterator, SpkTxOutIndex, +}; +use bitcoin::{ + hashes::{hash_newtype, sha256, Hash}, + OutPoint, Script, Transaction, TxOut, Txid, }; -use bitcoin::{OutPoint, Script, Transaction, TxOut, Txid}; use core::{ fmt::Debug, ops::{Bound, RangeBounds}, @@ -13,9 +16,14 @@ use core::{ use crate::Append; +hash_newtype! { + /// Represents the ID of a descriptor, defined as the sha256 hash of + /// the descriptor string, checksum included. + pub struct DescriptorId(pub sha256::Hash); +} /// Represents updates to the derivation index of a [`KeychainTxOutIndex`]. -/// It maps each keychain `K` to its last revealed index. +/// It maps each keychain `K` to a descriptor and its last revealed index. /// /// It can be applied to [`KeychainTxOutIndex`] with [`apply_changeset`]. [`ChangeSet] are /// monotone in that they will never decrease the revealed derivation index. @@ -35,46 +43,52 @@ use crate::Append; ) )] #[must_use] -pub struct ChangeSet(pub BTreeMap); - -impl ChangeSet { - /// Get the inner map of the keychain to its new derivation index. - pub fn as_inner(&self) -> &BTreeMap { - &self.0 - } +pub struct ChangeSet { + /// Contains the keychains that have been added and their respective descriptor + pub keychains_added: BTreeMap>, + /// Contains for each descriptor_id the last revealed index of derivation + pub last_revealed: BTreeMap, } impl Append for ChangeSet { /// Append another [`ChangeSet`] into self. /// + /// 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`]: /// If the keychain already exists, increase the index when the other's index > self's index. - /// If the keychain did not exist, append the new keychain. fn append(&mut self, mut other: Self) { - self.0.iter_mut().for_each(|(key, index)| { - if let Some(other_index) = other.0.remove(key) { + for (keychain, descriptor) in &mut self.keychains_added { + if let Some(other_descriptor) = other.keychains_added.remove(keychain) { + *descriptor = other_descriptor; + } + } + + for (descriptor_id, index) in &mut self.last_revealed { + if let Some(other_index) = other.last_revealed.remove(descriptor_id) { *index = other_index.max(*index); } - }); + } + // 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 - self.0.extend(other.0); + self.keychains_added.extend(other.keychains_added); + self.last_revealed.extend(other.last_revealed); } /// Returns whether the changeset are empty. fn is_empty(&self) -> bool { - self.0.is_empty() + self.last_revealed.is_empty() && self.keychains_added.is_empty() } } impl Default for ChangeSet { fn default() -> Self { - Self(Default::default()) - } -} - -impl AsRef> for ChangeSet { - fn as_ref(&self) -> &BTreeMap { - &self.0 + Self { + last_revealed: BTreeMap::default(), + keychains_added: BTreeMap::default(), + } } } @@ -119,7 +133,7 @@ const DEFAULT_LOOKAHEAD: u32 = 25; /// /// # Change sets /// -/// Methods that can update the last revealed index will return [`super::ChangeSet`] to report +/// Methods that can update the last revealed index or add keychains will return [`super::ChangeSet`] to report /// these changes. This can be persisted for future recovery. /// /// ## Synopsis @@ -144,10 +158,10 @@ const DEFAULT_LOOKAHEAD: u32 = 25; /// # let secp = bdk_chain::bitcoin::secp256k1::Secp256k1::signing_only(); /// # let (external_descriptor,_) = Descriptor::::parse_descriptor(&secp, "tr([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/0/*)").unwrap(); /// # let (internal_descriptor,_) = Descriptor::::parse_descriptor(&secp, "tr([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/1/*)").unwrap(); -/// # let (descriptor_for_user_42, _) = Descriptor::::parse_descriptor(&secp, "tr([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/2/*)").unwrap(); -/// txout_index.add_keychain(MyKeychain::External, external_descriptor); -/// txout_index.add_keychain(MyKeychain::Internal, internal_descriptor); -/// txout_index.add_keychain(MyKeychain::MyAppUser { user_id: 42 }, descriptor_for_user_42); +/// # let (descriptor_42, _) = Descriptor::::parse_descriptor(&secp, "tr([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/2/*)").unwrap(); +/// txout_index.insert_descriptor(external_descriptor, MyKeychain::External); +/// txout_index.insert_descriptor(internal_descriptor, MyKeychain::Internal); +/// txout_index.insert_descriptor(descriptor_42, MyKeychain::MyAppUser { user_id: 42 }); /// /// let new_spk_for_user = txout_index.reveal_next_spk(&MyKeychain::MyAppUser{ user_id: 42 }); /// ``` @@ -166,11 +180,13 @@ const DEFAULT_LOOKAHEAD: u32 = 25; /// [`all_unbounded_spk_iters`]: KeychainTxOutIndex::all_unbounded_spk_iters #[derive(Clone, Debug)] pub struct KeychainTxOutIndex { - inner: SpkTxOutIndex<(K, u32)>, - // descriptors of each keychain - keychains: BTreeMap>, + inner: SpkTxOutIndex<(DescriptorId, u32)>, + // keychain -> (descriptor, descriptor id) map + keychains_to_descriptors: BTreeMap)>, + // descriptor id -> keychain map + descriptor_ids_to_keychain: BTreeMap, // last revealed indexes - last_revealed: BTreeMap, + last_revealed: BTreeMap, // lookahead settings for each keychain lookahead: u32, } @@ -186,7 +202,13 @@ impl Indexer for KeychainTxOutIndex { fn index_txout(&mut self, outpoint: OutPoint, txout: &TxOut) -> Self::ChangeSet { match self.inner.scan_txout(outpoint, txout).cloned() { - Some((keychain, index)) => self.reveal_to_target(&keychain, index).1, + Some((descriptor_id, index)) => { + if let Some(keychain) = self.keychain_of_descriptor_id(&descriptor_id) { + self.reveal_to_target(&keychain.clone(), index).1 + } else { + super::ChangeSet::default() + } + } None => super::ChangeSet::default(), } } @@ -200,7 +222,13 @@ impl Indexer for KeychainTxOutIndex { } fn initial_changeset(&self) -> Self::ChangeSet { - super::ChangeSet(self.last_revealed.clone()) + super::ChangeSet { + keychains_added: self + .keychains() + .map(|(k, v)| (k.clone(), v.clone())) + .collect(), + last_revealed: self.last_revealed.clone(), + } } fn apply_changeset(&mut self, changeset: Self::ChangeSet) { @@ -226,7 +254,8 @@ impl KeychainTxOutIndex { pub fn new(lookahead: u32) -> Self { Self { inner: SpkTxOutIndex::default(), - keychains: BTreeMap::new(), + descriptor_ids_to_keychain: BTreeMap::new(), + keychains_to_descriptors: BTreeMap::new(), last_revealed: BTreeMap::new(), lookahead, } @@ -235,26 +264,44 @@ impl KeychainTxOutIndex { /// Methods that are *re-exposed* from the internal [`SpkTxOutIndex`]. impl KeychainTxOutIndex { + /// Returns the corresponding (descriptor_id, descriptor) given a keychain, if exists + fn descriptor_of_keychain( + &self, + keychain: &K, + ) -> Option<&(DescriptorId, Descriptor)> { + self.keychains_to_descriptors.get(keychain) + } + + /// Returns the corresponding keychain given a descriptor id, if exists + fn keychain_of_descriptor_id(&self, descriptor_id: &DescriptorId) -> Option<&K> { + self.descriptor_ids_to_keychain.get(descriptor_id) + } + /// Return a reference to the internal [`SpkTxOutIndex`]. /// /// **WARNING:** The internal index will contain lookahead spks. Refer to /// [struct-level docs](KeychainTxOutIndex) for more about `lookahead`. - pub fn inner(&self) -> &SpkTxOutIndex<(K, u32)> { + pub fn inner(&self) -> &SpkTxOutIndex<(DescriptorId, u32)> { &self.inner } - /// Get a reference to the set of indexed outpoints. - pub fn outpoints(&self) -> &BTreeSet<((K, u32), OutPoint)> { - self.inner.outpoints() + /// Get the set of indexed outpoints. + pub fn outpoints(&self) -> impl Iterator + '_ { + self.inner + .outpoints() + .iter() + .filter_map(|((desc_id, index), op)| { + self.keychain_of_descriptor_id(desc_id) + .map(|k| ((k.clone(), *index), *op)) + }) } /// Iterate over known txouts that spend to tracked script pubkeys. - pub fn txouts( - &self, - ) -> impl DoubleEndedIterator + ExactSizeIterator { - self.inner - .txouts() - .map(|((k, i), op, txo)| (k.clone(), *i, op, txo)) + pub fn txouts(&self) -> impl DoubleEndedIterator + '_ { + self.inner.txouts().filter_map(|((desc_id, i), op, txo)| { + self.keychain_of_descriptor_id(desc_id) + .map(|k| (k.clone(), *i, op, txo)) + }) } /// Finds all txouts on a transaction that has previously been scanned and indexed. @@ -264,7 +311,10 @@ impl KeychainTxOutIndex { ) -> impl DoubleEndedIterator { self.inner .txouts_in_tx(txid) - .map(|((k, i), op, txo)| (k.clone(), *i, op, txo)) + .filter_map(|((desc_id, i), op, txo)| { + self.keychain_of_descriptor_id(desc_id) + .map(|k| (k.clone(), *i, op, txo)) + }) } /// Return the [`TxOut`] of `outpoint` if it has been indexed. @@ -273,23 +323,26 @@ impl KeychainTxOutIndex { /// /// This calls [`SpkTxOutIndex::txout`] internally. pub fn txout(&self, outpoint: OutPoint) -> Option<(K, u32, &TxOut)> { - self.inner - .txout(outpoint) - .map(|((k, i), txo)| (k.clone(), *i, txo)) + let ((descriptor_id, index), txo) = self.inner.txout(outpoint)?; + let keychain = self.keychain_of_descriptor_id(descriptor_id)?; + Some((keychain.clone(), *index, txo)) } /// 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> { - self.inner.spk_at_index(&(keychain, index)) + let descriptor_id = self.keychains_to_descriptors.get(&keychain)?.0; + self.inner.spk_at_index(&(descriptor_id, index)) } /// 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)> { - self.inner.index_of_spk(script).cloned() + let (desc_id, last_index) = self.inner.index_of_spk(script)?; + self.keychain_of_descriptor_id(desc_id) + .map(|k| (k.clone(), *last_index)) } /// Returns whether the spk under the `keychain`'s `index` has been used. @@ -299,7 +352,11 @@ impl KeychainTxOutIndex { /// /// This calls [`SpkTxOutIndex::is_used`] internally. pub fn is_used(&self, keychain: K, index: u32) -> bool { - self.inner.is_used(&(keychain, index)) + 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, + } } /// Marks the script pubkey at `index` as used even though the tracker hasn't seen an output @@ -317,7 +374,11 @@ impl KeychainTxOutIndex { /// /// [`unmark_used`]: Self::unmark_used pub fn mark_used(&mut self, keychain: K, index: u32) -> bool { - self.inner.mark_used(&(keychain, index)) + 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, + } } /// Undoes the effect of [`mark_used`]. Returns whether the `index` is inserted back into @@ -330,7 +391,11 @@ impl KeychainTxOutIndex { /// /// [`mark_used`]: Self::mark_used pub fn unmark_used(&mut self, keychain: K, index: u32) -> bool { - self.inner.unmark_used(&(keychain, index)) + 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, + } } /// Computes total input value going from script pubkeys in the index (sent) and the total output @@ -357,29 +422,42 @@ impl KeychainTxOutIndex { } impl KeychainTxOutIndex { - /// Return a reference to the internal map of keychain to descriptors. - pub fn keychains(&self) -> &BTreeMap> { - &self.keychains + /// Return the map of the keychain to descriptors. + pub fn keychains(&self) -> impl Iterator)> + '_ { + self.keychains_to_descriptors + .iter() + .map(|(k, (_, d))| (k, d)) } - /// Add a keychain to the tracker's `txout_index` with a descriptor to derive addresses. + /// Insert a descriptor into the tracker's `txout_index`, with a keychain associated to it. /// - /// Adding a keychain means you will be able to derive new script pubkeys under that keychain + /// Adding a descriptor means you will be able to derive new script pubkeys under it /// and the txout index will discover transaction outputs with those script pubkeys. /// - /// # Panics - /// - /// This will panic if a different `descriptor` is introduced to the same `keychain`. - pub fn add_keychain(&mut self, keychain: K, descriptor: Descriptor) { - let old_descriptor = &*self - .keychains - .entry(keychain.clone()) - .or_insert_with(|| descriptor.clone()); - assert_eq!( - &descriptor, old_descriptor, - "keychain already contains a different descriptor" - ); + /// 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, + descriptor: Descriptor, + keychain: K, + ) -> super::ChangeSet { + let descriptor_id = descriptor.descriptor_id(); + self.keychains_to_descriptors + .insert(keychain.clone(), (descriptor_id, descriptor.clone())); + self.descriptor_ids_to_keychain + .insert(descriptor_id, keychain.clone()); self.replenish_lookahead(&keychain, self.lookahead); + super::ChangeSet { + keychains_added: [(keychain, descriptor)].into(), + 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> { + self.keychains_to_descriptors.get(keychain).map(|(_, d)| d) } /// Get the lookahead setting. @@ -402,26 +480,33 @@ impl KeychainTxOutIndex { } fn replenish_lookahead(&mut self, keychain: &K, lookahead: u32) { - let descriptor = self.keychains.get(keychain).expect("keychain must exist"); + let (descriptor_id, descriptor) = self + .keychains_to_descriptors + .get(keychain) + .expect("keychain must exist") + .clone(); let next_store_index = self.next_store_index(keychain); - let next_reveal_index = self.last_revealed.get(keychain).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((keychain.clone(), new_index), new_spk); + 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.clone(), + 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); } } fn next_store_index(&self, keychain: &K) -> u32 { + let descriptor_id = self + .descriptor_of_keychain(keychain) + .expect("keychain must exist") + .0; self.inner() .all_spks() // This range is filtering out the spks with a keychain different than // `keychain`. We don't use filter here as range is more optimized. - .range((keychain.clone(), u32::MIN)..(keychain.clone(), u32::MAX)) + .range((descriptor_id, u32::MIN)..(descriptor_id, u32::MAX)) .last() .map_or(0, |((_, index), _)| *index + 1) } @@ -432,60 +517,83 @@ impl KeychainTxOutIndex { /// /// This will panic if the given `keychain`'s descriptor does not exist. pub fn unbounded_spk_iter(&self, keychain: &K) -> SpkIterator> { - SpkIterator::new( - self.keychains - .get(keychain) - .expect("keychain does not exist") - .clone(), - ) + let descriptor = self + .descriptor_of_keychain(keychain) + .expect("Keychain must exist") + .1 + .clone(); + SpkIterator::new(descriptor) } /// Get unbounded spk iterators for all keychains. pub fn all_unbounded_spk_iters( &self, ) -> BTreeMap>> { - self.keychains + self.keychains_to_descriptors .iter() - .map(|(k, descriptor)| (k.clone(), SpkIterator::new(descriptor.clone()))) + .map(|(k, (_, descriptor))| (k.clone(), SpkIterator::new(descriptor.clone()))) .collect() } /// Iterate over revealed spks of all keychains. pub fn revealed_spks(&self) -> impl DoubleEndedIterator + Clone { - self.keychains.keys().flat_map(|keychain| { + self.keychains_to_descriptors.keys().flat_map(|keychain| { self.revealed_keychain_spks(keychain) .map(|(i, spk)| (keychain.clone(), i, spk)) }) } /// Iterate over revealed spks of the given `keychain`. + /// + /// # Panics + /// + /// This will panic if the given `keychain`'s descriptor does not exist. pub fn revealed_keychain_spks( &self, keychain: &K, ) -> impl DoubleEndedIterator + Clone { - let next_i = self.last_revealed.get(keychain).map_or(0, |&i| i + 1); + let desc_id = self + .keychains_to_descriptors + .get(keychain) + .expect("Must exist") + .0; + let next_i = self.last_revealed.get(&desc_id).map_or(0, |&i| i + 1); self.inner .all_spks() - .range((keychain.clone(), u32::MIN)..(keychain.clone(), next_i)) + .range((desc_id, u32::MIN)..(desc_id, next_i)) .map(|((_, i), spk)| (*i, spk.as_script())) } /// Iterate over revealed, but unused, spks of all keychains. pub fn unused_spks(&self) -> impl DoubleEndedIterator + Clone { - self.keychains.keys().flat_map(|keychain| { + self.keychains_to_descriptors.keys().flat_map(|keychain| { self.unused_keychain_spks(keychain) .map(|(i, spk)| (keychain.clone(), i, spk)) }) } /// Iterate over revealed, but unused, spks of the given `keychain`. + /// + /// # Panics + /// + /// This will panic if the given `keychain`'s descriptor does not exist. pub fn unused_keychain_spks( &self, keychain: &K, ) -> impl DoubleEndedIterator + Clone { - let next_i = self.last_revealed.get(keychain).map_or(0, |&i| i + 1); + let desc_id = self + .keychains_to_descriptors + .get(keychain) + .map(|(desc_id, _)| desc_id) + .cloned() + .unwrap_or( + sha256::Hash::from_byte_array([0; 32]) + //.to_byte_array() + .into(), + ); + let next_i = self.last_revealed.get(&desc_id).map_or(0, |&i| i + 1); self.inner - .unused_spks((keychain.clone(), u32::MIN)..(keychain.clone(), next_i)) + .unused_spks((desc_id, u32::MIN)..(desc_id, next_i)) .map(|((_, i), spk)| (*i, spk)) } @@ -504,8 +612,11 @@ impl KeychainTxOutIndex { /// /// Panics if the `keychain` does not exist. pub fn next_index(&self, keychain: &K) -> (u32, bool) { - let descriptor = self.keychains.get(keychain).expect("keychain must exist"); - let last_index = self.last_revealed.get(keychain).cloned(); + let (descriptor_id, descriptor) = self + .keychains_to_descriptors + .get(keychain) + .expect("must exist"); + let last_index = self.last_revealed.get(descriptor_id).cloned(); // we can only get the next index if the wildcard exists. let has_wildcard = descriptor.has_wildcard(); @@ -528,16 +639,35 @@ impl KeychainTxOutIndex { /// Get the last derivation index that is revealed for each keychain. /// /// Keychains with no revealed indices will not be included in the returned [`BTreeMap`]. - pub fn last_revealed_indices(&self) -> &BTreeMap { - &self.last_revealed + pub fn last_revealed_indices(&self) -> BTreeMap { + self.last_revealed + .iter() + .filter_map(|(descriptor_id, index)| { + self.keychain_of_descriptor_id(descriptor_id) + .map(|k| (k.clone(), *index)) + }) + .collect() } /// Get the last derivation index revealed for `keychain`. + /// + /// # Panics + /// + /// Panics if the `keychain` does not exist. pub fn last_revealed_index(&self, keychain: &K) -> Option { - self.last_revealed.get(keychain).cloned() + let descriptor_id = self + .keychains_to_descriptors + .get(keychain) + .expect("keychain must exist") + .0; + self.last_revealed.get(&descriptor_id).cloned() } /// Convenience method to call [`Self::reveal_to_target`] on multiple keychains. + /// + /// # Panics + /// + /// Panics if any keychain in `keychains` does not exist. pub fn reveal_to_target_multi( &mut self, keychains: &BTreeMap, @@ -581,13 +711,19 @@ impl KeychainTxOutIndex { SpkIterator>, super::ChangeSet, ) { - let descriptor = self.keychains.get(keychain).expect("keychain must exist"); + let (descriptor_id, descriptor) = self + .keychains_to_descriptors + .get(keychain) + .expect("must exist"); + // Cloning since I need to modify self.inner, and I can't do that while + // I'm borrowing descriptor_id and descriptor + let (descriptor_id, descriptor) = (*descriptor_id, descriptor.clone()); let has_wildcard = descriptor.has_wildcard(); let target_index = if has_wildcard { target_index } else { 0 }; let next_reveal_index = self .last_revealed - .get(keychain) + .get(&descriptor_id) .map_or(0, |index| *index + 1); debug_assert!(next_reveal_index + self.lookahead >= self.next_store_index(keychain)); @@ -595,10 +731,7 @@ impl KeychainTxOutIndex { // If the target_index is already revealed, we are done if next_reveal_index > target_index { return ( - SpkIterator::new_with_range( - descriptor.clone(), - next_reveal_index..next_reveal_index, - ), + SpkIterator::new_with_range(descriptor, next_reveal_index..next_reveal_index), super::ChangeSet::default(), ); } @@ -607,10 +740,8 @@ impl KeychainTxOutIndex { // 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 let range = next_reveal_index + self.lookahead..=target_index + self.lookahead; - for (new_index, new_spk) in SpkIterator::new_with_range(descriptor, range) { - let _inserted = self - .inner - .insert_spk((keychain.clone(), new_index), new_spk); + 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); debug_assert!(_inserted, "must not have existing spk"); debug_assert!( has_wildcard || new_index == 0, @@ -618,11 +749,14 @@ impl KeychainTxOutIndex { ); } - let _old_index = self.last_revealed.insert(keychain.clone(), target_index); + let _old_index = self.last_revealed.insert(descriptor_id, target_index); debug_assert!(_old_index < Some(target_index)); ( - SpkIterator::new_with_range(descriptor.clone(), next_reveal_index..target_index + 1), - super::ChangeSet(core::iter::once((keychain.clone(), target_index)).collect()), + 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(), + }, ) } @@ -641,11 +775,16 @@ impl KeychainTxOutIndex { /// /// Panics if the `keychain` does not exist. pub fn reveal_next_spk(&mut self, keychain: &K) -> ((u32, &Script), super::ChangeSet) { + let descriptor_id = self + .keychains_to_descriptors + .get(keychain) + .expect("Must exist") + .0; let (next_index, _) = self.next_index(keychain); let changeset = self.reveal_to_target(keychain, next_index).1; let script = self .inner - .spk_at_index(&(keychain.clone(), next_index)) + .spk_at_index(&(descriptor_id, next_index)) .expect("script must already be stored"); ((next_index, script), changeset) } @@ -682,6 +821,10 @@ impl KeychainTxOutIndex { /// /// Use [`keychain_outpoints_in_range`](KeychainTxOutIndex::keychain_outpoints_in_range) to /// iterate over a specific derivation range. + /// + /// # Panics + /// + /// Panics if `keychain` has never been added to the index pub fn keychain_outpoints( &self, keychain: &K, @@ -691,19 +834,28 @@ impl KeychainTxOutIndex { /// Iterate over [`OutPoint`]s that point to `TxOut`s with script pubkeys derived from /// `keychain` in a given derivation `range`. + /// + /// # Panics + /// + /// Panics if `keychain` has never been added to the index pub fn keychain_outpoints_in_range( &self, keychain: &K, range: impl RangeBounds, ) -> impl DoubleEndedIterator + '_ { + let descriptor_id = self + .keychains_to_descriptors + .get(keychain) + .expect("Must exist") + .0; let start = match range.start_bound() { - Bound::Included(i) => Bound::Included((keychain.clone(), *i)), - Bound::Excluded(i) => Bound::Excluded((keychain.clone(), *i)), + Bound::Included(i) => Bound::Included((descriptor_id, *i)), + Bound::Excluded(i) => Bound::Excluded((descriptor_id, *i)), Bound::Unbounded => Bound::Unbounded, }; let end = match range.end_bound() { - Bound::Included(i) => Bound::Included((keychain.clone(), *i)), - Bound::Excluded(i) => Bound::Excluded((keychain.clone(), *i)), + Bound::Included(i) => Bound::Included((descriptor_id, *i)), + Bound::Excluded(i) => Bound::Excluded((descriptor_id, *i)), Bound::Unbounded => Bound::Unbounded, }; self.inner @@ -720,7 +872,7 @@ impl KeychainTxOutIndex { /// 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 { - self.keychains + self.keychains_to_descriptors .iter() .filter_map(|(keychain, _)| { self.last_used_index(keychain) @@ -731,7 +883,25 @@ impl KeychainTxOutIndex { /// Applies the derivation changeset to the [`KeychainTxOutIndex`], extending the number of /// derived scripts per keychain, as specified in the `changeset`. + /// + /// # Panics + /// + /// This will panic if a different `descriptor` is introduced to the same `keychain`. pub fn apply_changeset(&mut self, changeset: super::ChangeSet) { - let _ = self.reveal_to_target_multi(&changeset.0); + let ChangeSet { + keychains_added, + last_revealed, + } = changeset; + for (keychain, descriptor) in keychains_added { + let _ = self.insert_descriptor(descriptor, keychain); + } + let last_revealed = last_revealed + .into_iter() + .filter_map(|(descriptor_id, index)| { + self.keychain_of_descriptor_id(&descriptor_id) + .map(|k| (k.clone(), index)) + }) + .collect(); + let _ = self.reveal_to_target_multi(&last_revealed); } } diff --git a/crates/chain/src/spk_iter.rs b/crates/chain/src/spk_iter.rs index 6846e66d48..e0eb42d757 100644 --- a/crates/chain/src/spk_iter.rs +++ b/crates/chain/src/spk_iter.rs @@ -158,8 +158,8 @@ mod test { let (external_descriptor,_) = Descriptor::::parse_descriptor(&secp, "tr([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/0/*)").unwrap(); let (internal_descriptor,_) = Descriptor::::parse_descriptor(&secp, "tr([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/1/*)").unwrap(); - txout_index.add_keychain(TestKeychain::External, external_descriptor.clone()); - txout_index.add_keychain(TestKeychain::Internal, internal_descriptor.clone()); + let _ = txout_index.insert_descriptor(external_descriptor.clone(), TestKeychain::External); + let _ = txout_index.insert_descriptor(internal_descriptor.clone(), TestKeychain::Internal); (txout_index, external_descriptor, internal_descriptor) } diff --git a/crates/chain/tests/common/mod.rs b/crates/chain/tests/common/mod.rs index 8688fc0bca..7e95b6d3b0 100644 --- a/crates/chain/tests/common/mod.rs +++ b/crates/chain/tests/common/mod.rs @@ -78,10 +78,13 @@ pub fn new_tx(lt: u32) -> bitcoin::Transaction { } #[allow(unused)] -pub const DESCRIPTORS: [&str; 5] = [ +pub const DESCRIPTORS: [&str; 7] = [ "tr([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/0/*)", + "tr([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/1/*)", "wpkh([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/1/0/*)", "tr(tprv8ZgxMBicQKsPd3krDUsBAmtnRsK3rb8u5yi1zhQgMhF1tR8MW7xfE4rnrbbsrbPR52e7rKapu6ztw1jXveJSCGHEriUGZV7mCe88duLp5pj/86'/1'/0'/0/*)", "tr(tprv8ZgxMBicQKsPd3krDUsBAmtnRsK3rb8u5yi1zhQgMhF1tR8MW7xfE4rnrbbsrbPR52e7rKapu6ztw1jXveJSCGHEriUGZV7mCe88duLp5pj/86'/1'/0'/1/*)", "wpkh(xprv9s21ZrQH143K4EXURwMHuLS469fFzZyXk7UUpdKfQwhoHcAiYTakpe8pMU2RiEdvrU9McyuE7YDoKcXkoAwEGoK53WBDnKKv2zZbb9BzttX/1/0/*)", + // non-wildcard + "wpkh([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/1/0)", ]; diff --git a/crates/chain/tests/common/tx_template.rs b/crates/chain/tests/common/tx_template.rs index b7b550240a..3b5fbe2302 100644 --- a/crates/chain/tests/common/tx_template.rs +++ b/crates/chain/tests/common/tx_template.rs @@ -52,7 +52,8 @@ impl TxOutTemplate { pub fn init_graph<'a, A: Anchor + Clone + 'a>( tx_templates: impl IntoIterator>, ) -> (TxGraph, SpkTxOutIndex, HashMap<&'a str, Txid>) { - let (descriptor, _) = Descriptor::parse_descriptor(&Secp256k1::signing_only(), super::DESCRIPTORS[2]).unwrap(); + let (descriptor, _) = + Descriptor::parse_descriptor(&Secp256k1::signing_only(), super::DESCRIPTORS[2]).unwrap(); let mut graph = TxGraph::::default(); let mut spk_index = SpkTxOutIndex::default(); (0..10).for_each(|index| { diff --git a/crates/chain/tests/test_indexed_tx_graph.rs b/crates/chain/tests/test_indexed_tx_graph.rs index 54bfd448e6..4cff93094d 100644 --- a/crates/chain/tests/test_indexed_tx_graph.rs +++ b/crates/chain/tests/test_indexed_tx_graph.rs @@ -3,11 +3,12 @@ mod common; use std::collections::BTreeSet; +use crate::common::DESCRIPTORS; use bdk_chain::{ indexed_tx_graph::{self, IndexedTxGraph}, keychain::{self, Balance, KeychainTxOutIndex}, local_chain::LocalChain, - tx_graph, BlockId, ChainPosition, ConfirmationHeightAnchor, + tx_graph, BlockId, ChainPosition, ConfirmationHeightAnchor, DescriptorExt, }; use bitcoin::{secp256k1::Secp256k1, OutPoint, Script, ScriptBuf, Transaction, TxIn, TxOut}; use miniscript::Descriptor; @@ -21,16 +22,15 @@ use miniscript::Descriptor; /// agnostic. #[test] fn insert_relevant_txs() { - let (descriptor, _) = - Descriptor::parse_descriptor(&Secp256k1::signing_only(), common::DESCRIPTORS[0]) - .expect("must be valid"); + let (descriptor, _) = Descriptor::parse_descriptor(&Secp256k1::signing_only(), DESCRIPTORS[0]) + .expect("must be valid"); let spk_0 = descriptor.at_derivation_index(0).unwrap().script_pubkey(); let spk_1 = descriptor.at_derivation_index(9).unwrap().script_pubkey(); let mut graph = IndexedTxGraph::>::new( KeychainTxOutIndex::new(10), ); - graph.index.add_keychain((), descriptor); + let _ = graph.index.insert_descriptor(descriptor.clone(), ()); let tx_a = Transaction { output: vec![ @@ -69,7 +69,10 @@ fn insert_relevant_txs() { txs: txs.clone().into(), ..Default::default() }, - indexer: keychain::ChangeSet([((), 9_u32)].into()), + indexer: keychain::ChangeSet { + last_revealed: [(descriptor.descriptor_id(), 9_u32)].into(), + keychains_added: [].into(), + }, }; assert_eq!( @@ -77,7 +80,16 @@ fn insert_relevant_txs() { changeset, ); - assert_eq!(graph.initial_changeset(), changeset,); + // The initial changeset will also contain info about the keychain we added + let initial_changeset = indexed_tx_graph::ChangeSet { + graph: changeset.graph, + indexer: keychain::ChangeSet { + last_revealed: changeset.indexer.last_revealed, + keychains_added: [((), descriptor)].into(), + }, + }; + + assert_eq!(graph.initial_changeset(), initial_changeset); } #[test] @@ -125,8 +137,8 @@ fn test_list_owned_txouts() { KeychainTxOutIndex::new(10), ); - graph.index.add_keychain("keychain_1".into(), desc_1); - graph.index.add_keychain("keychain_2".into(), desc_2); + let _ = graph.index.insert_descriptor(desc_1, "keychain_1".into()); + let _ = graph.index.insert_descriptor(desc_2, "keychain_2".into()); // Get trusted and untrusted addresses @@ -239,26 +251,18 @@ fn test_list_owned_txouts() { .unwrap_or_else(|| panic!("block must exist at {}", height)); let txouts = graph .graph() - .filter_chain_txouts( - &local_chain, - chain_tip, - graph.index.outpoints().iter().cloned(), - ) + .filter_chain_txouts(&local_chain, chain_tip, graph.index.outpoints()) .collect::>(); let utxos = graph .graph() - .filter_chain_unspents( - &local_chain, - chain_tip, - graph.index.outpoints().iter().cloned(), - ) + .filter_chain_unspents(&local_chain, chain_tip, graph.index.outpoints()) .collect::>(); let balance = graph.graph().balance( &local_chain, chain_tip, - graph.index.outpoints().iter().cloned(), + graph.index.outpoints(), |_, spk: &Script| trusted_spks.contains(&spk.to_owned()), ); diff --git a/crates/chain/tests/test_keychain_txout_index.rs b/crates/chain/tests/test_keychain_txout_index.rs index 2bf27e8423..41fc210e3a 100644 --- a/crates/chain/tests/test_keychain_txout_index.rs +++ b/crates/chain/tests/test_keychain_txout_index.rs @@ -5,36 +5,44 @@ mod common; use bdk_chain::{ collections::BTreeMap, indexed_tx_graph::Indexer, - keychain::{self, ChangeSet, KeychainTxOutIndex}, - Append, + keychain::{self, ChangeSet, DescriptorId, KeychainTxOutIndex}, + Append, DescriptorExt, }; use bitcoin::{secp256k1::Secp256k1, OutPoint, ScriptBuf, Transaction, TxOut}; use miniscript::{Descriptor, DescriptorPublicKey}; +use crate::common::DESCRIPTORS; + #[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd)] enum TestKeychain { External, Internal, } -fn init_txout_index( - lookahead: u32, -) -> ( - bdk_chain::keychain::KeychainTxOutIndex, - Descriptor, - Descriptor, -) { +struct TestIndex { + index: bdk_chain::keychain::KeychainTxOutIndex, + external_descriptor: Descriptor, + internal_descriptor: Descriptor, +} + +fn init_txout_index(lookahead: u32) -> TestIndex { let mut txout_index = bdk_chain::keychain::KeychainTxOutIndex::::new(lookahead); let secp = bdk_chain::bitcoin::secp256k1::Secp256k1::signing_only(); - let (external_descriptor,_) = Descriptor::::parse_descriptor(&secp, "tr([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/0/*)").unwrap(); - let (internal_descriptor,_) = Descriptor::::parse_descriptor(&secp, "tr([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/1/*)").unwrap(); - - txout_index.add_keychain(TestKeychain::External, external_descriptor.clone()); - txout_index.add_keychain(TestKeychain::Internal, internal_descriptor.clone()); - - (txout_index, external_descriptor, internal_descriptor) + let (external_descriptor, _) = + Descriptor::::parse_descriptor(&secp, DESCRIPTORS[0]).unwrap(); + let (internal_descriptor, _) = + Descriptor::::parse_descriptor(&secp, DESCRIPTORS[1]).unwrap(); + + let _ = txout_index.insert_descriptor(external_descriptor.clone(), TestKeychain::External); + let _ = txout_index.insert_descriptor(internal_descriptor.clone(), TestKeychain::Internal); + + TestIndex { + index: txout_index, + external_descriptor, + internal_descriptor, + } } fn spk_at_index(descriptor: &Descriptor, index: u32) -> ScriptBuf { @@ -46,59 +54,130 @@ fn spk_at_index(descriptor: &Descriptor, index: u32) -> Scr #[test] fn append_keychain_derivation_indices() { - #[derive(Ord, PartialOrd, Eq, PartialEq, Clone, Debug)] - enum Keychain { - One, - Two, - Three, - Four, - } - let mut lhs_di = BTreeMap::::default(); - let mut rhs_di = BTreeMap::::default(); - lhs_di.insert(Keychain::One, 7); - lhs_di.insert(Keychain::Two, 0); - rhs_di.insert(Keychain::One, 3); - rhs_di.insert(Keychain::Two, 5); - lhs_di.insert(Keychain::Three, 3); - rhs_di.insert(Keychain::Four, 4); - - let mut lhs = ChangeSet(lhs_di); - let rhs = ChangeSet(rhs_di); + let secp = bitcoin::secp256k1::Secp256k1::signing_only(); + let descriptor_ids: Vec<_> = DESCRIPTORS + .iter() + .take(4) + .map(|d| { + Descriptor::::parse_descriptor(&secp, d) + .unwrap() + .0 + .descriptor_id() + }) + .collect(); + + let mut lhs_di = BTreeMap::::default(); + let mut rhs_di = BTreeMap::::default(); + lhs_di.insert(descriptor_ids[0], 7); + lhs_di.insert(descriptor_ids[1], 0); + rhs_di.insert(descriptor_ids[1], 3); + rhs_di.insert(descriptor_ids[1], 5); + lhs_di.insert(descriptor_ids[2], 3); + rhs_di.insert(descriptor_ids[3], 4); + + let mut lhs = ChangeSet { + keychains_added: BTreeMap::<(), _>::new(), + last_revealed: lhs_di, + }; + let rhs = ChangeSet { + keychains_added: BTreeMap::<(), _>::new(), + last_revealed: rhs_di, + }; lhs.append(rhs); - // Exiting index doesn't update if the new index in `other` is lower than `self`. - assert_eq!(lhs.0.get(&Keychain::One), Some(&7)); + // Existing index doesn't update if the new index in `other` is lower than `self`. + assert_eq!(lhs.last_revealed.get(&descriptor_ids[0]), Some(&7)); // Existing index updates if the new index in `other` is higher than `self`. - assert_eq!(lhs.0.get(&Keychain::Two), Some(&5)); + assert_eq!(lhs.last_revealed.get(&descriptor_ids[1]), Some(&5)); // Existing index is unchanged if keychain doesn't exist in `other`. - assert_eq!(lhs.0.get(&Keychain::Three), Some(&3)); + assert_eq!(lhs.last_revealed.get(&descriptor_ids[2]), Some(&3)); // New keychain gets added if the keychain is in `other` but not in `self`. - assert_eq!(lhs.0.get(&Keychain::Four), Some(&4)); + assert_eq!(lhs.last_revealed.get(&descriptor_ids[3]), Some(&4)); +} + +#[test] +fn test_insert_different_desc_same_keychain() { + let TestIndex { + index: mut txout_index, + external_descriptor, + internal_descriptor, + } = init_txout_index(0); + assert_eq!( + txout_index.keychains().collect::>(), + vec![ + (&TestKeychain::External, &external_descriptor), + (&TestKeychain::Internal, &internal_descriptor) + ] + ); + + let changeset = ChangeSet { + keychains_added: [(TestKeychain::External, internal_descriptor.clone())].into(), + last_revealed: [].into(), + }; + txout_index.apply_changeset(changeset); + + assert_eq!( + txout_index.keychains().collect::>(), + vec![ + (&TestKeychain::External, &internal_descriptor), + (&TestKeychain::Internal, &internal_descriptor) + ] + ); + + let changeset = ChangeSet { + keychains_added: [(TestKeychain::Internal, external_descriptor.clone())].into(), + last_revealed: [].into(), + }; + txout_index.apply_changeset(changeset); + + assert_eq!( + txout_index.keychains().collect::>(), + vec![ + (&TestKeychain::External, &internal_descriptor), + (&TestKeychain::Internal, &external_descriptor) + ] + ); } #[test] fn test_set_all_derivation_indices() { use bdk_chain::indexed_tx_graph::Indexer; - let (mut txout_index, _, _) = init_txout_index(0); + let TestIndex { + index: mut txout_index, + external_descriptor, + internal_descriptor, + } = init_txout_index(0); let derive_to: BTreeMap<_, _> = [(TestKeychain::External, 12), (TestKeychain::Internal, 24)].into(); + let last_revealed: BTreeMap<_, _> = [ + (external_descriptor.descriptor_id(), 12), + (internal_descriptor.descriptor_id(), 24), + ] + .into(); assert_eq!( - txout_index.reveal_to_target_multi(&derive_to).1.as_inner(), - &derive_to + txout_index.reveal_to_target_multi(&derive_to).1, + ChangeSet { + keychains_added: BTreeMap::new(), + last_revealed: last_revealed.clone() + } ); - assert_eq!(txout_index.last_revealed_indices(), &derive_to); + assert_eq!(txout_index.last_revealed_indices(), derive_to); assert_eq!( txout_index.reveal_to_target_multi(&derive_to).1, keychain::ChangeSet::default(), "no changes if we set to the same thing" ); - assert_eq!(txout_index.initial_changeset().as_inner(), &derive_to); + assert_eq!(txout_index.initial_changeset().last_revealed, last_revealed); } #[test] fn test_lookahead() { - let (mut txout_index, external_desc, internal_desc) = init_txout_index(10); + let TestIndex { + index: mut txout_index, + external_descriptor, + internal_descriptor, + } = init_txout_index(10); // given: // - external lookahead set to 10 @@ -112,11 +191,11 @@ fn test_lookahead() { txout_index.reveal_to_target(&TestKeychain::External, index); assert_eq!( revealed_spks.collect::>(), - vec![(index, spk_at_index(&external_desc, index))], + vec![(index, spk_at_index(&external_descriptor, index))], ); assert_eq!( - revealed_changeset.as_inner(), - &[(TestKeychain::External, index)].into() + &revealed_changeset.last_revealed, + &[(external_descriptor.descriptor_id(), index)].into() ); assert_eq!( @@ -163,12 +242,12 @@ fn test_lookahead() { assert_eq!( revealed_spks.collect::>(), (0..=24) - .map(|index| (index, spk_at_index(&internal_desc, index))) + .map(|index| (index, spk_at_index(&internal_descriptor, index))) .collect::>(), ); assert_eq!( - revealed_changeset.as_inner(), - &[(TestKeychain::Internal, 24)].into() + &revealed_changeset.last_revealed, + &[(internal_descriptor.descriptor_id(), 24)].into() ); assert_eq!( txout_index.inner().all_spks().len(), @@ -204,14 +283,14 @@ fn test_lookahead() { let tx = Transaction { output: vec![ TxOut { - script_pubkey: external_desc + script_pubkey: external_descriptor .at_derivation_index(external_index) .unwrap() .script_pubkey(), value: 10_000, }, TxOut { - script_pubkey: internal_desc + script_pubkey: internal_descriptor .at_derivation_index(internal_index) .unwrap() .script_pubkey(), @@ -251,14 +330,18 @@ fn test_lookahead() { // - last used index should change as expected #[test] fn test_scan_with_lookahead() { - let (mut txout_index, external_desc, _) = init_txout_index(10); + let TestIndex { + index: mut txout_index, + external_descriptor, + .. + } = init_txout_index(10); let spks: BTreeMap = [0, 10, 20, 30] .into_iter() .map(|i| { ( i, - external_desc + external_descriptor .at_derivation_index(i) .unwrap() .script_pubkey(), @@ -275,8 +358,8 @@ fn test_scan_with_lookahead() { let changeset = txout_index.index_txout(op, &txout); assert_eq!( - changeset.as_inner(), - &[(TestKeychain::External, spk_i)].into() + &changeset.last_revealed, + &[(external_descriptor.descriptor_id(), spk_i)].into() ); assert_eq!( txout_index.last_revealed_index(&TestKeychain::External), @@ -289,7 +372,7 @@ fn test_scan_with_lookahead() { } // now try with index 41 (lookahead surpassed), we expect that the txout to not be indexed - let spk_41 = external_desc + let spk_41 = external_descriptor .at_derivation_index(41) .unwrap() .script_pubkey(); @@ -305,11 +388,11 @@ fn test_scan_with_lookahead() { #[test] #[rustfmt::skip] fn test_wildcard_derivations() { - let (mut txout_index, external_desc, _) = init_txout_index(0); - let external_spk_0 = external_desc.at_derivation_index(0).unwrap().script_pubkey(); - let external_spk_16 = external_desc.at_derivation_index(16).unwrap().script_pubkey(); - let external_spk_26 = external_desc.at_derivation_index(26).unwrap().script_pubkey(); - let external_spk_27 = external_desc.at_derivation_index(27).unwrap().script_pubkey(); + let TestIndex { index: mut txout_index, external_descriptor, .. } = init_txout_index(0); + let external_spk_0 = external_descriptor.at_derivation_index(0).unwrap().script_pubkey(); + let external_spk_16 = external_descriptor.at_derivation_index(16).unwrap().script_pubkey(); + let external_spk_26 = external_descriptor.at_derivation_index(26).unwrap().script_pubkey(); + let external_spk_27 = external_descriptor.at_derivation_index(27).unwrap().script_pubkey(); // - nothing is derived // - unused list is also empty @@ -320,10 +403,10 @@ fn test_wildcard_derivations() { assert_eq!(txout_index.next_index(&TestKeychain::External), (0, true)); let (spk, changeset) = txout_index.reveal_next_spk(&TestKeychain::External); assert_eq!(spk, (0_u32, external_spk_0.as_script())); - assert_eq!(changeset.as_inner(), &[(TestKeychain::External, 0)].into()); + assert_eq!(&changeset.last_revealed, &[(external_descriptor.descriptor_id(), 0)].into()); let (spk, changeset) = txout_index.next_unused_spk(&TestKeychain::External); assert_eq!(spk, (0_u32, external_spk_0.as_script())); - assert_eq!(changeset.as_inner(), &[].into()); + assert_eq!(&changeset.last_revealed, &[].into()); // - derived till 25 // - used all spks till 15. @@ -344,11 +427,11 @@ fn test_wildcard_derivations() { let (spk, changeset) = txout_index.reveal_next_spk(&TestKeychain::External); assert_eq!(spk, (26, external_spk_26.as_script())); - assert_eq!(changeset.as_inner(), &[(TestKeychain::External, 26)].into()); + assert_eq!(&changeset.last_revealed, &[(external_descriptor.descriptor_id(), 26)].into()); let (spk, changeset) = txout_index.next_unused_spk(&TestKeychain::External); assert_eq!(spk, (16, external_spk_16.as_script())); - assert_eq!(changeset.as_inner(), &[].into()); + assert_eq!(&changeset.last_revealed, &[].into()); // - Use all the derived till 26. // - next_unused() = ((27, ), keychain::ChangeSet) @@ -358,7 +441,7 @@ fn test_wildcard_derivations() { let (spk, changeset) = txout_index.next_unused_spk(&TestKeychain::External); assert_eq!(spk, (27, external_spk_27.as_script())); - assert_eq!(changeset.as_inner(), &[(TestKeychain::External, 27)].into()); + assert_eq!(&changeset.last_revealed, &[(external_descriptor.descriptor_id(), 27)].into()); } #[test] @@ -366,13 +449,14 @@ fn test_non_wildcard_derivations() { let mut txout_index = KeychainTxOutIndex::::new(0); let secp = bitcoin::secp256k1::Secp256k1::signing_only(); - let (no_wildcard_descriptor, _) = Descriptor::::parse_descriptor(&secp, "wpkh([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/1/0)").unwrap(); + let (no_wildcard_descriptor, _) = + Descriptor::::parse_descriptor(&secp, DESCRIPTORS[6]).unwrap(); let external_spk = no_wildcard_descriptor .at_derivation_index(0) .unwrap() .script_pubkey(); - txout_index.add_keychain(TestKeychain::External, no_wildcard_descriptor); + let _ = txout_index.insert_descriptor(no_wildcard_descriptor.clone(), TestKeychain::External); // given: // - `txout_index` with no stored scripts @@ -383,11 +467,14 @@ fn test_non_wildcard_derivations() { assert_eq!(txout_index.next_index(&TestKeychain::External), (0, true)); let (spk, changeset) = txout_index.reveal_next_spk(&TestKeychain::External); assert_eq!(spk, (0, external_spk.as_script())); - assert_eq!(changeset.as_inner(), &[(TestKeychain::External, 0)].into()); + assert_eq!( + &changeset.last_revealed, + &[(no_wildcard_descriptor.descriptor_id(), 0)].into() + ); let (spk, changeset) = txout_index.next_unused_spk(&TestKeychain::External); assert_eq!(spk, (0, external_spk.as_script())); - assert_eq!(changeset.as_inner(), &[].into()); + assert_eq!(&changeset.last_revealed, &[].into()); // given: // - the non-wildcard descriptor already has a stored and used script @@ -400,11 +487,11 @@ fn test_non_wildcard_derivations() { let (spk, changeset) = txout_index.reveal_next_spk(&TestKeychain::External); assert_eq!(spk, (0, external_spk.as_script())); - assert_eq!(changeset.as_inner(), &[].into()); + assert_eq!(&changeset.last_revealed, &[].into()); let (spk, changeset) = txout_index.next_unused_spk(&TestKeychain::External); assert_eq!(spk, (0, external_spk.as_script())); - assert_eq!(changeset.as_inner(), &[].into()); + assert_eq!(&changeset.last_revealed, &[].into()); let (revealed_spks, revealed_changeset) = txout_index.reveal_to_target(&TestKeychain::External, 200); assert_eq!(revealed_spks.count(), 0); diff --git a/example-crates/example_bitcoind_rpc_polling/src/main.rs b/example-crates/example_bitcoind_rpc_polling/src/main.rs index 88b83067b5..c7cc493fb0 100644 --- a/example-crates/example_bitcoind_rpc_polling/src/main.rs +++ b/example-crates/example_bitcoind_rpc_polling/src/main.rs @@ -215,7 +215,7 @@ fn main() -> anyhow::Result<()> { graph.graph().balance( &*chain, synced_to.block_id(), - graph.index.outpoints().iter().cloned(), + graph.index.outpoints(), |(k, _), _| k == &Keychain::Internal, ) }; @@ -343,7 +343,7 @@ fn main() -> anyhow::Result<()> { graph.graph().balance( &*chain, synced_to.block_id(), - graph.index.outpoints().iter().cloned(), + graph.index.outpoints(), |(k, _), _| k == &Keychain::Internal, ) }; diff --git a/example-crates/example_cli/src/lib.rs b/example-crates/example_cli/src/lib.rs index 4989c08c61..8ea2000467 100644 --- a/example-crates/example_cli/src/lib.rs +++ b/example-crates/example_cli/src/lib.rs @@ -247,7 +247,11 @@ where script_pubkey: address.script_pubkey(), }]; - let internal_keychain = if graph.index.keychains().get(&Keychain::Internal).is_some() { + let internal_keychain = if graph + .index + .keychains() + .any(|(k, _)| *k == Keychain::Internal) + { Keychain::Internal } else { Keychain::External @@ -264,8 +268,9 @@ where &graph .index .keychains() - .get(&internal_keychain) + .find(|(k, _)| *k == &internal_keychain) .expect("must exist") + .1 .at_derivation_index(change_index) .expect("change_index can't be hardened"), &assets, @@ -282,8 +287,9 @@ where min_drain_value: graph .index .keychains() - .get(&internal_keychain) + .find(|(k, _)| *k == &internal_keychain) .expect("must exist") + .1 .dust_value(), ..CoinSelectorOpt::fund_outputs( &outputs, @@ -414,7 +420,7 @@ pub fn planned_utxos, ) -> Result>, O::Error> { let chain_tip = chain.get_chain_tip()?; - let outpoints = graph.index.outpoints().iter().cloned(); + let outpoints = graph.index.outpoints(); graph .graph() .try_filter_chain_unspents(chain, chain_tip, outpoints) @@ -426,8 +432,9 @@ pub fn planned_utxos::default(); + // TODO: descriptors are already stored in the db, so we shouldn't re-insert + // them in the index here. However, the keymap is not stored in the database. let (descriptor, mut keymap) = Descriptor::::parse_descriptor(&secp, &args.descriptor)?; - index.add_keychain(Keychain::External, descriptor); + let _ = index.insert_descriptor(descriptor, Keychain::External); if let Some((internal_descriptor, internal_keymap)) = args .change_descriptor @@ -698,7 +707,7 @@ where .transpose()? { keymap.extend(internal_keymap); - index.add_keychain(Keychain::Internal, internal_descriptor); + let _ = index.insert_descriptor(internal_descriptor, Keychain::Internal); } let mut db_backend = match Store::::open_or_create_new(db_magic, &args.db_path) { diff --git a/example-crates/example_electrum/src/main.rs b/example-crates/example_electrum/src/main.rs index df34795bd9..9b6dbebc5d 100644 --- a/example-crates/example_electrum/src/main.rs +++ b/example-crates/example_electrum/src/main.rs @@ -238,7 +238,7 @@ fn main() -> anyhow::Result<()> { let mut outpoints: Box> = Box::new(core::iter::empty()); if utxos { - let init_outpoints = graph.index.outpoints().iter().cloned(); + let init_outpoints = graph.index.outpoints(); let utxos = graph .graph() diff --git a/example-crates/example_esplora/src/main.rs b/example-crates/example_esplora/src/main.rs index e922057066..76057df38c 100644 --- a/example-crates/example_esplora/src/main.rs +++ b/example-crates/example_esplora/src/main.rs @@ -269,7 +269,7 @@ fn main() -> anyhow::Result<()> { // We want to search for whether the UTXO is spent, and spent by which // transaction. We provide the outpoint of the UTXO to // `EsploraExt::update_tx_graph_without_keychain`. - let init_outpoints = graph.index.outpoints().iter().cloned(); + let init_outpoints = graph.index.outpoints(); let utxos = graph .graph() .filter_chain_unspents(&*chain, chain_tip, init_outpoints)