-- 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 #-}

-- | Assemble Plutus script arguments and datums as immutable Haskell values.
-- Maps preserve entry order and duplicate keys. Decode external datum CBOR into
-- 'EncodedPlutusData' to preserve the bytes that determine its hash.
--
-- @
-- import qualified Cometa.PlutusData as Data
--
-- main :: IO ()
-- main = print =<< Data.hash (Data.fromData (Data.Constr 0 [Data.Integer 42]))
-- @
module Cometa.PlutusData
  ( -- * Semantic data
    PlutusData (..),

    -- * CBOR and encoding identity
    EncodedPlutusData,
    encodeCbor,
    decodeCbor,
    fromData,
    integerFromText,
    toData,
    toCbor,

    -- * Datum 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.Crypto.Blake2b (Blake2bHash)
import qualified Cometa.Crypto.Blake2b as Blake2b
import qualified Cometa.Encoding.Integer as Integer
import Cometa.Error (Error (..))
import Control.Monad (replicateM, unless)
import Data.ByteString (ByteString)
import qualified Data.ByteString as ByteString
import qualified Data.ByteString.Lazy as Lazy
import Data.Maybe (isNothing)
import Data.Text (Text)
import qualified Data.Text as Text
import Data.Word (Word64)

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

-- | Semantic script data. Equality and ordering are structural: map entry order
-- and duplicates participate. Ordering follows constructor order, then payloads;
-- it does not define ledger map normalization.
data PlutusData
  = -- | Constructor alternative (0 through 2^64 - 1) and ordered fields.
    Constr Word64 [PlutusData]
  | -- | Ordered key/value pairs, including duplicate keys.
    Map [(PlutusData, PlutusData)]
  | -- | Ordered elements.
    List [PlutusData]
  | -- | An arbitrary-precision signed integer.
    Integer Integer
  | -- | Arbitrary bytes, including the empty byte string.
    Bytes ByteString
  deriving (PlutusData -> PlutusData -> Bool
(PlutusData -> PlutusData -> Bool)
-> (PlutusData -> PlutusData -> Bool) -> Eq PlutusData
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PlutusData -> PlutusData -> Bool
== :: PlutusData -> PlutusData -> Bool
$c/= :: PlutusData -> PlutusData -> Bool
/= :: PlutusData -> PlutusData -> Bool
Eq, Eq PlutusData
Eq PlutusData =>
(PlutusData -> PlutusData -> Ordering)
-> (PlutusData -> PlutusData -> Bool)
-> (PlutusData -> PlutusData -> Bool)
-> (PlutusData -> PlutusData -> Bool)
-> (PlutusData -> PlutusData -> Bool)
-> (PlutusData -> PlutusData -> PlutusData)
-> (PlutusData -> PlutusData -> PlutusData)
-> Ord PlutusData
PlutusData -> PlutusData -> Bool
PlutusData -> PlutusData -> Ordering
PlutusData -> PlutusData -> PlutusData
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 :: PlutusData -> PlutusData -> Ordering
compare :: PlutusData -> PlutusData -> Ordering
$c< :: PlutusData -> PlutusData -> Bool
< :: PlutusData -> PlutusData -> Bool
$c<= :: PlutusData -> PlutusData -> Bool
<= :: PlutusData -> PlutusData -> Bool
$c> :: PlutusData -> PlutusData -> Bool
> :: PlutusData -> PlutusData -> Bool
$c>= :: PlutusData -> PlutusData -> Bool
>= :: PlutusData -> PlutusData -> Bool
$cmax :: PlutusData -> PlutusData -> PlutusData
max :: PlutusData -> PlutusData -> PlutusData
$cmin :: PlutusData -> PlutusData -> PlutusData
min :: PlutusData -> PlutusData -> PlutusData
Ord, Int -> PlutusData -> ShowS
[PlutusData] -> ShowS
PlutusData -> String
(Int -> PlutusData -> ShowS)
-> (PlutusData -> String)
-> ([PlutusData] -> ShowS)
-> Show PlutusData
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PlutusData -> ShowS
showsPrec :: Int -> PlutusData -> ShowS
$cshow :: PlutusData -> String
show :: PlutusData -> String
$cshowList :: [PlutusData] -> ShowS
showList :: [PlutusData] -> ShowS
Show)

-- | Valid script data together with its exact CBOR identity. Equality and
-- ordering compare the original encoded bytes; inspect 'toData' for semantic
-- comparison. 'Show' is diagnostic and is not an interchange format.
data EncodedPlutusData = EncodedPlutusData !ByteString !PlutusData
  deriving (Int -> EncodedPlutusData -> ShowS
[EncodedPlutusData] -> ShowS
EncodedPlutusData -> String
(Int -> EncodedPlutusData -> ShowS)
-> (EncodedPlutusData -> String)
-> ([EncodedPlutusData] -> ShowS)
-> Show EncodedPlutusData
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> EncodedPlutusData -> ShowS
showsPrec :: Int -> EncodedPlutusData -> ShowS
$cshow :: EncodedPlutusData -> String
show :: EncodedPlutusData -> String
$cshowList :: [EncodedPlutusData] -> ShowS
showList :: [EncodedPlutusData] -> ShowS
Show)

instance Eq EncodedPlutusData where
  EncodedPlutusData
left == :: EncodedPlutusData -> EncodedPlutusData -> Bool
== EncodedPlutusData
right = EncodedPlutusData -> ByteString
toCbor EncodedPlutusData
left ByteString -> ByteString -> Bool
forall a. Eq a => a -> a -> Bool
== EncodedPlutusData -> ByteString
toCbor EncodedPlutusData
right

instance Ord EncodedPlutusData where
  compare :: EncodedPlutusData -> EncodedPlutusData -> Ordering
compare EncodedPlutusData
left EncodedPlutusData
right = ByteString -> ByteString -> Ordering
forall a. Ord a => a -> a -> Ordering
compare (EncodedPlutusData -> ByteString
toCbor EncodedPlutusData
left) (EncodedPlutusData -> ByteString
toCbor EncodedPlutusData
right)

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

-- | Parse an arbitrary-precision integer using a validated native-compatible
-- radix and assemble its fresh script-data encoding. No ledger metadata bounds
-- apply. Invalid text returns 'Cometa.Error.InvalidIntegerText'.
integerFromText :: Integer.Radix -> Text -> Either Error EncodedPlutusData
integerFromText :: Radix -> Text -> Either Error EncodedPlutusData
integerFromText Radix
radix Text
text = PlutusData -> EncodedPlutusData
fromData (PlutusData -> EncodedPlutusData)
-> (Integer -> PlutusData) -> Integer -> EncodedPlutusData
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Integer -> PlutusData
Integer (Integer -> EncodedPlutusData)
-> Either Error Integer -> Either Error EncodedPlutusData
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Radix -> Text -> Either Error Integer
Integer.fromText Radix
radix Text
text

-- | Serialize fresh semantic data with shortest integer/tag encodings, definite
-- lists and maps, and byte chunks of at most 64 bytes. Map order and duplicates
-- are retained. Re-encoding inspected external data may change its datum hash;
-- use 'toCbor' to preserve an existing encoding.
encodeCbor :: PlutusData -> ByteString
encodeCbor :: PlutusData -> ByteString
encodeCbor = Encoding -> ByteString
Write.toStrictByteString (Encoding -> ByteString)
-> (PlutusData -> Encoding) -> PlutusData -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PlutusData -> Encoding
encodeData

-- | Decode exactly one complete datum, accepting definite and indefinite lists,
-- maps and byte strings, nonminimal integer encodings, and constructor tags
-- 121..127, 1280..1400 and 102. Each definite byte leaf/chunk must contain at
-- most 64 bytes, including bignum payloads. Invalid input or trailing bytes
-- returns 'InvalidPlutusDataCbor'. The original bytes are retained unchanged.
decodeCbor :: ByteString -> Either Error EncodedPlutusData
decodeCbor :: ByteString -> Either Error EncodedPlutusData
decodeCbor ByteString
bytes = case (forall s. Decoder s PlutusData)
-> ByteString -> Either DeserialiseFailure (ByteString, PlutusData)
forall a.
(forall s. Decoder s a)
-> ByteString -> Either DeserialiseFailure (ByteString, a)
Read.deserialiseFromBytes Decoder s PlutusData
forall s. Decoder s PlutusData
decodeData (ByteString -> ByteString
Lazy.fromStrict ByteString
bytes) of
  Left DeserialiseFailure
failure -> Error -> Either Error EncodedPlutusData
forall a b. a -> Either a b
Left (Text -> Error
InvalidPlutusDataCbor (String -> Text
Text.pack (DeserialiseFailure -> String
forall a. Show a => a -> String
show DeserialiseFailure
failure)))
  Right (ByteString
remaining, PlutusData
value)
    | ByteString -> Bool
Lazy.null ByteString
remaining -> EncodedPlutusData -> Either Error EncodedPlutusData
forall a b. b -> Either a b
Right (ByteString -> PlutusData -> EncodedPlutusData
EncodedPlutusData ByteString
bytes PlutusData
value)
    | Bool
otherwise -> Error -> Either Error EncodedPlutusData
forall a b. a -> Either a b
Left (Text -> Error
InvalidPlutusDataCbor Text
"Trailing bytes after Plutus data")

-- | Give fresh semantic data a newly serialized encoding identity.
fromData :: PlutusData -> EncodedPlutusData
fromData :: PlutusData -> EncodedPlutusData
fromData PlutusData
value = ByteString -> PlutusData -> EncodedPlutusData
EncodedPlutusData (PlutusData -> ByteString
encodeCbor PlutusData
value) PlutusData
value

-- | Inspect semantic data without modifying its preserved encoding.
toData :: EncodedPlutusData -> PlutusData
toData :: EncodedPlutusData -> PlutusData
toData (EncodedPlutusData ByteString
_ PlutusData
value) = PlutusData
value

-- | Observe the exact original encoding, or the encoding assigned by 'fromData'.
toCbor :: EncodedPlutusData -> ByteString
toCbor :: EncodedPlutusData -> ByteString
toCbor (EncodedPlutusData ByteString
bytes PlutusData
_) = ByteString
bytes

-- | Compute the 32-byte BLAKE2b datum digest over the exact preserved CBOR.
-- Native allocation and operation failures return 'NativeError'.
hash :: EncodedPlutusData -> IO (Either Error Blake2bHash)
hash :: EncodedPlutusData -> IO (Either Error Blake2bHash)
hash = Int -> ByteString -> IO (Either Error Blake2bHash)
Blake2b.compute Int
32 (ByteString -> IO (Either Error Blake2bHash))
-> (EncodedPlutusData -> ByteString)
-> EncodedPlutusData
-> IO (Either Error Blake2bHash)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. EncodedPlutusData -> ByteString
toCbor

encodeData :: PlutusData -> Encode.Encoding
encodeData :: PlutusData -> Encoding
encodeData PlutusData
value = case PlutusData
value of
  Constr Word64
alternative [PlutusData]
fields
    | Word64
alternative Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
7 -> Word64 -> Encoding
Encode.encodeTag64 (Word64
121 Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
+ Word64
alternative) Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> [PlutusData] -> Encoding
encodeList [PlutusData]
fields
    | Word64
alternative Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< Word64
128 -> Word64 -> Encoding
Encode.encodeTag64 (Word64
1280 Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
+ Word64
alternative Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
- Word64
7) Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> [PlutusData] -> Encoding
encodeList [PlutusData]
fields
    | Bool
otherwise -> Word -> Encoding
Encode.encodeTag Word
102 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
Encode.encodeListLen Word
2 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word64 -> Encoding
Encode.encodeWord64 Word64
alternative Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> [PlutusData] -> Encoding
encodeList [PlutusData]
fields
  Map [(PlutusData, PlutusData)]
pairs -> Word -> Encoding
Encode.encodeMapLen (Int -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral ([(PlutusData, PlutusData)] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(PlutusData, PlutusData)]
pairs)) Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ((PlutusData, PlutusData) -> Encoding)
-> [(PlutusData, PlutusData)] -> Encoding
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (\(PlutusData
key, PlutusData
item) -> PlutusData -> Encoding
encodeData PlutusData
key Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> PlutusData -> Encoding
encodeData PlutusData
item) [(PlutusData, PlutusData)]
pairs
  List [PlutusData]
values -> [PlutusData] -> Encoding
encodeList [PlutusData]
values
  Integer Integer
number
    | Integer
number Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
>= Integer -> Integer
forall a. Num a => a -> a
negate (Integer
2 Integer -> Int -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^ (Int
64 :: Int)), Integer
number Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
2 Integer -> Int -> Integer
forall a b. (Num a, Integral b) => a -> b -> a
^ (Int
64 :: Int) -> Integer -> Encoding
Encode.encodeInteger Integer
number
    | Integer
number Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
>= Integer
0 -> Word -> Encoding
Encode.encodeTag Word
2 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
encodeBytes (Integer -> ByteString
integerBytes Integer
number)
    | Bool
otherwise -> Word -> Encoding
Encode.encodeTag Word
3 Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
encodeBytes (Integer -> ByteString
integerBytes (-Integer
1 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Integer
number))
  Bytes ByteString
bytes -> ByteString -> Encoding
encodeBytes ByteString
bytes

encodeList :: [PlutusData] -> Encode.Encoding
encodeList :: [PlutusData] -> Encoding
encodeList [PlutusData]
values = Word -> Encoding
Encode.encodeListLen (Int -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral ([PlutusData] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [PlutusData]
values)) Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> (PlutusData -> Encoding) -> [PlutusData] -> Encoding
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap PlutusData -> Encoding
encodeData [PlutusData]
values

encodeBytes :: ByteString -> Encode.Encoding
encodeBytes :: ByteString -> Encoding
encodeBytes ByteString
bytes
  | ByteString -> Int
ByteString.length ByteString
bytes Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
64 = ByteString -> Encoding
Encode.encodeBytes ByteString
bytes
  | Bool
otherwise = Encoding
Encode.encodeBytesIndef Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
chunks ByteString
bytes Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Encoding
Encode.encodeBreak
  where
    chunks :: ByteString -> Encoding
chunks ByteString
remaining
      | ByteString -> Bool
ByteString.null ByteString
remaining = Encoding
forall a. Monoid a => a
mempty
      | Bool
otherwise = let (ByteString
chunk, ByteString
rest) = Int -> ByteString -> (ByteString, ByteString)
ByteString.splitAt Int
64 ByteString
remaining in ByteString -> Encoding
Encode.encodeBytes ByteString
chunk Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ByteString -> Encoding
chunks ByteString
rest

integerBytes :: Integer -> ByteString
integerBytes :: Integer -> ByteString
integerBytes = ByteString -> ByteString
ByteString.reverse (ByteString -> ByteString)
-> (Integer -> ByteString) -> Integer -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Integer -> Maybe (Word8, Integer)) -> Integer -> ByteString
forall a. (a -> Maybe (Word8, a)) -> a -> ByteString
ByteString.unfoldr Integer -> Maybe (Word8, Integer)
forall {b} {a}. (Integral b, Num a) => b -> Maybe (a, b)
step
  where
    step :: b -> Maybe (a, b)
step b
0 = Maybe (a, b)
forall a. Maybe a
Nothing
    step b
number = (a, b) -> Maybe (a, b)
forall a. a -> Maybe a
Just (b -> a
forall a b. (Integral a, Num b) => a -> b
fromIntegral (b
number b -> b -> b
forall a. Integral a => a -> a -> a
`mod` b
256), b
number b -> b -> b
forall a. Integral a => a -> a -> a
`div` b
256)

decodeData :: Decode.Decoder s PlutusData
decodeData :: forall s. Decoder s PlutusData
decodeData = do
  TokenType
token <- Decoder s TokenType
forall s. Decoder s TokenType
Decode.peekTokenType
  case TokenType
token of
    TokenType
Decode.TypeUInt -> Integer -> PlutusData
Integer (Integer -> PlutusData)
-> Decoder s Integer -> Decoder s PlutusData
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s Integer
forall s. Decoder s Integer
Decode.decodeInteger
    TokenType
Decode.TypeUInt64 -> Integer -> PlutusData
Integer (Integer -> PlutusData)
-> Decoder s Integer -> Decoder s PlutusData
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s Integer
forall s. Decoder s Integer
Decode.decodeInteger
    TokenType
Decode.TypeNInt -> Integer -> PlutusData
Integer (Integer -> PlutusData)
-> Decoder s Integer -> Decoder s PlutusData
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s Integer
forall s. Decoder s Integer
Decode.decodeInteger
    TokenType
Decode.TypeNInt64 -> Integer -> PlutusData
Integer (Integer -> PlutusData)
-> Decoder s Integer -> Decoder s PlutusData
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s Integer
forall s. Decoder s Integer
Decode.decodeInteger
    TokenType
Decode.TypeInteger -> Decoder s PlutusData
forall s. Decoder s PlutusData
decodeTagged
    TokenType
Decode.TypeBytes -> ByteString -> PlutusData
Bytes (ByteString -> PlutusData)
-> Decoder s ByteString -> Decoder s PlutusData
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s ByteString
forall s. Decoder s ByteString
decodeBytes
    TokenType
Decode.TypeBytesIndef -> ByteString -> PlutusData
Bytes (ByteString -> PlutusData)
-> Decoder s ByteString -> Decoder s PlutusData
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s ByteString
forall s. Decoder s ByteString
decodeBytes
    TokenType
Decode.TypeListLen -> [PlutusData] -> PlutusData
List ([PlutusData] -> PlutusData)
-> Decoder s [PlutusData] -> Decoder s PlutusData
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s [PlutusData]
forall s. Decoder s [PlutusData]
decodeList
    TokenType
Decode.TypeListLen64 -> [PlutusData] -> PlutusData
List ([PlutusData] -> PlutusData)
-> Decoder s [PlutusData] -> Decoder s PlutusData
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s [PlutusData]
forall s. Decoder s [PlutusData]
decodeList
    TokenType
Decode.TypeListLenIndef -> [PlutusData] -> PlutusData
List ([PlutusData] -> PlutusData)
-> Decoder s [PlutusData] -> Decoder s PlutusData
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s [PlutusData]
forall s. Decoder s [PlutusData]
decodeList
    TokenType
Decode.TypeMapLen -> [(PlutusData, PlutusData)] -> PlutusData
Map ([(PlutusData, PlutusData)] -> PlutusData)
-> Decoder s [(PlutusData, PlutusData)] -> Decoder s PlutusData
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s [(PlutusData, PlutusData)]
forall s. Decoder s [(PlutusData, PlutusData)]
decodeMap
    TokenType
Decode.TypeMapLen64 -> [(PlutusData, PlutusData)] -> PlutusData
Map ([(PlutusData, PlutusData)] -> PlutusData)
-> Decoder s [(PlutusData, PlutusData)] -> Decoder s PlutusData
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s [(PlutusData, PlutusData)]
forall s. Decoder s [(PlutusData, PlutusData)]
decodeMap
    TokenType
Decode.TypeMapLenIndef -> [(PlutusData, PlutusData)] -> PlutusData
Map ([(PlutusData, PlutusData)] -> PlutusData)
-> Decoder s [(PlutusData, PlutusData)] -> Decoder s PlutusData
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s [(PlutusData, PlutusData)]
forall s. Decoder s [(PlutusData, PlutusData)]
decodeMap
    TokenType
Decode.TypeTag -> Decoder s PlutusData
forall s. Decoder s PlutusData
decodeTagged
    TokenType
Decode.TypeTag64 -> Decoder s PlutusData
forall s. Decoder s PlutusData
decodeTagged
    TokenType
_ -> String -> Decoder s PlutusData
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Expected Plutus constructor, map, list, integer or bytes"

decodeTagged :: Decode.Decoder s PlutusData
decodeTagged :: forall s. Decoder s PlutusData
decodeTagged = do
  Word64
tag <- Decoder s Word64
forall s. Decoder s Word64
Decode.decodeTag64
  case Word64
tag of
    Word64
2 -> Integer -> PlutusData
Integer (Integer -> PlutusData)
-> (ByteString -> Integer) -> ByteString -> PlutusData
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Integer
bytesInteger (ByteString -> PlutusData)
-> Decoder s ByteString -> Decoder s PlutusData
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s ByteString
forall s. Decoder s ByteString
decodeBytes
    Word64
3 -> Integer -> PlutusData
Integer (Integer -> PlutusData)
-> (ByteString -> Integer) -> ByteString -> PlutusData
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (-Integer
1 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
-) (Integer -> Integer)
-> (ByteString -> Integer) -> ByteString -> Integer
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Integer
bytesInteger (ByteString -> PlutusData)
-> Decoder s ByteString -> Decoder s PlutusData
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s ByteString
forall s. Decoder s ByteString
decodeBytes
    Word64
102 -> 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 (Maybe Int -> Bool
forall a. Maybe a -> Bool
isNothing Maybe Int
size Bool -> Bool -> Bool
|| Maybe Int
size Maybe Int -> Maybe Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int -> Maybe Int
forall a. a -> Maybe a
Just Int
2) (String -> Decoder s ()
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Extended constructor requires exactly two elements")
      Word64
alternative <- Decoder s Word64
forall s. Decoder s Word64
Decode.decodeWord64
      [PlutusData]
fields <- Decoder s [PlutusData]
forall s. Decoder s [PlutusData]
decodeList
      case Maybe Int
size of
        Maybe Int
Nothing -> do
          Bool
done <- Decoder s Bool
forall s. Decoder s Bool
Decode.decodeBreakOr
          Bool -> Decoder s () -> Decoder s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless Bool
done (String -> Decoder s ()
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Extended constructor requires exactly two elements")
        Just Int
_ -> () -> Decoder s ()
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
      PlutusData -> Decoder s PlutusData
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Word64 -> [PlutusData] -> PlutusData
Constr Word64
alternative [PlutusData]
fields)
    Word64
_
      | Word64
tag Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
>= Word64
121 Bool -> Bool -> Bool
&& Word64
tag Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
<= Word64
127 -> Word64 -> [PlutusData] -> PlutusData
Constr (Word64
tag Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
- Word64
121) ([PlutusData] -> PlutusData)
-> Decoder s [PlutusData] -> Decoder s PlutusData
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s [PlutusData]
forall s. Decoder s [PlutusData]
decodeList
      | Word64
tag Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
>= Word64
1280 Bool -> Bool -> Bool
&& Word64
tag Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
<= Word64
1400 -> Word64 -> [PlutusData] -> PlutusData
Constr (Word64
tag Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
- Word64
1280 Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
+ Word64
7) ([PlutusData] -> PlutusData)
-> Decoder s [PlutusData] -> Decoder s PlutusData
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s [PlutusData]
forall s. Decoder s [PlutusData]
decodeList
      | Bool
otherwise -> String -> Decoder s PlutusData
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Unknown Plutus constructor tag"

bytesInteger :: ByteString -> Integer
bytesInteger :: ByteString -> Integer
bytesInteger = (Integer -> Word8 -> Integer) -> Integer -> ByteString -> Integer
forall a. (a -> Word8 -> a) -> a -> ByteString -> a
ByteString.foldl' (\Integer
number Word8
byte -> Integer
number Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Integer
256 Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ Word8 -> Integer
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
byte) Integer
0

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
  case TokenType
token of
    TokenType
Decode.TypeBytes -> Decoder s ByteString
forall s. Decoder s ByteString
boundedBytes
    TokenType
Decode.TypeBytesIndef -> do
      Decoder s ()
forall s. Decoder s ()
Decode.decodeBytesIndef
      [ByteString] -> ByteString
ByteString.concat ([ByteString] -> ByteString)
-> Decoder s [ByteString] -> Decoder s ByteString
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s ByteString -> Decoder s [ByteString]
forall s value. Decoder s value -> Decoder s [value]
indefinite Decoder s ByteString
forall s. Decoder s ByteString
boundedBytes
    TokenType
_ -> String -> Decoder s ByteString
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Expected byte string"
  where
    boundedBytes :: Decoder s ByteString
boundedBytes = do
      ByteString
bytes <- Decoder s ByteString
forall s. Decoder s ByteString
Decode.decodeBytes
      Bool -> Decoder s () -> Decoder s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (ByteString -> Int
ByteString.length ByteString
bytes Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
64) (String -> Decoder s ()
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Plutus byte chunk exceeds 64 bytes")
      ByteString -> Decoder s ByteString
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ByteString
bytes

decodeList :: Decode.Decoder s [PlutusData]
decodeList :: forall s. Decoder s [PlutusData]
decodeList = do
  Maybe Int
size <- Decoder s (Maybe Int)
forall s. Decoder s (Maybe Int)
Decode.decodeListLenOrIndef
  case Maybe Int
size of
    Maybe Int
Nothing -> Decoder s PlutusData -> Decoder s [PlutusData]
forall s value. Decoder s value -> Decoder s [value]
indefinite Decoder s PlutusData
forall s. Decoder s PlutusData
decodeData
    Just Int
count -> Int -> Decoder s PlutusData -> Decoder s [PlutusData]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM Int
count Decoder s PlutusData
forall s. Decoder s PlutusData
decodeData

decodeMap :: Decode.Decoder s [(PlutusData, PlutusData)]
decodeMap :: forall s. Decoder s [(PlutusData, PlutusData)]
decodeMap = do
  Maybe Int
size <- Decoder s (Maybe Int)
forall s. Decoder s (Maybe Int)
Decode.decodeMapLenOrIndef
  case Maybe Int
size of
    Maybe Int
Nothing -> Decoder s (PlutusData, PlutusData)
-> Decoder s [(PlutusData, PlutusData)]
forall s value. Decoder s value -> Decoder s [value]
indefinite Decoder s (PlutusData, PlutusData)
forall {s}. Decoder s (PlutusData, PlutusData)
pair
    Just Int
count -> Int
-> Decoder s (PlutusData, PlutusData)
-> Decoder s [(PlutusData, PlutusData)]
forall (m :: * -> *) a. Applicative m => Int -> m a -> m [a]
replicateM Int
count Decoder s (PlutusData, PlutusData)
forall {s}. Decoder s (PlutusData, PlutusData)
pair
  where
    pair :: Decoder s (PlutusData, PlutusData)
pair = (,) (PlutusData -> PlutusData -> (PlutusData, PlutusData))
-> Decoder s PlutusData
-> Decoder s (PlutusData -> (PlutusData, PlutusData))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s PlutusData
forall s. Decoder s PlutusData
decodeData Decoder s (PlutusData -> (PlutusData, PlutusData))
-> Decoder s PlutusData -> Decoder s (PlutusData, PlutusData)
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s PlutusData
forall s. Decoder s PlutusData
decodeData

indefinite :: Decode.Decoder s value -> Decode.Decoder s [value]
indefinite :: forall s value. Decoder s value -> Decoder s [value]
indefinite Decoder s value
decoder = [value] -> Decoder s [value]
go []
  where
    go :: [value] -> Decoder s [value]
go [value]
accumulated = do
      Bool
done <- Decoder s Bool
forall s. Decoder s Bool
Decode.decodeBreakOr
      if Bool
done
        then [value] -> Decoder s [value]
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([value] -> [value]
forall a. [a] -> [a]
reverse [value]
accumulated)
        else do
          value
value <- Decoder s value
decoder
          [value] -> Decoder s [value]
go (value
value value -> [value] -> [value]
forall a. a -> [a] -> [a]
: [value]
accumulated)