Skip to content

Commit 2e46700

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

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};
@@ -498,7 +499,7 @@ impl_array!(12); // for OnionV2
498499
impl_array!(16); // for IPv6
499500
impl_array!(32); // for channel id & hmac
500501
impl_array!(PUBLIC_KEY_SIZE); // for PublicKey
501-
impl_array!(COMPACT_SIGNATURE_SIZE); // for Signature
502+
impl_array!(64); // for ecdsa::Signature and schnorr::Signature
502503
impl_array!(1300); // for OnionPacket.hop_data
503504

504505
impl Writeable for [u16; 8] {
@@ -663,7 +664,7 @@ impl Readable for Vec<u8> {
663664
Ok(ret)
664665
}
665666
}
666-
impl Writeable for Vec<Signature> {
667+
impl Writeable for Vec<ecdsa::Signature> {
667668
#[inline]
668669
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
669670
(self.len() as u16).write(w)?;
@@ -674,7 +675,7 @@ impl Writeable for Vec<Signature> {
674675
}
675676
}
676677

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

766-
impl Writeable for Signature {
767+
impl Writeable for ecdsa::Signature {
767768
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
768769
self.serialize_compact().write(w)
769770
}
@@ -773,10 +774,30 @@ impl Writeable for Signature {
773774
}
774775
}
775776

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

0 commit comments

Comments
 (0)