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

-- |
-- Module: Lightning.Protocol.BOLT1.Message
-- Copyright: (c) 2025 Jared Tobin
-- License: MIT
-- Maintainer: Jared Tobin <jared@ppad.tech>
--
-- The messages defined by BOLT #1.

module Lightning.Protocol.BOLT1.Message (
    Init(..)
  , Error(..)
  , Warning(..)
  , Ping(..)
  , Pong(..)
  , PeerStorage(..)
  , PeerStorageRetrieval(..)
  , Message(..)
  , message_type
  ) where

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

-- | The @init@ message (type 16).
--
--   The two feature vectors are kept as received. Per BOLT #1, a
--   receiver treats their union ('BOLT9.union') as the peer's
--   features.
data Init = Init
  { Init -> FeatureVector
init_global_features :: !BOLT9.FeatureVector
  , Init -> FeatureVector
init_features        :: !BOLT9.FeatureVector
  , Init -> Maybe [ChainHash]
init_networks        :: !(Maybe [ChainHash])
    -- ^ @networks@ (TLV type 1): chains the node is interested in
  , Init -> Maybe ByteString
init_remote_addr     :: !(Maybe BS.ByteString)
    -- ^ @remote_addr@ (TLV type 3): a raw BOLT #7 address descriptor
  , Init -> TlvStream
init_tlvs            :: !TlvStream
    -- ^ unknown odd TLV records
  } deriving (Init -> Init -> Bool
(Init -> Init -> Bool) -> (Init -> Init -> Bool) -> Eq Init
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Init -> Init -> Bool
== :: Init -> Init -> Bool
$c/= :: Init -> Init -> Bool
/= :: Init -> Init -> Bool
Eq, Int -> Init -> ShowS
[Init] -> ShowS
Init -> String
(Int -> Init -> ShowS)
-> (Init -> String) -> ([Init] -> ShowS) -> Show Init
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Init -> ShowS
showsPrec :: Int -> Init -> ShowS
$cshow :: Init -> String
show :: Init -> String
$cshowList :: [Init] -> ShowS
showList :: [Init] -> ShowS
Show, (forall x. Init -> Rep Init x)
-> (forall x. Rep Init x -> Init) -> Generic Init
forall x. Rep Init x -> Init
forall x. Init -> Rep Init x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Init -> Rep Init x
from :: forall x. Init -> Rep Init x
$cto :: forall x. Rep Init x -> Init
to :: forall x. Rep Init x -> Init
Generic)

instance NFData Init

-- | The @error@ message (type 17).
data Error = Error
  { Error -> ChannelId
error_channel_id :: !ChannelId
  , Error -> ByteString
error_data       :: !BS.ByteString
  , Error -> TlvStream
error_tlvs       :: !TlvStream
    -- ^ extension TLV records (all unknown odd)
  } deriving (Error -> Error -> Bool
(Error -> Error -> Bool) -> (Error -> Error -> Bool) -> Eq Error
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Error -> Error -> Bool
== :: Error -> Error -> Bool
$c/= :: Error -> Error -> Bool
/= :: Error -> Error -> Bool
Eq, Int -> Error -> ShowS
[Error] -> ShowS
Error -> String
(Int -> Error -> ShowS)
-> (Error -> String) -> ([Error] -> ShowS) -> Show Error
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Error -> ShowS
showsPrec :: Int -> Error -> ShowS
$cshow :: Error -> String
show :: Error -> String
$cshowList :: [Error] -> ShowS
showList :: [Error] -> ShowS
Show, (forall x. Error -> Rep Error x)
-> (forall x. Rep Error x -> Error) -> Generic Error
forall x. Rep Error x -> Error
forall x. Error -> Rep Error x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Error -> Rep Error x
from :: forall x. Error -> Rep Error x
$cto :: forall x. Rep Error x -> Error
to :: forall x. Rep Error x -> Error
Generic)

instance NFData Error

-- | The @warning@ message (type 1).
data Warning = Warning
  { Warning -> ChannelId
warning_channel_id :: !ChannelId
  , Warning -> ByteString
warning_data       :: !BS.ByteString
  , Warning -> TlvStream
warning_tlvs       :: !TlvStream
    -- ^ extension TLV records (all unknown odd)
  } deriving (Warning -> Warning -> Bool
(Warning -> Warning -> Bool)
-> (Warning -> Warning -> Bool) -> Eq Warning
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Warning -> Warning -> Bool
== :: Warning -> Warning -> Bool
$c/= :: Warning -> Warning -> Bool
/= :: Warning -> Warning -> Bool
Eq, Int -> Warning -> ShowS
[Warning] -> ShowS
Warning -> String
(Int -> Warning -> ShowS)
-> (Warning -> String) -> ([Warning] -> ShowS) -> Show Warning
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Warning -> ShowS
showsPrec :: Int -> Warning -> ShowS
$cshow :: Warning -> String
show :: Warning -> String
$cshowList :: [Warning] -> ShowS
showList :: [Warning] -> ShowS
Show, (forall x. Warning -> Rep Warning x)
-> (forall x. Rep Warning x -> Warning) -> Generic Warning
forall x. Rep Warning x -> Warning
forall x. Warning -> Rep Warning x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Warning -> Rep Warning x
from :: forall x. Warning -> Rep Warning x
$cto :: forall x. Rep Warning x -> Warning
to :: forall x. Rep Warning x -> Warning
Generic)

instance NFData Warning

-- | The @ping@ message (type 18).
data Ping = Ping
  { Ping -> Word16
ping_num_pong_bytes :: {-# UNPACK #-} !Word16
  , Ping -> ByteString
ping_ignored        :: !BS.ByteString
  , Ping -> TlvStream
ping_tlvs           :: !TlvStream
    -- ^ extension TLV records (all unknown odd)
  } deriving (Ping -> Ping -> Bool
(Ping -> Ping -> Bool) -> (Ping -> Ping -> Bool) -> Eq Ping
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Ping -> Ping -> Bool
== :: Ping -> Ping -> Bool
$c/= :: Ping -> Ping -> Bool
/= :: Ping -> Ping -> Bool
Eq, Int -> Ping -> ShowS
[Ping] -> ShowS
Ping -> String
(Int -> Ping -> ShowS)
-> (Ping -> String) -> ([Ping] -> ShowS) -> Show Ping
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Ping -> ShowS
showsPrec :: Int -> Ping -> ShowS
$cshow :: Ping -> String
show :: Ping -> String
$cshowList :: [Ping] -> ShowS
showList :: [Ping] -> ShowS
Show, (forall x. Ping -> Rep Ping x)
-> (forall x. Rep Ping x -> Ping) -> Generic Ping
forall x. Rep Ping x -> Ping
forall x. Ping -> Rep Ping x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Ping -> Rep Ping x
from :: forall x. Ping -> Rep Ping x
$cto :: forall x. Rep Ping x -> Ping
to :: forall x. Rep Ping x -> Ping
Generic)

instance NFData Ping

-- | The @pong@ message (type 19).
data Pong = Pong
  { Pong -> ByteString
pong_ignored :: !BS.ByteString
  , Pong -> TlvStream
pong_tlvs    :: !TlvStream
    -- ^ extension TLV records (all unknown odd)
  } deriving (Pong -> Pong -> Bool
(Pong -> Pong -> Bool) -> (Pong -> Pong -> Bool) -> Eq Pong
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Pong -> Pong -> Bool
== :: Pong -> Pong -> Bool
$c/= :: Pong -> Pong -> Bool
/= :: Pong -> Pong -> Bool
Eq, Int -> Pong -> ShowS
[Pong] -> ShowS
Pong -> String
(Int -> Pong -> ShowS)
-> (Pong -> String) -> ([Pong] -> ShowS) -> Show Pong
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Pong -> ShowS
showsPrec :: Int -> Pong -> ShowS
$cshow :: Pong -> String
show :: Pong -> String
$cshowList :: [Pong] -> ShowS
showList :: [Pong] -> ShowS
Show, (forall x. Pong -> Rep Pong x)
-> (forall x. Rep Pong x -> Pong) -> Generic Pong
forall x. Rep Pong x -> Pong
forall x. Pong -> Rep Pong x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Pong -> Rep Pong x
from :: forall x. Pong -> Rep Pong x
$cto :: forall x. Rep Pong x -> Pong
to :: forall x. Rep Pong x -> Pong
Generic)

instance NFData Pong

-- | The @peer_storage@ message (type 7).
data PeerStorage = PeerStorage
  { PeerStorage -> ByteString
peer_storage_blob :: !BS.ByteString
  , PeerStorage -> TlvStream
peer_storage_tlvs :: !TlvStream
    -- ^ extension TLV records (all unknown odd)
  } deriving (PeerStorage -> PeerStorage -> Bool
(PeerStorage -> PeerStorage -> Bool)
-> (PeerStorage -> PeerStorage -> Bool) -> Eq PeerStorage
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PeerStorage -> PeerStorage -> Bool
== :: PeerStorage -> PeerStorage -> Bool
$c/= :: PeerStorage -> PeerStorage -> Bool
/= :: PeerStorage -> PeerStorage -> Bool
Eq, Int -> PeerStorage -> ShowS
[PeerStorage] -> ShowS
PeerStorage -> String
(Int -> PeerStorage -> ShowS)
-> (PeerStorage -> String)
-> ([PeerStorage] -> ShowS)
-> Show PeerStorage
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PeerStorage -> ShowS
showsPrec :: Int -> PeerStorage -> ShowS
$cshow :: PeerStorage -> String
show :: PeerStorage -> String
$cshowList :: [PeerStorage] -> ShowS
showList :: [PeerStorage] -> ShowS
Show, (forall x. PeerStorage -> Rep PeerStorage x)
-> (forall x. Rep PeerStorage x -> PeerStorage)
-> Generic PeerStorage
forall x. Rep PeerStorage x -> PeerStorage
forall x. PeerStorage -> Rep PeerStorage x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. PeerStorage -> Rep PeerStorage x
from :: forall x. PeerStorage -> Rep PeerStorage x
$cto :: forall x. Rep PeerStorage x -> PeerStorage
to :: forall x. Rep PeerStorage x -> PeerStorage
Generic)

instance NFData PeerStorage

-- | The @peer_storage_retrieval@ message (type 9).
data PeerStorageRetrieval = PeerStorageRetrieval
  { PeerStorageRetrieval -> ByteString
peer_storage_retrieval_blob :: !BS.ByteString
  , PeerStorageRetrieval -> TlvStream
peer_storage_retrieval_tlvs :: !TlvStream
    -- ^ extension TLV records (all unknown odd)
  } deriving (PeerStorageRetrieval -> PeerStorageRetrieval -> Bool
(PeerStorageRetrieval -> PeerStorageRetrieval -> Bool)
-> (PeerStorageRetrieval -> PeerStorageRetrieval -> Bool)
-> Eq PeerStorageRetrieval
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PeerStorageRetrieval -> PeerStorageRetrieval -> Bool
== :: PeerStorageRetrieval -> PeerStorageRetrieval -> Bool
$c/= :: PeerStorageRetrieval -> PeerStorageRetrieval -> Bool
/= :: PeerStorageRetrieval -> PeerStorageRetrieval -> Bool
Eq, Int -> PeerStorageRetrieval -> ShowS
[PeerStorageRetrieval] -> ShowS
PeerStorageRetrieval -> String
(Int -> PeerStorageRetrieval -> ShowS)
-> (PeerStorageRetrieval -> String)
-> ([PeerStorageRetrieval] -> ShowS)
-> Show PeerStorageRetrieval
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PeerStorageRetrieval -> ShowS
showsPrec :: Int -> PeerStorageRetrieval -> ShowS
$cshow :: PeerStorageRetrieval -> String
show :: PeerStorageRetrieval -> String
$cshowList :: [PeerStorageRetrieval] -> ShowS
showList :: [PeerStorageRetrieval] -> ShowS
Show, (forall x. PeerStorageRetrieval -> Rep PeerStorageRetrieval x)
-> (forall x. Rep PeerStorageRetrieval x -> PeerStorageRetrieval)
-> Generic PeerStorageRetrieval
forall x. Rep PeerStorageRetrieval x -> PeerStorageRetrieval
forall x. PeerStorageRetrieval -> Rep PeerStorageRetrieval x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. PeerStorageRetrieval -> Rep PeerStorageRetrieval x
from :: forall x. PeerStorageRetrieval -> Rep PeerStorageRetrieval x
$cto :: forall x. Rep PeerStorageRetrieval x -> PeerStorageRetrieval
to :: forall x. Rep PeerStorageRetrieval x -> PeerStorageRetrieval
Generic)

instance NFData PeerStorageRetrieval

-- | A BOLT #1 message.
data Message
  = MsgInit !Init
  | MsgError !Error
  | MsgWarning !Warning
  | MsgPing !Ping
  | MsgPong !Pong
  | MsgPeerStorage !PeerStorage
  | MsgPeerStorageRetrieval !PeerStorageRetrieval
  deriving (Message -> Message -> Bool
(Message -> Message -> Bool)
-> (Message -> Message -> Bool) -> Eq Message
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Message -> Message -> Bool
== :: Message -> Message -> Bool
$c/= :: Message -> Message -> Bool
/= :: Message -> Message -> Bool
Eq, Int -> Message -> ShowS
[Message] -> ShowS
Message -> String
(Int -> Message -> ShowS)
-> (Message -> String) -> ([Message] -> ShowS) -> Show Message
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Message -> ShowS
showsPrec :: Int -> Message -> ShowS
$cshow :: Message -> String
show :: Message -> String
$cshowList :: [Message] -> ShowS
showList :: [Message] -> ShowS
Show, (forall x. Message -> Rep Message x)
-> (forall x. Rep Message x -> Message) -> Generic Message
forall x. Rep Message x -> Message
forall x. Message -> Rep Message x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Message -> Rep Message x
from :: forall x. Message -> Rep Message x
$cto :: forall x. Rep Message x -> Message
to :: forall x. Rep Message x -> Message
Generic)

instance NFData Message

-- | The wire type of a 'Message'.
--
--   >>> message_type (MsgPong (Pong "" empty_tlv_stream))
--   19
message_type :: Message -> Word16
message_type :: Message -> Word16
message_type Message
m = case Message
m of
  MsgInit {}                 -> Word16
16
  MsgError {}                -> Word16
17
  MsgWarning {}              -> Word16
1
  MsgPing {}                 -> Word16
18
  MsgPong {}                 -> Word16
19
  MsgPeerStorage {}          -> Word16
7
  MsgPeerStorageRetrieval {} -> Word16
9