Merge rust-bitcoin/rust-bitcoin#3519: psbt: Fix bug in Subtype consensus_encode

c89b816437 psbt: Fix bug in Subtype consensus_encode (Tobin C. Harding)

Pull request description:

  In #2906 we switched from using a `u8` for type keys to using a `u64` and encoding as a compact int (inline with the spec). Note that a `u8` encodes to the same bytes as a `u64` when the value is < 252.

  In that patch, I introduced a bug because the length returned by `PoprietaryKey::consensus_encode` uses a hard code 1 for the length of the encoding (because of single byte) instead of the variable length for the new compact encoding.

  Bug showed up in fuzzing, and was isolated by Jamil - mad props.

  Fix: #3501

ACKs for top commit:
  jamillambert:
    ACK c89b816437
  apoelstra:
    ACK c89b8164377123eb20476636f2f5271c6a687406; successfully ran local tests

Tree-SHA512: 1b61b6a9ece197d74038ceedb447fd3ca21db8e2a6a96c9281a99ac232c18c3ca55da8e3f46930401714d3575e9a406a36e4f44929ca963208a5df4be6b46cfb
This commit is contained in:
merge-script 2024-10-29 13:10:08 +00:00
commit 6c8d0ef95b
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 2 additions and 2 deletions

View File

@ -143,8 +143,8 @@ where
Subtype: Copy + From<u64> + Into<u64>, Subtype: Copy + From<u64> + Into<u64>,
{ {
fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> { fn consensus_encode<W: Write + ?Sized>(&self, w: &mut W) -> Result<usize, io::Error> {
let mut len = self.prefix.consensus_encode(w)? + 1; let mut len = self.prefix.consensus_encode(w)?;
w.emit_compact_size(self.subtype.into())?; len += w.emit_compact_size(self.subtype.into())?;
w.write_all(&self.key)?; w.write_all(&self.key)?;
len += self.key.len(); len += self.key.len();
Ok(len) Ok(len)