Skip to content

Commit 9577928

Browse files
authored
Merge pull request #885 from p2pderivatives/use-secp256k1-consts-in-ser
Fix size check in Vec<Signature> serialization + use consts
2 parents eee1c30 + 2e49758 commit 9577928

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

lightning/src/util/ser.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use std::cmp;
1818

1919
use bitcoin::secp256k1::Signature;
2020
use bitcoin::secp256k1::key::{PublicKey, SecretKey};
21+
use bitcoin::secp256k1::constants::{PUBLIC_KEY_SIZE, COMPACT_SIGNATURE_SIZE};
2122
use bitcoin::blockdata::script::Script;
2223
use bitcoin::blockdata::transaction::{OutPoint, Transaction, TxOut};
2324
use bitcoin::consensus;
@@ -423,8 +424,8 @@ impl_array!(4); // for IPv4
423424
impl_array!(10); // for OnionV2
424425
impl_array!(16); // for IPv6
425426
impl_array!(32); // for channel id & hmac
426-
impl_array!(33); // for PublicKey
427-
impl_array!(64); // for Signature
427+
impl_array!(PUBLIC_KEY_SIZE); // for PublicKey
428+
impl_array!(COMPACT_SIGNATURE_SIZE); // for Signature
428429
impl_array!(1300); // for OnionPacket.hop_data
429430

430431
// HashMap
@@ -493,7 +494,7 @@ impl Readable for Vec<Signature> {
493494
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
494495
let len: u16 = Readable::read(r)?;
495496
let byte_size = (len as usize)
496-
.checked_mul(33)
497+
.checked_mul(COMPACT_SIGNATURE_SIZE)
497498
.ok_or(DecodeError::BadLengthDescriptor)?;
498499
if byte_size > MAX_BUF_SIZE {
499500
return Err(DecodeError::BadLengthDescriptor);
@@ -528,7 +529,7 @@ impl Writeable for PublicKey {
528529

529530
impl Readable for PublicKey {
530531
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
531-
let buf: [u8; 33] = Readable::read(r)?;
532+
let buf: [u8; PUBLIC_KEY_SIZE] = Readable::read(r)?;
532533
match PublicKey::from_slice(&buf) {
533534
Ok(key) => Ok(key),
534535
Err(_) => return Err(DecodeError::InvalidValue),
@@ -577,7 +578,7 @@ impl Writeable for Signature {
577578

578579
impl Readable for Signature {
579580
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
580-
let buf: [u8; 64] = Readable::read(r)?;
581+
let buf: [u8; COMPACT_SIGNATURE_SIZE] = Readable::read(r)?;
581582
match Signature::from_compact(&buf) {
582583
Ok(sig) => Ok(sig),
583584
Err(_) => return Err(DecodeError::InvalidValue),

0 commit comments

Comments
 (0)