Merge rust-bitcoin/rust-bitcoin#3096: Add validation extension to script

298b96c579 Add an extension trait for script validation (Tobin C. Harding)
c1aa33ed89 Use impl syntax instead of generic (Tobin C. Harding)

Pull request description:

  Add an extension trait for the validation logic in preparation for moving the `Script` type to `primitives`.

ACKs for top commit:
  Kixunil:
    ACK 298b96c579
  apoelstra:
    ACK 298b96c579

Tree-SHA512: 6282bf7bd5657f0ec68e1369150969daf51f97dc6ff72a419fe823d60ab8a993f1e6d56b1cffa114580d388b36fe2bcbf7b9865776f98c46d68b7368168a07ee
This commit is contained in:
merge-script 2024-07-30 15:36:55 +00:00
commit 80671bbea8
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
2 changed files with 44 additions and 38 deletions

View File

@ -682,6 +682,8 @@ fn script_ord() {
#[test]
#[cfg(feature = "bitcoinconsensus")]
fn test_bitcoinconsensus() {
use crate::consensus_validation::ScriptExt as _;
// a random segwit transaction from the blockchain using native segwit
let spent_bytes = hex!("0020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d");
let spent = Script::from_bytes(&spent_bytes);

View File

@ -12,6 +12,7 @@ use crate::amount::Amount;
use crate::consensus::encode;
#[cfg(doc)]
use crate::consensus_validation;
use crate::internal_macros::define_extension_trait;
use crate::script::Script;
use crate::transaction::{OutPoint, Transaction, TxOut};
@ -115,45 +116,48 @@ where
Ok(())
}
impl Script {
/// Verifies spend of an input script.
///
/// Shorthand for [`Self::verify_with_flags`] with flag [`bitcoinconsensus::VERIFY_ALL_PRE_TAPROOT`].
///
/// # Parameters
///
/// * `index` - The input index in spending which is spending this transaction.
/// * `amount` - The amount this script guards.
/// * `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
pub fn verify(
&self,
index: usize,
amount: crate::Amount,
spending_tx: &[u8],
) -> Result<(), BitcoinconsensusError> {
verify_script(self, index, amount, spending_tx)
}
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.
///
/// Shorthand for [`Self::verify_with_flags`] with flag [`bitcoinconsensus::VERIFY_ALL_PRE_TAPROOT`].
///
/// # Parameters
///
/// * `index` - The input index in spending which is spending this transaction.
/// * `amount` - The amount this script guards.
/// * `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
fn verify(
&self,
index: usize,
amount: crate::Amount,
spending_tx: &[u8],
) -> Result<(), BitcoinconsensusError> {
verify_script(self, index, amount, spending_tx)
}
/// Verifies spend of an input script.
///
/// # Parameters
///
/// * `index` - The input index in spending which is spending this transaction.
/// * `amount` - The amount this script guards.
/// * `spending_tx` - The transaction that attempts to spend the output holding this script.
/// * `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
pub fn verify_with_flags<F: Into<u32>>(
&self,
index: usize,
amount: crate::Amount,
spending_tx: &[u8],
flags: F,
) -> Result<(), BitcoinconsensusError> {
verify_script_with_flags(self, index, amount, spending_tx, flags)
/// Verifies spend of an input script.
///
/// # Parameters
///
/// * `index` - The input index in spending which is spending this transaction.
/// * `amount` - The amount this script guards.
/// * `spending_tx` - The transaction that attempts to spend the output holding this script.
/// * `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
fn verify_with_flags(
&self,
index: usize,
amount: crate::Amount,
spending_tx: &[u8],
flags: impl Into<u32>,
) -> Result<(), BitcoinconsensusError> {
verify_script_with_flags(self, index, amount, spending_tx, flags)
}
}
}