{-# LANGUAGE OverloadedStrings #-}
module Cometa.Script.Native
(
NativeScript,
NativeScriptView (..),
signature,
allOf,
anyOf,
atLeast,
invalidBefore,
invalidAfter,
requireGuard,
view,
encodeCbor,
decodeCbor,
toJson,
fromJson,
hash,
)
where
import qualified Codec.CBOR.Decoding as Decode
import qualified Codec.CBOR.Encoding as Encode
import qualified Codec.CBOR.Read as Read
import qualified Codec.CBOR.Write as Write
import Cometa.Credential (Credential, Guard, KeyHash, Payment, ScriptHash)
import qualified Cometa.Credential as Credential
import qualified Cometa.Crypto.Blake2b as Blake2b
import Cometa.Error (Error (..))
import qualified Cometa.Internal.Hex as Hex
import Control.Monad (replicateM, unless)
import Data.Aeson ((.:), (.=))
import qualified Data.Aeson as Aeson
import qualified Data.Aeson.KeyMap as KeyMap
import qualified Data.Aeson.Types as Aeson
import Data.ByteString (ByteString)
import qualified Data.ByteString as ByteString
import qualified Data.ByteString.Lazy as Lazy
import qualified Data.Text as Text
import Data.Word (Word64)
data NativeScript = NativeScript !ByteString !NativeScriptView
deriving (Int -> NativeScript -> ShowS
[NativeScript] -> ShowS
NativeScript -> String
(Int -> NativeScript -> ShowS)
-> (NativeScript -> String)
-> ([NativeScript] -> ShowS)
-> Show NativeScript
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> NativeScript -> ShowS
showsPrec :: Int -> NativeScript -> ShowS
$cshow :: NativeScript -> String
show :: NativeScript -> String
$cshowList :: [NativeScript] -> ShowS
showList :: [NativeScript] -> ShowS
Show)
instance Eq NativeScript where
NativeScript
left == :: NativeScript -> NativeScript -> Bool
== NativeScript
right = NativeScript -> ByteString
encodeCbor NativeScript
left ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== NativeScript -> ByteString
encodeCbor NativeScript
right
instance Ord NativeScript where
compare :: NativeScript -> NativeScript -> Ordering
compare NativeScript
left NativeScript
right = ByteString -> ByteString -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (NativeScript -> ByteString
encodeCbor NativeScript
left) (NativeScript -> ByteString
encodeCbor NativeScript
right)
data NativeScriptView
=
RequireSignature !(KeyHash Payment)
|
RequireAll ![NativeScript]
|
RequireAny ![NativeScript]
|
RequireAtLeast !Word64 ![NativeScript]
|
InvalidBefore !Word64
|
InvalidAfter !Word64
|
RequireGuard !(Credential Guard)
deriving (NativeScriptView -> NativeScriptView -> Bool
(NativeScriptView -> NativeScriptView -> Bool)
-> (NativeScriptView -> NativeScriptView -> Bool)
-> Eq NativeScriptView
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: NativeScriptView -> NativeScriptView -> Bool
== :: NativeScriptView -> NativeScriptView -> Bool
$c/= :: NativeScriptView -> NativeScriptView -> Bool
/= :: NativeScriptView -> NativeScriptView -> Bool
Eq, Eq NativeScriptView
Eq NativeScriptView =>
(NativeScriptView -> NativeScriptView -> Ordering)
-> (NativeScriptView -> NativeScriptView -> Bool)
-> (NativeScriptView -> NativeScriptView -> Bool)
-> (NativeScriptView -> NativeScriptView -> Bool)
-> (NativeScriptView -> NativeScriptView -> Bool)
-> (NativeScriptView -> NativeScriptView -> NativeScriptView)
-> (NativeScriptView -> NativeScriptView -> NativeScriptView)
-> Ord NativeScriptView
NativeScriptView -> NativeScriptView -> Bool
NativeScriptView -> NativeScriptView -> Ordering
NativeScriptView -> NativeScriptView -> NativeScriptView
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 :: NativeScriptView -> NativeScriptView -> Ordering
compare :: NativeScriptView -> NativeScriptView -> Ordering
$c< :: NativeScriptView -> NativeScriptView -> Bool
< :: NativeScriptView -> NativeScriptView -> Bool
$c<= :: NativeScriptView -> NativeScriptView -> Bool
<= :: NativeScriptView -> NativeScriptView -> Bool
$c> :: NativeScriptView -> NativeScriptView -> Bool
> :: NativeScriptView -> NativeScriptView -> Bool
$c>= :: NativeScriptView -> NativeScriptView -> Bool
>= :: NativeScriptView -> NativeScriptView -> Bool
$cmax :: NativeScriptView -> NativeScriptView -> NativeScriptView
max :: NativeScriptView -> NativeScriptView -> NativeScriptView
$cmin :: NativeScriptView -> NativeScriptView -> NativeScriptView
min :: NativeScriptView -> NativeScriptView -> NativeScriptView
Ord, Int -> NativeScriptView -> ShowS
[NativeScriptView] -> ShowS
NativeScriptView -> String
(Int -> NativeScriptView -> ShowS)
-> (NativeScriptView -> String)
-> ([NativeScriptView] -> ShowS)
-> Show NativeScriptView
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> NativeScriptView -> ShowS
showsPrec :: Int -> NativeScriptView -> ShowS
$cshow :: NativeScriptView -> String
show :: NativeScriptView -> String
$cshowList :: [NativeScriptView] -> ShowS
showList :: [NativeScriptView] -> ShowS
Show)
signature :: KeyHash Payment -> NativeScript
signature :: KeyHash Payment -> NativeScript
signature KeyHash Payment
key = NativeScriptView -> NativeScript
assemble (KeyHash Payment -> NativeScriptView
RequireSignature KeyHash Payment
key)
allOf :: [NativeScript] -> NativeScript
allOf :: [NativeScript] -> NativeScript
allOf = NativeScriptView -> NativeScript
assemble (NativeScriptView -> NativeScript)
-> ([NativeScript] -> NativeScriptView)
-> [NativeScript]
-> NativeScript
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [NativeScript] -> NativeScriptView
RequireAll
anyOf :: [NativeScript] -> NativeScript
anyOf :: [NativeScript] -> NativeScript
anyOf = NativeScriptView -> NativeScript
assemble (NativeScriptView -> NativeScript)
-> ([NativeScript] -> NativeScriptView)
-> [NativeScript]
-> NativeScript
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [NativeScript] -> NativeScriptView
RequireAny
atLeast :: Word64 -> [NativeScript] -> NativeScript
atLeast :: Word64 -> [NativeScript] -> NativeScript
atLeast Word64
required [NativeScript]
children = NativeScriptView -> NativeScript
assemble (Word64 -> [NativeScript] -> NativeScriptView
RequireAtLeast Word64
required [NativeScript]
children)
invalidBefore :: Word64 -> NativeScript
invalidBefore :: Word64 -> NativeScript
invalidBefore = NativeScriptView -> NativeScript
assemble (NativeScriptView -> NativeScript)
-> (Word64 -> NativeScriptView) -> Word64 -> NativeScript
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> NativeScriptView
InvalidBefore
invalidAfter :: Word64 -> NativeScript
invalidAfter :: Word64 -> NativeScript
invalidAfter = NativeScriptView -> NativeScript
assemble (NativeScriptView -> NativeScript)
-> (Word64 -> NativeScriptView) -> Word64 -> NativeScript
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word64 -> NativeScriptView
InvalidAfter
requireGuard :: Credential Guard -> NativeScript
requireGuard :: Credential Guard -> NativeScript
requireGuard = NativeScriptView -> NativeScript
assemble (NativeScriptView -> NativeScript)
-> (Credential Guard -> NativeScriptView)
-> Credential Guard
-> NativeScript
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Credential Guard -> NativeScriptView
RequireGuard
view :: NativeScript -> NativeScriptView
view :: NativeScript -> NativeScriptView
view (NativeScript ByteString
_ NativeScriptView
alternative) = NativeScriptView
alternative
encodeCbor :: NativeScript -> ByteString
encodeCbor :: NativeScript -> ByteString
encodeCbor (NativeScript ByteString
bytes NativeScriptView
_) = ByteString
bytes
decodeCbor :: ByteString -> Either Error NativeScript
decodeCbor :: ByteString -> Either Error NativeScript
decodeCbor ByteString
bytes = case (forall s. Decoder s NativeScript)
-> ByteString
-> Either DeserialiseFailure (ByteString, NativeScript)
forall a.
(forall s. Decoder s a)
-> ByteString -> Either DeserialiseFailure (ByteString, a)
Read.deserialiseFromBytes (ByteString -> Decoder s NativeScript
forall s. ByteString -> Decoder s NativeScript
decodeScript ByteString
bytes) (ByteString -> ByteString
Lazy.fromStrict ByteString
bytes) of
Left DeserialiseFailure
failure -> Error -> Either Error NativeScript
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, NativeScript
script)
| ByteString -> Bool
Lazy.null ByteString
trailing -> NativeScript -> Either Error NativeScript
forall a b. b -> Either a b
Right NativeScript
script
| Bool
otherwise -> Error -> Either Error NativeScript
forall a b. a -> Either a b
Left (Text -> Error
InvalidScriptCbor Text
"Trailing bytes after native script")
hash :: NativeScript -> IO (Either Error ScriptHash)
hash :: NativeScript -> IO (Either Error ScriptHash)
hash NativeScript
script = do
Either Error Blake2bHash
result <- Int -> ByteString -> IO (Either Error Blake2bHash)
Blake2b.compute Int
28 (Word8 -> ByteString -> ByteString
ByteString.cons Word8
0 (NativeScript -> ByteString
encodeCbor NativeScript
script))
Either Error ScriptHash -> IO (Either Error ScriptHash)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Error Blake2bHash
result Either Error Blake2bHash
-> (Blake2bHash -> Either Error ScriptHash)
-> Either Error ScriptHash
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 ScriptHash
Credential.scriptHashFromBytes (ByteString -> Either Error ScriptHash)
-> (Blake2bHash -> ByteString)
-> Blake2bHash
-> Either Error ScriptHash
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Blake2bHash -> ByteString
Blake2b.toBytes)
assemble :: NativeScriptView -> NativeScript
assemble :: NativeScriptView -> NativeScript
assemble NativeScriptView
alternative = ByteString -> NativeScriptView -> NativeScript
NativeScript (Encoding -> ByteString
Write.toStrictByteString (NativeScriptView -> Encoding
encodeView NativeScriptView
alternative)) NativeScriptView
alternative
encodeView :: NativeScriptView -> Encode.Encoding
encodeView :: NativeScriptView -> Encoding
encodeView NativeScriptView
alternative = case NativeScriptView
alternative of
RequireSignature KeyHash Payment
key -> Word -> Encoding -> Encoding
pair Word
0 (ByteString -> Encoding
Encode.encodeBytes (KeyHash Payment -> ByteString
forall purpose. KeyHash purpose -> ByteString
Credential.keyHashBytes KeyHash Payment
key))
RequireAll [NativeScript]
children -> Word -> Encoding -> Encoding
pair Word
1 ([NativeScript] -> Encoding
encodeChildren [NativeScript]
children)
RequireAny [NativeScript]
children -> Word -> Encoding -> Encoding
pair Word
2 ([NativeScript] -> Encoding
encodeChildren [NativeScript]
children)
RequireAtLeast Word64
required [NativeScript]
children -> Word -> Encoding
Encode.encodeListLen Word
3 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
Encode.encodeWord Word
3 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word64 -> Encoding
Encode.encodeWord64 Word64
required Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> [NativeScript] -> Encoding
encodeChildren [NativeScript]
children
InvalidBefore Word64
slot -> Word -> Encoding -> Encoding
pair Word
4 (Word64 -> Encoding
Encode.encodeWord64 Word64
slot)
InvalidAfter Word64
slot -> Word -> Encoding -> Encoding
pair Word
5 (Word64 -> Encoding
Encode.encodeWord64 Word64
slot)
RequireGuard Credential Guard
credential -> Word -> Encoding -> Encoding
pair Word
6 (Credential Guard -> Encoding
encodeCredential Credential Guard
credential)
where
pair :: Word -> Encoding -> Encoding
pair Word
tag Encoding
payload = Word -> Encoding
Encode.encodeListLen Word
2 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
Encode.encodeWord Word
tag Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Encoding
payload
encodeChildren :: [NativeScript] -> Encode.Encoding
encodeChildren :: [NativeScript] -> Encoding
encodeChildren [NativeScript]
children = Word -> Encoding
Encode.encodeListLen (Int -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral ([NativeScript] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [NativeScript]
children)) Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> (NativeScript -> Encoding) -> [NativeScript] -> Encoding
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (ByteString -> Encoding
Encode.encodePreEncoded (ByteString -> Encoding)
-> (NativeScript -> ByteString) -> NativeScript -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. NativeScript -> ByteString
encodeCbor) [NativeScript]
children
encodeCredential :: Credential Guard -> Encode.Encoding
encodeCredential :: Credential Guard -> Encoding
encodeCredential Credential Guard
credential =
Word -> Encoding
Encode.encodeListLen Word
2 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> case Credential Guard
credential of
Credential.KeyCredential KeyHash Guard
key -> Word -> Encoding
Encode.encodeWord Word
0 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
Encode.encodeBytes (KeyHash Guard -> ByteString
forall purpose. KeyHash purpose -> ByteString
Credential.keyHashBytes KeyHash Guard
key)
Credential.ScriptCredential ScriptHash
script -> Word -> Encoding
Encode.encodeWord Word
1 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
Encode.encodeBytes (ScriptHash -> ByteString
Credential.scriptHashBytes ScriptHash
script)
decodeScript :: ByteString -> Decode.Decoder s NativeScript
decodeScript :: forall s. ByteString -> Decoder s NativeScript
decodeScript ByteString
bytes = do
(NativeScriptView
alternative, ByteOffset
start, ByteOffset
end) <- Decoder s NativeScriptView
-> Decoder s (NativeScriptView, ByteOffset, ByteOffset)
forall s a. Decoder s a -> Decoder s (a, ByteOffset, ByteOffset)
Decode.decodeWithByteSpan Decoder s NativeScriptView
forall {s}. Decoder s NativeScriptView
decodeAlternative
NativeScript -> Decoder s NativeScript
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> NativeScriptView -> NativeScript
NativeScript (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)) NativeScriptView
alternative)
where
decodeAlternative :: Decoder s NativeScriptView
decodeAlternative = do
Maybe Int
size <- Decoder s (Maybe Int)
forall s. Decoder s (Maybe Int)
Decode.decodeListLenOrIndef
Word
tag <- Decoder s Word
forall s. Decoder s Word
Decode.decodeWord
let arity :: Int
arity = if Word
tag Word -> Word -> Bool
forall a. Eq a => a -> a -> Bool
== Word
3 then Int
3 else Int
2
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
arity) Maybe Int
size) (String -> Decoder s ()
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Invalid native script arity")
NativeScriptView
alternative <- case Word
tag of
Word
0 -> KeyHash Payment -> NativeScriptView
RequireSignature (KeyHash Payment -> NativeScriptView)
-> Decoder s (KeyHash Payment) -> Decoder s NativeScriptView
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Decoder s ByteString
forall s. Decoder s ByteString
decodeBytes Decoder s ByteString
-> (ByteString -> Decoder s (KeyHash Payment))
-> Decoder s (KeyHash Payment)
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 (KeyHash Payment))
-> (KeyHash Payment -> Decoder s (KeyHash Payment))
-> Either Error (KeyHash Payment)
-> Decoder s (KeyHash Payment)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Decoder s (KeyHash Payment) -> Error -> Decoder s (KeyHash Payment)
forall a b. a -> b -> a
const (String -> Decoder s (KeyHash Payment)
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Invalid signature hash length")) KeyHash Payment -> Decoder s (KeyHash Payment)
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Error (KeyHash Payment) -> Decoder s (KeyHash Payment))
-> (ByteString -> Either Error (KeyHash Payment))
-> ByteString
-> Decoder s (KeyHash Payment)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either Error (KeyHash Payment)
forall purpose. ByteString -> Either Error (KeyHash purpose)
Credential.keyHashFromBytes)
Word
1 -> [NativeScript] -> NativeScriptView
RequireAll ([NativeScript] -> NativeScriptView)
-> Decoder s [NativeScript] -> Decoder s NativeScriptView
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Decoder s [NativeScript]
forall s. ByteString -> Decoder s [NativeScript]
decodeChildren ByteString
bytes
Word
2 -> [NativeScript] -> NativeScriptView
RequireAny ([NativeScript] -> NativeScriptView)
-> Decoder s [NativeScript] -> Decoder s NativeScriptView
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Decoder s [NativeScript]
forall s. ByteString -> Decoder s [NativeScript]
decodeChildren ByteString
bytes
Word
3 -> do
Word64
required <- Decoder s Word64
forall s. Decoder s Word64
Decode.decodeWord64
[NativeScript]
children <- ByteString -> Decoder s [NativeScript]
forall s. ByteString -> Decoder s [NativeScript]
decodeChildren ByteString
bytes
NativeScriptView -> Decoder s NativeScriptView
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Word64 -> [NativeScript] -> NativeScriptView
RequireAtLeast Word64
required [NativeScript]
children)
Word
4 -> Word64 -> NativeScriptView
InvalidBefore (Word64 -> NativeScriptView)
-> Decoder s Word64 -> Decoder s NativeScriptView
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s Word64
forall s. Decoder s Word64
Decode.decodeWord64
Word
5 -> Word64 -> NativeScriptView
InvalidAfter (Word64 -> NativeScriptView)
-> Decoder s Word64 -> Decoder s NativeScriptView
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s Word64
forall s. Decoder s Word64
Decode.decodeWord64
Word
6 -> Credential Guard -> NativeScriptView
RequireGuard (Credential Guard -> NativeScriptView)
-> Decoder s (Credential Guard) -> Decoder s NativeScriptView
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s (Credential Guard)
forall s. Decoder s (Credential Guard)
decodeCredential
Word
_ -> String -> Decoder s NativeScriptView
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Unknown native script alternative"
Maybe Int -> Decoder s ()
forall s. Maybe Int -> Decoder s ()
finish Maybe Int
size
NativeScriptView -> Decoder s NativeScriptView
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure NativeScriptView
alternative
decodeChildren :: ByteString -> Decode.Decoder s [NativeScript]
decodeChildren :: forall s. ByteString -> Decoder s [NativeScript]
decodeChildren ByteString
bytes = do
Maybe Int
size <- Decoder s (Maybe Int)
forall s. Decoder s (Maybe Int)
Decode.decodeListLenOrIndef
case Maybe Int
size of
Just Int
count -> Int -> Decoder s NativeScript -> Decoder s [NativeScript]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM Int
count (ByteString -> Decoder s NativeScript
forall s. ByteString -> Decoder s NativeScript
decodeScript ByteString
bytes)
Maybe Int
Nothing -> [NativeScript] -> Decoder s [NativeScript]
forall {s}. [NativeScript] -> Decoder s [NativeScript]
loop []
where
loop :: [NativeScript] -> Decoder s [NativeScript]
loop [NativeScript]
accumulated = do
Bool
ended <- Decoder s Bool
forall s. Decoder s Bool
Decode.decodeBreakOr
if Bool
ended
then [NativeScript] -> Decoder s [NativeScript]
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([NativeScript] -> [NativeScript]
forall a. [a] -> [a]
reverse [NativeScript]
accumulated)
else do
NativeScript
child <- ByteString -> Decoder s NativeScript
forall s. ByteString -> Decoder s NativeScript
decodeScript ByteString
bytes
[NativeScript] -> Decoder s [NativeScript]
loop (NativeScript
child NativeScript -> [NativeScript] -> [NativeScript]
forall a. a -> [a] -> [a]
: [NativeScript]
accumulated)
decodeCredential :: Decode.Decoder s (Credential Guard)
decodeCredential :: forall s. Decoder s (Credential Guard)
decodeCredential = 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 guard credential arity")
Word
tag <- Decoder s Word
forall s. Decoder s Word
Decode.decodeWord
ByteString
bytes <- Decoder s ByteString
forall s. Decoder s ByteString
decodeBytes
Credential Guard
credential <- case Word
tag of
Word
0 -> (Error -> Decoder s (Credential Guard))
-> (KeyHash Guard -> Decoder s (Credential Guard))
-> Either Error (KeyHash Guard)
-> Decoder s (Credential Guard)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Decoder s (Credential Guard)
-> Error -> Decoder s (Credential Guard)
forall a b. a -> b -> a
const (String -> Decoder s (Credential Guard)
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Invalid guard key hash length")) (Credential Guard -> Decoder s (Credential Guard)
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Credential Guard -> Decoder s (Credential Guard))
-> (KeyHash Guard -> Credential Guard)
-> KeyHash Guard
-> Decoder s (Credential Guard)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. KeyHash Guard -> Credential Guard
forall purpose. KeyHash purpose -> Credential purpose
Credential.KeyCredential) (ByteString -> Either Error (KeyHash Guard)
forall purpose. ByteString -> Either Error (KeyHash purpose)
Credential.keyHashFromBytes ByteString
bytes)
Word
1 -> (Error -> Decoder s (Credential Guard))
-> (ScriptHash -> Decoder s (Credential Guard))
-> Either Error ScriptHash
-> Decoder s (Credential Guard)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Decoder s (Credential Guard)
-> Error -> Decoder s (Credential Guard)
forall a b. a -> b -> a
const (String -> Decoder s (Credential Guard)
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Invalid guard script hash length")) (Credential Guard -> Decoder s (Credential Guard)
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Credential Guard -> Decoder s (Credential Guard))
-> (ScriptHash -> Credential Guard)
-> ScriptHash
-> Decoder s (Credential Guard)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ScriptHash -> Credential Guard
forall purpose. ScriptHash -> Credential purpose
Credential.ScriptCredential) (ByteString -> Either Error ScriptHash
Credential.scriptHashFromBytes ByteString
bytes)
Word
_ -> String -> Decoder s (Credential Guard)
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Unknown guard credential alternative"
Maybe Int -> Decoder s ()
forall s. Maybe Int -> Decoder s ()
finish Maybe Int
size
Credential Guard -> Decoder s (Credential Guard)
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Credential Guard
credential
finish :: Maybe Int -> Decode.Decoder s ()
finish :: forall s. Maybe Int -> Decoder s ()
finish Maybe Int
size = 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 array field")
decodeBytes :: Decode.Decoder s ByteString
decodeBytes :: forall s. Decoder s ByteString
decodeBytes = do
TokenType
token <- Decoder s TokenType
forall s. Decoder s TokenType
Decode.peekTokenType
if TokenType
token TokenType -> TokenType -> Bool
forall a. Eq a => a -> a -> Bool
== TokenType
Decode.TypeBytesIndef
then do
Decoder s ()
forall s. Decoder s ()
Decode.decodeBytesIndef
[ByteString] -> Decoder s ByteString
forall {s}. [ByteString] -> Decoder s ByteString
loop []
else Decoder s ByteString
forall s. Decoder s ByteString
Decode.decodeBytes
where
loop :: [ByteString] -> Decoder s ByteString
loop [ByteString]
accumulated = do
Bool
ended <- Decoder s Bool
forall s. Decoder s Bool
Decode.decodeBreakOr
if Bool
ended
then ByteString -> Decoder s ByteString
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([ByteString] -> ByteString
ByteString.concat ([ByteString] -> [ByteString]
forall a. [a] -> [a]
reverse [ByteString]
accumulated))
else do
ByteString
chunk <- Decoder s ByteString
forall s. Decoder s ByteString
Decode.decodeBytes
[ByteString] -> Decoder s ByteString
loop (ByteString
chunk ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: [ByteString]
accumulated)
toJson :: NativeScript -> Aeson.Value
toJson :: NativeScript -> Value
toJson NativeScript
script = case NativeScript -> NativeScriptView
view NativeScript
script of
RequireSignature KeyHash Payment
key -> [Pair] -> Value
Aeson.object [Key
"type" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= (Text
"sig" :: Text.Text), Key
"keyHash" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= ByteString -> Text
Hex.encodeHex (KeyHash Payment -> ByteString
forall purpose. KeyHash purpose -> ByteString
Credential.keyHashBytes KeyHash Payment
key)]
RequireAll [NativeScript]
children -> Text -> [NativeScript] -> [Pair] -> Value
childrenObject Text
"all" [NativeScript]
children []
RequireAny [NativeScript]
children -> Text -> [NativeScript] -> [Pair] -> Value
childrenObject Text
"any" [NativeScript]
children []
RequireAtLeast Word64
required [NativeScript]
children -> Text -> [NativeScript] -> [Pair] -> Value
childrenObject Text
"atLeast" [NativeScript]
children [Key
"required" Key -> Word64 -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Word64
required]
InvalidBefore Word64
slot -> [Pair] -> Value
Aeson.object [Key
"type" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= (Text
"after" :: Text.Text), Key
"slot" Key -> Word64 -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Word64
slot]
InvalidAfter Word64
slot -> [Pair] -> Value
Aeson.object [Key
"type" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= (Text
"before" :: Text.Text), Key
"slot" Key -> Word64 -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= Word64
slot]
RequireGuard Credential Guard
credential ->
[Pair] -> Value
Aeson.object
( [Key
"type" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= (Text
"guard" :: Text.Text)] [Pair] -> [Pair] -> [Pair]
forall a. Semigroup a => a -> a -> a
<> case Credential Guard
credential of
Credential.KeyCredential KeyHash Guard
credentialHash -> [Key
"keyHash" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= ByteString -> Text
Hex.encodeHex (KeyHash Guard -> ByteString
forall purpose. KeyHash purpose -> ByteString
Credential.keyHashBytes KeyHash Guard
credentialHash)]
Credential.ScriptCredential ScriptHash
credentialHash -> [Key
"scriptHash" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= ByteString -> Text
Hex.encodeHex (ScriptHash -> ByteString
Credential.scriptHashBytes ScriptHash
credentialHash)]
)
where
childrenObject :: Text -> [NativeScript] -> [Pair] -> Value
childrenObject Text
kind [NativeScript]
children [Pair]
fields = [Pair] -> Value
Aeson.object ([Key
"type" Key -> Text -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= (Text
kind :: Text.Text), Key
"scripts" Key -> [Value] -> Pair
forall v. ToJSON v => Key -> v -> Pair
forall e kv v. (KeyValue e kv, ToJSON v) => Key -> v -> kv
.= (NativeScript -> Value) -> [NativeScript] -> [Value]
forall a b. (a -> b) -> [a] -> [b]
map NativeScript -> Value
toJson [NativeScript]
children] [Pair] -> [Pair] -> [Pair]
forall a. Semigroup a => a -> a -> a
<> [Pair]
fields)
fromJson :: Aeson.Value -> Either Error NativeScript
fromJson :: Value -> Either Error NativeScript
fromJson = (String -> Either Error NativeScript)
-> (NativeScript -> Either Error NativeScript)
-> Either String NativeScript
-> Either Error NativeScript
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Error -> Either Error NativeScript
forall a b. a -> Either a b
Left (Error -> Either Error NativeScript)
-> (String -> Error) -> String -> Either Error NativeScript
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Error
InvalidScriptJson (Text -> Error) -> (String -> Text) -> String -> Error
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
Text.pack) NativeScript -> Either Error NativeScript
forall a b. b -> Either a b
Right (Either String NativeScript -> Either Error NativeScript)
-> (Value -> Either String NativeScript)
-> Value
-> Either Error NativeScript
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Value -> Parser NativeScript)
-> Value -> Either String NativeScript
forall a b. (a -> Parser b) -> a -> Either String b
Aeson.parseEither Value -> Parser NativeScript
parseScript
where
parseScript :: Value -> Parser NativeScript
parseScript = String
-> (Object -> Parser NativeScript) -> Value -> Parser NativeScript
forall a. String -> (Object -> Parser a) -> Value -> Parser a
Aeson.withObject String
"native script" ((Object -> Parser NativeScript) -> Value -> Parser NativeScript)
-> (Object -> Parser NativeScript) -> Value -> Parser NativeScript
forall a b. (a -> b) -> a -> b
$ \Object
fields -> do
Text
kind <- Object
fields Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"type"
case (Text
kind :: Text.Text) of
Text
"sig" -> KeyHash Payment -> NativeScript
signature (KeyHash Payment -> NativeScript)
-> Parser (KeyHash Payment) -> Parser NativeScript
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Object
fields Object -> Key -> Parser Text
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"keyHash" Parser Text
-> (Text -> Parser (KeyHash Payment)) -> Parser (KeyHash Payment)
forall a b. Parser a -> (a -> Parser b) -> Parser b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Text -> Parser (KeyHash Payment)
forall {m :: * -> *} {purpose}.
MonadFail m =>
Text -> m (KeyHash purpose)
parseKey)
Text
"all" -> [NativeScript] -> NativeScript
allOf ([NativeScript] -> NativeScript)
-> Parser [NativeScript] -> Parser NativeScript
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object -> Parser [NativeScript]
parseChildren Object
fields
Text
"any" -> [NativeScript] -> NativeScript
anyOf ([NativeScript] -> NativeScript)
-> Parser [NativeScript] -> Parser NativeScript
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object -> Parser [NativeScript]
parseChildren Object
fields
Text
"atLeast" -> Word64 -> [NativeScript] -> NativeScript
atLeast (Word64 -> [NativeScript] -> NativeScript)
-> Parser Word64 -> Parser ([NativeScript] -> NativeScript)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
fields Object -> Key -> Parser Word64
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"required" Parser ([NativeScript] -> NativeScript)
-> Parser [NativeScript] -> Parser NativeScript
forall a b. Parser (a -> b) -> Parser a -> Parser b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Object -> Parser [NativeScript]
parseChildren Object
fields
Text
"after" -> Word64 -> NativeScript
invalidBefore (Word64 -> NativeScript) -> Parser Word64 -> Parser NativeScript
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
fields Object -> Key -> Parser Word64
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"slot"
Text
"before" -> Word64 -> NativeScript
invalidAfter (Word64 -> NativeScript) -> Parser Word64 -> Parser NativeScript
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
fields Object -> Key -> Parser Word64
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"slot"
Text
"guard" ->
Credential Guard -> NativeScript
requireGuard (Credential Guard -> NativeScript)
-> Parser (Credential Guard) -> Parser NativeScript
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> case (Key -> Object -> Maybe Value
forall v. Key -> KeyMap v -> Maybe v
KeyMap.lookup Key
"keyHash" Object
fields, Key -> Object -> Maybe Value
forall v. Key -> KeyMap v -> Maybe v
KeyMap.lookup Key
"scriptHash" Object
fields) of
(Just Value
key, Maybe Value
Nothing) -> KeyHash Guard -> Credential Guard
forall purpose. KeyHash purpose -> Credential purpose
Credential.KeyCredential (KeyHash Guard -> Credential Guard)
-> Parser (KeyHash Guard) -> Parser (Credential Guard)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Value -> Parser Text
forall a. FromJSON a => Value -> Parser a
Aeson.parseJSON Value
key Parser Text
-> (Text -> Parser (KeyHash Guard)) -> Parser (KeyHash Guard)
forall a b. Parser a -> (a -> Parser b) -> Parser b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Text -> Parser (KeyHash Guard)
forall {m :: * -> *} {purpose}.
MonadFail m =>
Text -> m (KeyHash purpose)
parseKey)
(Maybe Value
Nothing, Just Value
hashValue) -> ScriptHash -> Credential Guard
forall purpose. ScriptHash -> Credential purpose
Credential.ScriptCredential (ScriptHash -> Credential Guard)
-> Parser ScriptHash -> Parser (Credential Guard)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Value -> Parser Text
forall a. FromJSON a => Value -> Parser a
Aeson.parseJSON Value
hashValue Parser Text -> (Text -> Parser ByteString) -> Parser ByteString
forall a b. Parser a -> (a -> Parser b) -> Parser b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Text -> Parser ByteString
forall {m :: * -> *}. MonadFail m => Text -> m ByteString
parseHash Parser ByteString
-> (ByteString -> Parser ScriptHash) -> Parser ScriptHash
forall a b. Parser a -> (a -> Parser b) -> Parser b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Error -> Parser ScriptHash)
-> (ScriptHash -> Parser ScriptHash)
-> Either Error ScriptHash
-> Parser ScriptHash
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> Parser ScriptHash
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Parser ScriptHash)
-> (Error -> String) -> Error -> Parser ScriptHash
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Error -> String
forall a. Show a => a -> String
show) ScriptHash -> Parser ScriptHash
forall a. a -> Parser a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Error ScriptHash -> Parser ScriptHash)
-> (ByteString -> Either Error ScriptHash)
-> ByteString
-> Parser ScriptHash
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either Error ScriptHash
Credential.scriptHashFromBytes)
(Maybe Value, Maybe Value)
_ -> String -> Parser (Credential Guard)
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"guard requires exactly one keyHash or scriptHash"
Text
_ -> String -> Parser NativeScript
forall a. String -> Parser a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"unknown native script type"
parseChildren :: Object -> Parser [NativeScript]
parseChildren Object
fields = Object
fields Object -> Key -> Parser [Value]
forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"scripts" Parser [Value]
-> ([Value] -> Parser [NativeScript]) -> Parser [NativeScript]
forall a b. Parser a -> (a -> Parser b) -> Parser b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Value -> Parser NativeScript) -> [Value] -> Parser [NativeScript]
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) -> [a] -> f [b]
traverse Value -> Parser NativeScript
parseScript
parseKey :: Text -> m (KeyHash purpose)
parseKey Text
text = Text -> m ByteString
forall {m :: * -> *}. MonadFail m => Text -> m ByteString
parseHash Text
text m ByteString
-> (ByteString -> m (KeyHash purpose)) -> m (KeyHash purpose)
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Error -> m (KeyHash purpose))
-> (KeyHash purpose -> m (KeyHash purpose))
-> Either Error (KeyHash purpose)
-> m (KeyHash purpose)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> m (KeyHash purpose)
forall a. String -> m a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> m (KeyHash purpose))
-> (Error -> String) -> Error -> m (KeyHash purpose)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Error -> String
forall a. Show a => a -> String
show) KeyHash purpose -> m (KeyHash purpose)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Error (KeyHash purpose) -> m (KeyHash purpose))
-> (ByteString -> Either Error (KeyHash purpose))
-> ByteString
-> m (KeyHash purpose)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either Error (KeyHash purpose)
forall purpose. ByteString -> Either Error (KeyHash purpose)
Credential.keyHashFromBytes
parseHash :: Text -> m ByteString
parseHash Text
text = m ByteString
-> (ByteString -> m ByteString) -> Maybe ByteString -> m ByteString
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (String -> m ByteString
forall a. String -> m a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"invalid hash hexadecimal") ByteString -> m ByteString
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Text -> Maybe ByteString
Hex.decodeHex Text
text)