Skip to content

Commit 2bb90b8

Browse files
committed
Introduce two extensions traits for ScriptBuf
In preparation for moving the `ScritpBuf` type to `primitives` add a public and private extension trait for the functions we want to leave here in `bitcoin`. Note, includes a change to the `difine_extension_trait` metavariable used on `$gent` from `ident` to `path` to support the generic `AsRef<PushBytes>`.
1 parent ae0a5bd commit 2bb90b8

File tree

14 files changed

+34
-25
lines changed

14 files changed

+34
-25
lines changed

bitcoin/examples/ecdsa-psbt.rs

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use bitcoin::bip32::{ChildNumber, DerivationPath, Fingerprint, IntoDerivationPat
3737
use bitcoin::consensus::encode;
3838
use bitcoin::locktime::absolute;
3939
use bitcoin::psbt::{self, Input, Psbt, PsbtSighashType};
40+
use bitcoin::script::ScriptBufExt as _;
4041
use bitcoin::secp256k1::{Secp256k1, Signing, Verification};
4142
use bitcoin::{
4243
transaction, Address, Amount, CompressedPublicKey, Network, OutPoint, ScriptBuf, Sequence,

bitcoin/examples/taproot-psbt.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +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 _;
87+
use bitcoin::script::{ScriptBufExt as _, ScriptExt as _};
8888
use bitcoin::secp256k1::Secp256k1;
8989
use bitcoin::sighash::{self, SighashCache, TapSighash, TapSighashType};
9090
use bitcoin::taproot::{self, LeafVersion, TapLeafHash, TaprootBuilder, TaprootSpendInfo};

bitcoin/src/address/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -894,6 +894,7 @@ mod tests {
894894
use super::*;
895895
use crate::network::params;
896896
use crate::network::Network::{Bitcoin, Testnet};
897+
use crate::script::ScriptBufExt as _;
897898

898899
fn roundtrips(addr: &Address, network: Network) {
899900
assert_eq!(

bitcoin/src/blockdata/script/builder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +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 _};
10+
use crate::script::{ScriptBufExt as _, ScriptExt as _, ScriptExtPriv as _};
1111
use crate::Sequence;
1212

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

bitcoin/src/blockdata/script/instruction.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// SPDX-License-Identifier: CC0-1.0
22

3-
use super::{read_uint_iter, Error, PushBytes, Script, ScriptBuf, UintError};
3+
use super::{
4+
read_uint_iter, Error, PushBytes, Script, ScriptBuf, ScriptBufExtPriv as _, UintError,
5+
};
46
use crate::opcodes::{self, Opcode};
57

68
/// A "parsed opcode" which allows iterating over a [`Script`] in a more sensible way.

bitcoin/src/blockdata/script/owned.rs

+18-19
Original file line numberDiff line numberDiff line change
@@ -89,30 +89,30 @@ impl ScriptBuf {
8989
}
9090
}
9191

92-
mod tmp_pub {
93-
use super::*;
94-
impl ScriptBuf {
92+
crate::internal_macros::define_extension_trait! {
93+
/// Extension functionality for the [`ScriptBuf`] type.
94+
pub trait ScriptBufExt impl for ScriptBuf {
9595
/// Creates a new script builder
96-
pub fn builder() -> Builder { Builder::new() }
96+
fn builder() -> Builder { Builder::new() }
9797

9898
/// Generates OP_RETURN-type of scriptPubkey for the given data.
99-
pub fn new_op_return<T: AsRef<PushBytes>>(data: T) -> Self {
99+
fn new_op_return<T: AsRef<PushBytes>>(data: T) -> Self {
100100
Builder::new().push_opcode(OP_RETURN).push_slice(data).into_script()
101101
}
102102

103103
/// Creates a [`ScriptBuf`] from a hex string.
104-
pub fn from_hex(s: &str) -> Result<Self, hex::HexToBytesError> {
104+
fn from_hex(s: &str) -> Result<ScriptBuf, hex::HexToBytesError> {
105105
let v = Vec::from_hex(s)?;
106106
Ok(ScriptBuf::from_bytes(v))
107107
}
108108

109109
/// Adds a single opcode to the script.
110-
pub fn push_opcode(&mut self, data: Opcode) { self.as_byte_vec().push(data.to_u8()); }
110+
fn push_opcode(&mut self, data: Opcode) { self.as_byte_vec().push(data.to_u8()); }
111111

112112
/// Adds instructions to push some arbitrary data onto the stack.
113-
pub fn push_slice<T: AsRef<PushBytes>>(&mut self, data: T) {
113+
fn push_slice<T: AsRef<PushBytes>>(&mut self, data: T) {
114114
let data = data.as_ref();
115-
self.reserve(Self::reserved_len_for_slice(data.len()));
115+
self.reserve(ScriptBuf::reserved_len_for_slice(data.len()));
116116
self.push_slice_no_opt(data);
117117
}
118118

@@ -122,15 +122,15 @@ mod tmp_pub {
122122
///
123123
/// The method panics if the instruction is a data push with length greater or equal to
124124
/// 0x100000000.
125-
pub fn push_instruction(&mut self, instruction: Instruction<'_>) {
125+
fn push_instruction(&mut self, instruction: Instruction<'_>) {
126126
match instruction {
127127
Instruction::Op(opcode) => self.push_opcode(opcode),
128128
Instruction::PushBytes(bytes) => self.push_slice(bytes),
129129
}
130130
}
131131

132132
/// Like push_instruction, but avoids calling `reserve` to not re-check the length.
133-
pub fn push_instruction_no_opt(&mut self, instruction: Instruction<'_>) {
133+
fn push_instruction_no_opt(&mut self, instruction: Instruction<'_>) {
134134
match instruction {
135135
Instruction::Op(opcode) => self.push_opcode(opcode),
136136
Instruction::PushBytes(bytes) => self.push_slice_no_opt(bytes),
@@ -151,23 +151,22 @@ mod tmp_pub {
151151
/// This function needs to iterate over the script to find the last instruction. Prefer
152152
/// `Builder` if you're creating the script from scratch or if you want to push `OP_VERIFY`
153153
/// multiple times.
154-
pub fn scan_and_push_verify(&mut self) { self.push_verify(self.last_opcode()); }
154+
fn scan_and_push_verify(&mut self) { self.push_verify(self.last_opcode()); }
155155
}
156156
}
157157

158-
mod tmp_priv {
159-
use super::*;
160-
impl ScriptBuf {
158+
crate::internal_macros::define_extension_trait! {
159+
pub(crate) trait ScriptBufExtPriv impl for ScriptBuf {
161160
/// Pretends to convert `&mut ScriptBuf` to `&mut Vec<u8>` so that it can be modified.
162161
///
163162
/// Note: if the returned value leaks the original `ScriptBuf` will become empty.
164-
pub(crate) fn as_byte_vec(&mut self) -> ScriptBufAsVec<'_> {
163+
fn as_byte_vec(&mut self) -> ScriptBufAsVec<'_> {
165164
let vec = core::mem::take(self).into_bytes();
166165
ScriptBufAsVec(self, vec)
167166
}
168167

169168
/// Pushes the slice without reserving
170-
pub(crate) fn push_slice_no_opt(&mut self, data: &PushBytes) {
169+
fn push_slice_no_opt(&mut self, data: &PushBytes) {
171170
let mut this = self.as_byte_vec();
172171
// Start with a PUSH opcode
173172
match data.len().to_u64() {
@@ -197,7 +196,7 @@ mod tmp_priv {
197196
}
198197

199198
/// Computes the sum of `len` and the length of an appropriate push opcode.
200-
pub(crate) fn reserved_len_for_slice(len: usize) -> usize {
199+
fn reserved_len_for_slice(len: usize) -> usize {
201200
len + match len {
202201
0..=0x4b => 1,
203202
0x4c..=0xff => 2,
@@ -211,7 +210,7 @@ mod tmp_priv {
211210
/// alternative.
212211
///
213212
/// See the public fn [`Self::scan_and_push_verify`] to learn more.
214-
pub(crate) fn push_verify(&mut self, last_opcode: Option<Opcode>) {
213+
fn push_verify(&mut self, last_opcode: Option<Opcode>) {
215214
match opcode_to_verify(last_opcode) {
216215
Some(opcode) => {
217216
self.as_byte_vec().pop();

bitcoin/src/crypto/ecdsa.rs

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ use io::Write;
1313

1414
use crate::prelude::{DisplayHex, Vec};
1515
use crate::script::PushBytes;
16+
#[cfg(doc)]
17+
use crate::script::ScriptBufExt as _;
1618
use crate::sighash::{EcdsaSighashType, NonStandardSighashTypeError};
1719

1820
const MAX_SIG_LEN: usize = 73;

bitcoin/src/crypto/sighash.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1470,6 +1470,7 @@ mod tests {
14701470
use super::*;
14711471
use crate::consensus::deserialize;
14721472
use crate::locktime::absolute;
1473+
use crate::script::ScriptBufExt as _;
14731474

14741475
extern crate serde_json;
14751476

bitcoin/src/internal_macros.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ macro_rules! define_extension_trait {
251251
($(#[$($trait_attrs:tt)*])* $trait_vis:vis trait $trait_name:ident impl for $ty:ident {
252252
$(
253253
$(#[$($fn_attrs:tt)*])*
254-
fn $fn:ident$(<$($gen:ident: $gent:ident),*>)?($($params:tt)*) $( -> $ret:ty )? $body:block
254+
fn $fn:ident$(<$($gen:ident: $gent:path),*>)?($($params:tt)*) $( -> $ret:ty )? $body:block
255255
)*
256256
}) => {
257257
$(#[$($trait_attrs)*])* $trait_vis trait $trait_name {

bitcoin/src/psbt/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1216,7 +1216,7 @@ mod tests {
12161216
use crate::locktime::absolute;
12171217
use crate::network::NetworkKind;
12181218
use crate::psbt::serialize::{Deserialize, Serialize};
1219-
use crate::script::ScriptBuf;
1219+
use crate::script::{ScriptBuf, ScriptBufExt as _};
12201220
use crate::transaction::{self, OutPoint, TxIn};
12211221
use crate::witness::Witness;
12221222
use crate::Sequence;

bitcoin/src/psbt/serialize.rs

+1
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ fn key_source_len(key_source: &KeySource) -> usize { 4 + 4 * (key_source.1).as_r
383383
#[cfg(test)]
384384
mod tests {
385385
use super::*;
386+
use crate::script::ScriptBufExt as _;
386387

387388
// Composes tree matching a given depth map, filled with dumb script leafs,
388389
// each of which consists of a single push-int op code, with int value

bitcoin/src/taproot/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1544,6 +1544,7 @@ mod test {
15441544
use secp256k1::VerifyOnly;
15451545

15461546
use super::*;
1547+
use crate::script::ScriptBufExt as _;
15471548
use crate::sighash::{TapSighash, TapSighashTag};
15481549
use crate::{Address, KnownHrp};
15491550
extern crate serde_json;

bitcoin/tests/bip_174.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use bitcoin::consensus::encode::{deserialize, serialize_hex};
99
use bitcoin::hex::FromHex;
1010
use bitcoin::opcodes::OP_0;
1111
use bitcoin::psbt::{Psbt, PsbtSighashType};
12-
use bitcoin::script::PushBytes;
12+
use bitcoin::script::{PushBytes, ScriptBufExt as _};
1313
use bitcoin::secp256k1::Secp256k1;
1414
use bitcoin::{
1515
absolute, script, transaction, Amount, Denomination, NetworkKind, OutPoint, PrivateKey,

bitcoin/tests/serde.rs

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use bitcoin::hex::FromHex;
3333
use bitcoin::locktime::{absolute, relative};
3434
use bitcoin::psbt::raw::{self, Key, Pair, ProprietaryKey};
3535
use bitcoin::psbt::{Input, Output, Psbt, PsbtSighashType};
36+
use bitcoin::script::ScriptBufExt as _;
3637
use bitcoin::sighash::{EcdsaSighashType, TapSighashType};
3738
use bitcoin::taproot::{self, ControlBlock, LeafVersion, TapTree, TaprootBuilder};
3839
use bitcoin::witness::Witness;

0 commit comments

Comments
 (0)