Replace impl blocks with extension traits
In preparation to move script types to `primitives` we replace impl block with extension traits by replacing the temporary modules with `define_extension_trait`.
This commit is contained in:
parent
b99bdcfdd6
commit
0857697665
|
@ -1,4 +1,5 @@
|
||||||
use bitcoin::address::script_pubkey::ScriptBufExt as _;
|
use bitcoin::address::script_pubkey::ScriptBufExt as _;
|
||||||
|
use bitcoin::script::ScriptExt as _;
|
||||||
use bitcoin::{
|
use bitcoin::{
|
||||||
consensus, ecdsa, sighash, Amount, CompressedPublicKey, Script, ScriptBuf, Transaction,
|
consensus, ecdsa, sighash, Amount, CompressedPublicKey, Script, ScriptBuf, Transaction,
|
||||||
};
|
};
|
||||||
|
|
|
@ -84,6 +84,7 @@ use bitcoin::consensus::encode;
|
||||||
use bitcoin::key::{TapTweak, XOnlyPublicKey};
|
use bitcoin::key::{TapTweak, XOnlyPublicKey};
|
||||||
use bitcoin::opcodes::all::{OP_CHECKSIG, OP_CLTV, OP_DROP};
|
use bitcoin::opcodes::all::{OP_CHECKSIG, OP_CLTV, OP_DROP};
|
||||||
use bitcoin::psbt::{self, Input, Output, Psbt, PsbtSighashType};
|
use bitcoin::psbt::{self, Input, Output, Psbt, PsbtSighashType};
|
||||||
|
use bitcoin::script::ScriptExt as _;
|
||||||
use bitcoin::secp256k1::Secp256k1;
|
use bitcoin::secp256k1::Secp256k1;
|
||||||
use bitcoin::sighash::{self, SighashCache, TapSighash, TapSighashType};
|
use bitcoin::sighash::{self, SighashCache, TapSighash, TapSighashType};
|
||||||
use bitcoin::taproot::{self, LeafVersion, TapLeafHash, TaprootBuilder, TaprootSpendInfo};
|
use bitcoin::taproot::{self, LeafVersion, TapLeafHash, TaprootBuilder, TaprootSpendInfo};
|
||||||
|
|
|
@ -51,7 +51,8 @@ use crate::prelude::{String, ToOwned};
|
||||||
use crate::script::witness_program::WitnessProgram;
|
use crate::script::witness_program::WitnessProgram;
|
||||||
use crate::script::witness_version::WitnessVersion;
|
use crate::script::witness_version::WitnessVersion;
|
||||||
use crate::script::{
|
use crate::script::{
|
||||||
self, RedeemScriptSizeError, Script, ScriptBuf, ScriptHash, WScriptHash, WitnessScriptSizeError,
|
self, RedeemScriptSizeError, Script, ScriptBuf, ScriptExt as _, ScriptHash, WScriptHash,
|
||||||
|
WitnessScriptSizeError,
|
||||||
};
|
};
|
||||||
use crate::taproot::TapNodeHash;
|
use crate::taproot::TapNodeHash;
|
||||||
|
|
||||||
|
|
|
@ -13,8 +13,8 @@ use crate::opcodes::all::*;
|
||||||
use crate::script::witness_program::WitnessProgram;
|
use crate::script::witness_program::WitnessProgram;
|
||||||
use crate::script::witness_version::WitnessVersion;
|
use crate::script::witness_version::WitnessVersion;
|
||||||
use crate::script::{
|
use crate::script::{
|
||||||
self, Builder, PushBytes, RedeemScriptSizeError, Script, ScriptBuf, ScriptHash, WScriptHash,
|
self, Builder, PushBytes, RedeemScriptSizeError, Script, ScriptBuf, ScriptExt as _, ScriptHash,
|
||||||
WitnessScriptSizeError,
|
WScriptHash, WitnessScriptSizeError,
|
||||||
};
|
};
|
||||||
use crate::taproot::TapNodeHash;
|
use crate::taproot::TapNodeHash;
|
||||||
|
|
||||||
|
|
|
@ -49,7 +49,7 @@ use crate::consensus::encode::VarInt;
|
||||||
use crate::consensus::{Decodable, Encodable};
|
use crate::consensus::{Decodable, Encodable};
|
||||||
use crate::internal_macros::impl_hashencode;
|
use crate::internal_macros::impl_hashencode;
|
||||||
use crate::prelude::{BTreeSet, Borrow, Vec};
|
use crate::prelude::{BTreeSet, Borrow, Vec};
|
||||||
use crate::script::Script;
|
use crate::script::{Script, ScriptExt as _};
|
||||||
use crate::transaction::OutPoint;
|
use crate::transaction::OutPoint;
|
||||||
|
|
||||||
/// Golomb encoding parameter as in BIP-158, see also https://gist.github.com/sipa/576d5f09c3b86c3b1b75598d799fc845
|
/// Golomb encoding parameter as in BIP-158, see also https://gist.github.com/sipa/576d5f09c3b86c3b1b75598d799fc845
|
||||||
|
|
|
@ -19,8 +19,9 @@ use crate::merkle_tree::{MerkleNode as _, TxMerkleNode, WitnessMerkleNode};
|
||||||
use crate::network::Params;
|
use crate::network::Params;
|
||||||
use crate::pow::{CompactTarget, Target, Work};
|
use crate::pow::{CompactTarget, Target, Work};
|
||||||
use crate::prelude::Vec;
|
use crate::prelude::Vec;
|
||||||
|
use crate::script::{self, ScriptExt as _};
|
||||||
use crate::transaction::{Transaction, Wtxid};
|
use crate::transaction::{Transaction, Wtxid};
|
||||||
use crate::{script, VarInt};
|
use crate::VarInt;
|
||||||
|
|
||||||
hashes::hash_newtype! {
|
hashes::hash_newtype! {
|
||||||
/// A bitcoin block hash.
|
/// A bitcoin block hash.
|
||||||
|
|
|
@ -135,31 +135,31 @@ impl Script {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mod tmp_pub {
|
crate::internal_macros::define_extension_trait! {
|
||||||
use super::*;
|
/// Extension functionality for the [`Script`] type.
|
||||||
impl Script {
|
pub trait ScriptExt impl for Script {
|
||||||
/// Returns an iterator over script bytes.
|
/// Returns an iterator over script bytes.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn bytes(&self) -> Bytes<'_> { Bytes(self.as_bytes().iter().copied()) }
|
fn bytes(&self) -> Bytes<'_> { Bytes(self.as_bytes().iter().copied()) }
|
||||||
|
|
||||||
/// Creates a new script builder
|
/// Creates a new script builder
|
||||||
pub fn builder() -> Builder { Builder::new() }
|
fn builder() -> Builder { Builder::new() }
|
||||||
|
|
||||||
/// Returns 160-bit hash of the script for P2SH outputs.
|
/// Returns 160-bit hash of the script for P2SH outputs.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn script_hash(&self) -> Result<ScriptHash, RedeemScriptSizeError> {
|
fn script_hash(&self) -> Result<ScriptHash, RedeemScriptSizeError> {
|
||||||
ScriptHash::from_script(self)
|
ScriptHash::from_script(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns 256-bit hash of the script for P2WSH outputs.
|
/// Returns 256-bit hash of the script for P2WSH outputs.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn wscript_hash(&self) -> Result<WScriptHash, WitnessScriptSizeError> {
|
fn wscript_hash(&self) -> Result<WScriptHash, WitnessScriptSizeError> {
|
||||||
WScriptHash::from_script(self)
|
WScriptHash::from_script(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Computes leaf hash of tapscript.
|
/// Computes leaf hash of tapscript.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn tapscript_leaf_hash(&self) -> TapLeafHash {
|
fn tapscript_leaf_hash(&self) -> TapLeafHash {
|
||||||
TapLeafHash::from_script(self, LeafVersion::TapScript)
|
TapLeafHash::from_script(self, LeafVersion::TapScript)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -174,7 +174,7 @@ mod tmp_pub {
|
||||||
/// > special meaning. The value of the first push is called the "version byte". The following
|
/// > special meaning. The value of the first push is called the "version byte". The following
|
||||||
/// > byte vector pushed is called the "witness program".
|
/// > byte vector pushed is called the "witness program".
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn witness_version(&self) -> Option<WitnessVersion> {
|
fn witness_version(&self) -> Option<WitnessVersion> {
|
||||||
let script_len = self.0.len();
|
let script_len = self.0.len();
|
||||||
if !(4..=42).contains(&script_len) {
|
if !(4..=42).contains(&script_len) {
|
||||||
return None;
|
return None;
|
||||||
|
@ -196,7 +196,7 @@ mod tmp_pub {
|
||||||
|
|
||||||
/// Checks whether a script pubkey is a P2SH output.
|
/// Checks whether a script pubkey is a P2SH output.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn is_p2sh(&self) -> bool {
|
fn is_p2sh(&self) -> bool {
|
||||||
self.0.len() == 23
|
self.0.len() == 23
|
||||||
&& self.0[0] == OP_HASH160.to_u8()
|
&& self.0[0] == OP_HASH160.to_u8()
|
||||||
&& self.0[1] == OP_PUSHBYTES_20.to_u8()
|
&& self.0[1] == OP_PUSHBYTES_20.to_u8()
|
||||||
|
@ -205,7 +205,7 @@ mod tmp_pub {
|
||||||
|
|
||||||
/// Checks whether a script pubkey is a P2PKH output.
|
/// Checks whether a script pubkey is a P2PKH output.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn is_p2pkh(&self) -> bool {
|
fn is_p2pkh(&self) -> bool {
|
||||||
self.0.len() == 25
|
self.0.len() == 25
|
||||||
&& self.0[0] == OP_DUP.to_u8()
|
&& self.0[0] == OP_DUP.to_u8()
|
||||||
&& self.0[1] == OP_HASH160.to_u8()
|
&& self.0[1] == OP_HASH160.to_u8()
|
||||||
|
@ -219,7 +219,7 @@ mod tmp_pub {
|
||||||
/// Note: `OP_RESERVED` (`0x50`) and all the OP_PUSHNUM operations
|
/// Note: `OP_RESERVED` (`0x50`) and all the OP_PUSHNUM operations
|
||||||
/// are considered push operations.
|
/// are considered push operations.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn is_push_only(&self) -> bool {
|
fn is_push_only(&self) -> bool {
|
||||||
for inst in self.instructions() {
|
for inst in self.instructions() {
|
||||||
match inst {
|
match inst {
|
||||||
Err(_) => return false,
|
Err(_) => return false,
|
||||||
|
@ -240,7 +240,7 @@ mod tmp_pub {
|
||||||
///
|
///
|
||||||
/// `2 <pubkey1> <pubkey2> <pubkey3> 3 OP_CHECKMULTISIG`
|
/// `2 <pubkey1> <pubkey2> <pubkey3> 3 OP_CHECKMULTISIG`
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn is_multisig(&self) -> bool {
|
fn is_multisig(&self) -> bool {
|
||||||
let required_sigs;
|
let required_sigs;
|
||||||
|
|
||||||
let mut instructions = self.instructions();
|
let mut instructions = self.instructions();
|
||||||
|
@ -288,11 +288,11 @@ mod tmp_pub {
|
||||||
|
|
||||||
/// Checks whether a script pubkey is a Segregated Witness (segwit) program.
|
/// Checks whether a script pubkey is a Segregated Witness (segwit) program.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn is_witness_program(&self) -> bool { self.witness_version().is_some() }
|
fn is_witness_program(&self) -> bool { self.witness_version().is_some() }
|
||||||
|
|
||||||
/// Checks whether a script pubkey is a P2WSH output.
|
/// Checks whether a script pubkey is a P2WSH output.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn is_p2wsh(&self) -> bool {
|
fn is_p2wsh(&self) -> bool {
|
||||||
self.0.len() == 34
|
self.0.len() == 34
|
||||||
&& self.witness_version() == Some(WitnessVersion::V0)
|
&& self.witness_version() == Some(WitnessVersion::V0)
|
||||||
&& self.0[1] == OP_PUSHBYTES_32.to_u8()
|
&& self.0[1] == OP_PUSHBYTES_32.to_u8()
|
||||||
|
@ -300,7 +300,7 @@ mod tmp_pub {
|
||||||
|
|
||||||
/// Checks whether a script pubkey is a P2WPKH output.
|
/// Checks whether a script pubkey is a P2WPKH output.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn is_p2wpkh(&self) -> bool {
|
fn is_p2wpkh(&self) -> bool {
|
||||||
self.0.len() == 22
|
self.0.len() == 22
|
||||||
&& self.witness_version() == Some(WitnessVersion::V0)
|
&& self.witness_version() == Some(WitnessVersion::V0)
|
||||||
&& self.0[1] == OP_PUSHBYTES_20.to_u8()
|
&& self.0[1] == OP_PUSHBYTES_20.to_u8()
|
||||||
|
@ -308,7 +308,7 @@ mod tmp_pub {
|
||||||
|
|
||||||
/// Checks whether a script pubkey is a P2TR output.
|
/// Checks whether a script pubkey is a P2TR output.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn is_p2tr(&self) -> bool {
|
fn is_p2tr(&self) -> bool {
|
||||||
self.0.len() == 34
|
self.0.len() == 34
|
||||||
&& self.witness_version() == Some(WitnessVersion::V1)
|
&& self.witness_version() == Some(WitnessVersion::V1)
|
||||||
&& self.0[1] == OP_PUSHBYTES_32.to_u8()
|
&& self.0[1] == OP_PUSHBYTES_32.to_u8()
|
||||||
|
@ -319,7 +319,7 @@ mod tmp_pub {
|
||||||
/// To validate if the OP_RETURN obeys Bitcoin Core's current standardness policy, use
|
/// To validate if the OP_RETURN obeys Bitcoin Core's current standardness policy, use
|
||||||
/// [`is_standard_op_return()`](Self::is_standard_op_return) instead.
|
/// [`is_standard_op_return()`](Self::is_standard_op_return) instead.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn is_op_return(&self) -> bool {
|
fn is_op_return(&self) -> bool {
|
||||||
match self.0.first() {
|
match self.0.first() {
|
||||||
Some(b) => *b == OP_RETURN.to_u8(),
|
Some(b) => *b == OP_RETURN.to_u8(),
|
||||||
None => false,
|
None => false,
|
||||||
|
@ -331,7 +331,7 @@ mod tmp_pub {
|
||||||
/// What this function considers to be standard may change without warning pending Bitcoin Core
|
/// What this function considers to be standard may change without warning pending Bitcoin Core
|
||||||
/// changes.
|
/// changes.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn is_standard_op_return(&self) -> bool { self.is_op_return() && self.0.len() <= 80 }
|
fn is_standard_op_return(&self) -> bool { self.is_op_return() && self.0.len() <= 80 }
|
||||||
|
|
||||||
/// Checks whether a script is trivially known to have no satisfying input.
|
/// Checks whether a script is trivially known to have no satisfying input.
|
||||||
///
|
///
|
||||||
|
@ -342,7 +342,7 @@ mod tmp_pub {
|
||||||
note = "The method has potentially confusing semantics and is going to be removed, you might want `is_op_return`"
|
note = "The method has potentially confusing semantics and is going to be removed, you might want `is_op_return`"
|
||||||
)]
|
)]
|
||||||
#[inline]
|
#[inline]
|
||||||
pub 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.0.first() {
|
||||||
|
@ -362,7 +362,7 @@ mod tmp_pub {
|
||||||
/// It merely gets the last push of the script.
|
/// It merely gets the last push of the script.
|
||||||
///
|
///
|
||||||
/// Use [`Script::is_p2sh`] on the scriptPubKey to check whether it is actually a P2SH script.
|
/// Use [`Script::is_p2sh`] on the scriptPubKey to check whether it is actually a P2SH script.
|
||||||
pub fn redeem_script(&self) -> Option<&Script> {
|
fn redeem_script(&self) -> Option<&Script> {
|
||||||
// Script must consist entirely of pushes.
|
// Script must consist entirely of pushes.
|
||||||
if self.instructions().any(|i| i.is_err() || i.unwrap().push_bytes().is_none()) {
|
if self.instructions().any(|i| i.is_err() || i.unwrap().push_bytes().is_none()) {
|
||||||
return None;
|
return None;
|
||||||
|
@ -378,7 +378,7 @@ mod tmp_pub {
|
||||||
/// Returns the minimum value an output with this script should have in order to be
|
/// Returns the minimum value an output with this script should have in order to be
|
||||||
/// broadcastable on today’s Bitcoin network.
|
/// broadcastable on today’s Bitcoin network.
|
||||||
#[deprecated(since = "0.32.0", note = "use minimal_non_dust and friends")]
|
#[deprecated(since = "0.32.0", note = "use minimal_non_dust and friends")]
|
||||||
pub fn dust_value(&self) -> crate::Amount { self.minimal_non_dust() }
|
fn dust_value(&self) -> crate::Amount { self.minimal_non_dust() }
|
||||||
|
|
||||||
/// Returns the minimum value an output with this script should have in order to be
|
/// Returns the minimum value an output with this script should have in order to be
|
||||||
/// broadcastable on today's Bitcoin network.
|
/// broadcastable on today's Bitcoin network.
|
||||||
|
@ -389,7 +389,7 @@ mod tmp_pub {
|
||||||
/// To use a custom value, use [`minimal_non_dust_custom`].
|
/// To use a custom value, use [`minimal_non_dust_custom`].
|
||||||
///
|
///
|
||||||
/// [`minimal_non_dust_custom`]: Script::minimal_non_dust_custom
|
/// [`minimal_non_dust_custom`]: Script::minimal_non_dust_custom
|
||||||
pub fn minimal_non_dust(&self) -> crate::Amount {
|
fn minimal_non_dust(&self) -> crate::Amount {
|
||||||
self.minimal_non_dust_internal(DUST_RELAY_TX_FEE.into())
|
self.minimal_non_dust_internal(DUST_RELAY_TX_FEE.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -404,7 +404,7 @@ mod tmp_pub {
|
||||||
/// To use the default Bitcoin Core value, use [`minimal_non_dust`].
|
/// To use the default Bitcoin Core value, use [`minimal_non_dust`].
|
||||||
///
|
///
|
||||||
/// [`minimal_non_dust`]: Script::minimal_non_dust
|
/// [`minimal_non_dust`]: Script::minimal_non_dust
|
||||||
pub fn minimal_non_dust_custom(&self, dust_relay_fee: FeeRate) -> crate::Amount {
|
fn minimal_non_dust_custom(&self, dust_relay_fee: FeeRate) -> crate::Amount {
|
||||||
self.minimal_non_dust_internal(dust_relay_fee.to_sat_per_kwu() * 4)
|
self.minimal_non_dust_internal(dust_relay_fee.to_sat_per_kwu() * 4)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -422,7 +422,7 @@ mod tmp_pub {
|
||||||
/// (Note: Taproot scripts don't count toward the sigop count of the block,
|
/// (Note: Taproot scripts don't count toward the sigop count of the block,
|
||||||
/// nor do they have CHECKMULTISIG operations. This function does not count OP_CHECKSIGADD,
|
/// nor do they have CHECKMULTISIG operations. This function does not count OP_CHECKSIGADD,
|
||||||
/// so do not use this to try and estimate if a Taproot script goes over the sigop budget.)
|
/// so do not use this to try and estimate if a Taproot script goes over the sigop budget.)
|
||||||
pub fn count_sigops(&self) -> usize { self.count_sigops_internal(true) }
|
fn count_sigops(&self) -> usize { self.count_sigops_internal(true) }
|
||||||
|
|
||||||
/// Counts the sigops for this Script using legacy counting.
|
/// Counts the sigops for this Script using legacy counting.
|
||||||
///
|
///
|
||||||
|
@ -436,7 +436,7 @@ mod tmp_pub {
|
||||||
/// (Note: Taproot scripts don't count toward the sigop count of the block,
|
/// (Note: Taproot scripts don't count toward the sigop count of the block,
|
||||||
/// nor do they have CHECKMULTISIG operations. This function does not count OP_CHECKSIGADD,
|
/// nor do they have CHECKMULTISIG operations. This function does not count OP_CHECKSIGADD,
|
||||||
/// so do not use this to try and estimate if a Taproot script goes over the sigop budget.)
|
/// so do not use this to try and estimate if a Taproot script goes over the sigop budget.)
|
||||||
pub fn count_sigops_legacy(&self) -> usize { self.count_sigops_internal(false) }
|
fn count_sigops_legacy(&self) -> usize { self.count_sigops_internal(false) }
|
||||||
|
|
||||||
/// Iterates over the script instructions.
|
/// Iterates over the script instructions.
|
||||||
///
|
///
|
||||||
|
@ -446,7 +446,7 @@ mod tmp_pub {
|
||||||
///
|
///
|
||||||
/// To force minimal pushes, use [`instructions_minimal`](Self::instructions_minimal).
|
/// To force minimal pushes, use [`instructions_minimal`](Self::instructions_minimal).
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn instructions(&self) -> Instructions {
|
fn instructions(&self) -> Instructions {
|
||||||
Instructions { data: self.0.iter(), enforce_minimal: false }
|
Instructions { data: self.0.iter(), enforce_minimal: false }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -455,7 +455,7 @@ mod tmp_pub {
|
||||||
/// This is similar to [`instructions`](Self::instructions) but an error is returned if a push
|
/// This is similar to [`instructions`](Self::instructions) but an error is returned if a push
|
||||||
/// is not minimal.
|
/// is not minimal.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn instructions_minimal(&self) -> Instructions {
|
fn instructions_minimal(&self) -> Instructions {
|
||||||
Instructions { data: self.0.iter(), enforce_minimal: true }
|
Instructions { data: self.0.iter(), enforce_minimal: true }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -467,7 +467,7 @@ mod tmp_pub {
|
||||||
///
|
///
|
||||||
/// To force minimal pushes, use [`Self::instruction_indices_minimal`].
|
/// To force minimal pushes, use [`Self::instruction_indices_minimal`].
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn instruction_indices(&self) -> InstructionIndices {
|
fn instruction_indices(&self) -> InstructionIndices {
|
||||||
InstructionIndices::from_instructions(self.instructions())
|
InstructionIndices::from_instructions(self.instructions())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -476,17 +476,17 @@ mod tmp_pub {
|
||||||
/// This is similar to [`instruction_indices`](Self::instruction_indices) but an error is
|
/// This is similar to [`instruction_indices`](Self::instruction_indices) but an error is
|
||||||
/// returned if a push is not minimal.
|
/// returned if a push is not minimal.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn instruction_indices_minimal(&self) -> InstructionIndices {
|
fn instruction_indices_minimal(&self) -> InstructionIndices {
|
||||||
InstructionIndices::from_instructions(self.instructions_minimal())
|
InstructionIndices::from_instructions(self.instructions_minimal())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Writes the human-readable assembly representation of the script to the formatter.
|
/// Writes the human-readable assembly representation of the script to the formatter.
|
||||||
pub fn fmt_asm(&self, f: &mut dyn fmt::Write) -> fmt::Result {
|
fn fmt_asm(&self, f: &mut dyn fmt::Write) -> fmt::Result {
|
||||||
bytes_to_asm_fmt(self.as_ref(), f)
|
bytes_to_asm_fmt(self.as_ref(), f)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the human-readable assembly representation of the script.
|
/// Returns the human-readable assembly representation of the script.
|
||||||
pub fn to_asm_string(&self) -> String {
|
fn to_asm_string(&self) -> String {
|
||||||
let mut buf = String::new();
|
let mut buf = String::new();
|
||||||
self.fmt_asm(&mut buf).unwrap();
|
self.fmt_asm(&mut buf).unwrap();
|
||||||
buf
|
buf
|
||||||
|
@ -497,19 +497,18 @@ mod tmp_pub {
|
||||||
/// This is a more convenient and performant way to write `format!("{:x}", script)`.
|
/// 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
|
/// For better performance you should generally prefer displaying the script but if `String` is
|
||||||
/// required (this is common in tests) this method can be used.
|
/// required (this is common in tests) this method can be used.
|
||||||
pub fn to_hex_string(&self) -> String { self.as_bytes().to_lower_hex_string() }
|
fn to_hex_string(&self) -> String { self.as_bytes().to_lower_hex_string() }
|
||||||
|
|
||||||
/// Returns the first opcode of the script (if there is any).
|
/// Returns the first opcode of the script (if there is any).
|
||||||
pub fn first_opcode(&self) -> Option<Opcode> {
|
fn first_opcode(&self) -> Option<Opcode> {
|
||||||
self.as_bytes().first().copied().map(From::from)
|
self.as_bytes().first().copied().map(From::from)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mod tmp_priv {
|
crate::internal_macros::define_extension_trait! {
|
||||||
use super::*;
|
pub(crate) trait ScriptExtPriv impl for Script {
|
||||||
impl Script {
|
fn minimal_non_dust_internal(&self, dust_relay_fee: u64) -> crate::Amount {
|
||||||
pub(crate) fn minimal_non_dust_internal(&self, dust_relay_fee: u64) -> crate::Amount {
|
|
||||||
// This must never be lower than Bitcoin Core's GetDustThreshold() (as of v0.21) as it may
|
// This must never be lower than Bitcoin Core's GetDustThreshold() (as of v0.21) as it may
|
||||||
// otherwise allow users to create transactions which likely can never be broadcast/confirmed.
|
// otherwise allow users to create transactions which likely can never be broadcast/confirmed.
|
||||||
let sats = dust_relay_fee
|
let sats = dust_relay_fee
|
||||||
|
@ -532,7 +531,7 @@ mod tmp_priv {
|
||||||
crate::Amount::from_sat(sats)
|
crate::Amount::from_sat(sats)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn count_sigops_internal(&self, accurate: bool) -> usize {
|
fn count_sigops_internal(&self, accurate: bool) -> usize {
|
||||||
let mut n = 0;
|
let mut n = 0;
|
||||||
let mut pushnum_cache = None;
|
let mut pushnum_cache = None;
|
||||||
for inst in self.instructions() {
|
for inst in self.instructions() {
|
||||||
|
@ -575,7 +574,7 @@ mod tmp_priv {
|
||||||
/// Iterates the script to find the last opcode.
|
/// Iterates the script to find the last opcode.
|
||||||
///
|
///
|
||||||
/// Returns `None` is the instruction is data push or if the script is empty.
|
/// Returns `None` is the instruction is data push or if the script is empty.
|
||||||
pub(in crate::blockdata::script) fn last_opcode(&self) -> Option<Opcode> {
|
fn last_opcode(&self) -> Option<Opcode> {
|
||||||
match self.instructions().last() {
|
match self.instructions().last() {
|
||||||
Some(Ok(Instruction::Op(op))) => Some(op),
|
Some(Ok(Instruction::Op(op))) => Some(op),
|
||||||
_ => None,
|
_ => None,
|
||||||
|
@ -585,7 +584,7 @@ mod tmp_priv {
|
||||||
/// Iterates the script to find the last pushdata.
|
/// Iterates the script to find the last pushdata.
|
||||||
///
|
///
|
||||||
/// Returns `None` if the instruction is an opcode or if the script is empty.
|
/// Returns `None` if the instruction is an opcode or if the script is empty.
|
||||||
pub(crate) fn last_pushdata(&self) -> Option<&PushBytes> {
|
fn last_pushdata(&self) -> Option<&PushBytes> {
|
||||||
match self.instructions().last() {
|
match self.instructions().last() {
|
||||||
// Handles op codes up to (but excluding) OP_PUSHNUM_NEG.
|
// Handles op codes up to (but excluding) OP_PUSHNUM_NEG.
|
||||||
Some(Ok(Instruction::PushBytes(bytes))) => Some(bytes),
|
Some(Ok(Instruction::PushBytes(bytes))) => Some(bytes),
|
||||||
|
|
|
@ -7,6 +7,7 @@ use crate::locktime::absolute;
|
||||||
use crate::opcodes::all::*;
|
use crate::opcodes::all::*;
|
||||||
use crate::opcodes::{self, Opcode};
|
use crate::opcodes::{self, Opcode};
|
||||||
use crate::prelude::Vec;
|
use crate::prelude::Vec;
|
||||||
|
use crate::script::{ScriptExt as _, ScriptExtPriv as _};
|
||||||
use crate::Sequence;
|
use crate::Sequence;
|
||||||
|
|
||||||
/// An Object which can be used to construct a script piece by piece.
|
/// An Object which can be used to construct a script piece by piece.
|
||||||
|
|
|
@ -6,7 +6,7 @@ use core::ops::Deref;
|
||||||
use hex::FromHex;
|
use hex::FromHex;
|
||||||
use internals::ToU64 as _;
|
use internals::ToU64 as _;
|
||||||
|
|
||||||
use super::{opcode_to_verify, Builder, Instruction, PushBytes, Script};
|
use super::{opcode_to_verify, Builder, Instruction, PushBytes, Script, ScriptExtPriv as _};
|
||||||
use crate::opcodes::all::*;
|
use crate::opcodes::all::*;
|
||||||
use crate::opcodes::{self, Opcode};
|
use crate::opcodes::{self, Opcode};
|
||||||
use crate::prelude::{Box, Vec};
|
use crate::prelude::{Box, Vec};
|
||||||
|
|
|
@ -15,6 +15,7 @@ use secp256k1::{Secp256k1, Verification};
|
||||||
use super::witness_version::WitnessVersion;
|
use super::witness_version::WitnessVersion;
|
||||||
use super::{PushBytes, Script, WScriptHash, WitnessScriptSizeError};
|
use super::{PushBytes, Script, WScriptHash, WitnessScriptSizeError};
|
||||||
use crate::crypto::key::{CompressedPublicKey, TapTweak, TweakedPublicKey, UntweakedPublicKey};
|
use crate::crypto::key::{CompressedPublicKey, TapTweak, TweakedPublicKey, UntweakedPublicKey};
|
||||||
|
use crate::script::ScriptExt as _;
|
||||||
use crate::taproot::TapNodeHash;
|
use crate::taproot::TapNodeHash;
|
||||||
|
|
||||||
/// The minimum byte size of a segregated witness program.
|
/// The minimum byte size of a segregated witness program.
|
||||||
|
|
|
@ -23,7 +23,7 @@ use crate::consensus::{encode, Decodable, Encodable};
|
||||||
use crate::internal_macros::{impl_consensus_encoding, impl_hashencode};
|
use crate::internal_macros::{impl_consensus_encoding, impl_hashencode};
|
||||||
use crate::locktime::absolute::{self, Height, Time};
|
use crate::locktime::absolute::{self, Height, Time};
|
||||||
use crate::prelude::{Borrow, Vec};
|
use crate::prelude::{Borrow, Vec};
|
||||||
use crate::script::{Script, ScriptBuf};
|
use crate::script::{Script, ScriptBuf, ScriptExt as _, ScriptExtPriv as _};
|
||||||
#[cfg(doc)]
|
#[cfg(doc)]
|
||||||
use crate::sighash::{EcdsaSighashType, TapSighashType};
|
use crate::sighash::{EcdsaSighashType, TapSighashType};
|
||||||
use crate::witness::Witness;
|
use crate::witness::Witness;
|
||||||
|
|
|
@ -13,6 +13,8 @@ use crate::consensus::encode::{Error, MAX_VEC_SIZE};
|
||||||
use crate::consensus::{Decodable, Encodable, WriteExt};
|
use crate::consensus::{Decodable, Encodable, WriteExt};
|
||||||
use crate::crypto::ecdsa;
|
use crate::crypto::ecdsa;
|
||||||
use crate::prelude::Vec;
|
use crate::prelude::Vec;
|
||||||
|
#[cfg(doc)]
|
||||||
|
use crate::script::ScriptExt as _;
|
||||||
use crate::taproot::{self, TAPROOT_ANNEX_PREFIX};
|
use crate::taproot::{self, TAPROOT_ANNEX_PREFIX};
|
||||||
use crate::{Script, VarInt};
|
use crate::{Script, VarInt};
|
||||||
|
|
||||||
|
|
|
@ -25,6 +25,7 @@ use crate::crypto::key::{PrivateKey, PublicKey};
|
||||||
use crate::crypto::{ecdsa, taproot};
|
use crate::crypto::{ecdsa, taproot};
|
||||||
use crate::key::{TapTweak, XOnlyPublicKey};
|
use crate::key::{TapTweak, XOnlyPublicKey};
|
||||||
use crate::prelude::{btree_map, BTreeMap, BTreeSet, Borrow, Box, Vec};
|
use crate::prelude::{btree_map, BTreeMap, BTreeSet, Borrow, Box, Vec};
|
||||||
|
use crate::script::ScriptExt as _;
|
||||||
use crate::sighash::{self, EcdsaSighashType, Prevouts, SighashCache};
|
use crate::sighash::{self, EcdsaSighashType, Prevouts, SighashCache};
|
||||||
use crate::transaction::{self, Transaction, TxOut};
|
use crate::transaction::{self, Transaction, TxOut};
|
||||||
use crate::{Amount, FeeRate, TapLeafHash, TapSighashType};
|
use crate::{Amount, FeeRate, TapLeafHash, TapSighashType};
|
||||||
|
|
|
@ -7,6 +7,7 @@ use bitcoin::bip32::{DerivationPath, Fingerprint};
|
||||||
use bitcoin::consensus::encode::serialize_hex;
|
use bitcoin::consensus::encode::serialize_hex;
|
||||||
use bitcoin::opcodes::all::OP_CHECKSIG;
|
use bitcoin::opcodes::all::OP_CHECKSIG;
|
||||||
use bitcoin::psbt::{GetKey, Input, KeyRequest, PsbtSighashType, SignError};
|
use bitcoin::psbt::{GetKey, Input, KeyRequest, PsbtSighashType, SignError};
|
||||||
|
use bitcoin::script::ScriptExt as _;
|
||||||
use bitcoin::taproot::{LeafVersion, TaprootBuilder, TaprootSpendInfo};
|
use bitcoin::taproot::{LeafVersion, TaprootBuilder, TaprootSpendInfo};
|
||||||
use bitcoin::transaction::Version;
|
use bitcoin::transaction::Version;
|
||||||
use bitcoin::{
|
use bitcoin::{
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
use bitcoin::address::Address;
|
use bitcoin::address::Address;
|
||||||
use bitcoin::consensus::encode;
|
use bitcoin::consensus::encode;
|
||||||
use bitcoin::{script, Network};
|
use bitcoin::script::{self, ScriptExt as _};
|
||||||
|
use bitcoin::Network;
|
||||||
use honggfuzz::fuzz;
|
use honggfuzz::fuzz;
|
||||||
|
|
||||||
fn do_test(data: &[u8]) {
|
fn do_test(data: &[u8]) {
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
|
||||||
|
use bitcoin::script::ScriptExt as _;
|
||||||
use honggfuzz::fuzz;
|
use honggfuzz::fuzz;
|
||||||
|
|
||||||
// faster than String, we don't need to actually produce the value, just check absence of panics
|
// faster than String, we don't need to actually produce the value, just check absence of panics
|
||||||
|
|
Loading…
Reference in New Issue