Skip to content

Commit 11c6f06

Browse files
committed
Schnorr Signature and XOnlyPublicKey serialization
BOLT 12 uses Schnorr signatures and x-only pubkeys in the offers message, which needs to be serialized.
1 parent 3a45ad7 commit 11c6f06

File tree

1 file changed

+51
-10
lines changed

1 file changed

+51
-10
lines changed

lightning/src/util/ser.rs

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ use core::cmp;
1919
use core::convert::TryFrom;
2020
use core::ops::Deref;
2121

22-
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;
22+
use bitcoin::secp256k1::{PublicKey, SecretKey, XOnlyPublicKey};
23+
use bitcoin::secp256k1::constants::{PUBLIC_KEY_SIZE, SECRET_KEY_SIZE, COMPACT_SIGNATURE_SIZE, SCHNORR_PUBLIC_KEY_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;
@@ -510,9 +511,9 @@ impl_array!(3); // for rgb
510511
impl_array!(4); // for IPv4
511512
impl_array!(12); // for OnionV2
512513
impl_array!(16); // for IPv6
513-
impl_array!(32); // for channel id & hmac
514+
impl_array!(32); // for channel id, hmac, and XOnlyPublicKey
514515
impl_array!(PUBLIC_KEY_SIZE); // for PublicKey
515-
impl_array!(COMPACT_SIGNATURE_SIZE); // for Signature
516+
impl_array!(64); // for ecdsa::Signature and schnorr::Signature
516517
impl_array!(1300); // for OnionPacket.hop_data
517518

518519
// HashMap
@@ -601,7 +602,7 @@ impl Readable for Vec<u8> {
601602
Ok(ret)
602603
}
603604
}
604-
impl Writeable for Vec<Signature> {
605+
impl Writeable for Vec<ecdsa::Signature> {
605606
#[inline]
606607
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
607608
(self.len() as u16).write(w)?;
@@ -612,7 +613,7 @@ impl Writeable for Vec<Signature> {
612613
}
613614
}
614615

615-
impl Readable for Vec<Signature> {
616+
impl Readable for Vec<ecdsa::Signature> {
616617
#[inline]
617618
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
618619
let len: u16 = Readable::read(r)?;
@@ -701,7 +702,7 @@ impl Readable for Sha256dHash {
701702
}
702703
}
703704

704-
impl Writeable for Signature {
705+
impl Writeable for ecdsa::Signature {
705706
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
706707
self.serialize_compact().write(w)
707708
}
@@ -711,16 +712,56 @@ impl Writeable for Signature {
711712
}
712713
}
713714

714-
impl Readable for Signature {
715+
impl Readable for ecdsa::Signature {
715716
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
716717
let buf: [u8; COMPACT_SIGNATURE_SIZE] = Readable::read(r)?;
717-
match Signature::from_compact(&buf) {
718+
match ecdsa::Signature::from_compact(&buf) {
718719
Ok(sig) => Ok(sig),
719720
Err(_) => return Err(DecodeError::InvalidValue),
720721
}
721722
}
722723
}
723724

725+
impl Writeable for schnorr::Signature {
726+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
727+
self.as_ref().write(w)
728+
}
729+
#[inline]
730+
fn serialized_length(&self) -> usize {
731+
SCHNORR_SIGNATURE_SIZE
732+
}
733+
}
734+
735+
impl Readable for schnorr::Signature {
736+
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
737+
let buf: [u8; SCHNORR_SIGNATURE_SIZE] = Readable::read(r)?;
738+
match schnorr::Signature::from_slice(&buf) {
739+
Ok(sig) => Ok(sig),
740+
Err(_) => return Err(DecodeError::InvalidValue),
741+
}
742+
}
743+
}
744+
745+
impl Writeable for XOnlyPublicKey {
746+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
747+
self.serialize().write(w)
748+
}
749+
#[inline]
750+
fn serialized_length(&self) -> usize {
751+
SCHNORR_PUBLIC_KEY_SIZE
752+
}
753+
}
754+
755+
impl Readable for XOnlyPublicKey {
756+
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
757+
let buf: [u8; SCHNORR_PUBLIC_KEY_SIZE] = Readable::read(r)?;
758+
match XOnlyPublicKey::from_slice(&buf) {
759+
Ok(key) => Ok(key),
760+
Err(_) => return Err(DecodeError::InvalidValue),
761+
}
762+
}
763+
}
764+
724765
impl Writeable for PaymentPreimage {
725766
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
726767
self.0.write(w)

0 commit comments

Comments
 (0)