Skip to content

BOLT 12 offer encoding and building #1719

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lightning/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ extern crate core;
pub mod util;
pub mod chain;
pub mod ln;
#[allow(unused)]
mod offers;
pub mod routing;
pub mod onion_message;

Expand Down
2 changes: 1 addition & 1 deletion lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6620,7 +6620,7 @@ impl Writeable for HTLCSource {
(1, payment_id_opt, option),
(2, first_hop_htlc_msat, required),
(3, payment_secret, option),
(4, path, vec_type),
(4, *path, vec_type),
(5, payment_params, option),
});
}
Expand Down
43 changes: 30 additions & 13 deletions lightning/src/ln/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ mod sealed {
// Byte 2
BasicMPP,
]);
define_context!(OfferContext, []);
// This isn't a "real" feature context, and is only used in the channel_type field in an
// `OpenChannel` message.
define_context!(ChannelTypeContext, [
Expand Down Expand Up @@ -366,7 +367,7 @@ mod sealed {
supports_keysend, requires_keysend);

#[cfg(test)]
define_feature!(123456789, UnknownFeature, [NodeContext, ChannelContext, InvoiceContext],
define_feature!(123456789, UnknownFeature, [NodeContext, ChannelContext, InvoiceContext, OfferContext],
"Feature flags for an unknown feature used in testing.", set_unknown_feature_optional,
set_unknown_feature_required, supports_unknown_test_feature, requires_unknown_test_feature);
}
Expand Down Expand Up @@ -425,6 +426,8 @@ pub type NodeFeatures = Features<sealed::NodeContext>;
pub type ChannelFeatures = Features<sealed::ChannelContext>;
/// Features used within an invoice.
pub type InvoiceFeatures = Features<sealed::InvoiceContext>;
/// Features used within an offer.
pub type OfferFeatures = Features<sealed::OfferContext>;

/// Features used within the channel_type field in an OpenChannel message.
///
Expand Down Expand Up @@ -684,6 +687,15 @@ impl<T: sealed::Wumbo> Features<T> {
}
}

#[cfg(test)]
impl<T: sealed::UnknownFeature> Features<T> {
pub(crate) fn unknown() -> Self {
let mut features = Self::empty();
features.set_unknown_feature_required();
features
}
}

macro_rules! impl_feature_len_prefixed_write {
($features: ident) => {
impl Writeable for $features {
Expand All @@ -704,21 +716,26 @@ impl_feature_len_prefixed_write!(ChannelFeatures);
impl_feature_len_prefixed_write!(NodeFeatures);
impl_feature_len_prefixed_write!(InvoiceFeatures);

// Because ChannelTypeFeatures only appears inside of TLVs, it doesn't have a length prefix when
// serialized. Thus, we can't use `impl_feature_len_prefixed_write`, above, and have to write our
// own serialization.
impl Writeable for ChannelTypeFeatures {
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
self.write_be(w)
}
}
impl Readable for ChannelTypeFeatures {
fn read<R: io::Read>(r: &mut R) -> Result<Self, DecodeError> {
let v = io_extras::read_to_end(r)?;
Ok(Self::from_be_bytes(v))
// Some features only appear inside of TLVs, so they don't have a length prefix when serialized.
macro_rules! impl_feature_tlv_write {
($features: ident) => {
impl Writeable for $features {
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
self.write_be(w)
}
}
impl Readable for $features {
fn read<R: io::Read>(r: &mut R) -> Result<Self, DecodeError> {
let v = io_extras::read_to_end(r)?;
Ok(Self::from_be_bytes(v))
}
}
}
}

impl_feature_tlv_write!(ChannelTypeFeatures);
impl_feature_tlv_write!(OfferFeatures);

#[cfg(test)]
mod tests {
use super::{ChannelFeatures, ChannelTypeFeatures, InitFeatures, InvoiceFeatures, NodeFeatures, sealed};
Expand Down
15 changes: 15 additions & 0 deletions lightning/src/offers/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// This file is Copyright its original authors, visible in version control
// history.
//
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
// You may not use this file except in accordance with one or both of these
// licenses.

//! Implementation of Lightning Offers
//! ([BOLT 12](https://github.com/lightning/bolts/blob/master/12-offer-encoding.md)).
//!
//! Offers are a flexible protocol for Lightning payments.

pub mod offer;
Loading