{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
module Cometa.Transaction.Output
( Output,
mkOutput,
address,
value,
datum,
referenceScript,
encodeCbor,
EncodedOutput,
decodeCbor,
fromOutput,
toOutput,
toCbor,
)
where
import qualified Codec.CBOR.Decoding as D
import qualified Codec.CBOR.Encoding as E
import Cometa.Address (Address)
import qualified Cometa.Address as Address
import Cometa.AssetId (AssetId (Ada))
import Cometa.Error (Error)
import qualified Cometa.Internal.TransactionCbor as Cbor
import qualified Cometa.Internal.TransactionValue as V
import qualified Cometa.PlutusData as Data
import Cometa.Script (Script)
import qualified Cometa.Script as Script
import Cometa.Transaction.Datum (Datum (..))
import qualified Cometa.Transaction.Datum as Datum
import Cometa.Value (Value)
import qualified Cometa.Value as Value
import Control.Monad (unless)
import Data.ByteString (ByteString)
import Data.Int (Int64)
import qualified Data.Map.Strict as Map
import Data.Maybe (fromMaybe)
import Data.Word (Word64)
data Output = Output !Address !Value !Datum !(Maybe Script) deriving (Output -> Output -> Bool
(Output -> Output -> Bool)
-> (Output -> Output -> Bool) -> Eq Output
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Output -> Output -> Bool
== :: Output -> Output -> Bool
$c/= :: Output -> Output -> Bool
/= :: Output -> Output -> Bool
Eq, Int -> Output -> ShowS
[Output] -> ShowS
Output -> String
(Int -> Output -> ShowS)
-> (Output -> String) -> ([Output] -> ShowS) -> Show Output
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Output -> ShowS
showsPrec :: Int -> Output -> ShowS
$cshow :: Output -> String
show :: Output -> String
$cshowList :: [Output] -> ShowS
showList :: [Output] -> ShowS
Show)
data EncodedOutput = EncodedOutput !ByteString !Output deriving (Int -> EncodedOutput -> ShowS
[EncodedOutput] -> ShowS
EncodedOutput -> String
(Int -> EncodedOutput -> ShowS)
-> (EncodedOutput -> String)
-> ([EncodedOutput] -> ShowS)
-> Show EncodedOutput
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> EncodedOutput -> ShowS
showsPrec :: Int -> EncodedOutput -> ShowS
$cshow :: EncodedOutput -> String
show :: EncodedOutput -> String
$cshowList :: [EncodedOutput] -> ShowS
showList :: [EncodedOutput] -> ShowS
Show)
instance Eq EncodedOutput where
EncodedOutput
left == :: EncodedOutput -> EncodedOutput -> Bool
== EncodedOutput
right = EncodedOutput -> ByteString
toCbor EncodedOutput
left ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== EncodedOutput -> ByteString
toCbor EncodedOutput
right
mkOutput :: Address -> Value -> Datum -> Maybe Script -> Either Error Output
mkOutput :: Address -> Value -> Datum -> Maybe Script -> Either Error Output
mkOutput Address
destination Value
amount Datum
dat Maybe Script
script = do
Value -> Either Error ()
V.validateOutput Value
amount
Output -> Either Error Output
forall a. a -> Either Error a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Address -> Value -> Datum -> Maybe Script -> Output
Output Address
destination Value
amount Datum
dat Maybe Script
script)
address :: Output -> Address
address :: Output -> Address
address (Output Address
result Value
_ Datum
_ Maybe Script
_) = Address
result
value :: Output -> Value
value :: Output -> Value
value (Output Address
_ Value
result Datum
_ Maybe Script
_) = Value
result
datum :: Output -> Datum
datum :: Output -> Datum
datum (Output Address
_ Value
_ Datum
result Maybe Script
_) = Datum
result
referenceScript :: Output -> Maybe Script
referenceScript :: Output -> Maybe Script
referenceScript (Output Address
_ Value
_ Datum
_ Maybe Script
result) = Maybe Script
result
encodeCbor :: Output -> ByteString
encodeCbor :: Output -> ByteString
encodeCbor (Output Address
destination Value
amount Datum
dat Maybe Script
script) = Encoding -> ByteString
Cbor.encode (Word -> Encoding
E.encodeMapLen (Int -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral ([(Word, Encoding)] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(Word, Encoding)]
fields)) Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ((Word, Encoding) -> Encoding) -> [(Word, Encoding)] -> Encoding
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (\(Word
key, Encoding
payload) -> Word -> Encoding
E.encodeWord Word
key Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Encoding
payload) [(Word, Encoding)]
fields)
where
assets :: [(AssetId, Integer)]
assets = Value -> [(AssetId, Integer)]
Value.toList (Value -> Value -> Value
Value.difference Value
amount (Integer -> Value
Value.lovelace (AssetId -> Value -> Integer
Value.quantity AssetId
Ada Value
amount)))
coin :: Encoding
coin = Integer -> Encoding
E.encodeInteger (AssetId -> Value -> Integer
Value.quantity AssetId
Ada Value
amount)
amountEncoding :: Encoding
amountEncoding = if [(AssetId, Integer)] -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [(AssetId, Integer)]
assets then Encoding
coin else Word -> Encoding
E.encodeListLen Word
2 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Encoding
coin Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Value -> Encoding
V.encodeAssets Value
amount
fields :: [(Word, Encoding)]
fields = [(Word
0, ByteString -> Encoding
E.encodeBytes (Address -> ByteString
Address.toBytes Address
destination)), (Word
1, Encoding
amountEncoding)] [(Word, Encoding)] -> [(Word, Encoding)] -> [(Word, Encoding)]
forall a. [a] -> [a] -> [a]
++ [(Word, Encoding)]
datumField [(Word, Encoding)] -> [(Word, Encoding)] -> [(Word, Encoding)]
forall a. [a] -> [a] -> [a]
++ [(Word, Encoding)]
-> (Script -> [(Word, Encoding)])
-> Maybe Script
-> [(Word, Encoding)]
forall b a. b -> (a -> b) -> Maybe a -> b
maybe [] (\Script
s -> [(Word
3, Word -> Encoding
E.encodeTag Word
24 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
E.encodeBytes (Script -> ByteString
Script.encodeCbor Script
s))]) Maybe Script
script
datumField :: [(Word, Encoding)]
datumField = case Datum
dat of
Datum
NoDatum -> []
DatumHash Hash32
hash -> [(Word
2, Word -> Encoding
E.encodeListLen Word
2 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
E.encodeWord Word
0 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
E.encodeBytes (Hash32 -> ByteString
Datum.hashBytes Hash32
hash))]
InlineDatum EncodedPlutusData
payload -> [(Word
2, Word -> Encoding
E.encodeListLen Word
2 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
E.encodeWord Word
1 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
E.encodeTag Word
24 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
E.encodeBytes (EncodedPlutusData -> ByteString
Data.toCbor EncodedPlutusData
payload))]
fromOutput :: Output -> EncodedOutput
fromOutput :: Output -> EncodedOutput
fromOutput Output
output = ByteString -> Output -> EncodedOutput
EncodedOutput (Output -> ByteString
encodeCbor Output
output) Output
output
toOutput :: EncodedOutput -> Output
toOutput :: EncodedOutput -> Output
toOutput (EncodedOutput ByteString
_ Output
output) = Output
output
toCbor :: EncodedOutput -> ByteString
toCbor :: EncodedOutput -> ByteString
toCbor (EncodedOutput ByteString
bytes Output
_) = ByteString
bytes
decodeCbor :: ByteString -> Either Error EncodedOutput
decodeCbor :: ByteString -> Either Error EncodedOutput
decodeCbor ByteString
bytes = ByteString -> Output -> EncodedOutput
EncodedOutput ByteString
bytes (Output -> EncodedOutput)
-> Either Error Output -> Either Error EncodedOutput
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (forall s. Decoder s Output) -> ByteString -> Either Error Output
forall a. (forall s. Decoder s a) -> ByteString -> Either Error a
Cbor.decode Decoder s Output
forall s. Decoder s Output
parser ByteString
bytes
where
parser :: Decoder s Output
parser = do
TokenType
token <- Decoder s TokenType
forall s. Decoder s TokenType
D.peekTokenType
if TokenType
token TokenType -> TokenType -> Bool
forall a. Eq a => a -> a -> Bool
== TokenType
D.TypeMapLen Bool -> Bool -> Bool
|| TokenType
token TokenType -> TokenType -> Bool
forall a. Eq a => a -> a -> Bool
== TokenType
D.TypeMapLenIndef then Decoder s Output
forall s. Decoder s Output
mapOutput else Decoder s Output
forall s. Decoder s Output
legacyOutput
validate :: Address -> Value -> Datum -> Maybe Script -> m Output
validate Address
destination Value
amount Datum
dat Maybe Script
script = (Error -> m Output)
-> (Output -> m Output) -> Either Error Output -> m Output
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> m Output
forall a. String -> m a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> m Output) -> (Error -> String) -> Error -> m Output
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Error -> String
forall a. Show a => a -> String
show) Output -> m Output
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Address -> Value -> Datum -> Maybe Script -> Either Error Output
mkOutput Address
destination Value
amount Datum
dat Maybe Script
script)
addressParser :: Decoder s Address
addressParser = Decoder s ByteString
forall s. Decoder s ByteString
D.decodeBytes Decoder s ByteString
-> (ByteString -> Decoder s Address) -> Decoder s Address
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 Address)
-> (Address -> Decoder s Address)
-> Either Error Address
-> Decoder s Address
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> Decoder s Address
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Decoder s Address)
-> (Error -> String) -> Error -> Decoder s Address
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Error -> String
forall a. Show a => a -> String
show) Address -> Decoder s Address
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Error Address -> Decoder s Address)
-> (ByteString -> Either Error Address)
-> ByteString
-> Decoder s Address
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either Error Address
Address.fromBytes
valueParser :: Decoder s Value
valueParser = do
TokenType
token <- Decoder s TokenType
forall s. Decoder s TokenType
D.peekTokenType
(Integer
coin, Value
assets) <-
if TokenType
token TokenType -> TokenType -> Bool
forall a. Eq a => a -> a -> Bool
== TokenType
D.TypeUInt Bool -> Bool -> Bool
|| TokenType
token TokenType -> TokenType -> Bool
forall a. Eq a => a -> a -> Bool
== TokenType
D.TypeUInt64
then (,) (Integer -> Value -> (Integer, Value))
-> Decoder s Integer -> Decoder s (Value -> (Integer, Value))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s Integer
forall s. Decoder s Integer
D.decodeInteger Decoder s (Value -> (Integer, Value))
-> Decoder s Value -> Decoder s (Integer, Value)
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
<*> Value -> Decoder s Value
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Value
forall a. Monoid a => a
mempty
else Int -> Decoder s (Integer, Value) -> Decoder s (Integer, Value)
forall s a. Int -> Decoder s a -> Decoder s a
Cbor.array Int
2 ((,) (Integer -> Value -> (Integer, Value))
-> Decoder s Integer -> Decoder s (Value -> (Integer, Value))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s Integer
forall s. Decoder s Integer
D.decodeInteger Decoder s (Value -> (Integer, Value))
-> Decoder s Value -> Decoder s (Integer, Value)
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
<*> Decoder s Value
forall s. Decoder s Value
V.decodeAssets)
Bool -> Decoder s () -> Decoder s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Integer
coin Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
>= Integer
0 Bool -> Bool -> Bool
&& Integer
coin Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
<= Int64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Int64
forall a. Bounded a => a
maxBound :: Int64)) (String -> Decoder s ()
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Output coin exceeds nonnegative int64")
Value -> Decoder s Value
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Integer -> Value
Value.lovelace Integer
coin Value -> Value -> Value
forall a. Semigroup a => a -> a -> a
<> Value
assets)
decodeField :: (forall s. D.Decoder s a) -> Word64 -> Map.Map Word64 ByteString -> D.Decoder t a
decodeField :: forall a t.
(forall s. Decoder s a)
-> Word64 -> Map Word64 ByteString -> Decoder t a
decodeField forall s. Decoder s a
parser' Word64
key Map Word64 ByteString
fields = case Word64 -> Map Word64 ByteString -> Maybe ByteString
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Word64
key Map Word64 ByteString
fields of
Maybe ByteString
Nothing -> String -> Decoder t a
forall a. String -> Decoder t a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Missing required output field"
Just ByteString
payload -> (Error -> Decoder t a)
-> (a -> Decoder t a) -> Either Error a -> Decoder t a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> Decoder t a
forall a. String -> Decoder t a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Decoder t a)
-> (Error -> String) -> Error -> Decoder t a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Error -> String
forall a. Show a => a -> String
show) a -> Decoder t a
forall a. a -> Decoder t a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((forall s. Decoder s a) -> ByteString -> Either Error a
forall a. (forall s. Decoder s a) -> ByteString -> Either Error a
Cbor.decode Decoder s a
forall s. Decoder s a
parser' ByteString
payload)
optionalField :: (forall s. D.Decoder s a) -> Word64 -> Map.Map Word64 ByteString -> D.Decoder t (Maybe a)
optionalField :: forall a t.
(forall s. Decoder s a)
-> Word64 -> Map Word64 ByteString -> Decoder t (Maybe a)
optionalField forall s. Decoder s a
parser' Word64
key Map Word64 ByteString
fields = (ByteString -> Decoder t a)
-> Maybe ByteString -> Decoder t (Maybe a)
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> Maybe a -> f (Maybe b)
traverse ((Error -> Decoder t a)
-> (a -> Decoder t a) -> Either Error a -> Decoder t a
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> Decoder t a
forall a. String -> Decoder t a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Decoder t a)
-> (Error -> String) -> Error -> Decoder t a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Error -> String
forall a. Show a => a -> String
show) a -> Decoder t a
forall a. a -> Decoder t a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Error a -> Decoder t a)
-> (ByteString -> Either Error a) -> ByteString -> Decoder t a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall s. Decoder s a) -> ByteString -> Either Error a
forall a. (forall s. Decoder s a) -> ByteString -> Either Error a
Cbor.decode Decoder s a
forall s. Decoder s a
parser') (Word64 -> Map Word64 ByteString -> Maybe ByteString
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Word64
key Map Word64 ByteString
fields)
mapOutput :: Decoder s Output
mapOutput = do
Map Word64 ByteString
fields <- ByteString -> Decoder s (Map Word64 ByteString)
forall s. ByteString -> Decoder s (Map Word64 ByteString)
Cbor.mapFields ByteString
bytes
Bool -> Decoder s () -> Decoder s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ((Word64 -> Bool) -> [Word64] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (Word64 -> [Word64] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Word64
0, Word64
1, Word64
2, Word64
3]) (Map Word64 ByteString -> [Word64]
forall k a. Map k a -> [k]
Map.keys Map Word64 ByteString
fields)) (String -> Decoder s ()
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Unknown output field")
Address
destination <- (forall s. Decoder s Address)
-> Word64 -> Map Word64 ByteString -> Decoder s Address
forall a t.
(forall s. Decoder s a)
-> Word64 -> Map Word64 ByteString -> Decoder t a
decodeField Decoder s Address
forall s. Decoder s Address
addressParser Word64
0 Map Word64 ByteString
fields
Value
amount <- (forall s. Decoder s Value)
-> Word64 -> Map Word64 ByteString -> Decoder s Value
forall a t.
(forall s. Decoder s a)
-> Word64 -> Map Word64 ByteString -> Decoder t a
decodeField Decoder s Value
forall s. Decoder s Value
valueParser Word64
1 Map Word64 ByteString
fields
Datum
dat <- Datum -> Maybe Datum -> Datum
forall a. a -> Maybe a -> a
fromMaybe Datum
NoDatum (Maybe Datum -> Datum)
-> Decoder s (Maybe Datum) -> Decoder s Datum
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (forall s. Decoder s Datum)
-> Word64 -> Map Word64 ByteString -> Decoder s (Maybe Datum)
forall a t.
(forall s. Decoder s a)
-> Word64 -> Map Word64 ByteString -> Decoder t (Maybe a)
optionalField Decoder s Datum
forall s. Decoder s Datum
datumParser Word64
2 Map Word64 ByteString
fields
Maybe Script
script <- (forall s. Decoder s Script)
-> Word64 -> Map Word64 ByteString -> Decoder s (Maybe Script)
forall a t.
(forall s. Decoder s a)
-> Word64 -> Map Word64 ByteString -> Decoder t (Maybe a)
optionalField Decoder s Script
forall s. Decoder s Script
scriptParser Word64
3 Map Word64 ByteString
fields
Address -> Value -> Datum -> Maybe Script -> Decoder s Output
forall {m :: * -> *}.
MonadFail m =>
Address -> Value -> Datum -> Maybe Script -> m Output
validate Address
destination Value
amount Datum
dat Maybe Script
script
datumParser :: Decoder s Datum
datumParser = Int -> Decoder s Datum -> Decoder s Datum
forall s a. Int -> Decoder s a -> Decoder s a
Cbor.array Int
2 (Decoder s Datum -> Decoder s Datum)
-> Decoder s Datum -> Decoder s Datum
forall a b. (a -> b) -> a -> b
$ do
Word
kind <- Decoder s Word
forall s. Decoder s Word
D.decodeWord
case Word
kind of
Word
0 -> Hash32 -> Datum
DatumHash (Hash32 -> Datum) -> Decoder s Hash32 -> Decoder s Datum
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Decoder s ByteString
forall s. Decoder s ByteString
D.decodeBytes Decoder s ByteString
-> (ByteString -> Decoder s Hash32) -> Decoder s Hash32
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 Hash32)
-> (Hash32 -> Decoder s Hash32)
-> Either Error Hash32
-> Decoder s Hash32
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> Decoder s Hash32
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Decoder s Hash32)
-> (Error -> String) -> Error -> Decoder s Hash32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Error -> String
forall a. Show a => a -> String
show) Hash32 -> Decoder s Hash32
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Error Hash32 -> Decoder s Hash32)
-> (ByteString -> Either Error Hash32)
-> ByteString
-> Decoder s Hash32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either Error Hash32
Datum.hashFromBytes)
Word
1 -> EncodedPlutusData -> Datum
InlineDatum (EncodedPlutusData -> Datum)
-> Decoder s EncodedPlutusData -> Decoder s Datum
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Decoder s ByteString
forall s. Decoder s ByteString
embedded Decoder s ByteString
-> (ByteString -> Decoder s EncodedPlutusData)
-> Decoder s EncodedPlutusData
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 EncodedPlutusData)
-> (EncodedPlutusData -> Decoder s EncodedPlutusData)
-> Either Error EncodedPlutusData
-> Decoder s EncodedPlutusData
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> Decoder s EncodedPlutusData
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Decoder s EncodedPlutusData)
-> (Error -> String) -> Error -> Decoder s EncodedPlutusData
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Error -> String
forall a. Show a => a -> String
show) EncodedPlutusData -> Decoder s EncodedPlutusData
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Error EncodedPlutusData -> Decoder s EncodedPlutusData)
-> (ByteString -> Either Error EncodedPlutusData)
-> ByteString
-> Decoder s EncodedPlutusData
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either Error EncodedPlutusData
Data.decodeCbor)
Word
_ -> String -> Decoder s Datum
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Invalid datum alternative"
scriptParser :: Decoder s Script
scriptParser = Decoder s ByteString
forall s. Decoder s ByteString
embedded Decoder s ByteString
-> (ByteString -> Decoder s Script) -> Decoder s Script
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 Script)
-> (Script -> Decoder s Script)
-> Either Error Script
-> Decoder s Script
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> Decoder s Script
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Decoder s Script)
-> (Error -> String) -> Error -> Decoder s Script
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Error -> String
forall a. Show a => a -> String
show) Script -> Decoder s Script
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Error Script -> Decoder s Script)
-> (ByteString -> Either Error Script)
-> ByteString
-> Decoder s Script
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either Error Script
Script.decodeCbor
embedded :: Decoder s ByteString
embedded = do
Word
tag <- Decoder s Word
forall s. Decoder s Word
D.decodeTag
Bool -> Decoder s () -> Decoder s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Word
tag Word -> Word -> Bool
forall a. Eq a => a -> a -> Bool
== Word
24) (String -> Decoder s ()
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Expected embedded CBOR tag24")
Decoder s ByteString
forall s. Decoder s ByteString
D.decodeBytes
legacyOutput :: Decoder s Output
legacyOutput = do
Maybe Int
count <- Decoder s (Maybe Int)
forall s. Decoder s (Maybe Int)
D.decodeListLenOrIndef
Bool -> Decoder s () -> Decoder s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Bool -> (Int -> Bool) -> Maybe Int -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
True (Int -> [Int] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Int
2, Int
3]) Maybe Int
count) (String -> Decoder s ()
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Invalid legacy output arity")
Address
destination <- Decoder s Address
forall s. Decoder s Address
addressParser
Value
amount <- Decoder s Value
forall s. Decoder s Value
valueParser
Bool
hasDatum <- case Maybe Int
count of
Just Int
3 -> Bool -> Decoder s Bool
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
True
Just Int
_ -> Bool -> Decoder s Bool
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Bool
False
Maybe Int
Nothing -> Bool -> Bool
not (Bool -> Bool) -> Decoder s Bool -> Decoder s Bool
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s Bool
forall s. Decoder s Bool
D.decodeBreakOr
Datum
dat <- if Bool
hasDatum then Hash32 -> Datum
DatumHash (Hash32 -> Datum) -> Decoder s Hash32 -> Decoder s Datum
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Decoder s ByteString
forall s. Decoder s ByteString
D.decodeBytes Decoder s ByteString
-> (ByteString -> Decoder s Hash32) -> Decoder s Hash32
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 Hash32)
-> (Hash32 -> Decoder s Hash32)
-> Either Error Hash32
-> Decoder s Hash32
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> Decoder s Hash32
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Decoder s Hash32)
-> (Error -> String) -> Error -> Decoder s Hash32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Error -> String
forall a. Show a => a -> String
show) Hash32 -> Decoder s Hash32
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Error Hash32 -> Decoder s Hash32)
-> (ByteString -> Either Error Hash32)
-> ByteString
-> Decoder s Hash32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either Error Hash32
Datum.hashFromBytes) else Datum -> Decoder s Datum
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Datum
NoDatum
case Maybe Int
count of
Maybe Int
Nothing | Bool
hasDatum -> Decoder s Bool
forall s. Decoder s Bool
D.decodeBreakOr Decoder s Bool -> (Bool -> Decoder s ()) -> Decoder s ()
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
>>= \Bool
ended -> Bool -> Decoder s () -> Decoder s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
ended (String -> Decoder s ()
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Extra legacy output field")
Maybe Int
_ -> () -> Decoder s ()
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
Address -> Value -> Datum -> Maybe Script -> Decoder s Output
forall {m :: * -> *}.
MonadFail m =>
Address -> Value -> Datum -> Maybe Script -> m Output
validate Address
destination Value
amount Datum
dat Maybe Script
forall a. Maybe a
Nothing