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:
parent
a246dc98a4
commit
3c62f74684
|
@ -19,7 +19,7 @@ use crate::blockdata::script::{
|
||||||
ScriptHash, WScriptHash,
|
ScriptHash, WScriptHash,
|
||||||
};
|
};
|
||||||
use crate::consensus::Encodable;
|
use crate::consensus::Encodable;
|
||||||
use crate::key::{PublicKey, UntweakedPublicKey};
|
use crate::key::{PublicKey, UntweakedPublicKey, WPubkeyHash};
|
||||||
use crate::policy::DUST_RELAY_TX_FEE;
|
use crate::policy::DUST_RELAY_TX_FEE;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::taproot::{LeafVersion, TapLeafHash, TapNodeHash};
|
use crate::taproot::{LeafVersion, TapLeafHash, TapNodeHash};
|
||||||
|
@ -385,14 +385,8 @@ impl Script {
|
||||||
/// [BIP143]: <https://github.com/bitcoin/bips/blob/99701f68a88ce33b2d0838eb84e115cef505b4c2/bip-0143.mediawiki>
|
/// [BIP143]: <https://github.com/bitcoin/bips/blob/99701f68a88ce33b2d0838eb84e115cef505b4c2/bip-0143.mediawiki>
|
||||||
pub fn p2wpkh_script_code(&self) -> Option<ScriptBuf> {
|
pub fn p2wpkh_script_code(&self) -> Option<ScriptBuf> {
|
||||||
self.p2wpkh().map(|wpkh| {
|
self.p2wpkh().map(|wpkh| {
|
||||||
Builder::new()
|
let wpkh = WPubkeyHash::from_slice(wpkh).expect("checked in p2wpkh()");
|
||||||
.push_opcode(OP_DUP)
|
ScriptBuf::p2wpkh_script_code(wpkh)
|
||||||
.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()
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -151,6 +151,21 @@ impl ScriptBuf {
|
||||||
Builder::new().push_opcode(version.into()).push_slice(program).into_script()
|
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.
|
/// 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()
|
||||||
|
|
|
@ -15,6 +15,7 @@ use internals::array_vec::ArrayVec;
|
||||||
use internals::write_err;
|
use internals::write_err;
|
||||||
use io::{Read, Write};
|
use io::{Read, Write};
|
||||||
|
|
||||||
|
use crate::blockdata::script::ScriptBuf;
|
||||||
use crate::crypto::ecdsa;
|
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;
|
||||||
|
@ -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
|
/// Write the public key into a writer
|
||||||
pub fn write_into<W: Write + ?Sized>(&self, writer: &mut W) -> Result<(), io::Error> {
|
pub fn write_into<W: Write + ?Sized>(&self, writer: &mut W) -> Result<(), io::Error> {
|
||||||
self.with_serialized(|bytes| writer.write_all(bytes))
|
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())
|
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
|
/// Write the public key into a writer
|
||||||
pub fn write_into<W: io::Write + ?Sized>(&self, writer: &mut W) -> Result<(), io::Error> {
|
pub fn write_into<W: io::Write + ?Sized>(&self, writer: &mut W) -> Result<(), io::Error> {
|
||||||
writer.write_all(&self.to_bytes())
|
writer.write_all(&self.to_bytes())
|
||||||
|
|
Loading…
Reference in New Issue