{-# LANGUAGE OverloadedStrings #-}
module Cometa.Script
( Script (..),
encodeCbor,
decodeCbor,
hash,
)
where
import qualified Codec.CBOR.Decoding as Decode
import qualified Codec.CBOR.Encoding as Encode
import qualified Codec.CBOR.Read as Read
import Codec.CBOR.Term (decodeTerm)
import qualified Codec.CBOR.Write as Write
import Cometa.Credential (ScriptHash)
import Cometa.Error (Error (..))
import qualified Cometa.Script.Native as Native
import qualified Cometa.Script.Plutus as Plutus
import Control.Monad (unless)
import Data.ByteString (ByteString)
import qualified Data.ByteString as ByteString
import qualified Data.ByteString.Lazy as Lazy
import qualified Data.Text as Text
data Script
=
Native !Native.NativeScript
|
Plutus !Plutus.PlutusScript
deriving (Script -> Script -> Bool
(Script -> Script -> Bool)
-> (Script -> Script -> Bool) -> Eq Script
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Script -> Script -> Bool
== :: Script -> Script -> Bool
$c/= :: Script -> Script -> Bool
/= :: Script -> Script -> Bool
Eq, Eq Script
Eq Script =>
(Script -> Script -> Ordering)
-> (Script -> Script -> Bool)
-> (Script -> Script -> Bool)
-> (Script -> Script -> Bool)
-> (Script -> Script -> Bool)
-> (Script -> Script -> Script)
-> (Script -> Script -> Script)
-> Ord Script
Script -> Script -> Bool
Script -> Script -> Ordering
Script -> Script -> Script
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: Script -> Script -> Ordering
compare :: Script -> Script -> Ordering
$c< :: Script -> Script -> Bool
< :: Script -> Script -> Bool
$c<= :: Script -> Script -> Bool
<= :: Script -> Script -> Bool
$c> :: Script -> Script -> Bool
> :: Script -> Script -> Bool
$c>= :: Script -> Script -> Bool
>= :: Script -> Script -> Bool
$cmax :: Script -> Script -> Script
max :: Script -> Script -> Script
$cmin :: Script -> Script -> Script
min :: Script -> Script -> Script
Ord, Int -> Script -> ShowS
[Script] -> ShowS
Script -> String
(Int -> Script -> ShowS)
-> (Script -> String) -> ([Script] -> ShowS) -> Show Script
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Script -> ShowS
showsPrec :: Int -> Script -> ShowS
$cshow :: Script -> String
show :: Script -> String
$cshowList :: [Script] -> ShowS
showList :: [Script] -> ShowS
Show)
encodeCbor :: Script -> ByteString
encodeCbor :: Script -> ByteString
encodeCbor Script
script = Encoding -> ByteString
Write.toStrictByteString (Word -> Encoding
Encode.encodeListLen Word
2 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Encoding
payload)
where
payload :: Encoding
payload = case Script
script of
Native NativeScript
native -> Word -> Encoding
Encode.encodeWord Word
0 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
Encode.encodePreEncoded (NativeScript -> ByteString
Native.encodeCbor NativeScript
native)
Plutus PlutusScript
plutus -> Word -> Encoding
Encode.encodeWord (Int -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Version -> Int
forall a. Enum a => a -> Int
fromEnum (PlutusScript -> Version
Plutus.version PlutusScript
plutus) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)) Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
Encode.encodePreEncoded (PlutusScript -> ByteString
Plutus.encodeCbor PlutusScript
plutus)
decodeCbor :: ByteString -> Either Error Script
decodeCbor :: ByteString -> Either Error Script
decodeCbor ByteString
bytes = case (forall s. Decoder s (Word, ByteString))
-> ByteString
-> Either DeserialiseFailure (ByteString, (Word, ByteString))
forall a.
(forall s. Decoder s a)
-> ByteString -> Either DeserialiseFailure (ByteString, a)
Read.deserialiseFromBytes Decoder s (Word, ByteString)
forall s. Decoder s (Word, ByteString)
decodeEnvelope (ByteString -> ByteString
Lazy.fromStrict ByteString
bytes) of
Left DeserialiseFailure
failure -> Error -> Either Error Script
forall a b. a -> Either a b
Left (Text -> Error
InvalidScriptCbor (String -> Text
Text.pack (DeserialiseFailure -> String
forall a. Show a => a -> String
show DeserialiseFailure
failure)))
Right (ByteString
trailing, (Word
language, ByteString
payload))
| Bool -> Bool
not (ByteString -> Bool
Lazy.null ByteString
trailing) -> Error -> Either Error Script
forall a b. a -> Either a b
Left (Text -> Error
InvalidScriptCbor Text
"Trailing bytes after script envelope")
| Bool
otherwise -> case Word
language of
Word
0 -> NativeScript -> Script
Native (NativeScript -> Script)
-> Either Error NativeScript -> Either Error Script
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either Error NativeScript
Native.decodeCbor ByteString
payload
Word
1 -> PlutusScript -> Script
Plutus (PlutusScript -> Script)
-> Either Error PlutusScript -> Either Error Script
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Version -> ByteString -> Either Error PlutusScript
Plutus.decodeCbor Version
Plutus.V1 ByteString
payload
Word
2 -> PlutusScript -> Script
Plutus (PlutusScript -> Script)
-> Either Error PlutusScript -> Either Error Script
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Version -> ByteString -> Either Error PlutusScript
Plutus.decodeCbor Version
Plutus.V2 ByteString
payload
Word
3 -> PlutusScript -> Script
Plutus (PlutusScript -> Script)
-> Either Error PlutusScript -> Either Error Script
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Version -> ByteString -> Either Error PlutusScript
Plutus.decodeCbor Version
Plutus.V3 ByteString
payload
Word
4 -> PlutusScript -> Script
Plutus (PlutusScript -> Script)
-> Either Error PlutusScript -> Either Error Script
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Version -> ByteString -> Either Error PlutusScript
Plutus.decodeCbor Version
Plutus.V4 ByteString
payload
Word
_ -> Error -> Either Error Script
forall a b. a -> Either a b
Left (Text -> Error
InvalidScriptCbor Text
"Unknown script language")
where
decodeEnvelope :: Decoder s (Word, ByteString)
decodeEnvelope = do
Maybe Int
size <- Decoder s (Maybe Int)
forall s. Decoder s (Maybe Int)
Decode.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
== Int
2) Maybe Int
size) (String -> Decoder s ()
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Invalid script envelope arity")
Word
language <- Decoder s Word
forall s. Decoder s Word
Decode.decodeWord
(Term
_, ByteOffset
start, ByteOffset
end) <- Decoder s Term -> Decoder s (Term, ByteOffset, ByteOffset)
forall s a. Decoder s a -> Decoder s (a, ByteOffset, ByteOffset)
Decode.decodeWithByteSpan Decoder s Term
forall s. Decoder s Term
decodeTerm
case Maybe Int
size of
Just Int
_ -> () -> Decoder s ()
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
Maybe Int
Nothing -> do
Bool
ended <- Decoder s Bool
forall s. Decoder s Bool
Decode.decodeBreakOr
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
"Unexpected script envelope field")
let payload :: ByteString
payload = Int -> ByteString -> ByteString
ByteString.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
ByteString.drop (ByteOffset -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral ByteOffset
start) ByteString
bytes)
(Word, ByteString) -> Decoder s (Word, ByteString)
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Word
language, ByteString
payload)
hash :: Script -> IO (Either Error ScriptHash)
hash :: Script -> IO (Either Error ScriptHash)
hash Script
script = case Script
script of
Native NativeScript
native -> NativeScript -> IO (Either Error ScriptHash)
Native.hash NativeScript
native
Plutus PlutusScript
plutus -> PlutusScript -> IO (Either Error ScriptHash)
Plutus.hash PlutusScript
plutus