| Copyright | (c) 2025 Jared Tobin |
|---|---|
| License | MIT |
| Maintainer | Jared Tobin <jared@ppad.tech> |
| Safe Haskell | Safe-Inferred |
| Language | Haskell2010 |
Lightning.Protocol.BOLT9
Description
Feature flags for the Lightning Network, per BOLT #9.
A feature vector is a big-endian bit field. Features are assigned pairs of bits: setting the even bit means the feature is required, setting the odd bit means it is optional (it's ok to be odd).
The examples below assume:
>>>:set -XOverloadedStrings>>>import Lightning.Protocol.BOLT9
Build a vector, check it before sending it, and render it:
>>>let fv = set_feature_with_deps OptionZeroconf Optional empty>>>list_features fv[(OptionScidAlias,Optional),(OptionZeroconf,Optional)]>>>validate_local InitContext fvRight ()>>>render fv"\b\128\NUL\NUL\NUL\NUL\NUL"
Check a vector received from a peer:
>>>validate_remote InitContext (parse "\DLE\NUL\NUL")Left (UnknownBit 20 :| [])>>>validate_remote InitContext (parse "\STX\NUL\NUL")Right ()
Synopsis
- data FeatureVector
- parse :: ByteString -> FeatureVector
- render :: FeatureVector -> ByteString
- empty :: FeatureVector
- union :: FeatureVector -> FeatureVector -> FeatureVector
- set_bit :: Int -> FeatureVector -> FeatureVector
- clear_bit :: Int -> FeatureVector -> FeatureVector
- test_bit :: Int -> FeatureVector -> Bool
- set_bits :: FeatureVector -> [Int]
- data Feature
- = OptionDataLossProtect
- | OptionUpfrontShutdownScript
- | GossipQueries
- | VarOnionOptin
- | GossipQueriesEx
- | OptionStaticRemotekey
- | PaymentSecret
- | BasicMpp
- | OptionSupportLargeChannel
- | OptionAnchors
- | OptionRouteBlinding
- | OptionShutdownAnysegwit
- | OptionDualFund
- | OptionQuiesce
- | OptionAttributionData
- | OptionOnionMessages
- | ZeroFeeCommitments
- | OptionProvideStorage
- | OptionChannelType
- | OptionScidAlias
- | OptionPaymentMetadata
- | OptionZeroconf
- | OptionSimpleClose
- | OptionSplice
- | OptionOnionMessagesOnlyChannels
- known_features :: [Feature]
- feature_by_bit :: Int -> Maybe Feature
- feature_bit :: Feature -> Int
- feature_name :: Feature -> String
- feature_contexts :: Feature -> [Context]
- feature_dependencies :: Feature -> [Feature]
- feature_assumed :: Feature -> Bool
- data FeatureLevel
- set_feature :: Feature -> FeatureLevel -> FeatureVector -> FeatureVector
- set_feature_with_deps :: Feature -> FeatureLevel -> FeatureVector -> FeatureVector
- clear_feature :: Feature -> FeatureVector -> FeatureVector
- test_feature :: Feature -> FeatureVector -> Maybe FeatureLevel
- list_features :: FeatureVector -> [(Feature, FeatureLevel)]
- data Context
- data ValidationError
- validate_local :: Context -> FeatureVector -> Either (NonEmpty ValidationError) ()
- validate_remote :: Context -> FeatureVector -> Either (NonEmpty ValidationError) ()
- data ChannelType = ChannelType {
- ct_basic :: !BasicChannelType
- ct_scid_alias :: !Bool
- ct_zeroconf :: !Bool
- data BasicChannelType
- channel_type :: FeatureVector -> Maybe ChannelType
- channel_type_features :: ChannelType -> FeatureVector
Feature vectors
data FeatureVector Source #
A feature vector.
Bit 0 is the least significant bit of the last byte. A vector
produced by parse keeps its bytes exactly, so render gives
them back unchanged (signatures over gossip messages cover them).
Every other operation produces a minimally-encoded vector, i.e.
one without leading zero bytes.
Equality ignores leading zero bytes.
>>>parse "\NUL\STX" == parse "\STX"True>>>set_bit 1 emptyparse "\STX"
Instances
| NFData FeatureVector Source # | |
Defined in Lightning.Protocol.BOLT9 Methods rnf :: FeatureVector -> () # | |
| Show FeatureVector Source # | |
Defined in Lightning.Protocol.BOLT9 Methods showsPrec :: Int -> FeatureVector -> ShowS # show :: FeatureVector -> String # showList :: [FeatureVector] -> ShowS # | |
| Eq FeatureVector Source # | |
Defined in Lightning.Protocol.BOLT9 Methods (==) :: FeatureVector -> FeatureVector -> Bool # (/=) :: FeatureVector -> FeatureVector -> Bool # | |
parse :: ByteString -> FeatureVector Source #
Parse a feature vector from its wire bytes. Every byte string is a valid feature vector.
>>>test_bit 9 (parse "\STX\NUL")True
render :: FeatureVector -> ByteString Source #
The wire bytes of a feature vector: the original bytes of a vector
produced by parse, and the minimal encoding of any other.
>>>render (parse "\NUL\STX")"\NUL\STX">>>render (union (parse "\NUL\STX") empty)"\STX"
empty :: FeatureVector Source #
The empty feature vector.
>>>render empty""
union :: FeatureVector -> FeatureVector -> FeatureVector Source #
The bitwise OR of two feature vectors.
BOLT #1 requires the receiver of an init message to combine its
globalfeatures and features fields this way.
>>>set_bits (union (parse "\STX") (parse "\SOH\NUL"))[1,8]
Bits
set_bit :: Int -> FeatureVector -> FeatureVector Source #
Set the bit at the given index. Negative indices are ignored.
>>>render (set_bit 9 empty)"\STX\NUL"
clear_bit :: Int -> FeatureVector -> FeatureVector Source #
Clear the bit at the given index. Negative indices are ignored.
>>>render (clear_bit 9 (parse "\STX\SOH"))"\SOH"
test_bit :: Int -> FeatureVector -> Bool Source #
Test the bit at the given index. Negative indices are never set.
>>>test_bit 9 (parse "\STX\NUL")True>>>test_bit 8 (parse "\STX\NUL")False
set_bits :: FeatureVector -> [Int] Source #
The indices of all set bits, in ascending order.
>>>set_bits (parse "\STX\NUL\SOH")[0,17]
Known features
The features assigned in the BOLT #9 table.
Constructors
| OptionDataLossProtect | 0/1 |
| OptionUpfrontShutdownScript | 4/5 |
| GossipQueries | 6/7 |
| VarOnionOptin | 8/9 |
| GossipQueriesEx | 10/11 |
| OptionStaticRemotekey | 12/13 |
| PaymentSecret | 14/15 |
| BasicMpp | 16/17 |
| OptionSupportLargeChannel | 18/19 |
| OptionAnchors | 22/23 |
| OptionRouteBlinding | 24/25 |
| OptionShutdownAnysegwit | 26/27 |
| OptionDualFund | 28/29 |
| OptionQuiesce | 34/35 |
| OptionAttributionData | 36/37 |
| OptionOnionMessages | 38/39 |
| ZeroFeeCommitments | 40/41 |
| OptionProvideStorage | 42/43 |
| OptionChannelType | 44/45 |
| OptionScidAlias | 46/47 |
| OptionPaymentMetadata | 48/49 |
| OptionZeroconf | 50/51 |
| OptionSimpleClose | 60/61 |
| OptionSplice | 62/63 |
| OptionOnionMessagesOnlyChannels | 66/67 |
Instances
| NFData Feature Source # | |||||
Defined in Lightning.Protocol.BOLT9 | |||||
| Generic Feature Source # | |||||
Defined in Lightning.Protocol.BOLT9 Associated Types
| |||||
| Show Feature Source # | |||||
| Eq Feature Source # | |||||
| Ord Feature Source # | |||||
Defined in Lightning.Protocol.BOLT9 | |||||
| type Rep Feature Source # | |||||
Defined in Lightning.Protocol.BOLT9 type Rep Feature = D1 ('MetaData "Feature" "Lightning.Protocol.BOLT9" "ppad-bolt9-0.1.0-2pi7ArXFPy5EEr7bj10TmI" 'False) ((((C1 ('MetaCons "OptionDataLossProtect" 'PrefixI 'False) (U1 :: Type -> Type) :+: (C1 ('MetaCons "OptionUpfrontShutdownScript" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "GossipQueries" 'PrefixI 'False) (U1 :: Type -> Type))) :+: (C1 ('MetaCons "VarOnionOptin" 'PrefixI 'False) (U1 :: Type -> Type) :+: (C1 ('MetaCons "GossipQueriesEx" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "OptionStaticRemotekey" 'PrefixI 'False) (U1 :: Type -> Type)))) :+: ((C1 ('MetaCons "PaymentSecret" 'PrefixI 'False) (U1 :: Type -> Type) :+: (C1 ('MetaCons "BasicMpp" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "OptionSupportLargeChannel" 'PrefixI 'False) (U1 :: Type -> Type))) :+: (C1 ('MetaCons "OptionAnchors" 'PrefixI 'False) (U1 :: Type -> Type) :+: (C1 ('MetaCons "OptionRouteBlinding" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "OptionShutdownAnysegwit" 'PrefixI 'False) (U1 :: Type -> Type))))) :+: (((C1 ('MetaCons "OptionDualFund" 'PrefixI 'False) (U1 :: Type -> Type) :+: (C1 ('MetaCons "OptionQuiesce" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "OptionAttributionData" 'PrefixI 'False) (U1 :: Type -> Type))) :+: (C1 ('MetaCons "OptionOnionMessages" 'PrefixI 'False) (U1 :: Type -> Type) :+: (C1 ('MetaCons "ZeroFeeCommitments" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "OptionProvideStorage" 'PrefixI 'False) (U1 :: Type -> Type)))) :+: ((C1 ('MetaCons "OptionChannelType" 'PrefixI 'False) (U1 :: Type -> Type) :+: (C1 ('MetaCons "OptionScidAlias" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "OptionPaymentMetadata" 'PrefixI 'False) (U1 :: Type -> Type))) :+: ((C1 ('MetaCons "OptionZeroconf" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "OptionSimpleClose" 'PrefixI 'False) (U1 :: Type -> Type)) :+: (C1 ('MetaCons "OptionSplice" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "OptionOnionMessagesOnlyChannels" 'PrefixI 'False) (U1 :: Type -> Type)))))) | |||||
known_features :: [Feature] Source #
All known features, in bit order.
>>>length known_features25
feature_by_bit :: Int -> Maybe Feature Source #
The feature that a bit (even or odd) belongs to, if any.
>>>feature_by_bit 16Just BasicMpp>>>feature_by_bit 17Just BasicMpp>>>feature_by_bit 20Nothing
feature_bit :: Feature -> Int Source #
A feature's even (required) bit. Its odd (optional) bit is the next one up.
>>>feature_bit BasicMpp16
feature_name :: Feature -> String Source #
A feature's name in the BOLT #9 table.
>>>feature_name BasicMpp"basic_mpp"
feature_contexts :: Feature -> [Context] Source #
The contexts the BOLT #9 table lists for a feature, in table order.
The list is empty for ASSUMED features and for GossipQueries.
>>>feature_contexts BasicMpp[InitContext,NodeContext,InvoiceContext]>>>feature_contexts PaymentSecret[]
feature_dependencies :: Feature -> [Feature] Source #
A feature's direct dependencies.
>>>feature_dependencies OptionZeroconf[OptionScidAlias]
feature_assumed :: Feature -> Bool Source #
Whether BOLT #9 marks a feature ASSUMED, i.e. supported by all nodes.
>>>feature_assumed PaymentSecretTrue
Feature operations
data FeatureLevel Source #
The level at which a feature is set.
Instances
| NFData FeatureLevel Source # | |||||
Defined in Lightning.Protocol.BOLT9 Methods rnf :: FeatureLevel -> () # | |||||
| Generic FeatureLevel Source # | |||||
Defined in Lightning.Protocol.BOLT9 Associated Types
| |||||
| Show FeatureLevel Source # | |||||
Defined in Lightning.Protocol.BOLT9 Methods showsPrec :: Int -> FeatureLevel -> ShowS # show :: FeatureLevel -> String # showList :: [FeatureLevel] -> ShowS # | |||||
| Eq FeatureLevel Source # | |||||
Defined in Lightning.Protocol.BOLT9 | |||||
| Ord FeatureLevel Source # | |||||
Defined in Lightning.Protocol.BOLT9 Methods compare :: FeatureLevel -> FeatureLevel -> Ordering # (<) :: FeatureLevel -> FeatureLevel -> Bool # (<=) :: FeatureLevel -> FeatureLevel -> Bool # (>) :: FeatureLevel -> FeatureLevel -> Bool # (>=) :: FeatureLevel -> FeatureLevel -> Bool # max :: FeatureLevel -> FeatureLevel -> FeatureLevel # min :: FeatureLevel -> FeatureLevel -> FeatureLevel # | |||||
| type Rep FeatureLevel Source # | |||||
set_feature :: Feature -> FeatureLevel -> FeatureVector -> FeatureVector Source #
Set a feature at the given level, clearing the other bit of its pair.
>>>set_bits (set_feature BasicMpp Optional empty)[17]>>>set_bits (set_feature BasicMpp Required (set_bit 17 empty))[16]
set_feature_with_deps :: Feature -> FeatureLevel -> FeatureVector -> FeatureVector Source #
Set a feature at the given level, together with its transitive dependencies (ASSUMED ones included). Dependencies that are not yet set are set at the same level; those already set keep theirs.
>>>list_features (set_feature_with_deps BasicMpp Optional empty)[(PaymentSecret,Optional),(BasicMpp,Optional)]
clear_feature :: Feature -> FeatureVector -> FeatureVector Source #
Clear both bits of a feature.
>>>clear_feature BasicMpp (set_feature BasicMpp Optional empty)parse ""
test_feature :: Feature -> FeatureVector -> Maybe FeatureLevel Source #
The level at which a feature is set, if it is.
A feature with both bits set is Required, as BOLT #9 directs the
receiver to treat it.
>>>test_feature BasicMpp (set_bit 17 empty)Just Optional>>>test_feature BasicMpp (set_bit 16 (set_bit 17 empty))Just Required>>>test_feature BasicMpp emptyNothing
list_features :: FeatureVector -> [(Feature, FeatureLevel)] Source #
The known features set in a vector, in bit order.
>>>list_features (parse "\SOH\128\NUL")[(PaymentSecret,Optional),(BasicMpp,Required)]
Validation
A field in which feature bits are presented: the values of the Context column of the BOLT #9 table.
The table's C- and C+ are not further contexts. They mark a
feature presented in channel_announcement as always optional or
always required. No feature at the implemented spec revision is
presented in channel_announcement in any form.
Constructors
| InitContext |
|
| NodeContext |
|
| ChannelContext |
|
| InvoiceContext |
|
| BlindedContext |
|
| ChannelTypeContext |
|
Instances
| NFData Context Source # | |||||
Defined in Lightning.Protocol.BOLT9 | |||||
| Generic Context Source # | |||||
Defined in Lightning.Protocol.BOLT9 Associated Types
| |||||
| Show Context Source # | |||||
| Eq Context Source # | |||||
| Ord Context Source # | |||||
Defined in Lightning.Protocol.BOLT9 | |||||
| type Rep Context Source # | |||||
Defined in Lightning.Protocol.BOLT9 type Rep Context = D1 ('MetaData "Context" "Lightning.Protocol.BOLT9" "ppad-bolt9-0.1.0-2pi7ArXFPy5EEr7bj10TmI" 'False) ((C1 ('MetaCons "InitContext" 'PrefixI 'False) (U1 :: Type -> Type) :+: (C1 ('MetaCons "NodeContext" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "ChannelContext" 'PrefixI 'False) (U1 :: Type -> Type))) :+: (C1 ('MetaCons "InvoiceContext" 'PrefixI 'False) (U1 :: Type -> Type) :+: (C1 ('MetaCons "BlindedContext" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "ChannelTypeContext" 'PrefixI 'False) (U1 :: Type -> Type)))) | |||||
data ValidationError Source #
A rule a feature vector breaks.
Constructors
| UnknownBit !Int | a bit belonging to no known feature is set |
| BothBitsSet !Feature | both bits of a feature are set |
| ContextNotAllowed !Feature !Context | a feature is set in a context that doesn't present it |
| MissingDependency !Feature !Feature | a set feature (first) lacks a dependency (second) |
| InvalidChannelType | a |
Instances
validate_local :: Context -> FeatureVector -> Either (NonEmpty ValidationError) () Source #
Validate a feature vector that we're about to send in the given context, per the origin requirements of BOLT #9 and BOLT #1.
For ChannelTypeContext, the vector must be a channel type that
BOLT #2 defines (see channel_type). For every other context,
checks that:
- no bit outside the known features is set;
- no feature has both bits set;
- each set feature is presented in the context. A feature with an
empty Context column (the ASSUMED features and
GossipQueries) is accepted inInitContext,NodeContextandInvoiceContext, where deployed nodes still present it; - each set feature's dependencies are set. A dependency on an ASSUMED feature counts as met even when its bits are clear.
Errors are reported in that order. No known feature is presented
in ChannelContext or BlindedContext, so only the empty vector
passes there (as BOLT #4 requires of allowed_features).
>>>validate_local InitContext (set_feature OptionZeroconf Optional empty)Left (MissingDependency OptionZeroconf OptionScidAlias :| [])>>>validate_local InitContext (set_feature BasicMpp Optional empty)Right ()>>>validate_local InitContext (set_bit 101 empty)Left (UnknownBit 101 :| [])
validate_remote :: Context -> FeatureVector -> Either (NonEmpty ValidationError) () Source #
Validate a feature vector received in the given context, per the receiver requirements for that context:
InitContext(BOLT #1) andInvoiceContext(BOLT #11): no unknown even bit is set, and each set feature's dependencies are set (ASSUMED dependencies count as met).NodeContextandChannelContext(BOLT #7): no unknown even bit is set. A failure here means the node or channel must not be routed through; the announcement itself is still valid.BlindedContext(BOLT #4): no unknown bit is set, odd or even.ChannelTypeContext(BOLT #2): the vector is a defined channel type (seechannel_type).
Unknown odd bits are otherwise ignored. A feature with both bits
set is not an error; it counts as Required (see test_feature).
Nor is a known feature outside its listed contexts, since that
rule binds only the sender.
>>>validate_remote InitContext (set_bit 101 empty)Right ()>>>validate_remote InitContext (set_bit 100 empty)Left (UnknownBit 100 :| [])>>>validate_remote BlindedContext (set_bit 101 empty)Left (UnknownBit 101 :| [])
Channel types
data ChannelType Source #
A channel type, per BOLT #2: a basic type plus any of the
OptionScidAlias and OptionZeroconf variations.
Channel types are an enumeration that reuses even feature bits;
they aren't subject to the dependency or context rules for
feature vectors. BOLT #2 also forbids OptionScidAlias in the
channel type of a channel to be announced, which needs the
announce_channel flag to check.
Constructors
| ChannelType | |
Fields
| |
Instances
| NFData ChannelType Source # | |||||
Defined in Lightning.Protocol.BOLT9 Methods rnf :: ChannelType -> () # | |||||
| Generic ChannelType Source # | |||||
Defined in Lightning.Protocol.BOLT9 Associated Types
| |||||
| Show ChannelType Source # | |||||
Defined in Lightning.Protocol.BOLT9 Methods showsPrec :: Int -> ChannelType -> ShowS # show :: ChannelType -> String # showList :: [ChannelType] -> ShowS # | |||||
| Eq ChannelType Source # | |||||
Defined in Lightning.Protocol.BOLT9 | |||||
| type Rep ChannelType Source # | |||||
Defined in Lightning.Protocol.BOLT9 type Rep ChannelType = D1 ('MetaData "ChannelType" "Lightning.Protocol.BOLT9" "ppad-bolt9-0.1.0-2pi7ArXFPy5EEr7bj10TmI" 'False) (C1 ('MetaCons "ChannelType" 'PrefixI 'True) (S1 ('MetaSel ('Just "ct_basic") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 BasicChannelType) :*: (S1 ('MetaSel ('Just "ct_scid_alias") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Bool) :*: S1 ('MetaSel ('Just "ct_zeroconf") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Bool)))) | |||||
data BasicChannelType Source #
The basic channel types of BOLT #2.
Constructors
| BasicStaticRemotekey |
|
| BasicAnchors |
|
| BasicZeroFeeCommitments |
|
Instances
| NFData BasicChannelType Source # | |||||
Defined in Lightning.Protocol.BOLT9 Methods rnf :: BasicChannelType -> () # | |||||
| Generic BasicChannelType Source # | |||||
Defined in Lightning.Protocol.BOLT9 Associated Types
Methods from :: BasicChannelType -> Rep BasicChannelType x # to :: Rep BasicChannelType x -> BasicChannelType # | |||||
| Show BasicChannelType Source # | |||||
Defined in Lightning.Protocol.BOLT9 Methods showsPrec :: Int -> BasicChannelType -> ShowS # show :: BasicChannelType -> String # showList :: [BasicChannelType] -> ShowS # | |||||
| Eq BasicChannelType Source # | |||||
Defined in Lightning.Protocol.BOLT9 Methods (==) :: BasicChannelType -> BasicChannelType -> Bool # (/=) :: BasicChannelType -> BasicChannelType -> Bool # | |||||
| Ord BasicChannelType Source # | |||||
Defined in Lightning.Protocol.BOLT9 Methods compare :: BasicChannelType -> BasicChannelType -> Ordering # (<) :: BasicChannelType -> BasicChannelType -> Bool # (<=) :: BasicChannelType -> BasicChannelType -> Bool # (>) :: BasicChannelType -> BasicChannelType -> Bool # (>=) :: BasicChannelType -> BasicChannelType -> Bool # max :: BasicChannelType -> BasicChannelType -> BasicChannelType # min :: BasicChannelType -> BasicChannelType -> BasicChannelType # | |||||
| type Rep BasicChannelType Source # | |||||
Defined in Lightning.Protocol.BOLT9 type Rep BasicChannelType = D1 ('MetaData "BasicChannelType" "Lightning.Protocol.BOLT9" "ppad-bolt9-0.1.0-2pi7ArXFPy5EEr7bj10TmI" 'False) (C1 ('MetaCons "BasicStaticRemotekey" 'PrefixI 'False) (U1 :: Type -> Type) :+: (C1 ('MetaCons "BasicAnchors" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "BasicZeroFeeCommitments" 'PrefixI 'False) (U1 :: Type -> Type))) | |||||
channel_type :: FeatureVector -> Maybe ChannelType Source #
The channel type that a channel_type vector encodes, if it is a
defined one. Leading zero bytes are ignored.
>>>let zeroconf = ChannelType BasicStaticRemotekey False True>>>channel_type (parse "\EOT\NUL\NUL\NUL\NUL\DLE\NUL") == Just zeroconfTrue>>>channel_type (set_bit 13 empty)Nothing
channel_type_features :: ChannelType -> FeatureVector Source #
The channel_type vector for a channel type.
>>>let anchors = ChannelType BasicAnchors False False>>>render (channel_type_features anchors)"@\DLE\NUL"