Use Script::as_bytes instead of inner when indexing

In preparation for moving the `Script` type to `primitives` stop
accessing the inner field before doing slice operations, use `as_bytes`
to first get at the slice.
This commit is contained in:
Tobin C. Harding 2024-08-21 11:14:28 +10:00
parent b0675a4a4f
commit 8b82363d97
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
1 changed files with 17 additions and 17 deletions

View File

@ -180,8 +180,8 @@ crate::internal_macros::define_extension_trait! {
return None; return None;
} }
let ver_opcode = Opcode::from(self.0[0]); // Version 0 or PUSHNUM_1-PUSHNUM_16 let ver_opcode = Opcode::from(self.as_bytes()[0]); // Version 0 or PUSHNUM_1-PUSHNUM_16
let push_opbyte = self.0[1]; // Second byte push opcode 2-40 bytes let push_opbyte = self.as_bytes()[1]; // Second byte push opcode 2-40 bytes
if push_opbyte < OP_PUSHBYTES_2.to_u8() || push_opbyte > OP_PUSHBYTES_40.to_u8() { if push_opbyte < OP_PUSHBYTES_2.to_u8() || push_opbyte > OP_PUSHBYTES_40.to_u8() {
return None; return None;
@ -198,20 +198,20 @@ crate::internal_macros::define_extension_trait! {
#[inline] #[inline]
fn is_p2sh(&self) -> bool { fn is_p2sh(&self) -> bool {
self.len() == 23 self.len() == 23
&& self.0[0] == OP_HASH160.to_u8() && self.as_bytes()[0] == OP_HASH160.to_u8()
&& self.0[1] == OP_PUSHBYTES_20.to_u8() && self.as_bytes()[1] == OP_PUSHBYTES_20.to_u8()
&& self.0[22] == OP_EQUAL.to_u8() && self.as_bytes()[22] == OP_EQUAL.to_u8()
} }
/// Checks whether a script pubkey is a P2PKH output. /// Checks whether a script pubkey is a P2PKH output.
#[inline] #[inline]
fn is_p2pkh(&self) -> bool { fn is_p2pkh(&self) -> bool {
self.len() == 25 self.len() == 25
&& self.0[0] == OP_DUP.to_u8() && self.as_bytes()[0] == OP_DUP.to_u8()
&& self.0[1] == OP_HASH160.to_u8() && self.as_bytes()[1] == OP_HASH160.to_u8()
&& self.0[2] == OP_PUSHBYTES_20.to_u8() && self.as_bytes()[2] == OP_PUSHBYTES_20.to_u8()
&& self.0[23] == OP_EQUALVERIFY.to_u8() && self.as_bytes()[23] == OP_EQUALVERIFY.to_u8()
&& self.0[24] == OP_CHECKSIG.to_u8() && self.as_bytes()[24] == OP_CHECKSIG.to_u8()
} }
/// Checks whether a script is push only. /// Checks whether a script is push only.
@ -295,7 +295,7 @@ crate::internal_macros::define_extension_trait! {
fn is_p2wsh(&self) -> bool { fn is_p2wsh(&self) -> bool {
self.len() == 34 self.len() == 34
&& self.witness_version() == Some(WitnessVersion::V0) && self.witness_version() == Some(WitnessVersion::V0)
&& self.0[1] == OP_PUSHBYTES_32.to_u8() && self.as_bytes()[1] == OP_PUSHBYTES_32.to_u8()
} }
/// Checks whether a script pubkey is a P2WPKH output. /// Checks whether a script pubkey is a P2WPKH output.
@ -303,7 +303,7 @@ crate::internal_macros::define_extension_trait! {
fn is_p2wpkh(&self) -> bool { fn is_p2wpkh(&self) -> bool {
self.len() == 22 self.len() == 22
&& self.witness_version() == Some(WitnessVersion::V0) && self.witness_version() == Some(WitnessVersion::V0)
&& self.0[1] == OP_PUSHBYTES_20.to_u8() && self.as_bytes()[1] == OP_PUSHBYTES_20.to_u8()
} }
/// Checks whether a script pubkey is a P2TR output. /// Checks whether a script pubkey is a P2TR output.
@ -311,7 +311,7 @@ crate::internal_macros::define_extension_trait! {
fn is_p2tr(&self) -> bool { fn is_p2tr(&self) -> bool {
self.len() == 34 self.len() == 34
&& self.witness_version() == Some(WitnessVersion::V1) && self.witness_version() == Some(WitnessVersion::V1)
&& self.0[1] == OP_PUSHBYTES_32.to_u8() && self.as_bytes()[1] == OP_PUSHBYTES_32.to_u8()
} }
/// Check if this is a consensus-valid OP_RETURN output. /// Check if this is a consensus-valid OP_RETURN output.
@ -320,7 +320,7 @@ crate::internal_macros::define_extension_trait! {
/// [`is_standard_op_return()`](Self::is_standard_op_return) instead. /// [`is_standard_op_return()`](Self::is_standard_op_return) instead.
#[inline] #[inline]
fn is_op_return(&self) -> bool { fn is_op_return(&self) -> bool {
match self.0.first() { match self.as_bytes().first() {
Some(b) => *b == OP_RETURN.to_u8(), Some(b) => *b == OP_RETURN.to_u8(),
None => false, None => false,
} }
@ -345,7 +345,7 @@ crate::internal_macros::define_extension_trait! {
fn is_provably_unspendable(&self) -> bool { fn is_provably_unspendable(&self) -> bool {
use crate::opcodes::Class::{IllegalOp, ReturnOp}; use crate::opcodes::Class::{IllegalOp, ReturnOp};
match self.0.first() { match self.as_bytes().first() {
Some(b) => { Some(b) => {
let first = Opcode::from(*b); let first = Opcode::from(*b);
let class = first.classify(opcodes::ClassifyContext::Legacy); let class = first.classify(opcodes::ClassifyContext::Legacy);
@ -447,7 +447,7 @@ crate::internal_macros::define_extension_trait! {
/// To force minimal pushes, use [`instructions_minimal`](Self::instructions_minimal). /// To force minimal pushes, use [`instructions_minimal`](Self::instructions_minimal).
#[inline] #[inline]
fn instructions(&self) -> Instructions { fn instructions(&self) -> Instructions {
Instructions { data: self.0.iter(), enforce_minimal: false } Instructions { data: self.as_bytes().iter(), enforce_minimal: false }
} }
/// Iterates over the script instructions while enforcing minimal pushes. /// Iterates over the script instructions while enforcing minimal pushes.
@ -456,7 +456,7 @@ crate::internal_macros::define_extension_trait! {
/// is not minimal. /// is not minimal.
#[inline] #[inline]
fn instructions_minimal(&self) -> Instructions { fn instructions_minimal(&self) -> Instructions {
Instructions { data: self.0.iter(), enforce_minimal: true } Instructions { data: self.as_bytes().iter(), enforce_minimal: true }
} }
/// Iterates over the script instructions and their indices. /// Iterates over the script instructions and their indices.