refactor(chain)!: update KeychainTxOutIndex methods to use owned ScriptBuf

This commit is contained in:
Steve Myers
2024-07-09 16:51:12 -05:00
parent 7c07b9de02
commit 79262185d5
9 changed files with 55 additions and 53 deletions

View File

@@ -36,8 +36,8 @@ use bdk_chain::{
};
use bitcoin::sighash::{EcdsaSighashType, TapSighashType};
use bitcoin::{
absolute, psbt, Address, Block, FeeRate, Network, OutPoint, Script, ScriptBuf, Sequence,
Transaction, TxOut, Txid, Witness,
absolute, psbt, Address, Block, FeeRate, Network, OutPoint, ScriptBuf, Sequence, Transaction,
TxOut, Txid, Witness,
};
use bitcoin::{consensus::encode::serialize, transaction, BlockHash, Psbt};
use bitcoin::{constants::genesis_block, Amount};
@@ -363,8 +363,8 @@ impl Wallet {
));
let index = create_indexer(descriptor, change_descriptor, params.lookahead)?;
let descriptor = index.get_descriptor(&KeychainKind::External).cloned();
let change_descriptor = index.get_descriptor(&KeychainKind::Internal).cloned();
let descriptor = index.get_descriptor(KeychainKind::External).cloned();
let change_descriptor = index.get_descriptor(KeychainKind::Internal).cloned();
let indexed_graph = IndexedTxGraph::new(index);
let indexed_graph_changeset = indexed_graph.initial_changeset();
@@ -705,20 +705,21 @@ impl Wallet {
.unused_keychain_spks(keychain)
.map(move |(index, spk)| AddressInfo {
index,
address: Address::from_script(spk, self.network).expect("must have address form"),
address: Address::from_script(spk.as_script(), self.network)
.expect("must have address form"),
keychain,
})
}
/// Return whether or not a `script` is part of this wallet (either internal or external)
pub fn is_mine(&self, script: &Script) -> bool {
pub fn is_mine(&self, script: ScriptBuf) -> bool {
self.indexed_graph.index.index_of_spk(script).is_some()
}
/// Finds how the wallet derived the script pubkey `spk`.
///
/// Will only return `Some(_)` if the wallet has given out the spk.
pub fn derivation_of_spk(&self, spk: &Script) -> Option<(KeychainKind, u32)> {
pub fn derivation_of_spk(&self, spk: ScriptBuf) -> Option<(KeychainKind, u32)> {
self.indexed_graph.index.index_of_spk(spk).cloned()
}
@@ -1060,7 +1061,7 @@ impl Wallet {
let descriptor = self
.indexed_graph
.index
.get_descriptor(&keychain)
.get_descriptor(keychain)
.expect("keychain must exist");
*wallet_signers = SignersContainer::build(keymap, descriptor, &self.secp);
}
@@ -1327,7 +1328,7 @@ impl Wallet {
return Err(CreateTxError::OutputBelowDustLimit(index));
}
if self.is_mine(script_pubkey) {
if self.is_mine(script_pubkey.clone()) {
received += Amount::from_sat(value);
}
@@ -1434,7 +1435,7 @@ impl Wallet {
remaining_amount, ..
} => fee_amount += remaining_amount,
Change { amount, fee } => {
if self.is_mine(&drain_script) {
if self.is_mine(drain_script.clone()) {
received += Amount::from_sat(*amount);
}
fee_amount += fee;
@@ -1555,7 +1556,7 @@ impl Wallet {
.cloned()
.into();
let weighted_utxo = match txout_index.index_of_spk(&txout.script_pubkey) {
let weighted_utxo = match txout_index.index_of_spk(txout.script_pubkey.clone()) {
Some(&(keychain, derivation_index)) => {
let satisfaction_weight = self
.public_descriptor(keychain)
@@ -1600,7 +1601,7 @@ impl Wallet {
let mut change_index = None;
for (index, txout) in tx.output.iter().enumerate() {
let change_keychain = KeychainKind::Internal;
match txout_index.index_of_spk(&txout.script_pubkey) {
match txout_index.index_of_spk(txout.script_pubkey.clone()) {
Some((keychain, _)) if *keychain == change_keychain => {
change_index = Some(index)
}
@@ -1862,7 +1863,7 @@ impl Wallet {
pub fn cancel_tx(&mut self, tx: &Transaction) {
let txout_index = &mut self.indexed_graph.index;
for txout in &tx.output {
if let Some((keychain, index)) = txout_index.index_of_spk(&txout.script_pubkey) {
if let Some((keychain, index)) = txout_index.index_of_spk(txout.script_pubkey.clone()) {
// NOTE: unmark_used will **not** make something unused if it has actually been used
// by a tx in the tracker. It only removes the superficial marking.
txout_index.unmark_used(*keychain, *index);
@@ -1874,7 +1875,7 @@ impl Wallet {
let &(keychain, child) = self
.indexed_graph
.index
.index_of_spk(&txout.script_pubkey)?;
.index_of_spk(txout.script_pubkey.clone())?;
let descriptor = self.public_descriptor(keychain);
descriptor.at_derivation_index(child).ok()
}
@@ -2089,7 +2090,7 @@ impl Wallet {
let &(keychain, child) = self
.indexed_graph
.index
.index_of_spk(&utxo.txout.script_pubkey)
.index_of_spk(utxo.txout.script_pubkey)
.ok_or(CreateTxError::UnknownUtxo)?;
let mut psbt_input = psbt::Input {
@@ -2135,7 +2136,7 @@ impl Wallet {
// Try to figure out the keychain and derivation for every input and output
for (is_input, index, out) in utxos.into_iter() {
if let Some(&(keychain, child)) =
self.indexed_graph.index.index_of_spk(&out.script_pubkey)
self.indexed_graph.index.index_of_spk(out.script_pubkey)
{
let desc = self.public_descriptor(keychain);
let desc = desc