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

-- | Validated 28-byte native-asset policy identifiers.
-- Import assigns an identifier; it does not execute or hash a policy script.
--
-- @
-- import qualified Cometa.Credential as Credential
-- import qualified Cometa.PolicyId as PolicyId
--
-- policy :: Credential.ScriptHash -> PolicyId.PolicyId
-- policy = PolicyId.fromScriptHash
-- @
module Cometa.PolicyId
  ( PolicyId,
    fromBytes,
    toBytes,
    fromHex,
    toHex,
    fromScriptHash,
    toScriptHash,
  )
where

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

import qualified Cometa.Credential as Credential
import Cometa.Error (Error (..))
import qualified Cometa.Internal.Hex as Hex
import Data.ByteString (ByteString)
import qualified Data.ByteString as ByteString
import Data.Text (Text)

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

-- | A 28-byte script-policy hash. Equality and ordering compare bytes
-- lexicographically. 'Show' is diagnostic output.
newtype PolicyId = PolicyId Credential.ScriptHash deriving (PolicyId -> PolicyId -> Bool
(PolicyId -> PolicyId -> Bool)
-> (PolicyId -> PolicyId -> Bool) -> Eq PolicyId
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PolicyId -> PolicyId -> Bool
== :: PolicyId -> PolicyId -> Bool
$c/= :: PolicyId -> PolicyId -> Bool
/= :: PolicyId -> PolicyId -> Bool
Eq, Eq PolicyId
Eq PolicyId =>
(PolicyId -> PolicyId -> Ordering)
-> (PolicyId -> PolicyId -> Bool)
-> (PolicyId -> PolicyId -> Bool)
-> (PolicyId -> PolicyId -> Bool)
-> (PolicyId -> PolicyId -> Bool)
-> (PolicyId -> PolicyId -> PolicyId)
-> (PolicyId -> PolicyId -> PolicyId)
-> Ord PolicyId
PolicyId -> PolicyId -> Bool
PolicyId -> PolicyId -> Ordering
PolicyId -> PolicyId -> PolicyId
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 :: PolicyId -> PolicyId -> Ordering
compare :: PolicyId -> PolicyId -> Ordering
$c< :: PolicyId -> PolicyId -> Bool
< :: PolicyId -> PolicyId -> Bool
$c<= :: PolicyId -> PolicyId -> Bool
<= :: PolicyId -> PolicyId -> Bool
$c> :: PolicyId -> PolicyId -> Bool
> :: PolicyId -> PolicyId -> Bool
$c>= :: PolicyId -> PolicyId -> Bool
>= :: PolicyId -> PolicyId -> Bool
$cmax :: PolicyId -> PolicyId -> PolicyId
max :: PolicyId -> PolicyId -> PolicyId
$cmin :: PolicyId -> PolicyId -> PolicyId
min :: PolicyId -> PolicyId -> PolicyId
Ord, Int -> PolicyId -> ShowS
[PolicyId] -> ShowS
PolicyId -> String
(Int -> PolicyId -> ShowS)
-> (PolicyId -> String) -> ([PolicyId] -> ShowS) -> Show PolicyId
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PolicyId -> ShowS
showsPrec :: Int -> PolicyId -> ShowS
$cshow :: PolicyId -> String
show :: PolicyId -> String
$cshowList :: [PolicyId] -> ShowS
showList :: [PolicyId] -> ShowS
Show)

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

-- | Import exactly 28 bytes. Other lengths return 'InvalidPolicyIdLength'
-- with the actual byte count. This does not compute a script hash.
fromBytes :: ByteString -> Either Error PolicyId
fromBytes :: ByteString -> Either Error PolicyId
fromBytes ByteString
bytes
  | ByteString -> Int
ByteString.length ByteString
bytes Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
28 = Error -> Either Error PolicyId
forall a b. a -> Either a b
Left (Int -> Error
InvalidPolicyIdLength (ByteString -> Int
ByteString.length ByteString
bytes))
  | Bool
otherwise = ScriptHash -> PolicyId
PolicyId (ScriptHash -> PolicyId)
-> Either Error ScriptHash -> Either Error PolicyId
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either Error ScriptHash
Credential.scriptHashFromBytes ByteString
bytes

-- | Exact policy-hash bytes.
toBytes :: PolicyId -> ByteString
toBytes :: PolicyId -> ByteString
toBytes = ScriptHash -> ByteString
Credential.scriptHashBytes (ScriptHash -> ByteString)
-> (PolicyId -> ScriptHash) -> PolicyId -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PolicyId -> ScriptHash
toScriptHash

-- | Import exactly 56 ASCII hexadecimal digits, accepting either case.
-- Malformed hexadecimal returns 'InvalidHex'; valid hex of another byte length
-- returns 'InvalidPolicyIdLength', including empty input.
fromHex :: Text -> Either Error PolicyId
fromHex :: Text -> Either Error PolicyId
fromHex Text
text = Either Error PolicyId
-> (ByteString -> Either Error PolicyId)
-> Maybe ByteString
-> Either Error PolicyId
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Error -> Either Error PolicyId
forall a b. a -> Either a b
Left Error
InvalidHex) ByteString -> Either Error PolicyId
fromBytes (Text -> Maybe ByteString
Hex.decodeHex Text
text)

-- | Lowercase hexadecimal without a prefix, always 56 digits.
toHex :: PolicyId -> Text
toHex :: PolicyId -> Text
toHex = ByteString -> Text
Hex.encodeHex (ByteString -> Text)
-> (PolicyId -> ByteString) -> PolicyId -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PolicyId -> ByteString
toBytes

-- | Assign an already validated script hash as a minting-policy identifier.
fromScriptHash :: Credential.ScriptHash -> PolicyId
fromScriptHash :: ScriptHash -> PolicyId
fromScriptHash = ScriptHash -> PolicyId
PolicyId

-- | The script hash identifying this policy.
toScriptHash :: PolicyId -> Credential.ScriptHash
toScriptHash :: PolicyId -> ScriptHash
toScriptHash (PolicyId ScriptHash
scriptHash) = ScriptHash
scriptHash