Script

Struct Script 

Source
pub struct Script(/* private fields */);
Available on crate feature alloc only.
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!

§References

§Bitcoin Core References

Implementations§

Source§

impl Script

Source

pub fn new() -> &'static Script

Constructs a new empty script.

Source

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

Treat byte slice as Script

Source

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

Treat mutable byte slice as Script

Source

pub fn as_bytes(&self) -> &[u8]

Returns the script data as a byte slice.

Source

pub fn as_mut_bytes(&mut self) -> &mut [u8]

Returns the script data as a mutable byte slice.

Source

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

Returns a copy of the script data.

Source

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

👎Deprecating in a future version: use to_vec instead

Returns a copy of the script data.

Source

pub fn len(&self) -> usize

Returns the length in bytes of the script.

Source

pub fn is_empty(&self) -> bool

Returns whether the script is the empty script.

Source

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

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

Source§

impl Script

Source

pub fn to_hex(&self) -> String

Gets the hex representation of this type

Trait Implementations§

Source§

impl AsMut<[u8]> for Script

Source§

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

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

impl AsMut<Script> for Script

Source§

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

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

impl AsMut<Script> for ScriptBuf

Source§

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

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

impl AsRef<[u8]> for Script

Source§

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

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

impl AsRef<Script> for Script

Source§

fn as_ref(&self) -> &Script

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

impl AsRef<Script> for ScriptBuf

Source§

fn as_ref(&self) -> &Script

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

impl Borrow<Script> for ScriptBuf

Source§

fn borrow(&self) -> &Script

Immutably borrows from an owned value. Read more
Source§

impl BorrowMut<Script> for ScriptBuf

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl Debug for Script

Source§

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

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

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

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 Display for Script

Source§

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

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

impl<'a> From<&'a Script> for Arc<Script>

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

Converts to this type from the input type.
Source§

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

Source§

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

Converts to this type from the input type.
Source§

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

Source§

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

Converts to this type from the input type.
Source§

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

Source§

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

Converts to this type from the input type.
Source§

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

Source§

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

Converts to this type from the input type.
Source§

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

Source§

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

Converts to this type from the input type.
Source§

impl From<ScriptBuf> for Box<Script>

Source§

fn from(v: ScriptBuf) -> Self

Converts to this type from the input type.
Source§

impl Hash for Script

Source§

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

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

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

Script subslicing operation - read slicing safety!

Source§

type Output = Script

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 Index<Range<usize>> for Script

Script subslicing operation - read slicing safety!

Source§

type Output = Script

The returned type after indexing.
Source§

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

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

impl Index<RangeFrom<usize>> for Script

Script subslicing operation - read slicing safety!

Source§

type Output = Script

The returned type after indexing.
Source§

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

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

impl Index<RangeFull> for Script

Script subslicing operation - read slicing safety!

Source§

type Output = Script

The returned type after indexing.
Source§

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

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

impl Index<RangeInclusive<usize>> for Script

Script subslicing operation - read slicing safety!

Source§

type Output = Script

The returned type after indexing.
Source§

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

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

impl Index<RangeTo<usize>> for Script

Script subslicing operation - read slicing safety!

Source§

type Output = Script

The returned type after indexing.
Source§

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

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

impl Index<RangeToInclusive<usize>> for Script

Script subslicing operation - read slicing safety!

Source§

type Output = Script

The returned type after indexing.
Source§

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

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

impl LowerHex for Script

Source§

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

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

impl Ord for Script

Source§

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

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

impl PartialEq<Script> for ScriptBuf

Source§

fn eq(&self, other: &Script) -> 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 PartialEq<ScriptBuf> for Script

Source§

fn eq(&self, other: &ScriptBuf) -> 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 PartialEq for Script

Source§

fn eq(&self, other: &Script) -> 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 PartialOrd<Script> for ScriptBuf

Source§

fn partial_cmp(&self, other: &Script) -> 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 PartialOrd<ScriptBuf> for Script

Source§

fn partial_cmp(&self, other: &ScriptBuf) -> 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 PartialOrd for Script

Source§

fn partial_cmp(&self, other: &Script) -> 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 Serialize for Script

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 ToOwned for Script

Source§

type Owned = ScriptBuf

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 UpperHex for Script

Source§

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

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

impl Eq for Script

Source§

impl StructuralPartialEq for Script

Auto Trait Implementations§

§

impl Freeze for Script

§

impl RefUnwindSafe for Script

§

impl Send for Script

§

impl !Sized for Script

§

impl Sync for Script

§

impl Unpin for Script

§

impl UnwindSafe for Script

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