Add an extension trait for script validation
Add an extension trait for the validation logic in preparation for moving the `Script` type to `primitives`.
This commit is contained in:
parent
c1aa33ed89
commit
298b96c579
|
@ -682,6 +682,8 @@ fn script_ord() {
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(feature = "bitcoinconsensus")]
|
#[cfg(feature = "bitcoinconsensus")]
|
||||||
fn test_bitcoinconsensus() {
|
fn test_bitcoinconsensus() {
|
||||||
|
use crate::consensus_validation::ScriptExt as _;
|
||||||
|
|
||||||
// a random segwit transaction from the blockchain using native segwit
|
// a random segwit transaction from the blockchain using native segwit
|
||||||
let spent_bytes = hex!("0020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d");
|
let spent_bytes = hex!("0020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d");
|
||||||
let spent = Script::from_bytes(&spent_bytes);
|
let spent = Script::from_bytes(&spent_bytes);
|
||||||
|
|
|
@ -12,6 +12,7 @@ use crate::amount::Amount;
|
||||||
use crate::consensus::encode;
|
use crate::consensus::encode;
|
||||||
#[cfg(doc)]
|
#[cfg(doc)]
|
||||||
use crate::consensus_validation;
|
use crate::consensus_validation;
|
||||||
|
use crate::internal_macros::define_extension_trait;
|
||||||
use crate::script::Script;
|
use crate::script::Script;
|
||||||
use crate::transaction::{OutPoint, Transaction, TxOut};
|
use crate::transaction::{OutPoint, Transaction, TxOut};
|
||||||
|
|
||||||
|
@ -115,7 +116,9 @@ where
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Script {
|
define_extension_trait! {
|
||||||
|
/// Extension functionality to add validation support to the [`Script`] type.
|
||||||
|
pub trait ScriptExt impl for Script {
|
||||||
/// Verifies spend of an input script.
|
/// Verifies spend of an input script.
|
||||||
///
|
///
|
||||||
/// Shorthand for [`Self::verify_with_flags`] with flag [`bitcoinconsensus::VERIFY_ALL_PRE_TAPROOT`].
|
/// Shorthand for [`Self::verify_with_flags`] with flag [`bitcoinconsensus::VERIFY_ALL_PRE_TAPROOT`].
|
||||||
|
@ -127,7 +130,7 @@ impl Script {
|
||||||
/// * `spending_tx` - The transaction that attempts to spend the output holding this script.
|
/// * `spending_tx` - The transaction that attempts to spend the output holding this script.
|
||||||
///
|
///
|
||||||
/// [`bitcoinconsensus::VERIFY_ALL_PRE_TAPROOT`]: https://docs.rs/bitcoinconsensus/0.106.0+26.0/bitcoinconsensus/constant.VERIFY_ALL_PRE_TAPROOT.html
|
/// [`bitcoinconsensus::VERIFY_ALL_PRE_TAPROOT`]: https://docs.rs/bitcoinconsensus/0.106.0+26.0/bitcoinconsensus/constant.VERIFY_ALL_PRE_TAPROOT.html
|
||||||
pub fn verify(
|
fn verify(
|
||||||
&self,
|
&self,
|
||||||
index: usize,
|
index: usize,
|
||||||
amount: crate::Amount,
|
amount: crate::Amount,
|
||||||
|
@ -146,7 +149,7 @@ impl Script {
|
||||||
/// * `flags` - Verification flags, see [`bitcoinconsensus::VERIFY_ALL_PRE_TAPROOT`] and similar.
|
/// * `flags` - Verification flags, see [`bitcoinconsensus::VERIFY_ALL_PRE_TAPROOT`] and similar.
|
||||||
///
|
///
|
||||||
/// [`bitcoinconsensus::VERIFY_ALL_PRE_TAPROOT`]: https://docs.rs/bitcoinconsensus/0.106.0+26.0/bitcoinconsensus/constant.VERIFY_ALL_PRE_TAPROOT.html
|
/// [`bitcoinconsensus::VERIFY_ALL_PRE_TAPROOT`]: https://docs.rs/bitcoinconsensus/0.106.0+26.0/bitcoinconsensus/constant.VERIFY_ALL_PRE_TAPROOT.html
|
||||||
pub fn verify_with_flags(
|
fn verify_with_flags(
|
||||||
&self,
|
&self,
|
||||||
index: usize,
|
index: usize,
|
||||||
amount: crate::Amount,
|
amount: crate::Amount,
|
||||||
|
@ -156,6 +159,7 @@ impl Script {
|
||||||
verify_script_with_flags(self, index, amount, spending_tx, flags)
|
verify_script_with_flags(self, index, amount, spending_tx, flags)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Transaction {
|
impl Transaction {
|
||||||
/// Verifies that this transaction is able to spend its inputs.
|
/// Verifies that this transaction is able to spend its inputs.
|
||||||
|
|
Loading…
Reference in New Issue