From dada6d65b78603d3fc492078b161721a91c9b933 Mon Sep 17 00:00:00 2001 From: Steven Roose Date: Thu, 27 Jul 2023 23:50:16 +0100 Subject: [PATCH 1/2] script: Move some inspector methods from ScriptBuf to Script --- bitcoin/src/blockdata/script/borrowed.rs | 20 ++++++++++++++++++++ bitcoin/src/blockdata/script/owned.rs | 20 -------------------- bitcoin/src/psbt/mod.rs | 10 ++++------ 3 files changed, 24 insertions(+), 26 deletions(-) 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)) From 07041d584d9d1937ab9a82579f7119f356c53644 Mon Sep 17 00:00:00 2001 From: The rustfmt Tyranny Date: Sat, 29 Jul 2023 20:52:16 +0100 Subject: [PATCH 2/2] Apply rustfmt --- bitcoin/src/psbt/mod.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/bitcoin/src/psbt/mod.rs b/bitcoin/src/psbt/mod.rs index 01510728..2e5b27d1 100644 --- a/bitcoin/src/psbt/mod.rs +++ b/bitcoin/src/psbt/mod.rs @@ -341,7 +341,10 @@ impl Psbt { Ok((Message::from(sighash), hash_ty)) } ShWpkh => { - let script_code = input.redeem_script.as_ref().expect("checked above") + let script_code = input + .redeem_script + .as_ref() + .expect("checked above") .p2wpkh_script_code() .ok_or(SignError::NotWpkh)?; let sighash =