| Copyright | (c) 2025 Jared Tobin |
|---|---|
| License | MIT |
| Maintainer | Jared Tobin <jared@ppad.tech> |
| Safe Haskell | Safe-Inferred |
| Language | Haskell2010 |
Lightning.Protocol.BOLT1
Description
Base protocol for the Lightning Network, per BOLT #1.
Synopsis
- data Message
- data MsgType
- msgTypeWord :: MsgType -> Word16
- data ChannelId
- channelId :: ByteString -> Maybe ChannelId
- allChannels :: ChannelId
- data Init = Init {
- initGlobalFeatures :: !ByteString
- initFeatures :: !ByteString
- initTlvs :: ![InitTlv]
- data Error = Error {}
- data Warning = Warning {}
- data Ping = Ping {}
- data Pong = Pong {}
- data PeerStorage = PeerStorage {}
- data PeerStorageRetrieval = PeerStorageRetrieval {}
- data TlvRecord = TlvRecord {
- tlvType :: !Word64
- tlvValue :: !ByteString
- data TlvStream
- tlvStream :: [TlvRecord] -> Maybe TlvStream
- unsafeTlvStream :: [TlvRecord] -> TlvStream
- data TlvError
- encodeTlvStream :: TlvStream -> ByteString
- decodeTlvStream :: ByteString -> Either TlvError TlvStream
- decodeTlvStreamWith :: (Word64 -> Bool) -> ByteString -> Either TlvError TlvStream
- decodeTlvStreamRaw :: ByteString -> Either TlvError TlvStream
- data InitTlv
- data ChainHash
- chainHash :: ByteString -> Maybe ChainHash
- unChainHash :: ChainHash -> ByteString
- data Envelope = Envelope {
- envType :: !MsgType
- envPayload :: !ByteString
- envExtension :: !(Maybe TlvStream)
- data EncodeError
- encodeMessage :: Message -> Either EncodeError ByteString
- encodeEnvelope :: Message -> Maybe TlvStream -> Either EncodeError ByteString
- data DecodeError
- decodeMessage :: MsgType -> ByteString -> Either DecodeError (Message, ByteString)
- decodeEnvelope :: ByteString -> Either DecodeError (Maybe Message, Maybe TlvStream)
- decodeEnvelopeWith :: (Word64 -> Bool) -> ByteString -> Either DecodeError (Maybe Message, Maybe TlvStream)
- encodeU16 :: Word16 -> ByteString
- encodeU32 :: Word32 -> ByteString
- encodeU64 :: Word64 -> ByteString
- encodeS8 :: Int8 -> ByteString
- encodeS16 :: Int16 -> ByteString
- encodeS32 :: Int32 -> ByteString
- encodeS64 :: Int64 -> ByteString
- encodeTu16 :: Word16 -> ByteString
- encodeTu32 :: Word32 -> ByteString
- encodeTu64 :: Word64 -> ByteString
- encodeMinSigned :: Int64 -> ByteString
- encodeBigSize :: Word64 -> ByteString
- decodeU16 :: ByteString -> Maybe (Word16, ByteString)
- decodeU32 :: ByteString -> Maybe (Word32, ByteString)
- decodeU64 :: ByteString -> Maybe (Word64, ByteString)
- decodeS8 :: ByteString -> Maybe (Int8, ByteString)
- decodeS16 :: ByteString -> Maybe (Int16, ByteString)
- decodeS32 :: ByteString -> Maybe (Int32, ByteString)
- decodeS64 :: ByteString -> Maybe (Int64, ByteString)
- decodeTu16 :: Int -> ByteString -> Maybe (Word16, ByteString)
- decodeTu32 :: Int -> ByteString -> Maybe (Word32, ByteString)
- decodeTu64 :: Int -> ByteString -> Maybe (Word64, ByteString)
- decodeMinSigned :: Int -> ByteString -> Maybe (Int64, ByteString)
- decodeBigSize :: ByteString -> Maybe (Word64, ByteString)
Message types
All BOLT #1 messages.
Constructors
| MsgInitVal !Init | |
| MsgErrorVal !Error | |
| MsgWarningVal !Warning | |
| MsgPingVal !Ping | |
| MsgPongVal !Pong | |
| MsgPeerStorageVal !PeerStorage | |
| MsgPeerStorageRetrievalVal !PeerStorageRetrieval |
Instances
BOLT #1 message type codes.
Constructors
| MsgInit | 16 |
| MsgError | 17 |
| MsgPing | 18 |
| MsgPong | 19 |
| MsgWarning | 1 |
| MsgPeerStorage | 7 |
| MsgPeerStorageRet | 9 |
| MsgUnknown !Word16 | Unknown type |
Instances
| NFData MsgType Source # | |||||
Defined in Lightning.Protocol.BOLT1.Message | |||||
| Generic MsgType Source # | |||||
Defined in Lightning.Protocol.BOLT1.Message Associated Types
| |||||
| Show MsgType Source # | |||||
| Eq MsgType Source # | |||||
| type Rep MsgType Source # | |||||
Defined in Lightning.Protocol.BOLT1.Message type Rep MsgType = D1 ('MetaData "MsgType" "Lightning.Protocol.BOLT1.Message" "ppad-bolt1-0.0.1-3AI6PDE70OhJud7vB7rIZR" 'False) (((C1 ('MetaCons "MsgInit" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "MsgError" 'PrefixI 'False) (U1 :: Type -> Type)) :+: (C1 ('MetaCons "MsgPing" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "MsgPong" 'PrefixI 'False) (U1 :: Type -> Type))) :+: ((C1 ('MetaCons "MsgWarning" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "MsgPeerStorage" 'PrefixI 'False) (U1 :: Type -> Type)) :+: (C1 ('MetaCons "MsgPeerStorageRet" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "MsgUnknown" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Word16))))) | |||||
msgTypeWord :: MsgType -> Word16 Source #
Get the numeric type code for a message type.
Channel identifiers
A 32-byte channel identifier.
Use channelId to construct, which validates the length.
Use allChannels for connection-level errors (all-zeros channel ID).
Instances
| NFData ChannelId Source # | |||||
Defined in Lightning.Protocol.BOLT1.Message | |||||
| Generic ChannelId Source # | |||||
Defined in Lightning.Protocol.BOLT1.Message Associated Types
| |||||
| Show ChannelId Source # | |||||
| Eq ChannelId Source # | |||||
| type Rep ChannelId Source # | |||||
Defined in Lightning.Protocol.BOLT1.Message type Rep ChannelId = D1 ('MetaData "ChannelId" "Lightning.Protocol.BOLT1.Message" "ppad-bolt1-0.0.1-3AI6PDE70OhJud7vB7rIZR" 'True) (C1 ('MetaCons "ChannelId" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 ByteString))) | |||||
channelId :: ByteString -> Maybe ChannelId Source #
Construct a ChannelId from a 32-byte ByteString.
Returns Nothing if the input is not exactly 32 bytes.
>>>channelId (BS.replicate 32 0x00)Just (ChannelId "\NUL\NUL...")>>>channelId "too short"Nothing
allChannels :: ChannelId Source #
The all-zeros channel ID, used for connection-level errors.
Per BOLT #1, setting channel_id to all zeros means the error applies to the connection rather than a specific channel.
Setup messages
The init message (type 16).
Constructors
| Init | |
Fields
| |
Instances
| NFData Init Source # | |||||
Defined in Lightning.Protocol.BOLT1.Message | |||||
| Generic Init Source # | |||||
Defined in Lightning.Protocol.BOLT1.Message Associated Types
| |||||
| Show Init Source # | |||||
| Eq Init Source # | |||||
| type Rep Init Source # | |||||
Defined in Lightning.Protocol.BOLT1.Message type Rep Init = D1 ('MetaData "Init" "Lightning.Protocol.BOLT1.Message" "ppad-bolt1-0.0.1-3AI6PDE70OhJud7vB7rIZR" 'False) (C1 ('MetaCons "Init" 'PrefixI 'True) (S1 ('MetaSel ('Just "initGlobalFeatures") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 ByteString) :*: (S1 ('MetaSel ('Just "initFeatures") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 ByteString) :*: S1 ('MetaSel ('Just "initTlvs") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 [InitTlv])))) | |||||
The error message (type 17).
Constructors
| Error | |
Fields
| |
Instances
| NFData Error Source # | |||||
Defined in Lightning.Protocol.BOLT1.Message | |||||
| Generic Error Source # | |||||
Defined in Lightning.Protocol.BOLT1.Message Associated Types
| |||||
| Show Error Source # | |||||
| Eq Error Source # | |||||
| type Rep Error Source # | |||||
Defined in Lightning.Protocol.BOLT1.Message type Rep Error = D1 ('MetaData "Error" "Lightning.Protocol.BOLT1.Message" "ppad-bolt1-0.0.1-3AI6PDE70OhJud7vB7rIZR" 'False) (C1 ('MetaCons "Error" 'PrefixI 'True) (S1 ('MetaSel ('Just "errorChannelId") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 ChannelId) :*: S1 ('MetaSel ('Just "errorData") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 ByteString))) | |||||
The warning message (type 1).
Constructors
| Warning | |
Fields | |
Instances
| NFData Warning Source # | |||||
Defined in Lightning.Protocol.BOLT1.Message | |||||
| Generic Warning Source # | |||||
Defined in Lightning.Protocol.BOLT1.Message Associated Types
| |||||
| Show Warning Source # | |||||
| Eq Warning Source # | |||||
| type Rep Warning Source # | |||||
Defined in Lightning.Protocol.BOLT1.Message type Rep Warning = D1 ('MetaData "Warning" "Lightning.Protocol.BOLT1.Message" "ppad-bolt1-0.0.1-3AI6PDE70OhJud7vB7rIZR" 'False) (C1 ('MetaCons "Warning" 'PrefixI 'True) (S1 ('MetaSel ('Just "warningChannelId") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 ChannelId) :*: S1 ('MetaSel ('Just "warningData") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 ByteString))) | |||||
Control messages
The ping message (type 18).
Constructors
| Ping | |
Fields
| |
Instances
| NFData Ping Source # | |||||
Defined in Lightning.Protocol.BOLT1.Message | |||||
| Generic Ping Source # | |||||
Defined in Lightning.Protocol.BOLT1.Message Associated Types
| |||||
| Show Ping Source # | |||||
| Eq Ping Source # | |||||
| type Rep Ping Source # | |||||
Defined in Lightning.Protocol.BOLT1.Message type Rep Ping = D1 ('MetaData "Ping" "Lightning.Protocol.BOLT1.Message" "ppad-bolt1-0.0.1-3AI6PDE70OhJud7vB7rIZR" 'False) (C1 ('MetaCons "Ping" 'PrefixI 'True) (S1 ('MetaSel ('Just "pingNumPongBytes") 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 Word16) :*: S1 ('MetaSel ('Just "pingIgnored") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 ByteString))) | |||||
The pong message (type 19).
Constructors
| Pong | |
Fields | |
Instances
| NFData Pong Source # | |||||
Defined in Lightning.Protocol.BOLT1.Message | |||||
| Generic Pong Source # | |||||
Defined in Lightning.Protocol.BOLT1.Message Associated Types
| |||||
| Show Pong Source # | |||||
| Eq Pong Source # | |||||
| type Rep Pong Source # | |||||
Defined in Lightning.Protocol.BOLT1.Message type Rep Pong = D1 ('MetaData "Pong" "Lightning.Protocol.BOLT1.Message" "ppad-bolt1-0.0.1-3AI6PDE70OhJud7vB7rIZR" 'False) (C1 ('MetaCons "Pong" 'PrefixI 'True) (S1 ('MetaSel ('Just "pongIgnored") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 ByteString))) | |||||
Peer storage
data PeerStorage Source #
The peer_storage message (type 7).
Constructors
| PeerStorage | |
Fields | |
Instances
| NFData PeerStorage Source # | |||||
Defined in Lightning.Protocol.BOLT1.Message Methods rnf :: PeerStorage -> () # | |||||
| Generic PeerStorage Source # | |||||
Defined in Lightning.Protocol.BOLT1.Message Associated Types
| |||||
| Show PeerStorage Source # | |||||
Defined in Lightning.Protocol.BOLT1.Message Methods showsPrec :: Int -> PeerStorage -> ShowS # show :: PeerStorage -> String # showList :: [PeerStorage] -> ShowS # | |||||
| Eq PeerStorage Source # | |||||
Defined in Lightning.Protocol.BOLT1.Message | |||||
| type Rep PeerStorage Source # | |||||
Defined in Lightning.Protocol.BOLT1.Message type Rep PeerStorage = D1 ('MetaData "PeerStorage" "Lightning.Protocol.BOLT1.Message" "ppad-bolt1-0.0.1-3AI6PDE70OhJud7vB7rIZR" 'False) (C1 ('MetaCons "PeerStorage" 'PrefixI 'True) (S1 ('MetaSel ('Just "peerStorageBlob") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 ByteString))) | |||||
data PeerStorageRetrieval Source #
The peer_storage_retrieval message (type 9).
Constructors
| PeerStorageRetrieval | |
Fields | |
Instances
| NFData PeerStorageRetrieval Source # | |||||
Defined in Lightning.Protocol.BOLT1.Message Methods rnf :: PeerStorageRetrieval -> () # | |||||
| Generic PeerStorageRetrieval Source # | |||||
Defined in Lightning.Protocol.BOLT1.Message Associated Types
Methods from :: PeerStorageRetrieval -> Rep PeerStorageRetrieval x # to :: Rep PeerStorageRetrieval x -> PeerStorageRetrieval # | |||||
| Show PeerStorageRetrieval Source # | |||||
Defined in Lightning.Protocol.BOLT1.Message Methods showsPrec :: Int -> PeerStorageRetrieval -> ShowS # show :: PeerStorageRetrieval -> String # showList :: [PeerStorageRetrieval] -> ShowS # | |||||
| Eq PeerStorageRetrieval Source # | |||||
Defined in Lightning.Protocol.BOLT1.Message Methods (==) :: PeerStorageRetrieval -> PeerStorageRetrieval -> Bool # (/=) :: PeerStorageRetrieval -> PeerStorageRetrieval -> Bool # | |||||
| type Rep PeerStorageRetrieval Source # | |||||
Defined in Lightning.Protocol.BOLT1.Message type Rep PeerStorageRetrieval = D1 ('MetaData "PeerStorageRetrieval" "Lightning.Protocol.BOLT1.Message" "ppad-bolt1-0.0.1-3AI6PDE70OhJud7vB7rIZR" 'False) (C1 ('MetaCons "PeerStorageRetrieval" 'PrefixI 'True) (S1 ('MetaSel ('Just "peerStorageRetrievalBlob") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 ByteString))) | |||||
TLV
A single TLV record.
Constructors
| TlvRecord | |
Fields
| |
Instances
| NFData TlvRecord Source # | |||||
Defined in Lightning.Protocol.BOLT1.TLV | |||||
| Generic TlvRecord Source # | |||||
Defined in Lightning.Protocol.BOLT1.TLV Associated Types
| |||||
| Show TlvRecord Source # | |||||
| Eq TlvRecord Source # | |||||
| type Rep TlvRecord Source # | |||||
Defined in Lightning.Protocol.BOLT1.TLV type Rep TlvRecord = D1 ('MetaData "TlvRecord" "Lightning.Protocol.BOLT1.TLV" "ppad-bolt1-0.0.1-3AI6PDE70OhJud7vB7rIZR" 'False) (C1 ('MetaCons "TlvRecord" 'PrefixI 'True) (S1 ('MetaSel ('Just "tlvType") 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 Word64) :*: S1 ('MetaSel ('Just "tlvValue") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 ByteString))) | |||||
A TLV stream (series of TLV records).
Instances
| NFData TlvStream Source # | |||||
Defined in Lightning.Protocol.BOLT1.TLV | |||||
| Generic TlvStream Source # | |||||
Defined in Lightning.Protocol.BOLT1.TLV Associated Types
| |||||
| Show TlvStream Source # | |||||
| Eq TlvStream Source # | |||||
| type Rep TlvStream Source # | |||||
Defined in Lightning.Protocol.BOLT1.TLV | |||||
unsafeTlvStream :: [TlvRecord] -> TlvStream Source #
Unsafe constructor for TlvStream that skips validation.
Use only when ordering is already guaranteed (e.g., in decode functions).
TLV decoding errors.
Constructors
| TlvNonMinimalEncoding | |
| TlvNotStrictlyIncreasing | |
| TlvLengthExceedsBounds | |
| TlvUnknownEvenType !Word64 | |
| TlvInvalidKnownType !Word64 |
Instances
| NFData TlvError Source # | |||||
Defined in Lightning.Protocol.BOLT1.TLV | |||||
| Generic TlvError Source # | |||||
Defined in Lightning.Protocol.BOLT1.TLV Associated Types
| |||||
| Show TlvError Source # | |||||
| Eq TlvError Source # | |||||
| type Rep TlvError Source # | |||||
Defined in Lightning.Protocol.BOLT1.TLV type Rep TlvError = D1 ('MetaData "TlvError" "Lightning.Protocol.BOLT1.TLV" "ppad-bolt1-0.0.1-3AI6PDE70OhJud7vB7rIZR" 'False) ((C1 ('MetaCons "TlvNonMinimalEncoding" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "TlvNotStrictlyIncreasing" 'PrefixI 'False) (U1 :: Type -> Type)) :+: (C1 ('MetaCons "TlvLengthExceedsBounds" 'PrefixI 'False) (U1 :: Type -> Type) :+: (C1 ('MetaCons "TlvUnknownEvenType" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Word64)) :+: C1 ('MetaCons "TlvInvalidKnownType" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Word64))))) | |||||
encodeTlvStream :: TlvStream -> ByteString Source #
Encode a TLV stream.
decodeTlvStream :: ByteString -> Either TlvError TlvStream Source #
Decode a TLV stream with BOLT #1 init_tlvs validation.
This uses the default known types for init messages (1 and 3).
For other contexts, use decodeTlvStreamWith with an appropriate
predicate.
Arguments
| :: (Word64 -> Bool) | Predicate: is this type known? |
| -> ByteString | |
| -> Either TlvError TlvStream |
Decode a TLV stream with configurable known-type predicate.
Per BOLT #1: - Types must be strictly increasing - Unknown even types cause failure - Unknown odd types are skipped
The predicate determines which types are "known" for the context.
decodeTlvStreamRaw :: ByteString -> Either TlvError TlvStream Source #
Decode a TLV stream without any known-type validation.
This decoder only enforces structural validity: - Types must be strictly increasing - Lengths must not exceed bounds
All records are returned regardless of type. Note: this does NOT
enforce the BOLT #1 unknown-even-type rule. Use decodeTlvStreamWith
with an appropriate predicate for spec-compliant parsing.
Init TLVs
TLV records for init message.
Constructors
| InitNetworks ![ChainHash] | Type 1: chain hashes (32 bytes each) |
| InitRemoteAddr !ByteString | Type 3: remote address |
Instances
| NFData InitTlv Source # | |||||
Defined in Lightning.Protocol.BOLT1.TLV | |||||
| Generic InitTlv Source # | |||||
Defined in Lightning.Protocol.BOLT1.TLV Associated Types
| |||||
| Show InitTlv Source # | |||||
| Eq InitTlv Source # | |||||
| type Rep InitTlv Source # | |||||
Defined in Lightning.Protocol.BOLT1.TLV type Rep InitTlv = D1 ('MetaData "InitTlv" "Lightning.Protocol.BOLT1.TLV" "ppad-bolt1-0.0.1-3AI6PDE70OhJud7vB7rIZR" 'False) (C1 ('MetaCons "InitNetworks" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 [ChainHash])) :+: C1 ('MetaCons "InitRemoteAddr" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 ByteString))) | |||||
A chain hash (32-byte hash identifying a blockchain).
Instances
| NFData ChainHash Source # | |||||
Defined in Lightning.Protocol.BOLT1.Prim | |||||
| Generic ChainHash Source # | |||||
Defined in Lightning.Protocol.BOLT1.Prim Associated Types
| |||||
| Show ChainHash Source # | |||||
| Eq ChainHash Source # | |||||
| type Rep ChainHash Source # | |||||
Defined in Lightning.Protocol.BOLT1.Prim type Rep ChainHash = D1 ('MetaData "ChainHash" "Lightning.Protocol.BOLT1.Prim" "ppad-bolt1-0.0.1-3AI6PDE70OhJud7vB7rIZR" 'True) (C1 ('MetaCons "ChainHash" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 ByteString))) | |||||
chainHash :: ByteString -> Maybe ChainHash Source #
Construct a chain hash from a 32-byte bytestring.
Returns Nothing if the input is not exactly 32 bytes.
unChainHash :: ChainHash -> ByteString Source #
Extract the raw bytes from a chain hash.
Message envelope
A complete message envelope with type, payload, and optional extension.
Constructors
| Envelope | |
Fields
| |
Instances
| NFData Envelope Source # | |||||
Defined in Lightning.Protocol.BOLT1.Message | |||||
| Generic Envelope Source # | |||||
Defined in Lightning.Protocol.BOLT1.Message Associated Types
| |||||
| Show Envelope Source # | |||||
| Eq Envelope Source # | |||||
| type Rep Envelope Source # | |||||
Defined in Lightning.Protocol.BOLT1.Message type Rep Envelope = D1 ('MetaData "Envelope" "Lightning.Protocol.BOLT1.Message" "ppad-bolt1-0.0.1-3AI6PDE70OhJud7vB7rIZR" 'False) (C1 ('MetaCons "Envelope" 'PrefixI 'True) (S1 ('MetaSel ('Just "envType") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 MsgType) :*: (S1 ('MetaSel ('Just "envPayload") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 ByteString) :*: S1 ('MetaSel ('Just "envExtension") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (Maybe TlvStream))))) | |||||
Encoding
data EncodeError Source #
Encoding errors.
Constructors
| EncodeLengthOverflow | Field length exceeds u16 max (65535 bytes) |
| EncodeMessageTooLarge | Total message size exceeds 65535 bytes |
Instances
| NFData EncodeError Source # | |||||
Defined in Lightning.Protocol.BOLT1.Codec Methods rnf :: EncodeError -> () # | |||||
| Generic EncodeError Source # | |||||
Defined in Lightning.Protocol.BOLT1.Codec Associated Types
| |||||
| Show EncodeError Source # | |||||
Defined in Lightning.Protocol.BOLT1.Codec Methods showsPrec :: Int -> EncodeError -> ShowS # show :: EncodeError -> String # showList :: [EncodeError] -> ShowS # | |||||
| Eq EncodeError Source # | |||||
Defined in Lightning.Protocol.BOLT1.Codec | |||||
| type Rep EncodeError Source # | |||||
Defined in Lightning.Protocol.BOLT1.Codec | |||||
encodeMessage :: Message -> Either EncodeError ByteString Source #
Encode a message to its payload bytes.
Checks that the payload does not exceed 65533 bytes (the maximum possible given the 2-byte type field and 65535-byte message limit).
encodeEnvelope :: Message -> Maybe TlvStream -> Either EncodeError ByteString Source #
Encode a message as a complete envelope (type + payload + extension).
Per BOLT #1, the total message size must not exceed 65535 bytes.
Decoding
data DecodeError Source #
Decoding errors.
Constructors
| DecodeInsufficientBytes | |
| DecodeInvalidLength | |
| DecodeUnknownEvenType !Word16 | |
| DecodeUnknownOddType !Word16 | |
| DecodeTlvError !TlvError | |
| DecodeInvalidChannelId | |
| DecodeInvalidExtension !TlvError |
Instances
| NFData DecodeError Source # | |||||
Defined in Lightning.Protocol.BOLT1.Codec Methods rnf :: DecodeError -> () # | |||||
| Generic DecodeError Source # | |||||
Defined in Lightning.Protocol.BOLT1.Codec Associated Types
| |||||
| Show DecodeError Source # | |||||
Defined in Lightning.Protocol.BOLT1.Codec Methods showsPrec :: Int -> DecodeError -> ShowS # show :: DecodeError -> String # showList :: [DecodeError] -> ShowS # | |||||
| Eq DecodeError Source # | |||||
Defined in Lightning.Protocol.BOLT1.Codec | |||||
| type Rep DecodeError Source # | |||||
Defined in Lightning.Protocol.BOLT1.Codec type Rep DecodeError = D1 ('MetaData "DecodeError" "Lightning.Protocol.BOLT1.Codec" "ppad-bolt1-0.0.1-3AI6PDE70OhJud7vB7rIZR" 'False) ((C1 ('MetaCons "DecodeInsufficientBytes" 'PrefixI 'False) (U1 :: Type -> Type) :+: (C1 ('MetaCons "DecodeInvalidLength" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "DecodeUnknownEvenType" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Word16)))) :+: ((C1 ('MetaCons "DecodeUnknownOddType" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Word16)) :+: C1 ('MetaCons "DecodeTlvError" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 TlvError))) :+: (C1 ('MetaCons "DecodeInvalidChannelId" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "DecodeInvalidExtension" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 TlvError))))) | |||||
decodeMessage :: MsgType -> ByteString -> Either DecodeError (Message, ByteString) Source #
Decode a message from its type and payload.
Returns the decoded message and any remaining bytes (for extensions). For unknown types, returns an appropriate error.
decodeEnvelope :: ByteString -> Either DecodeError (Maybe Message, Maybe TlvStream) Source #
Decode a complete envelope (type + payload + optional extension).
Per BOLT #1: - Unknown odd message types are ignored (returns Nothing for message) - Unknown even message types cause connection close (returns error) - Invalid extension TLV causes connection close (returns error)
This uses the default policy of treating all extension TLV types as
unknown. Use decodeEnvelopeWith for configurable extension handling.
Returns the decoded message (if known) and any extension TLVs.
Arguments
| :: (Word64 -> Bool) | Predicate: is this extension TLV type known? |
| -> ByteString | |
| -> Either DecodeError (Maybe Message, Maybe TlvStream) |
Decode a complete envelope with configurable extension TLV handling.
The predicate determines which extension TLV types are "known" and should be preserved. Unknown even types cause failure; unknown odd types are skipped.
Use decodeEnvelopeWith (const False) to reject all even extension
types (the default behavior of decodeEnvelope).
Use decodeEnvelopeWith (const True) to accept all extension types.
Primitive encoding
encodeU16 :: Word16 -> ByteString Source #
Encode a 16-bit unsigned integer (big-endian).
>>>encodeU16 0x0102"\SOH\STX"
encodeU32 :: Word32 -> ByteString Source #
Encode a 32-bit unsigned integer (big-endian).
>>>encodeU32 0x01020304"\SOH\STX\ETX\EOT"
encodeU64 :: Word64 -> ByteString Source #
Encode a 64-bit unsigned integer (big-endian).
>>>encodeU64 0x0102030405060708"\SOH\STX\ETX\EOT\ENQ\ACK\a\b"
encodeS8 :: Int8 -> ByteString Source #
Encode an 8-bit signed integer.
>>>encodeS8 42"*">>>encodeS8 (-42)"\214"
encodeS16 :: Int16 -> ByteString Source #
Encode a 16-bit signed integer (big-endian two's complement).
>>>encodeS16 0x0102"\SOH\STX">>>encodeS16 (-1)"\255\255"
encodeS32 :: Int32 -> ByteString Source #
Encode a 32-bit signed integer (big-endian two's complement).
>>>encodeS32 0x01020304"\SOH\STX\ETX\EOT">>>encodeS32 (-1)"\255\255\255\255"
encodeS64 :: Int64 -> ByteString Source #
Encode a 64-bit signed integer (big-endian two's complement).
>>>encodeS64 0x0102030405060708"\SOH\STX\ETX\EOT\ENQ\ACK\a\b">>>encodeS64 (-1)"\255\255\255\255\255\255\255\255"
encodeTu16 :: Word16 -> ByteString Source #
Encode a truncated 16-bit unsigned integer (0-2 bytes).
Leading zeros are omitted per BOLT #1. Zero encodes to empty.
>>>encodeTu16 0"">>>encodeTu16 1"\SOH">>>encodeTu16 256"\SOH\NUL"
encodeTu32 :: Word32 -> ByteString Source #
Encode a truncated 32-bit unsigned integer (0-4 bytes).
Leading zeros are omitted per BOLT #1. Zero encodes to empty.
>>>encodeTu32 0"">>>encodeTu32 1"\SOH">>>encodeTu32 0x010000"\SOH\NUL\NUL"
encodeTu64 :: Word64 -> ByteString Source #
Encode a truncated 64-bit unsigned integer (0-8 bytes).
Leading zeros are omitted per BOLT #1. Zero encodes to empty.
>>>encodeTu64 0"">>>encodeTu64 1"\SOH">>>encodeTu64 0x0100000000"\SOH\NUL\NUL\NUL\NUL"
encodeMinSigned :: Int64 -> ByteString Source #
Encode a signed 64-bit integer using minimal bytes.
Uses the smallest number of bytes that can represent the value in two's complement. Per BOLT #1 Appendix D test vectors.
>>>encodeMinSigned 0"\NUL">>>encodeMinSigned 127"\DEL">>>encodeMinSigned 128"\NUL\128">>>encodeMinSigned (-1)"\255">>>encodeMinSigned (-128)"\128">>>encodeMinSigned (-129)"\255\DEL"
encodeBigSize :: Word64 -> ByteString Source #
Encode a BigSize value (variable-length unsigned integer).
>>>encodeBigSize 0"\NUL">>>encodeBigSize 252"\252">>>encodeBigSize 253"\253\NUL\253">>>encodeBigSize 65536"\254\NUL\SOH\NUL\NUL"
Primitive decoding
decodeU16 :: ByteString -> Maybe (Word16, ByteString) Source #
Decode a 16-bit unsigned integer (big-endian).
decodeU32 :: ByteString -> Maybe (Word32, ByteString) Source #
Decode a 32-bit unsigned integer (big-endian).
decodeU64 :: ByteString -> Maybe (Word64, ByteString) Source #
Decode a 64-bit unsigned integer (big-endian).
decodeS8 :: ByteString -> Maybe (Int8, ByteString) Source #
Decode an 8-bit signed integer.
decodeS16 :: ByteString -> Maybe (Int16, ByteString) Source #
Decode a 16-bit signed integer (big-endian two's complement).
decodeS32 :: ByteString -> Maybe (Int32, ByteString) Source #
Decode a 32-bit signed integer (big-endian two's complement).
decodeS64 :: ByteString -> Maybe (Int64, ByteString) Source #
Decode a 64-bit signed integer (big-endian two's complement).
decodeTu16 :: Int -> ByteString -> Maybe (Word16, ByteString) Source #
Decode a truncated 16-bit unsigned integer (0-2 bytes).
Returns Nothing if the encoding is non-minimal (has leading zeros).
decodeTu32 :: Int -> ByteString -> Maybe (Word32, ByteString) Source #
Decode a truncated 32-bit unsigned integer (0-4 bytes).
Returns Nothing if the encoding is non-minimal (has leading zeros).
decodeTu64 :: Int -> ByteString -> Maybe (Word64, ByteString) Source #
Decode a truncated 64-bit unsigned integer (0-8 bytes).
Returns Nothing if the encoding is non-minimal (has leading zeros).
decodeMinSigned :: Int -> ByteString -> Maybe (Int64, ByteString) Source #
Decode a minimal signed integer (1, 2, 4, or 8 bytes).
Validates that the encoding is minimal: the value could not be represented in fewer bytes. Per BOLT #1 Appendix D test vectors.
decodeBigSize :: ByteString -> Maybe (Word64, ByteString) Source #
Decode a BigSize value with minimality check.