psbt: Fix bug in Subtype consensus_encode

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.
This commit is contained in:
Tobin C. Harding 2024-10-28 08:09:41 +11:00
parent b11bd9a6b5
commit c89b816437
No known key found for this signature in database
GPG Key ID: 40BF9E4C269D6607
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)