| Copyright | (c) 2025 Jared Tobin |
|---|---|
| License | MIT |
| Maintainer | Jared Tobin <jared@ppad.tech> |
| Safe Haskell | Safe-Inferred |
| Language | Haskell2010 |
Lightning.Protocol.BOLT1.Codec
Description
Message encoding and decoding for BOLT #1.
Synopsis
- data EncodeError
- encodeInit :: Init -> Either EncodeError ByteString
- encodeError :: Error -> Either EncodeError ByteString
- encodeWarning :: Warning -> Either EncodeError ByteString
- encodePing :: Ping -> Either EncodeError ByteString
- encodePong :: Pong -> Either EncodeError ByteString
- encodePeerStorage :: PeerStorage -> Either EncodeError ByteString
- encodePeerStorageRetrieval :: PeerStorageRetrieval -> Either EncodeError ByteString
- encodeMessage :: Message -> Either EncodeError ByteString
- encodeEnvelope :: Message -> Maybe TlvStream -> Either EncodeError ByteString
- data DecodeError
- decodeInit :: ByteString -> Either DecodeError (Init, ByteString)
- decodeError :: ByteString -> Either DecodeError (Error, ByteString)
- decodeWarning :: ByteString -> Either DecodeError (Warning, ByteString)
- decodePing :: ByteString -> Either DecodeError (Ping, ByteString)
- decodePong :: ByteString -> Either DecodeError (Pong, ByteString)
- decodePeerStorage :: ByteString -> Either DecodeError (PeerStorage, ByteString)
- decodePeerStorageRetrieval :: ByteString -> Either DecodeError (PeerStorageRetrieval, ByteString)
- 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)
Encoding errors
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 | |||||
Message encoding
encodeInit :: Init -> Either EncodeError ByteString Source #
Encode an Init message payload.
encodeError :: Error -> Either EncodeError ByteString Source #
Encode an Error message payload.
encodeWarning :: Warning -> Either EncodeError ByteString Source #
Encode a Warning message payload.
encodePing :: Ping -> Either EncodeError ByteString Source #
Encode a Ping message payload.
encodePong :: Pong -> Either EncodeError ByteString Source #
Encode a Pong message payload.
encodePeerStorage :: PeerStorage -> Either EncodeError ByteString Source #
Encode a PeerStorage message payload.
encodePeerStorageRetrieval :: PeerStorageRetrieval -> Either EncodeError ByteString Source #
Encode a PeerStorageRetrieval message payload.
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 errors
data DecodeError Source #
Decoding errors.
Constructors
| DecodeInsufficientBytes | |
| DecodeInvalidLength | |
| DecodeUnknownEvenType !Word16 | |
| DecodeUnknownOddType !Word16 | |
| DecodeTlvError !TlvError | |
| DecodeInvalidChannelId | |
| DecodeInvalidExtension !TlvError |
Instances
Message decoding
decodeInit :: ByteString -> Either DecodeError (Init, ByteString) Source #
Decode an Init message from payload bytes.
Returns the decoded message and any remaining bytes.
decodeError :: ByteString -> Either DecodeError (Error, ByteString) Source #
Decode an Error message from payload bytes.
decodeWarning :: ByteString -> Either DecodeError (Warning, ByteString) Source #
Decode a Warning message from payload bytes.
decodePing :: ByteString -> Either DecodeError (Ping, ByteString) Source #
Decode a Ping message from payload bytes.
decodePong :: ByteString -> Either DecodeError (Pong, ByteString) Source #
Decode a Pong message from payload bytes.
decodePeerStorage :: ByteString -> Either DecodeError (PeerStorage, ByteString) Source #
Decode a PeerStorage message from payload bytes.
decodePeerStorageRetrieval :: ByteString -> Either DecodeError (PeerStorageRetrieval, ByteString) Source #
Decode a PeerStorageRetrieval message from payload bytes.
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.