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

-- |
-- Module: Lightning.Protocol.BOLT2.Types
-- Copyright: (c) 2025 Jared Tobin
-- License: MIT
-- Maintainer: Jared Tobin <jared@ppad.tech>
--
-- Field types specific to BOLT #2.

module Lightning.Protocol.BOLT2.Types (
    HtlcId(..)
  , SerialId(..)
  , ScriptPubKey
  , script_pubkey
  , un_script_pubkey
  , Witness
  , witness
  , un_witness
  , OnionRoutingPacket
  , onion_routing_packet
  , un_onion_routing_packet
  , OnionHash
  , onion_hash
  , un_onion_hash
  , AttributionData
  , attribution_data
  , un_attribution_data
  , FeeRange(..)
  , NextFunding(..)
  ) where

import Bitcoin.Prim.Tx (TxId)
import Control.DeepSeq (NFData)
import qualified Data.ByteString as BS
import Data.Word (Word8, Word64)
import GHC.Generics (Generic)
import Lightning.Protocol.BOLT1 (Satoshi)

-- | An HTLC identifier (the @id@ field of the HTLC messages). The
--   offerer numbers its HTLCs from 0, incrementing by 1.
newtype HtlcId = HtlcId Word64
  deriving (HtlcId -> HtlcId -> Bool
(HtlcId -> HtlcId -> Bool)
-> (HtlcId -> HtlcId -> Bool) -> Eq HtlcId
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: HtlcId -> HtlcId -> Bool
== :: HtlcId -> HtlcId -> Bool
$c/= :: HtlcId -> HtlcId -> Bool
/= :: HtlcId -> HtlcId -> Bool
Eq, Eq HtlcId
Eq HtlcId =>
(HtlcId -> HtlcId -> Ordering)
-> (HtlcId -> HtlcId -> Bool)
-> (HtlcId -> HtlcId -> Bool)
-> (HtlcId -> HtlcId -> Bool)
-> (HtlcId -> HtlcId -> Bool)
-> (HtlcId -> HtlcId -> HtlcId)
-> (HtlcId -> HtlcId -> HtlcId)
-> Ord HtlcId
HtlcId -> HtlcId -> Bool
HtlcId -> HtlcId -> Ordering
HtlcId -> HtlcId -> HtlcId
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: HtlcId -> HtlcId -> Ordering
compare :: HtlcId -> HtlcId -> Ordering
$c< :: HtlcId -> HtlcId -> Bool
< :: HtlcId -> HtlcId -> Bool
$c<= :: HtlcId -> HtlcId -> Bool
<= :: HtlcId -> HtlcId -> Bool
$c> :: HtlcId -> HtlcId -> Bool
> :: HtlcId -> HtlcId -> Bool
$c>= :: HtlcId -> HtlcId -> Bool
>= :: HtlcId -> HtlcId -> Bool
$cmax :: HtlcId -> HtlcId -> HtlcId
max :: HtlcId -> HtlcId -> HtlcId
$cmin :: HtlcId -> HtlcId -> HtlcId
min :: HtlcId -> HtlcId -> HtlcId
Ord, Int -> HtlcId -> ShowS
[HtlcId] -> ShowS
HtlcId -> String
(Int -> HtlcId -> ShowS)
-> (HtlcId -> String) -> ([HtlcId] -> ShowS) -> Show HtlcId
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> HtlcId -> ShowS
showsPrec :: Int -> HtlcId -> ShowS
$cshow :: HtlcId -> String
show :: HtlcId -> String
$cshowList :: [HtlcId] -> ShowS
showList :: [HtlcId] -> ShowS
Show, (forall x. HtlcId -> Rep HtlcId x)
-> (forall x. Rep HtlcId x -> HtlcId) -> Generic HtlcId
forall x. Rep HtlcId x -> HtlcId
forall x. HtlcId -> Rep HtlcId x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. HtlcId -> Rep HtlcId x
from :: forall x. HtlcId -> Rep HtlcId x
$cto :: forall x. Rep HtlcId x -> HtlcId
to :: forall x. Rep HtlcId x -> HtlcId
Generic)

instance NFData HtlcId

-- | The @serial_id@ of an input or output in interactive transaction
--   construction. The initiator sends even values, the non-initiator
--   odd ones.
newtype SerialId = SerialId Word64
  deriving (SerialId -> SerialId -> Bool
(SerialId -> SerialId -> Bool)
-> (SerialId -> SerialId -> Bool) -> Eq SerialId
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SerialId -> SerialId -> Bool
== :: SerialId -> SerialId -> Bool
$c/= :: SerialId -> SerialId -> Bool
/= :: SerialId -> SerialId -> Bool
Eq, Eq SerialId
Eq SerialId =>
(SerialId -> SerialId -> Ordering)
-> (SerialId -> SerialId -> Bool)
-> (SerialId -> SerialId -> Bool)
-> (SerialId -> SerialId -> Bool)
-> (SerialId -> SerialId -> Bool)
-> (SerialId -> SerialId -> SerialId)
-> (SerialId -> SerialId -> SerialId)
-> Ord SerialId
SerialId -> SerialId -> Bool
SerialId -> SerialId -> Ordering
SerialId -> SerialId -> SerialId
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: SerialId -> SerialId -> Ordering
compare :: SerialId -> SerialId -> Ordering
$c< :: SerialId -> SerialId -> Bool
< :: SerialId -> SerialId -> Bool
$c<= :: SerialId -> SerialId -> Bool
<= :: SerialId -> SerialId -> Bool
$c> :: SerialId -> SerialId -> Bool
> :: SerialId -> SerialId -> Bool
$c>= :: SerialId -> SerialId -> Bool
>= :: SerialId -> SerialId -> Bool
$cmax :: SerialId -> SerialId -> SerialId
max :: SerialId -> SerialId -> SerialId
$cmin :: SerialId -> SerialId -> SerialId
min :: SerialId -> SerialId -> SerialId
Ord, Int -> SerialId -> ShowS
[SerialId] -> ShowS
SerialId -> String
(Int -> SerialId -> ShowS)
-> (SerialId -> String) -> ([SerialId] -> ShowS) -> Show SerialId
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SerialId -> ShowS
showsPrec :: Int -> SerialId -> ShowS
$cshow :: SerialId -> String
show :: SerialId -> String
$cshowList :: [SerialId] -> ShowS
showList :: [SerialId] -> ShowS
Show, (forall x. SerialId -> Rep SerialId x)
-> (forall x. Rep SerialId x -> SerialId) -> Generic SerialId
forall x. Rep SerialId x -> SerialId
forall x. SerialId -> Rep SerialId x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. SerialId -> Rep SerialId x
from :: forall x. SerialId -> Rep SerialId x
$cto :: forall x. Rep SerialId x -> SerialId
to :: forall x. Rep SerialId x -> SerialId
Generic)

instance NFData SerialId

-- | A scriptPubKey, at most 65535 bytes (the most a u16 length prefix
--   can describe).
newtype ScriptPubKey = ScriptPubKey BS.ByteString
  deriving (ScriptPubKey -> ScriptPubKey -> Bool
(ScriptPubKey -> ScriptPubKey -> Bool)
-> (ScriptPubKey -> ScriptPubKey -> Bool) -> Eq ScriptPubKey
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ScriptPubKey -> ScriptPubKey -> Bool
== :: ScriptPubKey -> ScriptPubKey -> Bool
$c/= :: ScriptPubKey -> ScriptPubKey -> Bool
/= :: ScriptPubKey -> ScriptPubKey -> Bool
Eq, Eq ScriptPubKey
Eq ScriptPubKey =>
(ScriptPubKey -> ScriptPubKey -> Ordering)
-> (ScriptPubKey -> ScriptPubKey -> Bool)
-> (ScriptPubKey -> ScriptPubKey -> Bool)
-> (ScriptPubKey -> ScriptPubKey -> Bool)
-> (ScriptPubKey -> ScriptPubKey -> Bool)
-> (ScriptPubKey -> ScriptPubKey -> ScriptPubKey)
-> (ScriptPubKey -> ScriptPubKey -> ScriptPubKey)
-> Ord ScriptPubKey
ScriptPubKey -> ScriptPubKey -> Bool
ScriptPubKey -> ScriptPubKey -> Ordering
ScriptPubKey -> ScriptPubKey -> ScriptPubKey
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: ScriptPubKey -> ScriptPubKey -> Ordering
compare :: ScriptPubKey -> ScriptPubKey -> Ordering
$c< :: ScriptPubKey -> ScriptPubKey -> Bool
< :: ScriptPubKey -> ScriptPubKey -> Bool
$c<= :: ScriptPubKey -> ScriptPubKey -> Bool
<= :: ScriptPubKey -> ScriptPubKey -> Bool
$c> :: ScriptPubKey -> ScriptPubKey -> Bool
> :: ScriptPubKey -> ScriptPubKey -> Bool
$c>= :: ScriptPubKey -> ScriptPubKey -> Bool
>= :: ScriptPubKey -> ScriptPubKey -> Bool
$cmax :: ScriptPubKey -> ScriptPubKey -> ScriptPubKey
max :: ScriptPubKey -> ScriptPubKey -> ScriptPubKey
$cmin :: ScriptPubKey -> ScriptPubKey -> ScriptPubKey
min :: ScriptPubKey -> ScriptPubKey -> ScriptPubKey
Ord, Int -> ScriptPubKey -> ShowS
[ScriptPubKey] -> ShowS
ScriptPubKey -> String
(Int -> ScriptPubKey -> ShowS)
-> (ScriptPubKey -> String)
-> ([ScriptPubKey] -> ShowS)
-> Show ScriptPubKey
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ScriptPubKey -> ShowS
showsPrec :: Int -> ScriptPubKey -> ShowS
$cshow :: ScriptPubKey -> String
show :: ScriptPubKey -> String
$cshowList :: [ScriptPubKey] -> ShowS
showList :: [ScriptPubKey] -> ShowS
Show, (forall x. ScriptPubKey -> Rep ScriptPubKey x)
-> (forall x. Rep ScriptPubKey x -> ScriptPubKey)
-> Generic ScriptPubKey
forall x. Rep ScriptPubKey x -> ScriptPubKey
forall x. ScriptPubKey -> Rep ScriptPubKey x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ScriptPubKey -> Rep ScriptPubKey x
from :: forall x. ScriptPubKey -> Rep ScriptPubKey x
$cto :: forall x. Rep ScriptPubKey x -> ScriptPubKey
to :: forall x. Rep ScriptPubKey x -> ScriptPubKey
Generic)

instance NFData ScriptPubKey

-- | Construct a t'ScriptPubKey', failing above 65535 bytes. The script
--   itself isn't checked; the forms acceptable in @shutdown@ depend on
--   negotiated features.
--
--   >>> script_pubkey "\x51\x02\x4e\x73"
--   Just (ScriptPubKey "Q\STXNs")
--   >>> script_pubkey (BS.replicate 65536 0x00)
--   Nothing
script_pubkey :: BS.ByteString -> Maybe ScriptPubKey
script_pubkey :: ByteString -> Maybe ScriptPubKey
script_pubkey ByteString
bs
  | ByteString -> Int
BS.length ByteString
bs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
65535 = ScriptPubKey -> Maybe ScriptPubKey
forall a. a -> Maybe a
Just (ByteString -> ScriptPubKey
ScriptPubKey ByteString
bs)
  | Bool
otherwise             = Maybe ScriptPubKey
forall a. Maybe a
Nothing
{-# INLINE script_pubkey #-}

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

-- | The serialized witness of one input in @tx_signatures@ (encoded as
--   in Bitcoin's wire protocol), at most 65535 bytes.
newtype Witness = Witness BS.ByteString
  deriving (Witness -> Witness -> Bool
(Witness -> Witness -> Bool)
-> (Witness -> Witness -> Bool) -> Eq Witness
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Witness -> Witness -> Bool
== :: Witness -> Witness -> Bool
$c/= :: Witness -> Witness -> Bool
/= :: Witness -> Witness -> Bool
Eq, Eq Witness
Eq Witness =>
(Witness -> Witness -> Ordering)
-> (Witness -> Witness -> Bool)
-> (Witness -> Witness -> Bool)
-> (Witness -> Witness -> Bool)
-> (Witness -> Witness -> Bool)
-> (Witness -> Witness -> Witness)
-> (Witness -> Witness -> Witness)
-> Ord Witness
Witness -> Witness -> Bool
Witness -> Witness -> Ordering
Witness -> Witness -> Witness
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Witness -> Witness -> Ordering
compare :: Witness -> Witness -> Ordering
$c< :: Witness -> Witness -> Bool
< :: Witness -> Witness -> Bool
$c<= :: Witness -> Witness -> Bool
<= :: Witness -> Witness -> Bool
$c> :: Witness -> Witness -> Bool
> :: Witness -> Witness -> Bool
$c>= :: Witness -> Witness -> Bool
>= :: Witness -> Witness -> Bool
$cmax :: Witness -> Witness -> Witness
max :: Witness -> Witness -> Witness
$cmin :: Witness -> Witness -> Witness
min :: Witness -> Witness -> Witness
Ord, Int -> Witness -> ShowS
[Witness] -> ShowS
Witness -> String
(Int -> Witness -> ShowS)
-> (Witness -> String) -> ([Witness] -> ShowS) -> Show Witness
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Witness -> ShowS
showsPrec :: Int -> Witness -> ShowS
$cshow :: Witness -> String
show :: Witness -> String
$cshowList :: [Witness] -> ShowS
showList :: [Witness] -> ShowS
Show, (forall x. Witness -> Rep Witness x)
-> (forall x. Rep Witness x -> Witness) -> Generic Witness
forall x. Rep Witness x -> Witness
forall x. Witness -> Rep Witness x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Witness -> Rep Witness x
from :: forall x. Witness -> Rep Witness x
$cto :: forall x. Rep Witness x -> Witness
to :: forall x. Rep Witness x -> Witness
Generic)

instance NFData Witness

-- | Construct a t'Witness', failing above 65535 bytes. The contents
--   aren't checked.
--
--   >>> witness "\x01\x01\x2a"
--   Just (Witness "\SOH\SOH*")
witness :: BS.ByteString -> Maybe Witness
witness :: ByteString -> Maybe Witness
witness ByteString
bs
  | ByteString -> Int
BS.length ByteString
bs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
65535 = Witness -> Maybe Witness
forall a. a -> Maybe a
Just (ByteString -> Witness
Witness ByteString
bs)
  | Bool
otherwise             = Maybe Witness
forall a. Maybe a
Nothing
{-# INLINE witness #-}

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

-- | The 1366-byte @onion_routing_packet@ of @update_add_htlc@, kept
--   opaque (BOLT #4 defines its structure).
newtype OnionRoutingPacket = OnionRoutingPacket BS.ByteString
  deriving (OnionRoutingPacket -> OnionRoutingPacket -> Bool
(OnionRoutingPacket -> OnionRoutingPacket -> Bool)
-> (OnionRoutingPacket -> OnionRoutingPacket -> Bool)
-> Eq OnionRoutingPacket
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: OnionRoutingPacket -> OnionRoutingPacket -> Bool
== :: OnionRoutingPacket -> OnionRoutingPacket -> Bool
$c/= :: OnionRoutingPacket -> OnionRoutingPacket -> Bool
/= :: OnionRoutingPacket -> OnionRoutingPacket -> Bool
Eq, Eq OnionRoutingPacket
Eq OnionRoutingPacket =>
(OnionRoutingPacket -> OnionRoutingPacket -> Ordering)
-> (OnionRoutingPacket -> OnionRoutingPacket -> Bool)
-> (OnionRoutingPacket -> OnionRoutingPacket -> Bool)
-> (OnionRoutingPacket -> OnionRoutingPacket -> Bool)
-> (OnionRoutingPacket -> OnionRoutingPacket -> Bool)
-> (OnionRoutingPacket -> OnionRoutingPacket -> OnionRoutingPacket)
-> (OnionRoutingPacket -> OnionRoutingPacket -> OnionRoutingPacket)
-> Ord OnionRoutingPacket
OnionRoutingPacket -> OnionRoutingPacket -> Bool
OnionRoutingPacket -> OnionRoutingPacket -> Ordering
OnionRoutingPacket -> OnionRoutingPacket -> OnionRoutingPacket
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: OnionRoutingPacket -> OnionRoutingPacket -> Ordering
compare :: OnionRoutingPacket -> OnionRoutingPacket -> Ordering
$c< :: OnionRoutingPacket -> OnionRoutingPacket -> Bool
< :: OnionRoutingPacket -> OnionRoutingPacket -> Bool
$c<= :: OnionRoutingPacket -> OnionRoutingPacket -> Bool
<= :: OnionRoutingPacket -> OnionRoutingPacket -> Bool
$c> :: OnionRoutingPacket -> OnionRoutingPacket -> Bool
> :: OnionRoutingPacket -> OnionRoutingPacket -> Bool
$c>= :: OnionRoutingPacket -> OnionRoutingPacket -> Bool
>= :: OnionRoutingPacket -> OnionRoutingPacket -> Bool
$cmax :: OnionRoutingPacket -> OnionRoutingPacket -> OnionRoutingPacket
max :: OnionRoutingPacket -> OnionRoutingPacket -> OnionRoutingPacket
$cmin :: OnionRoutingPacket -> OnionRoutingPacket -> OnionRoutingPacket
min :: OnionRoutingPacket -> OnionRoutingPacket -> OnionRoutingPacket
Ord, Int -> OnionRoutingPacket -> ShowS
[OnionRoutingPacket] -> ShowS
OnionRoutingPacket -> String
(Int -> OnionRoutingPacket -> ShowS)
-> (OnionRoutingPacket -> String)
-> ([OnionRoutingPacket] -> ShowS)
-> Show OnionRoutingPacket
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> OnionRoutingPacket -> ShowS
showsPrec :: Int -> OnionRoutingPacket -> ShowS
$cshow :: OnionRoutingPacket -> String
show :: OnionRoutingPacket -> String
$cshowList :: [OnionRoutingPacket] -> ShowS
showList :: [OnionRoutingPacket] -> ShowS
Show, (forall x. OnionRoutingPacket -> Rep OnionRoutingPacket x)
-> (forall x. Rep OnionRoutingPacket x -> OnionRoutingPacket)
-> Generic OnionRoutingPacket
forall x. Rep OnionRoutingPacket x -> OnionRoutingPacket
forall x. OnionRoutingPacket -> Rep OnionRoutingPacket x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. OnionRoutingPacket -> Rep OnionRoutingPacket x
from :: forall x. OnionRoutingPacket -> Rep OnionRoutingPacket x
$cto :: forall x. Rep OnionRoutingPacket x -> OnionRoutingPacket
to :: forall x. Rep OnionRoutingPacket x -> OnionRoutingPacket
Generic)

instance NFData OnionRoutingPacket

-- | Construct an t'OnionRoutingPacket' from exactly 1366 bytes.
--
--   >>> let bs = BS.replicate 1366 0x00
--   >>> fmap un_onion_routing_packet (onion_routing_packet bs) == Just bs
--   True
--   >>> onion_routing_packet (BS.take 1365 bs)
--   Nothing
onion_routing_packet :: BS.ByteString -> Maybe OnionRoutingPacket
onion_routing_packet :: ByteString -> Maybe OnionRoutingPacket
onion_routing_packet ByteString
bs
  | ByteString -> Int
BS.length ByteString
bs Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
1366 = OnionRoutingPacket -> Maybe OnionRoutingPacket
forall a. a -> Maybe a
Just (ByteString -> OnionRoutingPacket
OnionRoutingPacket ByteString
bs)
  | Bool
otherwise            = Maybe OnionRoutingPacket
forall a. Maybe a
Nothing
{-# INLINE onion_routing_packet #-}

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

-- | The SHA256 hash of an onion routing packet (the @sha256_of_onion@
--   field of @update_fail_malformed_htlc@).
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, Eq OnionHash
Eq OnionHash =>
(OnionHash -> OnionHash -> Ordering)
-> (OnionHash -> OnionHash -> Bool)
-> (OnionHash -> OnionHash -> Bool)
-> (OnionHash -> OnionHash -> Bool)
-> (OnionHash -> OnionHash -> Bool)
-> (OnionHash -> OnionHash -> OnionHash)
-> (OnionHash -> OnionHash -> OnionHash)
-> Ord OnionHash
OnionHash -> OnionHash -> Bool
OnionHash -> OnionHash -> Ordering
OnionHash -> OnionHash -> OnionHash
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: OnionHash -> OnionHash -> Ordering
compare :: OnionHash -> OnionHash -> Ordering
$c< :: OnionHash -> OnionHash -> Bool
< :: OnionHash -> OnionHash -> Bool
$c<= :: OnionHash -> OnionHash -> Bool
<= :: OnionHash -> OnionHash -> Bool
$c> :: OnionHash -> OnionHash -> Bool
> :: OnionHash -> OnionHash -> Bool
$c>= :: OnionHash -> OnionHash -> Bool
>= :: OnionHash -> OnionHash -> Bool
$cmax :: OnionHash -> OnionHash -> OnionHash
max :: OnionHash -> OnionHash -> OnionHash
$cmin :: OnionHash -> OnionHash -> OnionHash
min :: OnionHash -> OnionHash -> OnionHash
Ord, 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.
--
--   >>> onion_hash (BS.replicate 31 0x00)
--   Nothing
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 920-byte @attribution_data@ of @update_fulfill_htlc@ and
--   @update_fail_htlc@: twenty u32 HTLC hold times followed by 210
--   four-byte truncated HMACs. Kept opaque (BOLT #4 defines its use).
newtype AttributionData = AttributionData BS.ByteString
  deriving (AttributionData -> AttributionData -> Bool
(AttributionData -> AttributionData -> Bool)
-> (AttributionData -> AttributionData -> Bool)
-> Eq AttributionData
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: AttributionData -> AttributionData -> Bool
== :: AttributionData -> AttributionData -> Bool
$c/= :: AttributionData -> AttributionData -> Bool
/= :: AttributionData -> AttributionData -> Bool
Eq, Eq AttributionData
Eq AttributionData =>
(AttributionData -> AttributionData -> Ordering)
-> (AttributionData -> AttributionData -> Bool)
-> (AttributionData -> AttributionData -> Bool)
-> (AttributionData -> AttributionData -> Bool)
-> (AttributionData -> AttributionData -> Bool)
-> (AttributionData -> AttributionData -> AttributionData)
-> (AttributionData -> AttributionData -> AttributionData)
-> Ord AttributionData
AttributionData -> AttributionData -> Bool
AttributionData -> AttributionData -> Ordering
AttributionData -> AttributionData -> AttributionData
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: AttributionData -> AttributionData -> Ordering
compare :: AttributionData -> AttributionData -> Ordering
$c< :: AttributionData -> AttributionData -> Bool
< :: AttributionData -> AttributionData -> Bool
$c<= :: AttributionData -> AttributionData -> Bool
<= :: AttributionData -> AttributionData -> Bool
$c> :: AttributionData -> AttributionData -> Bool
> :: AttributionData -> AttributionData -> Bool
$c>= :: AttributionData -> AttributionData -> Bool
>= :: AttributionData -> AttributionData -> Bool
$cmax :: AttributionData -> AttributionData -> AttributionData
max :: AttributionData -> AttributionData -> AttributionData
$cmin :: AttributionData -> AttributionData -> AttributionData
min :: AttributionData -> AttributionData -> AttributionData
Ord, Int -> AttributionData -> ShowS
[AttributionData] -> ShowS
AttributionData -> String
(Int -> AttributionData -> ShowS)
-> (AttributionData -> String)
-> ([AttributionData] -> ShowS)
-> Show AttributionData
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> AttributionData -> ShowS
showsPrec :: Int -> AttributionData -> ShowS
$cshow :: AttributionData -> String
show :: AttributionData -> String
$cshowList :: [AttributionData] -> ShowS
showList :: [AttributionData] -> ShowS
Show, (forall x. AttributionData -> Rep AttributionData x)
-> (forall x. Rep AttributionData x -> AttributionData)
-> Generic AttributionData
forall x. Rep AttributionData x -> AttributionData
forall x. AttributionData -> Rep AttributionData x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. AttributionData -> Rep AttributionData x
from :: forall x. AttributionData -> Rep AttributionData x
$cto :: forall x. Rep AttributionData x -> AttributionData
to :: forall x. Rep AttributionData x -> AttributionData
Generic)

instance NFData AttributionData

-- | Construct an t'AttributionData' from exactly 920 bytes.
--
--   >>> let bs = BS.replicate 920 0x00
--   >>> fmap un_attribution_data (attribution_data bs) == Just bs
--   True
--   >>> attribution_data (BS.drop 1 bs)
--   Nothing
attribution_data :: BS.ByteString -> Maybe AttributionData
attribution_data :: ByteString -> Maybe AttributionData
attribution_data ByteString
bs
  | ByteString -> Int
BS.length ByteString
bs Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
920 = AttributionData -> Maybe AttributionData
forall a. a -> Maybe a
Just (ByteString -> AttributionData
AttributionData ByteString
bs)
  | Bool
otherwise           = Maybe AttributionData
forall a. Maybe a
Nothing
{-# INLINE attribution_data #-}

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

-- | The @fee_range@ of @closing_signed@: the fees the sender is
--   prepared to pay for the closing transaction.
data FeeRange = FeeRange
  { FeeRange -> Satoshi
fee_range_min_fee_satoshis :: {-# UNPACK #-} !Satoshi
  , FeeRange -> Satoshi
fee_range_max_fee_satoshis :: {-# UNPACK #-} !Satoshi
  } deriving (FeeRange -> FeeRange -> Bool
(FeeRange -> FeeRange -> Bool)
-> (FeeRange -> FeeRange -> Bool) -> Eq FeeRange
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: FeeRange -> FeeRange -> Bool
== :: FeeRange -> FeeRange -> Bool
$c/= :: FeeRange -> FeeRange -> Bool
/= :: FeeRange -> FeeRange -> Bool
Eq, Int -> FeeRange -> ShowS
[FeeRange] -> ShowS
FeeRange -> String
(Int -> FeeRange -> ShowS)
-> (FeeRange -> String) -> ([FeeRange] -> ShowS) -> Show FeeRange
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> FeeRange -> ShowS
showsPrec :: Int -> FeeRange -> ShowS
$cshow :: FeeRange -> String
show :: FeeRange -> String
$cshowList :: [FeeRange] -> ShowS
showList :: [FeeRange] -> ShowS
Show, (forall x. FeeRange -> Rep FeeRange x)
-> (forall x. Rep FeeRange x -> FeeRange) -> Generic FeeRange
forall x. Rep FeeRange x -> FeeRange
forall x. FeeRange -> Rep FeeRange x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. FeeRange -> Rep FeeRange x
from :: forall x. FeeRange -> Rep FeeRange x
$cto :: forall x. Rep FeeRange x -> FeeRange
to :: forall x. Rep FeeRange x -> FeeRange
Generic)

instance NFData FeeRange

-- | The @next_funding@ record of @channel_reestablish@: an interactive
--   funding transaction whose signatures are still being exchanged.
data NextFunding = NextFunding
  { NextFunding -> TxId
next_funding_txid             :: !TxId
  , NextFunding -> Word8
next_funding_retransmit_flags :: {-# UNPACK #-} !Word8
    -- ^ bit 0: the sender wants @commitment_signed@ retransmitted
  } deriving (NextFunding -> NextFunding -> Bool
(NextFunding -> NextFunding -> Bool)
-> (NextFunding -> NextFunding -> Bool) -> Eq NextFunding
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: NextFunding -> NextFunding -> Bool
== :: NextFunding -> NextFunding -> Bool
$c/= :: NextFunding -> NextFunding -> Bool
/= :: NextFunding -> NextFunding -> Bool
Eq, Int -> NextFunding -> ShowS
[NextFunding] -> ShowS
NextFunding -> String
(Int -> NextFunding -> ShowS)
-> (NextFunding -> String)
-> ([NextFunding] -> ShowS)
-> Show NextFunding
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> NextFunding -> ShowS
showsPrec :: Int -> NextFunding -> ShowS
$cshow :: NextFunding -> String
show :: NextFunding -> String
$cshowList :: [NextFunding] -> ShowS
showList :: [NextFunding] -> ShowS
Show, (forall x. NextFunding -> Rep NextFunding x)
-> (forall x. Rep NextFunding x -> NextFunding)
-> Generic NextFunding
forall x. Rep NextFunding x -> NextFunding
forall x. NextFunding -> Rep NextFunding x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. NextFunding -> Rep NextFunding x
from :: forall x. NextFunding -> Rep NextFunding x
$cto :: forall x. Rep NextFunding x -> NextFunding
to :: forall x. Rep NextFunding x -> NextFunding
Generic)

instance NFData NextFunding