pub struct Script(_);
Expand description
Bitcoin script slice.
See also the bitcoin::blockdata::script
module.
Script
is a script slice, the most primitive script type. It’s usually seen in its borrowed
form &Script
. It is always encoded as a series of bytes representing the opcodes and data
pushes.
Validity
Script
does not have any validity invariants - it’s essentially just a marked slice of
bytes. This is similar to Path
vs OsStr
where they
are trivially cast-able to each-other and Path
doesn’t guarantee being a usable FS path but
having a newtype still has value because of added methods, readability and basic type checking.
Although at least data pushes could be checked not to overflow the script, bad scripts are allowed to be in a transaction (outputs just become unspendable) and there even are such transactions in the chain. Thus we must allow such scripts to be placed in the transaction.
Slicing safety
Slicing is similar to how str
works: some ranges may be incorrect and indexing by
usize
is not supported. However, as opposed to std
, we have no way of checking
correctness without causing linear complexity so there are no panics on invalid
ranges! If you supply an invalid range, you’ll get a garbled script.
The range is considered valid if it’s at a boundary of instruction. Care must be taken especially with push operations because you could get a reference to arbitrary attacker-supplied bytes that look like a valid script.
It is recommended to use .instructions()
method to get an iterator over script
instructions and work with that instead.
Memory safety
The type is #[repr(transparent)]
for internal purposes only!
No consumer crate may rely on the represenation of the struct!
References
Bitcoin Core References
Implementations§
§impl Script
impl Script
pub fn from_bytes(bytes: &[u8]) -> &Script
pub fn from_bytes(bytes: &[u8]) -> &Script
Treat byte slice as Script
pub fn from_bytes_mut(bytes: &mut [u8]) -> &mut Script
pub fn from_bytes_mut(bytes: &mut [u8]) -> &mut Script
Treat mutable byte slice as Script
pub fn as_mut_bytes(&mut self) -> &mut [u8] ⓘ
pub fn as_mut_bytes(&mut self) -> &mut [u8] ⓘ
Returns the script data as a mutable byte slice.
pub fn script_hash(&self) -> ScriptHash
pub fn script_hash(&self) -> ScriptHash
Returns 160-bit hash of the script.
pub fn wscript_hash(&self) -> WScriptHash
pub fn wscript_hash(&self) -> WScriptHash
Returns 256-bit hash of the script for P2WSH outputs.
pub fn tapscript_leaf_hash(&self) -> TapLeafHash
pub fn tapscript_leaf_hash(&self) -> TapLeafHash
Computes leaf hash of tapscript.
pub fn to_v0_p2wsh(&self) -> ScriptBuf
pub fn to_v0_p2wsh(&self) -> ScriptBuf
Computes the P2WSH output corresponding to this witnessScript (aka the “witness redeem script”).
pub fn to_v1_p2tr<C>(
&self,
secp: &Secp256k1<C>,
internal_key: XOnlyPublicKey
) -> ScriptBufwhere
C: Verification,
pub fn to_v1_p2tr<C>( &self, secp: &Secp256k1<C>, internal_key: XOnlyPublicKey ) -> ScriptBufwhere 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.
pub fn witness_version(&self) -> Option<WitnessVersion>
pub fn witness_version(&self) -> Option<WitnessVersion>
Returns witness version of the script, if any, assuming the script is a scriptPubkey
.
pub fn is_p2pk(&self) -> bool
pub fn is_p2pk(&self) -> bool
Checks whether a script pubkey is a P2PK output.
You can obtain the public key, if its valid,
by calling p2pk_public_key()
pub fn p2pk_public_key(&self) -> Option<PublicKey>
pub fn p2pk_public_key(&self) -> Option<PublicKey>
Returns the public key if this script is P2PK with a valid public key.
This may return None
even when is_p2pk()
returns true.
This happens when the public key is invalid (e.g. the point not being on the curve).
It also implies the script is unspendable.
pub fn is_witness_program(&self) -> bool
pub fn is_witness_program(&self) -> bool
Checks whether a script pubkey is a Segregated Witness (segwit) program.
pub fn is_v0_p2wsh(&self) -> bool
pub fn is_v0_p2wsh(&self) -> bool
Checks whether a script pubkey is a P2WSH output.
pub fn is_v0_p2wpkh(&self) -> bool
pub fn is_v0_p2wpkh(&self) -> bool
Checks whether a script pubkey is a P2WPKH output.
pub fn is_v1_p2tr(&self) -> bool
pub fn is_v1_p2tr(&self) -> bool
Checks whether a script pubkey is a P2TR output.
pub fn is_op_return(&self) -> bool
pub fn is_op_return(&self) -> bool
Check if this is an OP_RETURN output.
pub fn is_provably_unspendable(&self) -> bool
pub fn is_provably_unspendable(&self) -> bool
Checks whether a script can be proven to have no satisfying input.
pub fn dust_value(&self) -> Amount
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.
pub fn instructions(&self) -> Instructions<'_> ⓘ
pub fn instructions(&self) -> Instructions<'_> ⓘ
Iterates over the script instructions.
Each returned item is a nested 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 call the bytes
method.
To force minimal pushes, use instructions_minimal
.
pub fn instructions_minimal(&self) -> Instructions<'_> ⓘ
pub fn instructions_minimal(&self) -> Instructions<'_> ⓘ
Iterates over the script instructions while enforcing minimal pushes.
This is similar to instructions
but an error is returned if a push
is not minimal.
pub fn instruction_indices(&self) -> InstructionIndices<'_> ⓘ
pub fn instruction_indices(&self) -> InstructionIndices<'_> ⓘ
Iterates over the script instructions and their indices.
Unless the script contains an error, the returned item consists of an index pointing to the position in the script where the instruction begins and the decoded instruction - either an opcode or data push.
To force minimal pushes, use Self::instruction_indices_minimal
.
pub fn instruction_indices_minimal(&self) -> InstructionIndices<'_> ⓘ
pub fn instruction_indices_minimal(&self) -> InstructionIndices<'_> ⓘ
Iterates over the script instructions and their indices while enforcing minimal pushes.
This is similar to instruction_indices
but an error is
returned if a push is not minimal.
pub fn fmt_asm(&self, f: &mut dyn Write) -> Result<(), Error>
pub fn fmt_asm(&self, f: &mut dyn Write) -> Result<(), Error>
Writes the assembly decoding of the script to the formatter.
pub fn to_asm_string(&self) -> String
pub fn to_asm_string(&self) -> String
Returns the assembly decoding of the script.
pub fn to_hex_string(&self) -> String
pub fn to_hex_string(&self) -> String
Formats the script as lower-case hex.
This is a more convenient and performant way to write format!("{:x}", script)
.
For better performance you should generally prefer displaying the script but if String
is
required (this is common in tests) this method is can be used.
pub fn first_opcode(&self) -> Option<All>
pub fn first_opcode(&self) -> Option<All>
Returns the first opcode of the script (if there is any).
pub fn into_script_buf(self: Box<Script, Global>) -> ScriptBuf
pub fn into_script_buf(self: Box<Script, Global>) -> ScriptBuf
Converts a Box<Script>
into a [ScriptBuf
] without copying or allocating.
Trait Implementations§
§impl BorrowMut<Script> for ScriptBuf
impl BorrowMut<Script> for ScriptBuf
§fn borrow_mut(&mut self) -> &mut Script
fn borrow_mut(&mut self) -> &mut Script
§impl<'de> Deserialize<'de> for &'de Script
impl<'de> Deserialize<'de> for &'de Script
Can only deserialize borrowed bytes.
§fn deserialize<D>(
deserializer: D
) -> Result<&'de Script, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>( deserializer: D ) -> Result<&'de Script, <D as Deserializer<'de>>::Error>where D: Deserializer<'de>,
§impl<'a> From<&'a Script> for Arc<Script>
impl<'a> From<&'a Script> for Arc<Script>
Note: This will fail to compile on old Rust for targets that don’t support atomics
§impl From<&Script> for ScriptHash
impl From<&Script> for ScriptHash
§fn from(script: &Script) -> ScriptHash
fn from(script: &Script) -> ScriptHash
§impl From<&Script> for WScriptHash
impl From<&Script> for WScriptHash
§fn from(script: &Script) -> WScriptHash
fn from(script: &Script) -> WScriptHash
§impl Index<(Bound<usize>, Bound<usize>)> for Script
impl Index<(Bound<usize>, Bound<usize>)> for Script
Script subslicing operation - read slicing safety!
§impl Index<RangeFull> for Script
impl Index<RangeFull> for Script
Script subslicing operation - read slicing safety!
§impl Index<RangeInclusive<usize>> for Script
impl Index<RangeInclusive<usize>> for Script
Script subslicing operation - read slicing safety!
§impl Index<RangeToInclusive<usize>> for Script
impl Index<RangeToInclusive<usize>> for Script
Script subslicing operation - read slicing safety!
§impl PartialOrd<Script> for Script
impl PartialOrd<Script> for Script
§fn partial_cmp(&self, other: &Script) -> Option<Ordering>
fn partial_cmp(&self, other: &Script) -> Option<Ordering>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more§impl PartialOrd<Script> for ScriptBuf
impl PartialOrd<Script> for ScriptBuf
§fn partial_cmp(&self, other: &Script) -> Option<Ordering>
fn partial_cmp(&self, other: &Script) -> Option<Ordering>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more§impl PartialOrd<ScriptBuf> for Script
impl PartialOrd<ScriptBuf> for Script
§fn partial_cmp(&self, other: &ScriptBuf) -> Option<Ordering>
fn partial_cmp(&self, other: &ScriptBuf) -> Option<Ordering>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more§impl Serialize for Script
impl Serialize for Script
§fn serialize<S>(
&self,
serializer: S
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>( &self, serializer: S ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where S: Serializer,
User-facing serialization for Script
.
§impl ToOwned for Script
impl ToOwned for Script
impl Eq for Script
impl StructuralEq for Script
impl StructuralPartialEq for Script
Auto Trait Implementations§
impl RefUnwindSafe for Script
impl Send for Script
impl !Sized for Script
impl Sync for Script
impl Unpin for Script
impl UnwindSafe for Script
Blanket Implementations§
§impl<T, U> AsByteSlice<T> for Uwhere
T: ToByteSlice,
U: AsRef<[T]> + ?Sized,
impl<T, U> AsByteSlice<T> for Uwhere T: ToByteSlice, U: AsRef<[T]> + ?Sized,
fn as_byte_slice(&self) -> &[u8] ⓘ
§impl<T, U> AsMutByteSlice<T> for Uwhere
T: ToMutByteSlice,
U: AsMut<[T]> + ?Sized,
impl<T, U> AsMutByteSlice<T> for Uwhere T: ToMutByteSlice, U: AsMut<[T]> + ?Sized,
fn as_mut_byte_slice(&mut self) -> &mut [u8] ⓘ
§impl<U> AsMutSliceOf for Uwhere
U: AsMut<[u8]> + ?Sized,
impl<U> AsMutSliceOf for Uwhere U: AsMut<[u8]> + ?Sized,
fn as_mut_slice_of<T>(&mut self) -> Result<&mut [T], Error>where T: FromByteSlice,
§impl<U> AsSliceOf for Uwhere
U: AsRef<[u8]> + ?Sized,
impl<U> AsSliceOf for Uwhere U: AsRef<[u8]> + ?Sized,
fn as_slice_of<T>(&self) -> Result<&[T], Error>where T: FromByteSlice,
source§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere Self: Sized,
§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere
Self: Borrow<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere Self: Borrow<B>, B: 'a + ?Sized, R: 'a,
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R
) -> Rwhere
Self: BorrowMut<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R ) -> Rwhere Self: BorrowMut<B>, B: 'a + ?Sized, R: 'a,
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere
Self: AsRef<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere Self: AsRef<U>, U: 'a + ?Sized, R: 'a,
self
, then passes self.as_ref()
into the pipe function.§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> Rwhere
Self: AsMut<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> Rwhere Self: AsMut<U>, U: 'a + ?Sized, R: 'a,
self
, then passes self.as_mut()
into the pipe
function.