Skip to content

Commit b3bfbf9

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

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
/// For variable-length values within TLV record where the length is encoded as part of the record.
@@ -632,7 +633,7 @@ impl Readable for Vec<u8> {
632633
Ok(ret)
633634
}
634635
}
635-
impl Writeable for Vec<Signature> {
636+
impl Writeable for Vec<ecdsa::Signature> {
636637
#[inline]
637638
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
638639
(self.len() as u16).write(w)?;
@@ -643,7 +644,7 @@ impl Writeable for Vec<Signature> {
643644
}
644645
}
645646

646-
impl Readable for Vec<Signature> {
647+
impl Readable for Vec<ecdsa::Signature> {
647648
#[inline]
648649
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
649650
let len: u16 = Readable::read(r)?;
@@ -732,7 +733,7 @@ impl Readable for Sha256dHash {
732733
}
733734
}
734735

735-
impl Writeable for Signature {
736+
impl Writeable for ecdsa::Signature {
736737
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
737738
self.serialize_compact().write(w)
738739
}
@@ -742,10 +743,30 @@ impl Writeable for Signature {
742743
}
743744
}
744745

745-
impl Readable for Signature {
746+
impl Readable for ecdsa::Signature {
746747
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
747748
let buf: [u8; COMPACT_SIGNATURE_SIZE] = Readable::read(r)?;
748-
match Signature::from_compact(&buf) {
749+
match ecdsa::Signature::from_compact(&buf) {
750+
Ok(sig) => Ok(sig),
751+
Err(_) => return Err(DecodeError::InvalidValue),
752+
}
753+
}
754+
}
755+
756+
impl Writeable for schnorr::Signature {
757+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
758+
self.as_ref().write(w)
759+
}
760+
#[inline]
761+
fn serialized_length(&self) -> usize {
762+
SCHNORR_SIGNATURE_SIZE
763+
}
764+
}
765+
766+
impl Readable for schnorr::Signature {
767+
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
768+
let buf: [u8; SCHNORR_SIGNATURE_SIZE] = Readable::read(r)?;
769+
match schnorr::Signature::from_slice(&buf) {
749770
Ok(sig) => Ok(sig),
750771
Err(_) => return Err(DecodeError::InvalidValue),
751772
}

0 commit comments

Comments
 (0)