{-# OPTIONS_HADDOCK prune #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}

-- |
-- Module: Lightning.Protocol.BOLT2.Types
-- Copyright: (c) 2025 Jared Tobin
-- License: MIT
-- Maintainer: Jared Tobin <jared@ppad.tech>
--
-- Core types for BOLT #2 peer protocol.
--
-- This module provides newtypes for identifiers, amounts, hashes, and
-- keys used in the Lightning Network peer protocol.

module Lightning.Protocol.BOLT2.Types (
  -- * Identifiers
    ChannelId
  , channelId
  , unChannelId

  -- * Amounts
  , Satoshis(..)
  , MilliSatoshis(..)
  , satoshisToMsat
  , msatToSatoshis

  -- * Cryptographic types
  , Signature
  , signature
  , unSignature
  , Point
  , point
  , unPoint
  , PaymentHash
  , paymentHash
  , unPaymentHash
  , PaymentPreimage
  , paymentPreimage
  , unPaymentPreimage
  , Secret
  , secret
  , unSecret

  -- * Transaction types
  , TxId
  , txId
  , unTxId
  , Outpoint(..)
  , ScriptPubKey
  , scriptPubKey
  , unScriptPubKey

  -- * Chain types
  , ChainHash
  , chainHash
  , unChainHash
  , ShortChannelId(..)
  , shortChannelId
  , scidBlockHeight
  , scidTxIndex
  , scidOutputIndex

  -- * Protocol types
  , FeatureBits
  , featureBits
  , unFeatureBits
  , OnionPacket
  , onionPacket
  , unOnionPacket

  -- * Constants
  , channelIdLen
  , signatureLen
  , pointLen
  , txIdLen
  , chainHashLen
  , shortChannelIdLen
  , paymentHashLen
  , paymentPreimageLen
  , onionPacketLen
  , secretLen
  ) where

import Control.DeepSeq (NFData)
import Data.Bits (unsafeShiftL, unsafeShiftR, (.&.), (.|.))
import qualified Data.ByteString as BS
import Data.Word (Word16, Word32, Word64)
import GHC.Generics (Generic)

-- constants -------------------------------------------------------------------

-- | Length of a channel_id in bytes (32).
channelIdLen :: Int
channelIdLen :: Int
channelIdLen = Int
32
{-# INLINE channelIdLen #-}

-- | Length of a signature in bytes (64, compact format).
signatureLen :: Int
signatureLen :: Int
signatureLen = Int
64
{-# INLINE signatureLen #-}

-- | Length of a compressed secp256k1 public key in bytes (33).
pointLen :: Int
pointLen :: Int
pointLen = Int
33
{-# INLINE pointLen #-}

-- | Length of a transaction ID in bytes (32).
txIdLen :: Int
txIdLen :: Int
txIdLen = Int
32
{-# INLINE txIdLen #-}

-- | Length of a chain hash in bytes (32).
chainHashLen :: Int
chainHashLen :: Int
chainHashLen = Int
32
{-# INLINE chainHashLen #-}

-- | Length of a short_channel_id in bytes (8).
shortChannelIdLen :: Int
shortChannelIdLen :: Int
shortChannelIdLen = Int
8
{-# INLINE shortChannelIdLen #-}

-- | Length of a payment hash in bytes (32).
paymentHashLen :: Int
paymentHashLen :: Int
paymentHashLen = Int
32
{-# INLINE paymentHashLen #-}

-- | Length of a payment preimage in bytes (32).
paymentPreimageLen :: Int
paymentPreimageLen :: Int
paymentPreimageLen = Int
32
{-# INLINE paymentPreimageLen #-}

-- | Length of an onion routing packet in bytes (1366).
onionPacketLen :: Int
onionPacketLen :: Int
onionPacketLen = Int
1366
{-# INLINE onionPacketLen #-}

-- | Length of a per-commitment secret in bytes (32).
secretLen :: Int
secretLen :: Int
secretLen = Int
32
{-# INLINE secretLen #-}

-- identifiers -----------------------------------------------------------------

-- | A 32-byte channel identifier.
--
-- Derived from the funding transaction by XORing @funding_txid@ with
-- @funding_output_index@ (big-endian, altering the last 2 bytes).
--
-- For v2 channels, derived as @SHA256(lesser-revocation-basepoint ||
-- greater-revocation-basepoint)@.
newtype ChannelId = ChannelId BS.ByteString
  deriving stock (ChannelId -> ChannelId -> Bool
(ChannelId -> ChannelId -> Bool)
-> (ChannelId -> ChannelId -> Bool) -> Eq ChannelId
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ChannelId -> ChannelId -> Bool
== :: ChannelId -> ChannelId -> Bool
$c/= :: ChannelId -> ChannelId -> Bool
/= :: ChannelId -> ChannelId -> Bool
Eq, Eq ChannelId
Eq ChannelId =>
(ChannelId -> ChannelId -> Ordering)
-> (ChannelId -> ChannelId -> Bool)
-> (ChannelId -> ChannelId -> Bool)
-> (ChannelId -> ChannelId -> Bool)
-> (ChannelId -> ChannelId -> Bool)
-> (ChannelId -> ChannelId -> ChannelId)
-> (ChannelId -> ChannelId -> ChannelId)
-> Ord ChannelId
ChannelId -> ChannelId -> Bool
ChannelId -> ChannelId -> Ordering
ChannelId -> ChannelId -> ChannelId
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 :: ChannelId -> ChannelId -> Ordering
compare :: ChannelId -> ChannelId -> Ordering
$c< :: ChannelId -> ChannelId -> Bool
< :: ChannelId -> ChannelId -> Bool
$c<= :: ChannelId -> ChannelId -> Bool
<= :: ChannelId -> ChannelId -> Bool
$c> :: ChannelId -> ChannelId -> Bool
> :: ChannelId -> ChannelId -> Bool
$c>= :: ChannelId -> ChannelId -> Bool
>= :: ChannelId -> ChannelId -> Bool
$cmax :: ChannelId -> ChannelId -> ChannelId
max :: ChannelId -> ChannelId -> ChannelId
$cmin :: ChannelId -> ChannelId -> ChannelId
min :: ChannelId -> ChannelId -> ChannelId
Ord, Int -> ChannelId -> ShowS
[ChannelId] -> ShowS
ChannelId -> String
(Int -> ChannelId -> ShowS)
-> (ChannelId -> String)
-> ([ChannelId] -> ShowS)
-> Show ChannelId
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ChannelId -> ShowS
showsPrec :: Int -> ChannelId -> ShowS
$cshow :: ChannelId -> String
show :: ChannelId -> String
$cshowList :: [ChannelId] -> ShowS
showList :: [ChannelId] -> ShowS
Show, (forall x. ChannelId -> Rep ChannelId x)
-> (forall x. Rep ChannelId x -> ChannelId) -> Generic ChannelId
forall x. Rep ChannelId x -> ChannelId
forall x. ChannelId -> Rep ChannelId x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ChannelId -> Rep ChannelId x
from :: forall x. ChannelId -> Rep ChannelId x
$cto :: forall x. Rep ChannelId x -> ChannelId
to :: forall x. Rep ChannelId x -> ChannelId
Generic)
  deriving newtype ChannelId -> ()
(ChannelId -> ()) -> NFData ChannelId
forall a. (a -> ()) -> NFData a
$crnf :: ChannelId -> ()
rnf :: ChannelId -> ()
NFData

-- | Construct a 'ChannelId' from a 32-byte 'BS.ByteString'.
--
-- Returns 'Nothing' if the input is not exactly 32 bytes.
--
-- >>> channelId (BS.replicate 32 0x00)
-- Just (ChannelId ...)
-- >>> channelId (BS.replicate 31 0x00)
-- Nothing
channelId :: BS.ByteString -> Maybe ChannelId
channelId :: ByteString -> Maybe ChannelId
channelId !ByteString
bs
  | ByteString -> Int
BS.length ByteString
bs Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
channelIdLen = ChannelId -> Maybe ChannelId
forall a. a -> Maybe a
Just (ChannelId -> Maybe ChannelId) -> ChannelId -> Maybe ChannelId
forall a b. (a -> b) -> a -> b
$! ByteString -> ChannelId
ChannelId ByteString
bs
  | Bool
otherwise                    = Maybe ChannelId
forall a. Maybe a
Nothing
{-# INLINABLE channelId #-}

-- | Extract the underlying 'BS.ByteString' from a 'ChannelId'.
unChannelId :: ChannelId -> BS.ByteString
unChannelId :: ChannelId -> ByteString
unChannelId (ChannelId ByteString
bs) = ByteString
bs
{-# INLINE unChannelId #-}

-- amounts ---------------------------------------------------------------------

-- | Amount in satoshis (1/100,000,000 of a bitcoin).
--
-- Stored as a 'Word64'. Maximum valid value is 21,000,000 * 100,000,000
-- = 2,100,000,000,000,000 satoshis.
newtype Satoshis = Satoshis { Satoshis -> Word64
unSatoshis :: Word64 }
  deriving stock (Satoshis -> Satoshis -> Bool
(Satoshis -> Satoshis -> Bool)
-> (Satoshis -> Satoshis -> Bool) -> Eq Satoshis
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Satoshis -> Satoshis -> Bool
== :: Satoshis -> Satoshis -> Bool
$c/= :: Satoshis -> Satoshis -> Bool
/= :: Satoshis -> Satoshis -> Bool
Eq, Eq Satoshis
Eq Satoshis =>
(Satoshis -> Satoshis -> Ordering)
-> (Satoshis -> Satoshis -> Bool)
-> (Satoshis -> Satoshis -> Bool)
-> (Satoshis -> Satoshis -> Bool)
-> (Satoshis -> Satoshis -> Bool)
-> (Satoshis -> Satoshis -> Satoshis)
-> (Satoshis -> Satoshis -> Satoshis)
-> Ord Satoshis
Satoshis -> Satoshis -> Bool
Satoshis -> Satoshis -> Ordering
Satoshis -> Satoshis -> Satoshis
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 :: Satoshis -> Satoshis -> Ordering
compare :: Satoshis -> Satoshis -> Ordering
$c< :: Satoshis -> Satoshis -> Bool
< :: Satoshis -> Satoshis -> Bool
$c<= :: Satoshis -> Satoshis -> Bool
<= :: Satoshis -> Satoshis -> Bool
$c> :: Satoshis -> Satoshis -> Bool
> :: Satoshis -> Satoshis -> Bool
$c>= :: Satoshis -> Satoshis -> Bool
>= :: Satoshis -> Satoshis -> Bool
$cmax :: Satoshis -> Satoshis -> Satoshis
max :: Satoshis -> Satoshis -> Satoshis
$cmin :: Satoshis -> Satoshis -> Satoshis
min :: Satoshis -> Satoshis -> Satoshis
Ord, Int -> Satoshis -> ShowS
[Satoshis] -> ShowS
Satoshis -> String
(Int -> Satoshis -> ShowS)
-> (Satoshis -> String) -> ([Satoshis] -> ShowS) -> Show Satoshis
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Satoshis -> ShowS
showsPrec :: Int -> Satoshis -> ShowS
$cshow :: Satoshis -> String
show :: Satoshis -> String
$cshowList :: [Satoshis] -> ShowS
showList :: [Satoshis] -> ShowS
Show, (forall x. Satoshis -> Rep Satoshis x)
-> (forall x. Rep Satoshis x -> Satoshis) -> Generic Satoshis
forall x. Rep Satoshis x -> Satoshis
forall x. Satoshis -> Rep Satoshis x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Satoshis -> Rep Satoshis x
from :: forall x. Satoshis -> Rep Satoshis x
$cto :: forall x. Rep Satoshis x -> Satoshis
to :: forall x. Rep Satoshis x -> Satoshis
Generic)
  deriving newtype (Satoshis -> ()
(Satoshis -> ()) -> NFData Satoshis
forall a. (a -> ()) -> NFData a
$crnf :: Satoshis -> ()
rnf :: Satoshis -> ()
NFData, Integer -> Satoshis
Satoshis -> Satoshis
Satoshis -> Satoshis -> Satoshis
(Satoshis -> Satoshis -> Satoshis)
-> (Satoshis -> Satoshis -> Satoshis)
-> (Satoshis -> Satoshis -> Satoshis)
-> (Satoshis -> Satoshis)
-> (Satoshis -> Satoshis)
-> (Satoshis -> Satoshis)
-> (Integer -> Satoshis)
-> Num Satoshis
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
$c+ :: Satoshis -> Satoshis -> Satoshis
+ :: Satoshis -> Satoshis -> Satoshis
$c- :: Satoshis -> Satoshis -> Satoshis
- :: Satoshis -> Satoshis -> Satoshis
$c* :: Satoshis -> Satoshis -> Satoshis
* :: Satoshis -> Satoshis -> Satoshis
$cnegate :: Satoshis -> Satoshis
negate :: Satoshis -> Satoshis
$cabs :: Satoshis -> Satoshis
abs :: Satoshis -> Satoshis
$csignum :: Satoshis -> Satoshis
signum :: Satoshis -> Satoshis
$cfromInteger :: Integer -> Satoshis
fromInteger :: Integer -> Satoshis
Num, Int -> Satoshis
Satoshis -> Int
Satoshis -> [Satoshis]
Satoshis -> Satoshis
Satoshis -> Satoshis -> [Satoshis]
Satoshis -> Satoshis -> Satoshis -> [Satoshis]
(Satoshis -> Satoshis)
-> (Satoshis -> Satoshis)
-> (Int -> Satoshis)
-> (Satoshis -> Int)
-> (Satoshis -> [Satoshis])
-> (Satoshis -> Satoshis -> [Satoshis])
-> (Satoshis -> Satoshis -> [Satoshis])
-> (Satoshis -> Satoshis -> Satoshis -> [Satoshis])
-> Enum Satoshis
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
$csucc :: Satoshis -> Satoshis
succ :: Satoshis -> Satoshis
$cpred :: Satoshis -> Satoshis
pred :: Satoshis -> Satoshis
$ctoEnum :: Int -> Satoshis
toEnum :: Int -> Satoshis
$cfromEnum :: Satoshis -> Int
fromEnum :: Satoshis -> Int
$cenumFrom :: Satoshis -> [Satoshis]
enumFrom :: Satoshis -> [Satoshis]
$cenumFromThen :: Satoshis -> Satoshis -> [Satoshis]
enumFromThen :: Satoshis -> Satoshis -> [Satoshis]
$cenumFromTo :: Satoshis -> Satoshis -> [Satoshis]
enumFromTo :: Satoshis -> Satoshis -> [Satoshis]
$cenumFromThenTo :: Satoshis -> Satoshis -> Satoshis -> [Satoshis]
enumFromThenTo :: Satoshis -> Satoshis -> Satoshis -> [Satoshis]
Enum, Num Satoshis
Ord Satoshis
(Num Satoshis, Ord Satoshis) =>
(Satoshis -> Rational) -> Real Satoshis
Satoshis -> Rational
forall a. (Num a, Ord a) => (a -> Rational) -> Real a
$ctoRational :: Satoshis -> Rational
toRational :: Satoshis -> Rational
Real, Enum Satoshis
Real Satoshis
(Real Satoshis, Enum Satoshis) =>
(Satoshis -> Satoshis -> Satoshis)
-> (Satoshis -> Satoshis -> Satoshis)
-> (Satoshis -> Satoshis -> Satoshis)
-> (Satoshis -> Satoshis -> Satoshis)
-> (Satoshis -> Satoshis -> (Satoshis, Satoshis))
-> (Satoshis -> Satoshis -> (Satoshis, Satoshis))
-> (Satoshis -> Integer)
-> Integral Satoshis
Satoshis -> Integer
Satoshis -> Satoshis -> (Satoshis, Satoshis)
Satoshis -> Satoshis -> Satoshis
forall a.
(Real a, Enum a) =>
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> (a, a))
-> (a -> a -> (a, a))
-> (a -> Integer)
-> Integral a
$cquot :: Satoshis -> Satoshis -> Satoshis
quot :: Satoshis -> Satoshis -> Satoshis
$crem :: Satoshis -> Satoshis -> Satoshis
rem :: Satoshis -> Satoshis -> Satoshis
$cdiv :: Satoshis -> Satoshis -> Satoshis
div :: Satoshis -> Satoshis -> Satoshis
$cmod :: Satoshis -> Satoshis -> Satoshis
mod :: Satoshis -> Satoshis -> Satoshis
$cquotRem :: Satoshis -> Satoshis -> (Satoshis, Satoshis)
quotRem :: Satoshis -> Satoshis -> (Satoshis, Satoshis)
$cdivMod :: Satoshis -> Satoshis -> (Satoshis, Satoshis)
divMod :: Satoshis -> Satoshis -> (Satoshis, Satoshis)
$ctoInteger :: Satoshis -> Integer
toInteger :: Satoshis -> Integer
Integral)

-- | Amount in millisatoshis (1/1000 of a satoshi).
--
-- Stored as a 'Word64'. Used for HTLC amounts and channel balances.
newtype MilliSatoshis = MilliSatoshis { MilliSatoshis -> Word64
unMilliSatoshis :: Word64 }
  deriving stock (MilliSatoshis -> MilliSatoshis -> Bool
(MilliSatoshis -> MilliSatoshis -> Bool)
-> (MilliSatoshis -> MilliSatoshis -> Bool) -> Eq MilliSatoshis
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: MilliSatoshis -> MilliSatoshis -> Bool
== :: MilliSatoshis -> MilliSatoshis -> Bool
$c/= :: MilliSatoshis -> MilliSatoshis -> Bool
/= :: MilliSatoshis -> MilliSatoshis -> Bool
Eq, Eq MilliSatoshis
Eq MilliSatoshis =>
(MilliSatoshis -> MilliSatoshis -> Ordering)
-> (MilliSatoshis -> MilliSatoshis -> Bool)
-> (MilliSatoshis -> MilliSatoshis -> Bool)
-> (MilliSatoshis -> MilliSatoshis -> Bool)
-> (MilliSatoshis -> MilliSatoshis -> Bool)
-> (MilliSatoshis -> MilliSatoshis -> MilliSatoshis)
-> (MilliSatoshis -> MilliSatoshis -> MilliSatoshis)
-> Ord MilliSatoshis
MilliSatoshis -> MilliSatoshis -> Bool
MilliSatoshis -> MilliSatoshis -> Ordering
MilliSatoshis -> MilliSatoshis -> MilliSatoshis
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 :: MilliSatoshis -> MilliSatoshis -> Ordering
compare :: MilliSatoshis -> MilliSatoshis -> Ordering
$c< :: MilliSatoshis -> MilliSatoshis -> Bool
< :: MilliSatoshis -> MilliSatoshis -> Bool
$c<= :: MilliSatoshis -> MilliSatoshis -> Bool
<= :: MilliSatoshis -> MilliSatoshis -> Bool
$c> :: MilliSatoshis -> MilliSatoshis -> Bool
> :: MilliSatoshis -> MilliSatoshis -> Bool
$c>= :: MilliSatoshis -> MilliSatoshis -> Bool
>= :: MilliSatoshis -> MilliSatoshis -> Bool
$cmax :: MilliSatoshis -> MilliSatoshis -> MilliSatoshis
max :: MilliSatoshis -> MilliSatoshis -> MilliSatoshis
$cmin :: MilliSatoshis -> MilliSatoshis -> MilliSatoshis
min :: MilliSatoshis -> MilliSatoshis -> MilliSatoshis
Ord, Int -> MilliSatoshis -> ShowS
[MilliSatoshis] -> ShowS
MilliSatoshis -> String
(Int -> MilliSatoshis -> ShowS)
-> (MilliSatoshis -> String)
-> ([MilliSatoshis] -> ShowS)
-> Show MilliSatoshis
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> MilliSatoshis -> ShowS
showsPrec :: Int -> MilliSatoshis -> ShowS
$cshow :: MilliSatoshis -> String
show :: MilliSatoshis -> String
$cshowList :: [MilliSatoshis] -> ShowS
showList :: [MilliSatoshis] -> ShowS
Show, (forall x. MilliSatoshis -> Rep MilliSatoshis x)
-> (forall x. Rep MilliSatoshis x -> MilliSatoshis)
-> Generic MilliSatoshis
forall x. Rep MilliSatoshis x -> MilliSatoshis
forall x. MilliSatoshis -> Rep MilliSatoshis x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. MilliSatoshis -> Rep MilliSatoshis x
from :: forall x. MilliSatoshis -> Rep MilliSatoshis x
$cto :: forall x. Rep MilliSatoshis x -> MilliSatoshis
to :: forall x. Rep MilliSatoshis x -> MilliSatoshis
Generic)
  deriving newtype (MilliSatoshis -> ()
(MilliSatoshis -> ()) -> NFData MilliSatoshis
forall a. (a -> ()) -> NFData a
$crnf :: MilliSatoshis -> ()
rnf :: MilliSatoshis -> ()
NFData, Integer -> MilliSatoshis
MilliSatoshis -> MilliSatoshis
MilliSatoshis -> MilliSatoshis -> MilliSatoshis
(MilliSatoshis -> MilliSatoshis -> MilliSatoshis)
-> (MilliSatoshis -> MilliSatoshis -> MilliSatoshis)
-> (MilliSatoshis -> MilliSatoshis -> MilliSatoshis)
-> (MilliSatoshis -> MilliSatoshis)
-> (MilliSatoshis -> MilliSatoshis)
-> (MilliSatoshis -> MilliSatoshis)
-> (Integer -> MilliSatoshis)
-> Num MilliSatoshis
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
$c+ :: MilliSatoshis -> MilliSatoshis -> MilliSatoshis
+ :: MilliSatoshis -> MilliSatoshis -> MilliSatoshis
$c- :: MilliSatoshis -> MilliSatoshis -> MilliSatoshis
- :: MilliSatoshis -> MilliSatoshis -> MilliSatoshis
$c* :: MilliSatoshis -> MilliSatoshis -> MilliSatoshis
* :: MilliSatoshis -> MilliSatoshis -> MilliSatoshis
$cnegate :: MilliSatoshis -> MilliSatoshis
negate :: MilliSatoshis -> MilliSatoshis
$cabs :: MilliSatoshis -> MilliSatoshis
abs :: MilliSatoshis -> MilliSatoshis
$csignum :: MilliSatoshis -> MilliSatoshis
signum :: MilliSatoshis -> MilliSatoshis
$cfromInteger :: Integer -> MilliSatoshis
fromInteger :: Integer -> MilliSatoshis
Num, Int -> MilliSatoshis
MilliSatoshis -> Int
MilliSatoshis -> [MilliSatoshis]
MilliSatoshis -> MilliSatoshis
MilliSatoshis -> MilliSatoshis -> [MilliSatoshis]
MilliSatoshis -> MilliSatoshis -> MilliSatoshis -> [MilliSatoshis]
(MilliSatoshis -> MilliSatoshis)
-> (MilliSatoshis -> MilliSatoshis)
-> (Int -> MilliSatoshis)
-> (MilliSatoshis -> Int)
-> (MilliSatoshis -> [MilliSatoshis])
-> (MilliSatoshis -> MilliSatoshis -> [MilliSatoshis])
-> (MilliSatoshis -> MilliSatoshis -> [MilliSatoshis])
-> (MilliSatoshis
    -> MilliSatoshis -> MilliSatoshis -> [MilliSatoshis])
-> Enum MilliSatoshis
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
$csucc :: MilliSatoshis -> MilliSatoshis
succ :: MilliSatoshis -> MilliSatoshis
$cpred :: MilliSatoshis -> MilliSatoshis
pred :: MilliSatoshis -> MilliSatoshis
$ctoEnum :: Int -> MilliSatoshis
toEnum :: Int -> MilliSatoshis
$cfromEnum :: MilliSatoshis -> Int
fromEnum :: MilliSatoshis -> Int
$cenumFrom :: MilliSatoshis -> [MilliSatoshis]
enumFrom :: MilliSatoshis -> [MilliSatoshis]
$cenumFromThen :: MilliSatoshis -> MilliSatoshis -> [MilliSatoshis]
enumFromThen :: MilliSatoshis -> MilliSatoshis -> [MilliSatoshis]
$cenumFromTo :: MilliSatoshis -> MilliSatoshis -> [MilliSatoshis]
enumFromTo :: MilliSatoshis -> MilliSatoshis -> [MilliSatoshis]
$cenumFromThenTo :: MilliSatoshis -> MilliSatoshis -> MilliSatoshis -> [MilliSatoshis]
enumFromThenTo :: MilliSatoshis -> MilliSatoshis -> MilliSatoshis -> [MilliSatoshis]
Enum, Num MilliSatoshis
Ord MilliSatoshis
(Num MilliSatoshis, Ord MilliSatoshis) =>
(MilliSatoshis -> Rational) -> Real MilliSatoshis
MilliSatoshis -> Rational
forall a. (Num a, Ord a) => (a -> Rational) -> Real a
$ctoRational :: MilliSatoshis -> Rational
toRational :: MilliSatoshis -> Rational
Real, Enum MilliSatoshis
Real MilliSatoshis
(Real MilliSatoshis, Enum MilliSatoshis) =>
(MilliSatoshis -> MilliSatoshis -> MilliSatoshis)
-> (MilliSatoshis -> MilliSatoshis -> MilliSatoshis)
-> (MilliSatoshis -> MilliSatoshis -> MilliSatoshis)
-> (MilliSatoshis -> MilliSatoshis -> MilliSatoshis)
-> (MilliSatoshis
    -> MilliSatoshis -> (MilliSatoshis, MilliSatoshis))
-> (MilliSatoshis
    -> MilliSatoshis -> (MilliSatoshis, MilliSatoshis))
-> (MilliSatoshis -> Integer)
-> Integral MilliSatoshis
MilliSatoshis -> Integer
MilliSatoshis -> MilliSatoshis -> (MilliSatoshis, MilliSatoshis)
MilliSatoshis -> MilliSatoshis -> MilliSatoshis
forall a.
(Real a, Enum a) =>
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> (a, a))
-> (a -> a -> (a, a))
-> (a -> Integer)
-> Integral a
$cquot :: MilliSatoshis -> MilliSatoshis -> MilliSatoshis
quot :: MilliSatoshis -> MilliSatoshis -> MilliSatoshis
$crem :: MilliSatoshis -> MilliSatoshis -> MilliSatoshis
rem :: MilliSatoshis -> MilliSatoshis -> MilliSatoshis
$cdiv :: MilliSatoshis -> MilliSatoshis -> MilliSatoshis
div :: MilliSatoshis -> MilliSatoshis -> MilliSatoshis
$cmod :: MilliSatoshis -> MilliSatoshis -> MilliSatoshis
mod :: MilliSatoshis -> MilliSatoshis -> MilliSatoshis
$cquotRem :: MilliSatoshis -> MilliSatoshis -> (MilliSatoshis, MilliSatoshis)
quotRem :: MilliSatoshis -> MilliSatoshis -> (MilliSatoshis, MilliSatoshis)
$cdivMod :: MilliSatoshis -> MilliSatoshis -> (MilliSatoshis, MilliSatoshis)
divMod :: MilliSatoshis -> MilliSatoshis -> (MilliSatoshis, MilliSatoshis)
$ctoInteger :: MilliSatoshis -> Integer
toInteger :: MilliSatoshis -> Integer
Integral)

-- | Convert 'Satoshis' to 'MilliSatoshis'.
--
-- >>> satoshisToMsat (Satoshis 1)
-- MilliSatoshis 1000
satoshisToMsat :: Satoshis -> MilliSatoshis
satoshisToMsat :: Satoshis -> MilliSatoshis
satoshisToMsat (Satoshis !Word64
s) = Word64 -> MilliSatoshis
MilliSatoshis (Word64 -> MilliSatoshis) -> Word64 -> MilliSatoshis
forall a b. (a -> b) -> a -> b
$! Word64
s Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
* Word64
1000
{-# INLINE satoshisToMsat #-}

-- | Convert 'MilliSatoshis' to 'Satoshis', rounding down.
--
-- >>> msatToSatoshis (MilliSatoshis 1500)
-- Satoshis 1
msatToSatoshis :: MilliSatoshis -> Satoshis
msatToSatoshis :: MilliSatoshis -> Satoshis
msatToSatoshis (MilliSatoshis !Word64
m) = Word64 -> Satoshis
Satoshis (Word64 -> Satoshis) -> Word64 -> Satoshis
forall a b. (a -> b) -> a -> b
$! Word64
m Word64 -> Word64 -> Word64
forall a. Integral a => a -> a -> a
`div` Word64
1000
{-# INLINE msatToSatoshis #-}

-- cryptographic types ---------------------------------------------------------

-- | A 64-byte compact ECDSA signature.
--
-- Used for commitment transaction signatures, HTLC signatures, and
-- closing transaction signatures.
newtype Signature = Signature BS.ByteString
  deriving stock (Signature -> Signature -> Bool
(Signature -> Signature -> Bool)
-> (Signature -> Signature -> Bool) -> Eq Signature
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Signature -> Signature -> Bool
== :: Signature -> Signature -> Bool
$c/= :: Signature -> Signature -> Bool
/= :: Signature -> Signature -> Bool
Eq, Eq Signature
Eq Signature =>
(Signature -> Signature -> Ordering)
-> (Signature -> Signature -> Bool)
-> (Signature -> Signature -> Bool)
-> (Signature -> Signature -> Bool)
-> (Signature -> Signature -> Bool)
-> (Signature -> Signature -> Signature)
-> (Signature -> Signature -> Signature)
-> Ord Signature
Signature -> Signature -> Bool
Signature -> Signature -> Ordering
Signature -> Signature -> Signature
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 :: Signature -> Signature -> Ordering
compare :: Signature -> Signature -> Ordering
$c< :: Signature -> Signature -> Bool
< :: Signature -> Signature -> Bool
$c<= :: Signature -> Signature -> Bool
<= :: Signature -> Signature -> Bool
$c> :: Signature -> Signature -> Bool
> :: Signature -> Signature -> Bool
$c>= :: Signature -> Signature -> Bool
>= :: Signature -> Signature -> Bool
$cmax :: Signature -> Signature -> Signature
max :: Signature -> Signature -> Signature
$cmin :: Signature -> Signature -> Signature
min :: Signature -> Signature -> Signature
Ord, Int -> Signature -> ShowS
[Signature] -> ShowS
Signature -> String
(Int -> Signature -> ShowS)
-> (Signature -> String)
-> ([Signature] -> ShowS)
-> Show Signature
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Signature -> ShowS
showsPrec :: Int -> Signature -> ShowS
$cshow :: Signature -> String
show :: Signature -> String
$cshowList :: [Signature] -> ShowS
showList :: [Signature] -> ShowS
Show, (forall x. Signature -> Rep Signature x)
-> (forall x. Rep Signature x -> Signature) -> Generic Signature
forall x. Rep Signature x -> Signature
forall x. Signature -> Rep Signature x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Signature -> Rep Signature x
from :: forall x. Signature -> Rep Signature x
$cto :: forall x. Rep Signature x -> Signature
to :: forall x. Rep Signature x -> Signature
Generic)
  deriving newtype Signature -> ()
(Signature -> ()) -> NFData Signature
forall a. (a -> ()) -> NFData a
$crnf :: Signature -> ()
rnf :: Signature -> ()
NFData

-- | Construct a 'Signature' from a 64-byte 'BS.ByteString'.
--
-- Returns 'Nothing' if the input is not exactly 64 bytes.
signature :: BS.ByteString -> Maybe Signature
signature :: ByteString -> Maybe Signature
signature !ByteString
bs
  | ByteString -> Int
BS.length ByteString
bs Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
signatureLen = Signature -> Maybe Signature
forall a. a -> Maybe a
Just (Signature -> Maybe Signature) -> Signature -> Maybe Signature
forall a b. (a -> b) -> a -> b
$! ByteString -> Signature
Signature ByteString
bs
  | Bool
otherwise                    = Maybe Signature
forall a. Maybe a
Nothing
{-# INLINABLE signature #-}

-- | Extract the underlying 'BS.ByteString' from a 'Signature'.
unSignature :: Signature -> BS.ByteString
unSignature :: Signature -> ByteString
unSignature (Signature ByteString
bs) = ByteString
bs
{-# INLINE unSignature #-}

-- | A 33-byte compressed secp256k1 public key.
--
-- Used for funding pubkeys, basepoints, and per-commitment points.
newtype Point = Point BS.ByteString
  deriving stock (Point -> Point -> Bool
(Point -> Point -> Bool) -> (Point -> Point -> Bool) -> Eq Point
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Point -> Point -> Bool
== :: Point -> Point -> Bool
$c/= :: Point -> Point -> Bool
/= :: Point -> Point -> Bool
Eq, Eq Point
Eq Point =>
(Point -> Point -> Ordering)
-> (Point -> Point -> Bool)
-> (Point -> Point -> Bool)
-> (Point -> Point -> Bool)
-> (Point -> Point -> Bool)
-> (Point -> Point -> Point)
-> (Point -> Point -> Point)
-> Ord Point
Point -> Point -> Bool
Point -> Point -> Ordering
Point -> Point -> Point
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 :: Point -> Point -> Ordering
compare :: Point -> Point -> Ordering
$c< :: Point -> Point -> Bool
< :: Point -> Point -> Bool
$c<= :: Point -> Point -> Bool
<= :: Point -> Point -> Bool
$c> :: Point -> Point -> Bool
> :: Point -> Point -> Bool
$c>= :: Point -> Point -> Bool
>= :: Point -> Point -> Bool
$cmax :: Point -> Point -> Point
max :: Point -> Point -> Point
$cmin :: Point -> Point -> Point
min :: Point -> Point -> Point
Ord, Int -> Point -> ShowS
[Point] -> ShowS
Point -> String
(Int -> Point -> ShowS)
-> (Point -> String) -> ([Point] -> ShowS) -> Show Point
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Point -> ShowS
showsPrec :: Int -> Point -> ShowS
$cshow :: Point -> String
show :: Point -> String
$cshowList :: [Point] -> ShowS
showList :: [Point] -> ShowS
Show, (forall x. Point -> Rep Point x)
-> (forall x. Rep Point x -> Point) -> Generic Point
forall x. Rep Point x -> Point
forall x. Point -> Rep Point x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Point -> Rep Point x
from :: forall x. Point -> Rep Point x
$cto :: forall x. Rep Point x -> Point
to :: forall x. Rep Point x -> Point
Generic)
  deriving newtype Point -> ()
(Point -> ()) -> NFData Point
forall a. (a -> ()) -> NFData a
$crnf :: Point -> ()
rnf :: Point -> ()
NFData

-- | Construct a 'Point' from a 33-byte 'BS.ByteString'.
--
-- Returns 'Nothing' if the input is not exactly 33 bytes.
--
-- Note: This only validates the length. Use secp256k1 libraries for
-- full point validation.
point :: BS.ByteString -> Maybe Point
point :: ByteString -> Maybe Point
point !ByteString
bs
  | ByteString -> Int
BS.length ByteString
bs Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
pointLen = Point -> Maybe Point
forall a. a -> Maybe a
Just (Point -> Maybe Point) -> Point -> Maybe Point
forall a b. (a -> b) -> a -> b
$! ByteString -> Point
Point ByteString
bs
  | Bool
otherwise                = Maybe Point
forall a. Maybe a
Nothing
{-# INLINABLE point #-}

-- | Extract the underlying 'BS.ByteString' from a 'Point'.
unPoint :: Point -> BS.ByteString
unPoint :: Point -> ByteString
unPoint (Point ByteString
bs) = ByteString
bs
{-# INLINE unPoint #-}

-- | A 32-byte SHA256 payment hash.
--
-- Used to identify HTLCs. The preimage that hashes to this value is
-- required to claim the HTLC.
newtype PaymentHash = PaymentHash BS.ByteString
  deriving stock (PaymentHash -> PaymentHash -> Bool
(PaymentHash -> PaymentHash -> Bool)
-> (PaymentHash -> PaymentHash -> Bool) -> Eq PaymentHash
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PaymentHash -> PaymentHash -> Bool
== :: PaymentHash -> PaymentHash -> Bool
$c/= :: PaymentHash -> PaymentHash -> Bool
/= :: PaymentHash -> PaymentHash -> Bool
Eq, Eq PaymentHash
Eq PaymentHash =>
(PaymentHash -> PaymentHash -> Ordering)
-> (PaymentHash -> PaymentHash -> Bool)
-> (PaymentHash -> PaymentHash -> Bool)
-> (PaymentHash -> PaymentHash -> Bool)
-> (PaymentHash -> PaymentHash -> Bool)
-> (PaymentHash -> PaymentHash -> PaymentHash)
-> (PaymentHash -> PaymentHash -> PaymentHash)
-> Ord PaymentHash
PaymentHash -> PaymentHash -> Bool
PaymentHash -> PaymentHash -> Ordering
PaymentHash -> PaymentHash -> PaymentHash
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 :: PaymentHash -> PaymentHash -> Ordering
compare :: PaymentHash -> PaymentHash -> Ordering
$c< :: PaymentHash -> PaymentHash -> Bool
< :: PaymentHash -> PaymentHash -> Bool
$c<= :: PaymentHash -> PaymentHash -> Bool
<= :: PaymentHash -> PaymentHash -> Bool
$c> :: PaymentHash -> PaymentHash -> Bool
> :: PaymentHash -> PaymentHash -> Bool
$c>= :: PaymentHash -> PaymentHash -> Bool
>= :: PaymentHash -> PaymentHash -> Bool
$cmax :: PaymentHash -> PaymentHash -> PaymentHash
max :: PaymentHash -> PaymentHash -> PaymentHash
$cmin :: PaymentHash -> PaymentHash -> PaymentHash
min :: PaymentHash -> PaymentHash -> PaymentHash
Ord, Int -> PaymentHash -> ShowS
[PaymentHash] -> ShowS
PaymentHash -> String
(Int -> PaymentHash -> ShowS)
-> (PaymentHash -> String)
-> ([PaymentHash] -> ShowS)
-> Show PaymentHash
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PaymentHash -> ShowS
showsPrec :: Int -> PaymentHash -> ShowS
$cshow :: PaymentHash -> String
show :: PaymentHash -> String
$cshowList :: [PaymentHash] -> ShowS
showList :: [PaymentHash] -> ShowS
Show, (forall x. PaymentHash -> Rep PaymentHash x)
-> (forall x. Rep PaymentHash x -> PaymentHash)
-> Generic PaymentHash
forall x. Rep PaymentHash x -> PaymentHash
forall x. PaymentHash -> Rep PaymentHash x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. PaymentHash -> Rep PaymentHash x
from :: forall x. PaymentHash -> Rep PaymentHash x
$cto :: forall x. Rep PaymentHash x -> PaymentHash
to :: forall x. Rep PaymentHash x -> PaymentHash
Generic)
  deriving newtype PaymentHash -> ()
(PaymentHash -> ()) -> NFData PaymentHash
forall a. (a -> ()) -> NFData a
$crnf :: PaymentHash -> ()
rnf :: PaymentHash -> ()
NFData

-- | Construct a 'PaymentHash' from a 32-byte 'BS.ByteString'.
--
-- Returns 'Nothing' if the input is not exactly 32 bytes.
paymentHash :: BS.ByteString -> Maybe PaymentHash
paymentHash :: ByteString -> Maybe PaymentHash
paymentHash !ByteString
bs
  | ByteString -> Int
BS.length ByteString
bs Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
paymentHashLen = PaymentHash -> Maybe PaymentHash
forall a. a -> Maybe a
Just (PaymentHash -> Maybe PaymentHash)
-> PaymentHash -> Maybe PaymentHash
forall a b. (a -> b) -> a -> b
$! ByteString -> PaymentHash
PaymentHash ByteString
bs
  | Bool
otherwise                      = Maybe PaymentHash
forall a. Maybe a
Nothing
{-# INLINABLE paymentHash #-}

-- | Extract the underlying 'BS.ByteString' from a 'PaymentHash'.
unPaymentHash :: PaymentHash -> BS.ByteString
unPaymentHash :: PaymentHash -> ByteString
unPaymentHash (PaymentHash ByteString
bs) = ByteString
bs
{-# INLINE unPaymentHash #-}

-- | A 32-byte payment preimage.
--
-- The SHA256 hash of this value produces the corresponding 'PaymentHash'.
-- Knowledge of the preimage allows claiming an HTLC.
newtype PaymentPreimage = PaymentPreimage BS.ByteString
  deriving stock (PaymentPreimage -> PaymentPreimage -> Bool
(PaymentPreimage -> PaymentPreimage -> Bool)
-> (PaymentPreimage -> PaymentPreimage -> Bool)
-> Eq PaymentPreimage
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PaymentPreimage -> PaymentPreimage -> Bool
== :: PaymentPreimage -> PaymentPreimage -> Bool
$c/= :: PaymentPreimage -> PaymentPreimage -> Bool
/= :: PaymentPreimage -> PaymentPreimage -> Bool
Eq, Eq PaymentPreimage
Eq PaymentPreimage =>
(PaymentPreimage -> PaymentPreimage -> Ordering)
-> (PaymentPreimage -> PaymentPreimage -> Bool)
-> (PaymentPreimage -> PaymentPreimage -> Bool)
-> (PaymentPreimage -> PaymentPreimage -> Bool)
-> (PaymentPreimage -> PaymentPreimage -> Bool)
-> (PaymentPreimage -> PaymentPreimage -> PaymentPreimage)
-> (PaymentPreimage -> PaymentPreimage -> PaymentPreimage)
-> Ord PaymentPreimage
PaymentPreimage -> PaymentPreimage -> Bool
PaymentPreimage -> PaymentPreimage -> Ordering
PaymentPreimage -> PaymentPreimage -> PaymentPreimage
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 :: PaymentPreimage -> PaymentPreimage -> Ordering
compare :: PaymentPreimage -> PaymentPreimage -> Ordering
$c< :: PaymentPreimage -> PaymentPreimage -> Bool
< :: PaymentPreimage -> PaymentPreimage -> Bool
$c<= :: PaymentPreimage -> PaymentPreimage -> Bool
<= :: PaymentPreimage -> PaymentPreimage -> Bool
$c> :: PaymentPreimage -> PaymentPreimage -> Bool
> :: PaymentPreimage -> PaymentPreimage -> Bool
$c>= :: PaymentPreimage -> PaymentPreimage -> Bool
>= :: PaymentPreimage -> PaymentPreimage -> Bool
$cmax :: PaymentPreimage -> PaymentPreimage -> PaymentPreimage
max :: PaymentPreimage -> PaymentPreimage -> PaymentPreimage
$cmin :: PaymentPreimage -> PaymentPreimage -> PaymentPreimage
min :: PaymentPreimage -> PaymentPreimage -> PaymentPreimage
Ord, Int -> PaymentPreimage -> ShowS
[PaymentPreimage] -> ShowS
PaymentPreimage -> String
(Int -> PaymentPreimage -> ShowS)
-> (PaymentPreimage -> String)
-> ([PaymentPreimage] -> ShowS)
-> Show PaymentPreimage
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PaymentPreimage -> ShowS
showsPrec :: Int -> PaymentPreimage -> ShowS
$cshow :: PaymentPreimage -> String
show :: PaymentPreimage -> String
$cshowList :: [PaymentPreimage] -> ShowS
showList :: [PaymentPreimage] -> ShowS
Show, (forall x. PaymentPreimage -> Rep PaymentPreimage x)
-> (forall x. Rep PaymentPreimage x -> PaymentPreimage)
-> Generic PaymentPreimage
forall x. Rep PaymentPreimage x -> PaymentPreimage
forall x. PaymentPreimage -> Rep PaymentPreimage x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. PaymentPreimage -> Rep PaymentPreimage x
from :: forall x. PaymentPreimage -> Rep PaymentPreimage x
$cto :: forall x. Rep PaymentPreimage x -> PaymentPreimage
to :: forall x. Rep PaymentPreimage x -> PaymentPreimage
Generic)
  deriving newtype PaymentPreimage -> ()
(PaymentPreimage -> ()) -> NFData PaymentPreimage
forall a. (a -> ()) -> NFData a
$crnf :: PaymentPreimage -> ()
rnf :: PaymentPreimage -> ()
NFData

-- | Construct a 'PaymentPreimage' from a 32-byte 'BS.ByteString'.
--
-- Returns 'Nothing' if the input is not exactly 32 bytes.
paymentPreimage :: BS.ByteString -> Maybe PaymentPreimage
paymentPreimage :: ByteString -> Maybe PaymentPreimage
paymentPreimage !ByteString
bs
  | ByteString -> Int
BS.length ByteString
bs Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
paymentPreimageLen = PaymentPreimage -> Maybe PaymentPreimage
forall a. a -> Maybe a
Just (PaymentPreimage -> Maybe PaymentPreimage)
-> PaymentPreimage -> Maybe PaymentPreimage
forall a b. (a -> b) -> a -> b
$! ByteString -> PaymentPreimage
PaymentPreimage ByteString
bs
  | Bool
otherwise                          = Maybe PaymentPreimage
forall a. Maybe a
Nothing
{-# INLINABLE paymentPreimage #-}

-- | Extract the underlying 'BS.ByteString' from a 'PaymentPreimage'.
unPaymentPreimage :: PaymentPreimage -> BS.ByteString
unPaymentPreimage :: PaymentPreimage -> ByteString
unPaymentPreimage (PaymentPreimage ByteString
bs) = ByteString
bs
{-# INLINE unPaymentPreimage #-}

-- | A 32-byte per-commitment secret.
--
-- Used in revoke_and_ack and channel_reestablish messages to revoke
-- old commitment transactions.
newtype Secret = Secret BS.ByteString
  deriving stock (Secret -> Secret -> Bool
(Secret -> Secret -> Bool)
-> (Secret -> Secret -> Bool) -> Eq Secret
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Secret -> Secret -> Bool
== :: Secret -> Secret -> Bool
$c/= :: Secret -> Secret -> Bool
/= :: Secret -> Secret -> Bool
Eq, Eq Secret
Eq Secret =>
(Secret -> Secret -> Ordering)
-> (Secret -> Secret -> Bool)
-> (Secret -> Secret -> Bool)
-> (Secret -> Secret -> Bool)
-> (Secret -> Secret -> Bool)
-> (Secret -> Secret -> Secret)
-> (Secret -> Secret -> Secret)
-> Ord Secret
Secret -> Secret -> Bool
Secret -> Secret -> Ordering
Secret -> Secret -> Secret
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 :: Secret -> Secret -> Ordering
compare :: Secret -> Secret -> Ordering
$c< :: Secret -> Secret -> Bool
< :: Secret -> Secret -> Bool
$c<= :: Secret -> Secret -> Bool
<= :: Secret -> Secret -> Bool
$c> :: Secret -> Secret -> Bool
> :: Secret -> Secret -> Bool
$c>= :: Secret -> Secret -> Bool
>= :: Secret -> Secret -> Bool
$cmax :: Secret -> Secret -> Secret
max :: Secret -> Secret -> Secret
$cmin :: Secret -> Secret -> Secret
min :: Secret -> Secret -> Secret
Ord, Int -> Secret -> ShowS
[Secret] -> ShowS
Secret -> String
(Int -> Secret -> ShowS)
-> (Secret -> String) -> ([Secret] -> ShowS) -> Show Secret
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Secret -> ShowS
showsPrec :: Int -> Secret -> ShowS
$cshow :: Secret -> String
show :: Secret -> String
$cshowList :: [Secret] -> ShowS
showList :: [Secret] -> ShowS
Show, (forall x. Secret -> Rep Secret x)
-> (forall x. Rep Secret x -> Secret) -> Generic Secret
forall x. Rep Secret x -> Secret
forall x. Secret -> Rep Secret x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Secret -> Rep Secret x
from :: forall x. Secret -> Rep Secret x
$cto :: forall x. Rep Secret x -> Secret
to :: forall x. Rep Secret x -> Secret
Generic)
  deriving newtype Secret -> ()
(Secret -> ()) -> NFData Secret
forall a. (a -> ()) -> NFData a
$crnf :: Secret -> ()
rnf :: Secret -> ()
NFData

-- | Construct a 'Secret' from a 32-byte 'BS.ByteString'.
--
-- Returns 'Nothing' if the input is not exactly 32 bytes.
secret :: BS.ByteString -> Maybe Secret
secret :: ByteString -> Maybe Secret
secret !ByteString
bs
  | ByteString -> Int
BS.length ByteString
bs Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
secretLen = Secret -> Maybe Secret
forall a. a -> Maybe a
Just (Secret -> Maybe Secret) -> Secret -> Maybe Secret
forall a b. (a -> b) -> a -> b
$! ByteString -> Secret
Secret ByteString
bs
  | Bool
otherwise                 = Maybe Secret
forall a. Maybe a
Nothing
{-# INLINABLE secret #-}

-- | Extract the underlying 'BS.ByteString' from a 'Secret'.
unSecret :: Secret -> BS.ByteString
unSecret :: Secret -> ByteString
unSecret (Secret ByteString
bs) = ByteString
bs
{-# INLINE unSecret #-}

-- transaction types -----------------------------------------------------------

-- | A 32-byte transaction identifier.
--
-- The double-SHA256 hash of a serialized transaction.
newtype TxId = TxId BS.ByteString
  deriving stock (TxId -> TxId -> Bool
(TxId -> TxId -> Bool) -> (TxId -> TxId -> Bool) -> Eq TxId
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TxId -> TxId -> Bool
== :: TxId -> TxId -> Bool
$c/= :: TxId -> TxId -> Bool
/= :: TxId -> TxId -> Bool
Eq, Eq TxId
Eq TxId =>
(TxId -> TxId -> Ordering)
-> (TxId -> TxId -> Bool)
-> (TxId -> TxId -> Bool)
-> (TxId -> TxId -> Bool)
-> (TxId -> TxId -> Bool)
-> (TxId -> TxId -> TxId)
-> (TxId -> TxId -> TxId)
-> Ord TxId
TxId -> TxId -> Bool
TxId -> TxId -> Ordering
TxId -> TxId -> TxId
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 :: TxId -> TxId -> Ordering
compare :: TxId -> TxId -> Ordering
$c< :: TxId -> TxId -> Bool
< :: TxId -> TxId -> Bool
$c<= :: TxId -> TxId -> Bool
<= :: TxId -> TxId -> Bool
$c> :: TxId -> TxId -> Bool
> :: TxId -> TxId -> Bool
$c>= :: TxId -> TxId -> Bool
>= :: TxId -> TxId -> Bool
$cmax :: TxId -> TxId -> TxId
max :: TxId -> TxId -> TxId
$cmin :: TxId -> TxId -> TxId
min :: TxId -> TxId -> TxId
Ord, Int -> TxId -> ShowS
[TxId] -> ShowS
TxId -> String
(Int -> TxId -> ShowS)
-> (TxId -> String) -> ([TxId] -> ShowS) -> Show TxId
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TxId -> ShowS
showsPrec :: Int -> TxId -> ShowS
$cshow :: TxId -> String
show :: TxId -> String
$cshowList :: [TxId] -> ShowS
showList :: [TxId] -> ShowS
Show, (forall x. TxId -> Rep TxId x)
-> (forall x. Rep TxId x -> TxId) -> Generic TxId
forall x. Rep TxId x -> TxId
forall x. TxId -> Rep TxId x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. TxId -> Rep TxId x
from :: forall x. TxId -> Rep TxId x
$cto :: forall x. Rep TxId x -> TxId
to :: forall x. Rep TxId x -> TxId
Generic)
  deriving newtype TxId -> ()
(TxId -> ()) -> NFData TxId
forall a. (a -> ()) -> NFData a
$crnf :: TxId -> ()
rnf :: TxId -> ()
NFData

-- | Construct a 'TxId' from a 32-byte 'BS.ByteString'.
--
-- Returns 'Nothing' if the input is not exactly 32 bytes.
txId :: BS.ByteString -> Maybe TxId
txId :: ByteString -> Maybe TxId
txId !ByteString
bs
  | ByteString -> Int
BS.length ByteString
bs Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
txIdLen = TxId -> Maybe TxId
forall a. a -> Maybe a
Just (TxId -> Maybe TxId) -> TxId -> Maybe TxId
forall a b. (a -> b) -> a -> b
$! ByteString -> TxId
TxId ByteString
bs
  | Bool
otherwise               = Maybe TxId
forall a. Maybe a
Nothing
{-# INLINABLE txId #-}

-- | Extract the underlying 'BS.ByteString' from a 'TxId'.
unTxId :: TxId -> BS.ByteString
unTxId :: TxId -> ByteString
unTxId (TxId ByteString
bs) = ByteString
bs
{-# INLINE unTxId #-}

-- | A transaction outpoint (txid + output index).
--
-- Identifies a specific output of a transaction.
data Outpoint = Outpoint
  { Outpoint -> TxId
outpointTxId :: {-# UNPACK #-} !TxId
  , Outpoint -> Word32
outpointVout :: {-# UNPACK #-} !Word32
  }
  deriving stock (Outpoint -> Outpoint -> Bool
(Outpoint -> Outpoint -> Bool)
-> (Outpoint -> Outpoint -> Bool) -> Eq Outpoint
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Outpoint -> Outpoint -> Bool
== :: Outpoint -> Outpoint -> Bool
$c/= :: Outpoint -> Outpoint -> Bool
/= :: Outpoint -> Outpoint -> Bool
Eq, Eq Outpoint
Eq Outpoint =>
(Outpoint -> Outpoint -> Ordering)
-> (Outpoint -> Outpoint -> Bool)
-> (Outpoint -> Outpoint -> Bool)
-> (Outpoint -> Outpoint -> Bool)
-> (Outpoint -> Outpoint -> Bool)
-> (Outpoint -> Outpoint -> Outpoint)
-> (Outpoint -> Outpoint -> Outpoint)
-> Ord Outpoint
Outpoint -> Outpoint -> Bool
Outpoint -> Outpoint -> Ordering
Outpoint -> Outpoint -> Outpoint
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 :: Outpoint -> Outpoint -> Ordering
compare :: Outpoint -> Outpoint -> Ordering
$c< :: Outpoint -> Outpoint -> Bool
< :: Outpoint -> Outpoint -> Bool
$c<= :: Outpoint -> Outpoint -> Bool
<= :: Outpoint -> Outpoint -> Bool
$c> :: Outpoint -> Outpoint -> Bool
> :: Outpoint -> Outpoint -> Bool
$c>= :: Outpoint -> Outpoint -> Bool
>= :: Outpoint -> Outpoint -> Bool
$cmax :: Outpoint -> Outpoint -> Outpoint
max :: Outpoint -> Outpoint -> Outpoint
$cmin :: Outpoint -> Outpoint -> Outpoint
min :: Outpoint -> Outpoint -> Outpoint
Ord, Int -> Outpoint -> ShowS
[Outpoint] -> ShowS
Outpoint -> String
(Int -> Outpoint -> ShowS)
-> (Outpoint -> String) -> ([Outpoint] -> ShowS) -> Show Outpoint
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Outpoint -> ShowS
showsPrec :: Int -> Outpoint -> ShowS
$cshow :: Outpoint -> String
show :: Outpoint -> String
$cshowList :: [Outpoint] -> ShowS
showList :: [Outpoint] -> ShowS
Show, (forall x. Outpoint -> Rep Outpoint x)
-> (forall x. Rep Outpoint x -> Outpoint) -> Generic Outpoint
forall x. Rep Outpoint x -> Outpoint
forall x. Outpoint -> Rep Outpoint x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Outpoint -> Rep Outpoint x
from :: forall x. Outpoint -> Rep Outpoint x
$cto :: forall x. Rep Outpoint x -> Outpoint
to :: forall x. Rep Outpoint x -> Outpoint
Generic)

instance NFData Outpoint

-- | A script pubkey (output script).
--
-- Variable length; used in shutdown messages, closing transactions, etc.
newtype ScriptPubKey = ScriptPubKey BS.ByteString
  deriving stock (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)
  deriving newtype ScriptPubKey -> ()
(ScriptPubKey -> ()) -> NFData ScriptPubKey
forall a. (a -> ()) -> NFData a
$crnf :: ScriptPubKey -> ()
rnf :: ScriptPubKey -> ()
NFData

-- | Construct a 'ScriptPubKey' from a 'BS.ByteString'.
--
-- Accepts any length; validation of script structure is left to higher
-- layers.
scriptPubKey :: BS.ByteString -> ScriptPubKey
scriptPubKey :: ByteString -> ScriptPubKey
scriptPubKey = ByteString -> ScriptPubKey
ScriptPubKey
{-# INLINE scriptPubKey #-}

-- | Extract the underlying 'BS.ByteString' from a 'ScriptPubKey'.
unScriptPubKey :: ScriptPubKey -> BS.ByteString
unScriptPubKey :: ScriptPubKey -> ByteString
unScriptPubKey (ScriptPubKey ByteString
bs) = ByteString
bs
{-# INLINE unScriptPubKey #-}

-- chain types -----------------------------------------------------------------

-- | A 32-byte chain hash.
--
-- Identifies the blockchain (typically the genesis block hash).
-- Used in @open_channel@ to specify which chain the channel will reside on.
newtype ChainHash = ChainHash BS.ByteString
  deriving stock (ChainHash -> ChainHash -> Bool
(ChainHash -> ChainHash -> Bool)
-> (ChainHash -> ChainHash -> Bool) -> Eq ChainHash
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ChainHash -> ChainHash -> Bool
== :: ChainHash -> ChainHash -> Bool
$c/= :: ChainHash -> ChainHash -> Bool
/= :: ChainHash -> ChainHash -> Bool
Eq, Eq ChainHash
Eq ChainHash =>
(ChainHash -> ChainHash -> Ordering)
-> (ChainHash -> ChainHash -> Bool)
-> (ChainHash -> ChainHash -> Bool)
-> (ChainHash -> ChainHash -> Bool)
-> (ChainHash -> ChainHash -> Bool)
-> (ChainHash -> ChainHash -> ChainHash)
-> (ChainHash -> ChainHash -> ChainHash)
-> Ord ChainHash
ChainHash -> ChainHash -> Bool
ChainHash -> ChainHash -> Ordering
ChainHash -> ChainHash -> ChainHash
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 :: ChainHash -> ChainHash -> Ordering
compare :: ChainHash -> ChainHash -> Ordering
$c< :: ChainHash -> ChainHash -> Bool
< :: ChainHash -> ChainHash -> Bool
$c<= :: ChainHash -> ChainHash -> Bool
<= :: ChainHash -> ChainHash -> Bool
$c> :: ChainHash -> ChainHash -> Bool
> :: ChainHash -> ChainHash -> Bool
$c>= :: ChainHash -> ChainHash -> Bool
>= :: ChainHash -> ChainHash -> Bool
$cmax :: ChainHash -> ChainHash -> ChainHash
max :: ChainHash -> ChainHash -> ChainHash
$cmin :: ChainHash -> ChainHash -> ChainHash
min :: ChainHash -> ChainHash -> ChainHash
Ord, Int -> ChainHash -> ShowS
[ChainHash] -> ShowS
ChainHash -> String
(Int -> ChainHash -> ShowS)
-> (ChainHash -> String)
-> ([ChainHash] -> ShowS)
-> Show ChainHash
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ChainHash -> ShowS
showsPrec :: Int -> ChainHash -> ShowS
$cshow :: ChainHash -> String
show :: ChainHash -> String
$cshowList :: [ChainHash] -> ShowS
showList :: [ChainHash] -> ShowS
Show, (forall x. ChainHash -> Rep ChainHash x)
-> (forall x. Rep ChainHash x -> ChainHash) -> Generic ChainHash
forall x. Rep ChainHash x -> ChainHash
forall x. ChainHash -> Rep ChainHash x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ChainHash -> Rep ChainHash x
from :: forall x. ChainHash -> Rep ChainHash x
$cto :: forall x. Rep ChainHash x -> ChainHash
to :: forall x. Rep ChainHash x -> ChainHash
Generic)
  deriving newtype ChainHash -> ()
(ChainHash -> ()) -> NFData ChainHash
forall a. (a -> ()) -> NFData a
$crnf :: ChainHash -> ()
rnf :: ChainHash -> ()
NFData

-- | Construct a 'ChainHash' from a 32-byte 'BS.ByteString'.
--
-- Returns 'Nothing' if the input is not exactly 32 bytes.
chainHash :: BS.ByteString -> Maybe ChainHash
chainHash :: ByteString -> Maybe ChainHash
chainHash !ByteString
bs
  | ByteString -> Int
BS.length ByteString
bs Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
chainHashLen = ChainHash -> Maybe ChainHash
forall a. a -> Maybe a
Just (ChainHash -> Maybe ChainHash) -> ChainHash -> Maybe ChainHash
forall a b. (a -> b) -> a -> b
$! ByteString -> ChainHash
ChainHash ByteString
bs
  | Bool
otherwise                    = Maybe ChainHash
forall a. Maybe a
Nothing
{-# INLINABLE chainHash #-}

-- | Extract the underlying 'BS.ByteString' from a 'ChainHash'.
unChainHash :: ChainHash -> BS.ByteString
unChainHash :: ChainHash -> ByteString
unChainHash (ChainHash ByteString
bs) = ByteString
bs
{-# INLINE unChainHash #-}

-- | A short channel identifier (8 bytes).
--
-- Encodes the block height (3 bytes), transaction index (3 bytes), and
-- output index (2 bytes) of the funding transaction output.
--
-- This is a compact representation for referencing channels in gossip
-- and routing.
data ShortChannelId = ShortChannelId
  { ShortChannelId -> Word64
scidBytes :: {-# UNPACK #-} !Word64
  }
  deriving stock (ShortChannelId -> ShortChannelId -> Bool
(ShortChannelId -> ShortChannelId -> Bool)
-> (ShortChannelId -> ShortChannelId -> Bool) -> Eq ShortChannelId
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ShortChannelId -> ShortChannelId -> Bool
== :: ShortChannelId -> ShortChannelId -> Bool
$c/= :: ShortChannelId -> ShortChannelId -> Bool
/= :: ShortChannelId -> ShortChannelId -> Bool
Eq, Eq ShortChannelId
Eq ShortChannelId =>
(ShortChannelId -> ShortChannelId -> Ordering)
-> (ShortChannelId -> ShortChannelId -> Bool)
-> (ShortChannelId -> ShortChannelId -> Bool)
-> (ShortChannelId -> ShortChannelId -> Bool)
-> (ShortChannelId -> ShortChannelId -> Bool)
-> (ShortChannelId -> ShortChannelId -> ShortChannelId)
-> (ShortChannelId -> ShortChannelId -> ShortChannelId)
-> Ord ShortChannelId
ShortChannelId -> ShortChannelId -> Bool
ShortChannelId -> ShortChannelId -> Ordering
ShortChannelId -> ShortChannelId -> ShortChannelId
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 :: ShortChannelId -> ShortChannelId -> Ordering
compare :: ShortChannelId -> ShortChannelId -> Ordering
$c< :: ShortChannelId -> ShortChannelId -> Bool
< :: ShortChannelId -> ShortChannelId -> Bool
$c<= :: ShortChannelId -> ShortChannelId -> Bool
<= :: ShortChannelId -> ShortChannelId -> Bool
$c> :: ShortChannelId -> ShortChannelId -> Bool
> :: ShortChannelId -> ShortChannelId -> Bool
$c>= :: ShortChannelId -> ShortChannelId -> Bool
>= :: ShortChannelId -> ShortChannelId -> Bool
$cmax :: ShortChannelId -> ShortChannelId -> ShortChannelId
max :: ShortChannelId -> ShortChannelId -> ShortChannelId
$cmin :: ShortChannelId -> ShortChannelId -> ShortChannelId
min :: ShortChannelId -> ShortChannelId -> ShortChannelId
Ord, Int -> ShortChannelId -> ShowS
[ShortChannelId] -> ShowS
ShortChannelId -> String
(Int -> ShortChannelId -> ShowS)
-> (ShortChannelId -> String)
-> ([ShortChannelId] -> ShowS)
-> Show ShortChannelId
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ShortChannelId -> ShowS
showsPrec :: Int -> ShortChannelId -> ShowS
$cshow :: ShortChannelId -> String
show :: ShortChannelId -> String
$cshowList :: [ShortChannelId] -> ShowS
showList :: [ShortChannelId] -> ShowS
Show, (forall x. ShortChannelId -> Rep ShortChannelId x)
-> (forall x. Rep ShortChannelId x -> ShortChannelId)
-> Generic ShortChannelId
forall x. Rep ShortChannelId x -> ShortChannelId
forall x. ShortChannelId -> Rep ShortChannelId x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ShortChannelId -> Rep ShortChannelId x
from :: forall x. ShortChannelId -> Rep ShortChannelId x
$cto :: forall x. Rep ShortChannelId x -> ShortChannelId
to :: forall x. Rep ShortChannelId x -> ShortChannelId
Generic)

instance NFData ShortChannelId

-- | Construct a 'ShortChannelId' from block height, tx index, and
-- output index.
--
-- Returns 'Nothing' if any component exceeds its maximum value:
--
-- * block height: max 16,777,215 (2^24 - 1)
-- * tx index: max 16,777,215 (2^24 - 1)
-- * output index: max 65,535 (2^16 - 1)
--
-- >>> shortChannelId 800000 1234 0
-- Just (ShortChannelId ...)
shortChannelId
  :: Word32  -- ^ Block height (24 bits max)
  -> Word32  -- ^ Transaction index (24 bits max)
  -> Word16  -- ^ Output index
  -> Maybe ShortChannelId
shortChannelId :: Word32 -> Word32 -> Word16 -> Maybe ShortChannelId
shortChannelId !Word32
blockHeight !Word32
txIndex !Word16
outputIndex
  | Word32
blockHeight Word32 -> Word32 -> Bool
forall a. Ord a => a -> a -> Bool
> Word32
0xFFFFFF = Maybe ShortChannelId
forall a. Maybe a
Nothing
  | Word32
txIndex Word32 -> Word32 -> Bool
forall a. Ord a => a -> a -> Bool
> Word32
0xFFFFFF     = Maybe ShortChannelId
forall a. Maybe a
Nothing
  | Bool
otherwise              = ShortChannelId -> Maybe ShortChannelId
forall a. a -> Maybe a
Just (ShortChannelId -> Maybe ShortChannelId)
-> ShortChannelId -> Maybe ShortChannelId
forall a b. (a -> b) -> a -> b
$! Word64 -> ShortChannelId
ShortChannelId Word64
scid
  where
    !scid :: Word64
scid = (Word32 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
blockHeight Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`unsafeShiftL` Int
40)
        Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|. (Word32 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word32
txIndex Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`unsafeShiftL` Int
16)
        Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.|. Word16 -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word16
outputIndex
{-# INLINABLE shortChannelId #-}

-- | Extract the block height from a 'ShortChannelId'.
scidBlockHeight :: ShortChannelId -> Word32
scidBlockHeight :: ShortChannelId -> Word32
scidBlockHeight (ShortChannelId !Word64
w) =
  Word64 -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64 -> Word32) -> Word64 -> Word32
forall a b. (a -> b) -> a -> b
$! (Word64
w Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
40) Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. Word64
0xFFFFFF
{-# INLINE scidBlockHeight #-}

-- | Extract the transaction index from a 'ShortChannelId'.
scidTxIndex :: ShortChannelId -> Word32
scidTxIndex :: ShortChannelId -> Word32
scidTxIndex (ShortChannelId !Word64
w) =
  Word64 -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64 -> Word32) -> Word64 -> Word32
forall a b. (a -> b) -> a -> b
$! (Word64
w Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
16) Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. Word64
0xFFFFFF
{-# INLINE scidTxIndex #-}

-- | Extract the output index from a 'ShortChannelId'.
scidOutputIndex :: ShortChannelId -> Word16
scidOutputIndex :: ShortChannelId -> Word16
scidOutputIndex (ShortChannelId !Word64
w) = Word64 -> Word16
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64 -> Word16) -> Word64 -> Word16
forall a b. (a -> b) -> a -> b
$! Word64
w Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. Word64
0xFFFF
{-# INLINE scidOutputIndex #-}

-- protocol types --------------------------------------------------------------

-- | Feature bits (variable length).
--
-- Encodes supported/required features. Even bits indicate required
-- features; odd bits indicate optional features.
newtype FeatureBits = FeatureBits BS.ByteString
  deriving stock (FeatureBits -> FeatureBits -> Bool
(FeatureBits -> FeatureBits -> Bool)
-> (FeatureBits -> FeatureBits -> Bool) -> Eq FeatureBits
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: FeatureBits -> FeatureBits -> Bool
== :: FeatureBits -> FeatureBits -> Bool
$c/= :: FeatureBits -> FeatureBits -> Bool
/= :: FeatureBits -> FeatureBits -> Bool
Eq, Eq FeatureBits
Eq FeatureBits =>
(FeatureBits -> FeatureBits -> Ordering)
-> (FeatureBits -> FeatureBits -> Bool)
-> (FeatureBits -> FeatureBits -> Bool)
-> (FeatureBits -> FeatureBits -> Bool)
-> (FeatureBits -> FeatureBits -> Bool)
-> (FeatureBits -> FeatureBits -> FeatureBits)
-> (FeatureBits -> FeatureBits -> FeatureBits)
-> Ord FeatureBits
FeatureBits -> FeatureBits -> Bool
FeatureBits -> FeatureBits -> Ordering
FeatureBits -> FeatureBits -> FeatureBits
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 :: FeatureBits -> FeatureBits -> Ordering
compare :: FeatureBits -> FeatureBits -> Ordering
$c< :: FeatureBits -> FeatureBits -> Bool
< :: FeatureBits -> FeatureBits -> Bool
$c<= :: FeatureBits -> FeatureBits -> Bool
<= :: FeatureBits -> FeatureBits -> Bool
$c> :: FeatureBits -> FeatureBits -> Bool
> :: FeatureBits -> FeatureBits -> Bool
$c>= :: FeatureBits -> FeatureBits -> Bool
>= :: FeatureBits -> FeatureBits -> Bool
$cmax :: FeatureBits -> FeatureBits -> FeatureBits
max :: FeatureBits -> FeatureBits -> FeatureBits
$cmin :: FeatureBits -> FeatureBits -> FeatureBits
min :: FeatureBits -> FeatureBits -> FeatureBits
Ord, Int -> FeatureBits -> ShowS
[FeatureBits] -> ShowS
FeatureBits -> String
(Int -> FeatureBits -> ShowS)
-> (FeatureBits -> String)
-> ([FeatureBits] -> ShowS)
-> Show FeatureBits
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> FeatureBits -> ShowS
showsPrec :: Int -> FeatureBits -> ShowS
$cshow :: FeatureBits -> String
show :: FeatureBits -> String
$cshowList :: [FeatureBits] -> ShowS
showList :: [FeatureBits] -> ShowS
Show, (forall x. FeatureBits -> Rep FeatureBits x)
-> (forall x. Rep FeatureBits x -> FeatureBits)
-> Generic FeatureBits
forall x. Rep FeatureBits x -> FeatureBits
forall x. FeatureBits -> Rep FeatureBits x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. FeatureBits -> Rep FeatureBits x
from :: forall x. FeatureBits -> Rep FeatureBits x
$cto :: forall x. Rep FeatureBits x -> FeatureBits
to :: forall x. Rep FeatureBits x -> FeatureBits
Generic)
  deriving newtype FeatureBits -> ()
(FeatureBits -> ()) -> NFData FeatureBits
forall a. (a -> ()) -> NFData a
$crnf :: FeatureBits -> ()
rnf :: FeatureBits -> ()
NFData

-- | Construct 'FeatureBits' from a 'BS.ByteString'.
--
-- Accepts any length; feature bit parsing is left to higher layers.
featureBits :: BS.ByteString -> FeatureBits
featureBits :: ByteString -> FeatureBits
featureBits = ByteString -> FeatureBits
FeatureBits
{-# INLINE featureBits #-}

-- | Extract the underlying 'BS.ByteString' from 'FeatureBits'.
unFeatureBits :: FeatureBits -> BS.ByteString
unFeatureBits :: FeatureBits -> ByteString
unFeatureBits (FeatureBits ByteString
bs) = ByteString
bs
{-# INLINE unFeatureBits #-}

-- | A 1366-byte onion routing packet.
--
-- Contains encrypted routing information for HTLC forwarding, as
-- specified in BOLT #4.
newtype OnionPacket = OnionPacket BS.ByteString
  deriving stock (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, Eq OnionPacket
Eq OnionPacket =>
(OnionPacket -> OnionPacket -> Ordering)
-> (OnionPacket -> OnionPacket -> Bool)
-> (OnionPacket -> OnionPacket -> Bool)
-> (OnionPacket -> OnionPacket -> Bool)
-> (OnionPacket -> OnionPacket -> Bool)
-> (OnionPacket -> OnionPacket -> OnionPacket)
-> (OnionPacket -> OnionPacket -> OnionPacket)
-> Ord OnionPacket
OnionPacket -> OnionPacket -> Bool
OnionPacket -> OnionPacket -> Ordering
OnionPacket -> OnionPacket -> OnionPacket
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 :: OnionPacket -> OnionPacket -> Ordering
compare :: OnionPacket -> OnionPacket -> Ordering
$c< :: OnionPacket -> OnionPacket -> Bool
< :: OnionPacket -> OnionPacket -> Bool
$c<= :: OnionPacket -> OnionPacket -> Bool
<= :: OnionPacket -> OnionPacket -> Bool
$c> :: OnionPacket -> OnionPacket -> Bool
> :: OnionPacket -> OnionPacket -> Bool
$c>= :: OnionPacket -> OnionPacket -> Bool
>= :: OnionPacket -> OnionPacket -> Bool
$cmax :: OnionPacket -> OnionPacket -> OnionPacket
max :: OnionPacket -> OnionPacket -> OnionPacket
$cmin :: OnionPacket -> OnionPacket -> OnionPacket
min :: OnionPacket -> OnionPacket -> OnionPacket
Ord, 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)
  deriving newtype OnionPacket -> ()
(OnionPacket -> ()) -> NFData OnionPacket
forall a. (a -> ()) -> NFData a
$crnf :: OnionPacket -> ()
rnf :: OnionPacket -> ()
NFData

-- | Construct an 'OnionPacket' from a 1366-byte 'BS.ByteString'.
--
-- Returns 'Nothing' if the input is not exactly 1366 bytes.
onionPacket :: BS.ByteString -> Maybe OnionPacket
onionPacket :: ByteString -> Maybe OnionPacket
onionPacket !ByteString
bs
  | ByteString -> Int
BS.length ByteString
bs Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
onionPacketLen = OnionPacket -> Maybe OnionPacket
forall a. a -> Maybe a
Just (OnionPacket -> Maybe OnionPacket)
-> OnionPacket -> Maybe OnionPacket
forall a b. (a -> b) -> a -> b
$! ByteString -> OnionPacket
OnionPacket ByteString
bs
  | Bool
otherwise                      = Maybe OnionPacket
forall a. Maybe a
Nothing
{-# INLINABLE onionPacket #-}

-- | Extract the underlying 'BS.ByteString' from an 'OnionPacket'.
unOnionPacket :: OnionPacket -> BS.ByteString
unOnionPacket :: OnionPacket -> ByteString
unOnionPacket (OnionPacket ByteString
bs) = ByteString
bs
{-# INLINE unOnionPacket #-}