ppad-bolt1-0.0.1: Base protocol per BOLT #1
Copyright(c) 2025 Jared Tobin
LicenseMIT
MaintainerJared Tobin <jared@ppad.tech>
Safe HaskellSafe-Inferred
LanguageHaskell2010

Lightning.Protocol.BOLT1.Codec

Description

Message encoding and decoding for BOLT #1.

Synopsis

Encoding errors

data EncodeError Source #

Encoding errors.

Constructors

EncodeLengthOverflow

Field length exceeds u16 max (65535 bytes)

EncodeMessageTooLarge

Total message size exceeds 65535 bytes

Instances

Instances details
NFData EncodeError Source # 
Instance details

Defined in Lightning.Protocol.BOLT1.Codec

Methods

rnf :: EncodeError -> () #

Generic EncodeError Source # 
Instance details

Defined in Lightning.Protocol.BOLT1.Codec

Associated Types

type Rep EncodeError 
Instance details

Defined in Lightning.Protocol.BOLT1.Codec

type Rep EncodeError = D1 ('MetaData "EncodeError" "Lightning.Protocol.BOLT1.Codec" "ppad-bolt1-0.0.1-3AI6PDE70OhJud7vB7rIZR" 'False) (C1 ('MetaCons "EncodeLengthOverflow" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "EncodeMessageTooLarge" 'PrefixI 'False) (U1 :: Type -> Type))
Show EncodeError Source # 
Instance details

Defined in Lightning.Protocol.BOLT1.Codec

Eq EncodeError Source # 
Instance details

Defined in Lightning.Protocol.BOLT1.Codec

type Rep EncodeError Source # 
Instance details

Defined in Lightning.Protocol.BOLT1.Codec

type Rep EncodeError = D1 ('MetaData "EncodeError" "Lightning.Protocol.BOLT1.Codec" "ppad-bolt1-0.0.1-3AI6PDE70OhJud7vB7rIZR" 'False) (C1 ('MetaCons "EncodeLengthOverflow" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "EncodeMessageTooLarge" 'PrefixI 'False) (U1 :: Type -> Type))

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.

Instances

Instances details
NFData DecodeError Source # 
Instance details

Defined in Lightning.Protocol.BOLT1.Codec

Methods

rnf :: DecodeError -> () #

Generic DecodeError Source # 
Instance details

Defined in Lightning.Protocol.BOLT1.Codec

Associated Types

type Rep DecodeError 
Instance details

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)))))
Show DecodeError Source # 
Instance details

Defined in Lightning.Protocol.BOLT1.Codec

Eq DecodeError Source # 
Instance details

Defined in Lightning.Protocol.BOLT1.Codec

type Rep DecodeError Source # 
Instance details

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)))))

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.

decodeEnvelopeWith Source #

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.