Skip to content

Commit 3119ade

Browse files
committed
Merge rust-bitcoin#3155: Extension trait for Script
0857697 Replace impl blocks with extension traits (Martin Habovstiak) b99bdcf Format `Script` blocks (Martin Habovstiak) b027edf Wrap `Script` impl blocks in temporary modules (Martin Habovstiak) 5a46154 Separate private `Script` methods (Martin Habovstiak) 27adc09 Generalize fn params in `define_extension_trait` (Martin Habovstiak) fcc3cb0 Support non-doc attrs in extension trait macro (Martin Habovstiak) ca1735f Separate POD methods (Tobin C. Harding) Pull request description: This moves methods from `Script` to extension traits in steps that should be easy to follow. Moving to `primitives` requires doing the same with `ScriptBuf` so I'm holding off until this approach gets concept ACK (or alternatively someone else can do it :)) Closes rust-bitcoin#3161 ACKs for top commit: tcharding: ACK 0857697 apoelstra: ACK 0857697 successfully ran local tests Tree-SHA512: 3768d879e36139cf971c1921d3236141cbe87d707fd4bab7852f6ed8857b7867fa4146dfe720bd54e3d8cc50ecdc93886a10254cf9a82246358253f0312ffb47
2 parents 49e420a + 0857697 commit 3119ade

File tree

18 files changed

+485
-423
lines changed

18 files changed

+485
-423
lines changed

bitcoin/examples/sighash.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use bitcoin::address::script_pubkey::ScriptBufExt as _;
2+
use bitcoin::script::ScriptExt as _;
23
use bitcoin::{
34
consensus, ecdsa, sighash, Amount, CompressedPublicKey, Script, ScriptBuf, Transaction,
45
};

bitcoin/examples/taproot-psbt.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ use bitcoin::consensus::encode;
8484
use bitcoin::key::{TapTweak, XOnlyPublicKey};
8585
use bitcoin::opcodes::all::{OP_CHECKSIG, OP_CLTV, OP_DROP};
8686
use bitcoin::psbt::{self, Input, Output, Psbt, PsbtSighashType};
87+
use bitcoin::script::ScriptExt as _;
8788
use bitcoin::secp256k1::Secp256k1;
8889
use bitcoin::sighash::{self, SighashCache, TapSighash, TapSighashType};
8990
use bitcoin::taproot::{self, LeafVersion, TapLeafHash, TaprootBuilder, TaprootSpendInfo};

bitcoin/src/address/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ use crate::prelude::{String, ToOwned};
5151
use crate::script::witness_program::WitnessProgram;
5252
use crate::script::witness_version::WitnessVersion;
5353
use crate::script::{
54-
self, RedeemScriptSizeError, Script, ScriptBuf, ScriptHash, WScriptHash, WitnessScriptSizeError,
54+
self, RedeemScriptSizeError, Script, ScriptBuf, ScriptExt as _, ScriptHash, WScriptHash,
55+
WitnessScriptSizeError,
5556
};
5657
use crate::taproot::TapNodeHash;
5758

bitcoin/src/address/script_pubkey.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ use crate::opcodes::all::*;
1313
use crate::script::witness_program::WitnessProgram;
1414
use crate::script::witness_version::WitnessVersion;
1515
use crate::script::{
16-
self, Builder, PushBytes, RedeemScriptSizeError, Script, ScriptBuf, ScriptHash, WScriptHash,
17-
WitnessScriptSizeError,
16+
self, Builder, PushBytes, RedeemScriptSizeError, Script, ScriptBuf, ScriptExt as _, ScriptHash,
17+
WScriptHash, WitnessScriptSizeError,
1818
};
1919
use crate::taproot::TapNodeHash;
2020

2121
define_extension_trait! {
2222
/// Extension functionality to add scriptPubkey support to the [`Builder`] type.
2323
pub trait BuilderExt impl for Builder {
2424
/// Adds instructions to push a public key onto the stack.
25-
fn push_key(self: Self, key: PublicKey) -> Builder {
25+
fn push_key(self, key: PublicKey) -> Builder {
2626
if key.compressed {
2727
self.push_slice(key.inner.serialize())
2828
} else {
@@ -31,7 +31,7 @@ define_extension_trait! {
3131
}
3232

3333
/// Adds instructions to push an XOnly public key onto the stack.
34-
fn push_x_only_key(self: Self, x_only_key: XOnlyPublicKey) -> Builder {
34+
fn push_x_only_key(self, x_only_key: XOnlyPublicKey) -> Builder {
3535
self.push_slice(x_only_key.serialize())
3636
}
3737
}
@@ -42,14 +42,14 @@ define_extension_trait! {
4242
pub trait ScriptExt impl for Script {
4343
/// Computes the P2WSH output corresponding to this witnessScript (aka the "witness redeem
4444
/// script").
45-
fn to_p2wsh(self: &Self) -> Result<ScriptBuf, WitnessScriptSizeError> {
45+
fn to_p2wsh(&self) -> Result<ScriptBuf, WitnessScriptSizeError> {
4646
self.wscript_hash().map(ScriptBuf::new_p2wsh)
4747
}
4848

4949
/// Computes P2TR output with a given internal key and a single script spending path equal to
5050
/// the current script, assuming that the script is a Tapscript.
5151
fn to_p2tr<C: Verification>(
52-
self: &Self,
52+
&self,
5353
secp: &Secp256k1<C>,
5454
internal_key: UntweakedPublicKey,
5555
) -> ScriptBuf {
@@ -59,15 +59,15 @@ define_extension_trait! {
5959
}
6060

6161
/// Computes the P2SH output corresponding to this redeem script.
62-
fn to_p2sh(self: &Self) -> Result<ScriptBuf, RedeemScriptSizeError> {
62+
fn to_p2sh(&self) -> Result<ScriptBuf, RedeemScriptSizeError> {
6363
self.script_hash().map(ScriptBuf::new_p2sh)
6464
}
6565

6666
/// Returns the script code used for spending a P2WPKH output if this script is a script pubkey
6767
/// for a P2WPKH output. The `scriptCode` is described in [BIP143].
6868
///
6969
/// [BIP143]: <https://github.com/bitcoin/bips/blob/99701f68a88ce33b2d0838eb84e115cef505b4c2/bip-0143.mediawiki>
70-
fn p2wpkh_script_code(self: &Self) -> Option<ScriptBuf> {
70+
fn p2wpkh_script_code(&self) -> Option<ScriptBuf> {
7171
if self.is_p2wpkh() {
7272
// The `self` script is 0x00, 0x14, <pubkey_hash>
7373
let bytes = &self.as_bytes()[2..];
@@ -82,14 +82,14 @@ define_extension_trait! {
8282
///
8383
/// You can obtain the public key, if its valid,
8484
/// by calling [`p2pk_public_key()`](Self::p2pk_public_key)
85-
fn is_p2pk(self: &Self) -> bool { self.p2pk_pubkey_bytes().is_some() }
85+
fn is_p2pk(&self) -> bool { self.p2pk_pubkey_bytes().is_some() }
8686

8787
/// Returns the public key if this script is P2PK with a **valid** public key.
8888
///
8989
/// This may return `None` even when [`is_p2pk()`](Self::is_p2pk) returns true.
9090
/// This happens when the public key is invalid (e.g. the point not being on the curve).
9191
/// In this situation the script is unspendable.
92-
fn p2pk_public_key(self: &Self) -> Option<PublicKey> {
92+
fn p2pk_public_key(&self) -> Option<PublicKey> {
9393
PublicKey::from_slice(self.p2pk_pubkey_bytes()?).ok()
9494
}
9595
}
@@ -98,7 +98,7 @@ define_extension_trait! {
9898
define_extension_trait! {
9999
pub(crate) trait ScriptExtPrivate impl for Script {
100100
/// Returns the bytes of the (possibly invalid) public key if this script is P2PK.
101-
fn p2pk_pubkey_bytes(self: &Self) -> Option<&[u8]> {
101+
fn p2pk_pubkey_bytes(&self) -> Option<&[u8]> {
102102
match self.len() {
103103
67 if self.as_bytes()[0] == OP_PUSHBYTES_65.to_u8()
104104
&& self.as_bytes()[66] == OP_CHECKSIG.to_u8() =>

bitcoin/src/bip158.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ use crate::consensus::encode::VarInt;
4949
use crate::consensus::{Decodable, Encodable};
5050
use crate::internal_macros::impl_hashencode;
5151
use crate::prelude::{BTreeSet, Borrow, Vec};
52-
use crate::script::Script;
52+
use crate::script::{Script, ScriptExt as _};
5353
use crate::transaction::OutPoint;
5454

5555
/// Golomb encoding parameter as in BIP-158, see also https://gist.github.com/sipa/576d5f09c3b86c3b1b75598d799fc845

bitcoin/src/blockdata/block.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ use crate::merkle_tree::{MerkleNode as _, TxMerkleNode, WitnessMerkleNode};
1919
use crate::network::Params;
2020
use crate::pow::{CompactTarget, Target, Work};
2121
use crate::prelude::Vec;
22+
use crate::script::{self, ScriptExt as _};
2223
use crate::transaction::{Transaction, Wtxid};
23-
use crate::{script, VarInt};
24+
use crate::VarInt;
2425

2526
hashes::hash_newtype! {
2627
/// A bitcoin block hash.

0 commit comments

Comments
 (0)