bolt7

Routing gossip protocol, per BOLT #7 (docs.ppad.tech/bolt7).
git clone git://git.ppad.tech/bolt7.git
Log | Files | Refs | README | LICENSE

Messages.hs (8552B)


      1 {-# OPTIONS_HADDOCK prune #-}
      2 
      3 {-# LANGUAGE BangPatterns #-}
      4 {-# LANGUAGE DeriveGeneric #-}
      5 
      6 -- |
      7 -- Module: Lightning.Protocol.BOLT7.Messages
      8 -- Copyright: (c) 2025 Jared Tobin
      9 -- License: MIT
     10 -- Maintainer: Jared Tobin <jared@ppad.tech>
     11 --
     12 -- BOLT #7 gossip message type definitions.
     13 
     14 module Lightning.Protocol.BOLT7.Messages (
     15   -- * Message types
     16     MsgType(..)
     17   , msgTypeCode
     18 
     19   -- * Channel announcement
     20   , ChannelAnnouncement(..)
     21 
     22   -- * Node announcement
     23   , NodeAnnouncement(..)
     24 
     25   -- * Channel update
     26   , ChannelUpdate(..)
     27 
     28   -- * Announcement signatures
     29   , AnnouncementSignatures(..)
     30 
     31   -- * Query messages
     32   , QueryShortChannelIds(..)
     33   , ReplyShortChannelIdsEnd(..)
     34   , QueryChannelRange(..)
     35   , ReplyChannelRange(..)
     36   , GossipTimestampFilter(..)
     37 
     38   -- * Union type
     39   , Message(..)
     40   ) where
     41 
     42 import Control.DeepSeq (NFData)
     43 import Data.ByteString (ByteString)
     44 import Data.Word (Word8, Word16, Word32)  -- Word8 still used by other messages
     45 import GHC.Generics (Generic)
     46 import Lightning.Protocol.BOLT1 (TlvStream)
     47 import Lightning.Protocol.BOLT7.Types
     48 
     49 -- Message type codes ----------------------------------------------------------
     50 
     51 -- | BOLT #7 message type codes.
     52 data MsgType
     53   = MsgChannelAnnouncement       -- ^ 256
     54   | MsgNodeAnnouncement          -- ^ 257
     55   | MsgChannelUpdate             -- ^ 258
     56   | MsgAnnouncementSignatures    -- ^ 259
     57   | MsgQueryShortChannelIds      -- ^ 261
     58   | MsgReplyShortChannelIdsEnd   -- ^ 262
     59   | MsgQueryChannelRange         -- ^ 263
     60   | MsgReplyChannelRange         -- ^ 264
     61   | MsgGossipTimestampFilter     -- ^ 265
     62   deriving (Eq, Show, Generic)
     63 
     64 instance NFData MsgType
     65 
     66 -- | Get numeric code for message type.
     67 msgTypeCode :: MsgType -> Word16
     68 msgTypeCode MsgChannelAnnouncement     = 256
     69 msgTypeCode MsgNodeAnnouncement        = 257
     70 msgTypeCode MsgChannelUpdate           = 258
     71 msgTypeCode MsgAnnouncementSignatures  = 259
     72 msgTypeCode MsgQueryShortChannelIds    = 261
     73 msgTypeCode MsgReplyShortChannelIdsEnd = 262
     74 msgTypeCode MsgQueryChannelRange       = 263
     75 msgTypeCode MsgReplyChannelRange       = 264
     76 msgTypeCode MsgGossipTimestampFilter   = 265
     77 {-# INLINE msgTypeCode #-}
     78 
     79 -- Channel announcement --------------------------------------------------------
     80 
     81 -- | channel_announcement message (type 256).
     82 --
     83 -- Announces a public channel to the network.
     84 data ChannelAnnouncement = ChannelAnnouncement
     85   { channelAnnNodeSig1     :: !Signature     -- ^ Signature from node_id_1
     86   , channelAnnNodeSig2     :: !Signature     -- ^ Signature from node_id_2
     87   , channelAnnBitcoinSig1  :: !Signature     -- ^ Signature from bitcoin_key_1
     88   , channelAnnBitcoinSig2  :: !Signature     -- ^ Signature from bitcoin_key_2
     89   , channelAnnFeatures     :: !FeatureBits   -- ^ Feature bits
     90   , channelAnnChainHash    :: !ChainHash     -- ^ Chain identifier
     91   , channelAnnShortChanId  :: !ShortChannelId -- ^ Short channel ID
     92   , channelAnnNodeId1      :: !NodeId        -- ^ First node (lexicographically)
     93   , channelAnnNodeId2      :: !NodeId        -- ^ Second node
     94   , channelAnnBitcoinKey1  :: !Point         -- ^ Bitcoin key for node_id_1
     95   , channelAnnBitcoinKey2  :: !Point         -- ^ Bitcoin key for node_id_2
     96   }
     97   deriving (Eq, Show, Generic)
     98 
     99 instance NFData ChannelAnnouncement
    100 
    101 -- Node announcement -----------------------------------------------------------
    102 
    103 -- | node_announcement message (type 257).
    104 --
    105 -- Advertises node metadata to the network.
    106 data NodeAnnouncement = NodeAnnouncement
    107   { nodeAnnSignature  :: !Signature      -- ^ Signature of message
    108   , nodeAnnFeatures   :: !FeatureBits    -- ^ Feature bits
    109   , nodeAnnTimestamp  :: !Timestamp      -- ^ Unix timestamp
    110   , nodeAnnNodeId     :: !NodeId         -- ^ Node public key
    111   , nodeAnnRgbColor   :: !RgbColor       -- ^ RGB color
    112   , nodeAnnAlias      :: !Alias          -- ^ Node alias (32 bytes UTF-8)
    113   , nodeAnnAddresses  :: ![Address]      -- ^ List of addresses
    114   }
    115   deriving (Eq, Show, Generic)
    116 
    117 instance NFData NodeAnnouncement
    118 
    119 -- Channel update --------------------------------------------------------------
    120 
    121 -- | channel_update message (type 258).
    122 --
    123 -- Communicates per-direction routing parameters.
    124 data ChannelUpdate = ChannelUpdate
    125   { chanUpdateSignature      :: !Signature       -- ^ Signature of message
    126   , chanUpdateChainHash      :: !ChainHash       -- ^ Chain identifier
    127   , chanUpdateShortChanId    :: !ShortChannelId  -- ^ Short channel ID
    128   , chanUpdateTimestamp      :: !Timestamp       -- ^ Unix timestamp
    129   , chanUpdateMsgFlags       :: !MessageFlags    -- ^ Message flags
    130   , chanUpdateChanFlags      :: !ChannelFlags    -- ^ Channel flags
    131   , chanUpdateCltvExpDelta   :: !CltvExpiryDelta -- ^ CLTV expiry delta
    132   , chanUpdateHtlcMinMsat    :: !HtlcMinimumMsat -- ^ Minimum HTLC msat
    133   , chanUpdateFeeBaseMsat    :: !FeeBaseMsat     -- ^ Base fee msat
    134   , chanUpdateFeeProportional :: !FeeProportionalMillionths -- ^ Prop fee
    135   , chanUpdateHtlcMaxMsat    :: !(Maybe HtlcMaximumMsat) -- ^ Max HTLC (optional)
    136   }
    137   deriving (Eq, Show, Generic)
    138 
    139 instance NFData ChannelUpdate
    140 
    141 -- Announcement signatures -----------------------------------------------------
    142 
    143 -- | announcement_signatures message (type 259).
    144 --
    145 -- Sent between channel peers to enable channel announcement.
    146 data AnnouncementSignatures = AnnouncementSignatures
    147   { annSigChannelId     :: !ChannelId       -- ^ Channel ID
    148   , annSigShortChanId   :: !ShortChannelId  -- ^ Short channel ID
    149   , annSigNodeSig       :: !Signature       -- ^ Node signature
    150   , annSigBitcoinSig    :: !Signature       -- ^ Bitcoin signature
    151   }
    152   deriving (Eq, Show, Generic)
    153 
    154 instance NFData AnnouncementSignatures
    155 
    156 -- Query messages --------------------------------------------------------------
    157 
    158 -- | query_short_channel_ids message (type 261).
    159 --
    160 -- Requests information about specific channels.
    161 data QueryShortChannelIds = QueryShortChannelIds
    162   { queryScidsChainHash :: !ChainHash    -- ^ Chain identifier
    163   , queryScidsData      :: !ByteString   -- ^ Encoded short_channel_ids
    164   , queryScidsTlvs      :: !TlvStream    -- ^ Optional TLV (query_flags)
    165   }
    166   deriving (Eq, Show, Generic)
    167 
    168 instance NFData QueryShortChannelIds
    169 
    170 -- | reply_short_channel_ids_end message (type 262).
    171 --
    172 -- Concludes response to query_short_channel_ids.
    173 data ReplyShortChannelIdsEnd = ReplyShortChannelIdsEnd
    174   { replyScidsChainHash    :: !ChainHash  -- ^ Chain identifier
    175   , replyScidsFullInfo     :: !Word8      -- ^ 1 if complete, 0 otherwise
    176   }
    177   deriving (Eq, Show, Generic)
    178 
    179 instance NFData ReplyShortChannelIdsEnd
    180 
    181 -- | query_channel_range message (type 263).
    182 --
    183 -- Queries channels within a block range.
    184 data QueryChannelRange = QueryChannelRange
    185   { queryRangeChainHash     :: !ChainHash  -- ^ Chain identifier
    186   , queryRangeFirstBlock    :: !Word32     -- ^ First block number
    187   , queryRangeNumBlocks     :: !Word32     -- ^ Number of blocks
    188   , queryRangeTlvs          :: !TlvStream  -- ^ Optional TLV (query_option)
    189   }
    190   deriving (Eq, Show, Generic)
    191 
    192 instance NFData QueryChannelRange
    193 
    194 -- | reply_channel_range message (type 264).
    195 --
    196 -- Responds to query_channel_range with channel IDs.
    197 data ReplyChannelRange = ReplyChannelRange
    198   { replyRangeChainHash   :: !ChainHash    -- ^ Chain identifier
    199   , replyRangeFirstBlock  :: !Word32       -- ^ First block number
    200   , replyRangeNumBlocks   :: !Word32       -- ^ Number of blocks
    201   , replyRangeSyncComplete :: !Word8       -- ^ 1 if sync complete
    202   , replyRangeData        :: !ByteString   -- ^ Encoded short_channel_ids
    203   , replyRangeTlvs        :: !TlvStream    -- ^ Optional TLVs
    204   }
    205   deriving (Eq, Show, Generic)
    206 
    207 instance NFData ReplyChannelRange
    208 
    209 -- | gossip_timestamp_filter message (type 265).
    210 --
    211 -- Constrains which gossip messages are relayed.
    212 data GossipTimestampFilter = GossipTimestampFilter
    213   { gossipFilterChainHash     :: !ChainHash  -- ^ Chain identifier
    214   , gossipFilterFirstTimestamp :: !Word32    -- ^ First timestamp
    215   , gossipFilterTimestampRange :: !Word32    -- ^ Timestamp range
    216   }
    217   deriving (Eq, Show, Generic)
    218 
    219 instance NFData GossipTimestampFilter
    220 
    221 -- Union type ------------------------------------------------------------------
    222 
    223 -- | Union of all BOLT #7 message types.
    224 data Message
    225   = MsgChanAnn !ChannelAnnouncement
    226   | MsgNodeAnn !NodeAnnouncement
    227   | MsgChanUpd !ChannelUpdate
    228   | MsgAnnSig  !AnnouncementSignatures
    229   | MsgQueryScids !QueryShortChannelIds
    230   | MsgReplyScids !ReplyShortChannelIdsEnd
    231   | MsgQueryRange !QueryChannelRange
    232   | MsgReplyRange !ReplyChannelRange
    233   | MsgGossipFilter !GossipTimestampFilter
    234   deriving (Eq, Show, Generic)
    235 
    236 instance NFData Message