commit 011fdb633264a59d10243e60c7bf403a84e519e3
parent 1353cecbe771b61d2d1dbee207fe0b14e929b389
Author: Jared Tobin <jared@jtobin.io>
Date: Sun, 25 Jan 2026 15:54:36 +0400
Refactor: extract encodeLenPrefixed helper
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Diffstat:
1 file changed, 10 insertions(+), 12 deletions(-)
diff --git a/lib/Lightning/Protocol/BOLT7/Codec.hs b/lib/Lightning/Protocol/BOLT7/Codec.hs
@@ -135,6 +135,11 @@ decodeLenPrefixed bs = do
else Right (BS.splitAt n rest)
{-# INLINE decodeLenPrefixed #-}
+-- | Encode with u16 length prefix.
+encodeLenPrefixed :: ByteString -> ByteString
+encodeLenPrefixed bs = Prim.encodeU16 (fromIntegral $ BS.length bs) <> bs
+{-# INLINE encodeLenPrefixed #-}
+
-- Type-specific decoders ------------------------------------------------------
-- | Decode Signature (64 bytes).
@@ -270,8 +275,7 @@ encodeChannelAnnouncement msg = mconcat
, getSignature (channelAnnNodeSig2 msg)
, getSignature (channelAnnBitcoinSig1 msg)
, getSignature (channelAnnBitcoinSig2 msg)
- , Prim.encodeU16 (fromIntegral $ BS.length features)
- , features
+ , encodeLenPrefixed (getFeatureBits (channelAnnFeatures msg))
, getChainHash (channelAnnChainHash msg)
, getShortChannelId (channelAnnShortChanId msg)
, getNodeId (channelAnnNodeId1 msg)
@@ -279,8 +283,6 @@ encodeChannelAnnouncement msg = mconcat
, getPoint (channelAnnBitcoinKey1 msg)
, getPoint (channelAnnBitcoinKey2 msg)
]
- where
- features = getFeatureBits (channelAnnFeatures msg)
-- | Decode channel_announcement message.
decodeChannelAnnouncement :: ByteString
@@ -323,14 +325,12 @@ encodeNodeAnnouncement msg = do
then Left EncodeLengthOverflow
else Right $ mconcat
[ getSignature (nodeAnnSignature msg)
- , Prim.encodeU16 (fromIntegral $ BS.length features)
- , features
+ , encodeLenPrefixed features
, Prim.encodeU32 (nodeAnnTimestamp msg)
, getNodeId (nodeAnnNodeId msg)
, getRgbColor (nodeAnnRgbColor msg)
, getAlias (nodeAnnAlias msg)
- , Prim.encodeU16 (fromIntegral $ BS.length addrData)
- , addrData
+ , encodeLenPrefixed addrData
]
-- | Encode address list.
@@ -476,8 +476,7 @@ encodeQueryShortChannelIds msg = do
then Left EncodeLengthOverflow
else Right $ mconcat
[ getChainHash (queryScidsChainHash msg)
- , Prim.encodeU16 (fromIntegral $ BS.length scidData)
- , scidData
+ , encodeLenPrefixed scidData
, TLV.encodeTlvStream (queryScidsTlvs msg)
]
@@ -556,8 +555,7 @@ encodeReplyChannelRange msg = do
, Prim.encodeU32 (replyRangeFirstBlock msg)
, Prim.encodeU32 (replyRangeNumBlocks msg)
, BS.singleton (replyRangeSyncComplete msg)
- , Prim.encodeU16 (fromIntegral $ BS.length rangeData)
- , rangeData
+ , encodeLenPrefixed rangeData
, TLV.encodeTlvStream (replyRangeTlvs msg)
]