Skip to content

Commit fe3ddd6

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

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};
@@ -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,7 +765,7 @@ 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
}
@@ -774,10 +775,30 @@ impl Writeable for Signature {
774775
}
775776
}
776777

777-
impl Readable for Signature {
778+
impl Readable for ecdsa::Signature {
778779
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
779780
let buf: [u8; COMPACT_SIGNATURE_SIZE] = Readable::read(r)?;
780-
match Signature::from_compact(&buf) {
781+
match ecdsa::Signature::from_compact(&buf) {
782+
Ok(sig) => Ok(sig),
783+
Err(_) => return Err(DecodeError::InvalidValue),
784+
}
785+
}
786+
}
787+
788+
impl Writeable for schnorr::Signature {
789+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
790+
self.as_ref().write(w)
791+
}
792+
#[inline]
793+
fn serialized_length(&self) -> usize {
794+
SCHNORR_SIGNATURE_SIZE
795+
}
796+
}
797+
798+
impl Readable for schnorr::Signature {
799+
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
800+
let buf: [u8; SCHNORR_SIGNATURE_SIZE] = Readable::read(r)?;
801+
match schnorr::Signature::from_slice(&buf) {
781802
Ok(sig) => Ok(sig),
782803
Err(_) => return Err(DecodeError::InvalidValue),
783804
}

0 commit comments

Comments
 (0)