Skip to content

Commit af455db

Browse files
committed
Schnorr Signature serialization
BOLT 12 uses Schnorr signatures for signing offers messages, which need to be serialized.
1 parent 9c64d64 commit af455db

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

lightning/src/util/ser.rs

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ use core::convert::TryFrom;
2020
use core::ops::Deref;
2121

2222
use bitcoin::secp256k1::{PublicKey, SecretKey};
23-
use bitcoin::secp256k1::constants::{PUBLIC_KEY_SIZE, SECRET_KEY_SIZE, COMPACT_SIGNATURE_SIZE};
24-
use bitcoin::secp256k1::ecdsa::Signature;
23+
use bitcoin::secp256k1::constants::{PUBLIC_KEY_SIZE, SECRET_KEY_SIZE, COMPACT_SIGNATURE_SIZE, SCHNORR_SIGNATURE_SIZE};
24+
use bitcoin::secp256k1::ecdsa;
25+
use bitcoin::secp256k1::schnorr;
2526
use bitcoin::blockdata::constants::ChainHash;
2627
use bitcoin::blockdata::script::Script;
2728
use bitcoin::blockdata::transaction::{OutPoint, Transaction, TxOut};
@@ -499,7 +500,7 @@ impl_array!(12); // for OnionV2
499500
impl_array!(16); // for IPv6
500501
impl_array!(32); // for channel id & hmac
501502
impl_array!(PUBLIC_KEY_SIZE); // for PublicKey
502-
impl_array!(COMPACT_SIGNATURE_SIZE); // for Signature
503+
impl_array!(64); // for ecdsa::Signature and schnorr::Signature
503504
impl_array!(1300); // for OnionPacket.hop_data
504505

505506
impl Writeable for [u16; 8] {
@@ -664,7 +665,7 @@ impl Readable for Vec<u8> {
664665
Ok(ret)
665666
}
666667
}
667-
impl Writeable for Vec<Signature> {
668+
impl Writeable for Vec<ecdsa::Signature> {
668669
#[inline]
669670
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
670671
(self.len() as u16).write(w)?;
@@ -675,7 +676,7 @@ impl Writeable for Vec<Signature> {
675676
}
676677
}
677678

678-
impl Readable for Vec<Signature> {
679+
impl Readable for Vec<ecdsa::Signature> {
679680
#[inline]
680681
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
681682
let len: u16 = Readable::read(r)?;
@@ -764,20 +765,32 @@ impl Readable for Sha256dHash {
764765
}
765766
}
766767

767-
impl Writeable for Signature {
768+
impl Writeable for ecdsa::Signature {
768769
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
769770
self.serialize_compact().write(w)
770771
}
771-
#[inline]
772-
fn serialized_length(&self) -> usize {
773-
COMPACT_SIGNATURE_SIZE
774-
}
775772
}
776773

777-
impl Readable for Signature {
774+
impl Readable for ecdsa::Signature {
778775
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
779776
let buf: [u8; COMPACT_SIGNATURE_SIZE] = Readable::read(r)?;
780-
match Signature::from_compact(&buf) {
777+
match ecdsa::Signature::from_compact(&buf) {
778+
Ok(sig) => Ok(sig),
779+
Err(_) => return Err(DecodeError::InvalidValue),
780+
}
781+
}
782+
}
783+
784+
impl Writeable for schnorr::Signature {
785+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
786+
self.as_ref().write(w)
787+
}
788+
}
789+
790+
impl Readable for schnorr::Signature {
791+
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
792+
let buf: [u8; SCHNORR_SIGNATURE_SIZE] = Readable::read(r)?;
793+
match schnorr::Signature::from_slice(&buf) {
781794
Ok(sig) => Ok(sig),
782795
Err(_) => return Err(DecodeError::InvalidValue),
783796
}

0 commit comments

Comments
 (0)