Generalize fn params in `define_extension_trait`

The macro was trying to "parse" the parameters of functions defined in
extension trait. This was not needed and it was causing problems around
the `self` parameter. In this commit we change the macro to just pass
the parameters through.
This commit is contained in:
Martin Habovstiak 2024-08-11 17:49:33 +02:00
parent fcc3cb03f0
commit 27adc09e9f
3 changed files with 14 additions and 14 deletions

View File

@ -22,7 +22,7 @@ define_extension_trait! {
/// Extension functionality to add scriptPubkey support to the [`Builder`] type. /// Extension functionality to add scriptPubkey support to the [`Builder`] type.
pub trait BuilderExt impl for Builder { pub trait BuilderExt impl for Builder {
/// Adds instructions to push a public key onto the stack. /// Adds instructions to push a public key onto the stack.
fn push_key(self: Self, key: PublicKey) -> Builder { fn push_key(self, key: PublicKey) -> Builder {
if key.compressed { if key.compressed {
self.push_slice(key.inner.serialize()) self.push_slice(key.inner.serialize())
} else { } else {
@ -31,7 +31,7 @@ define_extension_trait! {
} }
/// Adds instructions to push an XOnly public key onto the stack. /// Adds instructions to push an XOnly public key onto the stack.
fn push_x_only_key(self: Self, x_only_key: XOnlyPublicKey) -> Builder { fn push_x_only_key(self, x_only_key: XOnlyPublicKey) -> Builder {
self.push_slice(x_only_key.serialize()) self.push_slice(x_only_key.serialize())
} }
} }
@ -42,14 +42,14 @@ define_extension_trait! {
pub trait ScriptExt impl for Script { pub trait ScriptExt impl for Script {
/// Computes the P2WSH output corresponding to this witnessScript (aka the "witness redeem /// Computes the P2WSH output corresponding to this witnessScript (aka the "witness redeem
/// script"). /// script").
fn to_p2wsh(self: &Self) -> Result<ScriptBuf, WitnessScriptSizeError> { fn to_p2wsh(&self) -> Result<ScriptBuf, WitnessScriptSizeError> {
self.wscript_hash().map(ScriptBuf::new_p2wsh) self.wscript_hash().map(ScriptBuf::new_p2wsh)
} }
/// Computes P2TR output with a given internal key and a single script spending path equal to /// Computes P2TR output with a given internal key and a single script spending path equal to
/// the current script, assuming that the script is a Tapscript. /// the current script, assuming that the script is a Tapscript.
fn to_p2tr<C: Verification>( fn to_p2tr<C: Verification>(
self: &Self, &self,
secp: &Secp256k1<C>, secp: &Secp256k1<C>,
internal_key: UntweakedPublicKey, internal_key: UntweakedPublicKey,
) -> ScriptBuf { ) -> ScriptBuf {
@ -59,7 +59,7 @@ define_extension_trait! {
} }
/// Computes the P2SH output corresponding to this redeem script. /// Computes the P2SH output corresponding to this redeem script.
fn to_p2sh(self: &Self) -> Result<ScriptBuf, RedeemScriptSizeError> { fn to_p2sh(&self) -> Result<ScriptBuf, RedeemScriptSizeError> {
self.script_hash().map(ScriptBuf::new_p2sh) self.script_hash().map(ScriptBuf::new_p2sh)
} }
@ -67,7 +67,7 @@ define_extension_trait! {
/// for a P2WPKH output. The `scriptCode` is described in [BIP143]. /// for a P2WPKH output. The `scriptCode` is described in [BIP143].
/// ///
/// [BIP143]: <https://github.com/bitcoin/bips/blob/99701f68a88ce33b2d0838eb84e115cef505b4c2/bip-0143.mediawiki> /// [BIP143]: <https://github.com/bitcoin/bips/blob/99701f68a88ce33b2d0838eb84e115cef505b4c2/bip-0143.mediawiki>
fn p2wpkh_script_code(self: &Self) -> Option<ScriptBuf> { fn p2wpkh_script_code(&self) -> Option<ScriptBuf> {
if self.is_p2wpkh() { if self.is_p2wpkh() {
// 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..];
@ -82,14 +82,14 @@ define_extension_trait! {
/// ///
/// You can obtain the public key, if its valid, /// You can obtain the public key, if its valid,
/// by calling [`p2pk_public_key()`](Self::p2pk_public_key) /// by calling [`p2pk_public_key()`](Self::p2pk_public_key)
fn is_p2pk(self: &Self) -> bool { self.p2pk_pubkey_bytes().is_some() } fn is_p2pk(&self) -> bool { self.p2pk_pubkey_bytes().is_some() }
/// Returns the public key if this script is P2PK with a **valid** public key. /// Returns the public key if this script is P2PK with a **valid** public key.
/// ///
/// This may return `None` even when [`is_p2pk()`](Self::is_p2pk) returns true. /// This may return `None` even when [`is_p2pk()`](Self::is_p2pk) returns true.
/// This happens when the public key is invalid (e.g. the point not being on the curve). /// This happens when the public key is invalid (e.g. the point not being on the curve).
/// In this situation the script is unspendable. /// In this situation the script is unspendable.
fn p2pk_public_key(self: &Self) -> Option<PublicKey> { fn p2pk_public_key(&self) -> Option<PublicKey> {
PublicKey::from_slice(self.p2pk_pubkey_bytes()?).ok() PublicKey::from_slice(self.p2pk_pubkey_bytes()?).ok()
} }
} }
@ -98,7 +98,7 @@ define_extension_trait! {
define_extension_trait! { define_extension_trait! {
pub(crate) trait ScriptExtPrivate impl for Script { pub(crate) trait ScriptExtPrivate impl for Script {
/// Returns the bytes of the (possibly invalid) public key if this script is P2PK. /// Returns the bytes of the (possibly invalid) public key if this script is P2PK.
fn p2pk_pubkey_bytes(self: &Self) -> Option<&[u8]> { fn p2pk_pubkey_bytes(&self) -> Option<&[u8]> {
match self.len() { match self.len() {
67 if self.as_bytes()[0] == OP_PUSHBYTES_65.to_u8() 67 if self.as_bytes()[0] == OP_PUSHBYTES_65.to_u8()
&& self.as_bytes()[66] == OP_CHECKSIG.to_u8() => && self.as_bytes()[66] == OP_CHECKSIG.to_u8() =>

View File

@ -131,7 +131,7 @@ define_extension_trait! {
/// ///
/// [`bitcoinconsensus::VERIFY_ALL_PRE_TAPROOT`]: https://docs.rs/bitcoinconsensus/0.106.0+26.0/bitcoinconsensus/constant.VERIFY_ALL_PRE_TAPROOT.html /// [`bitcoinconsensus::VERIFY_ALL_PRE_TAPROOT`]: https://docs.rs/bitcoinconsensus/0.106.0+26.0/bitcoinconsensus/constant.VERIFY_ALL_PRE_TAPROOT.html
fn verify( fn verify(
self: &Self, &self,
index: usize, index: usize,
amount: crate::Amount, amount: crate::Amount,
spending_tx: &[u8], spending_tx: &[u8],
@ -150,7 +150,7 @@ define_extension_trait! {
/// ///
/// [`bitcoinconsensus::VERIFY_ALL_PRE_TAPROOT`]: https://docs.rs/bitcoinconsensus/0.106.0+26.0/bitcoinconsensus/constant.VERIFY_ALL_PRE_TAPROOT.html /// [`bitcoinconsensus::VERIFY_ALL_PRE_TAPROOT`]: https://docs.rs/bitcoinconsensus/0.106.0+26.0/bitcoinconsensus/constant.VERIFY_ALL_PRE_TAPROOT.html
fn verify_with_flags( fn verify_with_flags(
self: &Self, &self,
index: usize, index: usize,
amount: crate::Amount, amount: crate::Amount,
spending_tx: &[u8], spending_tx: &[u8],

View File

@ -251,7 +251,7 @@ macro_rules! define_extension_trait {
($(#[$($trait_attrs:tt)*])* $trait_vis:vis trait $trait_name:ident impl for $ty:ident { ($(#[$($trait_attrs:tt)*])* $trait_vis:vis trait $trait_name:ident impl for $ty:ident {
$( $(
$(#[$($fn_attrs:tt)*])* $(#[$($fn_attrs:tt)*])*
fn $fn:ident$(<$($gen:ident: $gent:ident),*>)?($($param_name:ident: $param_type:ty),* $(,)?) $( -> $ret:ty )? $body:block fn $fn:ident$(<$($gen:ident: $gent:ident),*>)?($($params:tt)*) $( -> $ret:ty )? $body:block
)* )*
}) => { }) => {
$(#[$($trait_attrs)*])* $trait_vis trait $trait_name { $(#[$($trait_attrs)*])* $trait_vis trait $trait_name {
@ -259,7 +259,7 @@ macro_rules! define_extension_trait {
$crate::internal_macros::only_doc_attrs! { $crate::internal_macros::only_doc_attrs! {
{ $(#[$($fn_attrs)*])* }, { $(#[$($fn_attrs)*])* },
{ {
fn $fn$(<$($gen: $gent),*>)?($($param_name: $param_type),*) $( -> $ret )?; fn $fn$(<$($gen: $gent),*>)?($($params)*) $( -> $ret )?;
} }
} }
)* )*
@ -270,7 +270,7 @@ macro_rules! define_extension_trait {
$crate::internal_macros::only_non_doc_attrs! { $crate::internal_macros::only_non_doc_attrs! {
{ $(#[$($fn_attrs)*])* }, { $(#[$($fn_attrs)*])* },
{ {
fn $fn$(<$($gen: $gent),*>)?($($param_name: $param_type),*) $( -> $ret )? $body fn $fn$(<$($gen: $gent),*>)?($($params)*) $( -> $ret )? $body
} }
} }
)* )*