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

-- |
-- Module: Lightning.Protocol.BOLT4.Types
-- Copyright: (c) 2025 Jared Tobin
-- License: MIT
-- Maintainer: Jared Tobin <jared@ppad.tech>
--
-- Data types for BOLT #4.

module Lightning.Protocol.BOLT4.Types (
  -- * Onion packets
    OnionPacket(..)
  , HopPayloads(..)
  , hop_payloads
  , un_hop_payloads
  , Hmac32(..)
  , hmac32
  , un_hmac32

  -- * Hop payloads
  , HopPayload(..)
  , empty_hop_payload
  , PaymentData(..)
  , PaymentSecret(..)
  , payment_secret
  , un_payment_secret

  -- * Route blinding
  , BlindedPath(..)
  , BlindedHop(..)
  , BlindedHopData(..)
  , empty_blinded_hop_data
  , PaymentRelay(..)
  , PaymentConstraints(..)
  , BlindedInfo(..)

  -- * Failure messages
  , FailureMessage(..)
  , Failure(..)
  , OnionHash(..)
  , onion_hash
  , un_onion_hash
  , failure_code
  , is_badonion
  , is_perm
  , is_node
  , is_update

  -- * Errors
  , DecodeError(..)
  , EncodeError(..)
  , ProcessError(..)
  ) where

import Control.DeepSeq (NFData(..))
import Data.Bits ((.&.))
import qualified Data.ByteString as BS
import Data.Word (Word8, Word16, Word32, Word64)
import GHC.Generics (Generic)
import qualified Lightning.Protocol.BOLT1 as BOLT1
import qualified Lightning.Protocol.BOLT4.Prim as P
import qualified Lightning.Protocol.BOLT9 as BOLT9

-- onion packets --------------------------------------------------------------

-- | An onion packet (@onion_packet@): 1366 bytes on the wire.
data OnionPacket = OnionPacket
  { OnionPacket -> Word8
onion_version      :: {-# UNPACK #-} !Word8
    -- ^ the version byte; 0 in this version of the protocol
  , OnionPacket -> Point
onion_public_key   :: !BOLT1.Point
    -- ^ the ephemeral public key
  , OnionPacket -> HopPayloads
onion_hop_payloads :: !HopPayloads
    -- ^ the obfuscated hop payloads
  , OnionPacket -> Hmac32
onion_hmac         :: !Hmac32
    -- ^ the HMAC authenticating the packet
  } deriving (OnionPacket -> OnionPacket -> Bool
(OnionPacket -> OnionPacket -> Bool)
-> (OnionPacket -> OnionPacket -> Bool) -> Eq OnionPacket
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: OnionPacket -> OnionPacket -> Bool
== :: OnionPacket -> OnionPacket -> Bool
$c/= :: OnionPacket -> OnionPacket -> Bool
/= :: OnionPacket -> OnionPacket -> Bool
Eq, Int -> OnionPacket -> ShowS
[OnionPacket] -> ShowS
OnionPacket -> String
(Int -> OnionPacket -> ShowS)
-> (OnionPacket -> String)
-> ([OnionPacket] -> ShowS)
-> Show OnionPacket
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> OnionPacket -> ShowS
showsPrec :: Int -> OnionPacket -> ShowS
$cshow :: OnionPacket -> String
show :: OnionPacket -> String
$cshowList :: [OnionPacket] -> ShowS
showList :: [OnionPacket] -> ShowS
Show, (forall x. OnionPacket -> Rep OnionPacket x)
-> (forall x. Rep OnionPacket x -> OnionPacket)
-> Generic OnionPacket
forall x. Rep OnionPacket x -> OnionPacket
forall x. OnionPacket -> Rep OnionPacket x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. OnionPacket -> Rep OnionPacket x
from :: forall x. OnionPacket -> Rep OnionPacket x
$cto :: forall x. Rep OnionPacket x -> OnionPacket
to :: forall x. Rep OnionPacket x -> OnionPacket
Generic)

instance NFData OnionPacket

-- | The 1300-byte @hop_payloads@ field of an onion packet.
newtype HopPayloads = HopPayloads BS.ByteString
  deriving (HopPayloads -> HopPayloads -> Bool
(HopPayloads -> HopPayloads -> Bool)
-> (HopPayloads -> HopPayloads -> Bool) -> Eq HopPayloads
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: HopPayloads -> HopPayloads -> Bool
== :: HopPayloads -> HopPayloads -> Bool
$c/= :: HopPayloads -> HopPayloads -> Bool
/= :: HopPayloads -> HopPayloads -> Bool
Eq, Int -> HopPayloads -> ShowS
[HopPayloads] -> ShowS
HopPayloads -> String
(Int -> HopPayloads -> ShowS)
-> (HopPayloads -> String)
-> ([HopPayloads] -> ShowS)
-> Show HopPayloads
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> HopPayloads -> ShowS
showsPrec :: Int -> HopPayloads -> ShowS
$cshow :: HopPayloads -> String
show :: HopPayloads -> String
$cshowList :: [HopPayloads] -> ShowS
showList :: [HopPayloads] -> ShowS
Show, (forall x. HopPayloads -> Rep HopPayloads x)
-> (forall x. Rep HopPayloads x -> HopPayloads)
-> Generic HopPayloads
forall x. Rep HopPayloads x -> HopPayloads
forall x. HopPayloads -> Rep HopPayloads x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. HopPayloads -> Rep HopPayloads x
from :: forall x. HopPayloads -> Rep HopPayloads x
$cto :: forall x. Rep HopPayloads x -> HopPayloads
to :: forall x. Rep HopPayloads x -> HopPayloads
Generic)

instance NFData HopPayloads

-- | Construct t'HopPayloads' from exactly 1300 bytes.
--
--   >>> let Just hp = hop_payloads (BS.replicate 1300 0)
--   >>> BS.length (un_hop_payloads hp)
--   1300
--   >>> hop_payloads (BS.replicate 1299 0)
--   Nothing
hop_payloads :: BS.ByteString -> Maybe HopPayloads
hop_payloads :: ByteString -> Maybe HopPayloads
hop_payloads ByteString
bs
  | ByteString -> Int
BS.length ByteString
bs Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1300 = HopPayloads -> Maybe HopPayloads
forall a. a -> Maybe a
Just (ByteString -> HopPayloads
HopPayloads ByteString
bs)
  | Bool
otherwise            = Maybe HopPayloads
forall a. Maybe a
Nothing
{-# INLINE hop_payloads #-}

-- | The bytes of t'HopPayloads'.
un_hop_payloads :: HopPayloads -> BS.ByteString
un_hop_payloads :: HopPayloads -> ByteString
un_hop_payloads (HopPayloads ByteString
bs) = ByteString
bs
{-# INLINE un_hop_payloads #-}

-- | A 32-byte HMAC.
newtype Hmac32 = Hmac32 BS.ByteString
  deriving (Hmac32 -> Hmac32 -> Bool
(Hmac32 -> Hmac32 -> Bool)
-> (Hmac32 -> Hmac32 -> Bool) -> Eq Hmac32
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Hmac32 -> Hmac32 -> Bool
== :: Hmac32 -> Hmac32 -> Bool
$c/= :: Hmac32 -> Hmac32 -> Bool
/= :: Hmac32 -> Hmac32 -> Bool
Eq, Int -> Hmac32 -> ShowS
[Hmac32] -> ShowS
Hmac32 -> String
(Int -> Hmac32 -> ShowS)
-> (Hmac32 -> String) -> ([Hmac32] -> ShowS) -> Show Hmac32
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Hmac32 -> ShowS
showsPrec :: Int -> Hmac32 -> ShowS
$cshow :: Hmac32 -> String
show :: Hmac32 -> String
$cshowList :: [Hmac32] -> ShowS
showList :: [Hmac32] -> ShowS
Show, (forall x. Hmac32 -> Rep Hmac32 x)
-> (forall x. Rep Hmac32 x -> Hmac32) -> Generic Hmac32
forall x. Rep Hmac32 x -> Hmac32
forall x. Hmac32 -> Rep Hmac32 x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Hmac32 -> Rep Hmac32 x
from :: forall x. Hmac32 -> Rep Hmac32 x
$cto :: forall x. Rep Hmac32 x -> Hmac32
to :: forall x. Rep Hmac32 x -> Hmac32
Generic)

instance NFData Hmac32

-- | Construct an t'Hmac32' from exactly 32 bytes.
--
--   >>> fmap (BS.length . un_hmac32) (hmac32 (BS.replicate 32 0))
--   Just 32
--   >>> hmac32 "too short"
--   Nothing
hmac32 :: BS.ByteString -> Maybe Hmac32
hmac32 :: ByteString -> Maybe Hmac32
hmac32 ByteString
bs
  | ByteString -> Int
BS.length ByteString
bs Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
32 = Hmac32 -> Maybe Hmac32
forall a. a -> Maybe a
Just (ByteString -> Hmac32
Hmac32 ByteString
bs)
  | Bool
otherwise          = Maybe Hmac32
forall a. Maybe a
Nothing
{-# INLINE hmac32 #-}

-- | The bytes of an t'Hmac32'.
un_hmac32 :: Hmac32 -> BS.ByteString
un_hmac32 :: Hmac32 -> ByteString
un_hmac32 (Hmac32 ByteString
bs) = ByteString
bs
{-# INLINE un_hmac32 #-}

-- hop payloads ---------------------------------------------------------------

-- | A hop's @payload@ TLV stream.
--
--   Each known record has a typed field. 'hp_extra' holds the remaining
--   records: decoding fills it with the unknown odd records (unknown even
--   records are rejected), and encoding accepts any record in it whose
--   type isn't one of the known ones.
data HopPayload = HopPayload
  { HopPayload -> Maybe MilliSatoshi
hp_amt_to_forward      :: !(Maybe BOLT1.MilliSatoshi)
    -- ^ type 2: @amt_to_forward@
  , HopPayload -> Maybe Word32
hp_outgoing_cltv_value :: !(Maybe Word32)
    -- ^ type 4: @outgoing_cltv_value@
  , HopPayload -> Maybe ShortChannelId
hp_short_channel_id    :: !(Maybe BOLT1.ShortChannelId)
    -- ^ type 6: @short_channel_id@
  , HopPayload -> Maybe PaymentData
hp_payment_data        :: !(Maybe PaymentData)
    -- ^ type 8: @payment_data@
  , HopPayload -> Maybe ByteString
hp_encrypted_data      :: !(Maybe BS.ByteString)
    -- ^ type 10: @encrypted_recipient_data@
  , HopPayload -> Maybe Point
hp_current_path_key    :: !(Maybe BOLT1.Point)
    -- ^ type 12: @current_path_key@
  , HopPayload -> Maybe ByteString
hp_payment_metadata    :: !(Maybe BS.ByteString)
    -- ^ type 16: @payment_metadata@
  , HopPayload -> Maybe MilliSatoshi
hp_total_amount_msat   :: !(Maybe BOLT1.MilliSatoshi)
    -- ^ type 18: @total_amount_msat@
  , HopPayload -> TlvStream
hp_extra               :: !BOLT1.TlvStream
    -- ^ other records
  } deriving (HopPayload -> HopPayload -> Bool
(HopPayload -> HopPayload -> Bool)
-> (HopPayload -> HopPayload -> Bool) -> Eq HopPayload
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: HopPayload -> HopPayload -> Bool
== :: HopPayload -> HopPayload -> Bool
$c/= :: HopPayload -> HopPayload -> Bool
/= :: HopPayload -> HopPayload -> Bool
Eq, Int -> HopPayload -> ShowS
[HopPayload] -> ShowS
HopPayload -> String
(Int -> HopPayload -> ShowS)
-> (HopPayload -> String)
-> ([HopPayload] -> ShowS)
-> Show HopPayload
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> HopPayload -> ShowS
showsPrec :: Int -> HopPayload -> ShowS
$cshow :: HopPayload -> String
show :: HopPayload -> String
$cshowList :: [HopPayload] -> ShowS
showList :: [HopPayload] -> ShowS
Show, (forall x. HopPayload -> Rep HopPayload x)
-> (forall x. Rep HopPayload x -> HopPayload) -> Generic HopPayload
forall x. Rep HopPayload x -> HopPayload
forall x. HopPayload -> Rep HopPayload x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. HopPayload -> Rep HopPayload x
from :: forall x. HopPayload -> Rep HopPayload x
$cto :: forall x. Rep HopPayload x -> HopPayload
to :: forall x. Rep HopPayload x -> HopPayload
Generic)

instance NFData HopPayload

-- | The empty t'HopPayload', to be updated with record syntax.
--
--   >>> let Just amt = BOLT1.milli_satoshi 1000
--   >>> let hp = empty_hop_payload { hp_amt_to_forward = Just amt }
--   >>> encode_hop_payload hp
--   Right "\STX\STX\ETX\232"
empty_hop_payload :: HopPayload
empty_hop_payload :: HopPayload
empty_hop_payload = Maybe MilliSatoshi
-> Maybe Word32
-> Maybe ShortChannelId
-> Maybe PaymentData
-> Maybe ByteString
-> Maybe Point
-> Maybe ByteString
-> Maybe MilliSatoshi
-> TlvStream
-> HopPayload
HopPayload
  Maybe MilliSatoshi
forall a. Maybe a
Nothing Maybe Word32
forall a. Maybe a
Nothing Maybe ShortChannelId
forall a. Maybe a
Nothing Maybe PaymentData
forall a. Maybe a
Nothing Maybe ByteString
forall a. Maybe a
Nothing Maybe Point
forall a. Maybe a
Nothing Maybe ByteString
forall a. Maybe a
Nothing Maybe MilliSatoshi
forall a. Maybe a
Nothing
  TlvStream
BOLT1.empty_tlv_stream

-- | The @payment_data@ record of a final hop's payload.
data PaymentData = PaymentData
  { PaymentData -> PaymentSecret
pd_payment_secret :: !PaymentSecret
  , PaymentData -> MilliSatoshi
pd_total_msat     :: !BOLT1.MilliSatoshi
  } deriving (PaymentData -> PaymentData -> Bool
(PaymentData -> PaymentData -> Bool)
-> (PaymentData -> PaymentData -> Bool) -> Eq PaymentData
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PaymentData -> PaymentData -> Bool
== :: PaymentData -> PaymentData -> Bool
$c/= :: PaymentData -> PaymentData -> Bool
/= :: PaymentData -> PaymentData -> Bool
Eq, Int -> PaymentData -> ShowS
[PaymentData] -> ShowS
PaymentData -> String
(Int -> PaymentData -> ShowS)
-> (PaymentData -> String)
-> ([PaymentData] -> ShowS)
-> Show PaymentData
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PaymentData -> ShowS
showsPrec :: Int -> PaymentData -> ShowS
$cshow :: PaymentData -> String
show :: PaymentData -> String
$cshowList :: [PaymentData] -> ShowS
showList :: [PaymentData] -> ShowS
Show, (forall x. PaymentData -> Rep PaymentData x)
-> (forall x. Rep PaymentData x -> PaymentData)
-> Generic PaymentData
forall x. Rep PaymentData x -> PaymentData
forall x. PaymentData -> Rep PaymentData x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. PaymentData -> Rep PaymentData x
from :: forall x. PaymentData -> Rep PaymentData x
$cto :: forall x. Rep PaymentData x -> PaymentData
to :: forall x. Rep PaymentData x -> PaymentData
Generic)

instance NFData PaymentData

-- | A 32-byte payment secret.
--
--   Its 'Show' instance is redacted and its 'Eq' instance runs in
--   constant time.
newtype PaymentSecret = PaymentSecret BS.ByteString

instance Eq PaymentSecret where
  PaymentSecret ByteString
a == :: PaymentSecret -> PaymentSecret -> Bool
== PaymentSecret ByteString
b = ByteString -> ByteString -> Bool
P.ct_eq ByteString
a ByteString
b

instance Show PaymentSecret where
  showsPrec :: Int -> PaymentSecret -> ShowS
showsPrec Int
d PaymentSecret
_ = Bool -> ShowS -> ShowS
showParen (Int
d Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
10) (ShowS -> ShowS) -> ShowS -> ShowS
forall a b. (a -> b) -> a -> b
$
    String -> ShowS
showString String
"PaymentSecret <redacted>"

instance NFData PaymentSecret where
  rnf :: PaymentSecret -> ()
rnf (PaymentSecret ByteString
bs) = ByteString -> ()
forall a. NFData a => a -> ()
rnf ByteString
bs

-- | Construct a t'PaymentSecret' from exactly 32 bytes.
--
--   >>> payment_secret (BS.replicate 32 0x01)
--   Just (PaymentSecret <redacted>)
--   >>> payment_secret (BS.replicate 16 0x01)
--   Nothing
payment_secret :: BS.ByteString -> Maybe PaymentSecret
payment_secret :: ByteString -> Maybe PaymentSecret
payment_secret ByteString
bs
  | ByteString -> Int
BS.length ByteString
bs Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
32 = PaymentSecret -> Maybe PaymentSecret
forall a. a -> Maybe a
Just (ByteString -> PaymentSecret
PaymentSecret ByteString
bs)
  | Bool
otherwise          = Maybe PaymentSecret
forall a. Maybe a
Nothing
{-# INLINE payment_secret #-}

-- | The bytes of a t'PaymentSecret'.
un_payment_secret :: PaymentSecret -> BS.ByteString
un_payment_secret :: PaymentSecret -> ByteString
un_payment_secret (PaymentSecret ByteString
bs) = ByteString
bs
{-# INLINE un_payment_secret #-}

-- route blinding -------------------------------------------------------------

-- | A blinded route (@blinded_path@), as created by a recipient.
data BlindedPath = BlindedPath
  { BlindedPath -> Point
bp_first_node_id  :: !BOLT1.Point
    -- ^ the (unblinded) introduction node
  , BlindedPath -> Point
bp_first_path_key :: !BOLT1.Point
    -- ^ the path key for the introduction node
  , BlindedPath -> [BlindedHop]
bp_hops           :: ![BlindedHop]
    -- ^ the blinded hops, starting with the introduction node
  } deriving (BlindedPath -> BlindedPath -> Bool
(BlindedPath -> BlindedPath -> Bool)
-> (BlindedPath -> BlindedPath -> Bool) -> Eq BlindedPath
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: BlindedPath -> BlindedPath -> Bool
== :: BlindedPath -> BlindedPath -> Bool
$c/= :: BlindedPath -> BlindedPath -> Bool
/= :: BlindedPath -> BlindedPath -> Bool
Eq, Int -> BlindedPath -> ShowS
[BlindedPath] -> ShowS
BlindedPath -> String
(Int -> BlindedPath -> ShowS)
-> (BlindedPath -> String)
-> ([BlindedPath] -> ShowS)
-> Show BlindedPath
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> BlindedPath -> ShowS
showsPrec :: Int -> BlindedPath -> ShowS
$cshow :: BlindedPath -> String
show :: BlindedPath -> String
$cshowList :: [BlindedPath] -> ShowS
showList :: [BlindedPath] -> ShowS
Show, (forall x. BlindedPath -> Rep BlindedPath x)
-> (forall x. Rep BlindedPath x -> BlindedPath)
-> Generic BlindedPath
forall x. Rep BlindedPath x -> BlindedPath
forall x. BlindedPath -> Rep BlindedPath x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. BlindedPath -> Rep BlindedPath x
from :: forall x. BlindedPath -> Rep BlindedPath x
$cto :: forall x. Rep BlindedPath x -> BlindedPath
to :: forall x. Rep BlindedPath x -> BlindedPath
Generic)

instance NFData BlindedPath

-- | A hop of a blinded route (@blinded_path_hop@).
data BlindedHop = BlindedHop
  { BlindedHop -> Point
bh_blinded_node_id :: !BOLT1.Point
    -- ^ the hop's blinded node id
  , BlindedHop -> ByteString
bh_encrypted_data  :: !BS.ByteString
    -- ^ the hop's @encrypted_recipient_data@
  } deriving (BlindedHop -> BlindedHop -> Bool
(BlindedHop -> BlindedHop -> Bool)
-> (BlindedHop -> BlindedHop -> Bool) -> Eq BlindedHop
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: BlindedHop -> BlindedHop -> Bool
== :: BlindedHop -> BlindedHop -> Bool
$c/= :: BlindedHop -> BlindedHop -> Bool
/= :: BlindedHop -> BlindedHop -> Bool
Eq, Int -> BlindedHop -> ShowS
[BlindedHop] -> ShowS
BlindedHop -> String
(Int -> BlindedHop -> ShowS)
-> (BlindedHop -> String)
-> ([BlindedHop] -> ShowS)
-> Show BlindedHop
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> BlindedHop -> ShowS
showsPrec :: Int -> BlindedHop -> ShowS
$cshow :: BlindedHop -> String
show :: BlindedHop -> String
$cshowList :: [BlindedHop] -> ShowS
showList :: [BlindedHop] -> ShowS
Show, (forall x. BlindedHop -> Rep BlindedHop x)
-> (forall x. Rep BlindedHop x -> BlindedHop) -> Generic BlindedHop
forall x. Rep BlindedHop x -> BlindedHop
forall x. BlindedHop -> Rep BlindedHop x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. BlindedHop -> Rep BlindedHop x
from :: forall x. BlindedHop -> Rep BlindedHop x
$cto :: forall x. Rep BlindedHop x -> BlindedHop
to :: forall x. Rep BlindedHop x -> BlindedHop
Generic)

instance NFData BlindedHop

-- | The @encrypted_data_tlv@ stream carried, encrypted, in a hop's
--   @encrypted_recipient_data@.
--
--   'bhd_extra' plays the same role as 'hp_extra' in t'HopPayload'.
data BlindedHopData = BlindedHopData
  { BlindedHopData -> Maybe ByteString
bhd_padding                :: !(Maybe BS.ByteString)
    -- ^ type 1: @padding@
  , BlindedHopData -> Maybe ShortChannelId
bhd_short_channel_id       :: !(Maybe BOLT1.ShortChannelId)
    -- ^ type 2: @short_channel_id@
  , BlindedHopData -> Maybe Point
bhd_next_node_id           :: !(Maybe BOLT1.Point)
    -- ^ type 4: @next_node_id@
  , BlindedHopData -> Maybe ByteString
bhd_path_id                :: !(Maybe BS.ByteString)
    -- ^ type 6: @path_id@
  , BlindedHopData -> Maybe Point
bhd_next_path_key_override :: !(Maybe BOLT1.Point)
    -- ^ type 8: @next_path_key_override@
  , BlindedHopData -> Maybe PaymentRelay
bhd_payment_relay          :: !(Maybe PaymentRelay)
    -- ^ type 10: @payment_relay@
  , BlindedHopData -> Maybe PaymentConstraints
bhd_payment_constraints    :: !(Maybe PaymentConstraints)
    -- ^ type 12: @payment_constraints@
  , BlindedHopData -> Maybe FeatureVector
bhd_allowed_features       :: !(Maybe BOLT9.FeatureVector)
    -- ^ type 14: @allowed_features@
  , BlindedHopData -> TlvStream
bhd_extra                  :: !BOLT1.TlvStream
    -- ^ other records
  } deriving (BlindedHopData -> BlindedHopData -> Bool
(BlindedHopData -> BlindedHopData -> Bool)
-> (BlindedHopData -> BlindedHopData -> Bool) -> Eq BlindedHopData
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: BlindedHopData -> BlindedHopData -> Bool
== :: BlindedHopData -> BlindedHopData -> Bool
$c/= :: BlindedHopData -> BlindedHopData -> Bool
/= :: BlindedHopData -> BlindedHopData -> Bool
Eq, Int -> BlindedHopData -> ShowS
[BlindedHopData] -> ShowS
BlindedHopData -> String
(Int -> BlindedHopData -> ShowS)
-> (BlindedHopData -> String)
-> ([BlindedHopData] -> ShowS)
-> Show BlindedHopData
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> BlindedHopData -> ShowS
showsPrec :: Int -> BlindedHopData -> ShowS
$cshow :: BlindedHopData -> String
show :: BlindedHopData -> String
$cshowList :: [BlindedHopData] -> ShowS
showList :: [BlindedHopData] -> ShowS
Show, (forall x. BlindedHopData -> Rep BlindedHopData x)
-> (forall x. Rep BlindedHopData x -> BlindedHopData)
-> Generic BlindedHopData
forall x. Rep BlindedHopData x -> BlindedHopData
forall x. BlindedHopData -> Rep BlindedHopData x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. BlindedHopData -> Rep BlindedHopData x
from :: forall x. BlindedHopData -> Rep BlindedHopData x
$cto :: forall x. Rep BlindedHopData x -> BlindedHopData
to :: forall x. Rep BlindedHopData x -> BlindedHopData
Generic)

instance NFData BlindedHopData

-- | The empty t'BlindedHopData', to be updated with record syntax.
--
--   >>> let bhd = empty_blinded_hop_data { bhd_path_id = Just "path id" }
--   >>> encode_blinded_hop_data bhd
--   Right "\ACK\apath id"
empty_blinded_hop_data :: BlindedHopData
empty_blinded_hop_data :: BlindedHopData
empty_blinded_hop_data = Maybe ByteString
-> Maybe ShortChannelId
-> Maybe Point
-> Maybe ByteString
-> Maybe Point
-> Maybe PaymentRelay
-> Maybe PaymentConstraints
-> Maybe FeatureVector
-> TlvStream
-> BlindedHopData
BlindedHopData
  Maybe ByteString
forall a. Maybe a
Nothing Maybe ShortChannelId
forall a. Maybe a
Nothing Maybe Point
forall a. Maybe a
Nothing Maybe ByteString
forall a. Maybe a
Nothing Maybe Point
forall a. Maybe a
Nothing Maybe PaymentRelay
forall a. Maybe a
Nothing Maybe PaymentConstraints
forall a. Maybe a
Nothing Maybe FeatureVector
forall a. Maybe a
Nothing
  TlvStream
BOLT1.empty_tlv_stream

-- | The @payment_relay@ record of t'BlindedHopData'.
data PaymentRelay = PaymentRelay
  { PaymentRelay -> Word16
pr_cltv_expiry_delta           :: {-# UNPACK #-} !Word16
  , PaymentRelay -> Word32
pr_fee_proportional_millionths :: {-# UNPACK #-} !Word32
  , PaymentRelay -> Word32
pr_fee_base_msat               :: {-# UNPACK #-} !Word32
  } deriving (PaymentRelay -> PaymentRelay -> Bool
(PaymentRelay -> PaymentRelay -> Bool)
-> (PaymentRelay -> PaymentRelay -> Bool) -> Eq PaymentRelay
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PaymentRelay -> PaymentRelay -> Bool
== :: PaymentRelay -> PaymentRelay -> Bool
$c/= :: PaymentRelay -> PaymentRelay -> Bool
/= :: PaymentRelay -> PaymentRelay -> Bool
Eq, Int -> PaymentRelay -> ShowS
[PaymentRelay] -> ShowS
PaymentRelay -> String
(Int -> PaymentRelay -> ShowS)
-> (PaymentRelay -> String)
-> ([PaymentRelay] -> ShowS)
-> Show PaymentRelay
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PaymentRelay -> ShowS
showsPrec :: Int -> PaymentRelay -> ShowS
$cshow :: PaymentRelay -> String
show :: PaymentRelay -> String
$cshowList :: [PaymentRelay] -> ShowS
showList :: [PaymentRelay] -> ShowS
Show, (forall x. PaymentRelay -> Rep PaymentRelay x)
-> (forall x. Rep PaymentRelay x -> PaymentRelay)
-> Generic PaymentRelay
forall x. Rep PaymentRelay x -> PaymentRelay
forall x. PaymentRelay -> Rep PaymentRelay x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. PaymentRelay -> Rep PaymentRelay x
from :: forall x. PaymentRelay -> Rep PaymentRelay x
$cto :: forall x. Rep PaymentRelay x -> PaymentRelay
to :: forall x. Rep PaymentRelay x -> PaymentRelay
Generic)

instance NFData PaymentRelay

-- | The @payment_constraints@ record of t'BlindedHopData'.
data PaymentConstraints = PaymentConstraints
  { PaymentConstraints -> Word32
pc_max_cltv_expiry   :: {-# UNPACK #-} !Word32
  , PaymentConstraints -> MilliSatoshi
pc_htlc_minimum_msat :: !BOLT1.MilliSatoshi
  } deriving (PaymentConstraints -> PaymentConstraints -> Bool
(PaymentConstraints -> PaymentConstraints -> Bool)
-> (PaymentConstraints -> PaymentConstraints -> Bool)
-> Eq PaymentConstraints
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PaymentConstraints -> PaymentConstraints -> Bool
== :: PaymentConstraints -> PaymentConstraints -> Bool
$c/= :: PaymentConstraints -> PaymentConstraints -> Bool
/= :: PaymentConstraints -> PaymentConstraints -> Bool
Eq, Int -> PaymentConstraints -> ShowS
[PaymentConstraints] -> ShowS
PaymentConstraints -> String
(Int -> PaymentConstraints -> ShowS)
-> (PaymentConstraints -> String)
-> ([PaymentConstraints] -> ShowS)
-> Show PaymentConstraints
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PaymentConstraints -> ShowS
showsPrec :: Int -> PaymentConstraints -> ShowS
$cshow :: PaymentConstraints -> String
show :: PaymentConstraints -> String
$cshowList :: [PaymentConstraints] -> ShowS
showList :: [PaymentConstraints] -> ShowS
Show, (forall x. PaymentConstraints -> Rep PaymentConstraints x)
-> (forall x. Rep PaymentConstraints x -> PaymentConstraints)
-> Generic PaymentConstraints
forall x. Rep PaymentConstraints x -> PaymentConstraints
forall x. PaymentConstraints -> Rep PaymentConstraints x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. PaymentConstraints -> Rep PaymentConstraints x
from :: forall x. PaymentConstraints -> Rep PaymentConstraints x
$cto :: forall x. Rep PaymentConstraints x -> PaymentConstraints
to :: forall x. Rep PaymentConstraints x -> PaymentConstraints
Generic)

instance NFData PaymentConstraints

-- | What a node in a blinded route learns from its
--   @encrypted_recipient_data@.
data BlindedInfo = BlindedInfo
  { BlindedInfo -> BlindedHopData
bi_data          :: !BlindedHopData
    -- ^ the decrypted @encrypted_data_tlv@
  , BlindedInfo -> Point
bi_next_path_key :: !BOLT1.Point
    -- ^ the @path_key@ to send to the next node
  } deriving (BlindedInfo -> BlindedInfo -> Bool
(BlindedInfo -> BlindedInfo -> Bool)
-> (BlindedInfo -> BlindedInfo -> Bool) -> Eq BlindedInfo
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: BlindedInfo -> BlindedInfo -> Bool
== :: BlindedInfo -> BlindedInfo -> Bool
$c/= :: BlindedInfo -> BlindedInfo -> Bool
/= :: BlindedInfo -> BlindedInfo -> Bool
Eq, Int -> BlindedInfo -> ShowS
[BlindedInfo] -> ShowS
BlindedInfo -> String
(Int -> BlindedInfo -> ShowS)
-> (BlindedInfo -> String)
-> ([BlindedInfo] -> ShowS)
-> Show BlindedInfo
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> BlindedInfo -> ShowS
showsPrec :: Int -> BlindedInfo -> ShowS
$cshow :: BlindedInfo -> String
show :: BlindedInfo -> String
$cshowList :: [BlindedInfo] -> ShowS
showList :: [BlindedInfo] -> ShowS
Show, (forall x. BlindedInfo -> Rep BlindedInfo x)
-> (forall x. Rep BlindedInfo x -> BlindedInfo)
-> Generic BlindedInfo
forall x. Rep BlindedInfo x -> BlindedInfo
forall x. BlindedInfo -> Rep BlindedInfo x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. BlindedInfo -> Rep BlindedInfo x
from :: forall x. BlindedInfo -> Rep BlindedInfo x
$cto :: forall x. Rep BlindedInfo x -> BlindedInfo
to :: forall x. Rep BlindedInfo x -> BlindedInfo
Generic)

instance NFData BlindedInfo

-- failure messages -----------------------------------------------------------

-- | A failure message (@failuremsg@): a failure, followed by an optional
--   TLV stream.
data FailureMessage = FailureMessage
  { FailureMessage -> Failure
fm_failure :: !Failure
  , FailureMessage -> TlvStream
fm_tlvs    :: !BOLT1.TlvStream
  } deriving (FailureMessage -> FailureMessage -> Bool
(FailureMessage -> FailureMessage -> Bool)
-> (FailureMessage -> FailureMessage -> Bool) -> Eq FailureMessage
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: FailureMessage -> FailureMessage -> Bool
== :: FailureMessage -> FailureMessage -> Bool
$c/= :: FailureMessage -> FailureMessage -> Bool
/= :: FailureMessage -> FailureMessage -> Bool
Eq, Int -> FailureMessage -> ShowS
[FailureMessage] -> ShowS
FailureMessage -> String
(Int -> FailureMessage -> ShowS)
-> (FailureMessage -> String)
-> ([FailureMessage] -> ShowS)
-> Show FailureMessage
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> FailureMessage -> ShowS
showsPrec :: Int -> FailureMessage -> ShowS
$cshow :: FailureMessage -> String
show :: FailureMessage -> String
$cshowList :: [FailureMessage] -> ShowS
showList :: [FailureMessage] -> ShowS
Show, (forall x. FailureMessage -> Rep FailureMessage x)
-> (forall x. Rep FailureMessage x -> FailureMessage)
-> Generic FailureMessage
forall x. Rep FailureMessage x -> FailureMessage
forall x. FailureMessage -> Rep FailureMessage x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. FailureMessage -> Rep FailureMessage x
from :: forall x. FailureMessage -> Rep FailureMessage x
$cto :: forall x. Rep FailureMessage x -> FailureMessage
to :: forall x. Rep FailureMessage x -> FailureMessage
Generic)

instance NFData FailureMessage

-- | A failure: a failure code and its data.
--
--   A @channel_update@ field holds the raw bytes of the update; it is
--   empty when the update is omitted.
data Failure
  = TemporaryNodeFailure
    -- ^ NODE|2
  | PermanentNodeFailure
    -- ^ PERM|NODE|2
  | RequiredNodeFeatureMissing
    -- ^ PERM|NODE|3
  | InvalidOnionVersion !OnionHash
    -- ^ BADONION|PERM|4: @sha256_of_onion@
  | InvalidOnionHmac !OnionHash
    -- ^ BADONION|PERM|5: @sha256_of_onion@
  | InvalidOnionKey !OnionHash
    -- ^ BADONION|PERM|6: @sha256_of_onion@
  | TemporaryChannelFailure !BS.ByteString
    -- ^ UPDATE|7: @channel_update@
  | PermanentChannelFailure
    -- ^ PERM|8
  | RequiredChannelFeatureMissing
    -- ^ PERM|9
  | UnknownNextPeer
    -- ^ PERM|10
  | AmountBelowMinimum !BOLT1.MilliSatoshi !BS.ByteString
    -- ^ UPDATE|11: @htlc_msat@, @channel_update@
  | FeeInsufficient !BOLT1.MilliSatoshi !BS.ByteString
    -- ^ UPDATE|12: @htlc_msat@, @channel_update@
  | IncorrectCltvExpiry {-# UNPACK #-} !Word32 !BS.ByteString
    -- ^ UPDATE|13: @cltv_expiry@, @channel_update@
  | ExpiryTooSoon !BS.ByteString
    -- ^ UPDATE|14: @channel_update@
  | IncorrectOrUnknownPaymentDetails !BOLT1.MilliSatoshi
                                     {-# UNPACK #-} !Word32
    -- ^ PERM|15: @htlc_msat@, @height@
  | FinalIncorrectCltvExpiry {-# UNPACK #-} !Word32
    -- ^ 18: @cltv_expiry@
  | FinalIncorrectHtlcAmount !BOLT1.MilliSatoshi
    -- ^ 19: @incoming_htlc_amt@
  | ChannelDisabled {-# UNPACK #-} !Word16 !BS.ByteString
    -- ^ UPDATE|20: @disabled_flags@, @channel_update@
  | ExpiryTooFar
    -- ^ 21
  | InvalidOnionPayload !(Maybe (Word64, Word16))
    -- ^ PERM|22: the optional @type@ and @offset@
  | MppTimeout
    -- ^ 23
  | InvalidOnionBlinding !OnionHash
    -- ^ BADONION|PERM|24: @sha256_of_onion@
  | UnknownFailure {-# UNPACK #-} !Word16 !BS.ByteString
    -- ^ a failure code not defined above, and the bytes following it
  deriving (Failure -> Failure -> Bool
(Failure -> Failure -> Bool)
-> (Failure -> Failure -> Bool) -> Eq Failure
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Failure -> Failure -> Bool
== :: Failure -> Failure -> Bool
$c/= :: Failure -> Failure -> Bool
/= :: Failure -> Failure -> Bool
Eq, Int -> Failure -> ShowS
[Failure] -> ShowS
Failure -> String
(Int -> Failure -> ShowS)
-> (Failure -> String) -> ([Failure] -> ShowS) -> Show Failure
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Failure -> ShowS
showsPrec :: Int -> Failure -> ShowS
$cshow :: Failure -> String
show :: Failure -> String
$cshowList :: [Failure] -> ShowS
showList :: [Failure] -> ShowS
Show, (forall x. Failure -> Rep Failure x)
-> (forall x. Rep Failure x -> Failure) -> Generic Failure
forall x. Rep Failure x -> Failure
forall x. Failure -> Rep Failure x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Failure -> Rep Failure x
from :: forall x. Failure -> Rep Failure x
$cto :: forall x. Rep Failure x -> Failure
to :: forall x. Rep Failure x -> Failure
Generic)

instance NFData Failure

-- | A 32-byte @sha256_of_onion@.
newtype OnionHash = OnionHash BS.ByteString
  deriving (OnionHash -> OnionHash -> Bool
(OnionHash -> OnionHash -> Bool)
-> (OnionHash -> OnionHash -> Bool) -> Eq OnionHash
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: OnionHash -> OnionHash -> Bool
== :: OnionHash -> OnionHash -> Bool
$c/= :: OnionHash -> OnionHash -> Bool
/= :: OnionHash -> OnionHash -> Bool
Eq, Int -> OnionHash -> ShowS
[OnionHash] -> ShowS
OnionHash -> String
(Int -> OnionHash -> ShowS)
-> (OnionHash -> String)
-> ([OnionHash] -> ShowS)
-> Show OnionHash
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> OnionHash -> ShowS
showsPrec :: Int -> OnionHash -> ShowS
$cshow :: OnionHash -> String
show :: OnionHash -> String
$cshowList :: [OnionHash] -> ShowS
showList :: [OnionHash] -> ShowS
Show, (forall x. OnionHash -> Rep OnionHash x)
-> (forall x. Rep OnionHash x -> OnionHash) -> Generic OnionHash
forall x. Rep OnionHash x -> OnionHash
forall x. OnionHash -> Rep OnionHash x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. OnionHash -> Rep OnionHash x
from :: forall x. OnionHash -> Rep OnionHash x
$cto :: forall x. Rep OnionHash x -> OnionHash
to :: forall x. Rep OnionHash x -> OnionHash
Generic)

instance NFData OnionHash

-- | Construct an t'OnionHash' from exactly 32 bytes.
--
--   >>> fmap (BS.length . un_onion_hash) (onion_hash (BS.replicate 32 0))
--   Just 32
onion_hash :: BS.ByteString -> Maybe OnionHash
onion_hash :: ByteString -> Maybe OnionHash
onion_hash ByteString
bs
  | ByteString -> Int
BS.length ByteString
bs Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
32 = OnionHash -> Maybe OnionHash
forall a. a -> Maybe a
Just (ByteString -> OnionHash
OnionHash ByteString
bs)
  | Bool
otherwise          = Maybe OnionHash
forall a. Maybe a
Nothing
{-# INLINE onion_hash #-}

-- | The bytes of an t'OnionHash'.
un_onion_hash :: OnionHash -> BS.ByteString
un_onion_hash :: OnionHash -> ByteString
un_onion_hash (OnionHash ByteString
bs) = ByteString
bs
{-# INLINE un_onion_hash #-}

-- | The @failure_code@ of a 'Failure'.
--
--   >>> failure_code TemporaryNodeFailure
--   8194
--   >>> failure_code (UnknownFailure 0x1234 mempty)
--   4660
failure_code :: Failure -> Word16
failure_code :: Failure -> Word16
failure_code Failure
f = case Failure
f of
  Failure
TemporaryNodeFailure                 -> Word16
0x2002
  Failure
PermanentNodeFailure                 -> Word16
0x6002
  Failure
RequiredNodeFeatureMissing           -> Word16
0x6003
  InvalidOnionVersion OnionHash
_                -> Word16
0xc004
  InvalidOnionHmac OnionHash
_                   -> Word16
0xc005
  InvalidOnionKey OnionHash
_                    -> Word16
0xc006
  TemporaryChannelFailure ByteString
_            -> Word16
0x1007
  Failure
PermanentChannelFailure              -> Word16
0x4008
  Failure
RequiredChannelFeatureMissing        -> Word16
0x4009
  Failure
UnknownNextPeer                      -> Word16
0x400a
  AmountBelowMinimum MilliSatoshi
_ ByteString
_               -> Word16
0x100b
  FeeInsufficient MilliSatoshi
_ ByteString
_                  -> Word16
0x100c
  IncorrectCltvExpiry Word32
_ ByteString
_              -> Word16
0x100d
  ExpiryTooSoon ByteString
_                      -> Word16
0x100e
  IncorrectOrUnknownPaymentDetails MilliSatoshi
_ Word32
_ -> Word16
0x400f
  FinalIncorrectCltvExpiry Word32
_           -> Word16
0x0012
  FinalIncorrectHtlcAmount MilliSatoshi
_           -> Word16
0x0013
  ChannelDisabled Word16
_ ByteString
_                  -> Word16
0x1014
  Failure
ExpiryTooFar                         -> Word16
0x0015
  InvalidOnionPayload Maybe (Word64, Word16)
_                -> Word16
0x4016
  Failure
MppTimeout                           -> Word16
0x0017
  InvalidOnionBlinding OnionHash
_               -> Word16
0xc018
  UnknownFailure Word16
c ByteString
_                   -> Word16
c

-- | Is the BADONION flag (0x8000) set: was the onion unparsable?
--
--   >>> is_badonion TemporaryNodeFailure
--   False
is_badonion :: Failure -> Bool
is_badonion :: Failure -> Bool
is_badonion Failure
f = Failure -> Word16
failure_code Failure
f Word16 -> Word16 -> Word16
forall a. Bits a => a -> a -> a
.&. Word16
0x8000 Word16 -> Word16 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word16
0
{-# INLINE is_badonion #-}

-- | Is the PERM flag (0x4000) set: is the failure permanent?
--
--   >>> is_perm PermanentNodeFailure
--   True
is_perm :: Failure -> Bool
is_perm :: Failure -> Bool
is_perm Failure
f = Failure -> Word16
failure_code Failure
f Word16 -> Word16 -> Word16
forall a. Bits a => a -> a -> a
.&. Word16
0x4000 Word16 -> Word16 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word16
0
{-# INLINE is_perm #-}

-- | Is the NODE flag (0x2000) set: is it a node (not channel) failure?
--
--   >>> is_node TemporaryNodeFailure
--   True
is_node :: Failure -> Bool
is_node :: Failure -> Bool
is_node Failure
f = Failure -> Word16
failure_code Failure
f Word16 -> Word16 -> Word16
forall a. Bits a => a -> a -> a
.&. Word16
0x2000 Word16 -> Word16 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word16
0
{-# INLINE is_node #-}

-- | Is the UPDATE flag (0x1000) set: was a channel forwarding parameter
--   violated?
--
--   >>> is_update (ExpiryTooSoon mempty)
--   True
is_update :: Failure -> Bool
is_update :: Failure -> Bool
is_update Failure
f = Failure -> Word16
failure_code Failure
f Word16 -> Word16 -> Word16
forall a. Bits a => a -> a -> a
.&. Word16
0x1000 Word16 -> Word16 -> Bool
forall a. Eq a => a -> a -> Bool
/= Word16
0
{-# INLINE is_update #-}

-- errors ---------------------------------------------------------------------

-- | Why a decoder failed.
data DecodeError
  = InvalidLength
    -- ^ the input has the wrong length
  | InvalidPoint
    -- ^ a point lacks a compressed-encoding prefix
  | InvalidTlvStream !BOLT1.TlvError
    -- ^ the TLV stream is malformed, or has an unknown even type
  | InvalidTlvValue {-# UNPACK #-} !Word64
    -- ^ the value of the record of this type is malformed
  | InvalidFailureData {-# UNPACK #-} !Word16
    -- ^ the data of a failure with this code is malformed
  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

-- | Why an encoder failed.
data EncodeError
  = ConflictingTlv
    -- ^ an extra TLV record has the type of a known record
  | FieldTooLong
    -- ^ a length-prefixed field exceeds its maximum length
  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 an onion packet was rejected.
data ProcessError
  = InvalidVersion {-# UNPACK #-} !Word8
    -- ^ the version byte is not 0
  | InvalidPublicKey
    -- ^ the ephemeral public key is not a valid point
  | HmacMismatch
    -- ^ the packet's HMAC is wrong
  | InvalidPayloadLength
    -- ^ the payload length is malformed, below 2, or past the end of
    --   @hop_payloads@
  | InvalidPayload !DecodeError
    -- ^ the payload is not a valid @payload@ TLV stream
  | InvalidPathKey
    -- ^ the path key is not a valid point
  | UnexpectedPathKey
    -- ^ a path key was given with no @encrypted_recipient_data@, or
    --   both a @path_key@ and a @current_path_key@ were given
  | MissingPathKey
    -- ^ @encrypted_recipient_data@ was given with no path key
  | InvalidRecipientData
    -- ^ the @encrypted_recipient_data@ doesn't decrypt to a valid
    --   @encrypted_data_tlv@ stream
  deriving (ProcessError -> ProcessError -> Bool
(ProcessError -> ProcessError -> Bool)
-> (ProcessError -> ProcessError -> Bool) -> Eq ProcessError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ProcessError -> ProcessError -> Bool
== :: ProcessError -> ProcessError -> Bool
$c/= :: ProcessError -> ProcessError -> Bool
/= :: ProcessError -> ProcessError -> Bool
Eq, Int -> ProcessError -> ShowS
[ProcessError] -> ShowS
ProcessError -> String
(Int -> ProcessError -> ShowS)
-> (ProcessError -> String)
-> ([ProcessError] -> ShowS)
-> Show ProcessError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ProcessError -> ShowS
showsPrec :: Int -> ProcessError -> ShowS
$cshow :: ProcessError -> String
show :: ProcessError -> String
$cshowList :: [ProcessError] -> ShowS
showList :: [ProcessError] -> ShowS
Show, (forall x. ProcessError -> Rep ProcessError x)
-> (forall x. Rep ProcessError x -> ProcessError)
-> Generic ProcessError
forall x. Rep ProcessError x -> ProcessError
forall x. ProcessError -> Rep ProcessError x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ProcessError -> Rep ProcessError x
from :: forall x. ProcessError -> Rep ProcessError x
$cto :: forall x. Rep ProcessError x -> ProcessError
to :: forall x. Rep ProcessError x -> ProcessError
Generic)

instance NFData ProcessError