-- Copyright 2026 Biglup Labs
-- SPDX-License-Identifier: Apache-2.0
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
--     https://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
{-# LANGUAGE OverloadedStrings #-}

-- | Native signature, threshold, time and Dijkstra guard scripts. Values own
-- their exact CBOR encoding. Equality and ordering compare that encoding, so
-- semantically equivalent nonminimal encodings can differ. Slot numbers count
-- slots, with an inclusive lower bound and exclusive upper bound.
--
-- @
-- import qualified Cometa.Script.Native as Native
--
-- window :: Native.NativeScript
-- window = Native.allOf [Native.invalidBefore 100, Native.invalidAfter 200]
-- @
module Cometa.Script.Native
  ( -- * Script values
    NativeScript,
    NativeScriptView (..),

    -- * Conditions
    signature,
    allOf,
    anyOf,
    atLeast,
    invalidBefore,
    invalidAfter,
    requireGuard,

    -- * Inspection and encoding
    view,
    encodeCbor,
    decodeCbor,
    toJson,
    fromJson,

    -- * Script hash
    hash,
  )
where

-- IMPORTS --------------------------------------------------------------------

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)

-- TYPES ----------------------------------------------------------------------

-- | An immutable script with validated hash lengths and unsigned thresholds.
-- Imported script encodings are retained exactly, including child encodings.
-- 'Show' is diagnostic, not an interchange format.
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)

-- | Read-only alternatives. Construct scripts with the functions below;
-- inspecting this value cannot bypass credential validation.
data NativeScriptView
  = -- | Requires a payment verification-key signature.
    RequireSignature !(KeyHash Payment)
  | -- | All children must succeed; an empty list succeeds.
    RequireAll ![NativeScript]
  | -- | One or more children must succeed; an empty list fails.
    RequireAny ![NativeScript]
  | -- | Required count followed by ordered children; zero always succeeds.
    RequireAtLeast !Word64 ![NativeScript]
  | -- | Inclusive lower slot bound.
    InvalidBefore !Word64
  | -- | Exclusive upper slot bound.
    InvalidAfter !Word64
  | -- | Credential must be present in transaction guards (Dijkstra onwards).
    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)

-- DEFINITIONS ----------------------------------------------------------------

-- | Require a signature from this payment key hash.
signature :: KeyHash Payment -> NativeScript
signature :: KeyHash Payment -> NativeScript
signature KeyHash Payment
key = NativeScriptView -> NativeScript
assemble (KeyHash Payment -> NativeScriptView
RequireSignature KeyHash Payment
key)

-- | Require every child in order. Empty input is an always-succeeding script.
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

-- | Require at least one child. Empty input is an always-failing script.
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

-- | Require this many successful children. Counts above the child count are
-- valid and deliberately unsatisfiable. Duplicate children retain their positions
-- and count separately; zero with no children is valid and always succeeds.
atLeast :: Word64 -> [NativeScript] -> NativeScript
atLeast :: Word64 -> [NativeScript] -> NativeScript
atLeast Word64
required [NativeScript]
children = NativeScriptView -> NativeScript
assemble (Word64 -> [NativeScript] -> NativeScriptView
RequireAtLeast Word64
required [NativeScript]
children)

-- | Require the validity interval to start at or after the supplied slot.
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

-- | Require the validity interval to end at or before the supplied exclusive
-- upper bound. Slot zero is valid and yields an unsatisfiable time restriction.
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

-- | Require a key or script guard credential. This alternative requires
-- Dijkstra ledger support; construction does not assert era availability.
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

-- | Inspect the semantic alternative and its ordered children.
view :: NativeScript -> NativeScriptView
view :: NativeScript -> NativeScriptView
view (NativeScript ByteString
_ NativeScriptView
alternative) = NativeScriptView
alternative

-- | Exact imported bytes, or a definite-length encoding for assembled values.
-- Assembling a parent preserves each child's exact encoding.
encodeCbor :: NativeScript -> ByteString
encodeCbor :: NativeScript -> ByteString
encodeCbor (NativeScript ByteString
bytes NativeScriptView
_) = ByteString
bytes

-- | Decode exactly one complete script, checking all alternatives and credential
-- hash lengths. Malformed, trailing or schema-invalid bytes
-- return 'InvalidScriptCbor'. Nonminimal and indefinite encodings are retained.
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")

-- | Compute BLAKE2b-224 over byte zero followed by the exact script encoding.
-- Native hashing failures return 'Cometa.Error.NativeError'.
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)

-- Byte spans point into the immutable complete input; every child snapshot is
-- retained independently of the decoder and can safely be assembled elsewhere.
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)

-- | Export the native script JSON construction schema, shared with cardano-api
-- for signatures, all/any/atLeast and before/after. Dijkstra guards use
-- @{"type":"guard","keyHash":"..."}@ or @"scriptHash"@ as accepted by
-- cardano-c. Numbers are unsigned JSON integers and hashes are lowercase hex.
-- This describes semantic conditions, so importing it creates fresh CBOR and
-- does not retain an imported script's original encoding or hash identity.
-- This construction schema is distinct from CIP-116 inspection JSON.
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)

-- | Import native script construction JSON. Required fields and hash lengths
-- are validated, thresholds and slots must be unsigned 64-bit integers, and
-- children preserve order and duplicates. A guard requires exactly one of
-- @keyHash@ or @scriptHash@. Other object fields are ignored. Invalid input
-- returns 'InvalidScriptJson'. Valid but unsatisfiable thresholds are retained.
-- Fresh definite CBOR is assembled; original JSON/CBOR encoding is not retained.
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)