diff --git a/bitcoin/src/blockdata/script/borrowed.rs b/bitcoin/src/blockdata/script/borrowed.rs index 9084b2b9..9fd12b27 100644 --- a/bitcoin/src/blockdata/script/borrowed.rs +++ b/bitcoin/src/blockdata/script/borrowed.rs @@ -295,6 +295,26 @@ impl Script { } } + /// Computes the P2SH output corresponding to this redeem script. + pub fn to_p2sh(&self) -> ScriptBuf { ScriptBuf::new_p2sh(&self.script_hash()) } + + /// Returns the script code used for spending a P2WPKH output if this script is a script pubkey + /// for a P2WPKH output. The `scriptCode` is described in [BIP143]. + /// + /// [BIP143]: + pub fn p2wpkh_script_code(&self) -> Option { + self.v0_p2wpkh().map(|wpkh| { + Builder::new() + .push_opcode(OP_DUP) + .push_opcode(OP_HASH160) + // The `self` script is 0x00, 0x14, + .push_slice(wpkh) + .push_opcode(OP_EQUALVERIFY) + .push_opcode(OP_CHECKSIG) + .into_script() + }) + } + /// Returns the minimum value an output with this script should have in order to be /// broadcastable on today's Bitcoin network. pub fn dust_value(&self) -> crate::Amount { diff --git a/bitcoin/src/blockdata/script/owned.rs b/bitcoin/src/blockdata/script/owned.rs index db917225..24f8f83f 100644 --- a/bitcoin/src/blockdata/script/owned.rs +++ b/bitcoin/src/blockdata/script/owned.rs @@ -172,26 +172,6 @@ impl ScriptBuf { /// This method doesn't (re)allocate. pub fn into_bytes(self) -> Vec { self.0 } - /// Computes the P2SH output corresponding to this redeem script. - pub fn to_p2sh(&self) -> ScriptBuf { ScriptBuf::new_p2sh(&self.script_hash()) } - - /// Returns the script code used for spending a P2WPKH output if this script is a script pubkey - /// for a P2WPKH output. The `scriptCode` is described in [BIP143]. - /// - /// [BIP143]: - pub fn p2wpkh_script_code(&self) -> Option { - self.v0_p2wpkh().map(|wpkh| { - Builder::new() - .push_opcode(OP_DUP) - .push_opcode(OP_HASH160) - // The `self` script is 0x00, 0x14, - .push_slice(wpkh) - .push_opcode(OP_EQUALVERIFY) - .push_opcode(OP_CHECKSIG) - .into_script() - }) - } - /// Adds a single opcode to the script. pub fn push_opcode(&mut self, data: opcodes::All) { self.0.push(data.to_u8()); } diff --git a/bitcoin/src/psbt/mod.rs b/bitcoin/src/psbt/mod.rs index 824b8a34..01510728 100644 --- a/bitcoin/src/psbt/mod.rs +++ b/bitcoin/src/psbt/mod.rs @@ -15,7 +15,6 @@ use internals::write_err; use secp256k1::{Message, Secp256k1, Signing}; use crate::bip32::{self, ExtendedPrivKey, ExtendedPubKey, KeySource}; -use crate::blockdata::script::ScriptBuf; use crate::blockdata::transaction::{Transaction, TxOut}; use crate::crypto::ecdsa; use crate::crypto::key::{PrivateKey, PublicKey}; @@ -336,16 +335,15 @@ impl Psbt { Ok((Message::from(sighash), hash_ty)) } Wpkh => { - let script_code = ScriptBuf::p2wpkh_script_code(spk).ok_or(SignError::NotWpkh)?; + let script_code = spk.p2wpkh_script_code().ok_or(SignError::NotWpkh)?; let sighash = cache.segwit_signature_hash(input_index, &script_code, utxo.value, hash_ty)?; Ok((Message::from(sighash), hash_ty)) } ShWpkh => { - let script_code = ScriptBuf::p2wpkh_script_code( - input.redeem_script.as_ref().expect("checked above"), - ) - .ok_or(SignError::NotWpkh)?; + let script_code = input.redeem_script.as_ref().expect("checked above") + .p2wpkh_script_code() + .ok_or(SignError::NotWpkh)?; let sighash = cache.segwit_signature_hash(input_index, &script_code, utxo.value, hash_ty)?; Ok((Message::from(sighash), hash_ty))