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
impl Script
Sourcepub fn from_chunks(chunks: Vec<ScriptChunk>) -> Self
pub fn from_chunks(chunks: Vec<ScriptChunk>) -> Self
Creates a script from a vector of chunks.
Sourcepub fn from_asm(asm: &str) -> Result<Self>
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();Sourcepub fn from_hex(hex: &str) -> Result<Self>
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();Sourcepub fn from_binary(bin: &[u8]) -> Result<Self>
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();Sourcepub fn truncated_push(&self) -> Option<usize>
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.
Sourcepub fn write_script(&mut self, script: &Script) -> &mut Self
pub fn write_script(&mut self, script: &Script) -> &mut Self
Appends another script to this script.
Sourcepub fn write_opcode(&mut self, op: u8) -> &mut Self
pub fn write_opcode(&mut self, op: u8) -> &mut Self
Appends an opcode to the script.
Sourcepub fn write_bin(&mut self, bin: &[u8]) -> &mut Self
pub fn write_bin(&mut self, bin: &[u8]) -> &mut Self
Appends binary data to the script, determining the appropriate opcode based on length.
Sourcepub fn write_number(&mut self, num: i64) -> &mut Self
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.
Sourcepub fn remove_codeseparators(&mut self) -> &mut Self
pub fn remove_codeseparators(&mut self) -> &mut Self
Removes all OP_CODESEPARATOR opcodes from the script.
Sourcepub fn find_and_delete(&mut self, script: &Script) -> &mut Self
pub fn find_and_delete(&mut self, script: &Script) -> &mut Self
Deletes the given script wherever it appears in the current script.
Sourcepub fn is_push_only(&self) -> bool
pub fn is_push_only(&self) -> bool
Checks if the script contains only push data operations.
Sourcepub fn chunks(&self) -> Vec<ScriptChunk>
pub fn chunks(&self) -> Vec<ScriptChunk>
Returns the chunks that make up this script.
Sourcepub fn set_chunk_opcode(&mut self, index: usize, op: u8) -> &mut Self
pub fn set_chunk_opcode(&mut self, index: usize, op: u8) -> &mut Self
Sets a specific chunk’s opcode at the given index.
Sourcepub fn is_locking_script(&self) -> bool
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.
Sourcepub fn is_unlocking_script(&self) -> bool
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.
Sourcepub fn is_p2pkh(&self) -> bool
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
Sourcepub fn is_p2pk(&self) -> bool
pub fn is_p2pk(&self) -> bool
Check if this is a Pay-to-Public-Key script Pattern: <33 or 65 bytes pubkey> OP_CHECKSIG
Sourcepub fn is_p2sh(&self) -> bool
pub fn is_p2sh(&self) -> bool
Check if this is a Pay-to-Script-Hash script Pattern: OP_HASH160 <20 bytes> OP_EQUAL
Sourcepub fn is_data(&self) -> bool
pub fn is_data(&self) -> bool
Check if this is a data-only script (OP_RETURN or OP_FALSE OP_RETURN)
Sourcepub fn is_safe_data_carrier(&self) -> bool
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.
Sourcepub fn is_multisig(&self) -> Option<(u8, u8)>
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
Sourcepub fn opcode_to_small_int(op: u8) -> Option<u8>
pub fn opcode_to_small_int(op: u8) -> Option<u8>
Convert OP_1 through OP_16 to their numeric values
Sourcepub fn extract_pubkey_hash(&self) -> Option<[u8; 20]>
pub fn extract_pubkey_hash(&self) -> Option<[u8; 20]>
Extract the public key hash from a P2PKH script
Sourcepub fn get_public_key(&self) -> Option<Vec<u8>>
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.
Sourcepub fn get_public_key_hex(&self) -> Option<String>
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.