Skip to content

Commit 2f9fe6d

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

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::script::Script;
2627
use bitcoin::blockdata::transaction::{OutPoint, Transaction, TxOut};
2728
use bitcoin::consensus;
@@ -530,7 +531,7 @@ impl_array!(12); // for OnionV2
530531
impl_array!(16); // for IPv6
531532
impl_array!(32); // for channel id & hmac
532533
impl_array!(PUBLIC_KEY_SIZE); // for PublicKey
533-
impl_array!(COMPACT_SIGNATURE_SIZE); // for Signature
534+
impl_array!(64); // for ecdsa::Signature and schnorr::Signature
534535
impl_array!(1300); // for OnionPacket.hop_data
535536

536537
/// For variable-length values within TLV subtypes where the length cannot be inferred from the
@@ -704,7 +705,7 @@ impl Readable for Vec<u8> {
704705
Ok(ret)
705706
}
706707
}
707-
impl Writeable for Vec<Signature> {
708+
impl Writeable for Vec<ecdsa::Signature> {
708709
#[inline]
709710
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
710711
(self.len() as u16).write(w)?;
@@ -715,7 +716,7 @@ impl Writeable for Vec<Signature> {
715716
}
716717
}
717718

718-
impl Readable for Vec<Signature> {
719+
impl Readable for Vec<ecdsa::Signature> {
719720
#[inline]
720721
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
721722
let len: u16 = Readable::read(r)?;
@@ -804,7 +805,7 @@ impl Readable for Sha256dHash {
804805
}
805806
}
806807

807-
impl Writeable for Signature {
808+
impl Writeable for ecdsa::Signature {
808809
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
809810
self.serialize_compact().write(w)
810811
}
@@ -814,10 +815,30 @@ impl Writeable for Signature {
814815
}
815816
}
816817

817-
impl Readable for Signature {
818+
impl Readable for ecdsa::Signature {
818819
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
819820
let buf: [u8; COMPACT_SIGNATURE_SIZE] = Readable::read(r)?;
820-
match Signature::from_compact(&buf) {
821+
match ecdsa::Signature::from_compact(&buf) {
822+
Ok(sig) => Ok(sig),
823+
Err(_) => return Err(DecodeError::InvalidValue),
824+
}
825+
}
826+
}
827+
828+
impl Writeable for schnorr::Signature {
829+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
830+
self.as_ref().write(w)
831+
}
832+
#[inline]
833+
fn serialized_length(&self) -> usize {
834+
SCHNORR_SIGNATURE_SIZE
835+
}
836+
}
837+
838+
impl Readable for schnorr::Signature {
839+
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
840+
let buf: [u8; SCHNORR_SIGNATURE_SIZE] = Readable::read(r)?;
841+
match schnorr::Signature::from_slice(&buf) {
821842
Ok(sig) => Ok(sig),
822843
Err(_) => return Err(DecodeError::InvalidValue),
823844
}

0 commit comments

Comments
 (0)