From c1aa33ed8987015ec8ebc50247598eb4ec2fc880 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Tue, 23 Jul 2024 06:23:11 -0500 Subject: [PATCH 1/2] Use impl syntax instead of generic A single trait bound can be expressed using the `impl` style. This is a breaking change because callers can no longer use turbofish. In this case that probably does not matter because users are likely just passing an integer in and letting the compiler infer the type. Done in preparation for moving logic into an extension trait so that the functions can be parsed by the `define_extension_trait` macro. ref: https://doc.rust-lang.org/reference/types/impl-trait.html --- bitcoin/src/consensus_validation.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bitcoin/src/consensus_validation.rs b/bitcoin/src/consensus_validation.rs index 3dc67dfc8..f6cbd66f6 100644 --- a/bitcoin/src/consensus_validation.rs +++ b/bitcoin/src/consensus_validation.rs @@ -146,12 +146,12 @@ impl 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>( + pub fn verify_with_flags( &self, index: usize, amount: crate::Amount, spending_tx: &[u8], - flags: F, + flags: impl Into, ) -> Result<(), BitcoinconsensusError> { verify_script_with_flags(self, index, amount, spending_tx, flags) } From 298b96c5792f19602e05acfe56e8d1f121648677 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Tue, 23 Jul 2024 06:25:17 -0500 Subject: [PATCH 2/2] Add an extension trait for script validation Add an extension trait for the validation logic in preparation for moving the `Script` type to `primitives`. --- bitcoin/src/blockdata/script/tests.rs | 2 + bitcoin/src/consensus_validation.rs | 80 ++++++++++++++------------- 2 files changed, 44 insertions(+), 38 deletions(-) diff --git a/bitcoin/src/blockdata/script/tests.rs b/bitcoin/src/blockdata/script/tests.rs index 2828bd1d7..dc5889664 100644 --- a/bitcoin/src/blockdata/script/tests.rs +++ b/bitcoin/src/blockdata/script/tests.rs @@ -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); diff --git a/bitcoin/src/consensus_validation.rs b/bitcoin/src/consensus_validation.rs index f6cbd66f6..b0177ec1e 100644 --- a/bitcoin/src/consensus_validation.rs +++ b/bitcoin/src/consensus_validation.rs @@ -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( - &self, - index: usize, - amount: crate::Amount, - spending_tx: &[u8], - flags: impl Into, - ) -> 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, + ) -> Result<(), BitcoinconsensusError> { + verify_script_with_flags(self, index, amount, spending_tx, flags) + } } }