Merge rust-bitcoin/rust-bitcoin#1964: script: Move some inspector methods from ScriptBuf to Script

07041d584d Apply rustfmt (The rustfmt Tyranny)
dada6d65b7 script: Move some inspector methods from ScriptBuf to Script (Steven Roose)

Pull request description:

  Noticed that these methods belong in Script.

ACKs for top commit:
  tcharding:
    ACK 07041d584d
  sanket1729:
    ACK 07041d584d.
  apoelstra:
    ACK 07041d584d

Tree-SHA512: cdcbdf22f0457123205621ec2834164c4598be1e5b221cf859d60e88110b19f8c1e484e86f60653af237e9c2acbcdbe5d2b4c98ccf239924386639c4ba6222f7
This commit is contained in:
Andrew Poelstra 2023-07-31 14:42:32 +00:00
commit cdf3e30b9d
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
3 changed files with 27 additions and 26 deletions

View File

@ -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]: <https://github.com/bitcoin/bips/blob/99701f68a88ce33b2d0838eb84e115cef505b4c2/bip-0143.mediawiki>
pub fn p2wpkh_script_code(&self) -> Option<ScriptBuf> {
self.v0_p2wpkh().map(|wpkh| {
Builder::new()
.push_opcode(OP_DUP)
.push_opcode(OP_HASH160)
// The `self` script is 0x00, 0x14, <pubkey_hash>
.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 {

View File

@ -172,26 +172,6 @@ impl ScriptBuf {
/// This method doesn't (re)allocate.
pub fn into_bytes(self) -> Vec<u8> { 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]: <https://github.com/bitcoin/bips/blob/99701f68a88ce33b2d0838eb84e115cef505b4c2/bip-0143.mediawiki>
pub fn p2wpkh_script_code(&self) -> Option<ScriptBuf> {
self.v0_p2wpkh().map(|wpkh| {
Builder::new()
.push_opcode(OP_DUP)
.push_opcode(OP_HASH160)
// The `self` script is 0x00, 0x14, <pubkey_hash>
.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()); }

View File

@ -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,15 +335,17 @@ 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"),
)
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)?;