Skip to main content

Script

Struct Script 

Source
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>

Source

pub const fn from_bytes(bytes: &[u8]) -> &Self

Treat byte slice as Script

Source

pub fn from_bytes_mut(bytes: &mut [u8]) -> &mut Self

Treat mutable byte slice as Script

Source§

impl<T> Script<T>

Source

pub const fn new() -> &'static Self

Constructs a new empty script.

Source

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).

Source

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).

Source

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).

Source

pub fn to_bytes(&self) -> Vec<u8>

👎Deprecated since 0.101.0: use to_vec instead

Returns a copy of the script data.

Source

pub const fn len(&self) -> usize

Returns the length in bytes of the script.

Source

pub const fn is_empty(&self) -> bool

Returns whether the script is the empty script.

Source

pub fn into_script_buf(self: Box<Self>) -> ScriptBuf<T>

Converts a Box<Script> into a ScriptBuf without copying or allocating.

Source

pub fn to_hex(&self) -> String

👎Deprecated since 1.0.0-rc.0: 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.

Trait Implementations§

Source§

impl<'a, T> Arbitrary<'a> for &'a Script<T>

Available on crate feature arbitrary only.
Source§

fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>

Generate an arbitrary value of Self from the given unstructured data. Read more
Source§

fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>

Generate an arbitrary value of Self from the entirety of the given unstructured data. Read more
Source§

fn size_hint(depth: usize) -> (usize, Option<usize>)

Get a size hint for how many bytes out of an Unstructured this type needs to construct itself. Read more
Source§

fn try_size_hint( depth: usize, ) -> Result<(usize, Option<usize>), MaxRecursionReached>

Get a size hint for how many bytes out of an Unstructured this type needs to construct itself. Read more
Source§

impl<T> AsMut<[u8]> for Script<T>

Source§

fn as_mut(&mut self) -> &mut [u8]

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl<T> AsMut<Script<T>> for Script<T>

Source§

fn as_mut(&mut self) -> &mut Self

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl<T> AsMut<Script<T>> for ScriptBuf<T>

Source§

fn as_mut(&mut self) -> &mut Script<T>

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl<T> AsRef<[u8]> for Script<T>

Source§

fn as_ref(&self) -> &[u8]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T> AsRef<Script<T>> for Script<T>

Source§

fn as_ref(&self) -> &Self

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T> AsRef<Script<T>> for ScriptBuf<T>

Source§

fn as_ref(&self) -> &Script<T>

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T> Borrow<Script<T>> for ScriptBuf<T>

Source§

fn borrow(&self) -> &Script<T>

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<Script<T>> for ScriptBuf<T>

Source§

fn borrow_mut(&mut self) -> &mut Script<T>

Mutably borrows from an owned value. Read more
Source§

impl<T> Debug for Script<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: 'static> Default for &Script<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de, T> Deserialize<'de> for &'de Script<T>

Available on crate feature serde only.

Can only deserialize borrowed bytes.

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<T> Display for Script<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Encodable for Script<T>

Source§

type Encoder<'e> = ScriptEncoder<'e> where Self: 'e

The encoder associated with this type. Conceptually, the encoder is like an iterator which yields byte slices.
Source§

fn encoder(&self) -> Self::Encoder<'_>

Constructs a “default encoder” for the type.
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

Source§

fn from(value: &'a Script<T>) -> Self

Converts to this type from the input type.
Source§

impl<'a, T> From<&'a Script<T>> for Box<Script<T>>

Source§

fn from(value: &'a Script<T>) -> Self

Converts to this type from the input type.
Source§

impl<'a, T> From<&'a Script<T>> for Cow<'a, Script<T>>

Source§

fn from(value: &'a Script<T>) -> Self

Converts to this type from the input type.
Source§

impl<'a, T> From<&'a Script<T>> for Rc<Script<T>>

Source§

fn from(value: &'a Script<T>) -> Self

Converts to this type from the input type.
Source§

impl<'a, T> From<&'a Script<T>> for ScriptBuf<T>

Source§

fn from(value: &'a Script<T>) -> Self

Converts to this type from the input type.
Source§

impl<'a, T> From<Cow<'a, Script<T>>> for Box<Script<T>>

Source§

fn from(value: Cow<'a, Script<T>>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<ScriptBuf<T>> for Box<Script<T>>

Source§

fn from(v: ScriptBuf<T>) -> Self

Converts to this type from the input type.
Source§

impl<T: Hash> Hash for Script<T>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
Source§

impl<T> Index<(Bound<usize>, Bound<usize>)> for Script<T>

Script subslicing operation - read slicing safety!

Source§

type Output = Script<T>

The returned type after indexing.
Source§

fn index(&self, index: (Bound<usize>, Bound<usize>)) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T> Index<Range<usize>> for Script<T>

Script subslicing operation - read slicing safety!

Source§

type Output = Script<T>

The returned type after indexing.
Source§

fn index(&self, index: Range<usize>) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T> Index<RangeFrom<usize>> for Script<T>

Script subslicing operation - read slicing safety!

Source§

type Output = Script<T>

The returned type after indexing.
Source§

fn index(&self, index: RangeFrom<usize>) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T> Index<RangeFull> for Script<T>

Script subslicing operation - read slicing safety!

Source§

type Output = Script<T>

The returned type after indexing.
Source§

fn index(&self, index: RangeFull) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T> Index<RangeInclusive<usize>> for Script<T>

Script subslicing operation - read slicing safety!

Source§

type Output = Script<T>

The returned type after indexing.
Source§

fn index(&self, index: RangeInclusive<usize>) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T> Index<RangeTo<usize>> for Script<T>

Script subslicing operation - read slicing safety!

Source§

type Output = Script<T>

The returned type after indexing.
Source§

fn index(&self, index: RangeTo<usize>) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T> Index<RangeToInclusive<usize>> for Script<T>

Script subslicing operation - read slicing safety!

Source§

type Output = Script<T>

The returned type after indexing.
Source§

fn index(&self, index: RangeToInclusive<usize>) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T> LowerHex for Script<T>

Available on crate feature hex only.
Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Ord> Ord for Script<T>

Source§

fn cmp(&self, other: &Script<T>) -> Ordering

This method returns an Ordering between self and other. Read more
Source§

impl<T: PartialEq> PartialEq<Script<T>> for ScriptBuf<T>

Source§

fn eq(&self, other: &Script<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: PartialEq> PartialEq<ScriptBuf<T>> for Script<T>

Source§

fn eq(&self, other: &ScriptBuf<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: PartialEq> PartialEq for Script<T>

Source§

fn eq(&self, other: &Script<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: PartialOrd> PartialOrd<Script<T>> for ScriptBuf<T>

Source§

fn partial_cmp(&self, other: &Script<T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: PartialOrd> PartialOrd<ScriptBuf<T>> for Script<T>

Source§

fn partial_cmp(&self, other: &ScriptBuf<T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: PartialOrd> PartialOrd for Script<T>

Source§

fn partial_cmp(&self, other: &Script<T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T> Serialize for Script<T>

Available on crate feature serde only.
Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

User-facing serialization for Script.

Source§

impl<T> ToOwned for Script<T>

Source§

type Owned = ScriptBuf<T>

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> Self::Owned

Creates owned data from borrowed data, usually by cloning. Read more
1.63.0 · Source§

fn clone_into(&self, target: &mut Self::Owned)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T: ScriptHashableTag> TryFrom<&Script<T>> for ScriptHash

Source§

type Error = RedeemScriptSizeError

The type returned in the event of a conversion error.
Source§

fn try_from(redeem_script: &Script<T>) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<&Script<WitnessScriptTag>> for WScriptHash

Source§

type Error = WitnessScriptSizeError

The type returned in the event of a conversion error.
Source§

fn try_from(witness_script: &WitnessScript) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<T> UpperHex for Script<T>

Available on crate feature hex only.
Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Eq> Eq for Script<T>

Source§

impl<T> StructuralPartialEq for Script<T>

Auto Trait Implementations§

§

impl<T> Freeze for Script<T>

§

impl<T> RefUnwindSafe for Script<T>
where T: RefUnwindSafe,

§

impl<T> Send for Script<T>
where T: Send,

§

impl<T> !Sized for Script<T>

§

impl<T> Sync for Script<T>
where T: Sync,

§

impl<T> Unpin for Script<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for Script<T>

§

impl<T> UnwindSafe for Script<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more