{-# OPTIONS_HADDOCK hide #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DeriveGeneric #-}

-- |
-- Module: Lightning.Protocol.BOLT1.Codec
-- Copyright: (c) 2025 Jared Tobin
-- License: MIT
-- Maintainer: Jared Tobin <jared@ppad.tech>
--
-- Encoding and decoding of BOLT #1 messages.

module Lightning.Protocol.BOLT1.Codec (
    EncodeError(..)
  , DecodeError(..)

  , encode_envelope
  , decode_envelope

  , encode_message
  , decode_message

  , encode_init
  , decode_init
  , encode_error
  , decode_error
  , encode_warning
  , decode_warning
  , encode_ping
  , decode_ping
  , encode_pong
  , decode_pong
  , encode_peer_storage
  , decode_peer_storage
  , encode_peer_storage_retrieval
  , decode_peer_storage_retrieval

  , ping_response
  ) where

import Control.DeepSeq (NFData)
import qualified Data.ByteString as BS
import Data.Word (Word16, Word64)
import GHC.Generics (Generic)
import Lightning.Protocol.BOLT1.Message
import Lightning.Protocol.BOLT1.Prim
import Lightning.Protocol.BOLT1.TLV
import qualified Lightning.Protocol.BOLT9 as BOLT9

-- | Why a message failed to encode.
data EncodeError
  = EncodeLengthOverflow
    -- ^ a length-prefixed field exceeds 65535 bytes
  | EncodeMessageTooLarge
    -- ^ the message (type and payload) exceeds 65535 bytes
  | EncodeInvalidTlvs
    -- ^ a record in a @_tlvs@ field duplicates a typed field
  deriving (EncodeError -> EncodeError -> Bool
(EncodeError -> EncodeError -> Bool)
-> (EncodeError -> EncodeError -> Bool) -> Eq EncodeError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: EncodeError -> EncodeError -> Bool
== :: EncodeError -> EncodeError -> Bool
$c/= :: EncodeError -> EncodeError -> Bool
/= :: EncodeError -> EncodeError -> Bool
Eq, Int -> EncodeError -> ShowS
[EncodeError] -> ShowS
EncodeError -> String
(Int -> EncodeError -> ShowS)
-> (EncodeError -> String)
-> ([EncodeError] -> ShowS)
-> Show EncodeError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> EncodeError -> ShowS
showsPrec :: Int -> EncodeError -> ShowS
$cshow :: EncodeError -> String
show :: EncodeError -> String
$cshowList :: [EncodeError] -> ShowS
showList :: [EncodeError] -> ShowS
Show, (forall x. EncodeError -> Rep EncodeError x)
-> (forall x. Rep EncodeError x -> EncodeError)
-> Generic EncodeError
forall x. Rep EncodeError x -> EncodeError
forall x. EncodeError -> Rep EncodeError x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. EncodeError -> Rep EncodeError x
from :: forall x. EncodeError -> Rep EncodeError x
$cto :: forall x. Rep EncodeError x -> EncodeError
to :: forall x. Rep EncodeError x -> EncodeError
Generic)

instance NFData EncodeError

-- | Why a message failed to decode.
data DecodeError
  = DecodeInsufficientBytes
    -- ^ the input ended before a field did
  | DecodeTlvError !TlvError
    -- ^ the message's TLV stream is malformed
  | DecodeInvalidTlvValue !Word64
    -- ^ a known TLV record (of the given type) has a malformed value
  | DecodeUnknownEvenType !Word16
    -- ^ an unknown even message type; the connection must be closed
  | DecodeUnknownOddType !Word16
    -- ^ an unknown odd message type; the message must be ignored
  deriving (DecodeError -> DecodeError -> Bool
(DecodeError -> DecodeError -> Bool)
-> (DecodeError -> DecodeError -> Bool) -> Eq DecodeError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DecodeError -> DecodeError -> Bool
== :: DecodeError -> DecodeError -> Bool
$c/= :: DecodeError -> DecodeError -> Bool
/= :: DecodeError -> DecodeError -> Bool
Eq, Int -> DecodeError -> ShowS
[DecodeError] -> ShowS
DecodeError -> String
(Int -> DecodeError -> ShowS)
-> (DecodeError -> String)
-> ([DecodeError] -> ShowS)
-> Show DecodeError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DecodeError -> ShowS
showsPrec :: Int -> DecodeError -> ShowS
$cshow :: DecodeError -> String
show :: DecodeError -> String
$cshowList :: [DecodeError] -> ShowS
showList :: [DecodeError] -> ShowS
Show, (forall x. DecodeError -> Rep DecodeError x)
-> (forall x. Rep DecodeError x -> DecodeError)
-> Generic DecodeError
forall x. Rep DecodeError x -> DecodeError
forall x. DecodeError -> Rep DecodeError x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. DecodeError -> Rep DecodeError x
from :: forall x. DecodeError -> Rep DecodeError x
$cto :: forall x. Rep DecodeError x -> DecodeError
to :: forall x. Rep DecodeError x -> DecodeError
Generic)

instance NFData DecodeError

-- framing --------------------------------------------------------------------

-- | Prefix a payload with its message type. Fails if the result
--   exceeds the 65535-byte message limit.
--
--   >>> encode_envelope 19 "\NUL\NUL"
--   Right "\NUL\DC3\NUL\NUL"
encode_envelope :: Word16 -> BS.ByteString -> Either EncodeError BS.ByteString
encode_envelope :: Word16 -> ByteString -> Either EncodeError ByteString
encode_envelope Word16
t ByteString
payload
  | ByteString -> Int
BS.length ByteString
payload Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
65533 = EncodeError -> Either EncodeError ByteString
forall a b. a -> Either a b
Left EncodeError
EncodeMessageTooLarge
  | Bool
otherwise                 = ByteString -> Either EncodeError ByteString
forall a b. b -> Either a b
Right (Word16 -> ByteString
encode_u16 Word16
t ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
payload)

-- | Split a message into its type and payload.
--
--   >>> decode_envelope "\NUL\DC3\NUL\NUL"
--   Right (19,"\NUL\NUL")
decode_envelope :: BS.ByteString -> Either DecodeError (Word16, BS.ByteString)
decode_envelope :: ByteString -> Either DecodeError (Word16, ByteString)
decode_envelope ByteString
bs = case ByteString -> Maybe (Word16, ByteString)
decode_u16 ByteString
bs of
  Just (Word16, ByteString)
r  -> (Word16, ByteString) -> Either DecodeError (Word16, ByteString)
forall a b. b -> Either a b
Right (Word16, ByteString)
r
  Maybe (Word16, ByteString)
Nothing -> DecodeError -> Either DecodeError (Word16, ByteString)
forall a b. a -> Either a b
Left DecodeError
DecodeInsufficientBytes

-- | Encode a BOLT #1 message, including its type.
--
--   >>> encode_message (MsgPong (Pong "" empty_tlv_stream))
--   Right "\NUL\DC3\NUL\NUL"
encode_message :: Message -> Either EncodeError BS.ByteString
encode_message :: Message -> Either EncodeError ByteString
encode_message Message
m = do
  payload <- case Message
m of
    MsgInit Init
a                 -> Init -> Either EncodeError ByteString
encode_init Init
a
    MsgError Error
a                -> Error -> Either EncodeError ByteString
encode_error Error
a
    MsgWarning Warning
a              -> Warning -> Either EncodeError ByteString
encode_warning Warning
a
    MsgPing Ping
a                 -> Ping -> Either EncodeError ByteString
encode_ping Ping
a
    MsgPong Pong
a                 -> Pong -> Either EncodeError ByteString
encode_pong Pong
a
    MsgPeerStorage PeerStorage
a          -> PeerStorage -> Either EncodeError ByteString
encode_peer_storage PeerStorage
a
    MsgPeerStorageRetrieval PeerStorageRetrieval
a -> PeerStorageRetrieval -> Either EncodeError ByteString
encode_peer_storage_retrieval PeerStorageRetrieval
a
  encode_envelope (message_type m) payload

-- | Decode a BOLT #1 message, including its type.
--
--   A type that BOLT #1 doesn't define yields 'DecodeUnknownEvenType'
--   or 'DecodeUnknownOddType'; callers dispatching messages from other
--   BOLTs should use 'decode_envelope' first.
--
--   >>> decode_message "\NUL\DC3\NUL\NUL"
--   Right (MsgPong (Pong {pong_ignored = "", pong_tlvs = TlvStream []}))
--   >>> decode_message "\NUL\DC4\NUL\NUL"
--   Left (DecodeUnknownEvenType 20)
decode_message :: BS.ByteString -> Either DecodeError Message
decode_message :: ByteString -> Either DecodeError Message
decode_message ByteString
bs = do
  (t, payload) <- ByteString -> Either DecodeError (Word16, ByteString)
decode_envelope ByteString
bs
  case t of
    Word16
16 -> Init -> Message
MsgInit (Init -> Message)
-> Either DecodeError Init -> Either DecodeError Message
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either DecodeError Init
decode_init ByteString
payload
    Word16
17 -> Error -> Message
MsgError (Error -> Message)
-> Either DecodeError Error -> Either DecodeError Message
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either DecodeError Error
decode_error ByteString
payload
    Word16
1  -> Warning -> Message
MsgWarning (Warning -> Message)
-> Either DecodeError Warning -> Either DecodeError Message
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either DecodeError Warning
decode_warning ByteString
payload
    Word16
18 -> Ping -> Message
MsgPing (Ping -> Message)
-> Either DecodeError Ping -> Either DecodeError Message
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either DecodeError Ping
decode_ping ByteString
payload
    Word16
19 -> Pong -> Message
MsgPong (Pong -> Message)
-> Either DecodeError Pong -> Either DecodeError Message
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either DecodeError Pong
decode_pong ByteString
payload
    Word16
7  -> PeerStorage -> Message
MsgPeerStorage (PeerStorage -> Message)
-> Either DecodeError PeerStorage -> Either DecodeError Message
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either DecodeError PeerStorage
decode_peer_storage ByteString
payload
    Word16
9  -> PeerStorageRetrieval -> Message
MsgPeerStorageRetrieval (PeerStorageRetrieval -> Message)
-> Either DecodeError PeerStorageRetrieval
-> Either DecodeError Message
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either DecodeError PeerStorageRetrieval
decode_peer_storage_retrieval ByteString
payload
    Word16
_ | Word16 -> Bool
forall a. Integral a => a -> Bool
even Word16
t    -> DecodeError -> Either DecodeError Message
forall a b. a -> Either a b
Left (Word16 -> DecodeError
DecodeUnknownEvenType Word16
t)
      | Bool
otherwise -> DecodeError -> Either DecodeError Message
forall a b. a -> Either a b
Left (Word16 -> DecodeError
DecodeUnknownOddType Word16
t)

-- helpers --------------------------------------------------------------------

prefixed :: BS.ByteString -> Either EncodeError BS.ByteString
prefixed :: ByteString -> Either EncodeError ByteString
prefixed ByteString
bs = Either EncodeError ByteString
-> (ByteString -> Either EncodeError ByteString)
-> Maybe ByteString
-> Either EncodeError ByteString
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (EncodeError -> Either EncodeError ByteString
forall a b. a -> Either a b
Left EncodeError
EncodeLengthOverflow) ByteString -> Either EncodeError ByteString
forall a b. b -> Either a b
Right (ByteString -> Maybe ByteString
encode_u16_prefixed ByteString
bs)

need :: Maybe a -> Either DecodeError a
need :: forall a. Maybe a -> Either DecodeError a
need = Either DecodeError a
-> (a -> Either DecodeError a) -> Maybe a -> Either DecodeError a
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (DecodeError -> Either DecodeError a
forall a b. a -> Either a b
Left DecodeError
DecodeInsufficientBytes) a -> Either DecodeError a
forall a b. b -> Either a b
Right
{-# INLINE need #-}

-- an extension stream in which no types are known
extension :: BS.ByteString -> Either DecodeError TlvStream
extension :: ByteString -> Either DecodeError TlvStream
extension ByteString
bs = (TlvError -> Either DecodeError TlvStream)
-> (TlvStream -> Either DecodeError TlvStream)
-> Either TlvError TlvStream
-> Either DecodeError TlvStream
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (DecodeError -> Either DecodeError TlvStream
forall a b. a -> Either a b
Left (DecodeError -> Either DecodeError TlvStream)
-> (TlvError -> DecodeError)
-> TlvError
-> Either DecodeError TlvStream
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TlvError -> DecodeError
DecodeTlvError) TlvStream -> Either DecodeError TlvStream
forall a b. b -> Either a b
Right
  ((Word64 -> Bool) -> ByteString -> Either TlvError TlvStream
decode_tlv_stream (Bool -> Word64 -> Bool
forall a b. a -> b -> a
const Bool
False) ByteString
bs)

-- init -----------------------------------------------------------------------

-- | Encode an 'Init' payload.
encode_init :: Init -> Either EncodeError BS.ByteString
encode_init :: Init -> Either EncodeError ByteString
encode_init (Init FeatureVector
gf FeatureVector
f Maybe [ChainHash]
nets Maybe ByteString
addr TlvStream
tlvs) = do
  gf' <- ByteString -> Either EncodeError ByteString
prefixed (FeatureVector -> ByteString
BOLT9.render FeatureVector
gf)
  f'  <- prefixed (BOLT9.render f)
  let known = [TlvRecord]
-> ([ChainHash] -> [TlvRecord]) -> Maybe [ChainHash] -> [TlvRecord]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] (\[ChainHash]
cs -> [Word64 -> ByteString -> TlvRecord
TlvRecord Word64
1 ([ByteString] -> ByteString
forall a. Monoid a => [a] -> a
mconcat ((ChainHash -> ByteString) -> [ChainHash] -> [ByteString]
forall a b. (a -> b) -> [a] -> [b]
map ChainHash -> ByteString
un_chain_hash [ChainHash]
cs))])
                Maybe [ChainHash]
nets
           [TlvRecord] -> [TlvRecord] -> [TlvRecord]
forall a. Semigroup a => a -> a -> a
<> [TlvRecord]
-> (ByteString -> [TlvRecord]) -> Maybe ByteString -> [TlvRecord]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] (\ByteString
a -> [Word64 -> ByteString -> TlvRecord
TlvRecord Word64
3 ByteString
a]) Maybe ByteString
addr
  stream <- maybe (Left EncodeInvalidTlvs) Right
              (tlv_stream (known <> un_tlv_stream tlvs))
  pure (gf' <> f' <> encode_tlv_stream stream)

-- | Decode an 'Init' payload.
decode_init :: BS.ByteString -> Either DecodeError Init
decode_init :: ByteString -> Either DecodeError Init
decode_init ByteString
bs = do
  (gf, r0) <- Maybe (ByteString, ByteString)
-> Either DecodeError (ByteString, ByteString)
forall a. Maybe a -> Either DecodeError a
need (ByteString -> Maybe (ByteString, ByteString)
decode_u16_prefixed ByteString
bs)
  (f, r1)  <- need (decode_u16_prefixed r0)
  stream   <- either (Left . DecodeTlvError) Right
                (decode_tlv_stream (\Word64
t -> Word64
t Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
1 Bool -> Bool -> Bool
|| Word64
t Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
3) r1)
  nets <- traverse networks (lookup_tlv 1 stream)
  pure Init
    { init_global_features = BOLT9.parse gf
    , init_features        = BOLT9.parse f
    , init_networks        = nets
    , init_remote_addr     = lookup_tlv 3 stream
    , init_tlvs            = filter_tlv_stream (\Word64
t -> Word64
t Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word64
1 Bool -> Bool -> Bool
&& Word64
t Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word64
3) stream
    }
  where
    networks :: ByteString -> Either DecodeError [ChainHash]
networks ByteString
v
      | ByteString -> Int
BS.length ByteString
v Int -> Int -> Int
forall a. Integral a => a -> a -> a
`rem` Int
32 Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
0 = DecodeError -> Either DecodeError [ChainHash]
forall a b. a -> Either a b
Left (Word64 -> DecodeError
DecodeInvalidTlvValue Word64
1)
      | Bool
otherwise = [ChainHash] -> Either DecodeError [ChainHash]
forall a b. b -> Either a b
Right (ByteString -> [ChainHash]
chunks ByteString
v)
    chunks :: ByteString -> [ChainHash]
chunks !ByteString
v = case ByteString -> Maybe (ChainHash, ByteString)
decode_chain_hash ByteString
v of
      Just (ChainHash
h, ByteString
rest) -> ChainHash
h ChainHash -> [ChainHash] -> [ChainHash]
forall a. a -> [a] -> [a]
: ByteString -> [ChainHash]
chunks ByteString
rest
      Maybe (ChainHash, ByteString)
Nothing        -> []

-- error, warning -------------------------------------------------------------

-- | Encode an 'Error' payload.
encode_error :: Error -> Either EncodeError BS.ByteString
encode_error :: Error -> Either EncodeError ByteString
encode_error (Error ChannelId
cid ByteString
dat TlvStream
tlvs) = do
  dat' <- ByteString -> Either EncodeError ByteString
prefixed ByteString
dat
  pure (un_channel_id cid <> dat' <> encode_tlv_stream tlvs)

-- | Decode an 'Error' payload.
decode_error :: BS.ByteString -> Either DecodeError Error
decode_error :: ByteString -> Either DecodeError Error
decode_error ByteString
bs = do
  (cid, r0) <- Maybe (ChannelId, ByteString)
-> Either DecodeError (ChannelId, ByteString)
forall a. Maybe a -> Either DecodeError a
need (ByteString -> Maybe (ChannelId, ByteString)
decode_channel_id ByteString
bs)
  (dat, r1) <- need (decode_u16_prefixed r0)
  Error cid dat <$> extension r1

-- | Encode a 'Warning' payload.
encode_warning :: Warning -> Either EncodeError BS.ByteString
encode_warning :: Warning -> Either EncodeError ByteString
encode_warning (Warning ChannelId
cid ByteString
dat TlvStream
tlvs) = do
  dat' <- ByteString -> Either EncodeError ByteString
prefixed ByteString
dat
  pure (un_channel_id cid <> dat' <> encode_tlv_stream tlvs)

-- | Decode a 'Warning' payload.
decode_warning :: BS.ByteString -> Either DecodeError Warning
decode_warning :: ByteString -> Either DecodeError Warning
decode_warning ByteString
bs = do
  (cid, r0) <- Maybe (ChannelId, ByteString)
-> Either DecodeError (ChannelId, ByteString)
forall a. Maybe a -> Either DecodeError a
need (ByteString -> Maybe (ChannelId, ByteString)
decode_channel_id ByteString
bs)
  (dat, r1) <- need (decode_u16_prefixed r0)
  Warning cid dat <$> extension r1

-- ping, pong -----------------------------------------------------------------

-- | Encode a 'Ping' payload.
encode_ping :: Ping -> Either EncodeError BS.ByteString
encode_ping :: Ping -> Either EncodeError ByteString
encode_ping (Ping Word16
n ByteString
ign TlvStream
tlvs) = do
  ign' <- ByteString -> Either EncodeError ByteString
prefixed ByteString
ign
  pure (encode_u16 n <> ign' <> encode_tlv_stream tlvs)

-- | Decode a 'Ping' payload.
decode_ping :: BS.ByteString -> Either DecodeError Ping
decode_ping :: ByteString -> Either DecodeError Ping
decode_ping ByteString
bs = do
  (n, r0)   <- Maybe (Word16, ByteString)
-> Either DecodeError (Word16, ByteString)
forall a. Maybe a -> Either DecodeError a
need (ByteString -> Maybe (Word16, ByteString)
decode_u16 ByteString
bs)
  (ign, r1) <- need (decode_u16_prefixed r0)
  Ping n ign <$> extension r1

-- | Encode a 'Pong' payload.
encode_pong :: Pong -> Either EncodeError BS.ByteString
encode_pong :: Pong -> Either EncodeError ByteString
encode_pong (Pong ByteString
ign TlvStream
tlvs) = do
  ign' <- ByteString -> Either EncodeError ByteString
prefixed ByteString
ign
  pure (ign' <> encode_tlv_stream tlvs)

-- | Decode a 'Pong' payload.
decode_pong :: BS.ByteString -> Either DecodeError Pong
decode_pong :: ByteString -> Either DecodeError Pong
decode_pong ByteString
bs = do
  (ign, r0) <- Maybe (ByteString, ByteString)
-> Either DecodeError (ByteString, ByteString)
forall a. Maybe a -> Either DecodeError a
need (ByteString -> Maybe (ByteString, ByteString)
decode_u16_prefixed ByteString
bs)
  Pong ign <$> extension r0

-- | The 'Pong' a node must send in reply to a 'Ping', if any. Per
--   BOLT #1, a ping requesting 65532 or more bytes is ignored.
--
--   >>> ping_response (Ping 4 "" empty_tlv_stream)
--   Just (Pong {pong_ignored = "\NUL\NUL\NUL\NUL", pong_tlvs = TlvStream []})
--   >>> ping_response (Ping 65532 "" empty_tlv_stream)
--   Nothing
ping_response :: Ping -> Maybe Pong
ping_response :: Ping -> Maybe Pong
ping_response (Ping Word16
n ByteString
_ TlvStream
_)
  | Word16
n Word16 -> Word16 -> Bool
forall a. Ord a => a -> a -> Bool
>= Word16
65532 = Maybe Pong
forall a. Maybe a
Nothing
  | Bool
otherwise  =
      Pong -> Maybe Pong
forall a. a -> Maybe a
Just (ByteString -> TlvStream -> Pong
Pong (Int -> Word8 -> ByteString
BS.replicate (Word16 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word16
n) Word8
0x00) TlvStream
empty_tlv_stream)

-- peer storage ---------------------------------------------------------------

-- | Encode a 'PeerStorage' payload.
encode_peer_storage :: PeerStorage -> Either EncodeError BS.ByteString
encode_peer_storage :: PeerStorage -> Either EncodeError ByteString
encode_peer_storage (PeerStorage ByteString
blob TlvStream
tlvs) = do
  blob' <- ByteString -> Either EncodeError ByteString
prefixed ByteString
blob
  pure (blob' <> encode_tlv_stream tlvs)

-- | Decode a 'PeerStorage' payload.
decode_peer_storage :: BS.ByteString -> Either DecodeError PeerStorage
decode_peer_storage :: ByteString -> Either DecodeError PeerStorage
decode_peer_storage ByteString
bs = do
  (blob, r0) <- Maybe (ByteString, ByteString)
-> Either DecodeError (ByteString, ByteString)
forall a. Maybe a -> Either DecodeError a
need (ByteString -> Maybe (ByteString, ByteString)
decode_u16_prefixed ByteString
bs)
  PeerStorage blob <$> extension r0

-- | Encode a 'PeerStorageRetrieval' payload.
encode_peer_storage_retrieval
  :: PeerStorageRetrieval -> Either EncodeError BS.ByteString
encode_peer_storage_retrieval :: PeerStorageRetrieval -> Either EncodeError ByteString
encode_peer_storage_retrieval (PeerStorageRetrieval ByteString
blob TlvStream
tlvs) = do
  blob' <- ByteString -> Either EncodeError ByteString
prefixed ByteString
blob
  pure (blob' <> encode_tlv_stream tlvs)

-- | Decode a 'PeerStorageRetrieval' payload.
decode_peer_storage_retrieval
  :: BS.ByteString -> Either DecodeError PeerStorageRetrieval
decode_peer_storage_retrieval :: ByteString -> Either DecodeError PeerStorageRetrieval
decode_peer_storage_retrieval ByteString
bs = do
  (blob, r0) <- Maybe (ByteString, ByteString)
-> Either DecodeError (ByteString, ByteString)
forall a. Maybe a -> Either DecodeError a
need (ByteString -> Maybe (ByteString, ByteString)
decode_u16_prefixed ByteString
bs)
  PeerStorageRetrieval blob <$> extension r0