Skip to main content

Script

Struct Script 

Source
pub struct Script { /* private fields */ }
Expand description

The Script class represents a script in a Bitcoin SV transaction.

Scripts can be constructed from ASM strings, hex strings, or binary data, and can be serialized back to any of these formats.

Implementations§

Source§

impl Script

Source

pub fn new() -> Self

Creates a new empty script.

Source

pub fn from_chunks(chunks: Vec<ScriptChunk>) -> Self

Creates a script from a vector of chunks.

Source

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

Constructs a Script instance from an ASM (Assembly) formatted string.

§Example
use bsv_rs::script::Script;

let script = Script::from_asm("OP_DUP OP_HASH160 abcd1234 OP_EQUALVERIFY OP_CHECKSIG").unwrap();
Source

pub fn from_hex(hex: &str) -> Result<Self>

Constructs a Script instance from a hexadecimal string.

§Example
use bsv_rs::script::Script;

let script = Script::from_hex("76a914000000000000000000000000000000000000000088ac").unwrap();
Source

pub fn from_binary(bin: &[u8]) -> Result<Self>

Constructs a Script instance from binary data.

§Example
use bsv_rs::script::Script;

let script = Script::from_binary(&[0x76, 0xa9]).unwrap();
Source

pub fn truncated_push(&self) -> Option<usize>

Parses raw bytes into script chunks. The chunk index of the first push that declares more bytes than the script holds (a direct push, or a OP_PUSHDATA1/2/4 whose length bytes or data are cut short), or None when every push is complete.

The parser is lenient (a truncated push yields a shorter chunk, as the TypeScript SDK’s does), but the reference refuses to READ such an opcode: GetOp returns false (script.h:190-191) and the interpreter fails the script with SCRIPT_ERR_BAD_OPCODE when its walk reaches it, executed or not (interpreter.cpp:450-451). Spend applies that at this index. Bytes after a top-level OP_RETURN are data on both sides and are not examined.

Source

pub fn to_asm(&self) -> String

Serializes the script to an ASM formatted string.

Source

pub fn to_hex(&self) -> String

Serializes the script to a hexadecimal string.

Source

pub fn to_binary(&self) -> Vec<u8> ⓘ

Serializes the script to binary (byte array).

Source

pub fn write_script(&mut self, script: &Script) -> &mut Self

Appends another script to this script.

Source

pub fn write_opcode(&mut self, op: u8) -> &mut Self

Appends an opcode to the script.

Source

pub fn write_bin(&mut self, bin: &[u8]) -> &mut Self

Appends binary data to the script, determining the appropriate opcode based on length.

Source

pub fn write_number(&mut self, num: i64) -> &mut Self

Appends a number to the script.

Numbers 0, -1, and 1-16 use special opcodes. Larger numbers are encoded in little-endian sign-magnitude format.

Source

pub fn remove_codeseparators(&mut self) -> &mut Self

Removes all OP_CODESEPARATOR opcodes from the script.

Source

pub fn find_and_delete(&mut self, script: &Script) -> &mut Self

Deletes the given script wherever it appears in the current script.

Source

pub fn is_push_only(&self) -> bool

Checks if the script contains only push data operations.

Source

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

Returns the chunks that make up this script.

Source

pub fn len(&self) -> usize

Returns the number of chunks in this script.

Source

pub fn is_empty(&self) -> bool

Returns true if this script is empty.

Source

pub fn set_chunk_opcode(&mut self, index: usize, op: u8) -> &mut Self

Sets a specific chunk’s opcode at the given index.

Source

pub fn is_locking_script(&self) -> bool

Returns true if this script is a locking script. Base Script always returns false; use LockingScript for true.

Source

pub fn is_unlocking_script(&self) -> bool

Returns true if this script is an unlocking script. Base Script always returns false; use UnlockingScript for true.

Source

pub fn is_p2pkh(&self) -> bool

Check if this is a Pay-to-Public-Key-Hash script Pattern: OP_DUP OP_HASH160 <20 bytes> OP_EQUALVERIFY OP_CHECKSIG

Source

pub fn is_p2pk(&self) -> bool

Check if this is a Pay-to-Public-Key script Pattern: <33 or 65 bytes pubkey> OP_CHECKSIG

Source

pub fn is_p2sh(&self) -> bool

Check if this is a Pay-to-Script-Hash script Pattern: OP_HASH160 <20 bytes> OP_EQUAL

Source

pub fn is_data(&self) -> bool

Check if this is a data-only script (OP_RETURN or OP_FALSE OP_RETURN)

Source

pub fn is_safe_data_carrier(&self) -> bool

Check if this is a safe data carrier script (OP_FALSE OP_RETURN pattern).

A safe data carrier is provably unspendable and prunable. Unlike bare OP_RETURN, it won’t be rejected by nodes even with large data payloads.

Source

pub fn is_multisig(&self) -> Option<(u8, u8)>

Check if this is a multisig script, returns (M, N) if so Pattern: OP_M <pubkey>... OP_N OP_CHECKMULTISIG

Source

pub fn opcode_to_small_int(op: u8) -> Option<u8>

Convert OP_1 through OP_16 to their numeric values

Source

pub fn extract_pubkey_hash(&self) -> Option<[u8; 20]>

Extract the public key hash from a P2PKH script

Source

pub fn get_public_key(&self) -> Option<Vec<u8>>

Extracts the public key from a P2PK script.

Returns the raw public key bytes from the first chunk of a P2PK script (<pubkey> OP_CHECKSIG), or None if this is not a P2PK script.

Matches the Go SDK’s Script.PubKey() method.

Source

pub fn get_public_key_hex(&self) -> Option<String>

Extracts the public key from a P2PK script as a hex string.

Returns the hex-encoded public key from the first chunk of a P2PK script, or None if this is not a P2PK script.

Matches the Go SDK’s Script.PubKeyHex() method.

Trait Implementations§

Source§

impl Clone for Script

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · 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 Eq for Script

Source§

impl From<LockingScript> for Script

Source§

fn from(locking: LockingScript) -> Self

Converts to this type from the input type.
Source§

impl From<Script> for LockingScript

Source§

fn from(script: Script) -> Self

Converts to this type from the input type.
Source§

impl From<Script> for UnlockingScript

Source§

fn from(script: Script) -> Self

Converts to this type from the input type.
Source§

impl From<UnlockingScript> for Script

Source§

fn from(unlocking: UnlockingScript) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for Script

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more

Auto Trait Implementations§

§

impl !Freeze for Script

§

impl !RefUnwindSafe for Script

§

impl !Sync for Script

§

impl Send for Script

§

impl Unpin for Script

§

impl UnsafeUnpin 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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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