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
This commit is contained in:
Tobin C. Harding 2023-01-20 16:29:22 +11:00
parent d66ee48482
commit 49e8b8da32
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
2 changed files with 3 additions and 3 deletions

View File

@ -69,7 +69,6 @@ See [`hashes::Hash::DISPLAY_BACKWARD`] for more details.
impl_hashencode!(Txid); impl_hashencode!(Txid);
impl_hashencode!(Wtxid); impl_hashencode!(Wtxid);
impl_hashencode!(BlockHash); impl_hashencode!(BlockHash);
impl_hashencode!(Sighash);
impl_hashencode!(TxMerkleNode); impl_hashencode!(TxMerkleNode);
impl_hashencode!(WitnessMerkleNode); impl_hashencode!(WitnessMerkleNode);

View File

@ -729,9 +729,10 @@ impl<R: Deref<Target = Transaction>> SighashCache<R> {
} else if sighash == EcdsaSighashType::Single && input_index < self.tx.output.len() { } else if sighash == EcdsaSighashType::Single && input_index < self.tx.output.len() {
let mut single_enc = Sighash::engine(); let mut single_enc = Sighash::engine();
self.tx.output[input_index].consensus_encode(&mut single_enc)?; 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 { } else {
zero_hash.consensus_encode(&mut writer)?; writer.write_all(&zero_hash[..])?;
} }
self.tx.lock_time.consensus_encode(&mut writer)?; self.tx.lock_time.consensus_encode(&mut writer)?;