Skip to content

Commit a412fcb

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

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

lightning/src/util/ser.rs

Lines changed: 29 additions & 8 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};
@@ -531,7 +532,7 @@ impl_array!(12); // for OnionV2
531532
impl_array!(16); // for IPv6
532533
impl_array!(32); // for channel id & hmac
533534
impl_array!(PUBLIC_KEY_SIZE); // for PublicKey
534-
impl_array!(COMPACT_SIGNATURE_SIZE); // for Signature
535+
impl_array!(64); // for ecdsa::Signature and schnorr::Signature
535536
impl_array!(1300); // for OnionPacket.hop_data
536537

537538
/// For variable-length values within TLV record where the length is encoded as part of the record.
@@ -676,7 +677,7 @@ impl Readable for Vec<u8> {
676677
Ok(ret)
677678
}
678679
}
679-
impl Writeable for Vec<Signature> {
680+
impl Writeable for Vec<ecdsa::Signature> {
680681
#[inline]
681682
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
682683
(self.len() as u16).write(w)?;
@@ -687,7 +688,7 @@ impl Writeable for Vec<Signature> {
687688
}
688689
}
689690

690-
impl Readable for Vec<Signature> {
691+
impl Readable for Vec<ecdsa::Signature> {
691692
#[inline]
692693
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
693694
let len: u16 = Readable::read(r)?;
@@ -776,7 +777,7 @@ impl Readable for Sha256dHash {
776777
}
777778
}
778779

779-
impl Writeable for Signature {
780+
impl Writeable for ecdsa::Signature {
780781
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
781782
self.serialize_compact().write(w)
782783
}
@@ -786,10 +787,30 @@ impl Writeable for Signature {
786787
}
787788
}
788789

789-
impl Readable for Signature {
790+
impl Readable for ecdsa::Signature {
790791
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
791792
let buf: [u8; COMPACT_SIGNATURE_SIZE] = Readable::read(r)?;
792-
match Signature::from_compact(&buf) {
793+
match ecdsa::Signature::from_compact(&buf) {
794+
Ok(sig) => Ok(sig),
795+
Err(_) => return Err(DecodeError::InvalidValue),
796+
}
797+
}
798+
}
799+
800+
impl Writeable for schnorr::Signature {
801+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
802+
self.as_ref().write(w)
803+
}
804+
#[inline]
805+
fn serialized_length(&self) -> usize {
806+
SCHNORR_SIGNATURE_SIZE
807+
}
808+
}
809+
810+
impl Readable for schnorr::Signature {
811+
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
812+
let buf: [u8; SCHNORR_SIGNATURE_SIZE] = Readable::read(r)?;
813+
match schnorr::Signature::from_slice(&buf) {
793814
Ok(sig) => Ok(sig),
794815
Err(_) => return Err(DecodeError::InvalidValue),
795816
}

0 commit comments

Comments
 (0)