| Copyright | (c) 2025 Jared Tobin |
|---|---|
| License | MIT |
| Maintainer | Jared Tobin <jared@ppad.tech> |
| Safe Haskell | None |
| Language | Haskell2010 |
Lightning.Protocol.BOLT1.Prim
Contents
- Chain hash
- Channel identifier
- Signatures and keys
- Payment types
- Per-commitment secret
- Short channel identifier
- Amounts
- Unsigned integer encoding
- Signed integer encoding
- Truncated unsigned integer encoding
- Minimal signed integer encoding
- BigSize encoding
- Unsigned integer decoding
- Signed integer decoding
- Truncated unsigned integer decoding
- Minimal signed integer decoding
- BigSize decoding
- Internal helpers
Description
Primitive type encoding and decoding for BOLT #1.
Synopsis
- newtype ChainHash = ChainHash ByteString
- chainHash :: ByteString -> Maybe ChainHash
- unChainHash :: ChainHash -> ByteString
- newtype ChannelId = ChannelId ByteString
- channelId :: ByteString -> Maybe ChannelId
- unChannelId :: ChannelId -> ByteString
- allChannels :: ChannelId
- newtype Signature = Signature ByteString
- signature :: ByteString -> Maybe Signature
- unSignature :: Signature -> ByteString
- newtype Point = Point ByteString
- point :: ByteString -> Maybe Point
- unPoint :: Point -> ByteString
- newtype PaymentHash = PaymentHash ByteString
- paymentHash :: ByteString -> Maybe PaymentHash
- unPaymentHash :: PaymentHash -> ByteString
- newtype PaymentPreimage = PaymentPreimage ByteString
- paymentPreimage :: ByteString -> Maybe PaymentPreimage
- unPaymentPreimage :: PaymentPreimage -> ByteString
- newtype PerCommitmentSecret = PerCommitmentSecret ByteString
- perCommitmentSecret :: ByteString -> Maybe PerCommitmentSecret
- unPerCommitmentSecret :: PerCommitmentSecret -> ByteString
- newtype ShortChannelId = ShortChannelId Word64
- shortChannelId :: Word32 -> Word32 -> Word16 -> Maybe ShortChannelId
- scidWord64 :: ShortChannelId -> Word64
- scidBlockHeight :: ShortChannelId -> Word32
- scidTxIndex :: ShortChannelId -> Word32
- scidOutputIndex :: ShortChannelId -> Word16
- newtype Satoshi = Satoshi {}
- newtype MilliSatoshi = MilliSatoshi {}
- satToMsat :: Satoshi -> MilliSatoshi
- msatToSat :: MilliSatoshi -> Satoshi
- 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)
- encodeLength :: ByteString -> Maybe ByteString
Chain hash
A chain hash (32-byte hash identifying a blockchain).
Constructors
| ChainHash ByteString |
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 # | |||||
| Ord 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.
Channel identifier
A 32-byte channel identifier.
Constructors
| ChannelId ByteString |
Instances
| NFData ChannelId Source # | |||||
Defined in Lightning.Protocol.BOLT1.Prim | |||||
| Generic ChannelId Source # | |||||
Defined in Lightning.Protocol.BOLT1.Prim Associated Types
| |||||
| Show ChannelId Source # | |||||
| Eq ChannelId Source # | |||||
| Ord ChannelId Source # | |||||
| type Rep ChannelId Source # | |||||
Defined in Lightning.Protocol.BOLT1.Prim type Rep ChannelId = D1 ('MetaData "ChannelId" "Lightning.Protocol.BOLT1.Prim" "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.
unChannelId :: ChannelId -> ByteString Source #
Extract the raw bytes from a ChannelId.
allChannels :: ChannelId Source #
The all-zeros channel ID (connection-level errors).
Signatures and keys
A 64-byte compact ECDSA signature.
Constructors
| Signature ByteString |
Instances
| NFData Signature Source # | |||||
Defined in Lightning.Protocol.BOLT1.Prim | |||||
| Generic Signature Source # | |||||
Defined in Lightning.Protocol.BOLT1.Prim Associated Types
| |||||
| Show Signature Source # | |||||
| Eq Signature Source # | |||||
| Ord Signature Source # | |||||
| type Rep Signature Source # | |||||
Defined in Lightning.Protocol.BOLT1.Prim type Rep Signature = D1 ('MetaData "Signature" "Lightning.Protocol.BOLT1.Prim" "ppad-bolt1-0.0.1-3AI6PDE70OhJud7vB7rIZR" 'True) (C1 ('MetaCons "Signature" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 ByteString))) | |||||
signature :: ByteString -> Maybe Signature Source #
Construct a Signature from a 64-byte ByteString.
Returns Nothing if the input is not exactly 64 bytes.
unSignature :: Signature -> ByteString Source #
Extract the raw bytes from a Signature.
A 33-byte compressed secp256k1 public key.
Constructors
| Point ByteString |
Instances
| NFData Point Source # | |||||
Defined in Lightning.Protocol.BOLT1.Prim | |||||
| Generic Point Source # | |||||
Defined in Lightning.Protocol.BOLT1.Prim Associated Types
| |||||
| Show Point Source # | |||||
| Eq Point Source # | |||||
| Ord Point Source # | |||||
| type Rep Point Source # | |||||
Defined in Lightning.Protocol.BOLT1.Prim type Rep Point = D1 ('MetaData "Point" "Lightning.Protocol.BOLT1.Prim" "ppad-bolt1-0.0.1-3AI6PDE70OhJud7vB7rIZR" 'True) (C1 ('MetaCons "Point" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 ByteString))) | |||||
point :: ByteString -> Maybe Point Source #
Construct a Point from a 33-byte ByteString.
Returns Nothing if the input is not exactly 33 bytes.
Payment types
newtype PaymentHash Source #
A 32-byte SHA256 payment hash.
Constructors
| PaymentHash ByteString |
Instances
paymentHash :: ByteString -> Maybe PaymentHash Source #
Construct a PaymentHash from a 32-byte ByteString.
Returns Nothing if the input is not exactly 32 bytes.
unPaymentHash :: PaymentHash -> ByteString Source #
Extract the raw bytes from a PaymentHash.
newtype PaymentPreimage Source #
A 32-byte payment preimage.
Constructors
| PaymentPreimage ByteString |
Instances
| NFData PaymentPreimage Source # | |||||
Defined in Lightning.Protocol.BOLT1.Prim Methods rnf :: PaymentPreimage -> () # | |||||
| Generic PaymentPreimage Source # | |||||
Defined in Lightning.Protocol.BOLT1.Prim Associated Types
Methods from :: PaymentPreimage -> Rep PaymentPreimage x # to :: Rep PaymentPreimage x -> PaymentPreimage # | |||||
| Show PaymentPreimage Source # | |||||
Defined in Lightning.Protocol.BOLT1.Prim Methods showsPrec :: Int -> PaymentPreimage -> ShowS # show :: PaymentPreimage -> String # showList :: [PaymentPreimage] -> ShowS # | |||||
| Eq PaymentPreimage Source # | |||||
Defined in Lightning.Protocol.BOLT1.Prim Methods (==) :: PaymentPreimage -> PaymentPreimage -> Bool # (/=) :: PaymentPreimage -> PaymentPreimage -> Bool # | |||||
| Ord PaymentPreimage Source # | |||||
Defined in Lightning.Protocol.BOLT1.Prim Methods compare :: PaymentPreimage -> PaymentPreimage -> Ordering # (<) :: PaymentPreimage -> PaymentPreimage -> Bool # (<=) :: PaymentPreimage -> PaymentPreimage -> Bool # (>) :: PaymentPreimage -> PaymentPreimage -> Bool # (>=) :: PaymentPreimage -> PaymentPreimage -> Bool # max :: PaymentPreimage -> PaymentPreimage -> PaymentPreimage # min :: PaymentPreimage -> PaymentPreimage -> PaymentPreimage # | |||||
| type Rep PaymentPreimage Source # | |||||
Defined in Lightning.Protocol.BOLT1.Prim type Rep PaymentPreimage = D1 ('MetaData "PaymentPreimage" "Lightning.Protocol.BOLT1.Prim" "ppad-bolt1-0.0.1-3AI6PDE70OhJud7vB7rIZR" 'True) (C1 ('MetaCons "PaymentPreimage" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 ByteString))) | |||||
paymentPreimage :: ByteString -> Maybe PaymentPreimage Source #
Construct a PaymentPreimage from a 32-byte ByteString.
Returns Nothing if the input is not exactly 32 bytes.
unPaymentPreimage :: PaymentPreimage -> ByteString Source #
Extract the raw bytes from a PaymentPreimage.
Per-commitment secret
newtype PerCommitmentSecret Source #
A 32-byte per-commitment secret.
Constructors
| PerCommitmentSecret ByteString |
Instances
| NFData PerCommitmentSecret Source # | |||||
Defined in Lightning.Protocol.BOLT1.Prim Methods rnf :: PerCommitmentSecret -> () # | |||||
| Generic PerCommitmentSecret Source # | |||||
Defined in Lightning.Protocol.BOLT1.Prim Associated Types
Methods from :: PerCommitmentSecret -> Rep PerCommitmentSecret x # to :: Rep PerCommitmentSecret x -> PerCommitmentSecret # | |||||
| Show PerCommitmentSecret Source # | |||||
Defined in Lightning.Protocol.BOLT1.Prim Methods showsPrec :: Int -> PerCommitmentSecret -> ShowS # show :: PerCommitmentSecret -> String # showList :: [PerCommitmentSecret] -> ShowS # | |||||
| Eq PerCommitmentSecret Source # | |||||
Defined in Lightning.Protocol.BOLT1.Prim Methods (==) :: PerCommitmentSecret -> PerCommitmentSecret -> Bool # (/=) :: PerCommitmentSecret -> PerCommitmentSecret -> Bool # | |||||
| Ord PerCommitmentSecret Source # | |||||
Defined in Lightning.Protocol.BOLT1.Prim Methods compare :: PerCommitmentSecret -> PerCommitmentSecret -> Ordering # (<) :: PerCommitmentSecret -> PerCommitmentSecret -> Bool # (<=) :: PerCommitmentSecret -> PerCommitmentSecret -> Bool # (>) :: PerCommitmentSecret -> PerCommitmentSecret -> Bool # (>=) :: PerCommitmentSecret -> PerCommitmentSecret -> Bool # max :: PerCommitmentSecret -> PerCommitmentSecret -> PerCommitmentSecret # min :: PerCommitmentSecret -> PerCommitmentSecret -> PerCommitmentSecret # | |||||
| type Rep PerCommitmentSecret Source # | |||||
Defined in Lightning.Protocol.BOLT1.Prim type Rep PerCommitmentSecret = D1 ('MetaData "PerCommitmentSecret" "Lightning.Protocol.BOLT1.Prim" "ppad-bolt1-0.0.1-3AI6PDE70OhJud7vB7rIZR" 'True) (C1 ('MetaCons "PerCommitmentSecret" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 ByteString))) | |||||
perCommitmentSecret :: ByteString -> Maybe PerCommitmentSecret Source #
Construct a PerCommitmentSecret from a 32-byte
ByteString.
Returns Nothing if the input is not exactly 32 bytes.
unPerCommitmentSecret :: PerCommitmentSecret -> ByteString Source #
Extract the raw bytes from a PerCommitmentSecret.
Short channel identifier
newtype ShortChannelId Source #
A short channel identifier (8 bytes packed as Word64).
Encodes block height (3 bytes), transaction index (3 bytes), and output index (2 bytes).
Constructors
| ShortChannelId Word64 |
Instances
| NFData ShortChannelId Source # | |||||
Defined in Lightning.Protocol.BOLT1.Prim Methods rnf :: ShortChannelId -> () # | |||||
| Generic ShortChannelId Source # | |||||
Defined in Lightning.Protocol.BOLT1.Prim Associated Types
Methods from :: ShortChannelId -> Rep ShortChannelId x # to :: Rep ShortChannelId x -> ShortChannelId # | |||||
| Show ShortChannelId Source # | |||||
Defined in Lightning.Protocol.BOLT1.Prim Methods showsPrec :: Int -> ShortChannelId -> ShowS # show :: ShortChannelId -> String # showList :: [ShortChannelId] -> ShowS # | |||||
| Eq ShortChannelId Source # | |||||
Defined in Lightning.Protocol.BOLT1.Prim Methods (==) :: ShortChannelId -> ShortChannelId -> Bool # (/=) :: ShortChannelId -> ShortChannelId -> Bool # | |||||
| Ord ShortChannelId Source # | |||||
Defined in Lightning.Protocol.BOLT1.Prim Methods compare :: ShortChannelId -> ShortChannelId -> Ordering # (<) :: ShortChannelId -> ShortChannelId -> Bool # (<=) :: ShortChannelId -> ShortChannelId -> Bool # (>) :: ShortChannelId -> ShortChannelId -> Bool # (>=) :: ShortChannelId -> ShortChannelId -> Bool # max :: ShortChannelId -> ShortChannelId -> ShortChannelId # min :: ShortChannelId -> ShortChannelId -> ShortChannelId # | |||||
| type Rep ShortChannelId Source # | |||||
Defined in Lightning.Protocol.BOLT1.Prim type Rep ShortChannelId = D1 ('MetaData "ShortChannelId" "Lightning.Protocol.BOLT1.Prim" "ppad-bolt1-0.0.1-3AI6PDE70OhJud7vB7rIZR" 'True) (C1 ('MetaCons "ShortChannelId" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Word64))) | |||||
Arguments
| :: Word32 | Block height (24 bits max) |
| -> Word32 | Transaction index (24 bits max) |
| -> Word16 | Output index |
| -> Maybe ShortChannelId |
Construct a ShortChannelId from components.
Returns Nothing if block height or tx index exceed 24 bits.
scidWord64 :: ShortChannelId -> Word64 Source #
Extract the packed Word64 from a ShortChannelId.
scidBlockHeight :: ShortChannelId -> Word32 Source #
Extract the block height from a ShortChannelId.
scidTxIndex :: ShortChannelId -> Word32 Source #
Extract the transaction index from a ShortChannelId.
scidOutputIndex :: ShortChannelId -> Word16 Source #
Extract the output index from a ShortChannelId.
Amounts
Amount in satoshis.
Instances
| NFData Satoshi Source # | |||||
Defined in Lightning.Protocol.BOLT1.Prim | |||||
| Enum Satoshi Source # | |||||
| Generic Satoshi Source # | |||||
Defined in Lightning.Protocol.BOLT1.Prim Associated Types
| |||||
| Num Satoshi Source # | |||||
| Integral Satoshi Source # | |||||
Defined in Lightning.Protocol.BOLT1.Prim | |||||
| Real Satoshi Source # | |||||
Defined in Lightning.Protocol.BOLT1.Prim Methods toRational :: Satoshi -> Rational # | |||||
| Show Satoshi Source # | |||||
| Eq Satoshi Source # | |||||
| Ord Satoshi Source # | |||||
Defined in Lightning.Protocol.BOLT1.Prim | |||||
| type Rep Satoshi Source # | |||||
Defined in Lightning.Protocol.BOLT1.Prim | |||||
newtype MilliSatoshi Source #
Amount in millisatoshis.
Constructors
| MilliSatoshi | |
Fields | |
Instances
| NFData MilliSatoshi Source # | |||||
Defined in Lightning.Protocol.BOLT1.Prim Methods rnf :: MilliSatoshi -> () # | |||||
| Enum MilliSatoshi Source # | |||||
Defined in Lightning.Protocol.BOLT1.Prim Methods succ :: MilliSatoshi -> MilliSatoshi # pred :: MilliSatoshi -> MilliSatoshi # toEnum :: Int -> MilliSatoshi # fromEnum :: MilliSatoshi -> Int # enumFrom :: MilliSatoshi -> [MilliSatoshi] # enumFromThen :: MilliSatoshi -> MilliSatoshi -> [MilliSatoshi] # enumFromTo :: MilliSatoshi -> MilliSatoshi -> [MilliSatoshi] # enumFromThenTo :: MilliSatoshi -> MilliSatoshi -> MilliSatoshi -> [MilliSatoshi] # | |||||
| Generic MilliSatoshi Source # | |||||
Defined in Lightning.Protocol.BOLT1.Prim Associated Types
| |||||
| Num MilliSatoshi Source # | |||||
Defined in Lightning.Protocol.BOLT1.Prim Methods (+) :: MilliSatoshi -> MilliSatoshi -> MilliSatoshi # (-) :: MilliSatoshi -> MilliSatoshi -> MilliSatoshi # (*) :: MilliSatoshi -> MilliSatoshi -> MilliSatoshi # negate :: MilliSatoshi -> MilliSatoshi # abs :: MilliSatoshi -> MilliSatoshi # signum :: MilliSatoshi -> MilliSatoshi # fromInteger :: Integer -> MilliSatoshi # | |||||
| Integral MilliSatoshi Source # | |||||
Defined in Lightning.Protocol.BOLT1.Prim Methods quot :: MilliSatoshi -> MilliSatoshi -> MilliSatoshi # rem :: MilliSatoshi -> MilliSatoshi -> MilliSatoshi # div :: MilliSatoshi -> MilliSatoshi -> MilliSatoshi # mod :: MilliSatoshi -> MilliSatoshi -> MilliSatoshi # quotRem :: MilliSatoshi -> MilliSatoshi -> (MilliSatoshi, MilliSatoshi) # divMod :: MilliSatoshi -> MilliSatoshi -> (MilliSatoshi, MilliSatoshi) # toInteger :: MilliSatoshi -> Integer # | |||||
| Real MilliSatoshi Source # | |||||
Defined in Lightning.Protocol.BOLT1.Prim Methods toRational :: MilliSatoshi -> Rational # | |||||
| Show MilliSatoshi Source # | |||||
Defined in Lightning.Protocol.BOLT1.Prim Methods showsPrec :: Int -> MilliSatoshi -> ShowS # show :: MilliSatoshi -> String # showList :: [MilliSatoshi] -> ShowS # | |||||
| Eq MilliSatoshi Source # | |||||
Defined in Lightning.Protocol.BOLT1.Prim | |||||
| Ord MilliSatoshi Source # | |||||
Defined in Lightning.Protocol.BOLT1.Prim Methods compare :: MilliSatoshi -> MilliSatoshi -> Ordering # (<) :: MilliSatoshi -> MilliSatoshi -> Bool # (<=) :: MilliSatoshi -> MilliSatoshi -> Bool # (>) :: MilliSatoshi -> MilliSatoshi -> Bool # (>=) :: MilliSatoshi -> MilliSatoshi -> Bool # max :: MilliSatoshi -> MilliSatoshi -> MilliSatoshi # min :: MilliSatoshi -> MilliSatoshi -> MilliSatoshi # | |||||
| type Rep MilliSatoshi Source # | |||||
Defined in Lightning.Protocol.BOLT1.Prim type Rep MilliSatoshi = D1 ('MetaData "MilliSatoshi" "Lightning.Protocol.BOLT1.Prim" "ppad-bolt1-0.0.1-3AI6PDE70OhJud7vB7rIZR" 'True) (C1 ('MetaCons "MilliSatoshi" 'PrefixI 'True) (S1 ('MetaSel ('Just "unMilliSatoshi") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 Word64))) | |||||
satToMsat :: Satoshi -> MilliSatoshi Source #
Convert Satoshi to MilliSatoshi.
msatToSat :: MilliSatoshi -> Satoshi Source #
Convert MilliSatoshi to Satoshi (rounds down).
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.