From 3b60ad55678518f80528b9bd313cccba43e74982 Mon Sep 17 00:00:00 2001 From: "S. Santos" Date: Mon, 18 Sep 2023 23:44:49 -0300 Subject: [PATCH] example: Modify `taproot-psbt.rs` to make the use of prevouts clearer. --- bitcoin/examples/taproot-psbt.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/bitcoin/examples/taproot-psbt.rs b/bitcoin/examples/taproot-psbt.rs index 4d8e00e8..ddc6d7e3 100644 --- a/bitcoin/examples/taproot-psbt.rs +++ b/bitcoin/examples/taproot-psbt.rs @@ -218,6 +218,7 @@ struct P2trUtxo<'a> { derivation_path: &'a str, } +#[allow(clippy::single_element_loop)] fn generate_bip86_key_spend_tx( secp: &secp256k1::Secp256k1, master_xpriv: Xpriv, @@ -267,6 +268,16 @@ fn generate_bip86_key_spend_tx( input.tap_internal_key = Some(input_pubkey); psbt.inputs = vec![input]; + // The `Prevouts::All` array is used to create the sighash to sign for each input in the + // `psbt.inputs` array, as such it must be the same length and in the same order as the inputs. + let mut input_txouts = Vec::::new(); + for input in [&input_utxo].iter() { + input_txouts.push(TxOut { + value: input.amount_in_sats, + script_pubkey: ScriptBuf::from_hex(input.script_pubkey)?, + }); + } + // SIGNER let unsigned_tx = psbt.unsigned_tx.clone(); psbt.inputs.iter_mut().enumerate().try_for_each::<_, Result<(), Box>>( @@ -277,10 +288,7 @@ fn generate_bip86_key_spend_tx( .unwrap_or(TapSighashType::All); let hash = SighashCache::new(&unsigned_tx).taproot_key_spend_signature_hash( vout, - &sighash::Prevouts::All(&[TxOut { - value: from_amount, - script_pubkey: ScriptBuf::from_hex(input_utxo.script_pubkey)?, - }]), + &sighash::Prevouts::All(input_txouts.as_slice()), hash_ty, )?;