From 41e447172175a55c1e1274179486fc58fe82a84e Mon Sep 17 00:00:00 2001 From: Elichai Turkel Date: Wed, 8 Apr 2020 14:35:48 +0300 Subject: [PATCH] Remove alloc when hashing sighash --- src/blockdata/transaction.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/blockdata/transaction.rs b/src/blockdata/transaction.rs index 99b87f22..f24d8727 100644 --- a/src/blockdata/transaction.rs +++ b/src/blockdata/transaction.rs @@ -33,7 +33,7 @@ use util::endian; use blockdata::constants::WITNESS_SCALE_FACTOR; #[cfg(feature="bitcoinconsensus")] use blockdata::script; use blockdata::script::Script; -use consensus::{encode, serialize, Decodable, Encodable}; +use consensus::{encode, Decodable, Encodable}; use hash_types::*; use VarInt; @@ -378,9 +378,12 @@ impl Transaction { _ => unreachable!() }; // hash the result - let mut raw_vec = serialize(&tx); - raw_vec.extend_from_slice(&endian::u32_to_array_le(sighash_u32)); - SigHash::hash(&raw_vec) + // TODO: Sanity assert that consensus_encode returned length matches the hashed length in the hasher. + let sighash_arr = endian::u32_to_array_le(sighash_u32); + let mut engine = SigHash::engine(); + tx.consensus_encode(&mut engine).unwrap(); + sighash_arr.consensus_encode(&mut engine).unwrap(); + SigHash::from_engine(engine) } /// Gets the "weight" of this transaction, as defined by BIP141. For transactions with an empty @@ -441,7 +444,7 @@ impl Transaction { /// 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 { - let tx = serialize(&*self); + 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())?;