Skip to content

Commit 0857697

Browse files
committedAug 13, 2024
Replace impl blocks with extension traits
In preparation to move script types to `primitives` we replace impl block with extension traits by replacing the temporary modules with `define_extension_trait`.
1 parent b99bdcf commit 0857697

16 files changed

+60
-49
lines changed
 

‎bitcoin/examples/sighash.rs

+1
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

+1
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

+2-1
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

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ 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

‎bitcoin/src/bip158.rs

+1-1
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

+2-1
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.

‎bitcoin/src/blockdata/script/borrowed.rs

+40-41
Original file line numberDiff line numberDiff line change
@@ -135,31 +135,31 @@ impl Script {
135135
}
136136
}
137137

138-
mod tmp_pub {
139-
use super::*;
140-
impl Script {
138+
crate::internal_macros::define_extension_trait! {
139+
/// Extension functionality for the [`Script`] type.
140+
pub trait ScriptExt impl for Script {
141141
/// Returns an iterator over script bytes.
142142
#[inline]
143-
pub fn bytes(&self) -> Bytes<'_> { Bytes(self.as_bytes().iter().copied()) }
143+
fn bytes(&self) -> Bytes<'_> { Bytes(self.as_bytes().iter().copied()) }
144144

145145
/// Creates a new script builder
146-
pub fn builder() -> Builder { Builder::new() }
146+
fn builder() -> Builder { Builder::new() }
147147

148148
/// Returns 160-bit hash of the script for P2SH outputs.
149149
#[inline]
150-
pub fn script_hash(&self) -> Result<ScriptHash, RedeemScriptSizeError> {
150+
fn script_hash(&self) -> Result<ScriptHash, RedeemScriptSizeError> {
151151
ScriptHash::from_script(self)
152152
}
153153

154154
/// Returns 256-bit hash of the script for P2WSH outputs.
155155
#[inline]
156-
pub fn wscript_hash(&self) -> Result<WScriptHash, WitnessScriptSizeError> {
156+
fn wscript_hash(&self) -> Result<WScriptHash, WitnessScriptSizeError> {
157157
WScriptHash::from_script(self)
158158
}
159159

160160
/// Computes leaf hash of tapscript.
161161
#[inline]
162-
pub fn tapscript_leaf_hash(&self) -> TapLeafHash {
162+
fn tapscript_leaf_hash(&self) -> TapLeafHash {
163163
TapLeafHash::from_script(self, LeafVersion::TapScript)
164164
}
165165

@@ -174,7 +174,7 @@ mod tmp_pub {
174174
/// > special meaning. The value of the first push is called the "version byte". The following
175175
/// > byte vector pushed is called the "witness program".
176176
#[inline]
177-
pub fn witness_version(&self) -> Option<WitnessVersion> {
177+
fn witness_version(&self) -> Option<WitnessVersion> {
178178
let script_len = self.0.len();
179179
if !(4..=42).contains(&script_len) {
180180
return None;
@@ -196,7 +196,7 @@ mod tmp_pub {
196196

197197
/// Checks whether a script pubkey is a P2SH output.
198198
#[inline]
199-
pub fn is_p2sh(&self) -> bool {
199+
fn is_p2sh(&self) -> bool {
200200
self.0.len() == 23
201201
&& self.0[0] == OP_HASH160.to_u8()
202202
&& self.0[1] == OP_PUSHBYTES_20.to_u8()
@@ -205,7 +205,7 @@ mod tmp_pub {
205205

206206
/// Checks whether a script pubkey is a P2PKH output.
207207
#[inline]
208-
pub fn is_p2pkh(&self) -> bool {
208+
fn is_p2pkh(&self) -> bool {
209209
self.0.len() == 25
210210
&& self.0[0] == OP_DUP.to_u8()
211211
&& self.0[1] == OP_HASH160.to_u8()
@@ -219,7 +219,7 @@ mod tmp_pub {
219219
/// Note: `OP_RESERVED` (`0x50`) and all the OP_PUSHNUM operations
220220
/// are considered push operations.
221221
#[inline]
222-
pub fn is_push_only(&self) -> bool {
222+
fn is_push_only(&self) -> bool {
223223
for inst in self.instructions() {
224224
match inst {
225225
Err(_) => return false,
@@ -240,7 +240,7 @@ mod tmp_pub {
240240
///
241241
/// `2 <pubkey1> <pubkey2> <pubkey3> 3 OP_CHECKMULTISIG`
242242
#[inline]
243-
pub fn is_multisig(&self) -> bool {
243+
fn is_multisig(&self) -> bool {
244244
let required_sigs;
245245

246246
let mut instructions = self.instructions();
@@ -288,27 +288,27 @@ mod tmp_pub {
288288

289289
/// Checks whether a script pubkey is a Segregated Witness (segwit) program.
290290
#[inline]
291-
pub fn is_witness_program(&self) -> bool { self.witness_version().is_some() }
291+
fn is_witness_program(&self) -> bool { self.witness_version().is_some() }
292292

293293
/// Checks whether a script pubkey is a P2WSH output.
294294
#[inline]
295-
pub fn is_p2wsh(&self) -> bool {
295+
fn is_p2wsh(&self) -> bool {
296296
self.0.len() == 34
297297
&& self.witness_version() == Some(WitnessVersion::V0)
298298
&& self.0[1] == OP_PUSHBYTES_32.to_u8()
299299
}
300300

301301
/// Checks whether a script pubkey is a P2WPKH output.
302302
#[inline]
303-
pub fn is_p2wpkh(&self) -> bool {
303+
fn is_p2wpkh(&self) -> bool {
304304
self.0.len() == 22
305305
&& self.witness_version() == Some(WitnessVersion::V0)
306306
&& self.0[1] == OP_PUSHBYTES_20.to_u8()
307307
}
308308

309309
/// Checks whether a script pubkey is a P2TR output.
310310
#[inline]
311-
pub fn is_p2tr(&self) -> bool {
311+
fn is_p2tr(&self) -> bool {
312312
self.0.len() == 34
313313
&& self.witness_version() == Some(WitnessVersion::V1)
314314
&& self.0[1] == OP_PUSHBYTES_32.to_u8()
@@ -319,7 +319,7 @@ mod tmp_pub {
319319
/// To validate if the OP_RETURN obeys Bitcoin Core's current standardness policy, use
320320
/// [`is_standard_op_return()`](Self::is_standard_op_return) instead.
321321
#[inline]
322-
pub fn is_op_return(&self) -> bool {
322+
fn is_op_return(&self) -> bool {
323323
match self.0.first() {
324324
Some(b) => *b == OP_RETURN.to_u8(),
325325
None => false,
@@ -331,7 +331,7 @@ mod tmp_pub {
331331
/// What this function considers to be standard may change without warning pending Bitcoin Core
332332
/// changes.
333333
#[inline]
334-
pub fn is_standard_op_return(&self) -> bool { self.is_op_return() && self.0.len() <= 80 }
334+
fn is_standard_op_return(&self) -> bool { self.is_op_return() && self.0.len() <= 80 }
335335

336336
/// Checks whether a script is trivially known to have no satisfying input.
337337
///
@@ -342,7 +342,7 @@ mod tmp_pub {
342342
note = "The method has potentially confusing semantics and is going to be removed, you might want `is_op_return`"
343343
)]
344344
#[inline]
345-
pub fn is_provably_unspendable(&self) -> bool {
345+
fn is_provably_unspendable(&self) -> bool {
346346
use crate::opcodes::Class::{IllegalOp, ReturnOp};
347347

348348
match self.0.first() {
@@ -362,7 +362,7 @@ mod tmp_pub {
362362
/// It merely gets the last push of the script.
363363
///
364364
/// Use [`Script::is_p2sh`] on the scriptPubKey to check whether it is actually a P2SH script.
365-
pub fn redeem_script(&self) -> Option<&Script> {
365+
fn redeem_script(&self) -> Option<&Script> {
366366
// Script must consist entirely of pushes.
367367
if self.instructions().any(|i| i.is_err() || i.unwrap().push_bytes().is_none()) {
368368
return None;
@@ -378,7 +378,7 @@ mod tmp_pub {
378378
/// Returns the minimum value an output with this script should have in order to be
379379
/// broadcastable on today’s Bitcoin network.
380380
#[deprecated(since = "0.32.0", note = "use minimal_non_dust and friends")]
381-
pub fn dust_value(&self) -> crate::Amount { self.minimal_non_dust() }
381+
fn dust_value(&self) -> crate::Amount { self.minimal_non_dust() }
382382

383383
/// Returns the minimum value an output with this script should have in order to be
384384
/// broadcastable on today's Bitcoin network.
@@ -389,7 +389,7 @@ mod tmp_pub {
389389
/// To use a custom value, use [`minimal_non_dust_custom`].
390390
///
391391
/// [`minimal_non_dust_custom`]: Script::minimal_non_dust_custom
392-
pub fn minimal_non_dust(&self) -> crate::Amount {
392+
fn minimal_non_dust(&self) -> crate::Amount {
393393
self.minimal_non_dust_internal(DUST_RELAY_TX_FEE.into())
394394
}
395395

@@ -404,7 +404,7 @@ mod tmp_pub {
404404
/// To use the default Bitcoin Core value, use [`minimal_non_dust`].
405405
///
406406
/// [`minimal_non_dust`]: Script::minimal_non_dust
407-
pub fn minimal_non_dust_custom(&self, dust_relay_fee: FeeRate) -> crate::Amount {
407+
fn minimal_non_dust_custom(&self, dust_relay_fee: FeeRate) -> crate::Amount {
408408
self.minimal_non_dust_internal(dust_relay_fee.to_sat_per_kwu() * 4)
409409
}
410410

@@ -422,7 +422,7 @@ mod tmp_pub {
422422
/// (Note: Taproot scripts don't count toward the sigop count of the block,
423423
/// nor do they have CHECKMULTISIG operations. This function does not count OP_CHECKSIGADD,
424424
/// so do not use this to try and estimate if a Taproot script goes over the sigop budget.)
425-
pub fn count_sigops(&self) -> usize { self.count_sigops_internal(true) }
425+
fn count_sigops(&self) -> usize { self.count_sigops_internal(true) }
426426

427427
/// Counts the sigops for this Script using legacy counting.
428428
///
@@ -436,7 +436,7 @@ mod tmp_pub {
436436
/// (Note: Taproot scripts don't count toward the sigop count of the block,
437437
/// nor do they have CHECKMULTISIG operations. This function does not count OP_CHECKSIGADD,
438438
/// so do not use this to try and estimate if a Taproot script goes over the sigop budget.)
439-
pub fn count_sigops_legacy(&self) -> usize { self.count_sigops_internal(false) }
439+
fn count_sigops_legacy(&self) -> usize { self.count_sigops_internal(false) }
440440

441441
/// Iterates over the script instructions.
442442
///
@@ -446,7 +446,7 @@ mod tmp_pub {
446446
///
447447
/// To force minimal pushes, use [`instructions_minimal`](Self::instructions_minimal).
448448
#[inline]
449-
pub fn instructions(&self) -> Instructions {
449+
fn instructions(&self) -> Instructions {
450450
Instructions { data: self.0.iter(), enforce_minimal: false }
451451
}
452452

@@ -455,7 +455,7 @@ mod tmp_pub {
455455
/// This is similar to [`instructions`](Self::instructions) but an error is returned if a push
456456
/// is not minimal.
457457
#[inline]
458-
pub fn instructions_minimal(&self) -> Instructions {
458+
fn instructions_minimal(&self) -> Instructions {
459459
Instructions { data: self.0.iter(), enforce_minimal: true }
460460
}
461461

@@ -467,7 +467,7 @@ mod tmp_pub {
467467
///
468468
/// To force minimal pushes, use [`Self::instruction_indices_minimal`].
469469
#[inline]
470-
pub fn instruction_indices(&self) -> InstructionIndices {
470+
fn instruction_indices(&self) -> InstructionIndices {
471471
InstructionIndices::from_instructions(self.instructions())
472472
}
473473

@@ -476,17 +476,17 @@ mod tmp_pub {
476476
/// This is similar to [`instruction_indices`](Self::instruction_indices) but an error is
477477
/// returned if a push is not minimal.
478478
#[inline]
479-
pub fn instruction_indices_minimal(&self) -> InstructionIndices {
479+
fn instruction_indices_minimal(&self) -> InstructionIndices {
480480
InstructionIndices::from_instructions(self.instructions_minimal())
481481
}
482482

483483
/// Writes the human-readable assembly representation of the script to the formatter.
484-
pub fn fmt_asm(&self, f: &mut dyn fmt::Write) -> fmt::Result {
484+
fn fmt_asm(&self, f: &mut dyn fmt::Write) -> fmt::Result {
485485
bytes_to_asm_fmt(self.as_ref(), f)
486486
}
487487

488488
/// Returns the human-readable assembly representation of the script.
489-
pub fn to_asm_string(&self) -> String {
489+
fn to_asm_string(&self) -> String {
490490
let mut buf = String::new();
491491
self.fmt_asm(&mut buf).unwrap();
492492
buf
@@ -497,19 +497,18 @@ mod tmp_pub {
497497
/// This is a more convenient and performant way to write `format!("{:x}", script)`.
498498
/// For better performance you should generally prefer displaying the script but if `String` is
499499
/// required (this is common in tests) this method can be used.
500-
pub fn to_hex_string(&self) -> String { self.as_bytes().to_lower_hex_string() }
500+
fn to_hex_string(&self) -> String { self.as_bytes().to_lower_hex_string() }
501501

502502
/// Returns the first opcode of the script (if there is any).
503-
pub fn first_opcode(&self) -> Option<Opcode> {
503+
fn first_opcode(&self) -> Option<Opcode> {
504504
self.as_bytes().first().copied().map(From::from)
505505
}
506506
}
507507
}
508508

509-
mod tmp_priv {
510-
use super::*;
511-
impl Script {
512-
pub(crate) fn minimal_non_dust_internal(&self, dust_relay_fee: u64) -> crate::Amount {
509+
crate::internal_macros::define_extension_trait! {
510+
pub(crate) trait ScriptExtPriv impl for Script {
511+
fn minimal_non_dust_internal(&self, dust_relay_fee: u64) -> crate::Amount {
513512
// This must never be lower than Bitcoin Core's GetDustThreshold() (as of v0.21) as it may
514513
// otherwise allow users to create transactions which likely can never be broadcast/confirmed.
515514
let sats = dust_relay_fee
@@ -532,7 +531,7 @@ mod tmp_priv {
532531
crate::Amount::from_sat(sats)
533532
}
534533

535-
pub(crate) fn count_sigops_internal(&self, accurate: bool) -> usize {
534+
fn count_sigops_internal(&self, accurate: bool) -> usize {
536535
let mut n = 0;
537536
let mut pushnum_cache = None;
538537
for inst in self.instructions() {
@@ -575,7 +574,7 @@ mod tmp_priv {
575574
/// Iterates the script to find the last opcode.
576575
///
577576
/// Returns `None` is the instruction is data push or if the script is empty.
578-
pub(in crate::blockdata::script) fn last_opcode(&self) -> Option<Opcode> {
577+
fn last_opcode(&self) -> Option<Opcode> {
579578
match self.instructions().last() {
580579
Some(Ok(Instruction::Op(op))) => Some(op),
581580
_ => None,
@@ -585,7 +584,7 @@ mod tmp_priv {
585584
/// Iterates the script to find the last pushdata.
586585
///
587586
/// Returns `None` if the instruction is an opcode or if the script is empty.
588-
pub(crate) fn last_pushdata(&self) -> Option<&PushBytes> {
587+
fn last_pushdata(&self) -> Option<&PushBytes> {
589588
match self.instructions().last() {
590589
// Handles op codes up to (but excluding) OP_PUSHNUM_NEG.
591590
Some(Ok(Instruction::PushBytes(bytes))) => Some(bytes),

‎bitcoin/src/blockdata/script/builder.rs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::locktime::absolute;
77
use crate::opcodes::all::*;
88
use crate::opcodes::{self, Opcode};
99
use crate::prelude::Vec;
10+
use crate::script::{ScriptExt as _, ScriptExtPriv as _};
1011
use crate::Sequence;
1112

1213
/// An Object which can be used to construct a script piece by piece.

‎bitcoin/src/blockdata/script/owned.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use core::ops::Deref;
66
use hex::FromHex;
77
use internals::ToU64 as _;
88

9-
use super::{opcode_to_verify, Builder, Instruction, PushBytes, Script};
9+
use super::{opcode_to_verify, Builder, Instruction, PushBytes, Script, ScriptExtPriv as _};
1010
use crate::opcodes::all::*;
1111
use crate::opcodes::{self, Opcode};
1212
use crate::prelude::{Box, Vec};

‎bitcoin/src/blockdata/script/witness_program.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use secp256k1::{Secp256k1, Verification};
1515
use super::witness_version::WitnessVersion;
1616
use super::{PushBytes, Script, WScriptHash, WitnessScriptSizeError};
1717
use crate::crypto::key::{CompressedPublicKey, TapTweak, TweakedPublicKey, UntweakedPublicKey};
18+
use crate::script::ScriptExt as _;
1819
use crate::taproot::TapNodeHash;
1920

2021
/// The minimum byte size of a segregated witness program.

‎bitcoin/src/blockdata/transaction.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::consensus::{encode, Decodable, Encodable};
2323
use crate::internal_macros::{impl_consensus_encoding, impl_hashencode};
2424
use crate::locktime::absolute::{self, Height, Time};
2525
use crate::prelude::{Borrow, Vec};
26-
use crate::script::{Script, ScriptBuf};
26+
use crate::script::{Script, ScriptBuf, ScriptExt as _, ScriptExtPriv as _};
2727
#[cfg(doc)]
2828
use crate::sighash::{EcdsaSighashType, TapSighashType};
2929
use crate::witness::Witness;

‎bitcoin/src/blockdata/witness.rs

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ use crate::consensus::encode::{Error, MAX_VEC_SIZE};
1313
use crate::consensus::{Decodable, Encodable, WriteExt};
1414
use crate::crypto::ecdsa;
1515
use crate::prelude::Vec;
16+
#[cfg(doc)]
17+
use crate::script::ScriptExt as _;
1618
use crate::taproot::{self, TAPROOT_ANNEX_PREFIX};
1719
use crate::{Script, VarInt};
1820

0 commit comments

Comments
 (0)
Please sign in to comment.