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

-- |
-- Module: Lightning.Protocol.BOLT1.Prim
-- Copyright: (c) 2025 Jared Tobin
-- License: MIT
-- Maintainer: Jared Tobin <jared@ppad.tech>
--
-- Fundamental types and primitive encodings for BOLT #1.

module Lightning.Protocol.BOLT1.Prim (
  -- * Fixed-size byte types
    ChainHash
  , chain_hash
  , un_chain_hash
  , ChannelId
  , channel_id
  , un_channel_id
  , all_channels
  , Signature
  , signature
  , un_signature
  , Point
  , point
  , un_point
  , PaymentHash
  , payment_hash
  , un_payment_hash
  , PaymentPreimage
  , payment_preimage
  , un_payment_preimage
  , PerCommitmentSecret
  , per_commitment_secret
  , un_per_commitment_secret

  -- * Short channel identifiers
  , ShortChannelId(..)
  , short_channel_id
  , scid_block_height
  , scid_tx_index
  , scid_output_index

  -- * Amounts
  , Satoshi
  , satoshi
  , un_satoshi
  , max_satoshi
  , MilliSatoshi
  , milli_satoshi
  , un_milli_satoshi
  , max_milli_satoshi
  , sat_to_msat
  , msat_to_sat
  , add_sat
  , sub_sat
  , add_msat
  , sub_msat

  -- * Unsigned integers
  , encode_u16
  , encode_u32
  , encode_u64
  , decode_u16
  , decode_u32
  , decode_u64

  -- * Signed integers
  , encode_s8
  , encode_s16
  , encode_s32
  , encode_s64
  , decode_s8
  , decode_s16
  , decode_s32
  , decode_s64

  -- * Truncated unsigned integers
  , encode_tu16
  , encode_tu32
  , encode_tu64
  , decode_tu16
  , decode_tu32
  , decode_tu64

  -- * BigSize
  , encode_bigsize
  , decode_bigsize
  , BigSizeError(..)
  , decode_bigsize_detailed

  -- * Length-prefixed bytes
  , encode_u16_prefixed
  , decode_u16_prefixed

  -- * Fixed-size field codecs
  , decode_chain_hash
  , decode_channel_id
  , decode_signature
  , decode_point
  , decode_payment_hash
  , decode_payment_preimage
  , decode_per_commitment_secret
  , encode_short_channel_id
  , decode_short_channel_id
  , encode_satoshi
  , decode_satoshi
  , encode_milli_satoshi
  , decode_milli_satoshi
  ) where

import Control.DeepSeq (NFData(..))
import Data.Bits ((.&.), (.|.), unsafeShiftL, unsafeShiftR, xor)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Internal as BI
import qualified Data.ByteString.Unsafe as BU
import Data.Int (Int8, Int16, Int32, Int64)
import Data.Word (Word8, Word16, Word32, Word64)
import Foreign.Ptr (Ptr)
import Foreign.Storable (pokeByteOff)
import GHC.Generics (Generic)

fi :: (Integral a, Num b) => a -> b
fi :: forall a b. (Integral a, Num b) => a -> b
fi = a -> b
forall a b. (Integral a, Num b) => a -> b
fromIntegral
{-# INLINE fi #-}

-- constant-time equality on equal-length bytestrings
ct_eq :: BS.ByteString -> BS.ByteString -> Bool
ct_eq :: ByteString -> ByteString -> Bool
ct_eq ByteString
a ByteString
b
  | ByteString -> Int
BS.length ByteString
a Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= ByteString -> Int
BS.length ByteString
b = Bool
False
  | Bool
otherwise = Word8 -> Int -> Bool
go Word8
0 Int
0
  where
    !n :: Int
n = ByteString -> Int
BS.length ByteString
a
    go :: Word8 -> Int -> Bool
    go :: Word8 -> Int -> Bool
go !Word8
acc !Int
i
      | Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
n    = Word8
acc Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
0
      | Bool
otherwise =
          Word8 -> Int -> Bool
go (Word8
acc Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|. (ByteString -> Int -> Word8
BU.unsafeIndex ByteString
a Int
i Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
`xor` ByteString -> Int -> Word8
BU.unsafeIndex ByteString
b Int
i))
             (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)

-- take exactly n bytes from the front of the input
take_n :: Int -> BS.ByteString -> Maybe (BS.ByteString, BS.ByteString)
take_n :: Int -> ByteString -> Maybe (ByteString, ByteString)
take_n !Int
n !ByteString
bs
  | ByteString -> Int
BS.length ByteString
bs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
n = Maybe (ByteString, ByteString)
forall a. Maybe a
Nothing
  | Bool
otherwise        = (ByteString, ByteString) -> Maybe (ByteString, ByteString)
forall a. a -> Maybe a
Just (Int -> ByteString -> ByteString
BU.unsafeTake Int
n ByteString
bs, Int -> ByteString -> ByteString
BU.unsafeDrop Int
n ByteString
bs)
{-# INLINE take_n #-}

-- fixed-size byte types ------------------------------------------------------

-- | A 32-byte chain hash, identifying the chain a channel or message
--   belongs to.
newtype ChainHash = ChainHash BS.ByteString
  deriving (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)

instance NFData ChainHash

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

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

-- | A 32-byte channel identifier.
newtype ChannelId = ChannelId BS.ByteString
  deriving (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)

instance NFData ChannelId

-- | Construct a 'ChannelId' from exactly 32 bytes.
--
--   >>> fmap (BS.length . un_channel_id) (channel_id (BS.replicate 32 1))
--   Just 32
channel_id :: BS.ByteString -> Maybe ChannelId
channel_id :: ByteString -> Maybe ChannelId
channel_id ByteString
bs
  | ByteString -> Int
BS.length ByteString
bs Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
32 = ChannelId -> Maybe ChannelId
forall a. a -> Maybe a
Just (ByteString -> ChannelId
ChannelId ByteString
bs)
  | Bool
otherwise          = Maybe ChannelId
forall a. Maybe a
Nothing
{-# INLINE channel_id #-}

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

-- | The all-zero channel identifier, which refers to all channels
--   (e.g. in connection-level @error@ and @warning@ messages).
all_channels :: ChannelId
all_channels :: ChannelId
all_channels = ByteString -> ChannelId
ChannelId (Int -> Word8 -> ByteString
BS.replicate Int
32 Word8
0x00)

-- | A 64-byte compact ECDSA signature.
newtype Signature = Signature BS.ByteString
  deriving (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)

instance NFData Signature

-- | Construct a 'Signature' from exactly 64 bytes.
--
--   >>> signature (BS.replicate 63 0x00)
--   Nothing
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
64 = Signature -> Maybe Signature
forall a. a -> Maybe a
Just (ByteString -> Signature
Signature ByteString
bs)
  | Bool
otherwise          = Maybe Signature
forall a. Maybe a
Nothing
{-# INLINE signature #-}

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

-- | A 33-byte compressed secp256k1 point.
newtype Point = Point BS.ByteString
  deriving (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)

instance NFData Point

-- | Construct a 'Point' from 33 bytes with a compressed-encoding
--   prefix (0x02 or 0x03). The curve equation is not checked; use
--   ppad-secp256k1 to parse the point if it must be on the curve.
--
--   >>> point (BS.cons 0x04 (BS.replicate 32 0x01))
--   Nothing
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
33
  , let !h :: Word8
h = ByteString -> Int -> Word8
BU.unsafeIndex ByteString
bs Int
0
  , Word8
h Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
0x02 Bool -> Bool -> Bool
|| Word8
h Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
0x03 = Point -> Maybe Point
forall a. a -> Maybe a
Just (ByteString -> Point
Point ByteString
bs)
  | Bool
otherwise              = Maybe Point
forall a. Maybe a
Nothing
{-# INLINE point #-}

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

-- | A 32-byte SHA256 payment hash.
newtype PaymentHash = PaymentHash BS.ByteString
  deriving (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)

instance NFData PaymentHash

-- | Construct a 'PaymentHash' from exactly 32 bytes.
payment_hash :: BS.ByteString -> Maybe PaymentHash
payment_hash :: ByteString -> Maybe PaymentHash
payment_hash ByteString
bs
  | ByteString -> Int
BS.length ByteString
bs Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
32 = PaymentHash -> Maybe PaymentHash
forall a. a -> Maybe a
Just (ByteString -> PaymentHash
PaymentHash ByteString
bs)
  | Bool
otherwise          = Maybe PaymentHash
forall a. Maybe a
Nothing
{-# INLINE payment_hash #-}

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

-- | A 32-byte payment preimage.
--
--   This is secret material: its 'Show' instance is redacted and its
--   'Eq' instance runs in constant time.
newtype PaymentPreimage = PaymentPreimage BS.ByteString

instance Eq PaymentPreimage where
  PaymentPreimage ByteString
a == :: PaymentPreimage -> PaymentPreimage -> Bool
== PaymentPreimage ByteString
b = ByteString -> ByteString -> Bool
ct_eq ByteString
a ByteString
b

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

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

-- | Construct a 'PaymentPreimage' from exactly 32 bytes.
payment_preimage :: BS.ByteString -> Maybe PaymentPreimage
payment_preimage :: ByteString -> Maybe PaymentPreimage
payment_preimage ByteString
bs
  | ByteString -> Int
BS.length ByteString
bs Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
32 = PaymentPreimage -> Maybe PaymentPreimage
forall a. a -> Maybe a
Just (ByteString -> PaymentPreimage
PaymentPreimage ByteString
bs)
  | Bool
otherwise          = Maybe PaymentPreimage
forall a. Maybe a
Nothing
{-# INLINE payment_preimage #-}

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

-- | A 32-byte per-commitment secret.
--
--   This is secret material: its 'Show' instance is redacted and its
--   'Eq' instance runs in constant time.
newtype PerCommitmentSecret = PerCommitmentSecret BS.ByteString

instance Eq PerCommitmentSecret where
  PerCommitmentSecret ByteString
a == :: PerCommitmentSecret -> PerCommitmentSecret -> Bool
== PerCommitmentSecret ByteString
b = ByteString -> ByteString -> Bool
ct_eq ByteString
a ByteString
b

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

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

-- | Construct a 'PerCommitmentSecret' from exactly 32 bytes.
per_commitment_secret :: BS.ByteString -> Maybe PerCommitmentSecret
per_commitment_secret :: ByteString -> Maybe PerCommitmentSecret
per_commitment_secret ByteString
bs
  | ByteString -> Int
BS.length ByteString
bs Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
32 = PerCommitmentSecret -> Maybe PerCommitmentSecret
forall a. a -> Maybe a
Just (ByteString -> PerCommitmentSecret
PerCommitmentSecret ByteString
bs)
  | Bool
otherwise          = Maybe PerCommitmentSecret
forall a. Maybe a
Nothing
{-# INLINE per_commitment_secret #-}

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

-- short channel identifiers --------------------------------------------------

-- | A short channel identifier: block height (3 bytes), transaction
--   index (3 bytes) and output index (2 bytes), packed big-endian into
--   a 'Word64'. Every 'Word64' is a valid packing.
newtype ShortChannelId = ShortChannelId Word64
  deriving (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 its components. Fails if the
--   block height or transaction index exceed 24 bits.
--
--   >>> short_channel_id 539268 845 1
--   Just (ShortChannelId 592931436542885889)
short_channel_id
  :: Word32 -- ^ block height
  -> Word32 -- ^ transaction index
  -> Word16 -- ^ output index
  -> Maybe ShortChannelId
short_channel_id :: Word32 -> Word32 -> Word16 -> Maybe ShortChannelId
short_channel_id Word32
h Word32
t Word16
o
  | Word32
h Word32 -> Word32 -> Bool
forall a. Ord a => a -> a -> Bool
> Word32
0xFFFFFF = Maybe ShortChannelId
forall a. Maybe a
Nothing
  | Word32
t 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 -> ShortChannelId) -> Word64 -> ShortChannelId
forall a b. (a -> b) -> a -> b
$!
          (Word32 -> Word64
forall a b. (Integral a, Num b) => a -> b
fi Word32
h 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
fi Word32
t 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
fi Word16
o
{-# INLINE short_channel_id #-}

-- | The block height of a 'ShortChannelId'.
scid_block_height :: ShortChannelId -> Word32
scid_block_height :: ShortChannelId -> Word32
scid_block_height (ShortChannelId Word64
w) = Word64 -> Word32
forall a b. (Integral a, Num b) => a -> b
fi (Word64
w Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
40)
{-# INLINE scid_block_height #-}

-- | The transaction index of a 'ShortChannelId'.
scid_tx_index :: ShortChannelId -> Word32
scid_tx_index :: ShortChannelId -> Word32
scid_tx_index (ShortChannelId Word64
w) = Word64 -> Word32
forall a b. (Integral a, Num b) => a -> b
fi ((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 scid_tx_index #-}

-- | The output index of a 'ShortChannelId'.
scid_output_index :: ShortChannelId -> Word16
scid_output_index :: ShortChannelId -> Word16
scid_output_index (ShortChannelId Word64
w) = Word64 -> Word16
forall a b. (Integral a, Num b) => a -> b
fi (Word64
w Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. Word64
0xFFFF)
{-# INLINE scid_output_index #-}

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

-- | An amount in satoshis, at most 21 million BTC ('max_satoshi').
newtype Satoshi = Satoshi Word64
  deriving (Satoshi -> Satoshi -> Bool
(Satoshi -> Satoshi -> Bool)
-> (Satoshi -> Satoshi -> Bool) -> Eq Satoshi
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Satoshi -> Satoshi -> Bool
== :: Satoshi -> Satoshi -> Bool
$c/= :: Satoshi -> Satoshi -> Bool
/= :: Satoshi -> Satoshi -> Bool
Eq, Eq Satoshi
Eq Satoshi =>
(Satoshi -> Satoshi -> Ordering)
-> (Satoshi -> Satoshi -> Bool)
-> (Satoshi -> Satoshi -> Bool)
-> (Satoshi -> Satoshi -> Bool)
-> (Satoshi -> Satoshi -> Bool)
-> (Satoshi -> Satoshi -> Satoshi)
-> (Satoshi -> Satoshi -> Satoshi)
-> Ord Satoshi
Satoshi -> Satoshi -> Bool
Satoshi -> Satoshi -> Ordering
Satoshi -> Satoshi -> Satoshi
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 :: Satoshi -> Satoshi -> Ordering
compare :: Satoshi -> Satoshi -> Ordering
$c< :: Satoshi -> Satoshi -> Bool
< :: Satoshi -> Satoshi -> Bool
$c<= :: Satoshi -> Satoshi -> Bool
<= :: Satoshi -> Satoshi -> Bool
$c> :: Satoshi -> Satoshi -> Bool
> :: Satoshi -> Satoshi -> Bool
$c>= :: Satoshi -> Satoshi -> Bool
>= :: Satoshi -> Satoshi -> Bool
$cmax :: Satoshi -> Satoshi -> Satoshi
max :: Satoshi -> Satoshi -> Satoshi
$cmin :: Satoshi -> Satoshi -> Satoshi
min :: Satoshi -> Satoshi -> Satoshi
Ord, Int -> Satoshi -> ShowS
[Satoshi] -> ShowS
Satoshi -> String
(Int -> Satoshi -> ShowS)
-> (Satoshi -> String) -> ([Satoshi] -> ShowS) -> Show Satoshi
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Satoshi -> ShowS
showsPrec :: Int -> Satoshi -> ShowS
$cshow :: Satoshi -> String
show :: Satoshi -> String
$cshowList :: [Satoshi] -> ShowS
showList :: [Satoshi] -> ShowS
Show, (forall x. Satoshi -> Rep Satoshi x)
-> (forall x. Rep Satoshi x -> Satoshi) -> Generic Satoshi
forall x. Rep Satoshi x -> Satoshi
forall x. Satoshi -> Rep Satoshi x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Satoshi -> Rep Satoshi x
from :: forall x. Satoshi -> Rep Satoshi x
$cto :: forall x. Rep Satoshi x -> Satoshi
to :: forall x. Rep Satoshi x -> Satoshi
Generic)

instance NFData Satoshi

-- | An amount in millisatoshis, at most 21 million BTC
--   ('max_milli_satoshi').
newtype MilliSatoshi = MilliSatoshi Word64
  deriving (MilliSatoshi -> MilliSatoshi -> Bool
(MilliSatoshi -> MilliSatoshi -> Bool)
-> (MilliSatoshi -> MilliSatoshi -> Bool) -> Eq MilliSatoshi
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: MilliSatoshi -> MilliSatoshi -> Bool
== :: MilliSatoshi -> MilliSatoshi -> Bool
$c/= :: MilliSatoshi -> MilliSatoshi -> Bool
/= :: MilliSatoshi -> MilliSatoshi -> Bool
Eq, Eq MilliSatoshi
Eq MilliSatoshi =>
(MilliSatoshi -> MilliSatoshi -> Ordering)
-> (MilliSatoshi -> MilliSatoshi -> Bool)
-> (MilliSatoshi -> MilliSatoshi -> Bool)
-> (MilliSatoshi -> MilliSatoshi -> Bool)
-> (MilliSatoshi -> MilliSatoshi -> Bool)
-> (MilliSatoshi -> MilliSatoshi -> MilliSatoshi)
-> (MilliSatoshi -> MilliSatoshi -> MilliSatoshi)
-> Ord MilliSatoshi
MilliSatoshi -> MilliSatoshi -> Bool
MilliSatoshi -> MilliSatoshi -> Ordering
MilliSatoshi -> MilliSatoshi -> MilliSatoshi
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 :: MilliSatoshi -> MilliSatoshi -> Ordering
compare :: MilliSatoshi -> MilliSatoshi -> Ordering
$c< :: MilliSatoshi -> MilliSatoshi -> Bool
< :: MilliSatoshi -> MilliSatoshi -> Bool
$c<= :: MilliSatoshi -> MilliSatoshi -> Bool
<= :: MilliSatoshi -> MilliSatoshi -> Bool
$c> :: MilliSatoshi -> MilliSatoshi -> Bool
> :: MilliSatoshi -> MilliSatoshi -> Bool
$c>= :: MilliSatoshi -> MilliSatoshi -> Bool
>= :: MilliSatoshi -> MilliSatoshi -> Bool
$cmax :: MilliSatoshi -> MilliSatoshi -> MilliSatoshi
max :: MilliSatoshi -> MilliSatoshi -> MilliSatoshi
$cmin :: MilliSatoshi -> MilliSatoshi -> MilliSatoshi
min :: MilliSatoshi -> MilliSatoshi -> MilliSatoshi
Ord, Int -> MilliSatoshi -> ShowS
[MilliSatoshi] -> ShowS
MilliSatoshi -> String
(Int -> MilliSatoshi -> ShowS)
-> (MilliSatoshi -> String)
-> ([MilliSatoshi] -> ShowS)
-> Show MilliSatoshi
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> MilliSatoshi -> ShowS
showsPrec :: Int -> MilliSatoshi -> ShowS
$cshow :: MilliSatoshi -> String
show :: MilliSatoshi -> String
$cshowList :: [MilliSatoshi] -> ShowS
showList :: [MilliSatoshi] -> ShowS
Show, (forall x. MilliSatoshi -> Rep MilliSatoshi x)
-> (forall x. Rep MilliSatoshi x -> MilliSatoshi)
-> Generic MilliSatoshi
forall x. Rep MilliSatoshi x -> MilliSatoshi
forall x. MilliSatoshi -> Rep MilliSatoshi x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. MilliSatoshi -> Rep MilliSatoshi x
from :: forall x. MilliSatoshi -> Rep MilliSatoshi x
$cto :: forall x. Rep MilliSatoshi x -> MilliSatoshi
to :: forall x. Rep MilliSatoshi x -> MilliSatoshi
Generic)

instance NFData MilliSatoshi

-- | 21 million BTC, in satoshis (0x000775f05a074000).
max_satoshi :: Satoshi
max_satoshi :: Satoshi
max_satoshi = Word64 -> Satoshi
Satoshi Word64
0x000775f05a074000

-- | 21 million BTC, in millisatoshis (0x1d24b2dfac520000).
max_milli_satoshi :: MilliSatoshi
max_milli_satoshi :: MilliSatoshi
max_milli_satoshi = Word64 -> MilliSatoshi
MilliSatoshi Word64
0x1d24b2dfac520000

-- | Construct a 'Satoshi' amount, failing above 21 million BTC.
--
--   >>> satoshi 1000
--   Just (Satoshi 1000)
--   >>> satoshi 0xffffffffffffffff
--   Nothing
satoshi :: Word64 -> Maybe Satoshi
satoshi :: Word64 -> Maybe Satoshi
satoshi Word64
w
  | Word64
w Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
<= Word64
0x000775f05a074000 = Satoshi -> Maybe Satoshi
forall a. a -> Maybe a
Just (Word64 -> Satoshi
Satoshi Word64
w)
  | Bool
otherwise               = Maybe Satoshi
forall a. Maybe a
Nothing
{-# INLINE satoshi #-}

-- | The number of satoshis.
un_satoshi :: Satoshi -> Word64
un_satoshi :: Satoshi -> Word64
un_satoshi (Satoshi Word64
w) = Word64
w
{-# INLINE un_satoshi #-}

-- | Construct a 'MilliSatoshi' amount, failing above 21 million BTC.
milli_satoshi :: Word64 -> Maybe MilliSatoshi
milli_satoshi :: Word64 -> Maybe MilliSatoshi
milli_satoshi Word64
w
  | Word64
w Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
<= Word64
0x1d24b2dfac520000 = MilliSatoshi -> Maybe MilliSatoshi
forall a. a -> Maybe a
Just (Word64 -> MilliSatoshi
MilliSatoshi Word64
w)
  | Bool
otherwise               = Maybe MilliSatoshi
forall a. Maybe a
Nothing
{-# INLINE milli_satoshi #-}

-- | The number of millisatoshis.
un_milli_satoshi :: MilliSatoshi -> Word64
un_milli_satoshi :: MilliSatoshi -> Word64
un_milli_satoshi (MilliSatoshi Word64
w) = Word64
w
{-# INLINE un_milli_satoshi #-}

-- | Convert satoshis to millisatoshis. Total, since the bounds
--   correspond.
--
--   >>> fmap sat_to_msat (satoshi 5)
--   Just (MilliSatoshi 5000)
sat_to_msat :: Satoshi -> MilliSatoshi
sat_to_msat :: Satoshi -> MilliSatoshi
sat_to_msat (Satoshi Word64
s) = Word64 -> MilliSatoshi
MilliSatoshi (Word64
s Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
* Word64
1000)
{-# INLINE sat_to_msat #-}

-- | Convert millisatoshis to satoshis, rounding down.
msat_to_sat :: MilliSatoshi -> Satoshi
msat_to_sat :: MilliSatoshi -> Satoshi
msat_to_sat (MilliSatoshi Word64
m) = Word64 -> Satoshi
Satoshi (Word64
m Word64 -> Word64 -> Word64
forall a. Integral a => a -> a -> a
`quot` Word64
1000)
{-# INLINE msat_to_sat #-}

-- | Add two amounts, failing above 21 million BTC. (Amounts are
--   bounded well below 2^63, so the sum can't wrap.)
add_sat :: Satoshi -> Satoshi -> Maybe Satoshi
add_sat :: Satoshi -> Satoshi -> Maybe Satoshi
add_sat (Satoshi Word64
a) (Satoshi Word64
b) = Word64 -> Maybe Satoshi
satoshi (Word64
a Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
+ Word64
b)
{-# INLINE add_sat #-}

-- | Subtract the second amount from the first, failing if the result
--   would be negative.
sub_sat :: Satoshi -> Satoshi -> Maybe Satoshi
sub_sat :: Satoshi -> Satoshi -> Maybe Satoshi
sub_sat (Satoshi Word64
a) (Satoshi Word64
b)
  | Word64
b Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
> Word64
a     = Maybe Satoshi
forall a. Maybe a
Nothing
  | Bool
otherwise = Satoshi -> Maybe Satoshi
forall a. a -> Maybe a
Just (Word64 -> Satoshi
Satoshi (Word64
a Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
- Word64
b))
{-# INLINE sub_sat #-}

-- | Add two amounts, failing above 21 million BTC.
add_msat :: MilliSatoshi -> MilliSatoshi -> Maybe MilliSatoshi
add_msat :: MilliSatoshi -> MilliSatoshi -> Maybe MilliSatoshi
add_msat (MilliSatoshi Word64
a) (MilliSatoshi Word64
b) = Word64 -> Maybe MilliSatoshi
milli_satoshi (Word64
a Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
+ Word64
b)
{-# INLINE add_msat #-}

-- | Subtract the second amount from the first, failing if the result
--   would be negative.
sub_msat :: MilliSatoshi -> MilliSatoshi -> Maybe MilliSatoshi
sub_msat :: MilliSatoshi -> MilliSatoshi -> Maybe MilliSatoshi
sub_msat (MilliSatoshi Word64
a) (MilliSatoshi Word64
b)
  | Word64
b Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
> Word64
a     = Maybe MilliSatoshi
forall a. Maybe a
Nothing
  | Bool
otherwise = MilliSatoshi -> Maybe MilliSatoshi
forall a. a -> Maybe a
Just (Word64 -> MilliSatoshi
MilliSatoshi (Word64
a Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
- Word64
b))
{-# INLINE sub_msat #-}

-- unsigned integers ----------------------------------------------------------

poke8 :: Ptr Word8 -> Int -> Word8 -> IO ()
poke8 :: Ptr Word8 -> Int -> Word8 -> IO ()
poke8 = Ptr Word8 -> Int -> Word8 -> IO ()
forall b. Ptr b -> Int -> Word8 -> IO ()
forall a b. Storable a => Ptr b -> Int -> a -> IO ()
pokeByteOff
{-# INLINE poke8 #-}

-- | Encode a 'Word16' as 2 big-endian bytes.
--
--   >>> encode_u16 0x0102
--   "\SOH\STX"
encode_u16 :: Word16 -> BS.ByteString
encode_u16 :: Word16 -> ByteString
encode_u16 Word16
w = Int -> (Ptr Word8 -> IO ()) -> ByteString
BI.unsafeCreate Int
2 ((Ptr Word8 -> IO ()) -> ByteString)
-> (Ptr Word8 -> IO ()) -> ByteString
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
p -> do
  Ptr Word8 -> Int -> Word8 -> IO ()
poke8 Ptr Word8
p Int
0 (Word16 -> Word8
forall a b. (Integral a, Num b) => a -> b
fi (Word16
w Word16 -> Int -> Word16
forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
8))
  Ptr Word8 -> Int -> Word8 -> IO ()
poke8 Ptr Word8
p Int
1 (Word16 -> Word8
forall a b. (Integral a, Num b) => a -> b
fi Word16
w)
{-# INLINE encode_u16 #-}

-- | Encode a 'Word32' as 4 big-endian bytes.
encode_u32 :: Word32 -> BS.ByteString
encode_u32 :: Word32 -> ByteString
encode_u32 Word32
w = Int -> (Ptr Word8 -> IO ()) -> ByteString
BI.unsafeCreate Int
4 ((Ptr Word8 -> IO ()) -> ByteString)
-> (Ptr Word8 -> IO ()) -> ByteString
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
p -> do
  Ptr Word8 -> Int -> Word8 -> IO ()
poke8 Ptr Word8
p Int
0 (Word32 -> Word8
forall a b. (Integral a, Num b) => a -> b
fi (Word32
w Word32 -> Int -> Word32
forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
24))
  Ptr Word8 -> Int -> Word8 -> IO ()
poke8 Ptr Word8
p Int
1 (Word32 -> Word8
forall a b. (Integral a, Num b) => a -> b
fi (Word32
w Word32 -> Int -> Word32
forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
16))
  Ptr Word8 -> Int -> Word8 -> IO ()
poke8 Ptr Word8
p Int
2 (Word32 -> Word8
forall a b. (Integral a, Num b) => a -> b
fi (Word32
w Word32 -> Int -> Word32
forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
8))
  Ptr Word8 -> Int -> Word8 -> IO ()
poke8 Ptr Word8
p Int
3 (Word32 -> Word8
forall a b. (Integral a, Num b) => a -> b
fi Word32
w)
{-# INLINE encode_u32 #-}

-- | Encode a 'Word64' as 8 big-endian bytes.
encode_u64 :: Word64 -> BS.ByteString
encode_u64 :: Word64 -> ByteString
encode_u64 Word64
w = Int -> (Ptr Word8 -> IO ()) -> ByteString
BI.unsafeCreate Int
8 ((Ptr Word8 -> IO ()) -> ByteString)
-> (Ptr Word8 -> IO ()) -> ByteString
forall a b. (a -> b) -> a -> b
$ \Ptr Word8
p -> do
  Ptr Word8 -> Int -> Word8 -> IO ()
poke8 Ptr Word8
p Int
0 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fi (Word64
w Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
56))
  Ptr Word8 -> Int -> Word8 -> IO ()
poke8 Ptr Word8
p Int
1 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fi (Word64
w Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
48))
  Ptr Word8 -> Int -> Word8 -> IO ()
poke8 Ptr Word8
p Int
2 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fi (Word64
w Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
40))
  Ptr Word8 -> Int -> Word8 -> IO ()
poke8 Ptr Word8
p Int
3 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fi (Word64
w Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
32))
  Ptr Word8 -> Int -> Word8 -> IO ()
poke8 Ptr Word8
p Int
4 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fi (Word64
w Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
24))
  Ptr Word8 -> Int -> Word8 -> IO ()
poke8 Ptr Word8
p Int
5 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fi (Word64
w Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
16))
  Ptr Word8 -> Int -> Word8 -> IO ()
poke8 Ptr Word8
p Int
6 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fi (Word64
w Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
8))
  Ptr Word8 -> Int -> Word8 -> IO ()
poke8 Ptr Word8
p Int
7 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fi Word64
w)
{-# INLINE encode_u64 #-}

-- big-endian word from the first n bytes (n <= 8, length checked)
be :: Int -> BS.ByteString -> Word64
be :: Int -> ByteString -> Word64
be Int
n ByteString
bs = Word64 -> Int -> Word64
forall {t}. (Bits t, Num t) => t -> Int -> t
go Word64
0 Int
0
  where
    go :: t -> Int -> t
go !t
acc !Int
i
      | Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
n    = t
acc
      | Bool
otherwise =
          t -> Int -> t
go ((t
acc t -> Int -> t
forall a. Bits a => a -> Int -> a
`unsafeShiftL` Int
8) t -> t -> t
forall a. Bits a => a -> a -> a
.|. Word8 -> t
forall a b. (Integral a, Num b) => a -> b
fi (ByteString -> Int -> Word8
BU.unsafeIndex ByteString
bs Int
i)) (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
{-# INLINE be #-}

-- | Decode 2 big-endian bytes, returning the remaining input.
--
--   >>> decode_u16 "\SOH\STXrest"
--   Just (258,"rest")
decode_u16 :: BS.ByteString -> Maybe (Word16, BS.ByteString)
decode_u16 :: ByteString -> Maybe (Word16, ByteString)
decode_u16 ByteString
bs
  | ByteString -> Int
BS.length ByteString
bs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
2 = Maybe (Word16, ByteString)
forall a. Maybe a
Nothing
  | Bool
otherwise        = (Word16, ByteString) -> Maybe (Word16, ByteString)
forall a. a -> Maybe a
Just (Word64 -> Word16
forall a b. (Integral a, Num b) => a -> b
fi (Int -> ByteString -> Word64
be Int
2 ByteString
bs), Int -> ByteString -> ByteString
BU.unsafeDrop Int
2 ByteString
bs)
{-# INLINE decode_u16 #-}

-- | Decode 4 big-endian bytes, returning the remaining input.
decode_u32 :: BS.ByteString -> Maybe (Word32, BS.ByteString)
decode_u32 :: ByteString -> Maybe (Word32, ByteString)
decode_u32 ByteString
bs
  | ByteString -> Int
BS.length ByteString
bs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
4 = Maybe (Word32, ByteString)
forall a. Maybe a
Nothing
  | Bool
otherwise        = (Word32, ByteString) -> Maybe (Word32, ByteString)
forall a. a -> Maybe a
Just (Word64 -> Word32
forall a b. (Integral a, Num b) => a -> b
fi (Int -> ByteString -> Word64
be Int
4 ByteString
bs), Int -> ByteString -> ByteString
BU.unsafeDrop Int
4 ByteString
bs)
{-# INLINE decode_u32 #-}

-- | Decode 8 big-endian bytes, returning the remaining input.
decode_u64 :: BS.ByteString -> Maybe (Word64, BS.ByteString)
decode_u64 :: ByteString -> Maybe (Word64, ByteString)
decode_u64 ByteString
bs
  | ByteString -> Int
BS.length ByteString
bs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
8 = Maybe (Word64, ByteString)
forall a. Maybe a
Nothing
  | Bool
otherwise        = (Word64, ByteString) -> Maybe (Word64, ByteString)
forall a. a -> Maybe a
Just (Int -> ByteString -> Word64
be Int
8 ByteString
bs, Int -> ByteString -> ByteString
BU.unsafeDrop Int
8 ByteString
bs)
{-# INLINE decode_u64 #-}

-- signed integers ------------------------------------------------------------

-- | Encode an 'Int8' as 1 byte (two's complement).
--
--   >>> encode_s8 (-42)
--   "\214"
encode_s8 :: Int8 -> BS.ByteString
encode_s8 :: Int8 -> ByteString
encode_s8 = Word8 -> ByteString
BS.singleton (Word8 -> ByteString) -> (Int8 -> Word8) -> Int8 -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int8 -> Word8
forall a b. (Integral a, Num b) => a -> b
fi
{-# INLINE encode_s8 #-}

-- | Encode an 'Int16' as 2 big-endian bytes (two's complement).
encode_s16 :: Int16 -> BS.ByteString
encode_s16 :: Int16 -> ByteString
encode_s16 = Word16 -> ByteString
encode_u16 (Word16 -> ByteString) -> (Int16 -> Word16) -> Int16 -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int16 -> Word16
forall a b. (Integral a, Num b) => a -> b
fi
{-# INLINE encode_s16 #-}

-- | Encode an 'Int32' as 4 big-endian bytes (two's complement).
encode_s32 :: Int32 -> BS.ByteString
encode_s32 :: Int32 -> ByteString
encode_s32 = Word32 -> ByteString
encode_u32 (Word32 -> ByteString) -> (Int32 -> Word32) -> Int32 -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int32 -> Word32
forall a b. (Integral a, Num b) => a -> b
fi
{-# INLINE encode_s32 #-}

-- | Encode an 'Int64' as 8 big-endian bytes (two's complement).
encode_s64 :: Int64 -> BS.ByteString
encode_s64 :: Int64 -> ByteString
encode_s64 = Word64 -> ByteString
encode_u64 (Word64 -> ByteString) -> (Int64 -> Word64) -> Int64 -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int64 -> Word64
forall a b. (Integral a, Num b) => a -> b
fi
{-# INLINE encode_s64 #-}

-- | Decode 1 byte (two's complement), returning the remaining input.
decode_s8 :: BS.ByteString -> Maybe (Int8, BS.ByteString)
decode_s8 :: ByteString -> Maybe (Int8, ByteString)
decode_s8 ByteString
bs = case ByteString -> Maybe (Word8, ByteString)
BS.uncons ByteString
bs of
  Just (Word8
h, ByteString
t) -> (Int8, ByteString) -> Maybe (Int8, ByteString)
forall a. a -> Maybe a
Just (Word8 -> Int8
forall a b. (Integral a, Num b) => a -> b
fi Word8
h, ByteString
t)
  Maybe (Word8, ByteString)
Nothing     -> Maybe (Int8, ByteString)
forall a. Maybe a
Nothing
{-# INLINE decode_s8 #-}

-- | Decode 2 big-endian bytes (two's complement).
decode_s16 :: BS.ByteString -> Maybe (Int16, BS.ByteString)
decode_s16 :: ByteString -> Maybe (Int16, ByteString)
decode_s16 ByteString
bs = case ByteString -> Maybe (Word16, ByteString)
decode_u16 ByteString
bs of
  Just (Word16
w, ByteString
t) -> (Int16, ByteString) -> Maybe (Int16, ByteString)
forall a. a -> Maybe a
Just (Word16 -> Int16
forall a b. (Integral a, Num b) => a -> b
fi Word16
w, ByteString
t)
  Maybe (Word16, ByteString)
Nothing     -> Maybe (Int16, ByteString)
forall a. Maybe a
Nothing
{-# INLINE decode_s16 #-}

-- | Decode 4 big-endian bytes (two's complement).
decode_s32 :: BS.ByteString -> Maybe (Int32, BS.ByteString)
decode_s32 :: ByteString -> Maybe (Int32, ByteString)
decode_s32 ByteString
bs = case ByteString -> Maybe (Word32, ByteString)
decode_u32 ByteString
bs of
  Just (Word32
w, ByteString
t) -> (Int32, ByteString) -> Maybe (Int32, ByteString)
forall a. a -> Maybe a
Just (Word32 -> Int32
forall a b. (Integral a, Num b) => a -> b
fi Word32
w, ByteString
t)
  Maybe (Word32, ByteString)
Nothing     -> Maybe (Int32, ByteString)
forall a. Maybe a
Nothing
{-# INLINE decode_s32 #-}

-- | Decode 8 big-endian bytes (two's complement).
decode_s64 :: BS.ByteString -> Maybe (Int64, BS.ByteString)
decode_s64 :: ByteString -> Maybe (Int64, ByteString)
decode_s64 ByteString
bs = case ByteString -> Maybe (Word64, ByteString)
decode_u64 ByteString
bs of
  Just (Word64
w, ByteString
t) -> (Int64, ByteString) -> Maybe (Int64, ByteString)
forall a. a -> Maybe a
Just (Word64 -> Int64
forall a b. (Integral a, Num b) => a -> b
fi Word64
w, ByteString
t)
  Maybe (Word64, ByteString)
Nothing     -> Maybe (Int64, ByteString)
forall a. Maybe a
Nothing
{-# INLINE decode_s64 #-}

-- truncated unsigned integers ------------------------------------------------

-- minimal big-endian encoding: no leading zero bytes (zero is empty)
encode_tu :: Word64 -> BS.ByteString
encode_tu :: Word64 -> ByteString
encode_tu Word64
w = Int -> ByteString -> ByteString
BS.drop (Int -> Int
lz Int
0) (Word64 -> ByteString
encode_u64 Word64
w)
  where
    lz :: Int -> Int
    lz :: Int -> Int
lz !Int
i
      | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
8 Bool -> Bool -> Bool
&& (Word64
w Word64 -> Int -> Word64
forall a. Bits a => a -> Int -> a
`unsafeShiftR` (Int
56 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
8 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
i)) Word64 -> Word64 -> Word64
forall a. Bits a => a -> a -> a
.&. Word64
0xff Word64 -> Word64 -> Bool
forall a. Eq a => a -> a -> Bool
== Word64
0 = Int -> Int
lz (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
      | Bool
otherwise = Int
i

decode_tu :: Int -> BS.ByteString -> Maybe Word64
decode_tu :: Int -> ByteString -> Maybe Word64
decode_tu Int
maxlen ByteString
bs
  | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
maxlen                       = Maybe Word64
forall a. Maybe a
Nothing
  | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 Bool -> Bool -> Bool
&& ByteString -> Int -> Word8
BU.unsafeIndex ByteString
bs Int
0 Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
0 = Maybe Word64
forall a. Maybe a
Nothing
  | Bool
otherwise                        = Word64 -> Maybe Word64
forall a. a -> Maybe a
Just (Int -> ByteString -> Word64
be Int
n ByteString
bs)
  where
    !n :: Int
n = ByteString -> Int
BS.length ByteString
bs

-- | Encode a 'Word16' as a truncated integer (no leading zero bytes).
--
--   >>> encode_tu16 0
--   ""
--   >>> encode_tu16 0x0100
--   "\SOH\NUL"
encode_tu16 :: Word16 -> BS.ByteString
encode_tu16 :: Word16 -> ByteString
encode_tu16 = Word64 -> ByteString
encode_tu (Word64 -> ByteString)
-> (Word16 -> Word64) -> Word16 -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word16 -> Word64
forall a b. (Integral a, Num b) => a -> b
fi

-- | Encode a 'Word32' as a truncated integer (no leading zero bytes).
encode_tu32 :: Word32 -> BS.ByteString
encode_tu32 :: Word32 -> ByteString
encode_tu32 = Word64 -> ByteString
encode_tu (Word64 -> ByteString)
-> (Word32 -> Word64) -> Word32 -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word32 -> Word64
forall a b. (Integral a, Num b) => a -> b
fi

-- | Encode a 'Word64' as a truncated integer (no leading zero bytes).
encode_tu64 :: Word64 -> BS.ByteString
encode_tu64 :: Word64 -> ByteString
encode_tu64 = Word64 -> ByteString
encode_tu

-- | Decode a truncated 16-bit integer occupying the whole input. Fails
--   on more than 2 bytes or a non-minimal encoding.
--
--   >>> decode_tu16 "\SOH\NUL"
--   Just 256
--   >>> decode_tu16 "\NUL\SOH"
--   Nothing
decode_tu16 :: BS.ByteString -> Maybe Word16
decode_tu16 :: ByteString -> Maybe Word16
decode_tu16 = (Word64 -> Word16) -> Maybe Word64 -> Maybe Word16
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Word64 -> Word16
forall a b. (Integral a, Num b) => a -> b
fi (Maybe Word64 -> Maybe Word16)
-> (ByteString -> Maybe Word64) -> ByteString -> Maybe Word16
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> ByteString -> Maybe Word64
decode_tu Int
2

-- | Decode a truncated 32-bit integer occupying the whole input. Fails
--   on more than 4 bytes or a non-minimal encoding.
decode_tu32 :: BS.ByteString -> Maybe Word32
decode_tu32 :: ByteString -> Maybe Word32
decode_tu32 = (Word64 -> Word32) -> Maybe Word64 -> Maybe Word32
forall a b. (a -> b) -> Maybe a -> Maybe b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Word64 -> Word32
forall a b. (Integral a, Num b) => a -> b
fi (Maybe Word64 -> Maybe Word32)
-> (ByteString -> Maybe Word64) -> ByteString -> Maybe Word32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> ByteString -> Maybe Word64
decode_tu Int
4

-- | Decode a truncated 64-bit integer occupying the whole input. Fails
--   on more than 8 bytes or a non-minimal encoding.
decode_tu64 :: BS.ByteString -> Maybe Word64
decode_tu64 :: ByteString -> Maybe Word64
decode_tu64 = Int -> ByteString -> Maybe Word64
decode_tu Int
8

-- bigsize --------------------------------------------------------------------

-- | Encode a 'Word64' in the minimal BigSize format.
--
--   >>> encode_bigsize 252
--   "\252"
--   >>> encode_bigsize 253
--   "\253\NUL\253"
encode_bigsize :: Word64 -> BS.ByteString
encode_bigsize :: Word64 -> ByteString
encode_bigsize Word64
w
  | Word64
w Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
0xfd        = Word8 -> ByteString
BS.singleton (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fi Word64
w)
  | Word64
w Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
<= Word64
0xffff     = Word8 -> ByteString -> ByteString
BS.cons Word8
0xfd (Word16 -> ByteString
encode_u16 (Word64 -> Word16
forall a b. (Integral a, Num b) => a -> b
fi Word64
w))
  | Word64
w Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
<= Word64
0xffffffff = Word8 -> ByteString -> ByteString
BS.cons Word8
0xfe (Word32 -> ByteString
encode_u32 (Word64 -> Word32
forall a b. (Integral a, Num b) => a -> b
fi Word64
w))
  | Bool
otherwise       = Word8 -> ByteString -> ByteString
BS.cons Word8
0xff (Word64 -> ByteString
encode_u64 Word64
w)

-- | Why a BigSize failed to decode.
data BigSizeError
  = BigSizeTruncated   -- ^ input ended early
  | BigSizeNonMinimal  -- ^ a shorter encoding exists
  deriving (BigSizeError -> BigSizeError -> Bool
(BigSizeError -> BigSizeError -> Bool)
-> (BigSizeError -> BigSizeError -> Bool) -> Eq BigSizeError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: BigSizeError -> BigSizeError -> Bool
== :: BigSizeError -> BigSizeError -> Bool
$c/= :: BigSizeError -> BigSizeError -> Bool
/= :: BigSizeError -> BigSizeError -> Bool
Eq, Int -> BigSizeError -> ShowS
[BigSizeError] -> ShowS
BigSizeError -> String
(Int -> BigSizeError -> ShowS)
-> (BigSizeError -> String)
-> ([BigSizeError] -> ShowS)
-> Show BigSizeError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> BigSizeError -> ShowS
showsPrec :: Int -> BigSizeError -> ShowS
$cshow :: BigSizeError -> String
show :: BigSizeError -> String
$cshowList :: [BigSizeError] -> ShowS
showList :: [BigSizeError] -> ShowS
Show, (forall x. BigSizeError -> Rep BigSizeError x)
-> (forall x. Rep BigSizeError x -> BigSizeError)
-> Generic BigSizeError
forall x. Rep BigSizeError x -> BigSizeError
forall x. BigSizeError -> Rep BigSizeError x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. BigSizeError -> Rep BigSizeError x
from :: forall x. BigSizeError -> Rep BigSizeError x
$cto :: forall x. Rep BigSizeError x -> BigSizeError
to :: forall x. Rep BigSizeError x -> BigSizeError
Generic)

instance NFData BigSizeError

-- | Decode a BigSize, distinguishing truncation from non-minimal
--   encodings.
decode_bigsize_detailed
  :: BS.ByteString -> Either BigSizeError (Word64, BS.ByteString)
decode_bigsize_detailed :: ByteString -> Either BigSizeError (Word64, ByteString)
decode_bigsize_detailed ByteString
bs = case ByteString -> Maybe (Word8, ByteString)
BS.uncons ByteString
bs of
  Maybe (Word8, ByteString)
Nothing -> BigSizeError -> Either BigSizeError (Word64, ByteString)
forall a b. a -> Either a b
Left BigSizeError
BigSizeTruncated
  Just (Word8
h, ByteString
t) -> case Word8
h of
    Word8
0xfd -> Int
-> Word64 -> ByteString -> Either BigSizeError (Word64, ByteString)
wide Int
2 Word64
0xfd ByteString
t
    Word8
0xfe -> Int
-> Word64 -> ByteString -> Either BigSizeError (Word64, ByteString)
wide Int
4 Word64
0x10000 ByteString
t
    Word8
0xff -> Int
-> Word64 -> ByteString -> Either BigSizeError (Word64, ByteString)
wide Int
8 Word64
0x100000000 ByteString
t
    Word8
_    -> (Word64, ByteString) -> Either BigSizeError (Word64, ByteString)
forall a b. b -> Either a b
Right (Word8 -> Word64
forall a b. (Integral a, Num b) => a -> b
fi Word8
h, ByteString
t)
  where
    wide :: Int
-> Word64 -> ByteString -> Either BigSizeError (Word64, ByteString)
wide Int
n Word64
lo ByteString
t = case Int -> ByteString -> Maybe (ByteString, ByteString)
take_n Int
n ByteString
t of
      Maybe (ByteString, ByteString)
Nothing -> BigSizeError -> Either BigSizeError (Word64, ByteString)
forall a b. a -> Either a b
Left BigSizeError
BigSizeTruncated
      Just (ByteString
v, ByteString
r)
        | Word64
w Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
lo    -> BigSizeError -> Either BigSizeError (Word64, ByteString)
forall a b. a -> Either a b
Left BigSizeError
BigSizeNonMinimal
        | Bool
otherwise -> (Word64, ByteString) -> Either BigSizeError (Word64, ByteString)
forall a b. b -> Either a b
Right (Word64
w, ByteString
r)
        where
          !w :: Word64
w = Int -> ByteString -> Word64
be Int
n ByteString
v

-- | Decode a minimally-encoded BigSize, returning the remaining input.
--
--   >>> decode_bigsize "\253\NUL\253rest"
--   Just (253,"rest")
--   >>> decode_bigsize "\253\NUL\252"
--   Nothing
decode_bigsize :: BS.ByteString -> Maybe (Word64, BS.ByteString)
decode_bigsize :: ByteString -> Maybe (Word64, ByteString)
decode_bigsize ByteString
bs = case ByteString -> Either BigSizeError (Word64, ByteString)
decode_bigsize_detailed ByteString
bs of
  Right (Word64, ByteString)
r -> (Word64, ByteString) -> Maybe (Word64, ByteString)
forall a. a -> Maybe a
Just (Word64, ByteString)
r
  Left BigSizeError
_  -> Maybe (Word64, ByteString)
forall a. Maybe a
Nothing

-- length-prefixed bytes ------------------------------------------------------

-- | Prefix bytes with their length as a u16. Fails if the input
--   exceeds 65535 bytes.
--
--   >>> encode_u16_prefixed "abc"
--   Just "\NUL\ETXabc"
encode_u16_prefixed :: BS.ByteString -> Maybe BS.ByteString
encode_u16_prefixed :: ByteString -> Maybe ByteString
encode_u16_prefixed ByteString
bs
  | ByteString -> Int
BS.length ByteString
bs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0xffff = Maybe ByteString
forall a. Maybe a
Nothing
  | Bool
otherwise = ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just (Word16 -> ByteString
encode_u16 (Int -> Word16
forall a b. (Integral a, Num b) => a -> b
fi (ByteString -> Int
BS.length ByteString
bs)) ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
bs)

-- | Decode u16-length-prefixed bytes, returning the remaining input.
--
--   >>> decode_u16_prefixed "\NUL\ETXabcrest"
--   Just ("abc","rest")
decode_u16_prefixed
  :: BS.ByteString -> Maybe (BS.ByteString, BS.ByteString)
decode_u16_prefixed :: ByteString -> Maybe (ByteString, ByteString)
decode_u16_prefixed ByteString
bs = do
  (n, rest) <- ByteString -> Maybe (Word16, ByteString)
decode_u16 ByteString
bs
  take_n (fi n) rest

-- fixed-size field codecs ----------------------------------------------------

-- | Decode a 32-byte 'ChainHash', returning the remaining input.
decode_chain_hash :: BS.ByteString -> Maybe (ChainHash, BS.ByteString)
decode_chain_hash :: ByteString -> Maybe (ChainHash, ByteString)
decode_chain_hash ByteString
bs = do
  (h, r) <- Int -> ByteString -> Maybe (ByteString, ByteString)
take_n Int
32 ByteString
bs
  pure (ChainHash h, r)

-- | Decode a 32-byte 'ChannelId', returning the remaining input.
decode_channel_id :: BS.ByteString -> Maybe (ChannelId, BS.ByteString)
decode_channel_id :: ByteString -> Maybe (ChannelId, ByteString)
decode_channel_id ByteString
bs = do
  (h, r) <- Int -> ByteString -> Maybe (ByteString, ByteString)
take_n Int
32 ByteString
bs
  pure (ChannelId h, r)

-- | Decode a 64-byte 'Signature', returning the remaining input.
decode_signature :: BS.ByteString -> Maybe (Signature, BS.ByteString)
decode_signature :: ByteString -> Maybe (Signature, ByteString)
decode_signature ByteString
bs = do
  (h, r) <- Int -> ByteString -> Maybe (ByteString, ByteString)
take_n Int
64 ByteString
bs
  pure (Signature h, r)

-- | Decode a 33-byte 'Point', returning the remaining input. Fails if
--   the prefix byte is not 0x02 or 0x03.
decode_point :: BS.ByteString -> Maybe (Point, BS.ByteString)
decode_point :: ByteString -> Maybe (Point, ByteString)
decode_point ByteString
bs = do
  (h, r) <- Int -> ByteString -> Maybe (ByteString, ByteString)
take_n Int
33 ByteString
bs
  p <- point h
  pure (p, r)

-- | Decode a 32-byte 'PaymentHash', returning the remaining input.
decode_payment_hash :: BS.ByteString -> Maybe (PaymentHash, BS.ByteString)
decode_payment_hash :: ByteString -> Maybe (PaymentHash, ByteString)
decode_payment_hash ByteString
bs = do
  (h, r) <- Int -> ByteString -> Maybe (ByteString, ByteString)
take_n Int
32 ByteString
bs
  pure (PaymentHash h, r)

-- | Decode a 32-byte 'PaymentPreimage', returning the remaining input.
decode_payment_preimage
  :: BS.ByteString -> Maybe (PaymentPreimage, BS.ByteString)
decode_payment_preimage :: ByteString -> Maybe (PaymentPreimage, ByteString)
decode_payment_preimage ByteString
bs = do
  (h, r) <- Int -> ByteString -> Maybe (ByteString, ByteString)
take_n Int
32 ByteString
bs
  pure (PaymentPreimage h, r)

-- | Decode a 32-byte 'PerCommitmentSecret', returning the remaining
--   input.
decode_per_commitment_secret
  :: BS.ByteString -> Maybe (PerCommitmentSecret, BS.ByteString)
decode_per_commitment_secret :: ByteString -> Maybe (PerCommitmentSecret, ByteString)
decode_per_commitment_secret ByteString
bs = do
  (h, r) <- Int -> ByteString -> Maybe (ByteString, ByteString)
take_n Int
32 ByteString
bs
  pure (PerCommitmentSecret h, r)

-- | Encode a 'ShortChannelId' as 8 big-endian bytes.
encode_short_channel_id :: ShortChannelId -> BS.ByteString
encode_short_channel_id :: ShortChannelId -> ByteString
encode_short_channel_id (ShortChannelId Word64
w) = Word64 -> ByteString
encode_u64 Word64
w
{-# INLINE encode_short_channel_id #-}

-- | Decode an 8-byte 'ShortChannelId', returning the remaining input.
decode_short_channel_id
  :: BS.ByteString -> Maybe (ShortChannelId, BS.ByteString)
decode_short_channel_id :: ByteString -> Maybe (ShortChannelId, ByteString)
decode_short_channel_id ByteString
bs = do
  (w, r) <- ByteString -> Maybe (Word64, ByteString)
decode_u64 ByteString
bs
  pure (ShortChannelId w, r)
{-# INLINE decode_short_channel_id #-}

-- | Encode a 'Satoshi' amount as a u64.
encode_satoshi :: Satoshi -> BS.ByteString
encode_satoshi :: Satoshi -> ByteString
encode_satoshi (Satoshi Word64
w) = Word64 -> ByteString
encode_u64 Word64
w
{-# INLINE encode_satoshi #-}

-- | Decode a u64 'Satoshi' amount, returning the remaining input. Fails
--   above 21 million BTC.
decode_satoshi :: BS.ByteString -> Maybe (Satoshi, BS.ByteString)
decode_satoshi :: ByteString -> Maybe (Satoshi, ByteString)
decode_satoshi ByteString
bs = do
  (w, r) <- ByteString -> Maybe (Word64, ByteString)
decode_u64 ByteString
bs
  s <- satoshi w
  pure (s, r)
{-# INLINE decode_satoshi #-}

-- | Encode a 'MilliSatoshi' amount as a u64.
encode_milli_satoshi :: MilliSatoshi -> BS.ByteString
encode_milli_satoshi :: MilliSatoshi -> ByteString
encode_milli_satoshi (MilliSatoshi Word64
w) = Word64 -> ByteString
encode_u64 Word64
w
{-# INLINE encode_milli_satoshi #-}

-- | Decode a u64 'MilliSatoshi' amount, returning the remaining input.
--   Fails above 21 million BTC.
decode_milli_satoshi
  :: BS.ByteString -> Maybe (MilliSatoshi, BS.ByteString)
decode_milli_satoshi :: ByteString -> Maybe (MilliSatoshi, ByteString)
decode_milli_satoshi ByteString
bs = do
  (w, r) <- ByteString -> Maybe (Word64, ByteString)
decode_u64 ByteString
bs
  m <- milli_satoshi w
  pure (m, r)
{-# INLINE decode_milli_satoshi #-}