+use bitcoin::bech32;
+use bitcoin::bech32::{Base32Len, FromBase32, ToBase32, u5, WriteBase32};
+use crate::ln::msgs::DecodeError;
+use crate::util::ser::{Readable, WithoutLength, Writeable, Writer};
+
+mod sealed {
+ use crate::prelude::*;
+ use crate::ln::features::Features;
+
+ /// The context in which [`Features`] are applicable. Defines which features are known to the
+ /// implementation, though specification of them as required or optional is up to the code
+ /// constructing a features object.
+ pub trait Context {
+ /// Bitmask for selecting features that are known to the implementation.
+ const KNOWN_FEATURE_MASK: &'static [u8];
+ }
+
+ /// Defines a [`Context`] by stating which features it requires and which are optional. Features
+ /// are specified as a comma-separated list of bytes where each byte is a pipe-delimited list of
+ /// feature identifiers.
+ macro_rules! define_context {
+ ($context: ident, [$( $( $known_feature: ident )|*, )*]) => {
+ #[derive(Eq, PartialEq)]
+ pub struct $context {}
+
+ impl Context for $context {
+ const KNOWN_FEATURE_MASK: &'static [u8] = &[
+ $(
+ 0b00_00_00_00 $(|
+ <Self as $known_feature>::REQUIRED_MASK |
+ <Self as $known_feature>::OPTIONAL_MASK)*,
+ )*
+ ];
+ }
+
+ impl alloc::fmt::Display for Features<$context> {
+ fn fmt(&self, fmt: &mut alloc::fmt::Formatter) -> Result<(), alloc::fmt::Error> {
+ $(
+ $(
+ fmt.write_fmt(format_args!("{}: {}, ", stringify!($known_feature),
+ if <$context as $known_feature>::requires_feature(&self.flags) { "required" }
+ else if <$context as $known_feature>::supports_feature(&self.flags) { "supported" }
+ else { "not supported" }))?;
+ )*
+ {} // Rust gets mad if we only have a $()* block here, so add a dummy {}
+ )*
+ fmt.write_fmt(format_args!("unknown flags: {}",
+ if self.requires_unknown_bits() { "required" }
+ else if self.supports_unknown_bits() { "supported" } else { "none" }))
+ }
+ }
+ };
+ }
+
+ define_context!(InitContext, [
+ // Byte 0
+ DataLossProtect | InitialRoutingSync | UpfrontShutdownScript | GossipQueries,
+ // Byte 1
+ VariableLengthOnion | StaticRemoteKey | PaymentSecret,
+ // Byte 2
+ BasicMPP | Wumbo | AnchorsZeroFeeHtlcTx,
+ // Byte 3
+ ShutdownAnySegwit,
+ // Byte 4
+ OnionMessages,
+ // Byte 5
+ ChannelType | SCIDPrivacy,
+ // Byte 6
+ ZeroConf,
+ ]);
+ define_context!(NodeContext, [
+ // Byte 0
+ DataLossProtect | UpfrontShutdownScript | GossipQueries,
+ // Byte 1
+ VariableLengthOnion | StaticRemoteKey | PaymentSecret,
+ // Byte 2
+ BasicMPP | Wumbo | AnchorsZeroFeeHtlcTx,
+ // Byte 3
+ ShutdownAnySegwit,
+ // Byte 4
+ OnionMessages,
+ // Byte 5
+ ChannelType | SCIDPrivacy,
+ // Byte 6
+ ZeroConf | Keysend,
+ ]);
+ define_context!(ChannelContext, []);
+ define_context!(InvoiceContext, [
+ // Byte 0
+ ,
+ // Byte 1
+ VariableLengthOnion | PaymentSecret,
+ // Byte 2
+ BasicMPP,
+ // Byte 3
+ ,
+ // Byte 4
+ ,
+ // Byte 5
+ ,
+ // Byte 6
+ PaymentMetadata,
+ ]);
+ define_context!(OfferContext, []);
+ define_context!(InvoiceRequestContext, []);
+ define_context!(Bolt12InvoiceContext, [
+ // Byte 0
+ ,
+ // Byte 1
+ ,
+ // Byte 2
+ BasicMPP,
+ ]);
+ define_context!(BlindedHopContext, []);
+ // This isn't a "real" feature context, and is only used in the channel_type field in an
+ // `OpenChannel` message.
+ define_context!(ChannelTypeContext, [
+ // Byte 0
+ ,
+ // Byte 1
+ StaticRemoteKey,
+ // Byte 2
+ AnchorsZeroFeeHtlcTx,
+ // Byte 3
+ ,
+ // Byte 4
+ ,
+ // Byte 5
+ SCIDPrivacy,
+ // Byte 6
+ ZeroConf,
+ ]);
+
+ /// Defines a feature with the given bits for the specified [`Context`]s. The generated trait is
+ /// useful for manipulating feature flags.
+ macro_rules! define_feature {
+ ($odd_bit: expr, $feature: ident, [$($context: ty),+], $doc: expr, $optional_setter: ident,
+ $required_setter: ident, $supported_getter: ident) => {
+ #[doc = $doc]
+ ///
+ /// See [BOLT #9] for details.
+ ///
+ /// [BOLT #9]: https://github.com/lightning/bolts/blob/master/09-features.md
+ pub trait $feature: Context {
+ /// The bit used to signify that the feature is required.
+ const EVEN_BIT: usize = $odd_bit - 1;
+
+ /// The bit used to signify that the feature is optional.
+ const ODD_BIT: usize = $odd_bit;
+
+ /// Assertion that [`EVEN_BIT`] is actually even.
+ ///
+ /// [`EVEN_BIT`]: #associatedconstant.EVEN_BIT
+ const ASSERT_EVEN_BIT_PARITY: usize;
+
+ /// Assertion that [`ODD_BIT`] is actually odd.
+ ///
+ /// [`ODD_BIT`]: #associatedconstant.ODD_BIT
+ const ASSERT_ODD_BIT_PARITY: usize;
+
+ /// Assertion that the bits are set in the context's [`KNOWN_FEATURE_MASK`].
+ ///
+ /// [`KNOWN_FEATURE_MASK`]: Context::KNOWN_FEATURE_MASK
+ #[cfg(not(test))] // We violate this constraint with `UnknownFeature`
+ const ASSERT_BITS_IN_MASK: u8;
+
+ /// The byte where the feature is set.
+ const BYTE_OFFSET: usize = Self::EVEN_BIT / 8;
+
+ /// The bitmask for the feature's required flag relative to the [`BYTE_OFFSET`].
+ ///
+ /// [`BYTE_OFFSET`]: #associatedconstant.BYTE_OFFSET
+ const REQUIRED_MASK: u8 = 1 << (Self::EVEN_BIT - 8 * Self::BYTE_OFFSET);
+
+ /// The bitmask for the feature's optional flag relative to the [`BYTE_OFFSET`].
+ ///
+ /// [`BYTE_OFFSET`]: #associatedconstant.BYTE_OFFSET
+ const OPTIONAL_MASK: u8 = 1 << (Self::ODD_BIT - 8 * Self::BYTE_OFFSET);
+
+ /// Returns whether the feature is required by the given flags.
+ #[inline]
+ fn requires_feature(flags: &Vec<u8>) -> bool {
+ flags.len() > Self::BYTE_OFFSET &&
+ (flags[Self::BYTE_OFFSET] & Self::REQUIRED_MASK) != 0
+ }
+
+ /// Returns whether the feature is supported by the given flags.
+ #[inline]
+ fn supports_feature(flags: &Vec<u8>) -> bool {
+ flags.len() > Self::BYTE_OFFSET &&
+ (flags[Self::BYTE_OFFSET] & (Self::REQUIRED_MASK | Self::OPTIONAL_MASK)) != 0
+ }
+
+ /// Sets the feature's required (even) bit in the given flags.
+ #[inline]
+ fn set_required_bit(flags: &mut Vec<u8>) {
+ if flags.len() <= Self::BYTE_OFFSET {
+ flags.resize(Self::BYTE_OFFSET + 1, 0u8);
+ }
+
+ flags[Self::BYTE_OFFSET] |= Self::REQUIRED_MASK;
+ flags[Self::BYTE_OFFSET] &= !Self::OPTIONAL_MASK;
+ }
+
+ /// Sets the feature's optional (odd) bit in the given flags.
+ #[inline]
+ fn set_optional_bit(flags: &mut Vec<u8>) {
+ if flags.len() <= Self::BYTE_OFFSET {
+ flags.resize(Self::BYTE_OFFSET + 1, 0u8);
+ }
+
+ flags[Self::BYTE_OFFSET] |= Self::OPTIONAL_MASK;
+ }
+
+ /// Clears the feature's required (even) and optional (odd) bits from the given
+ /// flags.
+ #[inline]
+ fn clear_bits(flags: &mut Vec<u8>) {
+ if flags.len() > Self::BYTE_OFFSET {
+ flags[Self::BYTE_OFFSET] &= !Self::REQUIRED_MASK;
+ flags[Self::BYTE_OFFSET] &= !Self::OPTIONAL_MASK;
+ }
+
+ let last_non_zero_byte = flags.iter().rposition(|&byte| byte != 0);
+ let size = if let Some(offset) = last_non_zero_byte { offset + 1 } else { 0 };
+ flags.resize(size, 0u8);
+ }
+ }
+
+ impl <T: $feature> Features<T> {
+ /// Set this feature as optional.
+ pub fn $optional_setter(&mut self) {
+ <T as $feature>::set_optional_bit(&mut self.flags);
+ }
+
+ /// Set this feature as required.
+ pub fn $required_setter(&mut self) {
+ <T as $feature>::set_required_bit(&mut self.flags);
+ }
+
+ /// Checks if this feature is supported.
+ pub fn $supported_getter(&self) -> bool {
+ <T as $feature>::supports_feature(&self.flags)
+ }
+ }
+
+ $(
+ impl $feature for $context {
+ // EVEN_BIT % 2 == 0
+ const ASSERT_EVEN_BIT_PARITY: usize = 0 - (<Self as $feature>::EVEN_BIT % 2);
+
+ // ODD_BIT % 2 == 1
+ const ASSERT_ODD_BIT_PARITY: usize = (<Self as $feature>::ODD_BIT % 2) - 1;
+
+ // (byte & (REQUIRED_MASK | OPTIONAL_MASK)) >> (EVEN_BIT % 8) == 3
+ #[cfg(not(test))] // We violate this constraint with `UnknownFeature`
+ const ASSERT_BITS_IN_MASK: u8 =
+ ((<$context>::KNOWN_FEATURE_MASK[<Self as $feature>::BYTE_OFFSET] & (<Self as $feature>::REQUIRED_MASK | <Self as $feature>::OPTIONAL_MASK))
+ >> (<Self as $feature>::EVEN_BIT % 8)) - 3;
+ }
+ )*
+ };
+ ($odd_bit: expr, $feature: ident, [$($context: ty),+], $doc: expr, $optional_setter: ident,
+ $required_setter: ident, $supported_getter: ident, $required_getter: ident) => {
+ define_feature!($odd_bit, $feature, [$($context),+], $doc, $optional_setter, $required_setter, $supported_getter);
+ impl <T: $feature> Features<T> {
+ /// Checks if this feature is required.
+ pub fn $required_getter(&self) -> bool {
+ <T as $feature>::requires_feature(&self.flags)
+ }
+ }
+ }
+ }
+
+ define_feature!(1, DataLossProtect, [InitContext, NodeContext],
+ "Feature flags for `option_data_loss_protect`.", set_data_loss_protect_optional,
+ set_data_loss_protect_required, supports_data_loss_protect, requires_data_loss_protect);
+ // NOTE: Per Bolt #9, initial_routing_sync has no even bit.
+ define_feature!(3, InitialRoutingSync, [InitContext], "Feature flags for `initial_routing_sync`.",
+ set_initial_routing_sync_optional, set_initial_routing_sync_required,
+ initial_routing_sync);
+ define_feature!(5, UpfrontShutdownScript, [InitContext, NodeContext],
+ "Feature flags for `option_upfront_shutdown_script`.", set_upfront_shutdown_script_optional,
+ set_upfront_shutdown_script_required, supports_upfront_shutdown_script,
+ requires_upfront_shutdown_script);
+ define_feature!(7, GossipQueries, [InitContext, NodeContext],
+ "Feature flags for `gossip_queries`.", set_gossip_queries_optional, set_gossip_queries_required,
+ supports_gossip_queries, requires_gossip_queries);
+ define_feature!(9, VariableLengthOnion, [InitContext, NodeContext, InvoiceContext],
+ "Feature flags for `var_onion_optin`.", set_variable_length_onion_optional,
+ set_variable_length_onion_required, supports_variable_length_onion,
+ requires_variable_length_onion);
+ define_feature!(13, StaticRemoteKey, [InitContext, NodeContext, ChannelTypeContext],
+ "Feature flags for `option_static_remotekey`.", set_static_remote_key_optional,
+ set_static_remote_key_required, supports_static_remote_key, requires_static_remote_key);
+ define_feature!(15, PaymentSecret, [InitContext, NodeContext, InvoiceContext],
+ "Feature flags for `payment_secret`.", set_payment_secret_optional, set_payment_secret_required,
+ supports_payment_secret, requires_payment_secret);
+ define_feature!(17, BasicMPP, [InitContext, NodeContext, InvoiceContext, Bolt12InvoiceContext],
+ "Feature flags for `basic_mpp`.", set_basic_mpp_optional, set_basic_mpp_required,
+ supports_basic_mpp, requires_basic_mpp);
+ define_feature!(19, Wumbo, [InitContext, NodeContext],
+ "Feature flags for `option_support_large_channel` (aka wumbo channels).", set_wumbo_optional, set_wumbo_required,
+ supports_wumbo, requires_wumbo);
+ define_feature!(23, AnchorsZeroFeeHtlcTx, [InitContext, NodeContext, ChannelTypeContext],
+ "Feature flags for `option_anchors_zero_fee_htlc_tx`.", set_anchors_zero_fee_htlc_tx_optional,
+ set_anchors_zero_fee_htlc_tx_required, supports_anchors_zero_fee_htlc_tx, requires_anchors_zero_fee_htlc_tx);
+ define_feature!(27, ShutdownAnySegwit, [InitContext, NodeContext],
+ "Feature flags for `opt_shutdown_anysegwit`.", set_shutdown_any_segwit_optional,
+ set_shutdown_any_segwit_required, supports_shutdown_anysegwit, requires_shutdown_anysegwit);
+ define_feature!(39, OnionMessages, [InitContext, NodeContext],
+ "Feature flags for `option_onion_messages`.", set_onion_messages_optional,
+ set_onion_messages_required, supports_onion_messages, requires_onion_messages);
+ define_feature!(45, ChannelType, [InitContext, NodeContext],
+ "Feature flags for `option_channel_type`.", set_channel_type_optional,
+ set_channel_type_required, supports_channel_type, requires_channel_type);
+ define_feature!(47, SCIDPrivacy, [InitContext, NodeContext, ChannelTypeContext],
+ "Feature flags for only forwarding with SCID aliasing. Called `option_scid_alias` in the BOLTs",
+ set_scid_privacy_optional, set_scid_privacy_required, supports_scid_privacy, requires_scid_privacy);
+ define_feature!(49, PaymentMetadata, [InvoiceContext],
+ "Feature flags for payment metadata in invoices.", set_payment_metadata_optional,
+ set_payment_metadata_required, supports_payment_metadata, requires_payment_metadata);
+ define_feature!(51, ZeroConf, [InitContext, NodeContext, ChannelTypeContext],
+ "Feature flags for accepting channels with zero confirmations. Called `option_zeroconf` in the BOLTs",
+ set_zero_conf_optional, set_zero_conf_required, supports_zero_conf, requires_zero_conf);
+ define_feature!(55, Keysend, [NodeContext],
+ "Feature flags for keysend payments.", set_keysend_optional, set_keysend_required,
+ supports_keysend, requires_keysend);
+ // Note: update the module-level docs when a new feature bit is added!
+
+ #[cfg(test)]
+ define_feature!(123456789, UnknownFeature,
+ [NodeContext, ChannelContext, InvoiceContext, OfferContext, InvoiceRequestContext, Bolt12InvoiceContext, BlindedHopContext],
+ "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);
+}