From 3aaa5d6846b05c79620fe68eca67ac8bb7e472de Mon Sep 17 00:00:00 2001 From: Riccardo Casatta Date: Fri, 30 Apr 2021 16:05:17 +0200 Subject: [PATCH] Add verify with flags --- src/blockdata/script.rs | 19 +++++++++++++------ src/blockdata/transaction.rs | 13 ++++++++++--- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/blockdata/script.rs b/src/blockdata/script.rs index 729707d1..7b94b822 100644 --- a/src/blockdata/script.rs +++ b/src/blockdata/script.rs @@ -449,13 +449,20 @@ impl Script { } #[cfg(feature="bitcoinconsensus")] - /// verify spend of an input script - /// # Parameters - /// * index - the input index in spending which is spending this transaction - /// * amount - the amount this script guards - /// * spending - the transaction that attempts to spend the output holding this script + /// Shorthand for [Self::verify_with_flags] with flag [bitcoinconsensus::VERIFY_ALL] pub fn verify (&self, index: usize, amount: u64, spending: &[u8]) -> Result<(), Error> { - Ok(bitcoinconsensus::verify (&self.0[..], amount, spending, index)?) + self.verify_with_flags(index, amount, spending, ::bitcoinconsensus::VERIFY_ALL) + } + + #[cfg(feature="bitcoinconsensus")] + /// Verify spend of an input script + /// # Parameters + /// * `index` - the input index in spending which is spending this transaction + /// * `amount` - the amount this script guards + /// * `spending` - the transaction that attempts to spend the output holding this script + /// * `flags` - verification flags, see [bitcoinconsensus::VERIFY_ALL] and similar + pub fn verify_with_flags>(&self, index: usize, amount: u64, spending: &[u8], flags: F) -> Result<(), Error> { + Ok(bitcoinconsensus::verify_with_flags (&self.0[..], amount, spending, index, flags.into())?) } /// Write the assembly decoding of the script bytes to the formatter. diff --git a/src/blockdata/transaction.rs b/src/blockdata/transaction.rs index bc28f347..ed99107f 100644 --- a/src/blockdata/transaction.rs +++ b/src/blockdata/transaction.rs @@ -462,15 +462,22 @@ impl Transaction { } } + #[cfg(feature="bitcoinconsensus")] + /// Shorthand for [Self::verify_with_flags] with flag [bitcoinconsensus::VERIFY_ALL] + pub fn verify(&self, spent: S) -> Result<(), script::Error> + where S: FnMut(&OutPoint) -> Option { + self.verify_with_flags(spent, ::bitcoinconsensus::VERIFY_ALL) + } + #[cfg(feature="bitcoinconsensus")] /// Verify that this transaction is able to spend its inputs /// The lambda spent should not return the same TxOut twice! - pub fn verify(&self, mut spent: S) -> Result<(), script::Error> - where S: FnMut(&OutPoint) -> Option { + pub fn verify_with_flags(&self, mut spent: S, flags: F) -> Result<(), script::Error> + where S: FnMut(&OutPoint) -> Option, F : Into + Copy { let tx = encode::serialize(&*self); for (idx, input) in self.input.iter().enumerate() { if let Some(output) = spent(&input.previous_output) { - output.script_pubkey.verify(idx, output.value, tx.as_slice())?; + output.script_pubkey.verify_with_flags(idx, output.value, tx.as_slice(), flags)?; } else { return Err(script::Error::UnknownSpentOutput(input.previous_output.clone())); }