-- 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.

-- | Output datum alternatives. Inline data retains exact datum bytes because
-- re-encoding a datum can change its hash.
--
-- @
-- noDatum = NoDatum
-- @
module Cometa.Transaction.Datum (Datum (..), Hash32, hashFromBytes, hashBytes) where

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

import Cometa.Error (Error (..))
import Cometa.PlutusData (EncodedPlutusData)
import Data.ByteString (ByteString)
import qualified Data.ByteString as B

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

-- | An exact 32-byte datum, script-data or auxiliary-data digest. Equality and
-- ordering compare bytes; importing does not verify how the digest was obtained.
newtype Hash32 = Hash32 ByteString deriving (Hash32 -> Hash32 -> Bool
(Hash32 -> Hash32 -> Bool)
-> (Hash32 -> Hash32 -> Bool) -> Eq Hash32
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Hash32 -> Hash32 -> Bool
== :: Hash32 -> Hash32 -> Bool
$c/= :: Hash32 -> Hash32 -> Bool
/= :: Hash32 -> Hash32 -> Bool
Eq, Eq Hash32
Eq Hash32 =>
(Hash32 -> Hash32 -> Ordering)
-> (Hash32 -> Hash32 -> Bool)
-> (Hash32 -> Hash32 -> Bool)
-> (Hash32 -> Hash32 -> Bool)
-> (Hash32 -> Hash32 -> Bool)
-> (Hash32 -> Hash32 -> Hash32)
-> (Hash32 -> Hash32 -> Hash32)
-> Ord Hash32
Hash32 -> Hash32 -> Bool
Hash32 -> Hash32 -> Ordering
Hash32 -> Hash32 -> Hash32
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 :: Hash32 -> Hash32 -> Ordering
compare :: Hash32 -> Hash32 -> Ordering
$c< :: Hash32 -> Hash32 -> Bool
< :: Hash32 -> Hash32 -> Bool
$c<= :: Hash32 -> Hash32 -> Bool
<= :: Hash32 -> Hash32 -> Bool
$c> :: Hash32 -> Hash32 -> Bool
> :: Hash32 -> Hash32 -> Bool
$c>= :: Hash32 -> Hash32 -> Bool
>= :: Hash32 -> Hash32 -> Bool
$cmax :: Hash32 -> Hash32 -> Hash32
max :: Hash32 -> Hash32 -> Hash32
$cmin :: Hash32 -> Hash32 -> Hash32
min :: Hash32 -> Hash32 -> Hash32
Ord, Int -> Hash32 -> ShowS
[Hash32] -> ShowS
Hash32 -> String
(Int -> Hash32 -> ShowS)
-> (Hash32 -> String) -> ([Hash32] -> ShowS) -> Show Hash32
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Hash32 -> ShowS
showsPrec :: Int -> Hash32 -> ShowS
$cshow :: Hash32 -> String
show :: Hash32 -> String
$cshowList :: [Hash32] -> ShowS
showList :: [Hash32] -> ShowS
Show)

-- | Output datum choice. A hash carries no witness datum; inline data carries
-- the exact encoded datum, including arbitrary map order and duplicate keys.
data Datum
  = -- | No datum is attached to the output.
    NoDatum
  | -- | Hash identifying a datum supplied separately as a witness when needed.
    DatumHash !Hash32
  | -- | Exact datum bytes embedded directly in the output.
    InlineDatum !EncodedPlutusData
  deriving (Datum -> Datum -> Bool
(Datum -> Datum -> Bool) -> (Datum -> Datum -> Bool) -> Eq Datum
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Datum -> Datum -> Bool
== :: Datum -> Datum -> Bool
$c/= :: Datum -> Datum -> Bool
/= :: Datum -> Datum -> Bool
Eq, Eq Datum
Eq Datum =>
(Datum -> Datum -> Ordering)
-> (Datum -> Datum -> Bool)
-> (Datum -> Datum -> Bool)
-> (Datum -> Datum -> Bool)
-> (Datum -> Datum -> Bool)
-> (Datum -> Datum -> Datum)
-> (Datum -> Datum -> Datum)
-> Ord Datum
Datum -> Datum -> Bool
Datum -> Datum -> Ordering
Datum -> Datum -> Datum
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 :: Datum -> Datum -> Ordering
compare :: Datum -> Datum -> Ordering
$c< :: Datum -> Datum -> Bool
< :: Datum -> Datum -> Bool
$c<= :: Datum -> Datum -> Bool
<= :: Datum -> Datum -> Bool
$c> :: Datum -> Datum -> Bool
> :: Datum -> Datum -> Bool
$c>= :: Datum -> Datum -> Bool
>= :: Datum -> Datum -> Bool
$cmax :: Datum -> Datum -> Datum
max :: Datum -> Datum -> Datum
$cmin :: Datum -> Datum -> Datum
min :: Datum -> Datum -> Datum
Ord, Int -> Datum -> ShowS
[Datum] -> ShowS
Datum -> String
(Int -> Datum -> ShowS)
-> (Datum -> String) -> ([Datum] -> ShowS) -> Show Datum
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Datum -> ShowS
showsPrec :: Int -> Datum -> ShowS
$cshow :: Datum -> String
show :: Datum -> String
$cshowList :: [Datum] -> ShowS
showList :: [Datum] -> ShowS
Show)

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

-- | Validate exactly 32 digest bytes; otherwise return 'InvalidTransactionHashLength'.
hashFromBytes :: ByteString -> Either Error Hash32
hashFromBytes :: ByteString -> Either Error Hash32
hashFromBytes ByteString
bytes
  | ByteString -> Int
B.length ByteString
bytes Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
32 = Hash32 -> Either Error Hash32
forall a b. b -> Either a b
Right (ByteString -> Hash32
Hash32 ByteString
bytes)
  | Bool
otherwise = Error -> Either Error Hash32
forall a b. a -> Either a b
Left (Int -> Error
InvalidTransactionHashLength (ByteString -> Int
B.length ByteString
bytes))

-- | Exact digest bytes, always length 32.
hashBytes :: Hash32 -> ByteString
hashBytes :: Hash32 -> ByteString
hashBytes (Hash32 ByteString
bytes) = ByteString
bytes