Merge rust-bitcoin/rust-bitcoin#3098: Make `ScriptBuf::p2wpkh_script_code` stand alone

feef34fdea Make ScriptBuf::p2wpkh_script_code stand alone (Tobin C. Harding)

Pull request description:

  We would like to move the `Script` type to `primitives` without moving any key stuff, including pubkey hashes. We may later, before releasing `primitives`, move the `WPubkeyHash` at which time this patch can be reverted or re-implemented on `ScriptBuf`.

  The `ScriptBuf::p2wpkh_script_code` function does not take `self` as a parameter but it does return `Self` - this can trivially be made into a standalone function.

  Make `ScriptBuf::p2wpkh_script_code` a standalone function.

ACKs for top commit:
  Kixunil:
    ACK feef34fdea
  apoelstra:
    ACK feef34fdea

Tree-SHA512: 9de43bb274480b85d328ddd4cb6d79b501a525837307ca399dea1c3d39e9a7c0d8719dbb0e5d852629351d2b1fe09ce0244df82c53ff9ef2342be85a71887329
This commit is contained in:
merge-script 2024-07-27 21:08:34 +00:00
commit 5f79ff0966
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
4 changed files with 20 additions and 20 deletions

View File

@ -13,7 +13,7 @@ use crate::opcodes::all::*;
use crate::script::witness_program::WitnessProgram; use crate::script::witness_program::WitnessProgram;
use crate::script::witness_version::WitnessVersion; use crate::script::witness_version::WitnessVersion;
use crate::script::{ use crate::script::{
Builder, PushBytes, RedeemScriptSizeError, Script, ScriptBuf, ScriptHash, WScriptHash, self, Builder, PushBytes, RedeemScriptSizeError, Script, ScriptBuf, ScriptHash, WScriptHash,
WitnessScriptSizeError, WitnessScriptSizeError,
}; };
use crate::taproot::TapNodeHash; use crate::taproot::TapNodeHash;
@ -72,7 +72,7 @@ define_extension_trait! {
// The `self` script is 0x00, 0x14, <pubkey_hash> // The `self` script is 0x00, 0x14, <pubkey_hash>
let bytes = &self.as_bytes()[2..]; let bytes = &self.as_bytes()[2..];
let wpkh = WPubkeyHash::from_slice(bytes).expect("length checked in is_p2wpkh()"); let wpkh = WPubkeyHash::from_slice(bytes).expect("length checked in is_p2wpkh()");
Some(ScriptBuf::p2wpkh_script_code(wpkh)) Some(script::p2wpkh_script_code(wpkh))
} else { } else {
None None
} }

View File

@ -70,6 +70,7 @@ use io::{BufRead, Write};
use crate::consensus::{encode, Decodable, Encodable}; use crate::consensus::{encode, Decodable, Encodable};
use crate::constants::{MAX_REDEEM_SCRIPT_SIZE, MAX_WITNESS_SCRIPT_SIZE}; use crate::constants::{MAX_REDEEM_SCRIPT_SIZE, MAX_WITNESS_SCRIPT_SIZE};
use crate::internal_macros::impl_asref_push_bytes; use crate::internal_macros::impl_asref_push_bytes;
use crate::key::WPubkeyHash;
use crate::opcodes::all::*; use crate::opcodes::all::*;
use crate::opcodes::{self, Opcode}; use crate::opcodes::{self, Opcode};
use crate::prelude::{Borrow, BorrowMut, Box, Cow, DisplayHex, ToOwned, Vec}; use crate::prelude::{Borrow, BorrowMut, Box, Cow, DisplayHex, ToOwned, Vec};
@ -199,6 +200,21 @@ impl TryFrom<&Script> for WScriptHash {
} }
} }
/// Creates the script code used for spending 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(wpkh: WPubkeyHash) -> ScriptBuf {
Builder::new()
.push_opcode(OP_DUP)
.push_opcode(OP_HASH160)
.push_slice(wpkh)
.push_opcode(OP_EQUALVERIFY)
.push_opcode(OP_CHECKSIG)
.into_script()
}
/// Encodes an integer in script(minimal CScriptNum) format. /// Encodes an integer in script(minimal CScriptNum) format.
/// ///
/// Writes bytes into the buffer and returns the number of bytes written. /// Writes bytes into the buffer and returns the number of bytes written.

View File

@ -6,7 +6,6 @@ use core::ops::Deref;
use hex::FromHex; use hex::FromHex;
use super::{opcode_to_verify, Builder, Instruction, PushBytes, Script}; use super::{opcode_to_verify, Builder, Instruction, PushBytes, Script};
use crate::key::WPubkeyHash;
use crate::opcodes::all::*; use crate::opcodes::all::*;
use crate::opcodes::{self, Opcode}; use crate::opcodes::{self, Opcode};
use crate::prelude::{Box, Vec}; use crate::prelude::{Box, Vec};
@ -67,21 +66,6 @@ impl ScriptBuf {
/// Creates a new script builder /// Creates a new script builder
pub fn builder() -> Builder { Builder::new() } pub fn builder() -> Builder { Builder::new() }
/// Creates the script code used for spending 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(wpkh: WPubkeyHash) -> ScriptBuf {
Builder::new()
.push_opcode(OP_DUP)
.push_opcode(OP_HASH160)
.push_slice(wpkh)
.push_opcode(OP_EQUALVERIFY)
.push_opcode(OP_CHECKSIG)
.into_script()
}
/// Generates OP_RETURN-type of scriptPubkey for the given data. /// Generates OP_RETURN-type of scriptPubkey for the given data.
pub fn new_op_return<T: AsRef<PushBytes>>(data: T) -> Self { pub fn new_op_return<T: AsRef<PushBytes>>(data: T) -> Self {
Builder::new().push_opcode(OP_RETURN).push_slice(data).into_script() Builder::new().push_opcode(OP_RETURN).push_slice(data).into_script()

View File

@ -19,7 +19,7 @@ use crate::crypto::ecdsa;
use crate::internal_macros::impl_asref_push_bytes; use crate::internal_macros::impl_asref_push_bytes;
use crate::network::NetworkKind; use crate::network::NetworkKind;
use crate::prelude::{DisplayHex, String, Vec}; use crate::prelude::{DisplayHex, String, Vec};
use crate::script::ScriptBuf; use crate::script::{self, ScriptBuf};
use crate::taproot::{TapNodeHash, TapTweakHash}; use crate::taproot::{TapNodeHash, TapTweakHash};
#[rustfmt::skip] // Keep public re-exports separate. #[rustfmt::skip] // Keep public re-exports separate.
@ -286,7 +286,7 @@ impl CompressedPublicKey {
/// Returns the script code used to spend a P2WPKH input. /// Returns the script code used to spend a P2WPKH input.
pub fn p2wpkh_script_code(&self) -> ScriptBuf { pub fn p2wpkh_script_code(&self) -> ScriptBuf {
ScriptBuf::p2wpkh_script_code(self.wpubkey_hash()) script::p2wpkh_script_code(self.wpubkey_hash())
} }
/// Writes the public key into a writer. /// Writes the public key into a writer.