{-# OPTIONS_HADDOCK hide #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DeriveGeneric #-}
module Lightning.Protocol.BOLT2.Codec (
EncodeError(..)
, DecodeError(..)
, encode_message
, decode_message
, encode_open_channel
, decode_open_channel
, encode_accept_channel
, decode_accept_channel
, encode_funding_created
, decode_funding_created
, encode_funding_signed
, decode_funding_signed
, encode_channel_ready
, decode_channel_ready
, encode_open_channel2
, decode_open_channel2
, encode_accept_channel2
, decode_accept_channel2
, encode_tx_add_input
, decode_tx_add_input
, encode_tx_add_output
, decode_tx_add_output
, encode_tx_remove_input
, decode_tx_remove_input
, encode_tx_remove_output
, decode_tx_remove_output
, encode_tx_complete
, decode_tx_complete
, encode_tx_signatures
, decode_tx_signatures
, encode_tx_init_rbf
, decode_tx_init_rbf
, encode_tx_ack_rbf
, decode_tx_ack_rbf
, encode_tx_abort
, decode_tx_abort
, encode_stfu
, decode_stfu
, encode_shutdown
, decode_shutdown
, encode_closing_complete
, decode_closing_complete
, encode_closing_sig
, decode_closing_sig
, encode_closing_signed
, decode_closing_signed
, encode_update_add_htlc
, decode_update_add_htlc
, encode_update_fulfill_htlc
, decode_update_fulfill_htlc
, encode_update_fail_htlc
, decode_update_fail_htlc
, encode_update_fail_malformed_htlc
, decode_update_fail_malformed_htlc
, encode_commitment_signed
, decode_commitment_signed
, encode_revoke_and_ack
, decode_revoke_and_ack
, encode_update_fee
, decode_update_fee
, encode_channel_reestablish
, decode_channel_reestablish
) where
import Bitcoin.Prim.Tx (TxId, mk_txid, un_txid)
import Control.DeepSeq (NFData)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Unsafe as BU
import Data.Int (Int64)
import Data.Word (Word8, Word16, Word32, Word64)
import GHC.Generics (Generic)
import qualified Lightning.Protocol.BOLT1 as BOLT1
import Lightning.Protocol.BOLT1
( ChannelId, Signature, Point, TlvRecord(..), TlvStream, TlvError )
import qualified Lightning.Protocol.BOLT9 as BOLT9
import Lightning.Protocol.BOLT2.Messages
import Lightning.Protocol.BOLT2.Types
data EncodeError
= EncodeLengthOverflow
| EncodeInvalidTlvs
| EncodeMessageTooLarge
deriving (EncodeError -> EncodeError -> Bool
(EncodeError -> EncodeError -> Bool)
-> (EncodeError -> EncodeError -> Bool) -> Eq EncodeError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: EncodeError -> EncodeError -> Bool
== :: EncodeError -> EncodeError -> Bool
$c/= :: EncodeError -> EncodeError -> Bool
/= :: EncodeError -> EncodeError -> Bool
Eq, Int -> EncodeError -> ShowS
[EncodeError] -> ShowS
EncodeError -> String
(Int -> EncodeError -> ShowS)
-> (EncodeError -> String)
-> ([EncodeError] -> ShowS)
-> Show EncodeError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> EncodeError -> ShowS
showsPrec :: Int -> EncodeError -> ShowS
$cshow :: EncodeError -> String
show :: EncodeError -> String
$cshowList :: [EncodeError] -> ShowS
showList :: [EncodeError] -> ShowS
Show, (forall x. EncodeError -> Rep EncodeError x)
-> (forall x. Rep EncodeError x -> EncodeError)
-> Generic EncodeError
forall x. Rep EncodeError x -> EncodeError
forall x. EncodeError -> Rep EncodeError x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. EncodeError -> Rep EncodeError x
from :: forall x. EncodeError -> Rep EncodeError x
$cto :: forall x. Rep EncodeError x -> EncodeError
to :: forall x. Rep EncodeError x -> EncodeError
Generic)
instance NFData EncodeError
data DecodeError
= DecodeInsufficientBytes
| DecodeInvalidPoint
| DecodeInvalidAmount
| DecodeInvalidInitiator
| DecodeTlvError !TlvError
| DecodeInvalidTlvValue !Word64
| DecodeUnknownEvenType !Word16
| DecodeUnknownOddType !Word16
deriving (DecodeError -> DecodeError -> Bool
(DecodeError -> DecodeError -> Bool)
-> (DecodeError -> DecodeError -> Bool) -> Eq DecodeError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: DecodeError -> DecodeError -> Bool
== :: DecodeError -> DecodeError -> Bool
$c/= :: DecodeError -> DecodeError -> Bool
/= :: DecodeError -> DecodeError -> Bool
Eq, Int -> DecodeError -> ShowS
[DecodeError] -> ShowS
DecodeError -> String
(Int -> DecodeError -> ShowS)
-> (DecodeError -> String)
-> ([DecodeError] -> ShowS)
-> Show DecodeError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> DecodeError -> ShowS
showsPrec :: Int -> DecodeError -> ShowS
$cshow :: DecodeError -> String
show :: DecodeError -> String
$cshowList :: [DecodeError] -> ShowS
showList :: [DecodeError] -> ShowS
Show, (forall x. DecodeError -> Rep DecodeError x)
-> (forall x. Rep DecodeError x -> DecodeError)
-> Generic DecodeError
forall x. Rep DecodeError x -> DecodeError
forall x. DecodeError -> Rep DecodeError x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. DecodeError -> Rep DecodeError x
from :: forall x. DecodeError -> Rep DecodeError x
$cto :: forall x. Rep DecodeError x -> DecodeError
to :: forall x. Rep DecodeError x -> DecodeError
Generic)
instance NFData DecodeError
newtype Parser a = Parser
{ forall a.
Parser a -> ByteString -> Either DecodeError (a, ByteString)
run_parser :: BS.ByteString -> Either DecodeError (a, BS.ByteString) }
instance Functor Parser where
fmap :: forall a b. (a -> b) -> Parser a -> Parser b
fmap a -> b
f (Parser ByteString -> Either DecodeError (a, ByteString)
p) = (ByteString -> Either DecodeError (b, ByteString)) -> Parser b
forall a.
(ByteString -> Either DecodeError (a, ByteString)) -> Parser a
Parser ((ByteString -> Either DecodeError (b, ByteString)) -> Parser b)
-> (ByteString -> Either DecodeError (b, ByteString)) -> Parser b
forall a b. (a -> b) -> a -> b
$ \ByteString
bs -> case ByteString -> Either DecodeError (a, ByteString)
p ByteString
bs of
Left DecodeError
e -> DecodeError -> Either DecodeError (b, ByteString)
forall a b. a -> Either a b
Left DecodeError
e
Right (a
a, ByteString
r) -> (b, ByteString) -> Either DecodeError (b, ByteString)
forall a b. b -> Either a b
Right (a -> b
f a
a, ByteString
r)
{-# INLINE fmap #-}
instance Applicative Parser where
pure :: forall a. a -> Parser a
pure a
a = (ByteString -> Either DecodeError (a, ByteString)) -> Parser a
forall a.
(ByteString -> Either DecodeError (a, ByteString)) -> Parser a
Parser ((ByteString -> Either DecodeError (a, ByteString)) -> Parser a)
-> (ByteString -> Either DecodeError (a, ByteString)) -> Parser a
forall a b. (a -> b) -> a -> b
$ \ByteString
bs -> (a, ByteString) -> Either DecodeError (a, ByteString)
forall a b. b -> Either a b
Right (a
a, ByteString
bs)
{-# INLINE pure #-}
Parser ByteString -> Either DecodeError (a -> b, ByteString)
pf <*> :: forall a b. Parser (a -> b) -> Parser a -> Parser b
<*> Parser ByteString -> Either DecodeError (a, ByteString)
pa = (ByteString -> Either DecodeError (b, ByteString)) -> Parser b
forall a.
(ByteString -> Either DecodeError (a, ByteString)) -> Parser a
Parser ((ByteString -> Either DecodeError (b, ByteString)) -> Parser b)
-> (ByteString -> Either DecodeError (b, ByteString)) -> Parser b
forall a b. (a -> b) -> a -> b
$ \ByteString
bs -> case ByteString -> Either DecodeError (a -> b, ByteString)
pf ByteString
bs of
Left DecodeError
e -> DecodeError -> Either DecodeError (b, ByteString)
forall a b. a -> Either a b
Left DecodeError
e
Right (a -> b
f, ByteString
r) -> case ByteString -> Either DecodeError (a, ByteString)
pa ByteString
r of
Left DecodeError
e -> DecodeError -> Either DecodeError (b, ByteString)
forall a b. a -> Either a b
Left DecodeError
e
Right (a
a, ByteString
r') -> (b, ByteString) -> Either DecodeError (b, ByteString)
forall a b. b -> Either a b
Right (a -> b
f a
a, ByteString
r')
{-# INLINE (<*>) #-}
instance Monad Parser where
Parser ByteString -> Either DecodeError (a, ByteString)
p >>= :: forall a b. Parser a -> (a -> Parser b) -> Parser b
>>= a -> Parser b
k = (ByteString -> Either DecodeError (b, ByteString)) -> Parser b
forall a.
(ByteString -> Either DecodeError (a, ByteString)) -> Parser a
Parser ((ByteString -> Either DecodeError (b, ByteString)) -> Parser b)
-> (ByteString -> Either DecodeError (b, ByteString)) -> Parser b
forall a b. (a -> b) -> a -> b
$ \ByteString
bs -> case ByteString -> Either DecodeError (a, ByteString)
p ByteString
bs of
Left DecodeError
e -> DecodeError -> Either DecodeError (b, ByteString)
forall a b. a -> Either a b
Left DecodeError
e
Right (a
a, ByteString
r) -> Parser b -> ByteString -> Either DecodeError (b, ByteString)
forall a.
Parser a -> ByteString -> Either DecodeError (a, ByteString)
run_parser (a -> Parser b
k a
a) ByteString
r
{-# INLINE (>>=) #-}
parse :: Parser a -> BS.ByteString -> Either DecodeError a
parse :: forall a. Parser a -> ByteString -> Either DecodeError a
parse Parser a
p ByteString
bs = ((a, ByteString) -> a)
-> Either DecodeError (a, ByteString) -> Either DecodeError a
forall a b.
(a -> b) -> Either DecodeError a -> Either DecodeError b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (a, ByteString) -> a
forall a b. (a, b) -> a
fst (Parser a -> ByteString -> Either DecodeError (a, ByteString)
forall a.
Parser a -> ByteString -> Either DecodeError (a, ByteString)
run_parser Parser a
p ByteString
bs)
{-# INLINE parse #-}
failure :: DecodeError -> Parser a
failure :: forall a. DecodeError -> Parser a
failure DecodeError
e = (ByteString -> Either DecodeError (a, ByteString)) -> Parser a
forall a.
(ByteString -> Either DecodeError (a, ByteString)) -> Parser a
Parser ((ByteString -> Either DecodeError (a, ByteString)) -> Parser a)
-> (ByteString -> Either DecodeError (a, ByteString)) -> Parser a
forall a b. (a -> b) -> a -> b
$ \ByteString
_ -> DecodeError -> Either DecodeError (a, ByteString)
forall a b. a -> Either a b
Left DecodeError
e
{-# INLINE failure #-}
field :: (BS.ByteString -> Maybe (a, BS.ByteString)) -> Parser a
field :: forall a. (ByteString -> Maybe (a, ByteString)) -> Parser a
field ByteString -> Maybe (a, ByteString)
f = (ByteString -> Either DecodeError (a, ByteString)) -> Parser a
forall a.
(ByteString -> Either DecodeError (a, ByteString)) -> Parser a
Parser ((ByteString -> Either DecodeError (a, ByteString)) -> Parser a)
-> (ByteString -> Either DecodeError (a, ByteString)) -> Parser a
forall a b. (a -> b) -> a -> b
$ \ByteString
bs -> case ByteString -> Maybe (a, ByteString)
f ByteString
bs of
Maybe (a, ByteString)
Nothing -> DecodeError -> Either DecodeError (a, ByteString)
forall a b. a -> Either a b
Left DecodeError
DecodeInsufficientBytes
Just (a, ByteString)
r -> (a, ByteString) -> Either DecodeError (a, ByteString)
forall a b. b -> Either a b
Right (a, ByteString)
r
{-# INLINE field #-}
bytes :: Int -> Parser BS.ByteString
bytes :: Int -> Parser ByteString
bytes Int
n = (ByteString -> Either DecodeError (ByteString, ByteString))
-> Parser ByteString
forall a.
(ByteString -> Either DecodeError (a, ByteString)) -> Parser a
Parser ((ByteString -> Either DecodeError (ByteString, ByteString))
-> Parser ByteString)
-> (ByteString -> Either DecodeError (ByteString, ByteString))
-> Parser ByteString
forall a b. (a -> b) -> a -> b
$ \ByteString
bs ->
if ByteString -> Int
BS.length ByteString
bs Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
n
then DecodeError -> Either DecodeError (ByteString, ByteString)
forall a b. a -> Either a b
Left DecodeError
DecodeInsufficientBytes
else (ByteString, ByteString)
-> Either DecodeError (ByteString, ByteString)
forall a b. b -> Either a b
Right (Int -> ByteString -> ByteString
BU.unsafeTake Int
n ByteString
bs, Int -> ByteString -> ByteString
BU.unsafeDrop Int
n ByteString
bs)
{-# INLINE bytes #-}
sized :: Int -> (BS.ByteString -> Maybe a) -> Parser a
sized :: forall a. Int -> (ByteString -> Maybe a) -> Parser a
sized Int
n ByteString -> Maybe a
f = do
b <- Int -> Parser ByteString
bytes Int
n
maybe (failure DecodeInsufficientBytes) pure (f b)
{-# INLINE sized #-}
u8 :: Parser Word8
u8 :: Parser Word8
u8 = (ByteString -> Either DecodeError (Word8, ByteString))
-> Parser Word8
forall a.
(ByteString -> Either DecodeError (a, ByteString)) -> Parser a
Parser ((ByteString -> Either DecodeError (Word8, ByteString))
-> Parser Word8)
-> (ByteString -> Either DecodeError (Word8, ByteString))
-> Parser Word8
forall a b. (a -> b) -> a -> b
$ \ByteString
bs -> case ByteString -> Maybe (Word8, ByteString)
BS.uncons ByteString
bs of
Maybe (Word8, ByteString)
Nothing -> DecodeError -> Either DecodeError (Word8, ByteString)
forall a b. a -> Either a b
Left DecodeError
DecodeInsufficientBytes
Just (Word8, ByteString)
r -> (Word8, ByteString) -> Either DecodeError (Word8, ByteString)
forall a b. b -> Either a b
Right (Word8, ByteString)
r
{-# INLINE u8 #-}
u16 :: Parser Word16
u16 :: Parser Word16
u16 = (ByteString -> Maybe (Word16, ByteString)) -> Parser Word16
forall a. (ByteString -> Maybe (a, ByteString)) -> Parser a
field ByteString -> Maybe (Word16, ByteString)
BOLT1.decode_u16
{-# INLINE u16 #-}
u32 :: Parser Word32
u32 :: Parser Word32
u32 = (ByteString -> Maybe (Word32, ByteString)) -> Parser Word32
forall a. (ByteString -> Maybe (a, ByteString)) -> Parser a
field ByteString -> Maybe (Word32, ByteString)
BOLT1.decode_u32
{-# INLINE u32 #-}
u64 :: Parser Word64
u64 :: Parser Word64
u64 = (ByteString -> Maybe (Word64, ByteString)) -> Parser Word64
forall a. (ByteString -> Maybe (a, ByteString)) -> Parser a
field ByteString -> Maybe (Word64, ByteString)
BOLT1.decode_u64
{-# INLINE u64 #-}
chain_hash :: Parser BOLT1.ChainHash
chain_hash :: Parser ChainHash
chain_hash = (ByteString -> Maybe (ChainHash, ByteString)) -> Parser ChainHash
forall a. (ByteString -> Maybe (a, ByteString)) -> Parser a
field ByteString -> Maybe (ChainHash, ByteString)
BOLT1.decode_chain_hash
{-# INLINE chain_hash #-}
channel_id :: Parser ChannelId
channel_id :: Parser ChannelId
channel_id = (ByteString -> Maybe (ChannelId, ByteString)) -> Parser ChannelId
forall a. (ByteString -> Maybe (a, ByteString)) -> Parser a
field ByteString -> Maybe (ChannelId, ByteString)
BOLT1.decode_channel_id
{-# INLINE channel_id #-}
signature :: Parser Signature
signature :: Parser Signature
signature = (ByteString -> Maybe (Signature, ByteString)) -> Parser Signature
forall a. (ByteString -> Maybe (a, ByteString)) -> Parser a
field ByteString -> Maybe (Signature, ByteString)
BOLT1.decode_signature
{-# INLINE signature #-}
payment_hash :: Parser BOLT1.PaymentHash
payment_hash :: Parser PaymentHash
payment_hash = (ByteString -> Maybe (PaymentHash, ByteString))
-> Parser PaymentHash
forall a. (ByteString -> Maybe (a, ByteString)) -> Parser a
field ByteString -> Maybe (PaymentHash, ByteString)
BOLT1.decode_payment_hash
{-# INLINE payment_hash #-}
payment_preimage :: Parser BOLT1.PaymentPreimage
payment_preimage :: Parser PaymentPreimage
payment_preimage = (ByteString -> Maybe (PaymentPreimage, ByteString))
-> Parser PaymentPreimage
forall a. (ByteString -> Maybe (a, ByteString)) -> Parser a
field ByteString -> Maybe (PaymentPreimage, ByteString)
BOLT1.decode_payment_preimage
{-# INLINE payment_preimage #-}
per_commitment_secret :: Parser BOLT1.PerCommitmentSecret
per_commitment_secret :: Parser PerCommitmentSecret
per_commitment_secret = (ByteString -> Maybe (PerCommitmentSecret, ByteString))
-> Parser PerCommitmentSecret
forall a. (ByteString -> Maybe (a, ByteString)) -> Parser a
field ByteString -> Maybe (PerCommitmentSecret, ByteString)
BOLT1.decode_per_commitment_secret
{-# INLINE per_commitment_secret #-}
point :: Parser Point
point :: Parser Point
point = do
b <- Int -> Parser ByteString
bytes Int
33
maybe (failure DecodeInvalidPoint) pure (BOLT1.point b)
{-# INLINE point #-}
satoshi :: Parser BOLT1.Satoshi
satoshi :: Parser Satoshi
satoshi = do
w <- Parser Word64
u64
maybe (failure DecodeInvalidAmount) pure (BOLT1.satoshi w)
{-# INLINE satoshi #-}
milli_satoshi :: Parser BOLT1.MilliSatoshi
milli_satoshi :: Parser MilliSatoshi
milli_satoshi = do
w <- Parser Word64
u64
maybe (failure DecodeInvalidAmount) pure (BOLT1.milli_satoshi w)
{-# INLINE milli_satoshi #-}
txid :: Parser TxId
txid :: Parser TxId
txid = Int -> (ByteString -> Maybe TxId) -> Parser TxId
forall a. Int -> (ByteString -> Maybe a) -> Parser a
sized Int
32 ByteString -> Maybe TxId
mk_txid
{-# INLINE txid #-}
htlc_id :: Parser HtlcId
htlc_id :: Parser HtlcId
htlc_id = (Word64 -> HtlcId) -> Parser Word64 -> Parser HtlcId
forall a b. (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Word64 -> HtlcId
HtlcId Parser Word64
u64
{-# INLINE htlc_id #-}
serial_id :: Parser SerialId
serial_id :: Parser SerialId
serial_id = (Word64 -> SerialId) -> Parser Word64 -> Parser SerialId
forall a b. (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Word64 -> SerialId
SerialId Parser Word64
u64
{-# INLINE serial_id #-}
prefixed :: Parser BS.ByteString
prefixed :: Parser ByteString
prefixed = (ByteString -> Maybe (ByteString, ByteString)) -> Parser ByteString
forall a. (ByteString -> Maybe (a, ByteString)) -> Parser a
field ByteString -> Maybe (ByteString, ByteString)
BOLT1.decode_u16_prefixed
{-# INLINE prefixed #-}
script :: Parser ScriptPubKey
script :: Parser ScriptPubKey
script = do
b <- Parser ByteString
prefixed
maybe (failure DecodeInsufficientBytes) pure (script_pubkey b)
{-# INLINE script #-}
count :: Int -> Parser a -> Parser [a]
count :: forall a. Int -> Parser a -> Parser [a]
count Int
n0 Parser a
p = Int -> [a] -> Parser [a]
forall {t}. (Ord t, Num t) => t -> [a] -> Parser [a]
go Int
n0 []
where
go :: t -> [a] -> Parser [a]
go !t
n ![a]
acc
| t
n t -> t -> Bool
forall a. Ord a => a -> a -> Bool
<= t
0 = [a] -> Parser [a]
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([a] -> [a]
forall a. [a] -> [a]
reverse [a]
acc)
| Bool
otherwise = Parser a
p Parser a -> (a -> Parser [a]) -> Parser [a]
forall a b. Parser a -> (a -> Parser b) -> Parser b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \a
a -> t -> [a] -> Parser [a]
go (t
n t -> t -> t
forall a. Num a => a -> a -> a
- t
1) (a
a a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [a]
acc)
{-# INLINE count #-}
tlvs :: [Word64] -> Parser TlvStream
tlvs :: [Word64] -> Parser TlvStream
tlvs [Word64]
known = (ByteString -> Either DecodeError (TlvStream, ByteString))
-> Parser TlvStream
forall a.
(ByteString -> Either DecodeError (a, ByteString)) -> Parser a
Parser ((ByteString -> Either DecodeError (TlvStream, ByteString))
-> Parser TlvStream)
-> (ByteString -> Either DecodeError (TlvStream, ByteString))
-> Parser TlvStream
forall a b. (a -> b) -> a -> b
$ \ByteString
bs ->
case (Word64 -> Bool) -> ByteString -> Either TlvError TlvStream
BOLT1.decode_tlv_stream (Word64 -> [Word64] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Word64]
known) ByteString
bs of
Left TlvError
e -> DecodeError -> Either DecodeError (TlvStream, ByteString)
forall a b. a -> Either a b
Left (TlvError -> DecodeError
DecodeTlvError TlvError
e)
Right TlvStream
s -> (TlvStream, ByteString)
-> Either DecodeError (TlvStream, ByteString)
forall a b. b -> Either a b
Right (TlvStream
s, ByteString
BS.empty)
{-# INLINE tlvs #-}
unknown :: [Word64] -> TlvStream -> TlvStream
unknown :: [Word64] -> TlvStream -> TlvStream
unknown [Word64]
known = (Word64 -> Bool) -> TlvStream -> TlvStream
BOLT1.filter_tlv_stream (Word64 -> [Word64] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [Word64]
known)
{-# INLINE unknown #-}
record :: Word64 -> (BS.ByteString -> Maybe a) -> TlvStream -> Parser (Maybe a)
record :: forall a.
Word64 -> (ByteString -> Maybe a) -> TlvStream -> Parser (Maybe a)
record Word64
t ByteString -> Maybe a
f TlvStream
s = case Word64 -> TlvStream -> Maybe ByteString
BOLT1.lookup_tlv Word64
t TlvStream
s of
Maybe ByteString
Nothing -> Maybe a -> Parser (Maybe a)
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe a
forall a. Maybe a
Nothing
Just ByteString
v -> Parser (Maybe a)
-> (a -> Parser (Maybe a)) -> Maybe a -> Parser (Maybe a)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (DecodeError -> Parser (Maybe a)
forall a. DecodeError -> Parser a
failure (Word64 -> DecodeError
DecodeInvalidTlvValue Word64
t)) (Maybe a -> Parser (Maybe a)
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe a -> Parser (Maybe a))
-> (a -> Maybe a) -> a -> Parser (Maybe a)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Maybe a
forall a. a -> Maybe a
Just) (ByteString -> Maybe a
f ByteString
v)
{-# INLINE record #-}
flag :: Word64 -> TlvStream -> Parser Bool
flag :: Word64 -> TlvStream -> Parser Bool
flag Word64
t TlvStream
s = (Maybe () -> Bool) -> Parser (Maybe ()) -> Parser Bool
forall a b. (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Bool -> (() -> Bool) -> Maybe () -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False (Bool -> () -> Bool
forall a b. a -> b -> a
const Bool
True)) (Word64
-> (ByteString -> Maybe ()) -> TlvStream -> Parser (Maybe ())
forall a.
Word64 -> (ByteString -> Maybe a) -> TlvStream -> Parser (Maybe a)
record Word64
t ByteString -> Maybe ()
empty_value TlvStream
s)
where
empty_value :: ByteString -> Maybe ()
empty_value ByteString
v
| ByteString -> Bool
BS.null ByteString
v = () -> Maybe ()
forall a. a -> Maybe a
Just ()
| Bool
otherwise = Maybe ()
forall a. Maybe a
Nothing
{-# INLINE flag #-}
whole :: (BS.ByteString -> Maybe (a, BS.ByteString)) -> BS.ByteString
-> Maybe a
whole :: forall a.
(ByteString -> Maybe (a, ByteString)) -> ByteString -> Maybe a
whole ByteString -> Maybe (a, ByteString)
f ByteString
v = case ByteString -> Maybe (a, ByteString)
f ByteString
v of
Just (a
a, ByteString
r) | ByteString -> Bool
BS.null ByteString
r -> a -> Maybe a
forall a. a -> Maybe a
Just a
a
Maybe (a, ByteString)
_ -> Maybe a
forall a. Maybe a
Nothing
{-# INLINE whole #-}
tlv_point :: BS.ByteString -> Maybe Point
tlv_point :: ByteString -> Maybe Point
tlv_point ByteString
v
| ByteString -> Int
BS.length ByteString
v Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
33 = ByteString -> Maybe Point
BOLT1.point ByteString
v
| Bool
otherwise = Maybe Point
forall a. Maybe a
Nothing
tlv_s64 :: BS.ByteString -> Maybe Int64
tlv_s64 :: ByteString -> Maybe Int64
tlv_s64 = (ByteString -> Maybe (Int64, ByteString))
-> ByteString -> Maybe Int64
forall a.
(ByteString -> Maybe (a, ByteString)) -> ByteString -> Maybe a
whole ByteString -> Maybe (Int64, ByteString)
BOLT1.decode_s64
tlv_fee_range :: BS.ByteString -> Maybe FeeRange
tlv_fee_range :: ByteString -> Maybe FeeRange
tlv_fee_range ByteString
v = do
(lo, r0) <- ByteString -> Maybe (Satoshi, ByteString)
BOLT1.decode_satoshi ByteString
v
hi <- whole BOLT1.decode_satoshi r0
pure (FeeRange lo hi)
tlv_next_funding :: BS.ByteString -> Maybe NextFunding
tlv_next_funding :: ByteString -> Maybe NextFunding
tlv_next_funding ByteString
v
| ByteString -> Int
BS.length ByteString
v Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
33 = do
t <- ByteString -> Maybe TxId
mk_txid (Int -> ByteString -> ByteString
BU.unsafeTake Int
32 ByteString
v)
pure (NextFunding t (BU.unsafeIndex v 32))
| Bool
otherwise = Maybe NextFunding
forall a. Maybe a
Nothing
u16_prefixed :: BS.ByteString -> Either EncodeError BS.ByteString
u16_prefixed :: ByteString -> Either EncodeError ByteString
u16_prefixed ByteString
bs = Either EncodeError ByteString
-> (ByteString -> Either EncodeError ByteString)
-> Maybe ByteString
-> Either EncodeError ByteString
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (EncodeError -> Either EncodeError ByteString
forall a b. a -> Either a b
Left EncodeError
EncodeLengthOverflow) ByteString -> Either EncodeError ByteString
forall a b. b -> Either a b
Right
(ByteString -> Maybe ByteString
BOLT1.encode_u16_prefixed ByteString
bs)
{-# INLINE u16_prefixed #-}
script_bytes :: ScriptPubKey -> BS.ByteString
script_bytes :: ScriptPubKey -> ByteString
script_bytes ScriptPubKey
s =
let !b :: ByteString
b = ScriptPubKey -> ByteString
un_script_pubkey ScriptPubKey
s
in Word16 -> ByteString
BOLT1.encode_u16 (Int -> Word16
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
BS.length ByteString
b)) ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
b
{-# INLINE script_bytes #-}
count_prefix :: [a] -> Either EncodeError BS.ByteString
count_prefix :: forall a. [a] -> Either EncodeError ByteString
count_prefix [a]
xs
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
65535 = EncodeError -> Either EncodeError ByteString
forall a b. a -> Either a b
Left EncodeError
EncodeLengthOverflow
| Bool
otherwise = ByteString -> Either EncodeError ByteString
forall a b. b -> Either a b
Right (Word16 -> ByteString
BOLT1.encode_u16 (Int -> Word16
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
n))
where
!n :: Int
n = [a] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [a]
xs
{-# INLINE count_prefix #-}
txid_bytes :: TxId -> BS.ByteString
txid_bytes :: TxId -> ByteString
txid_bytes = TxId -> ByteString
un_txid
{-# INLINE txid_bytes #-}
word8 :: Word8 -> BS.ByteString
word8 :: Word8 -> ByteString
word8 = Word8 -> ByteString
BS.singleton
{-# INLINE word8 #-}
encode_tlvs
:: [Word64] -> [TlvRecord] -> TlvStream -> Either EncodeError BS.ByteString
encode_tlvs :: [Word64]
-> [TlvRecord] -> TlvStream -> Either EncodeError ByteString
encode_tlvs [Word64]
known [TlvRecord]
recs TlvStream
extra
| (TlvRecord -> Bool) -> [TlvRecord] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any ((Word64 -> [Word64] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Word64]
known) (Word64 -> Bool) -> (TlvRecord -> Word64) -> TlvRecord -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TlvRecord -> Word64
tlv_type) [TlvRecord]
others = EncodeError -> Either EncodeError ByteString
forall a b. a -> Either a b
Left EncodeError
EncodeInvalidTlvs
| Bool
otherwise = case [TlvRecord] -> Maybe TlvStream
BOLT1.tlv_stream ([TlvRecord]
recs [TlvRecord] -> [TlvRecord] -> [TlvRecord]
forall a. Semigroup a => a -> a -> a
<> [TlvRecord]
others) of
Maybe TlvStream
Nothing -> EncodeError -> Either EncodeError ByteString
forall a b. a -> Either a b
Left EncodeError
EncodeInvalidTlvs
Just TlvStream
s -> ByteString -> Either EncodeError ByteString
forall a b. b -> Either a b
Right (TlvStream -> ByteString
BOLT1.encode_tlv_stream TlvStream
s)
where
others :: [TlvRecord]
others = TlvStream -> [TlvRecord]
BOLT1.un_tlv_stream TlvStream
extra
{-# INLINE encode_tlvs #-}
opt :: Word64 -> (a -> BS.ByteString) -> Maybe a -> [TlvRecord]
opt :: forall a. Word64 -> (a -> ByteString) -> Maybe a -> [TlvRecord]
opt Word64
t a -> ByteString
f = [TlvRecord] -> (a -> [TlvRecord]) -> Maybe a -> [TlvRecord]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] (\a
a -> [Word64 -> ByteString -> TlvRecord
TlvRecord Word64
t (a -> ByteString
f a
a)])
{-# INLINE opt #-}
opt_flag :: Word64 -> Bool -> [TlvRecord]
opt_flag :: Word64 -> Bool -> [TlvRecord]
opt_flag Word64
t Bool
b = if Bool
b then [Word64 -> ByteString -> TlvRecord
TlvRecord Word64
t ByteString
BS.empty] else []
{-# INLINE opt_flag #-}
fee_range_bytes :: FeeRange -> BS.ByteString
fee_range_bytes :: FeeRange -> ByteString
fee_range_bytes (FeeRange Satoshi
lo Satoshi
hi) =
Satoshi -> ByteString
BOLT1.encode_satoshi Satoshi
lo ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Satoshi -> ByteString
BOLT1.encode_satoshi Satoshi
hi
next_funding_bytes :: NextFunding -> BS.ByteString
next_funding_bytes :: NextFunding -> ByteString
next_funding_bytes (NextFunding TxId
t Word8
f) = TxId -> ByteString
txid_bytes TxId
t ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> Word8 -> ByteString
word8 Word8
f
encode_message :: Message -> Either EncodeError BS.ByteString
encode_message :: Message -> Either EncodeError ByteString
encode_message Message
m = do
payload <- case Message
m of
MsgOpenChannel OpenChannel
a -> OpenChannel -> Either EncodeError ByteString
encode_open_channel OpenChannel
a
MsgAcceptChannel AcceptChannel
a -> AcceptChannel -> Either EncodeError ByteString
encode_accept_channel AcceptChannel
a
MsgFundingCreated FundingCreated
a -> ByteString -> Either EncodeError ByteString
forall a b. b -> Either a b
Right (FundingCreated -> ByteString
encode_funding_created FundingCreated
a)
MsgFundingSigned FundingSigned
a -> ByteString -> Either EncodeError ByteString
forall a b. b -> Either a b
Right (FundingSigned -> ByteString
encode_funding_signed FundingSigned
a)
MsgChannelReady ChannelReady
a -> ChannelReady -> Either EncodeError ByteString
encode_channel_ready ChannelReady
a
MsgOpenChannel2 OpenChannel2
a -> OpenChannel2 -> Either EncodeError ByteString
encode_open_channel2 OpenChannel2
a
MsgAcceptChannel2 AcceptChannel2
a -> AcceptChannel2 -> Either EncodeError ByteString
encode_accept_channel2 AcceptChannel2
a
MsgTxAddInput TxAddInput
a -> TxAddInput -> Either EncodeError ByteString
encode_tx_add_input TxAddInput
a
MsgTxAddOutput TxAddOutput
a -> ByteString -> Either EncodeError ByteString
forall a b. b -> Either a b
Right (TxAddOutput -> ByteString
encode_tx_add_output TxAddOutput
a)
MsgTxRemoveInput TxRemoveInput
a -> ByteString -> Either EncodeError ByteString
forall a b. b -> Either a b
Right (TxRemoveInput -> ByteString
encode_tx_remove_input TxRemoveInput
a)
MsgTxRemoveOutput TxRemoveOutput
a -> ByteString -> Either EncodeError ByteString
forall a b. b -> Either a b
Right (TxRemoveOutput -> ByteString
encode_tx_remove_output TxRemoveOutput
a)
MsgTxComplete TxComplete
a -> ByteString -> Either EncodeError ByteString
forall a b. b -> Either a b
Right (TxComplete -> ByteString
encode_tx_complete TxComplete
a)
MsgTxSignatures TxSignatures
a -> TxSignatures -> Either EncodeError ByteString
encode_tx_signatures TxSignatures
a
MsgTxInitRbf TxInitRbf
a -> TxInitRbf -> Either EncodeError ByteString
encode_tx_init_rbf TxInitRbf
a
MsgTxAckRbf TxAckRbf
a -> TxAckRbf -> Either EncodeError ByteString
encode_tx_ack_rbf TxAckRbf
a
MsgTxAbort TxAbort
a -> TxAbort -> Either EncodeError ByteString
encode_tx_abort TxAbort
a
MsgStfu Stfu
a -> ByteString -> Either EncodeError ByteString
forall a b. b -> Either a b
Right (Stfu -> ByteString
encode_stfu Stfu
a)
MsgShutdown Shutdown
a -> ByteString -> Either EncodeError ByteString
forall a b. b -> Either a b
Right (Shutdown -> ByteString
encode_shutdown Shutdown
a)
MsgClosingComplete ClosingComplete
a -> ClosingComplete -> Either EncodeError ByteString
encode_closing_complete ClosingComplete
a
MsgClosingSig ClosingSig
a -> ClosingSig -> Either EncodeError ByteString
encode_closing_sig ClosingSig
a
MsgClosingSigned ClosingSigned
a -> ClosingSigned -> Either EncodeError ByteString
encode_closing_signed ClosingSigned
a
MsgUpdateAddHtlc UpdateAddHtlc
a -> UpdateAddHtlc -> Either EncodeError ByteString
encode_update_add_htlc UpdateAddHtlc
a
MsgUpdateFulfillHtlc UpdateFulfillHtlc
a -> UpdateFulfillHtlc -> Either EncodeError ByteString
encode_update_fulfill_htlc UpdateFulfillHtlc
a
MsgUpdateFailHtlc UpdateFailHtlc
a -> UpdateFailHtlc -> Either EncodeError ByteString
encode_update_fail_htlc UpdateFailHtlc
a
MsgUpdateFailMalformedHtlc UpdateFailMalformedHtlc
a ->
ByteString -> Either EncodeError ByteString
forall a b. b -> Either a b
Right (UpdateFailMalformedHtlc -> ByteString
encode_update_fail_malformed_htlc UpdateFailMalformedHtlc
a)
MsgCommitmentSigned CommitmentSigned
a -> CommitmentSigned -> Either EncodeError ByteString
encode_commitment_signed CommitmentSigned
a
MsgRevokeAndAck RevokeAndAck
a -> ByteString -> Either EncodeError ByteString
forall a b. b -> Either a b
Right (RevokeAndAck -> ByteString
encode_revoke_and_ack RevokeAndAck
a)
MsgUpdateFee UpdateFee
a -> ByteString -> Either EncodeError ByteString
forall a b. b -> Either a b
Right (UpdateFee -> ByteString
encode_update_fee UpdateFee
a)
MsgChannelReestablish ChannelReestablish
a -> ChannelReestablish -> Either EncodeError ByteString
encode_channel_reestablish ChannelReestablish
a
case BOLT1.encode_envelope (message_type m) payload of
Left EncodeError
_ -> EncodeError -> Either EncodeError ByteString
forall a b. a -> Either a b
Left EncodeError
EncodeMessageTooLarge
Right ByteString
w -> ByteString -> Either EncodeError ByteString
forall a b. b -> Either a b
Right ByteString
w
decode_message :: BS.ByteString -> Either DecodeError Message
decode_message :: ByteString -> Either DecodeError Message
decode_message ByteString
bs = do
(t, payload) <- case ByteString -> Either DecodeError (Word16, ByteString)
BOLT1.decode_envelope ByteString
bs of
Left DecodeError
_ -> DecodeError -> Either DecodeError (Word16, ByteString)
forall a b. a -> Either a b
Left DecodeError
DecodeInsufficientBytes
Right (Word16, ByteString)
r -> (Word16, ByteString) -> Either DecodeError (Word16, ByteString)
forall a b. b -> Either a b
Right (Word16, ByteString)
r
case t of
Word16
32 -> OpenChannel -> Message
MsgOpenChannel (OpenChannel -> Message)
-> Either DecodeError OpenChannel -> Either DecodeError Message
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either DecodeError OpenChannel
decode_open_channel ByteString
payload
Word16
33 -> AcceptChannel -> Message
MsgAcceptChannel (AcceptChannel -> Message)
-> Either DecodeError AcceptChannel -> Either DecodeError Message
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either DecodeError AcceptChannel
decode_accept_channel ByteString
payload
Word16
34 -> FundingCreated -> Message
MsgFundingCreated (FundingCreated -> Message)
-> Either DecodeError FundingCreated -> Either DecodeError Message
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either DecodeError FundingCreated
decode_funding_created ByteString
payload
Word16
35 -> FundingSigned -> Message
MsgFundingSigned (FundingSigned -> Message)
-> Either DecodeError FundingSigned -> Either DecodeError Message
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either DecodeError FundingSigned
decode_funding_signed ByteString
payload
Word16
36 -> ChannelReady -> Message
MsgChannelReady (ChannelReady -> Message)
-> Either DecodeError ChannelReady -> Either DecodeError Message
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either DecodeError ChannelReady
decode_channel_ready ByteString
payload
Word16
64 -> OpenChannel2 -> Message
MsgOpenChannel2 (OpenChannel2 -> Message)
-> Either DecodeError OpenChannel2 -> Either DecodeError Message
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either DecodeError OpenChannel2
decode_open_channel2 ByteString
payload
Word16
65 -> AcceptChannel2 -> Message
MsgAcceptChannel2 (AcceptChannel2 -> Message)
-> Either DecodeError AcceptChannel2 -> Either DecodeError Message
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either DecodeError AcceptChannel2
decode_accept_channel2 ByteString
payload
Word16
66 -> TxAddInput -> Message
MsgTxAddInput (TxAddInput -> Message)
-> Either DecodeError TxAddInput -> Either DecodeError Message
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either DecodeError TxAddInput
decode_tx_add_input ByteString
payload
Word16
67 -> TxAddOutput -> Message
MsgTxAddOutput (TxAddOutput -> Message)
-> Either DecodeError TxAddOutput -> Either DecodeError Message
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either DecodeError TxAddOutput
decode_tx_add_output ByteString
payload
Word16
68 -> TxRemoveInput -> Message
MsgTxRemoveInput (TxRemoveInput -> Message)
-> Either DecodeError TxRemoveInput -> Either DecodeError Message
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either DecodeError TxRemoveInput
decode_tx_remove_input ByteString
payload
Word16
69 -> TxRemoveOutput -> Message
MsgTxRemoveOutput (TxRemoveOutput -> Message)
-> Either DecodeError TxRemoveOutput -> Either DecodeError Message
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either DecodeError TxRemoveOutput
decode_tx_remove_output ByteString
payload
Word16
70 -> TxComplete -> Message
MsgTxComplete (TxComplete -> Message)
-> Either DecodeError TxComplete -> Either DecodeError Message
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either DecodeError TxComplete
decode_tx_complete ByteString
payload
Word16
71 -> TxSignatures -> Message
MsgTxSignatures (TxSignatures -> Message)
-> Either DecodeError TxSignatures -> Either DecodeError Message
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either DecodeError TxSignatures
decode_tx_signatures ByteString
payload
Word16
72 -> TxInitRbf -> Message
MsgTxInitRbf (TxInitRbf -> Message)
-> Either DecodeError TxInitRbf -> Either DecodeError Message
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either DecodeError TxInitRbf
decode_tx_init_rbf ByteString
payload
Word16
73 -> TxAckRbf -> Message
MsgTxAckRbf (TxAckRbf -> Message)
-> Either DecodeError TxAckRbf -> Either DecodeError Message
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either DecodeError TxAckRbf
decode_tx_ack_rbf ByteString
payload
Word16
74 -> TxAbort -> Message
MsgTxAbort (TxAbort -> Message)
-> Either DecodeError TxAbort -> Either DecodeError Message
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either DecodeError TxAbort
decode_tx_abort ByteString
payload
Word16
2 -> Stfu -> Message
MsgStfu (Stfu -> Message)
-> Either DecodeError Stfu -> Either DecodeError Message
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either DecodeError Stfu
decode_stfu ByteString
payload
Word16
38 -> Shutdown -> Message
MsgShutdown (Shutdown -> Message)
-> Either DecodeError Shutdown -> Either DecodeError Message
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either DecodeError Shutdown
decode_shutdown ByteString
payload
Word16
40 -> ClosingComplete -> Message
MsgClosingComplete (ClosingComplete -> Message)
-> Either DecodeError ClosingComplete -> Either DecodeError Message
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either DecodeError ClosingComplete
decode_closing_complete ByteString
payload
Word16
41 -> ClosingSig -> Message
MsgClosingSig (ClosingSig -> Message)
-> Either DecodeError ClosingSig -> Either DecodeError Message
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either DecodeError ClosingSig
decode_closing_sig ByteString
payload
Word16
39 -> ClosingSigned -> Message
MsgClosingSigned (ClosingSigned -> Message)
-> Either DecodeError ClosingSigned -> Either DecodeError Message
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either DecodeError ClosingSigned
decode_closing_signed ByteString
payload
Word16
128 -> UpdateAddHtlc -> Message
MsgUpdateAddHtlc (UpdateAddHtlc -> Message)
-> Either DecodeError UpdateAddHtlc -> Either DecodeError Message
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either DecodeError UpdateAddHtlc
decode_update_add_htlc ByteString
payload
Word16
130 -> UpdateFulfillHtlc -> Message
MsgUpdateFulfillHtlc (UpdateFulfillHtlc -> Message)
-> Either DecodeError UpdateFulfillHtlc
-> Either DecodeError Message
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either DecodeError UpdateFulfillHtlc
decode_update_fulfill_htlc ByteString
payload
Word16
131 -> UpdateFailHtlc -> Message
MsgUpdateFailHtlc (UpdateFailHtlc -> Message)
-> Either DecodeError UpdateFailHtlc -> Either DecodeError Message
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either DecodeError UpdateFailHtlc
decode_update_fail_htlc ByteString
payload
Word16
135 -> UpdateFailMalformedHtlc -> Message
MsgUpdateFailMalformedHtlc (UpdateFailMalformedHtlc -> Message)
-> Either DecodeError UpdateFailMalformedHtlc
-> Either DecodeError Message
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
ByteString -> Either DecodeError UpdateFailMalformedHtlc
decode_update_fail_malformed_htlc ByteString
payload
Word16
132 -> CommitmentSigned -> Message
MsgCommitmentSigned (CommitmentSigned -> Message)
-> Either DecodeError CommitmentSigned
-> Either DecodeError Message
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either DecodeError CommitmentSigned
decode_commitment_signed ByteString
payload
Word16
133 -> RevokeAndAck -> Message
MsgRevokeAndAck (RevokeAndAck -> Message)
-> Either DecodeError RevokeAndAck -> Either DecodeError Message
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either DecodeError RevokeAndAck
decode_revoke_and_ack ByteString
payload
Word16
134 -> UpdateFee -> Message
MsgUpdateFee (UpdateFee -> Message)
-> Either DecodeError UpdateFee -> Either DecodeError Message
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either DecodeError UpdateFee
decode_update_fee ByteString
payload
Word16
136 -> ChannelReestablish -> Message
MsgChannelReestablish (ChannelReestablish -> Message)
-> Either DecodeError ChannelReestablish
-> Either DecodeError Message
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either DecodeError ChannelReestablish
decode_channel_reestablish ByteString
payload
Word16
_ | Word16 -> Bool
forall a. Integral a => a -> Bool
even Word16
t -> DecodeError -> Either DecodeError Message
forall a b. a -> Either a b
Left (Word16 -> DecodeError
DecodeUnknownEvenType Word16
t)
| Bool
otherwise -> DecodeError -> Either DecodeError Message
forall a b. a -> Either a b
Left (Word16 -> DecodeError
DecodeUnknownOddType Word16
t)
open_channel_known :: [Word64]
open_channel_known :: [Word64]
open_channel_known = [Word64
0, Word64
1]
encode_open_channel :: OpenChannel -> Either EncodeError BS.ByteString
encode_open_channel :: OpenChannel -> Either EncodeError ByteString
encode_open_channel OpenChannel
m = do
ts <- [Word64]
-> [TlvRecord] -> TlvStream -> Either EncodeError ByteString
encode_tlvs [Word64]
open_channel_known
( Word64
-> (ScriptPubKey -> ByteString)
-> Maybe ScriptPubKey
-> [TlvRecord]
forall a. Word64 -> (a -> ByteString) -> Maybe a -> [TlvRecord]
opt Word64
0 ScriptPubKey -> ByteString
un_script_pubkey (OpenChannel -> Maybe ScriptPubKey
open_channel_upfront_shutdown_script OpenChannel
m)
[TlvRecord] -> [TlvRecord] -> [TlvRecord]
forall a. Semigroup a => a -> a -> a
<> Word64
-> (FeatureVector -> ByteString)
-> Maybe FeatureVector
-> [TlvRecord]
forall a. Word64 -> (a -> ByteString) -> Maybe a -> [TlvRecord]
opt Word64
1 FeatureVector -> ByteString
BOLT9.render (OpenChannel -> Maybe FeatureVector
open_channel_channel_type OpenChannel
m) )
(OpenChannel -> TlvStream
open_channel_tlvs OpenChannel
m)
pure $ mconcat [
BOLT1.un_chain_hash (open_channel_chain_hash m)
, BOLT1.un_channel_id (open_channel_temporary_channel_id m)
, BOLT1.encode_satoshi (open_channel_funding_satoshis m)
, BOLT1.encode_milli_satoshi (open_channel_push_msat m)
, BOLT1.encode_satoshi (open_channel_dust_limit_satoshis m)
, BOLT1.encode_u64 (open_channel_max_htlc_value_in_flight_msat m)
, BOLT1.encode_satoshi (open_channel_channel_reserve_satoshis m)
, BOLT1.encode_milli_satoshi (open_channel_htlc_minimum_msat m)
, BOLT1.encode_u32 (open_channel_feerate_per_kw m)
, BOLT1.encode_u16 (open_channel_to_self_delay m)
, BOLT1.encode_u16 (open_channel_max_accepted_htlcs m)
, BOLT1.un_point (open_channel_funding_pubkey m)
, BOLT1.un_point (open_channel_revocation_basepoint m)
, BOLT1.un_point (open_channel_payment_basepoint m)
, BOLT1.un_point (open_channel_delayed_payment_basepoint m)
, BOLT1.un_point (open_channel_htlc_basepoint m)
, BOLT1.un_point (open_channel_first_per_commitment_point m)
, word8 (open_channel_channel_flags m)
, ts
]
decode_open_channel :: BS.ByteString -> Either DecodeError OpenChannel
decode_open_channel :: ByteString -> Either DecodeError OpenChannel
decode_open_channel = Parser OpenChannel -> ByteString -> Either DecodeError OpenChannel
forall a. Parser a -> ByteString -> Either DecodeError a
parse (Parser OpenChannel
-> ByteString -> Either DecodeError OpenChannel)
-> Parser OpenChannel
-> ByteString
-> Either DecodeError OpenChannel
forall a b. (a -> b) -> a -> b
$ do
ch <- Parser ChainHash
chain_hash
tcid <- channel_id
fund <- satoshi
push <- milli_satoshi
dust <- satoshi
maxv <- u64
res <- satoshi
hmin <- milli_satoshi
fee <- u32
delay <- u16
maxa <- u16
fpk <- point
rev <- point
pay <- point
del <- point
htlc <- point
pcp <- point
flags <- u8
s <- tlvs open_channel_known
shut <- record 0 script_pubkey s
ctype <- record 1 (Just . BOLT9.parse) s
pure OpenChannel {
open_channel_chain_hash = ch
, open_channel_temporary_channel_id = tcid
, open_channel_funding_satoshis = fund
, open_channel_push_msat = push
, open_channel_dust_limit_satoshis = dust
, open_channel_max_htlc_value_in_flight_msat = maxv
, open_channel_channel_reserve_satoshis = res
, open_channel_htlc_minimum_msat = hmin
, open_channel_feerate_per_kw = fee
, open_channel_to_self_delay = delay
, open_channel_max_accepted_htlcs = maxa
, open_channel_funding_pubkey = fpk
, open_channel_revocation_basepoint = rev
, open_channel_payment_basepoint = pay
, open_channel_delayed_payment_basepoint = del
, open_channel_htlc_basepoint = htlc
, open_channel_first_per_commitment_point = pcp
, open_channel_channel_flags = flags
, open_channel_upfront_shutdown_script = shut
, open_channel_channel_type = ctype
, open_channel_tlvs = unknown open_channel_known s
}
accept_channel_known :: [Word64]
accept_channel_known :: [Word64]
accept_channel_known = [Word64
0, Word64
1]
encode_accept_channel :: AcceptChannel -> Either EncodeError BS.ByteString
encode_accept_channel :: AcceptChannel -> Either EncodeError ByteString
encode_accept_channel AcceptChannel
m = do
ts <- [Word64]
-> [TlvRecord] -> TlvStream -> Either EncodeError ByteString
encode_tlvs [Word64]
accept_channel_known
( Word64
-> (ScriptPubKey -> ByteString)
-> Maybe ScriptPubKey
-> [TlvRecord]
forall a. Word64 -> (a -> ByteString) -> Maybe a -> [TlvRecord]
opt Word64
0 ScriptPubKey -> ByteString
un_script_pubkey (AcceptChannel -> Maybe ScriptPubKey
accept_channel_upfront_shutdown_script AcceptChannel
m)
[TlvRecord] -> [TlvRecord] -> [TlvRecord]
forall a. Semigroup a => a -> a -> a
<> Word64
-> (FeatureVector -> ByteString)
-> Maybe FeatureVector
-> [TlvRecord]
forall a. Word64 -> (a -> ByteString) -> Maybe a -> [TlvRecord]
opt Word64
1 FeatureVector -> ByteString
BOLT9.render (AcceptChannel -> Maybe FeatureVector
accept_channel_channel_type AcceptChannel
m) )
(AcceptChannel -> TlvStream
accept_channel_tlvs AcceptChannel
m)
pure $ mconcat [
BOLT1.un_channel_id (accept_channel_temporary_channel_id m)
, BOLT1.encode_satoshi (accept_channel_dust_limit_satoshis m)
, BOLT1.encode_u64 (accept_channel_max_htlc_value_in_flight_msat m)
, BOLT1.encode_satoshi (accept_channel_channel_reserve_satoshis m)
, BOLT1.encode_milli_satoshi (accept_channel_htlc_minimum_msat m)
, BOLT1.encode_u32 (accept_channel_minimum_depth m)
, BOLT1.encode_u16 (accept_channel_to_self_delay m)
, BOLT1.encode_u16 (accept_channel_max_accepted_htlcs m)
, BOLT1.un_point (accept_channel_funding_pubkey m)
, BOLT1.un_point (accept_channel_revocation_basepoint m)
, BOLT1.un_point (accept_channel_payment_basepoint m)
, BOLT1.un_point (accept_channel_delayed_payment_basepoint m)
, BOLT1.un_point (accept_channel_htlc_basepoint m)
, BOLT1.un_point (accept_channel_first_per_commitment_point m)
, ts
]
decode_accept_channel :: BS.ByteString -> Either DecodeError AcceptChannel
decode_accept_channel :: ByteString -> Either DecodeError AcceptChannel
decode_accept_channel = Parser AcceptChannel
-> ByteString -> Either DecodeError AcceptChannel
forall a. Parser a -> ByteString -> Either DecodeError a
parse (Parser AcceptChannel
-> ByteString -> Either DecodeError AcceptChannel)
-> Parser AcceptChannel
-> ByteString
-> Either DecodeError AcceptChannel
forall a b. (a -> b) -> a -> b
$ do
tcid <- Parser ChannelId
channel_id
dust <- satoshi
maxv <- u64
res <- satoshi
hmin <- milli_satoshi
depth <- u32
delay <- u16
maxa <- u16
fpk <- point
rev <- point
pay <- point
del <- point
htlc <- point
pcp <- point
s <- tlvs accept_channel_known
shut <- record 0 script_pubkey s
ctype <- record 1 (Just . BOLT9.parse) s
pure AcceptChannel {
accept_channel_temporary_channel_id = tcid
, accept_channel_dust_limit_satoshis = dust
, accept_channel_max_htlc_value_in_flight_msat = maxv
, accept_channel_channel_reserve_satoshis = res
, accept_channel_htlc_minimum_msat = hmin
, accept_channel_minimum_depth = depth
, accept_channel_to_self_delay = delay
, accept_channel_max_accepted_htlcs = maxa
, accept_channel_funding_pubkey = fpk
, accept_channel_revocation_basepoint = rev
, accept_channel_payment_basepoint = pay
, accept_channel_delayed_payment_basepoint = del
, accept_channel_htlc_basepoint = htlc
, accept_channel_first_per_commitment_point = pcp
, accept_channel_upfront_shutdown_script = shut
, accept_channel_channel_type = ctype
, accept_channel_tlvs = unknown accept_channel_known s
}
encode_funding_created :: FundingCreated -> BS.ByteString
encode_funding_created :: FundingCreated -> ByteString
encode_funding_created FundingCreated
m = [ByteString] -> ByteString
forall a. Monoid a => [a] -> a
mconcat [
ChannelId -> ByteString
BOLT1.un_channel_id (FundingCreated -> ChannelId
funding_created_temporary_channel_id FundingCreated
m)
, TxId -> ByteString
txid_bytes (FundingCreated -> TxId
funding_created_funding_txid FundingCreated
m)
, Word16 -> ByteString
BOLT1.encode_u16 (FundingCreated -> Word16
funding_created_funding_output_index FundingCreated
m)
, Signature -> ByteString
BOLT1.un_signature (FundingCreated -> Signature
funding_created_signature FundingCreated
m)
, TlvStream -> ByteString
BOLT1.encode_tlv_stream (FundingCreated -> TlvStream
funding_created_tlvs FundingCreated
m)
]
decode_funding_created :: BS.ByteString -> Either DecodeError FundingCreated
decode_funding_created :: ByteString -> Either DecodeError FundingCreated
decode_funding_created = Parser FundingCreated
-> ByteString -> Either DecodeError FundingCreated
forall a. Parser a -> ByteString -> Either DecodeError a
parse (Parser FundingCreated
-> ByteString -> Either DecodeError FundingCreated)
-> Parser FundingCreated
-> ByteString
-> Either DecodeError FundingCreated
forall a b. (a -> b) -> a -> b
$
ChannelId
-> TxId -> Word16 -> Signature -> TlvStream -> FundingCreated
FundingCreated (ChannelId
-> TxId -> Word16 -> Signature -> TlvStream -> FundingCreated)
-> Parser ChannelId
-> Parser
(TxId -> Word16 -> Signature -> TlvStream -> FundingCreated)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ChannelId
channel_id Parser (TxId -> Word16 -> Signature -> TlvStream -> FundingCreated)
-> Parser TxId
-> Parser (Word16 -> Signature -> TlvStream -> FundingCreated)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser TxId
txid Parser (Word16 -> Signature -> TlvStream -> FundingCreated)
-> Parser Word16
-> Parser (Signature -> TlvStream -> FundingCreated)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Word16
u16 Parser (Signature -> TlvStream -> FundingCreated)
-> Parser Signature -> Parser (TlvStream -> FundingCreated)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Signature
signature Parser (TlvStream -> FundingCreated)
-> Parser TlvStream -> Parser FundingCreated
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> [Word64] -> Parser TlvStream
tlvs []
encode_funding_signed :: FundingSigned -> BS.ByteString
encode_funding_signed :: FundingSigned -> ByteString
encode_funding_signed FundingSigned
m = [ByteString] -> ByteString
forall a. Monoid a => [a] -> a
mconcat [
ChannelId -> ByteString
BOLT1.un_channel_id (FundingSigned -> ChannelId
funding_signed_channel_id FundingSigned
m)
, Signature -> ByteString
BOLT1.un_signature (FundingSigned -> Signature
funding_signed_signature FundingSigned
m)
, TlvStream -> ByteString
BOLT1.encode_tlv_stream (FundingSigned -> TlvStream
funding_signed_tlvs FundingSigned
m)
]
decode_funding_signed :: BS.ByteString -> Either DecodeError FundingSigned
decode_funding_signed :: ByteString -> Either DecodeError FundingSigned
decode_funding_signed = Parser FundingSigned
-> ByteString -> Either DecodeError FundingSigned
forall a. Parser a -> ByteString -> Either DecodeError a
parse (Parser FundingSigned
-> ByteString -> Either DecodeError FundingSigned)
-> Parser FundingSigned
-> ByteString
-> Either DecodeError FundingSigned
forall a b. (a -> b) -> a -> b
$
ChannelId -> Signature -> TlvStream -> FundingSigned
FundingSigned (ChannelId -> Signature -> TlvStream -> FundingSigned)
-> Parser ChannelId
-> Parser (Signature -> TlvStream -> FundingSigned)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ChannelId
channel_id Parser (Signature -> TlvStream -> FundingSigned)
-> Parser Signature -> Parser (TlvStream -> FundingSigned)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Signature
signature Parser (TlvStream -> FundingSigned)
-> Parser TlvStream -> Parser FundingSigned
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> [Word64] -> Parser TlvStream
tlvs []
channel_ready_known :: [Word64]
channel_ready_known :: [Word64]
channel_ready_known = [Word64
1]
encode_channel_ready :: ChannelReady -> Either EncodeError BS.ByteString
encode_channel_ready :: ChannelReady -> Either EncodeError ByteString
encode_channel_ready ChannelReady
m = do
ts <- [Word64]
-> [TlvRecord] -> TlvStream -> Either EncodeError ByteString
encode_tlvs [Word64]
channel_ready_known
(Word64
-> (ShortChannelId -> ByteString)
-> Maybe ShortChannelId
-> [TlvRecord]
forall a. Word64 -> (a -> ByteString) -> Maybe a -> [TlvRecord]
opt Word64
1 ShortChannelId -> ByteString
BOLT1.encode_short_channel_id (ChannelReady -> Maybe ShortChannelId
channel_ready_alias ChannelReady
m))
(ChannelReady -> TlvStream
channel_ready_tlvs ChannelReady
m)
pure $ mconcat [
BOLT1.un_channel_id (channel_ready_channel_id m)
, BOLT1.un_point (channel_ready_second_per_commitment_point m)
, ts
]
decode_channel_ready :: BS.ByteString -> Either DecodeError ChannelReady
decode_channel_ready :: ByteString -> Either DecodeError ChannelReady
decode_channel_ready = Parser ChannelReady
-> ByteString -> Either DecodeError ChannelReady
forall a. Parser a -> ByteString -> Either DecodeError a
parse (Parser ChannelReady
-> ByteString -> Either DecodeError ChannelReady)
-> Parser ChannelReady
-> ByteString
-> Either DecodeError ChannelReady
forall a b. (a -> b) -> a -> b
$ do
cid <- Parser ChannelId
channel_id
pcp <- point
s <- tlvs channel_ready_known
alias <- record 1 (whole BOLT1.decode_short_channel_id) s
pure (ChannelReady cid pcp alias (unknown channel_ready_known s))
opening_known :: [Word64]
opening_known :: [Word64]
opening_known = [Word64
0, Word64
1, Word64
2]
encode_open_channel2 :: OpenChannel2 -> Either EncodeError BS.ByteString
encode_open_channel2 :: OpenChannel2 -> Either EncodeError ByteString
encode_open_channel2 OpenChannel2
m = do
ts <- [Word64]
-> [TlvRecord] -> TlvStream -> Either EncodeError ByteString
encode_tlvs [Word64]
opening_known
( Word64
-> (ScriptPubKey -> ByteString)
-> Maybe ScriptPubKey
-> [TlvRecord]
forall a. Word64 -> (a -> ByteString) -> Maybe a -> [TlvRecord]
opt Word64
0 ScriptPubKey -> ByteString
un_script_pubkey (OpenChannel2 -> Maybe ScriptPubKey
open_channel2_upfront_shutdown_script OpenChannel2
m)
[TlvRecord] -> [TlvRecord] -> [TlvRecord]
forall a. Semigroup a => a -> a -> a
<> Word64
-> (FeatureVector -> ByteString)
-> Maybe FeatureVector
-> [TlvRecord]
forall a. Word64 -> (a -> ByteString) -> Maybe a -> [TlvRecord]
opt Word64
1 FeatureVector -> ByteString
BOLT9.render (OpenChannel2 -> Maybe FeatureVector
open_channel2_channel_type OpenChannel2
m)
[TlvRecord] -> [TlvRecord] -> [TlvRecord]
forall a. Semigroup a => a -> a -> a
<> Word64 -> Bool -> [TlvRecord]
opt_flag Word64
2 (OpenChannel2 -> Bool
open_channel2_require_confirmed_inputs OpenChannel2
m) )
(OpenChannel2 -> TlvStream
open_channel2_tlvs OpenChannel2
m)
pure $ mconcat [
BOLT1.un_chain_hash (open_channel2_chain_hash m)
, BOLT1.un_channel_id (open_channel2_temporary_channel_id m)
, BOLT1.encode_u32 (open_channel2_funding_feerate_perkw m)
, BOLT1.encode_u32 (open_channel2_commitment_feerate_perkw m)
, BOLT1.encode_satoshi (open_channel2_funding_satoshis m)
, BOLT1.encode_satoshi (open_channel2_dust_limit_satoshis m)
, BOLT1.encode_u64 (open_channel2_max_htlc_value_in_flight_msat m)
, BOLT1.encode_milli_satoshi (open_channel2_htlc_minimum_msat m)
, BOLT1.encode_u16 (open_channel2_to_self_delay m)
, BOLT1.encode_u16 (open_channel2_max_accepted_htlcs m)
, BOLT1.encode_u32 (open_channel2_locktime m)
, BOLT1.un_point (open_channel2_funding_pubkey m)
, BOLT1.un_point (open_channel2_revocation_basepoint m)
, BOLT1.un_point (open_channel2_payment_basepoint m)
, BOLT1.un_point (open_channel2_delayed_payment_basepoint m)
, BOLT1.un_point (open_channel2_htlc_basepoint m)
, BOLT1.un_point (open_channel2_first_per_commitment_point m)
, BOLT1.un_point (open_channel2_second_per_commitment_point m)
, word8 (open_channel2_channel_flags m)
, ts
]
decode_open_channel2 :: BS.ByteString -> Either DecodeError OpenChannel2
decode_open_channel2 :: ByteString -> Either DecodeError OpenChannel2
decode_open_channel2 = Parser OpenChannel2
-> ByteString -> Either DecodeError OpenChannel2
forall a. Parser a -> ByteString -> Either DecodeError a
parse (Parser OpenChannel2
-> ByteString -> Either DecodeError OpenChannel2)
-> Parser OpenChannel2
-> ByteString
-> Either DecodeError OpenChannel2
forall a b. (a -> b) -> a -> b
$ do
ch <- Parser ChainHash
chain_hash
tcid <- channel_id
ffee <- u32
cfee <- u32
fund <- satoshi
dust <- satoshi
maxv <- u64
hmin <- milli_satoshi
delay <- u16
maxa <- u16
lock <- u32
fpk <- point
rev <- point
pay <- point
del <- point
htlc <- point
pcp1 <- point
pcp2 <- point
flags <- u8
s <- tlvs opening_known
shut <- record 0 script_pubkey s
ctype <- record 1 (Just . BOLT9.parse) s
conf <- flag 2 s
pure OpenChannel2 {
open_channel2_chain_hash = ch
, open_channel2_temporary_channel_id = tcid
, open_channel2_funding_feerate_perkw = ffee
, open_channel2_commitment_feerate_perkw = cfee
, open_channel2_funding_satoshis = fund
, open_channel2_dust_limit_satoshis = dust
, open_channel2_max_htlc_value_in_flight_msat = maxv
, open_channel2_htlc_minimum_msat = hmin
, open_channel2_to_self_delay = delay
, open_channel2_max_accepted_htlcs = maxa
, open_channel2_locktime = lock
, open_channel2_funding_pubkey = fpk
, open_channel2_revocation_basepoint = rev
, open_channel2_payment_basepoint = pay
, open_channel2_delayed_payment_basepoint = del
, open_channel2_htlc_basepoint = htlc
, open_channel2_first_per_commitment_point = pcp1
, open_channel2_second_per_commitment_point = pcp2
, open_channel2_channel_flags = flags
, open_channel2_upfront_shutdown_script = shut
, open_channel2_channel_type = ctype
, open_channel2_require_confirmed_inputs = conf
, open_channel2_tlvs = unknown opening_known s
}
encode_accept_channel2 :: AcceptChannel2 -> Either EncodeError BS.ByteString
encode_accept_channel2 :: AcceptChannel2 -> Either EncodeError ByteString
encode_accept_channel2 AcceptChannel2
m = do
ts <- [Word64]
-> [TlvRecord] -> TlvStream -> Either EncodeError ByteString
encode_tlvs [Word64]
opening_known
( Word64
-> (ScriptPubKey -> ByteString)
-> Maybe ScriptPubKey
-> [TlvRecord]
forall a. Word64 -> (a -> ByteString) -> Maybe a -> [TlvRecord]
opt Word64
0 ScriptPubKey -> ByteString
un_script_pubkey (AcceptChannel2 -> Maybe ScriptPubKey
accept_channel2_upfront_shutdown_script AcceptChannel2
m)
[TlvRecord] -> [TlvRecord] -> [TlvRecord]
forall a. Semigroup a => a -> a -> a
<> Word64
-> (FeatureVector -> ByteString)
-> Maybe FeatureVector
-> [TlvRecord]
forall a. Word64 -> (a -> ByteString) -> Maybe a -> [TlvRecord]
opt Word64
1 FeatureVector -> ByteString
BOLT9.render (AcceptChannel2 -> Maybe FeatureVector
accept_channel2_channel_type AcceptChannel2
m)
[TlvRecord] -> [TlvRecord] -> [TlvRecord]
forall a. Semigroup a => a -> a -> a
<> Word64 -> Bool -> [TlvRecord]
opt_flag Word64
2 (AcceptChannel2 -> Bool
accept_channel2_require_confirmed_inputs AcceptChannel2
m) )
(AcceptChannel2 -> TlvStream
accept_channel2_tlvs AcceptChannel2
m)
pure $ mconcat [
BOLT1.un_channel_id (accept_channel2_temporary_channel_id m)
, BOLT1.encode_satoshi (accept_channel2_funding_satoshis m)
, BOLT1.encode_satoshi (accept_channel2_dust_limit_satoshis m)
, BOLT1.encode_u64 (accept_channel2_max_htlc_value_in_flight_msat m)
, BOLT1.encode_milli_satoshi (accept_channel2_htlc_minimum_msat m)
, BOLT1.encode_u32 (accept_channel2_minimum_depth m)
, BOLT1.encode_u16 (accept_channel2_to_self_delay m)
, BOLT1.encode_u16 (accept_channel2_max_accepted_htlcs m)
, BOLT1.un_point (accept_channel2_funding_pubkey m)
, BOLT1.un_point (accept_channel2_revocation_basepoint m)
, BOLT1.un_point (accept_channel2_payment_basepoint m)
, BOLT1.un_point (accept_channel2_delayed_payment_basepoint m)
, BOLT1.un_point (accept_channel2_htlc_basepoint m)
, BOLT1.un_point (accept_channel2_first_per_commitment_point m)
, BOLT1.un_point (accept_channel2_second_per_commitment_point m)
, ts
]
decode_accept_channel2 :: BS.ByteString -> Either DecodeError AcceptChannel2
decode_accept_channel2 :: ByteString -> Either DecodeError AcceptChannel2
decode_accept_channel2 = Parser AcceptChannel2
-> ByteString -> Either DecodeError AcceptChannel2
forall a. Parser a -> ByteString -> Either DecodeError a
parse (Parser AcceptChannel2
-> ByteString -> Either DecodeError AcceptChannel2)
-> Parser AcceptChannel2
-> ByteString
-> Either DecodeError AcceptChannel2
forall a b. (a -> b) -> a -> b
$ do
tcid <- Parser ChannelId
channel_id
fund <- satoshi
dust <- satoshi
maxv <- u64
hmin <- milli_satoshi
depth <- u32
delay <- u16
maxa <- u16
fpk <- point
rev <- point
pay <- point
del <- point
htlc <- point
pcp1 <- point
pcp2 <- point
s <- tlvs opening_known
shut <- record 0 script_pubkey s
ctype <- record 1 (Just . BOLT9.parse) s
conf <- flag 2 s
pure AcceptChannel2 {
accept_channel2_temporary_channel_id = tcid
, accept_channel2_funding_satoshis = fund
, accept_channel2_dust_limit_satoshis = dust
, accept_channel2_max_htlc_value_in_flight_msat = maxv
, accept_channel2_htlc_minimum_msat = hmin
, accept_channel2_minimum_depth = depth
, accept_channel2_to_self_delay = delay
, accept_channel2_max_accepted_htlcs = maxa
, accept_channel2_funding_pubkey = fpk
, accept_channel2_revocation_basepoint = rev
, accept_channel2_payment_basepoint = pay
, accept_channel2_delayed_payment_basepoint = del
, accept_channel2_htlc_basepoint = htlc
, accept_channel2_first_per_commitment_point = pcp1
, accept_channel2_second_per_commitment_point = pcp2
, accept_channel2_upfront_shutdown_script = shut
, accept_channel2_channel_type = ctype
, accept_channel2_require_confirmed_inputs = conf
, accept_channel2_tlvs = unknown opening_known s
}
encode_tx_add_input :: TxAddInput -> Either EncodeError BS.ByteString
encode_tx_add_input :: TxAddInput -> Either EncodeError ByteString
encode_tx_add_input TxAddInput
m = do
prev <- ByteString -> Either EncodeError ByteString
u16_prefixed (TxAddInput -> ByteString
tx_add_input_prevtx TxAddInput
m)
let SerialId sid = tx_add_input_serial_id m
pure $ mconcat [
BOLT1.un_channel_id (tx_add_input_channel_id m)
, BOLT1.encode_u64 sid
, prev
, BOLT1.encode_u32 (tx_add_input_prevtx_vout m)
, BOLT1.encode_u32 (tx_add_input_sequence m)
, BOLT1.encode_tlv_stream (tx_add_input_tlvs m)
]
decode_tx_add_input :: BS.ByteString -> Either DecodeError TxAddInput
decode_tx_add_input :: ByteString -> Either DecodeError TxAddInput
decode_tx_add_input = Parser TxAddInput -> ByteString -> Either DecodeError TxAddInput
forall a. Parser a -> ByteString -> Either DecodeError a
parse (Parser TxAddInput -> ByteString -> Either DecodeError TxAddInput)
-> Parser TxAddInput -> ByteString -> Either DecodeError TxAddInput
forall a b. (a -> b) -> a -> b
$
ChannelId
-> SerialId
-> ByteString
-> Word32
-> Word32
-> TlvStream
-> TxAddInput
TxAddInput (ChannelId
-> SerialId
-> ByteString
-> Word32
-> Word32
-> TlvStream
-> TxAddInput)
-> Parser ChannelId
-> Parser
(SerialId
-> ByteString -> Word32 -> Word32 -> TlvStream -> TxAddInput)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ChannelId
channel_id Parser
(SerialId
-> ByteString -> Word32 -> Word32 -> TlvStream -> TxAddInput)
-> Parser SerialId
-> Parser
(ByteString -> Word32 -> Word32 -> TlvStream -> TxAddInput)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser SerialId
serial_id Parser (ByteString -> Word32 -> Word32 -> TlvStream -> TxAddInput)
-> Parser ByteString
-> Parser (Word32 -> Word32 -> TlvStream -> TxAddInput)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser ByteString
prefixed Parser (Word32 -> Word32 -> TlvStream -> TxAddInput)
-> Parser Word32 -> Parser (Word32 -> TlvStream -> TxAddInput)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Word32
u32 Parser (Word32 -> TlvStream -> TxAddInput)
-> Parser Word32 -> Parser (TlvStream -> TxAddInput)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Word32
u32
Parser (TlvStream -> TxAddInput)
-> Parser TlvStream -> Parser TxAddInput
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> [Word64] -> Parser TlvStream
tlvs []
encode_tx_add_output :: TxAddOutput -> BS.ByteString
encode_tx_add_output :: TxAddOutput -> ByteString
encode_tx_add_output TxAddOutput
m =
let SerialId Word64
sid = TxAddOutput -> SerialId
tx_add_output_serial_id TxAddOutput
m
in [ByteString] -> ByteString
forall a. Monoid a => [a] -> a
mconcat [
ChannelId -> ByteString
BOLT1.un_channel_id (TxAddOutput -> ChannelId
tx_add_output_channel_id TxAddOutput
m)
, Word64 -> ByteString
BOLT1.encode_u64 Word64
sid
, Satoshi -> ByteString
BOLT1.encode_satoshi (TxAddOutput -> Satoshi
tx_add_output_sats TxAddOutput
m)
, ScriptPubKey -> ByteString
script_bytes (TxAddOutput -> ScriptPubKey
tx_add_output_script TxAddOutput
m)
, TlvStream -> ByteString
BOLT1.encode_tlv_stream (TxAddOutput -> TlvStream
tx_add_output_tlvs TxAddOutput
m)
]
decode_tx_add_output :: BS.ByteString -> Either DecodeError TxAddOutput
decode_tx_add_output :: ByteString -> Either DecodeError TxAddOutput
decode_tx_add_output = Parser TxAddOutput -> ByteString -> Either DecodeError TxAddOutput
forall a. Parser a -> ByteString -> Either DecodeError a
parse (Parser TxAddOutput
-> ByteString -> Either DecodeError TxAddOutput)
-> Parser TxAddOutput
-> ByteString
-> Either DecodeError TxAddOutput
forall a b. (a -> b) -> a -> b
$
ChannelId
-> SerialId -> Satoshi -> ScriptPubKey -> TlvStream -> TxAddOutput
TxAddOutput (ChannelId
-> SerialId -> Satoshi -> ScriptPubKey -> TlvStream -> TxAddOutput)
-> Parser ChannelId
-> Parser
(SerialId -> Satoshi -> ScriptPubKey -> TlvStream -> TxAddOutput)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ChannelId
channel_id Parser
(SerialId -> Satoshi -> ScriptPubKey -> TlvStream -> TxAddOutput)
-> Parser SerialId
-> Parser (Satoshi -> ScriptPubKey -> TlvStream -> TxAddOutput)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser SerialId
serial_id Parser (Satoshi -> ScriptPubKey -> TlvStream -> TxAddOutput)
-> Parser Satoshi
-> Parser (ScriptPubKey -> TlvStream -> TxAddOutput)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Satoshi
satoshi Parser (ScriptPubKey -> TlvStream -> TxAddOutput)
-> Parser ScriptPubKey -> Parser (TlvStream -> TxAddOutput)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser ScriptPubKey
script
Parser (TlvStream -> TxAddOutput)
-> Parser TlvStream -> Parser TxAddOutput
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> [Word64] -> Parser TlvStream
tlvs []
encode_tx_remove_input :: TxRemoveInput -> BS.ByteString
encode_tx_remove_input :: TxRemoveInput -> ByteString
encode_tx_remove_input (TxRemoveInput ChannelId
cid (SerialId Word64
sid) TlvStream
ts) = [ByteString] -> ByteString
forall a. Monoid a => [a] -> a
mconcat [
ChannelId -> ByteString
BOLT1.un_channel_id ChannelId
cid
, Word64 -> ByteString
BOLT1.encode_u64 Word64
sid
, TlvStream -> ByteString
BOLT1.encode_tlv_stream TlvStream
ts
]
decode_tx_remove_input :: BS.ByteString -> Either DecodeError TxRemoveInput
decode_tx_remove_input :: ByteString -> Either DecodeError TxRemoveInput
decode_tx_remove_input = Parser TxRemoveInput
-> ByteString -> Either DecodeError TxRemoveInput
forall a. Parser a -> ByteString -> Either DecodeError a
parse (Parser TxRemoveInput
-> ByteString -> Either DecodeError TxRemoveInput)
-> Parser TxRemoveInput
-> ByteString
-> Either DecodeError TxRemoveInput
forall a b. (a -> b) -> a -> b
$
ChannelId -> SerialId -> TlvStream -> TxRemoveInput
TxRemoveInput (ChannelId -> SerialId -> TlvStream -> TxRemoveInput)
-> Parser ChannelId
-> Parser (SerialId -> TlvStream -> TxRemoveInput)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ChannelId
channel_id Parser (SerialId -> TlvStream -> TxRemoveInput)
-> Parser SerialId -> Parser (TlvStream -> TxRemoveInput)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser SerialId
serial_id Parser (TlvStream -> TxRemoveInput)
-> Parser TlvStream -> Parser TxRemoveInput
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> [Word64] -> Parser TlvStream
tlvs []
encode_tx_remove_output :: TxRemoveOutput -> BS.ByteString
encode_tx_remove_output :: TxRemoveOutput -> ByteString
encode_tx_remove_output (TxRemoveOutput ChannelId
cid (SerialId Word64
sid) TlvStream
ts) = [ByteString] -> ByteString
forall a. Monoid a => [a] -> a
mconcat [
ChannelId -> ByteString
BOLT1.un_channel_id ChannelId
cid
, Word64 -> ByteString
BOLT1.encode_u64 Word64
sid
, TlvStream -> ByteString
BOLT1.encode_tlv_stream TlvStream
ts
]
decode_tx_remove_output
:: BS.ByteString -> Either DecodeError TxRemoveOutput
decode_tx_remove_output :: ByteString -> Either DecodeError TxRemoveOutput
decode_tx_remove_output = Parser TxRemoveOutput
-> ByteString -> Either DecodeError TxRemoveOutput
forall a. Parser a -> ByteString -> Either DecodeError a
parse (Parser TxRemoveOutput
-> ByteString -> Either DecodeError TxRemoveOutput)
-> Parser TxRemoveOutput
-> ByteString
-> Either DecodeError TxRemoveOutput
forall a b. (a -> b) -> a -> b
$
ChannelId -> SerialId -> TlvStream -> TxRemoveOutput
TxRemoveOutput (ChannelId -> SerialId -> TlvStream -> TxRemoveOutput)
-> Parser ChannelId
-> Parser (SerialId -> TlvStream -> TxRemoveOutput)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ChannelId
channel_id Parser (SerialId -> TlvStream -> TxRemoveOutput)
-> Parser SerialId -> Parser (TlvStream -> TxRemoveOutput)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser SerialId
serial_id Parser (TlvStream -> TxRemoveOutput)
-> Parser TlvStream -> Parser TxRemoveOutput
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> [Word64] -> Parser TlvStream
tlvs []
encode_tx_complete :: TxComplete -> BS.ByteString
encode_tx_complete :: TxComplete -> ByteString
encode_tx_complete (TxComplete ChannelId
cid TlvStream
ts) =
ChannelId -> ByteString
BOLT1.un_channel_id ChannelId
cid ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> TlvStream -> ByteString
BOLT1.encode_tlv_stream TlvStream
ts
decode_tx_complete :: BS.ByteString -> Either DecodeError TxComplete
decode_tx_complete :: ByteString -> Either DecodeError TxComplete
decode_tx_complete = Parser TxComplete -> ByteString -> Either DecodeError TxComplete
forall a. Parser a -> ByteString -> Either DecodeError a
parse (Parser TxComplete -> ByteString -> Either DecodeError TxComplete)
-> Parser TxComplete -> ByteString -> Either DecodeError TxComplete
forall a b. (a -> b) -> a -> b
$ ChannelId -> TlvStream -> TxComplete
TxComplete (ChannelId -> TlvStream -> TxComplete)
-> Parser ChannelId -> Parser (TlvStream -> TxComplete)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ChannelId
channel_id Parser (TlvStream -> TxComplete)
-> Parser TlvStream -> Parser TxComplete
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> [Word64] -> Parser TlvStream
tlvs []
encode_tx_signatures :: TxSignatures -> Either EncodeError BS.ByteString
encode_tx_signatures :: TxSignatures -> Either EncodeError ByteString
encode_tx_signatures TxSignatures
m = do
let ws :: [Witness]
ws = TxSignatures -> [Witness]
tx_signatures_witnesses TxSignatures
m
n <- [Witness] -> Either EncodeError ByteString
forall a. [a] -> Either EncodeError ByteString
count_prefix [Witness]
ws
pure $ mconcat [
BOLT1.un_channel_id (tx_signatures_channel_id m)
, txid_bytes (tx_signatures_txid m)
, n
, mconcat [ BOLT1.encode_u16 (fromIntegral (BS.length w)) <> w
| w <- map un_witness ws ]
, BOLT1.encode_tlv_stream (tx_signatures_tlvs m)
]
decode_tx_signatures :: BS.ByteString -> Either DecodeError TxSignatures
decode_tx_signatures :: ByteString -> Either DecodeError TxSignatures
decode_tx_signatures = Parser TxSignatures
-> ByteString -> Either DecodeError TxSignatures
forall a. Parser a -> ByteString -> Either DecodeError a
parse (Parser TxSignatures
-> ByteString -> Either DecodeError TxSignatures)
-> Parser TxSignatures
-> ByteString
-> Either DecodeError TxSignatures
forall a b. (a -> b) -> a -> b
$ do
cid <- Parser ChannelId
channel_id
t <- txid
n <- u16
ws <- count (fromIntegral n) wit
TxSignatures cid t ws <$> tlvs []
where
wit :: Parser Witness
wit = do
b <- Parser ByteString
prefixed
maybe (failure DecodeInsufficientBytes) pure (witness b)
rbf_known :: [Word64]
rbf_known :: [Word64]
rbf_known = [Word64
0, Word64
2]
encode_tx_init_rbf :: TxInitRbf -> Either EncodeError BS.ByteString
encode_tx_init_rbf :: TxInitRbf -> Either EncodeError ByteString
encode_tx_init_rbf TxInitRbf
m = do
ts <- [Word64]
-> [TlvRecord] -> TlvStream -> Either EncodeError ByteString
encode_tlvs [Word64]
rbf_known
( Word64 -> (Int64 -> ByteString) -> Maybe Int64 -> [TlvRecord]
forall a. Word64 -> (a -> ByteString) -> Maybe a -> [TlvRecord]
opt Word64
0 Int64 -> ByteString
BOLT1.encode_s64 (TxInitRbf -> Maybe Int64
tx_init_rbf_funding_output_contribution TxInitRbf
m)
[TlvRecord] -> [TlvRecord] -> [TlvRecord]
forall a. Semigroup a => a -> a -> a
<> Word64 -> Bool -> [TlvRecord]
opt_flag Word64
2 (TxInitRbf -> Bool
tx_init_rbf_require_confirmed_inputs TxInitRbf
m) )
(TxInitRbf -> TlvStream
tx_init_rbf_tlvs TxInitRbf
m)
pure $ mconcat [
BOLT1.un_channel_id (tx_init_rbf_channel_id m)
, BOLT1.encode_u32 (tx_init_rbf_locktime m)
, BOLT1.encode_u32 (tx_init_rbf_feerate m)
, ts
]
decode_tx_init_rbf :: BS.ByteString -> Either DecodeError TxInitRbf
decode_tx_init_rbf :: ByteString -> Either DecodeError TxInitRbf
decode_tx_init_rbf = Parser TxInitRbf -> ByteString -> Either DecodeError TxInitRbf
forall a. Parser a -> ByteString -> Either DecodeError a
parse (Parser TxInitRbf -> ByteString -> Either DecodeError TxInitRbf)
-> Parser TxInitRbf -> ByteString -> Either DecodeError TxInitRbf
forall a b. (a -> b) -> a -> b
$ do
cid <- Parser ChannelId
channel_id
lock <- u32
fee <- u32
s <- tlvs rbf_known
contrib <- record 0 tlv_s64 s
conf <- flag 2 s
pure (TxInitRbf cid lock fee contrib conf (unknown rbf_known s))
encode_tx_ack_rbf :: TxAckRbf -> Either EncodeError BS.ByteString
encode_tx_ack_rbf :: TxAckRbf -> Either EncodeError ByteString
encode_tx_ack_rbf TxAckRbf
m = do
ts <- [Word64]
-> [TlvRecord] -> TlvStream -> Either EncodeError ByteString
encode_tlvs [Word64]
rbf_known
( Word64 -> (Int64 -> ByteString) -> Maybe Int64 -> [TlvRecord]
forall a. Word64 -> (a -> ByteString) -> Maybe a -> [TlvRecord]
opt Word64
0 Int64 -> ByteString
BOLT1.encode_s64 (TxAckRbf -> Maybe Int64
tx_ack_rbf_funding_output_contribution TxAckRbf
m)
[TlvRecord] -> [TlvRecord] -> [TlvRecord]
forall a. Semigroup a => a -> a -> a
<> Word64 -> Bool -> [TlvRecord]
opt_flag Word64
2 (TxAckRbf -> Bool
tx_ack_rbf_require_confirmed_inputs TxAckRbf
m) )
(TxAckRbf -> TlvStream
tx_ack_rbf_tlvs TxAckRbf
m)
pure (BOLT1.un_channel_id (tx_ack_rbf_channel_id m) <> ts)
decode_tx_ack_rbf :: BS.ByteString -> Either DecodeError TxAckRbf
decode_tx_ack_rbf :: ByteString -> Either DecodeError TxAckRbf
decode_tx_ack_rbf = Parser TxAckRbf -> ByteString -> Either DecodeError TxAckRbf
forall a. Parser a -> ByteString -> Either DecodeError a
parse (Parser TxAckRbf -> ByteString -> Either DecodeError TxAckRbf)
-> Parser TxAckRbf -> ByteString -> Either DecodeError TxAckRbf
forall a b. (a -> b) -> a -> b
$ do
cid <- Parser ChannelId
channel_id
s <- tlvs rbf_known
contrib <- record 0 tlv_s64 s
conf <- flag 2 s
pure (TxAckRbf cid contrib conf (unknown rbf_known s))
encode_tx_abort :: TxAbort -> Either EncodeError BS.ByteString
encode_tx_abort :: TxAbort -> Either EncodeError ByteString
encode_tx_abort (TxAbort ChannelId
cid ByteString
dat TlvStream
ts) = do
dat' <- ByteString -> Either EncodeError ByteString
u16_prefixed ByteString
dat
pure (BOLT1.un_channel_id cid <> dat' <> BOLT1.encode_tlv_stream ts)
decode_tx_abort :: BS.ByteString -> Either DecodeError TxAbort
decode_tx_abort :: ByteString -> Either DecodeError TxAbort
decode_tx_abort = Parser TxAbort -> ByteString -> Either DecodeError TxAbort
forall a. Parser a -> ByteString -> Either DecodeError a
parse (Parser TxAbort -> ByteString -> Either DecodeError TxAbort)
-> Parser TxAbort -> ByteString -> Either DecodeError TxAbort
forall a b. (a -> b) -> a -> b
$ ChannelId -> ByteString -> TlvStream -> TxAbort
TxAbort (ChannelId -> ByteString -> TlvStream -> TxAbort)
-> Parser ChannelId -> Parser (ByteString -> TlvStream -> TxAbort)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ChannelId
channel_id Parser (ByteString -> TlvStream -> TxAbort)
-> Parser ByteString -> Parser (TlvStream -> TxAbort)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser ByteString
prefixed Parser (TlvStream -> TxAbort) -> Parser TlvStream -> Parser TxAbort
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> [Word64] -> Parser TlvStream
tlvs []
encode_stfu :: Stfu -> BS.ByteString
encode_stfu :: Stfu -> ByteString
encode_stfu (Stfu ChannelId
cid Bool
ini TlvStream
ts) = [ByteString] -> ByteString
forall a. Monoid a => [a] -> a
mconcat [
ChannelId -> ByteString
BOLT1.un_channel_id ChannelId
cid
, Word8 -> ByteString
word8 (if Bool
ini then Word8
1 else Word8
0)
, TlvStream -> ByteString
BOLT1.encode_tlv_stream TlvStream
ts
]
decode_stfu :: BS.ByteString -> Either DecodeError Stfu
decode_stfu :: ByteString -> Either DecodeError Stfu
decode_stfu = Parser Stfu -> ByteString -> Either DecodeError Stfu
forall a. Parser a -> ByteString -> Either DecodeError a
parse (Parser Stfu -> ByteString -> Either DecodeError Stfu)
-> Parser Stfu -> ByteString -> Either DecodeError Stfu
forall a b. (a -> b) -> a -> b
$ do
cid <- Parser ChannelId
channel_id
b <- u8
ini <- case b of
Word8
0 -> Bool -> Parser Bool
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
False
Word8
1 -> Bool -> Parser Bool
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
True
Word8
_ -> DecodeError -> Parser Bool
forall a. DecodeError -> Parser a
failure DecodeError
DecodeInvalidInitiator
Stfu cid ini <$> tlvs []
encode_shutdown :: Shutdown -> BS.ByteString
encode_shutdown :: Shutdown -> ByteString
encode_shutdown (Shutdown ChannelId
cid ScriptPubKey
spk TlvStream
ts) = [ByteString] -> ByteString
forall a. Monoid a => [a] -> a
mconcat [
ChannelId -> ByteString
BOLT1.un_channel_id ChannelId
cid
, ScriptPubKey -> ByteString
script_bytes ScriptPubKey
spk
, TlvStream -> ByteString
BOLT1.encode_tlv_stream TlvStream
ts
]
decode_shutdown :: BS.ByteString -> Either DecodeError Shutdown
decode_shutdown :: ByteString -> Either DecodeError Shutdown
decode_shutdown = Parser Shutdown -> ByteString -> Either DecodeError Shutdown
forall a. Parser a -> ByteString -> Either DecodeError a
parse (Parser Shutdown -> ByteString -> Either DecodeError Shutdown)
-> Parser Shutdown -> ByteString -> Either DecodeError Shutdown
forall a b. (a -> b) -> a -> b
$ ChannelId -> ScriptPubKey -> TlvStream -> Shutdown
Shutdown (ChannelId -> ScriptPubKey -> TlvStream -> Shutdown)
-> Parser ChannelId
-> Parser (ScriptPubKey -> TlvStream -> Shutdown)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ChannelId
channel_id Parser (ScriptPubKey -> TlvStream -> Shutdown)
-> Parser ScriptPubKey -> Parser (TlvStream -> Shutdown)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser ScriptPubKey
script Parser (TlvStream -> Shutdown)
-> Parser TlvStream -> Parser Shutdown
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> [Word64] -> Parser TlvStream
tlvs []
closing_known :: [Word64]
closing_known :: [Word64]
closing_known = [Word64
1, Word64
2, Word64
3]
closing_records
:: Maybe Signature -> Maybe Signature -> Maybe Signature -> [TlvRecord]
closing_records :: Maybe Signature
-> Maybe Signature -> Maybe Signature -> [TlvRecord]
closing_records Maybe Signature
a Maybe Signature
b Maybe Signature
c =
Word64
-> (Signature -> ByteString) -> Maybe Signature -> [TlvRecord]
forall a. Word64 -> (a -> ByteString) -> Maybe a -> [TlvRecord]
opt Word64
1 Signature -> ByteString
BOLT1.un_signature Maybe Signature
a
[TlvRecord] -> [TlvRecord] -> [TlvRecord]
forall a. Semigroup a => a -> a -> a
<> Word64
-> (Signature -> ByteString) -> Maybe Signature -> [TlvRecord]
forall a. Word64 -> (a -> ByteString) -> Maybe a -> [TlvRecord]
opt Word64
2 Signature -> ByteString
BOLT1.un_signature Maybe Signature
b
[TlvRecord] -> [TlvRecord] -> [TlvRecord]
forall a. Semigroup a => a -> a -> a
<> Word64
-> (Signature -> ByteString) -> Maybe Signature -> [TlvRecord]
forall a. Word64 -> (a -> ByteString) -> Maybe a -> [TlvRecord]
opt Word64
3 Signature -> ByteString
BOLT1.un_signature Maybe Signature
c
encode_closing_complete :: ClosingComplete -> Either EncodeError BS.ByteString
encode_closing_complete :: ClosingComplete -> Either EncodeError ByteString
encode_closing_complete ClosingComplete
m = do
ts <- [Word64]
-> [TlvRecord] -> TlvStream -> Either EncodeError ByteString
encode_tlvs [Word64]
closing_known
(Maybe Signature
-> Maybe Signature -> Maybe Signature -> [TlvRecord]
closing_records
(ClosingComplete -> Maybe Signature
closing_complete_closer_output_only ClosingComplete
m)
(ClosingComplete -> Maybe Signature
closing_complete_closee_output_only ClosingComplete
m)
(ClosingComplete -> Maybe Signature
closing_complete_closer_and_closee_outputs ClosingComplete
m))
(ClosingComplete -> TlvStream
closing_complete_tlvs ClosingComplete
m)
pure $ mconcat [
BOLT1.un_channel_id (closing_complete_channel_id m)
, script_bytes (closing_complete_closer_scriptpubkey m)
, script_bytes (closing_complete_closee_scriptpubkey m)
, BOLT1.encode_satoshi (closing_complete_fee_satoshis m)
, BOLT1.encode_u32 (closing_complete_locktime m)
, ts
]
decode_closing_complete
:: BS.ByteString -> Either DecodeError ClosingComplete
decode_closing_complete :: ByteString -> Either DecodeError ClosingComplete
decode_closing_complete = Parser ClosingComplete
-> ByteString -> Either DecodeError ClosingComplete
forall a. Parser a -> ByteString -> Either DecodeError a
parse (Parser ClosingComplete
-> ByteString -> Either DecodeError ClosingComplete)
-> Parser ClosingComplete
-> ByteString
-> Either DecodeError ClosingComplete
forall a b. (a -> b) -> a -> b
$ do
cid <- Parser ChannelId
channel_id
closer <- script
closee <- script
fee <- satoshi
lock <- u32
s <- tlvs closing_known
a <- record 1 BOLT1.signature s
b <- record 2 BOLT1.signature s
c <- record 3 BOLT1.signature s
pure (ClosingComplete cid closer closee fee lock a b c
(unknown closing_known s))
encode_closing_sig :: ClosingSig -> Either EncodeError BS.ByteString
encode_closing_sig :: ClosingSig -> Either EncodeError ByteString
encode_closing_sig ClosingSig
m = do
ts <- [Word64]
-> [TlvRecord] -> TlvStream -> Either EncodeError ByteString
encode_tlvs [Word64]
closing_known
(Maybe Signature
-> Maybe Signature -> Maybe Signature -> [TlvRecord]
closing_records
(ClosingSig -> Maybe Signature
closing_sig_closer_output_only ClosingSig
m)
(ClosingSig -> Maybe Signature
closing_sig_closee_output_only ClosingSig
m)
(ClosingSig -> Maybe Signature
closing_sig_closer_and_closee_outputs ClosingSig
m))
(ClosingSig -> TlvStream
closing_sig_tlvs ClosingSig
m)
pure $ mconcat [
BOLT1.un_channel_id (closing_sig_channel_id m)
, script_bytes (closing_sig_closer_scriptpubkey m)
, script_bytes (closing_sig_closee_scriptpubkey m)
, BOLT1.encode_satoshi (closing_sig_fee_satoshis m)
, BOLT1.encode_u32 (closing_sig_locktime m)
, ts
]
decode_closing_sig :: BS.ByteString -> Either DecodeError ClosingSig
decode_closing_sig :: ByteString -> Either DecodeError ClosingSig
decode_closing_sig = Parser ClosingSig -> ByteString -> Either DecodeError ClosingSig
forall a. Parser a -> ByteString -> Either DecodeError a
parse (Parser ClosingSig -> ByteString -> Either DecodeError ClosingSig)
-> Parser ClosingSig -> ByteString -> Either DecodeError ClosingSig
forall a b. (a -> b) -> a -> b
$ do
cid <- Parser ChannelId
channel_id
closer <- script
closee <- script
fee <- satoshi
lock <- u32
s <- tlvs closing_known
a <- record 1 BOLT1.signature s
b <- record 2 BOLT1.signature s
c <- record 3 BOLT1.signature s
pure (ClosingSig cid closer closee fee lock a b c
(unknown closing_known s))
closing_signed_known :: [Word64]
closing_signed_known :: [Word64]
closing_signed_known = [Word64
1]
encode_closing_signed :: ClosingSigned -> Either EncodeError BS.ByteString
encode_closing_signed :: ClosingSigned -> Either EncodeError ByteString
encode_closing_signed ClosingSigned
m = do
ts <- [Word64]
-> [TlvRecord] -> TlvStream -> Either EncodeError ByteString
encode_tlvs [Word64]
closing_signed_known
(Word64 -> (FeeRange -> ByteString) -> Maybe FeeRange -> [TlvRecord]
forall a. Word64 -> (a -> ByteString) -> Maybe a -> [TlvRecord]
opt Word64
1 FeeRange -> ByteString
fee_range_bytes (ClosingSigned -> Maybe FeeRange
closing_signed_fee_range ClosingSigned
m))
(ClosingSigned -> TlvStream
closing_signed_tlvs ClosingSigned
m)
pure $ mconcat [
BOLT1.un_channel_id (closing_signed_channel_id m)
, BOLT1.encode_satoshi (closing_signed_fee_satoshis m)
, BOLT1.un_signature (closing_signed_signature m)
, ts
]
decode_closing_signed :: BS.ByteString -> Either DecodeError ClosingSigned
decode_closing_signed :: ByteString -> Either DecodeError ClosingSigned
decode_closing_signed = Parser ClosingSigned
-> ByteString -> Either DecodeError ClosingSigned
forall a. Parser a -> ByteString -> Either DecodeError a
parse (Parser ClosingSigned
-> ByteString -> Either DecodeError ClosingSigned)
-> Parser ClosingSigned
-> ByteString
-> Either DecodeError ClosingSigned
forall a b. (a -> b) -> a -> b
$ do
cid <- Parser ChannelId
channel_id
fee <- satoshi
sig <- signature
s <- tlvs closing_signed_known
range <- record 1 tlv_fee_range s
pure (ClosingSigned cid fee sig range (unknown closing_signed_known s))
update_add_htlc_known :: [Word64]
update_add_htlc_known :: [Word64]
update_add_htlc_known = [Word64
0]
encode_update_add_htlc :: UpdateAddHtlc -> Either EncodeError BS.ByteString
encode_update_add_htlc :: UpdateAddHtlc -> Either EncodeError ByteString
encode_update_add_htlc UpdateAddHtlc
m = do
ts <- [Word64]
-> [TlvRecord] -> TlvStream -> Either EncodeError ByteString
encode_tlvs [Word64]
update_add_htlc_known
(Word64 -> (Point -> ByteString) -> Maybe Point -> [TlvRecord]
forall a. Word64 -> (a -> ByteString) -> Maybe a -> [TlvRecord]
opt Word64
0 Point -> ByteString
BOLT1.un_point (UpdateAddHtlc -> Maybe Point
update_add_htlc_path_key UpdateAddHtlc
m))
(UpdateAddHtlc -> TlvStream
update_add_htlc_tlvs UpdateAddHtlc
m)
let HtlcId hid = update_add_htlc_id m
pure $ mconcat [
BOLT1.un_channel_id (update_add_htlc_channel_id m)
, BOLT1.encode_u64 hid
, BOLT1.encode_milli_satoshi (update_add_htlc_amount_msat m)
, BOLT1.un_payment_hash (update_add_htlc_payment_hash m)
, BOLT1.encode_u32 (update_add_htlc_cltv_expiry m)
, un_onion_routing_packet (update_add_htlc_onion_routing_packet m)
, ts
]
decode_update_add_htlc :: BS.ByteString -> Either DecodeError UpdateAddHtlc
decode_update_add_htlc :: ByteString -> Either DecodeError UpdateAddHtlc
decode_update_add_htlc = Parser UpdateAddHtlc
-> ByteString -> Either DecodeError UpdateAddHtlc
forall a. Parser a -> ByteString -> Either DecodeError a
parse (Parser UpdateAddHtlc
-> ByteString -> Either DecodeError UpdateAddHtlc)
-> Parser UpdateAddHtlc
-> ByteString
-> Either DecodeError UpdateAddHtlc
forall a b. (a -> b) -> a -> b
$ do
cid <- Parser ChannelId
channel_id
hid <- htlc_id
amt <- milli_satoshi
ph <- payment_hash
cltv <- u32
onion <- sized 1366 onion_routing_packet
s <- tlvs update_add_htlc_known
pk <- record 0 tlv_point s
pure (UpdateAddHtlc cid hid amt ph cltv onion pk
(unknown update_add_htlc_known s))
update_fulfill_htlc_known :: [Word64]
update_fulfill_htlc_known :: [Word64]
update_fulfill_htlc_known = [Word64
1, Word64
3]
encode_update_fulfill_htlc
:: UpdateFulfillHtlc -> Either EncodeError BS.ByteString
encode_update_fulfill_htlc :: UpdateFulfillHtlc -> Either EncodeError ByteString
encode_update_fulfill_htlc UpdateFulfillHtlc
m = do
ts <- [Word64]
-> [TlvRecord] -> TlvStream -> Either EncodeError ByteString
encode_tlvs [Word64]
update_fulfill_htlc_known
( Word64
-> (AttributionData -> ByteString)
-> Maybe AttributionData
-> [TlvRecord]
forall a. Word64 -> (a -> ByteString) -> Maybe a -> [TlvRecord]
opt Word64
1 AttributionData -> ByteString
un_attribution_data (UpdateFulfillHtlc -> Maybe AttributionData
update_fulfill_htlc_attribution_data UpdateFulfillHtlc
m)
[TlvRecord] -> [TlvRecord] -> [TlvRecord]
forall a. Semigroup a => a -> a -> a
<> Word64
-> (ByteString -> ByteString) -> Maybe ByteString -> [TlvRecord]
forall a. Word64 -> (a -> ByteString) -> Maybe a -> [TlvRecord]
opt Word64
3 ByteString -> ByteString
forall a. a -> a
id (UpdateFulfillHtlc -> Maybe ByteString
update_fulfill_htlc_fulfillment_payload UpdateFulfillHtlc
m) )
(UpdateFulfillHtlc -> TlvStream
update_fulfill_htlc_tlvs UpdateFulfillHtlc
m)
let HtlcId hid = update_fulfill_htlc_id m
pure $ mconcat [
BOLT1.un_channel_id (update_fulfill_htlc_channel_id m)
, BOLT1.encode_u64 hid
, BOLT1.un_payment_preimage (update_fulfill_htlc_payment_preimage m)
, ts
]
decode_update_fulfill_htlc
:: BS.ByteString -> Either DecodeError UpdateFulfillHtlc
decode_update_fulfill_htlc :: ByteString -> Either DecodeError UpdateFulfillHtlc
decode_update_fulfill_htlc = Parser UpdateFulfillHtlc
-> ByteString -> Either DecodeError UpdateFulfillHtlc
forall a. Parser a -> ByteString -> Either DecodeError a
parse (Parser UpdateFulfillHtlc
-> ByteString -> Either DecodeError UpdateFulfillHtlc)
-> Parser UpdateFulfillHtlc
-> ByteString
-> Either DecodeError UpdateFulfillHtlc
forall a b. (a -> b) -> a -> b
$ do
cid <- Parser ChannelId
channel_id
hid <- htlc_id
pre <- payment_preimage
s <- tlvs update_fulfill_htlc_known
att <- record 1 attribution_data s
pay <- record 3 Just s
pure (UpdateFulfillHtlc cid hid pre att pay
(unknown update_fulfill_htlc_known s))
update_fail_htlc_known :: [Word64]
update_fail_htlc_known :: [Word64]
update_fail_htlc_known = [Word64
1]
encode_update_fail_htlc :: UpdateFailHtlc -> Either EncodeError BS.ByteString
encode_update_fail_htlc :: UpdateFailHtlc -> Either EncodeError ByteString
encode_update_fail_htlc UpdateFailHtlc
m = do
reason <- ByteString -> Either EncodeError ByteString
u16_prefixed (UpdateFailHtlc -> ByteString
update_fail_htlc_reason UpdateFailHtlc
m)
ts <- encode_tlvs update_fail_htlc_known
(opt 1 un_attribution_data (update_fail_htlc_attribution_data m))
(update_fail_htlc_tlvs m)
let HtlcId hid = update_fail_htlc_id m
pure $ mconcat [
BOLT1.un_channel_id (update_fail_htlc_channel_id m)
, BOLT1.encode_u64 hid
, reason
, ts
]
decode_update_fail_htlc :: BS.ByteString -> Either DecodeError UpdateFailHtlc
decode_update_fail_htlc :: ByteString -> Either DecodeError UpdateFailHtlc
decode_update_fail_htlc = Parser UpdateFailHtlc
-> ByteString -> Either DecodeError UpdateFailHtlc
forall a. Parser a -> ByteString -> Either DecodeError a
parse (Parser UpdateFailHtlc
-> ByteString -> Either DecodeError UpdateFailHtlc)
-> Parser UpdateFailHtlc
-> ByteString
-> Either DecodeError UpdateFailHtlc
forall a b. (a -> b) -> a -> b
$ do
cid <- Parser ChannelId
channel_id
hid <- htlc_id
reason <- prefixed
s <- tlvs update_fail_htlc_known
att <- record 1 attribution_data s
pure (UpdateFailHtlc cid hid reason att (unknown update_fail_htlc_known s))
encode_update_fail_malformed_htlc :: UpdateFailMalformedHtlc -> BS.ByteString
encode_update_fail_malformed_htlc :: UpdateFailMalformedHtlc -> ByteString
encode_update_fail_malformed_htlc
(UpdateFailMalformedHtlc ChannelId
cid (HtlcId Word64
hid) OnionHash
oh Word16
code TlvStream
ts) = [ByteString] -> ByteString
forall a. Monoid a => [a] -> a
mconcat [
ChannelId -> ByteString
BOLT1.un_channel_id ChannelId
cid
, Word64 -> ByteString
BOLT1.encode_u64 Word64
hid
, OnionHash -> ByteString
un_onion_hash OnionHash
oh
, Word16 -> ByteString
BOLT1.encode_u16 Word16
code
, TlvStream -> ByteString
BOLT1.encode_tlv_stream TlvStream
ts
]
decode_update_fail_malformed_htlc
:: BS.ByteString -> Either DecodeError UpdateFailMalformedHtlc
decode_update_fail_malformed_htlc :: ByteString -> Either DecodeError UpdateFailMalformedHtlc
decode_update_fail_malformed_htlc = Parser UpdateFailMalformedHtlc
-> ByteString -> Either DecodeError UpdateFailMalformedHtlc
forall a. Parser a -> ByteString -> Either DecodeError a
parse (Parser UpdateFailMalformedHtlc
-> ByteString -> Either DecodeError UpdateFailMalformedHtlc)
-> Parser UpdateFailMalformedHtlc
-> ByteString
-> Either DecodeError UpdateFailMalformedHtlc
forall a b. (a -> b) -> a -> b
$
ChannelId
-> HtlcId
-> OnionHash
-> Word16
-> TlvStream
-> UpdateFailMalformedHtlc
UpdateFailMalformedHtlc (ChannelId
-> HtlcId
-> OnionHash
-> Word16
-> TlvStream
-> UpdateFailMalformedHtlc)
-> Parser ChannelId
-> Parser
(HtlcId
-> OnionHash -> Word16 -> TlvStream -> UpdateFailMalformedHtlc)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ChannelId
channel_id Parser
(HtlcId
-> OnionHash -> Word16 -> TlvStream -> UpdateFailMalformedHtlc)
-> Parser HtlcId
-> Parser
(OnionHash -> Word16 -> TlvStream -> UpdateFailMalformedHtlc)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser HtlcId
htlc_id Parser
(OnionHash -> Word16 -> TlvStream -> UpdateFailMalformedHtlc)
-> Parser OnionHash
-> Parser (Word16 -> TlvStream -> UpdateFailMalformedHtlc)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Int -> (ByteString -> Maybe OnionHash) -> Parser OnionHash
forall a. Int -> (ByteString -> Maybe a) -> Parser a
sized Int
32 ByteString -> Maybe OnionHash
onion_hash
Parser (Word16 -> TlvStream -> UpdateFailMalformedHtlc)
-> Parser Word16 -> Parser (TlvStream -> UpdateFailMalformedHtlc)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Word16
u16 Parser (TlvStream -> UpdateFailMalformedHtlc)
-> Parser TlvStream -> Parser UpdateFailMalformedHtlc
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> [Word64] -> Parser TlvStream
tlvs []
commitment_signed_known :: [Word64]
commitment_signed_known :: [Word64]
commitment_signed_known = [Word64
1]
encode_commitment_signed
:: CommitmentSigned -> Either EncodeError BS.ByteString
encode_commitment_signed :: CommitmentSigned -> Either EncodeError ByteString
encode_commitment_signed CommitmentSigned
m = do
let sigs :: [Signature]
sigs = CommitmentSigned -> [Signature]
commitment_signed_htlc_signatures CommitmentSigned
m
n <- [Signature] -> Either EncodeError ByteString
forall a. [a] -> Either EncodeError ByteString
count_prefix [Signature]
sigs
ts <- encode_tlvs commitment_signed_known
(opt 1 txid_bytes (commitment_signed_funding_txid m))
(commitment_signed_tlvs m)
pure $ mconcat [
BOLT1.un_channel_id (commitment_signed_channel_id m)
, BOLT1.un_signature (commitment_signed_signature m)
, n
, mconcat (map BOLT1.un_signature sigs)
, ts
]
decode_commitment_signed
:: BS.ByteString -> Either DecodeError CommitmentSigned
decode_commitment_signed :: ByteString -> Either DecodeError CommitmentSigned
decode_commitment_signed = Parser CommitmentSigned
-> ByteString -> Either DecodeError CommitmentSigned
forall a. Parser a -> ByteString -> Either DecodeError a
parse (Parser CommitmentSigned
-> ByteString -> Either DecodeError CommitmentSigned)
-> Parser CommitmentSigned
-> ByteString
-> Either DecodeError CommitmentSigned
forall a b. (a -> b) -> a -> b
$ do
cid <- Parser ChannelId
channel_id
sig <- signature
n <- u16
sigs <- count (fromIntegral n) signature
s <- tlvs commitment_signed_known
fund <- record 1 mk_txid s
pure (CommitmentSigned cid sig sigs fund (unknown commitment_signed_known s))
encode_revoke_and_ack :: RevokeAndAck -> BS.ByteString
encode_revoke_and_ack :: RevokeAndAck -> ByteString
encode_revoke_and_ack (RevokeAndAck ChannelId
cid PerCommitmentSecret
sec Point
pcp TlvStream
ts) = [ByteString] -> ByteString
forall a. Monoid a => [a] -> a
mconcat [
ChannelId -> ByteString
BOLT1.un_channel_id ChannelId
cid
, PerCommitmentSecret -> ByteString
BOLT1.un_per_commitment_secret PerCommitmentSecret
sec
, Point -> ByteString
BOLT1.un_point Point
pcp
, TlvStream -> ByteString
BOLT1.encode_tlv_stream TlvStream
ts
]
decode_revoke_and_ack :: BS.ByteString -> Either DecodeError RevokeAndAck
decode_revoke_and_ack :: ByteString -> Either DecodeError RevokeAndAck
decode_revoke_and_ack = Parser RevokeAndAck
-> ByteString -> Either DecodeError RevokeAndAck
forall a. Parser a -> ByteString -> Either DecodeError a
parse (Parser RevokeAndAck
-> ByteString -> Either DecodeError RevokeAndAck)
-> Parser RevokeAndAck
-> ByteString
-> Either DecodeError RevokeAndAck
forall a b. (a -> b) -> a -> b
$
ChannelId
-> PerCommitmentSecret -> Point -> TlvStream -> RevokeAndAck
RevokeAndAck (ChannelId
-> PerCommitmentSecret -> Point -> TlvStream -> RevokeAndAck)
-> Parser ChannelId
-> Parser
(PerCommitmentSecret -> Point -> TlvStream -> RevokeAndAck)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ChannelId
channel_id Parser (PerCommitmentSecret -> Point -> TlvStream -> RevokeAndAck)
-> Parser PerCommitmentSecret
-> Parser (Point -> TlvStream -> RevokeAndAck)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser PerCommitmentSecret
per_commitment_secret Parser (Point -> TlvStream -> RevokeAndAck)
-> Parser Point -> Parser (TlvStream -> RevokeAndAck)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Point
point Parser (TlvStream -> RevokeAndAck)
-> Parser TlvStream -> Parser RevokeAndAck
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> [Word64] -> Parser TlvStream
tlvs []
encode_update_fee :: UpdateFee -> BS.ByteString
encode_update_fee :: UpdateFee -> ByteString
encode_update_fee (UpdateFee ChannelId
cid Word32
fee TlvStream
ts) = [ByteString] -> ByteString
forall a. Monoid a => [a] -> a
mconcat [
ChannelId -> ByteString
BOLT1.un_channel_id ChannelId
cid
, Word32 -> ByteString
BOLT1.encode_u32 Word32
fee
, TlvStream -> ByteString
BOLT1.encode_tlv_stream TlvStream
ts
]
decode_update_fee :: BS.ByteString -> Either DecodeError UpdateFee
decode_update_fee :: ByteString -> Either DecodeError UpdateFee
decode_update_fee = Parser UpdateFee -> ByteString -> Either DecodeError UpdateFee
forall a. Parser a -> ByteString -> Either DecodeError a
parse (Parser UpdateFee -> ByteString -> Either DecodeError UpdateFee)
-> Parser UpdateFee -> ByteString -> Either DecodeError UpdateFee
forall a b. (a -> b) -> a -> b
$ ChannelId -> Word32 -> TlvStream -> UpdateFee
UpdateFee (ChannelId -> Word32 -> TlvStream -> UpdateFee)
-> Parser ChannelId -> Parser (Word32 -> TlvStream -> UpdateFee)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser ChannelId
channel_id Parser (Word32 -> TlvStream -> UpdateFee)
-> Parser Word32 -> Parser (TlvStream -> UpdateFee)
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Parser Word32
u32 Parser (TlvStream -> UpdateFee)
-> Parser TlvStream -> Parser UpdateFee
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> [Word64] -> Parser TlvStream
tlvs []
channel_reestablish_known :: [Word64]
channel_reestablish_known :: [Word64]
channel_reestablish_known = [Word64
1]
encode_channel_reestablish
:: ChannelReestablish -> Either EncodeError BS.ByteString
encode_channel_reestablish :: ChannelReestablish -> Either EncodeError ByteString
encode_channel_reestablish ChannelReestablish
m = do
ts <- [Word64]
-> [TlvRecord] -> TlvStream -> Either EncodeError ByteString
encode_tlvs [Word64]
channel_reestablish_known
(Word64
-> (NextFunding -> ByteString) -> Maybe NextFunding -> [TlvRecord]
forall a. Word64 -> (a -> ByteString) -> Maybe a -> [TlvRecord]
opt Word64
1 NextFunding -> ByteString
next_funding_bytes (ChannelReestablish -> Maybe NextFunding
channel_reestablish_next_funding ChannelReestablish
m))
(ChannelReestablish -> TlvStream
channel_reestablish_tlvs ChannelReestablish
m)
pure $ mconcat [
BOLT1.un_channel_id (channel_reestablish_channel_id m)
, BOLT1.encode_u64 (channel_reestablish_next_commitment_number m)
, BOLT1.encode_u64 (channel_reestablish_next_revocation_number m)
, BOLT1.un_per_commitment_secret
(channel_reestablish_your_last_per_commitment_secret m)
, BOLT1.un_point (channel_reestablish_my_current_per_commitment_point m)
, ts
]
decode_channel_reestablish
:: BS.ByteString -> Either DecodeError ChannelReestablish
decode_channel_reestablish :: ByteString -> Either DecodeError ChannelReestablish
decode_channel_reestablish = Parser ChannelReestablish
-> ByteString -> Either DecodeError ChannelReestablish
forall a. Parser a -> ByteString -> Either DecodeError a
parse (Parser ChannelReestablish
-> ByteString -> Either DecodeError ChannelReestablish)
-> Parser ChannelReestablish
-> ByteString
-> Either DecodeError ChannelReestablish
forall a b. (a -> b) -> a -> b
$ do
cid <- Parser ChannelId
channel_id
nc <- u64
nr <- u64
sec <- per_commitment_secret
pcp <- point
s <- tlvs channel_reestablish_known
next <- record 1 tlv_next_funding s
pure (ChannelReestablish cid nc nr sec pcp next
(unknown channel_reestablish_known s))