script: Move some inspector methods from ScriptBuf to Script

This commit is contained in:
Steven Roose 2023-07-27 23:50:16 +01:00
parent 04976eddcf
commit dada6d65b7
No known key found for this signature in database
GPG Key ID: 2F2A88D7F8D68E87
3 changed files with 24 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 /// Returns the minimum value an output with this script should have in order to be
/// broadcastable on today's Bitcoin network. /// broadcastable on today's Bitcoin network.
pub fn dust_value(&self) -> crate::Amount { pub fn dust_value(&self) -> crate::Amount {

View File

@ -172,26 +172,6 @@ impl ScriptBuf {
/// This method doesn't (re)allocate. /// This method doesn't (re)allocate.
pub fn into_bytes(self) -> Vec<u8> { self.0 } 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. /// Adds a single opcode to the script.
pub fn push_opcode(&mut self, data: opcodes::All) { self.0.push(data.to_u8()); } 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 secp256k1::{Message, Secp256k1, Signing};
use crate::bip32::{self, ExtendedPrivKey, ExtendedPubKey, KeySource}; use crate::bip32::{self, ExtendedPrivKey, ExtendedPubKey, KeySource};
use crate::blockdata::script::ScriptBuf;
use crate::blockdata::transaction::{Transaction, TxOut}; use crate::blockdata::transaction::{Transaction, TxOut};
use crate::crypto::ecdsa; use crate::crypto::ecdsa;
use crate::crypto::key::{PrivateKey, PublicKey}; use crate::crypto::key::{PrivateKey, PublicKey};
@ -336,15 +335,14 @@ impl Psbt {
Ok((Message::from(sighash), hash_ty)) Ok((Message::from(sighash), hash_ty))
} }
Wpkh => { 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 = let sighash =
cache.segwit_signature_hash(input_index, &script_code, utxo.value, hash_ty)?; cache.segwit_signature_hash(input_index, &script_code, utxo.value, hash_ty)?;
Ok((Message::from(sighash), hash_ty)) Ok((Message::from(sighash), hash_ty))
} }
ShWpkh => { ShWpkh => {
let script_code = ScriptBuf::p2wpkh_script_code( let script_code = input.redeem_script.as_ref().expect("checked above")
input.redeem_script.as_ref().expect("checked above"), .p2wpkh_script_code()
)
.ok_or(SignError::NotWpkh)?; .ok_or(SignError::NotWpkh)?;
let sighash = let sighash =
cache.segwit_signature_hash(input_index, &script_code, utxo.value, hash_ty)?; cache.segwit_signature_hash(input_index, &script_code, utxo.value, hash_ty)?;