Struct elements::script::Script

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

A Bitcoin script

Implementations§

source§

impl Script

source

pub fn new() -> Script

Creates a new empty script

source

pub fn new_p2pk(pubkey: &PublicKey) -> Script

Generates P2PK-type of scriptPubkey

source

pub fn new_p2pkh(pubkey_hash: &PubkeyHash) -> Script

Generates P2PKH-type of scriptPubkey

source

pub fn new_p2sh(script_hash: &ScriptHash) -> Script

Generates P2SH-type of scriptPubkey with a given hash of the redeem script

source

pub fn new_v0_wpkh(pubkey_hash: &WPubkeyHash) -> Script

Generates P2WPKH-type of scriptPubkey

source

pub fn new_v0_wsh(script_hash: &WScriptHash) -> Script

Generates P2WSH-type of scriptPubkey with a given hash of the redeem script

source

pub fn new_v1_p2tr<C: Verification>( secp: &Secp256k1<C>, internal_key: UntweakedPublicKey, merkle_root: Option<TapNodeHash> ) -> Script

Generates P2TR for script spending path using an internal public key and some optional script tree merkle root.

source

pub fn new_v1_p2tr_tweaked(output_key: TweakedPublicKey) -> Script

Generates P2TR for key spending path for a known TweakedPublicKey.

source

pub fn new_witness_program(ver: Fe32, program: &[u8]) -> Script

Generates P2WSH-type of scriptPubkey with a given hash of the redeem script

source

pub fn new_op_return(data: &[u8]) -> Script

Generates OP_RETURN-type of scriptPubkey for a given data

source

pub fn script_hash(&self) -> ScriptHash

Returns 160-bit hash of the script

source

pub fn wscript_hash(&self) -> WScriptHash

Returns 256-bit hash of the script for P2WSH outputs

source

pub fn len(&self) -> usize

The length in bytes of the script

source

pub fn is_empty(&self) -> bool

Whether the script is the empty script

source

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

Returns the script data

source

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

Returns a copy of the script data

source

pub fn into_bytes(self) -> Vec<u8>

Convert the script into a byte vector

source

pub fn to_p2sh(&self) -> Script

Compute the P2SH output corresponding to this redeem script

source

pub fn to_v0_p2wsh(&self) -> Script

Compute the P2WSH output corresponding to this witnessScript (aka the “witness redeem script”)

source

pub fn is_p2sh(&self) -> bool

Checks whether a script pubkey is a p2sh output

source

pub fn is_p2pkh(&self) -> bool

Checks whether a script pubkey is a p2pkh output

source

pub fn is_p2pk(&self) -> bool

Checks whether a script pubkey is a p2pk output

source

pub fn is_witness_program(&self) -> bool

Checks whether a script pubkey is a Segregated Witness (segwit) program.

source

pub fn is_v0_p2wsh(&self) -> bool

Checks whether a script pubkey is a p2wsh output

source

pub fn is_v1_p2tr(&self) -> bool

Checks whether a script pubkey is a P2TR output.

source

pub fn is_v1plus_p2witprog(&self) -> bool

Checks whether a script pubkey is a p2wsh output

source

pub fn is_v0_p2wpkh(&self) -> bool

Checks whether a script pubkey is a p2wpkh output

source

pub fn is_op_return(&self) -> bool

Check if this is an OP_RETURN output

source

pub fn is_provably_unspendable(&self) -> bool

Whether a script can be proven to have no satisfying input In elements, is_provably_unspendable is consensus critical matches the implementation of CScript::IsUnspendable()

source

pub fn instructions(&self) -> Instructions<'_>

Iterate over the script in the form of Instructions, which are an enum covering opcodes, datapushes and errors. At most one error will be returned and then the iterator will end. To instead iterate over the script as sequence of bytes, treat it as a slice using script[..] or convert it to a vector using into_bytes().

To force minimal pushes, use Script::instructions_minimal.

source

pub fn instructions_minimal(&self) -> Instructions<'_>

Iterate over the script in the form of Instructions while enforcing minimal pushes.

source

pub fn fmt_asm(&self, f: &mut dyn Write) -> Result

Write the assembly decoding of the script to the formatter.

source

pub fn asm(&self) -> String

Get the assembly decoding of the script.

Trait Implementations§

source§

impl Clone for Script

source§

fn clone(&self) -> Script

Returns a copy 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 Decodable for Script

source§

fn consensus_decode<D: Read>(d: D) -> Result<Self, Error>

Decode an object with a well-defined format
source§

impl Default for Script

source§

fn default() -> Script

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

impl Deserialize for Script

source§

fn deserialize(bytes: &[u8]) -> Result<Self, Error>

Deserialize a value from raw data.
source§

impl Display for Script

source§

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

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

impl Encodable for Script

source§

fn consensus_encode<S: Write>(&self, s: S) -> Result<usize, Error>

Encode an object with a well-defined format, should only ever error if the underlying Write errors. Returns the number of bytes written on success
source§

impl From<Vec<u8>> for Script

Creates a new script from an existing vector

source§

fn from(v: Vec<u8>) -> Script

Converts to this type from the input type.
source§

impl FromHex for Script

source§

fn from_byte_iter<I>(iter: I) -> Result<Self, Error>

Produces an object from a byte iterator.
source§

fn from_hex(s: &str) -> Result<Self, Error>

Produces an object from a hex string.
source§

impl FromStr for Script

§

type Err = Error

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<Self, Error>

Parses a string s to return a value of this type. Read more
source§

impl Hash for Script

source§

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

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl Index<Range<usize>> for Script

§

type Output = [u8]

The returned type after indexing.
source§

fn index(&self, index: Range<usize>) -> &[u8]

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

impl Index<RangeFrom<usize>> for Script

§

type Output = [u8]

The returned type after indexing.
source§

fn index(&self, index: RangeFrom<usize>) -> &[u8]

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

impl Index<RangeFull> for Script

§

type Output = [u8]

The returned type after indexing.
source§

fn index(&self, _: RangeFull) -> &[u8]

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

impl Index<RangeTo<usize>> for Script

§

type Output = [u8]

The returned type after indexing.
source§

fn index(&self, index: RangeTo<usize>) -> &[u8]

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

impl Index<usize> for Script

§

type Output = u8

The returned type after indexing.
source§

fn index(&self, index: usize) -> &u8

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.
source§

impl Ord for Script

source§

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

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq for Script

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
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

This method 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

This method 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

This method 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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Serialize for Script

source§

fn serialize(&self) -> Vec<u8>

Serialize a value as raw data.
source§

impl UpperHex for Script

source§

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

Formats the value using the given formatter.
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> 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> ToHex for T
where T: LowerHex,

source§

fn to_hex(&self) -> String

Outputs the hash in hexadecimal form.

source§

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

§

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§

default 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>,

§

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

§

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

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

§

fn vzip(self) -> V