Add public functions p2wpkh_script_code

Add two public API functions on the two public keys, both called
`p2wpkh_script_code` to do exactly as the name suggests.

Of note, I was not able to find anywhere to use these in example code,
this is because of we always use the new `p2wpkh_signature_hash`
function. The new functions may be useful for a user calling
`segwit_v0_encode_signing_data_to`. The may help document the library as
well.
This commit is contained in:
Tobin C. Harding 2024-02-05 17:51:37 +11:00
parent a246dc98a4
commit 3c62f74684
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
3 changed files with 30 additions and 9 deletions

View File

@ -19,7 +19,7 @@ use crate::blockdata::script::{
ScriptHash, WScriptHash,
};
use crate::consensus::Encodable;
use crate::key::{PublicKey, UntweakedPublicKey};
use crate::key::{PublicKey, UntweakedPublicKey, WPubkeyHash};
use crate::policy::DUST_RELAY_TX_FEE;
use crate::prelude::*;
use crate::taproot::{LeafVersion, TapLeafHash, TapNodeHash};
@ -385,14 +385,8 @@ impl Script {
/// [BIP143]: <https://github.com/bitcoin/bips/blob/99701f68a88ce33b2d0838eb84e115cef505b4c2/bip-0143.mediawiki>
pub fn p2wpkh_script_code(&self) -> Option<ScriptBuf> {
self.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()
let wpkh = WPubkeyHash::from_slice(wpkh).expect("checked in p2wpkh()");
ScriptBuf::p2wpkh_script_code(wpkh)
})
}

View File

@ -151,6 +151,21 @@ impl ScriptBuf {
Builder::new().push_opcode(version.into()).push_slice(program).into_script()
}
/// 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.
pub fn new_op_return<T: AsRef<PushBytes>>(data: T) -> Self {
Builder::new().push_opcode(OP_RETURN).push_slice(data).into_script()

View File

@ -15,6 +15,7 @@ use internals::array_vec::ArrayVec;
use internals::write_err;
use io::{Read, Write};
use crate::blockdata::script::ScriptBuf;
use crate::crypto::ecdsa;
use crate::internal_macros::impl_asref_push_bytes;
use crate::network::NetworkKind;
@ -71,6 +72,12 @@ impl PublicKey {
}
}
/// Returns the script code used to spend a P2WPKH input.
pub fn p2wpkh_script_code(&self) -> Result<ScriptBuf, UncompressedPubkeyError> {
let key = CompressedPublicKey::try_from(*self)?;
Ok(key.p2wpkh_script_code())
}
/// Write the public key into a writer
pub fn write_into<W: Write + ?Sized>(&self, writer: &mut W) -> Result<(), io::Error> {
self.with_serialized(|bytes| writer.write_all(bytes))
@ -266,6 +273,11 @@ impl CompressedPublicKey {
WPubkeyHash::from_byte_array(hash160::Hash::hash(&self.to_bytes()).to_byte_array())
}
/// Returns the script code used to spend a P2WPKH input.
pub fn p2wpkh_script_code(&self) -> ScriptBuf {
ScriptBuf::p2wpkh_script_code(self.wpubkey_hash())
}
/// Write the public key into a writer
pub fn write_into<W: io::Write + ?Sized>(&self, writer: &mut W) -> Result<(), io::Error> {
writer.write_all(&self.to_bytes())