Struct 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

👎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>( secp: &Secp256k1<C>, internal_key: XOnlyPublicKey, merkle_root: Option<TapBranchHash>, ) -> Script
where C: Verification,

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>( &self, secp: &Secp256k1<C>, internal_key: XOnlyPublicKey, ) -> Script
where C: Verification,

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 bytes_to_asm_fmt(script: &[u8], f: &mut dyn Write) -> Result<(), Error>

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

Source

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

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<(), Error>

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

impl Decodable for Script

Source§

fn consensus_decode_from_finite_reader<D>(d: D) -> Result<Script, Error>
where D: Read,

Decode Self from a size-limited reader. Read more
Source§

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

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

Source§

fn deserialize<D>( deserializer: D, ) -> Result<Script, <D as Deserializer<'de>>::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<Script, Error>

Deserialize a value from raw data.
Source§

impl Display for Script

Source§

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

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

impl Encodable for Script

Source§

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

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

Source§

type Err = Error

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

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

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

impl Hash for Script

Source§

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

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

Source§

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

The returned type after indexing.
Source§

fn index(&self, index: I) -> &<Script as Index<I>>::Output

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

impl LowerHex for Script

Source§

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

Formats the value using the given formatter. Read more
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,

Restrict a value to a certain interval. Read more
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 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

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

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

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

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<S>( &self, serializer: S, ) -> Result<<S as Serializer>::Ok, <S as Serializer>::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<(), Error>

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

impl Eq for Script

Source§

impl StructuralPartialEq for Script

Auto Trait Implementations§

§

impl Freeze for Script

§

impl RefUnwindSafe for Script

§

impl Send for Script

§

impl Sync for Script

§

impl Unpin 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> 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]>,

Source§

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

Source§

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

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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,

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<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,