{-# LANGUAGE OverloadedStrings #-}
module Cometa.Encoding.Reencode
( nativeScript,
nativeScripts,
transaction,
subTransaction,
)
where
import qualified Cometa.Encoding.Cbor as Cbor
import Cometa.Error (Error)
import qualified Cometa.Internal.Reencode as Internal
import qualified Cometa.Script.Native as Native
import qualified Cometa.SubTransaction as Sub
import qualified Cometa.Transaction as Tx
nativeScript :: Native.NativeScript -> IO (Either Error Native.NativeScript)
nativeScript :: NativeScript -> IO (Either Error NativeScript)
nativeScript NativeScript
source = (Either Error ByteString -> Either Error NativeScript)
-> IO (Either Error ByteString) -> IO (Either Error NativeScript)
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Either Error ByteString
-> (ByteString -> Either Error NativeScript)
-> Either Error NativeScript
forall a b.
Either Error a -> (a -> Either Error b) -> Either Error b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> Either Error NativeScript
Native.decodeCbor) (Text -> CUInt -> ByteString -> IO (Either Error ByteString)
Internal.reencode Text
"encoding.reencode.nativeScript" CUInt
0 (NativeScript -> ByteString
Native.encodeCbor NativeScript
source))
nativeScripts :: Cbor.Cbor -> IO (Either Error Cbor.Cbor)
nativeScripts :: Cbor -> IO (Either Error Cbor)
nativeScripts Cbor
source = do
Either Error ByteString
result <- Text -> CUInt -> ByteString -> IO (Either Error ByteString)
Internal.reencode Text
"encoding.reencode.nativeScripts" CUInt
1 (Cbor -> ByteString
Cbor.toBytes Cbor
source)
(Error -> IO (Either Error Cbor))
-> (ByteString -> IO (Either Error Cbor))
-> Either Error ByteString
-> IO (Either Error Cbor)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Either Error Cbor -> IO (Either Error Cbor)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Error Cbor -> IO (Either Error Cbor))
-> (Error -> Either Error Cbor) -> Error -> IO (Either Error Cbor)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Error -> Either Error Cbor
forall a b. a -> Either a b
Left) ByteString -> IO (Either Error Cbor)
Cbor.fromBytes Either Error ByteString
result
transaction :: Tx.Transaction -> IO (Either Error Tx.Transaction)
transaction :: Transaction -> IO (Either Error Transaction)
transaction Transaction
source = (Either Error ByteString -> Either Error Transaction)
-> IO (Either Error ByteString) -> IO (Either Error Transaction)
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Either Error ByteString
-> (ByteString -> Either Error Transaction)
-> Either Error Transaction
forall a b.
Either Error a -> (a -> Either Error b) -> Either Error b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> Either Error Transaction
Tx.decodeCbor) (Text -> CUInt -> ByteString -> IO (Either Error ByteString)
Internal.reencode Text
"encoding.reencode.transaction" CUInt
2 (Transaction -> ByteString
Tx.encodeCbor Transaction
source))
subTransaction :: Sub.SubTransaction -> IO (Either Error Sub.SubTransaction)
subTransaction :: SubTransaction -> IO (Either Error SubTransaction)
subTransaction SubTransaction
source = (Either Error ByteString -> Either Error SubTransaction)
-> IO (Either Error ByteString) -> IO (Either Error SubTransaction)
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Either Error ByteString
-> (ByteString -> Either Error SubTransaction)
-> Either Error SubTransaction
forall a b.
Either Error a -> (a -> Either Error b) -> Either Error b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> Either Error SubTransaction
Sub.decodeCbor) (Text -> CUInt -> ByteString -> IO (Either Error ByteString)
Internal.reencode Text
"encoding.reencode.subTransaction" CUInt
3 (SubTransaction -> ByteString
Sub.encodeCbor SubTransaction
source))