Skip to content

Commit 90e4464

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

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};
@@ -491,7 +492,7 @@ impl_array!(12); // for OnionV2
491492
impl_array!(16); // for IPv6
492493
impl_array!(32); // for channel id & hmac
493494
impl_array!(PUBLIC_KEY_SIZE); // for PublicKey
494-
impl_array!(COMPACT_SIGNATURE_SIZE); // for Signature
495+
impl_array!(64); // for ecdsa::Signature and schnorr::Signature
495496
impl_array!(1300); // for OnionPacket.hop_data
496497

497498
impl Writeable for [u16; 8] {
@@ -656,7 +657,7 @@ impl Readable for Vec<u8> {
656657
Ok(ret)
657658
}
658659
}
659-
impl Writeable for Vec<Signature> {
660+
impl Writeable for Vec<ecdsa::Signature> {
660661
#[inline]
661662
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
662663
(self.len() as u16).write(w)?;
@@ -667,7 +668,7 @@ impl Writeable for Vec<Signature> {
667668
}
668669
}
669670

670-
impl Readable for Vec<Signature> {
671+
impl Readable for Vec<ecdsa::Signature> {
671672
#[inline]
672673
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
673674
let len: u16 = Readable::read(r)?;
@@ -756,7 +757,7 @@ impl Readable for Sha256dHash {
756757
}
757758
}
758759

759-
impl Writeable for Signature {
760+
impl Writeable for ecdsa::Signature {
760761
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
761762
self.serialize_compact().write(w)
762763
}
@@ -766,10 +767,30 @@ impl Writeable for Signature {
766767
}
767768
}
768769

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

0 commit comments

Comments
 (0)