From 49e8b8da32179b2357db83b234b16eeb46149951 Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Fri, 20 Jan 2023 16:29:22 +1100 Subject: [PATCH] Use write_all for sighash encoding `Sighash` does not need to implement `Encodable` because it is claimed (I don't know exactly myself) that `Sighash` is never consensus encode in Bitcoin. We are currently relying on `Sighash` to implement `Encodable` when encoding creating the segwit v0 sighash for a single input. For reference, from BIP143: If sighash type is SINGLE and the input index is smaller than the number of outputs, hashOutputs is the double SHA256 of the output amount with scriptPubKey of the same index as the input; We can use `write_all` directly to write the hashed bytes and remove the implementation of `Encodable` from the `Sighash` type. While we are at it, use `write_all` to write the zero hash also to make the code more uniform and understandable. Fix: #1549 --- bitcoin/src/hash_types.rs | 1 - bitcoin/src/sighash.rs | 5 +++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/bitcoin/src/hash_types.rs b/bitcoin/src/hash_types.rs index b8b28cce..edcbd98a 100644 --- a/bitcoin/src/hash_types.rs +++ b/bitcoin/src/hash_types.rs @@ -69,7 +69,6 @@ See [`hashes::Hash::DISPLAY_BACKWARD`] for more details. impl_hashencode!(Txid); impl_hashencode!(Wtxid); impl_hashencode!(BlockHash); - impl_hashencode!(Sighash); impl_hashencode!(TxMerkleNode); impl_hashencode!(WitnessMerkleNode); diff --git a/bitcoin/src/sighash.rs b/bitcoin/src/sighash.rs index 27495279..961ba497 100644 --- a/bitcoin/src/sighash.rs +++ b/bitcoin/src/sighash.rs @@ -729,9 +729,10 @@ impl> SighashCache { } else if sighash == EcdsaSighashType::Single && input_index < self.tx.output.len() { let mut single_enc = Sighash::engine(); self.tx.output[input_index].consensus_encode(&mut single_enc)?; - Sighash::from_engine(single_enc).consensus_encode(&mut writer)?; + let hash = Sighash::from_engine(single_enc); + writer.write_all(&hash[..])?; } else { - zero_hash.consensus_encode(&mut writer)?; + writer.write_all(&zero_hash[..])?; } self.tx.lock_time.consensus_encode(&mut writer)?;