From 1329f6c08fdb8f97a509895ae905175dd698bd5c Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Sun, 1 Jan 2023 10:27:46 +1100 Subject: [PATCH] Use p2wpkh_script_code from rust-bitcoin Currently we have a function to construct the script code for a P2WPKH, this code is now upstream in `rust-bitcoin` so we can use it. Fix: #364 --- src/psbt/mod.rs | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/src/psbt/mod.rs b/src/psbt/mod.rs index a7ad89576..48e4aace4 100644 --- a/src/psbt/mod.rs +++ b/src/psbt/mod.rs @@ -893,14 +893,14 @@ impl PsbtExt for Psbt { .unwrap_or(false); if inp_spk.is_v0_p2wpkh() || inp_spk.is_v0_p2wsh() || is_nested_wpkh || is_nested_wsh { let msg = if inp_spk.is_v0_p2wpkh() { - let script_code = script_code_wpkh(inp_spk); + let script_code = inp_spk.p2wpkh_script_code().expect("checked is p2wpkh above"); cache.segwit_signature_hash(idx, &script_code, amt, hash_ty)? } else if is_nested_wpkh { - let script_code = script_code_wpkh( - inp.redeem_script - .as_ref() - .expect("Redeem script non-empty checked earlier"), - ); + let script_code = inp.redeem_script + .as_ref() + .expect("Redeem script non-empty checked earlier") + .p2wpkh_script_code() + .expect("checked is p2wpkh above"); cache.segwit_signature_hash(idx, &script_code, amt, hash_ty)? } else { // wsh and nested wsh, script code is witness script @@ -1254,17 +1254,6 @@ fn update_item_with_descriptor_helper( Ok((derived, true)) } -// Get a script from witness script pubkey hash -fn script_code_wpkh(script: &Script) -> Script { - assert!(script.is_v0_p2wpkh()); - // ugly segwit stuff - let mut script_code = vec![0x76u8, 0xa9, 0x14]; - script_code.extend(&script.as_bytes()[2..]); - script_code.push(0x88); - script_code.push(0xac); - Script::from(script_code) -} - /// Return error type for [`PsbtExt::update_input_with_descriptor`] #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)] pub enum UtxoUpdateError {