Skip to content

Commit f2c6779

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

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};
@@ -527,7 +528,7 @@ impl_array!(12); // for OnionV2
527528
impl_array!(16); // for IPv6
528529
impl_array!(32); // for channel id & hmac
529530
impl_array!(PUBLIC_KEY_SIZE); // for PublicKey
530-
impl_array!(COMPACT_SIGNATURE_SIZE); // for Signature
531+
impl_array!(64); // for ecdsa::Signature and schnorr::Signature
531532
impl_array!(1300); // for OnionPacket.hop_data
532533

533534
/// For variable-length values within TLV record where the length is encoded as part of the record.
@@ -659,7 +660,7 @@ impl Readable for Vec<u8> {
659660
Ok(ret)
660661
}
661662
}
662-
impl Writeable for Vec<Signature> {
663+
impl Writeable for Vec<ecdsa::Signature> {
663664
#[inline]
664665
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
665666
(self.len() as u16).write(w)?;
@@ -670,7 +671,7 @@ impl Writeable for Vec<Signature> {
670671
}
671672
}
672673

673-
impl Readable for Vec<Signature> {
674+
impl Readable for Vec<ecdsa::Signature> {
674675
#[inline]
675676
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
676677
let len: u16 = Readable::read(r)?;
@@ -759,7 +760,7 @@ impl Readable for Sha256dHash {
759760
}
760761
}
761762

762-
impl Writeable for Signature {
763+
impl Writeable for ecdsa::Signature {
763764
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
764765
self.serialize_compact().write(w)
765766
}
@@ -769,10 +770,30 @@ impl Writeable for Signature {
769770
}
770771
}
771772

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

0 commit comments

Comments
 (0)