Remove alloc when hashing sighash

This commit is contained in:
Elichai Turkel 2020-04-08 14:35:48 +03:00
parent c3a7d1b27c
commit 41e4471721
No known key found for this signature in database
GPG Key ID: 9383CDE9E8E66A7F
1 changed files with 8 additions and 5 deletions

View File

@ -33,7 +33,7 @@ use util::endian;
use blockdata::constants::WITNESS_SCALE_FACTOR; use blockdata::constants::WITNESS_SCALE_FACTOR;
#[cfg(feature="bitcoinconsensus")] use blockdata::script; #[cfg(feature="bitcoinconsensus")] use blockdata::script;
use blockdata::script::Script; use blockdata::script::Script;
use consensus::{encode, serialize, Decodable, Encodable}; use consensus::{encode, Decodable, Encodable};
use hash_types::*; use hash_types::*;
use VarInt; use VarInt;
@ -378,9 +378,12 @@ impl Transaction {
_ => unreachable!() _ => unreachable!()
}; };
// hash the result // hash the result
let mut raw_vec = serialize(&tx); // TODO: Sanity assert that consensus_encode returned length matches the hashed length in the hasher.
raw_vec.extend_from_slice(&endian::u32_to_array_le(sighash_u32)); let sighash_arr = endian::u32_to_array_le(sighash_u32);
SigHash::hash(&raw_vec) 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 /// 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! /// The lambda spent should not return the same TxOut twice!
pub fn verify<S>(&self, mut spent: S) -> Result<(), script::Error> pub fn verify<S>(&self, mut spent: S) -> Result<(), script::Error>
where S: FnMut(&OutPoint) -> Option<TxOut> { where S: FnMut(&OutPoint) -> Option<TxOut> {
let tx = serialize(&*self); let tx = encode::serialize(&*self);
for (idx, input) in self.input.iter().enumerate() { for (idx, input) in self.input.iter().enumerate() {
if let Some(output) = spent(&input.previous_output) { if let Some(output) = spent(&input.previous_output) {
output.script_pubkey.verify(idx, output.value, tx.as_slice())?; output.script_pubkey.verify(idx, output.value, tx.as_slice())?;