pub struct ScriptBuf<T>(/* private fields */);Expand description
An owned, growable script.
ScriptBuf is the most common script type that has the ownership over the contents of the
script. It has a close relationship with its borrowed counterpart, Script.
Just as other similar types, this implements Deref, so deref coercions apply. Also note
that all the safety/validity restrictions that apply to Script apply to ScriptBuf as well.
§Hexadecimal strings
Scripts are consensus encoded with a length prefix and as a result of this in some places in the
ecosystem one will encounter hex strings that include the prefix while in other places the
prefix is excluded. To support parsing and formatting scripts as hex we provide a bunch of
different APIs and trait implementations. Please see examples/script.rs for a thorough
example of all the APIs.
§Panics
ScriptBuf is backed by Vec and inherits its panic behavior. This means that attempting to
construct scripts larger than isize::MAX bytes will panic.
Implementations§
Source§impl<T> ScriptBuf<T>
impl<T> ScriptBuf<T>
Sourcepub const fn from_bytes(bytes: Vec<u8>) -> Self
pub const fn from_bytes(bytes: Vec<u8>) -> Self
Converts byte vector into script.
This method doesn’t (re)allocate. bytes is just the script bytes not consensus
encoding (i.e no length prefix).
Sourcepub fn from_hex_prefixed(
s: &str,
) -> Result<Self, FromHexError<ScriptBufDecoderError>>
pub fn from_hex_prefixed( s: &str, ) -> Result<Self, FromHexError<ScriptBufDecoderError>>
Sourcepub fn from_hex_no_length_prefix(
s: &str,
) -> Result<Self, DecodeVariableLengthBytesError>
pub fn from_hex_no_length_prefix( s: &str, ) -> Result<Self, DecodeVariableLengthBytesError>
Constructs a new ScriptBuf from a hex string.
This is not consensus encoding. If your hex string is a consensus encoded script
then use ScriptBuf::from_hex_prefixed.
There is no script decoding error path because what ever is in the hex input string is assumed to be the script. This means if you pass a consensus encoded hex string into this function there will be no error and the script will not be what you expect.
§Errors
Errors if s cannot be parsed into a vector.
Sourcepub fn as_mut_script(&mut self) -> &mut Script<T>
pub fn as_mut_script(&mut self) -> &mut Script<T>
Returns a mutable reference to unsized script.
Sourcepub fn into_bytes(self) -> Vec<u8> ⓘ
pub fn into_bytes(self) -> Vec<u8> ⓘ
Converts the script into a byte vector.
This method doesn’t (re)allocate.
§Returns
Just the script bytes not consensus encoding (which includes a length prefix).
Sourcepub fn into_boxed_script(self) -> Box<Script<T>>
pub fn into_boxed_script(self) -> Box<Script<T>>
Converts this ScriptBuf into a boxed 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 before adding data to the script so that the
reallocation can be avoided.
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Constructs a new empty script with at least the specified capacity.
Sourcepub fn reserve(&mut self, additional_len: usize)
pub fn reserve(&mut self, additional_len: usize)
Pre-allocates at least additional_len bytes if needed.
Reserves capacity for at least additional_len more bytes to be inserted in the given
script. The script may reserve more space to speculatively avoid frequent reallocations.
After calling reserve, capacity will be greater than or equal to
self.len() + additional_len. Does nothing if capacity is already sufficient.
§Panics
Panics if the new capacity exceeds isize::MAX bytes.
Sourcepub fn reserve_exact(&mut self, additional_len: usize)
pub fn reserve_exact(&mut self, additional_len: usize)
Pre-allocates exactly additional_len bytes if needed.
Unlike reserve, this will not deliberately over-allocate to speculatively avoid frequent
allocations. After calling reserve_exact, capacity will be greater than or equal to
self.len() + additional. Does nothing if the capacity is already sufficient.
Note that the allocator may give the collection more space than it requests. Therefore,
capacity cannot be relied upon to be precisely minimal. Prefer reserve
if future insertions are expected.
§Panics
Panics if the new capacity exceeds isize::MAX bytes.
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of bytes available for writing without reallocation.
It is guaranteed that script.capacity() >= script.len() always holds.
Sourcepub fn to_hex(&self) -> String
👎Deprecated since 1.0.0-rc.0: use format!("{var:x}") instead
pub fn to_hex(&self) -> String
use format!("{var:x}") instead
Gets the hex representation of this script.
§Returns
Just the script bytes in hexadecimal not consensus encoding of the script i.e., the string will not include a length prefix.
Sourcepub fn push_opcode(&mut self, data: Opcode)
pub fn push_opcode(&mut self, data: Opcode)
Adds a single opcode to the script.
Sourcepub fn push_slice<D: AsRef<PushBytes>>(&mut self, data: D)
pub fn push_slice<D: AsRef<PushBytes>>(&mut self, data: D)
Adds instructions to push some arbitrary data onto the stack.
If the data can be exactly produced by a numeric opcode, that opcode
will be used, since its behavior is equivalent but will not violate minimality
rules. To avoid this, use ScriptBuf::push_slice_non_minimal which will always
use a push opcode.
However, this method does not enforce any numeric minimality rules. If your pushes should be interpreted as numbers, ensure your input does not have any leading zeros. In particular, the number 0 should be encoded as an empty string rather than as a single 0 byte.
Sourcepub fn push_slice_non_minimal<D: AsRef<PushBytes>>(&mut self, data: D)
pub fn push_slice_non_minimal<D: AsRef<PushBytes>>(&mut self, data: D)
Adds instructions to push some arbitrary data onto the stack without minimality.
Standardness rules require push minimality according to CheckMinimalPush of core.
Source§impl ScriptBuf<ScriptPubKeyTag>
impl ScriptBuf<ScriptPubKeyTag>
Sourcepub fn new_op_return<T: AsRef<PushBytes>>(data: T) -> Self
pub fn new_op_return<T: AsRef<PushBytes>>(data: T) -> Self
Generates OP_RETURN-type of scriptPubkey for the given data.
Sourcepub fn new_p2sh(script_hash: ScriptHash) -> Self
pub fn new_p2sh(script_hash: ScriptHash) -> Self
Generates P2SH-type of scriptPubkey with a given hash of the redeem script.
Source§impl<T: ScriptHashableTag> ScriptBuf<T>
impl<T: ScriptHashableTag> ScriptBuf<T>
Sourcepub fn new_p2wsh(script_hash: WScriptHash) -> Self
pub fn new_p2wsh(script_hash: WScriptHash) -> Self
Generates a P2WSH witness program script with a given hash of the witness script.
Methods from Deref<Target = Script<T>>§
Sourcepub fn as_bytes(&self) -> &[u8] ⓘ
pub fn as_bytes(&self) -> &[u8] ⓘ
Returns the script data as a byte slice.
This is just the script bytes not consensus encoding (which includes a length prefix).
Sourcepub fn as_mut_bytes(&mut self) -> &mut [u8] ⓘ
pub fn as_mut_bytes(&mut self) -> &mut [u8] ⓘ
Returns the script data as a mutable byte slice.
This is just the script bytes not consensus encoding (which includes a length prefix).
Sourcepub fn to_vec(&self) -> Vec<u8> ⓘ
pub fn to_vec(&self) -> Vec<u8> ⓘ
Returns a copy of the script data.
This is just the script bytes not consensus encoding (which includes a length prefix).
Sourcepub fn to_bytes(&self) -> Vec<u8> ⓘ
👎Deprecated since 0.101.0: use to_vec instead
pub fn to_bytes(&self) -> Vec<u8> ⓘ
use to_vec instead
Returns a copy of the script data.
Sourcepub fn to_hex_string_prefixed(&self) -> String
pub fn to_hex_string_prefixed(&self) -> String
Consensus encodes the script as lower-case hex.
Consensus encoding includes a length prefix. To hex encode without the length prefix use
to_hex_string_no_length_prefix.
Sourcepub fn to_hex_string_no_length_prefix(&self) -> String
pub fn to_hex_string_no_length_prefix(&self) -> String
Encodes the script as lower-case hex.
This is not consensus encoding. The returned hex string will not include the length
prefix. See to_hex_string_prefixed.
Sourcepub fn to_hex(&self) -> String
👎Deprecated since 1.0.0-rc.0: use format!("{var:x}") instead
pub fn to_hex(&self) -> String
use format!("{var:x}") instead
Gets the hex representation of this script.
§Returns
Just the script bytes in hexadecimal not consensus encoding of the script i.e., the string will not include a length prefix.
Sourcepub fn witness_version(&self) -> Option<WitnessVersion>where
T: ScriptHashableTag,
pub fn witness_version(&self) -> Option<WitnessVersion>where
T: ScriptHashableTag,
Returns witness version of the script, if any.
§Returns
The witness version if this script is found to conform to the SegWit rules:
A scriptPubKey (or redeemScript as defined in BIP-0016/P2SH) that consists of a 1-byte push opcode (for 0 to 16) followed by a data push between 2 and 40 bytes gets a new special meaning. The value of the first push is called the “version byte”. The following byte vector pushed is called the “witness program”.
Sourcepub fn is_p2wsh(&self) -> boolwhere
T: ScriptHashableTag,
pub fn is_p2wsh(&self) -> boolwhere
T: ScriptHashableTag,
Checks whether a script pubkey is a P2WSH output.
Sourcepub fn is_p2wpkh(&self) -> boolwhere
T: ScriptHashableTag,
pub fn is_p2wpkh(&self) -> boolwhere
T: ScriptHashableTag,
Checks whether a script pubkey is a P2WPKH output.
Sourcepub fn script_hash(&self) -> Result<ScriptHash, RedeemScriptSizeError>
pub fn script_hash(&self) -> Result<ScriptHash, RedeemScriptSizeError>
Returns 160-bit hash of the script for P2SH outputs.
§Errors
Returns an error if the script exceeds 520 bytes.
Trait Implementations§
Source§impl<'a, T> Arbitrary<'a> for ScriptBuf<T>
Available on crate feature arbitrary only.
impl<'a, T> Arbitrary<'a> for ScriptBuf<T>
arbitrary only.Source§fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>
Self from the given unstructured data. Read moreSource§fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
Self from the entirety of the given
unstructured data. Read moreSource§fn size_hint(depth: usize) -> (usize, Option<usize>)
fn size_hint(depth: usize) -> (usize, Option<usize>)
Unstructured this type
needs to construct itself. Read moreSource§fn try_size_hint(
depth: usize,
) -> Result<(usize, Option<usize>), MaxRecursionReached>
fn try_size_hint( depth: usize, ) -> Result<(usize, Option<usize>), MaxRecursionReached>
Unstructured this type
needs to construct itself. Read moreSource§impl<T> BorrowMut<Script<T>> for ScriptBuf<T>
impl<T> BorrowMut<Script<T>> for ScriptBuf<T>
Source§fn borrow_mut(&mut self) -> &mut Script<T>
fn borrow_mut(&mut self) -> &mut Script<T>
Source§impl<'de, T> Deserialize<'de> for ScriptBuf<T>
Available on crate feature serde only.
impl<'de, T> Deserialize<'de> for ScriptBuf<T>
serde only.