Skip to main content

Script

Struct Script 

Source
pub struct Script(/* private fields */);
Expand description

A Bitcoin script, represented as a byte vector newtype.

Implementations§

Source§

impl Script

Source

pub fn new() -> Self

Create a new empty script.

§Returns

An empty Script instance.

Source

pub fn from_hex(hex_str: &str) -> Result<Self, ScriptError>

Create a script from a hex-encoded string.

§Arguments
  • hex_str - A hex string (e.g. “76a914…88ac”).
§Returns

A Script wrapping the decoded bytes, or an error if the hex is invalid.

Source

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

Create a script from raw bytes.

§Arguments
  • bytes - Raw script bytes.
§Returns

A Script wrapping a copy of the given bytes.

Source

pub fn from_asm(asm: &str) -> Result<Self, ScriptError>

Create a script from a Bitcoin ASM string.

Parses space-separated tokens where known opcodes (e.g. “OP_DUP”) are emitted directly and hex strings are treated as push data.

§Arguments
  • asm - A space-separated ASM string.
§Returns

A Script, or an error if any token is invalid.

Source

pub fn to_hex(&self) -> String

Encode the script as a hex string.

§Returns

A lowercase hex representation of the script bytes.

Source

pub fn to_asm(&self) -> String

Convert the script to its ASM (human-readable assembly) representation.

Each opcode or data push is represented as a space-separated token. Data pushes appear as their hex encoding; opcodes appear by name.

§Returns

A space-separated ASM string. Returns empty string for empty/invalid scripts.

Source

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

Return a reference to the underlying bytes.

§Returns

A byte slice of the script contents.

Source

pub fn len(&self) -> usize

Return the length of the script in bytes.

§Returns

The number of bytes in the script.

Source

pub fn is_empty(&self) -> bool

Check if the script is empty (zero bytes).

§Returns

true if the script has no bytes.

Source

pub fn is_p2pkh(&self) -> bool

Check if this is a Pay-to-Public-Key-Hash (P2PKH) output script.

Pattern: OP_DUP OP_HASH160 <20 bytes> OP_EQUALVERIFY OP_CHECKSIG

§Returns

true if the script matches the P2PKH pattern.

Source

pub fn is_p2pk(&self) -> bool

Check if this is a Pay-to-Public-Key (P2PK) output script.

Pattern: <pubkey> OP_CHECKSIG (pubkey is 33 or 65 bytes with valid prefix).

§Returns

true if the script matches the P2PK pattern.

Source

pub fn is_p2sh(&self) -> bool

Check if this is a Pay-to-Script-Hash (P2SH) output script.

Pattern: OP_HASH160 <20 bytes> OP_EQUAL

§Returns

true if the script matches the P2SH pattern.

Source

pub fn is_data(&self) -> bool

Check if this is a data output script (OP_RETURN or OP_FALSE OP_RETURN).

§Returns

true if the script begins with OP_RETURN or OP_FALSE OP_RETURN.

Source

pub fn is_multisig_out(&self) -> bool

Check if this is a multisig output script.

Pattern: OP_N <pubkey1> <pubkey2> ... OP_M OP_CHECKMULTISIG

§Returns

true if the script matches the multisig output pattern.

Source

pub fn public_key_hash(&self) -> Result<Vec<u8>, ScriptError>

Extract the public key hash from a P2PKH script.

Returns the 20-byte hash160 if the script starts with OP_DUP OP_HASH160.

§Returns

The 20-byte public key hash, or an error if the script is not P2PKH.

Source

pub fn chunks(&self) -> Result<Vec<ScriptChunk>, ScriptError>

Parse the script into a vector of decoded chunks.

§Returns

A vector of ScriptChunk values, or an error if the script is malformed.

Source

pub fn append_push_data(&mut self, data: &[u8]) -> Result<(), ScriptError>

Append data bytes to the script with the proper PUSHDATA prefix.

Chooses the minimal encoding: direct push for 1-75 bytes, OP_PUSHDATA1 for 76-255, OP_PUSHDATA2 for 256-65535, etc.

§Arguments
  • data - The data bytes to push.
§Returns

Ok(()) on success, or an error if the data is too large.

Source

pub fn append_push_data_hex(&mut self, hex_str: &str) -> Result<(), ScriptError>

Append hex-encoded data to the script with proper PUSHDATA prefix.

§Arguments
  • hex_str - Hex string to decode and push.
§Returns

Ok(()) on success, or an error if the hex is invalid or data too large.

Source

pub fn append_opcodes(&mut self, opcodes: &[u8]) -> Result<(), ScriptError>

Append raw opcodes to the script.

Rejects push data opcodes (OP_DATA_1..OP_PUSHDATA4) to prevent misuse. Use append_push_data for those.

§Arguments
  • opcodes - Slice of opcode bytes to append.
§Returns

Ok(()) on success, or an error if a push data opcode is encountered.

Source

pub fn equals(&self, other: &Script) -> bool

Check if this script is byte-equal to another script.

§Arguments
  • other - The other script to compare with.
§Returns

true if both scripts have identical bytes.

Trait Implementations§

Source§

impl Clone for Script

Source§

fn clone(&self) -> Script

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. 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 Default for Script

Source§

fn default() -> Self

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

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

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

Display the script as a lowercase hex string.

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

Source§

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

Serialize this value into the given Serde serializer. Read more
Source§

impl Eq for Script

Source§

impl StructuralPartialEq for Script

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

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

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,