module Cometa.Transaction.Utxo (Utxo, utxo, input, output, encodeCbor, decodeCbor) where
import qualified Codec.CBOR.Encoding as E
import Cometa.Error (Error)
import qualified Cometa.Internal.TransactionCbor as Cbor
import Cometa.Transaction.Input (Input)
import qualified Cometa.Transaction.Input as Input
import Cometa.Transaction.Output (Output)
import qualified Cometa.Transaction.Output as Output
import Data.ByteString (ByteString)
data Utxo = Utxo !Input !Output deriving (Utxo -> Utxo -> Bool
(Utxo -> Utxo -> Bool) -> (Utxo -> Utxo -> Bool) -> Eq Utxo
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Utxo -> Utxo -> Bool
== :: Utxo -> Utxo -> Bool
$c/= :: Utxo -> Utxo -> Bool
/= :: Utxo -> Utxo -> Bool
Eq, Int -> Utxo -> ShowS
[Utxo] -> ShowS
Utxo -> String
(Int -> Utxo -> ShowS)
-> (Utxo -> String) -> ([Utxo] -> ShowS) -> Show Utxo
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Utxo -> ShowS
showsPrec :: Int -> Utxo -> ShowS
$cshow :: Utxo -> String
show :: Utxo -> String
$cshowList :: [Utxo] -> ShowS
showList :: [Utxo] -> ShowS
Show)
utxo :: Input -> Output -> Utxo
utxo :: Input -> Output -> Utxo
utxo = Input -> Output -> Utxo
Utxo
input :: Utxo -> Input
input :: Utxo -> Input
input (Utxo Input
result Output
_) = Input
result
output :: Utxo -> Output
output :: Utxo -> Output
output (Utxo Input
_ Output
result) = Output
result
encodeCbor :: Utxo -> ByteString
encodeCbor :: Utxo -> ByteString
encodeCbor (Utxo Input
reference Output
resolved) = Encoding -> ByteString
Cbor.encode (Word -> Encoding
E.encodeListLen Word
2 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
Cbor.raw (Input -> ByteString
Input.encodeCbor Input
reference) Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
Cbor.raw (Output -> ByteString
Output.encodeCbor Output
resolved))
decodeCbor :: ByteString -> Either Error Utxo
decodeCbor :: ByteString -> Either Error Utxo
decodeCbor ByteString
bytes = (forall s. Decoder s Utxo) -> ByteString -> Either Error Utxo
forall a. (forall s. Decoder s a) -> ByteString -> Either Error a
Cbor.decode (Int -> Decoder s Utxo -> Decoder s Utxo
forall s a. Int -> Decoder s a -> Decoder s a
Cbor.array Int
2 (Input -> Output -> Utxo
Utxo (Input -> Output -> Utxo)
-> Decoder s Input -> Decoder s (Output -> Utxo)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> (ByteString -> Either Error Input) -> Decoder s Input
forall a s.
ByteString -> (ByteString -> Either Error a) -> Decoder s a
Cbor.nested ByteString
bytes ByteString -> Either Error Input
Input.decodeCbor Decoder s (Output -> Utxo) -> Decoder s Output -> Decoder s Utxo
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
<*> (EncodedOutput -> Output
Output.toOutput (EncodedOutput -> Output)
-> Decoder s EncodedOutput -> Decoder s Output
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString
-> (ByteString -> Either Error EncodedOutput)
-> Decoder s EncodedOutput
forall a s.
ByteString -> (ByteString -> Either Error a) -> Decoder s a
Cbor.nested ByteString
bytes ByteString -> Either Error EncodedOutput
Output.decodeCbor))) ByteString
bytes