{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
module Cometa.Internal.TransactionCbor (decode, decodeAs, encode, raw, nested, array, list, entries, setList, optional, slice, mapFields) where
import qualified Codec.CBOR.Decoding as D
import qualified Codec.CBOR.Encoding as E
import qualified Codec.CBOR.Read as R
import qualified Codec.CBOR.Term as T
import qualified Codec.CBOR.Write as W
import Cometa.Error (Error (..))
import qualified Cometa.Internal.ProposalCbor as P
import Control.Monad (unless)
import Data.ByteString (ByteString)
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as L
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.Text (Text)
import qualified Data.Text as Text
import Data.Word (Word64)
encode :: E.Encoding -> ByteString
encode :: Encoding -> ByteString
encode = Encoding -> ByteString
W.toStrictByteString
decode :: (forall s. D.Decoder s a) -> ByteString -> Either Error a
decode :: forall a. (forall s. Decoder s a) -> ByteString -> Either Error a
decode = (Text -> Error)
-> (forall s. Decoder s a) -> ByteString -> Either Error a
forall a.
(Text -> Error)
-> (forall s. Decoder s a) -> ByteString -> Either Error a
decodeAs Text -> Error
InvalidTransactionCbor
decodeAs :: (Text -> Error) -> (forall s. D.Decoder s a) -> ByteString -> Either Error a
decodeAs :: forall a.
(Text -> Error)
-> (forall s. Decoder s a) -> ByteString -> Either Error a
decodeAs Text -> Error
failure forall s. Decoder s a
parser ByteString
bytes = case (forall s. Decoder s a)
-> ByteString -> Either DeserialiseFailure (ByteString, a)
forall a.
(forall s. Decoder s a)
-> ByteString -> Either DeserialiseFailure (ByteString, a)
R.deserialiseFromBytes Decoder s a
forall s. Decoder s a
parser (ByteString -> ByteString
L.fromStrict ByteString
bytes) of
Left DeserialiseFailure
problem -> Error -> Either Error a
forall a b. a -> Either a b
Left (Text -> Error
failure (String -> Text
Text.pack (DeserialiseFailure -> String
forall a. Show a => a -> String
show DeserialiseFailure
problem)))
Right (ByteString
rest, a
value)
| ByteString -> Bool
L.null ByteString
rest -> a -> Either Error a
forall a b. b -> Either a b
Right a
value
| Bool
otherwise -> Error -> Either Error a
forall a b. a -> Either a b
Left (Text -> Error
failure Text
"Trailing transaction CBOR")
raw :: ByteString -> E.Encoding
raw :: ByteString -> Encoding
raw = ByteString -> Encoding
E.encodePreEncoded
slice :: ByteString -> D.Decoder s ByteString
slice :: forall s. ByteString -> Decoder s ByteString
slice ByteString
bytes = do
(Term
_, ByteOffset
start, ByteOffset
end) <- Decoder s Term -> Decoder s (Term, ByteOffset, ByteOffset)
forall s a. Decoder s a -> Decoder s (a, ByteOffset, ByteOffset)
D.decodeWithByteSpan Decoder s Term
forall s. Decoder s Term
T.decodeTerm
ByteString -> Decoder s ByteString
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Int -> ByteString -> ByteString
B.take (ByteOffset -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteOffset
end ByteOffset -> ByteOffset -> ByteOffset
forall a. Num a => a -> a -> a
- ByteOffset
start)) (Int -> ByteString -> ByteString
B.drop (ByteOffset -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral ByteOffset
start) ByteString
bytes))
nested :: ByteString -> (ByteString -> Either Error a) -> D.Decoder s a
nested :: forall a s.
ByteString -> (ByteString -> Either Error a) -> Decoder s a
nested ByteString
bytes ByteString -> Either Error a
parser = ByteString -> Decoder s ByteString
forall s. ByteString -> Decoder s ByteString
slice ByteString
bytes Decoder s ByteString -> (ByteString -> Decoder s a) -> Decoder s a
forall a b. Decoder s a -> (a -> Decoder s b) -> Decoder s b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Error -> Decoder s a)
-> (a -> Decoder s a) -> Either Error a -> Decoder s a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> Decoder s a
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Decoder s a)
-> (Error -> String) -> Error -> Decoder s a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Error -> String
forall a. Show a => a -> String
show) a -> Decoder s a
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Error a -> Decoder s a)
-> (ByteString -> Either Error a) -> ByteString -> Decoder s a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either Error a
parser
array :: Int -> D.Decoder s a -> D.Decoder s a
array :: forall s a. Int -> Decoder s a -> Decoder s a
array = Int -> Decoder s a -> Decoder s a
forall s a. Int -> Decoder s a -> Decoder s a
P.array
list :: D.Decoder s a -> D.Decoder s [a]
list :: forall s a. Decoder s a -> Decoder s [a]
list = Decoder s a -> Decoder s [a]
forall s a. Decoder s a -> Decoder s [a]
P.list
entries :: D.Decoder s a -> D.Decoder s [a]
entries :: forall s a. Decoder s a -> Decoder s [a]
entries = Decoder s a -> Decoder s [a]
forall s a. Decoder s a -> Decoder s [a]
P.entries
setList :: D.Decoder s a -> D.Decoder s [a]
setList :: forall s a. Decoder s a -> Decoder s [a]
setList = Decoder s a -> Decoder s [a]
forall s a. Decoder s a -> Decoder s [a]
P.setList
optional :: D.Decoder s a -> D.Decoder s (Maybe a)
optional :: forall s a. Decoder s a -> Decoder s (Maybe a)
optional = Decoder s a -> Decoder s (Maybe a)
forall s a. Decoder s a -> Decoder s (Maybe a)
P.optional
mapFields :: ByteString -> D.Decoder s (Map Word64 ByteString)
mapFields :: forall s. ByteString -> Decoder s (Map Word64 ByteString)
mapFields ByteString
bytes = do
[(Word64, ByteString)]
pairs <- Decoder s (Word64, ByteString) -> Decoder s [(Word64, ByteString)]
forall s a. Decoder s a -> Decoder s [a]
entries ((,) (Word64 -> ByteString -> (Word64, ByteString))
-> Decoder s Word64
-> Decoder s (ByteString -> (Word64, ByteString))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s Word64
forall s. Decoder s Word64
D.decodeWord64 Decoder s (ByteString -> (Word64, ByteString))
-> Decoder s ByteString -> Decoder s (Word64, ByteString)
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ByteString -> Decoder s ByteString
forall s. ByteString -> Decoder s ByteString
slice ByteString
bytes)
let result :: Map Word64 ByteString
result = [(Word64, ByteString)] -> Map Word64 ByteString
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [(Word64, ByteString)]
pairs
Bool -> Decoder s () -> Decoder s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Map Word64 ByteString -> Int
forall k a. Map k a -> Int
Map.size Map Word64 ByteString
result Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== [(Word64, ByteString)] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(Word64, ByteString)]
pairs) (String -> Decoder s ()
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Duplicate transaction map key")
Map Word64 ByteString -> Decoder s (Map Word64 ByteString)
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Map Word64 ByteString
result