ppad-bolt4-0.0.1: BOLT4 (onion routing) for Lightning Network
Copyright(c) 2025 Jared Tobin
LicenseMIT
MaintainerJared Tobin <jared@ppad.tech>
Safe HaskellSafe-Inferred
LanguageHaskell2010

Lightning.Protocol.BOLT4.Codec

Description

Serialization and deserialization for BOLT4 types.

Synopsis

BigSize encoding

encodeBigSize :: Word64 -> ByteString Source #

Encode integer as BigSize.

  • 0-0xFC: 1 byte
  • 0xFD-0xFFFF: 0xFD ++ 2 bytes BE
  • 0x10000-0xFFFFFFFF: 0xFE ++ 4 bytes BE
  • larger: 0xFF ++ 8 bytes BE

decodeBigSize :: ByteString -> Maybe (Word64, ByteString) Source #

Decode BigSize, returning (value, remaining bytes).

bigSizeLen :: Word64 -> Int Source #

Get encoded size of a BigSize value without encoding.

TLV encoding

encodeTlv :: TlvRecord -> ByteString Source #

Encode a TLV record.

decodeTlv :: ByteString -> Maybe (TlvRecord, ByteString) Source #

Decode a single TLV record.

decodeTlvStream :: ByteString -> Maybe [TlvRecord] Source #

Decode a TLV stream (sequence of records). Validates strictly increasing type order.

encodeTlvStream :: [TlvRecord] -> ByteString Source #

Encode a TLV stream from records. Records must be sorted by type, no duplicates.

Packet serialization

encodeOnionPacket :: OnionPacket -> ByteString Source #

Serialize OnionPacket to 1366 bytes.

decodeOnionPacket :: ByteString -> Maybe OnionPacket Source #

Parse OnionPacket from 1366 bytes.

encodeHopPayload :: HopPayload -> ByteString Source #

Encode HopPayload to bytes (without length prefix).

decodeHopPayload :: ByteString -> Maybe HopPayload Source #

Decode HopPayload from bytes.

ShortChannelId

encodeShortChannelId :: ShortChannelId -> ByteString Source #

Encode ShortChannelId to 8 bytes. Format: 3 bytes block || 3 bytes tx || 2 bytes output (all BE)

decodeShortChannelId :: ByteString -> Maybe ShortChannelId Source #

Decode ShortChannelId from 8 bytes.

Failure messages

Internal helpers (for Blinding)

toStrict :: Builder -> ByteString Source #

Convert Builder to strict ByteString.

word16BE :: ByteString -> Word16 Source #

Decode big-endian Word16.

word32BE :: ByteString -> Word32 Source #

Decode big-endian Word32.

encodeWord64TU :: Word64 -> ByteString Source #

Encode Word64 as truncated unsigned (minimal bytes).

decodeWord64TU :: ByteString -> Maybe Word64 Source #

Decode truncated unsigned to Word64.

encodeWord32TU :: Word32 -> ByteString Source #

Encode Word32 as truncated unsigned.

decodeWord32TU :: ByteString -> Maybe Word32 Source #

Decode truncated unsigned to Word32.