pub struct Script<T>(/* private fields */);Expand description
Bitcoin script slice.
See also the bitcoin::script module.
Script is a script slice, the most primitive script type. It’s usually seen in its borrowed
form &Script. It is always encoded as a series of bytes representing the opcodes and data
pushes.
§Validity
Script does not have any validity invariants - it’s essentially just a marked slice of
bytes. This is similar to Path vs OsStr where they
are trivially cast-able to each-other and Path doesn’t guarantee being a usable FS path but
having a newtype still has value because of added methods, readability and basic type checking.
Although at least data pushes could be checked not to overflow the script, bad scripts are allowed to be in a transaction (outputs just become unspendable) and there even are such transactions in the chain. Thus we must allow such scripts to be placed in the transaction.
§Slicing safety
Slicing is similar to how str works: some ranges may be incorrect and indexing by
usize is not supported. However, as opposed to std, we have no way of checking
correctness without causing linear complexity so there are no panics on invalid
ranges! If you supply an invalid range, you’ll get a garbled script.
The range is considered valid if it’s at a boundary of instruction. Care must be taken especially with push operations because you could get a reference to arbitrary attacker-supplied bytes that look like a valid script.
It is recommended to use .instructions() method to get an iterator over script
instructions and work with that instead.
§Memory safety
The type is #[repr(transparent)] for internal purposes only!
No consumer crate may rely on the representation of the struct!
§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.
§Bitcoin Core References
Implementations§
Source§impl<T> Script<T>
impl<T> Script<T>
Sourcepub const fn from_bytes(bytes: &[u8]) -> &Self
pub const fn from_bytes(bytes: &[u8]) -> &Self
Treat byte slice as Script
Sourcepub fn from_bytes_mut(bytes: &mut [u8]) -> &mut Self
pub fn from_bytes_mut(bytes: &mut [u8]) -> &mut Self
Treat mutable byte slice as Script
Source§impl<T> Script<T>
impl<T> Script<T>
Sourcepub const fn as_bytes(&self) -> &[u8] ⓘ
pub const 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> ⓘ
Returns a copy of the script data.
Sourcepub fn into_script_buf(self: Box<Self>) -> ScriptBuf<T>
pub fn into_script_buf(self: Box<Self>) -> ScriptBuf<T>
Converts a Box<Script> into a ScriptBuf without copying or allocating.
Trait Implementations§
Source§impl<'a, T> Arbitrary<'a> for &'a Script<T>
Available on crate feature arbitrary only.
impl<'a, T> Arbitrary<'a> for &'a Script<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 &'de Script<T>
Available on crate feature serde only.Can only deserialize borrowed bytes.
impl<'de, T> Deserialize<'de> for &'de Script<T>
serde only.Can only deserialize borrowed bytes.
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl<'a, T> From<&'a Script<T>> for Arc<Script<T>>
Available on target_has_atomic=ptr only.Note: This will fail to compile on old Rust for targets that don’t support atomics
impl<'a, T> From<&'a Script<T>> for Arc<Script<T>>
target_has_atomic=ptr only.Note: This will fail to compile on old Rust for targets that don’t support atomics
Source§impl<T> Index<(Bound<usize>, Bound<usize>)> for Script<T>
Script subslicing operation - read slicing safety!
impl<T> Index<(Bound<usize>, Bound<usize>)> for Script<T>
Script subslicing operation - read slicing safety!
Source§impl<T> Index<RangeFrom<usize>> for Script<T>
Script subslicing operation - read slicing safety!
impl<T> Index<RangeFrom<usize>> for Script<T>
Script subslicing operation - read slicing safety!
Source§impl<T> Index<RangeInclusive<usize>> for Script<T>
Script subslicing operation - read slicing safety!
impl<T> Index<RangeInclusive<usize>> for Script<T>
Script subslicing operation - read slicing safety!
Source§impl<T> Index<RangeTo<usize>> for Script<T>
Script subslicing operation - read slicing safety!
impl<T> Index<RangeTo<usize>> for Script<T>
Script subslicing operation - read slicing safety!
Source§impl<T> Index<RangeToInclusive<usize>> for Script<T>
Script subslicing operation - read slicing safety!
impl<T> Index<RangeToInclusive<usize>> for Script<T>
Script subslicing operation - read slicing safety!