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.Prim

Description

Primitive type encoding and decoding for BOLT #1.

Synopsis

Chain hash

data ChainHash Source #

A chain hash (32-byte hash identifying a blockchain).

Instances

Instances details
NFData ChainHash Source # 
Instance details

Defined in Lightning.Protocol.BOLT1.Prim

Methods

rnf :: ChainHash -> () #

Generic ChainHash Source # 
Instance details

Defined in Lightning.Protocol.BOLT1.Prim

Associated Types

type Rep ChainHash 
Instance details

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

Defined in Lightning.Protocol.BOLT1.Prim

Eq ChainHash Source # 
Instance details

Defined in Lightning.Protocol.BOLT1.Prim

type Rep ChainHash Source # 
Instance details

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.

Unsigned integer 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"

Signed integer encoding

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"

Truncated unsigned integer encoding

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"

Minimal signed integer encoding

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"

BigSize encoding

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"

Unsigned integer 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).

Signed integer decoding

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

Truncated unsigned integer decoding

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

Minimal signed integer decoding

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.

BigSize decoding

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

Decode a BigSize value with minimality check.

Internal helpers

encodeLength :: ByteString -> Maybe ByteString Source #

Encode a length as u16, checking bounds.

Returns Nothing if the length exceeds 65535.