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

👎Deprecated since 0.28.0: use Script::new_v0_p2wpkh method instead

Generates P2WPKH-type of scriptPubkey.

source

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

Generates P2WPKH-type of scriptPubkey.

source

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

👎Deprecated since 0.28.0: use Script::new_v0_p2wsh method instead

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

source

pub fn new_v0_p2wsh(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<TapBranchHash> ) -> 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(version: WitnessVersion, 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 the 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

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 as_bytes(&self) -> &[u8]

Returns the script data as a byte slice.

source

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

Returns a copy of the script data.

source

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

Converts the script into a byte vector.

source

pub fn to_p2sh(&self) -> Script

Computes the P2SH output corresponding to this redeem script.

source

pub fn to_v0_p2wsh(&self) -> Script

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

source

pub fn to_v1_p2tr<C: Verification>( &self, secp: &Secp256k1<C>, internal_key: UntweakedPublicKey ) -> Script

Computes P2TR output with a given internal key and a single script spending path equal to the current script, assuming that the script is a Tapscript.

source

pub fn witness_version(&self) -> Option<WitnessVersion>

Returns witness version of the script, if any, assuming the script is a scriptPubkey.

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_v0_p2wpkh(&self) -> bool

Checks whether a script pubkey is a P2WPKH output.

source

pub fn is_v1_p2tr(&self) -> bool

Checks whether a script pubkey is a P2TR 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

Checks whether a script can be proven to have no satisfying input.

source

pub fn dust_value(&self) -> Amount

Returns the minimum value an output with this script should have in order to be broadcastable on today’s Bitcoin network.

source

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

Iterates 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 Self::instructions_minimal.

source

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

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

source

pub fn verify( &self, index: usize, amount: Amount, spending: &[u8] ) -> Result<(), Error>

Available on crate feature bitcoinconsensus only.
source

pub fn verify_with_flags<F: Into<u32>>( &self, index: usize, amount: Amount, spending: &[u8], flags: F ) -> Result<(), Error>

Available on crate feature bitcoinconsensus only.

Verifies spend of an input script.

Parameters
  • index - The input index in spending which is spending this transaction.
  • amount - The amount this script guards.
  • spending - The transaction that attempts to spend the output holding this script.
  • flags - Verification flags, see bitcoinconsensus::VERIFY_ALL and similar.
source

pub fn bytes_to_asm_fmt(script: &[u8], f: &mut dyn Write) -> Result

Writes the assembly decoding of the script bytes to the formatter.

source

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

Writes the assembly decoding of the script to the formatter.

source

pub fn bytes_to_asm(script: &[u8]) -> String

Creates an assembly decoding of the script in the given byte slice.

source

pub fn asm(&self) -> String

Returns the assembly decoding of the script.

Trait Implementations§

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 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<'de> Deserialize<'de> for Script

Available on crate feature serde only.
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 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. Returns the number of bytes written on success. Read more
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>

Produce an object from a byte iterator
source§

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

Produce 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<I> Index<I> for Script
where [u8]: Index<I>,

§

type Output = <[u8] as Index<I>>::Output

The returned type after indexing.
source§

fn index(&self, index: I) -> &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.
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

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 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 StructuralEq 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> Base32Len for T
where T: AsRef<[u8]>,

source§

fn base32_len(&self) -> usize

Calculate the base32 serialized length
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<'f, T> CheckBase32<Vec<u5>> for T
where T: AsRef<[u8]>,

§

type Err = Error

Error type if conversion fails
source§

fn check_base32(self) -> Result<Vec<u5>, <T as CheckBase32<Vec<u5>>>::Err>

Check if all values are in range and return array-like struct of u5 values
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> ToBase32 for T
where T: AsRef<[u8]>,

source§

fn write_base32<W>(&self, writer: &mut W) -> Result<(), <W as WriteBase32>::Err>
where W: WriteBase32,

Encode as base32 and write it to the supplied writer Implementations shouldn’t allocate.
source§

fn to_base32(&self) -> Vec<u5>

Convert Self to base32 vector
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

source§

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