use core::convert::{TryFrom, TryInto};
use core::fmt;
#[cfg(rust_v_1_53)]
use core::ops::Bound;
use core::ops::{Index, Range, RangeFull, RangeFrom, RangeTo, RangeInclusive, RangeToInclusive};
use secp256k1::{Secp256k1, Verification};
use crate::address::WitnessVersion;
use crate::blockdata::opcodes::{self, all::*};
use crate::blockdata::script::{bytes_to_asm_fmt, Builder, Instruction, Instructions, InstructionIndices, ScriptBuf};
#[cfg(feature = "bitcoinconsensus")]
use crate::blockdata::script::Error;
use crate::consensus::Encodable;
use crate::hash_types::{ScriptHash, WScriptHash};
use crate::hashes::Hash;
use crate::key::{PublicKey, UntweakedPublicKey};
use crate::policy::DUST_RELAY_TX_FEE;
use crate::prelude::*;
use crate::taproot::{LeafVersion, TapNodeHash, TapLeafHash};
#[derive(PartialOrd, Ord, PartialEq, Eq, Hash)]
#[repr(transparent)]
pub struct Script(pub (in crate::blockdata::script) [u8]);
impl ToOwned for Script {
type Owned = ScriptBuf;
fn to_owned(&self) -> Self::Owned {
ScriptBuf(self.0.to_owned())
}
}
impl Script {
#[inline]
pub fn from_bytes(bytes: &[u8]) -> &Script {
unsafe {
&*(bytes as *const [u8] as *const Script)
}
}
#[inline]
pub fn from_bytes_mut(bytes: &mut [u8]) -> &mut Script {
unsafe {
&mut *(bytes as *mut [u8] as *mut Script)
}
}
#[inline]
pub fn as_bytes(&self) -> &[u8] {
&self.0
}
#[inline]
pub fn as_mut_bytes(&mut self) -> &mut [u8] {
&mut self.0
}
#[inline]
pub fn empty() -> &'static Script { Script::from_bytes(&[]) }
pub fn builder() -> Builder {
Builder::new()
}
#[inline]
pub fn script_hash(&self) -> ScriptHash {
ScriptHash::hash(self.as_bytes())
}
#[inline]
pub fn wscript_hash(&self) -> WScriptHash {
WScriptHash::hash(self.as_bytes())
}
#[inline]
pub fn tapscript_leaf_hash(&self) -> TapLeafHash {
TapLeafHash::from_script(self, LeafVersion::TapScript)
}
#[inline]
pub fn len(&self) -> usize { self.0.len() }
#[inline]
pub fn is_empty(&self) -> bool { self.0.is_empty() }
#[inline]
pub fn to_bytes(&self) -> Vec<u8> { self.0.to_owned() }
#[inline]
pub fn bytes(&self) -> Bytes<'_> {
Bytes(self.as_bytes().iter().copied())
}
#[inline]
pub fn to_v0_p2wsh(&self) -> ScriptBuf {
ScriptBuf::new_v0_p2wsh(&self.wscript_hash())
}
#[inline]
pub fn to_v1_p2tr<C: Verification>(&self, secp: &Secp256k1<C>, internal_key: UntweakedPublicKey) -> ScriptBuf {
let leaf_hash = self.tapscript_leaf_hash();
let merkle_root = TapNodeHash::from(leaf_hash);
ScriptBuf::new_v1_p2tr(secp, internal_key, Some(merkle_root))
}
#[inline]
pub fn witness_version(&self) -> Option<WitnessVersion> {
self.0.first().and_then(|opcode| WitnessVersion::try_from(opcodes::All::from(*opcode)).ok())
}
#[inline]
pub fn is_p2sh(&self) -> bool {
self.0.len() == 23
&& self.0[0] == OP_HASH160.to_u8()
&& self.0[1] == OP_PUSHBYTES_20.to_u8()
&& self.0[22] == OP_EQUAL.to_u8()
}
#[inline]
pub fn is_p2pkh(&self) -> bool {
self.0.len() == 25
&& self.0[0] == OP_DUP.to_u8()
&& self.0[1] == OP_HASH160.to_u8()
&& self.0[2] == OP_PUSHBYTES_20.to_u8()
&& self.0[23] == OP_EQUALVERIFY.to_u8()
&& self.0[24] == OP_CHECKSIG.to_u8()
}
#[inline]
pub fn is_p2pk(&self) -> bool {
self.p2pk_pubkey_bytes().is_some()
}
#[inline]
pub fn p2pk_public_key(&self) -> Option<PublicKey> {
PublicKey::from_slice(self.p2pk_pubkey_bytes()?).ok()
}
#[inline]
pub(in crate::blockdata::script) fn p2pk_pubkey_bytes(&self) -> Option<&[u8]> {
match self.len() {
67 if self.0[0] == OP_PUSHBYTES_65.to_u8()
&& self.0[66] == OP_CHECKSIG.to_u8() => {
Some(&self.0[1..66])
}
35 if self.0[0] == OP_PUSHBYTES_33.to_u8()
&& self.0[34] == OP_CHECKSIG.to_u8() => {
Some(&self.0[1..34])
}
_ => None
}
}
#[inline]
pub fn is_witness_program(&self) -> bool {
let script_len = self.0.len();
if !(4..=42).contains(&script_len) {
return false
}
let ver_opcode = opcodes::All::from(self.0[0]); let push_opbyte = self.0[1]; WitnessVersion::try_from(ver_opcode).is_ok()
&& push_opbyte >= OP_PUSHBYTES_2.to_u8()
&& push_opbyte <= OP_PUSHBYTES_40.to_u8()
&& script_len - 2 == push_opbyte as usize
}
#[inline]
pub fn is_v0_p2wsh(&self) -> bool {
self.0.len() == 34
&& self.witness_version() == Some(WitnessVersion::V0)
&& self.0[1] == OP_PUSHBYTES_32.to_u8()
}
#[inline]
pub fn is_v0_p2wpkh(&self) -> bool {
self.0.len() == 22
&& self.witness_version() == Some(WitnessVersion::V0)
&& self.0[1] == OP_PUSHBYTES_20.to_u8()
}
pub(crate) fn v0_p2wpkh(&self) -> Option<&[u8; 20]> {
if self.is_v0_p2wpkh() {
Some(self.0[2..].try_into().expect("is_v0_p2wpkh checks the length"))
} else {
None
}
}
#[inline]
pub fn is_v1_p2tr(&self) -> bool {
self.0.len() == 34
&& self.witness_version() == Some(WitnessVersion::V1)
&& self.0[1] == OP_PUSHBYTES_32.to_u8()
}
#[inline]
pub fn is_op_return (&self) -> bool {
match self.0.first() {
Some(b) => *b == OP_RETURN.to_u8(),
None => false
}
}
#[inline]
pub fn is_provably_unspendable(&self) -> bool {
use crate::blockdata::opcodes::Class::{ReturnOp, IllegalOp};
match self.0.first() {
Some(b) => {
let first = opcodes::All::from(*b);
let class = first.classify(opcodes::ClassifyContext::Legacy);
class == ReturnOp || class == IllegalOp
},
None => false,
}
}
pub fn dust_value(&self) -> crate::Amount {
let sats = DUST_RELAY_TX_FEE as u64 / 1000 * if self.is_op_return() {
0
} else if self.is_witness_program() {
32 + 4 + 1 + (107 / 4) + 4 + 8 + self.consensus_encode(&mut sink()).expect("sinks don't error") as u64 } else {
32 + 4 + 1 + 107 + 4 + 8 + self.consensus_encode(&mut sink()).expect("sinks don't error") as u64 };
crate::Amount::from_sat(sats)
}
#[inline]
pub fn instructions(&self) -> Instructions<'_> {
Instructions {
data: self.0.iter(),
enforce_minimal: false,
}
}
#[inline]
pub fn instructions_minimal(&self) -> Instructions<'_> {
Instructions {
data: self.0.iter(),
enforce_minimal: true,
}
}
#[inline]
pub fn instruction_indices(&self) -> InstructionIndices<'_> {
InstructionIndices::from_instructions(self.instructions())
}
#[inline]
pub fn instruction_indices_minimal(&self) -> InstructionIndices<'_> {
InstructionIndices::from_instructions(self.instructions_minimal())
}
#[cfg(feature="bitcoinconsensus")]
#[cfg_attr(docsrs, doc(cfg(feature = "bitcoinconsensus")))]
pub fn verify (&self, index: usize, amount: crate::Amount, spending_tx: &[u8]) -> Result<(), Error> {
self.verify_with_flags(index, amount, spending_tx, bitcoinconsensus::VERIFY_ALL)
}
#[cfg(feature="bitcoinconsensus")]
#[cfg_attr(docsrs, doc(cfg(feature = "bitcoinconsensus")))]
pub fn verify_with_flags<F: Into<u32>>(&self, index: usize, amount: crate::Amount, spending_tx: &[u8], flags: F) -> Result<(), Error> {
Ok(bitcoinconsensus::verify_with_flags (&self.0[..], amount.to_sat(), spending_tx, index, flags.into())?)
}
pub fn fmt_asm(&self, f: &mut dyn fmt::Write) -> fmt::Result {
bytes_to_asm_fmt(self.as_ref(), f)
}
pub fn to_asm_string(&self) -> String {
let mut buf = String::new();
self.fmt_asm(&mut buf).unwrap();
buf
}
pub fn to_hex_string(&self) -> String {
self.as_bytes().to_lower_hex_string()
}
pub fn first_opcode(&self) -> Option<opcodes::All> {
self.as_bytes().first().copied().map(From::from)
}
pub(in crate::blockdata::script) fn last_opcode(&self) -> Option<opcodes::All> {
match self.instructions().last() {
Some(Ok(Instruction::Op(op))) => Some(op),
_ => None,
}
}
#[must_use = "`self` will be dropped if the result is not used"]
pub fn into_script_buf(self: Box<Self>) -> ScriptBuf {
let rw = Box::into_raw(self) as *mut [u8];
let inner = unsafe { Box::from_raw(rw) };
ScriptBuf(Vec::from(inner))
}
}
pub struct Bytes<'a>(core::iter::Copied<core::slice::Iter<'a, u8>>);
impl Iterator for Bytes<'_> {
type Item = u8;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.0.next()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
#[inline]
fn nth(&mut self, n: usize) -> Option<Self::Item> {
self.0.nth(n)
}
}
impl DoubleEndedIterator for Bytes<'_> {
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
self.0.next_back()
}
#[inline]
fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
self.0.nth_back(n)
}
}
impl ExactSizeIterator for Bytes<'_> {}
impl core::iter::FusedIterator for Bytes<'_> {}
macro_rules! delegate_index {
($($type:ty),* $(,)?) => {
$(
impl Index<$type> for Script {
type Output = Self;
#[inline]
fn index(&self, index: $type) -> &Self::Output {
Self::from_bytes(&self.0[index])
}
}
)*
}
}
delegate_index!(Range<usize>, RangeFrom<usize>, RangeTo<usize>, RangeFull, RangeInclusive<usize>, RangeToInclusive<usize>);
#[cfg(rust_v_1_53)]
#[cfg_attr(docsrs, doc(cfg(rust_v_1_53)))]
delegate_index!((Bound<usize>, Bound<usize>));