Skip to content

Commit f1428fd

Browse files
authored
Merge pull request #1719 from jkczyz/2022-09-offer-encoding
BOLT 12 `offer` encoding and building
2 parents 554fc02 + 8ba09e0 commit f1428fd

File tree

14 files changed

+987
-77
lines changed

14 files changed

+987
-77
lines changed

lightning/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ extern crate core;
7878
pub mod util;
7979
pub mod chain;
8080
pub mod ln;
81+
#[allow(unused)]
82+
mod offers;
8183
pub mod routing;
8284
pub mod onion_message;
8385

lightning/src/ln/channelmanager.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6658,7 +6658,7 @@ impl Writeable for HTLCSource {
66586658
(1, payment_id_opt, option),
66596659
(2, first_hop_htlc_msat, required),
66606660
(3, payment_secret, option),
6661-
(4, path, vec_type),
6661+
(4, *path, vec_type),
66626662
(5, payment_params, option),
66636663
});
66646664
}

lightning/src/ln/features.rs

+30-13
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ mod sealed {
157157
// Byte 2
158158
BasicMPP,
159159
]);
160+
define_context!(OfferContext, []);
160161
// This isn't a "real" feature context, and is only used in the channel_type field in an
161162
// `OpenChannel` message.
162163
define_context!(ChannelTypeContext, [
@@ -366,7 +367,7 @@ mod sealed {
366367
supports_keysend, requires_keysend);
367368

368369
#[cfg(test)]
369-
define_feature!(123456789, UnknownFeature, [NodeContext, ChannelContext, InvoiceContext],
370+
define_feature!(123456789, UnknownFeature, [NodeContext, ChannelContext, InvoiceContext, OfferContext],
370371
"Feature flags for an unknown feature used in testing.", set_unknown_feature_optional,
371372
set_unknown_feature_required, supports_unknown_test_feature, requires_unknown_test_feature);
372373
}
@@ -425,6 +426,8 @@ pub type NodeFeatures = Features<sealed::NodeContext>;
425426
pub type ChannelFeatures = Features<sealed::ChannelContext>;
426427
/// Features used within an invoice.
427428
pub type InvoiceFeatures = Features<sealed::InvoiceContext>;
429+
/// Features used within an offer.
430+
pub type OfferFeatures = Features<sealed::OfferContext>;
428431

429432
/// Features used within the channel_type field in an OpenChannel message.
430433
///
@@ -684,6 +687,15 @@ impl<T: sealed::Wumbo> Features<T> {
684687
}
685688
}
686689

690+
#[cfg(test)]
691+
impl<T: sealed::UnknownFeature> Features<T> {
692+
pub(crate) fn unknown() -> Self {
693+
let mut features = Self::empty();
694+
features.set_unknown_feature_required();
695+
features
696+
}
697+
}
698+
687699
macro_rules! impl_feature_len_prefixed_write {
688700
($features: ident) => {
689701
impl Writeable for $features {
@@ -704,21 +716,26 @@ impl_feature_len_prefixed_write!(ChannelFeatures);
704716
impl_feature_len_prefixed_write!(NodeFeatures);
705717
impl_feature_len_prefixed_write!(InvoiceFeatures);
706718

707-
// Because ChannelTypeFeatures only appears inside of TLVs, it doesn't have a length prefix when
708-
// serialized. Thus, we can't use `impl_feature_len_prefixed_write`, above, and have to write our
709-
// own serialization.
710-
impl Writeable for ChannelTypeFeatures {
711-
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
712-
self.write_be(w)
713-
}
714-
}
715-
impl Readable for ChannelTypeFeatures {
716-
fn read<R: io::Read>(r: &mut R) -> Result<Self, DecodeError> {
717-
let v = io_extras::read_to_end(r)?;
718-
Ok(Self::from_be_bytes(v))
719+
// Some features only appear inside of TLVs, so they don't have a length prefix when serialized.
720+
macro_rules! impl_feature_tlv_write {
721+
($features: ident) => {
722+
impl Writeable for $features {
723+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
724+
self.write_be(w)
725+
}
726+
}
727+
impl Readable for $features {
728+
fn read<R: io::Read>(r: &mut R) -> Result<Self, DecodeError> {
729+
let v = io_extras::read_to_end(r)?;
730+
Ok(Self::from_be_bytes(v))
731+
}
732+
}
719733
}
720734
}
721735

736+
impl_feature_tlv_write!(ChannelTypeFeatures);
737+
impl_feature_tlv_write!(OfferFeatures);
738+
722739
#[cfg(test)]
723740
mod tests {
724741
use super::{ChannelFeatures, ChannelTypeFeatures, InitFeatures, InvoiceFeatures, NodeFeatures, sealed};

lightning/src/offers/mod.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// This file is Copyright its original authors, visible in version control
2+
// history.
3+
//
4+
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5+
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7+
// You may not use this file except in accordance with one or both of these
8+
// licenses.
9+
10+
//! Implementation of Lightning Offers
11+
//! ([BOLT 12](https://github.com/lightning/bolts/blob/master/12-offer-encoding.md)).
12+
//!
13+
//! Offers are a flexible protocol for Lightning payments.
14+
15+
pub mod offer;

0 commit comments

Comments
 (0)