diff --git a/bitcoin/examples/ecdsa-psbt.rs b/bitcoin/examples/ecdsa-psbt.rs index bff17d181d..943e0dab9c 100644 --- a/bitcoin/examples/ecdsa-psbt.rs +++ b/bitcoin/examples/ecdsa-psbt.rs @@ -37,6 +37,7 @@ use bitcoin::bip32::{ChildNumber, DerivationPath, Fingerprint, IntoDerivationPat use bitcoin::consensus::encode; use bitcoin::locktime::absolute; use bitcoin::psbt::{self, Input, Psbt, PsbtSighashType}; +use bitcoin::script::ScriptBufExt as _; use bitcoin::secp256k1::{Secp256k1, Signing, Verification}; use bitcoin::{ transaction, Address, Amount, CompressedPublicKey, Network, OutPoint, ScriptBuf, Sequence, diff --git a/bitcoin/examples/taproot-psbt.rs b/bitcoin/examples/taproot-psbt.rs index 734c096ebd..be43816c66 100644 --- a/bitcoin/examples/taproot-psbt.rs +++ b/bitcoin/examples/taproot-psbt.rs @@ -84,7 +84,7 @@ use bitcoin::consensus::encode; use bitcoin::key::{TapTweak, XOnlyPublicKey}; use bitcoin::opcodes::all::{OP_CHECKSIG, OP_CLTV, OP_DROP}; use bitcoin::psbt::{self, Input, Output, Psbt, PsbtSighashType}; -use bitcoin::script::ScriptExt as _; +use bitcoin::script::{ScriptBufExt as _, ScriptExt as _}; use bitcoin::secp256k1::Secp256k1; use bitcoin::sighash::{self, SighashCache, TapSighash, TapSighashType}; use bitcoin::taproot::{self, LeafVersion, TapLeafHash, TaprootBuilder, TaprootSpendInfo}; diff --git a/bitcoin/src/address/mod.rs b/bitcoin/src/address/mod.rs index e2495b70e7..b41952e7d8 100644 --- a/bitcoin/src/address/mod.rs +++ b/bitcoin/src/address/mod.rs @@ -894,6 +894,7 @@ mod tests { use super::*; use crate::network::params; use crate::network::Network::{Bitcoin, Testnet}; + use crate::script::ScriptBufExt as _; fn roundtrips(addr: &Address, network: Network) { assert_eq!( diff --git a/bitcoin/src/blockdata/script/builder.rs b/bitcoin/src/blockdata/script/builder.rs index 1261266d63..5303d37efd 100644 --- a/bitcoin/src/blockdata/script/builder.rs +++ b/bitcoin/src/blockdata/script/builder.rs @@ -7,7 +7,7 @@ use crate::locktime::absolute; use crate::opcodes::all::*; use crate::opcodes::{self, Opcode}; use crate::prelude::Vec; -use crate::script::{ScriptExt as _, ScriptExtPriv as _}; +use crate::script::{ScriptBufExt as _, ScriptExt as _, ScriptExtPriv as _}; use crate::Sequence; /// An Object which can be used to construct a script piece by piece. diff --git a/bitcoin/src/blockdata/script/instruction.rs b/bitcoin/src/blockdata/script/instruction.rs index 7898d6a7e3..8a1865a2b5 100644 --- a/bitcoin/src/blockdata/script/instruction.rs +++ b/bitcoin/src/blockdata/script/instruction.rs @@ -1,6 +1,8 @@ // SPDX-License-Identifier: CC0-1.0 -use super::{read_uint_iter, Error, PushBytes, Script, ScriptBuf, UintError}; +use super::{ + read_uint_iter, Error, PushBytes, Script, ScriptBuf, ScriptBufExtPriv as _, UintError, +}; use crate::opcodes::{self, Opcode}; /// A "parsed opcode" which allows iterating over a [`Script`] in a more sensible way. diff --git a/bitcoin/src/blockdata/script/owned.rs b/bitcoin/src/blockdata/script/owned.rs index a6d283729e..a5e1d05106 100644 --- a/bitcoin/src/blockdata/script/owned.rs +++ b/bitcoin/src/blockdata/script/owned.rs @@ -64,20 +64,6 @@ impl ScriptBuf { /// Returns a mutable reference to unsized script. pub fn as_mut_script(&mut self) -> &mut Script { Script::from_bytes_mut(&mut self.0) } - /// Creates a new script builder - pub fn builder() -> Builder { Builder::new() } - - /// Generates OP_RETURN-type of scriptPubkey for the given data. - pub fn new_op_return>(data: T) -> Self { - Builder::new().push_opcode(OP_RETURN).push_slice(data).into_script() - } - - /// Creates a [`ScriptBuf`] from a hex string. - pub fn from_hex(s: &str) -> Result { - let v = Vec::from_hex(s)?; - Ok(ScriptBuf::from_bytes(v)) - } - /// Converts byte vector into script. /// /// This method doesn't (re)allocate. @@ -88,119 +74,179 @@ impl ScriptBuf { /// This method doesn't (re)allocate. pub fn into_bytes(self) -> Vec { self.0 } - /// Adds a single opcode to the script. - pub fn push_opcode(&mut self, data: Opcode) { self.0.push(data.to_u8()); } - - /// Adds instructions to push some arbitrary data onto the stack. - pub fn push_slice>(&mut self, data: T) { - let data = data.as_ref(); - self.reserve(Self::reserved_len_for_slice(data.len())); - self.push_slice_no_opt(data); + /// Converts this `ScriptBuf` into a [boxed](Box) [`Script`]. + /// + /// This method reallocates if the capacity is greater than length of the script but should not + /// when they are equal. If you know beforehand that you need to create a script of exact size + /// use [`reserve_exact`](Self::reserve_exact) before adding data to the script so that the + /// reallocation can be avoided. + #[must_use = "`self` will be dropped if the result is not used"] + #[inline] + pub fn into_boxed_script(self) -> Box