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

-- | Ada and native assets with validated policy and name components.
--
-- @
-- import qualified Cometa.AssetId as AssetId
-- import qualified Cometa.AssetName as AssetName
-- import qualified Cometa.PolicyId as PolicyId
--
-- unnamedToken :: PolicyId.PolicyId -> AssetId.AssetId
-- unnamedToken policy = AssetId.Native policy AssetName.empty
-- @
module Cometa.AssetId
  ( AssetId (..),
    fromBytes,
    toBytes,
    fromHex,
    toHex,
    fingerprint,
  )
where

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

import qualified Codec.Binary.Bech32 as Bech32
import Cometa.AssetName (AssetName)
import qualified Cometa.AssetName as AssetName
import qualified Cometa.Crypto.Blake2b as Blake2b
import Cometa.Error (Error (..))
import qualified Cometa.Internal.Hex as Hex
import Cometa.PolicyId (PolicyId)
import qualified Cometa.PolicyId as PolicyId
import Data.ByteString (ByteString)
import qualified Data.ByteString as ByteString
import Data.Text (Text)

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

-- | An asset's identity. Ordering places Ada first, then compares native policy
-- bytes and name bytes lexicographically. 'Show' is diagnostic output.
data AssetId
  = -- | Ada; its quantity is measured in lovelace (one millionth of an Ada).
    Ada
  | -- | A native asset, identified by policy and exact name bytes. An empty
    -- name under a policy is a native asset, distinct from Ada.
    Native !PolicyId !AssetName
  deriving (AssetId -> AssetId -> Bool
(AssetId -> AssetId -> Bool)
-> (AssetId -> AssetId -> Bool) -> Eq AssetId
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: AssetId -> AssetId -> Bool
== :: AssetId -> AssetId -> Bool
$c/= :: AssetId -> AssetId -> Bool
/= :: AssetId -> AssetId -> Bool
Eq, Eq AssetId
Eq AssetId =>
(AssetId -> AssetId -> Ordering)
-> (AssetId -> AssetId -> Bool)
-> (AssetId -> AssetId -> Bool)
-> (AssetId -> AssetId -> Bool)
-> (AssetId -> AssetId -> Bool)
-> (AssetId -> AssetId -> AssetId)
-> (AssetId -> AssetId -> AssetId)
-> Ord AssetId
AssetId -> AssetId -> Bool
AssetId -> AssetId -> Ordering
AssetId -> AssetId -> AssetId
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 :: AssetId -> AssetId -> Ordering
compare :: AssetId -> AssetId -> Ordering
$c< :: AssetId -> AssetId -> Bool
< :: AssetId -> AssetId -> Bool
$c<= :: AssetId -> AssetId -> Bool
<= :: AssetId -> AssetId -> Bool
$c> :: AssetId -> AssetId -> Bool
> :: AssetId -> AssetId -> Bool
$c>= :: AssetId -> AssetId -> Bool
>= :: AssetId -> AssetId -> Bool
$cmax :: AssetId -> AssetId -> AssetId
max :: AssetId -> AssetId -> AssetId
$cmin :: AssetId -> AssetId -> AssetId
min :: AssetId -> AssetId -> AssetId
Ord, Int -> AssetId -> ShowS
[AssetId] -> ShowS
AssetId -> String
(Int -> AssetId -> ShowS)
-> (AssetId -> String) -> ([AssetId] -> ShowS) -> Show AssetId
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> AssetId -> ShowS
showsPrec :: Int -> AssetId -> ShowS
$cshow :: AssetId -> String
show :: AssetId -> String
$cshowList :: [AssetId] -> ShowS
showList :: [AssetId] -> ShowS
Show)

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

-- | Import a native asset as its 28-byte policy followed by zero through 32
-- exact name bytes. Other lengths, including empty input, return
-- 'InvalidAssetIdLength'. Ada has no encoding in this format.
fromBytes :: ByteString -> Either Error AssetId
fromBytes :: ByteString -> Either Error AssetId
fromBytes ByteString
bytes
  | Int
size Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
28 Bool -> Bool -> Bool
|| Int
size Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
60 = Error -> Either Error AssetId
forall a b. a -> Either a b
Left (Int -> Error
InvalidAssetIdLength Int
size)
  | Bool
otherwise = PolicyId -> AssetName -> AssetId
Native (PolicyId -> AssetName -> AssetId)
-> Either Error PolicyId -> Either Error (AssetName -> AssetId)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either Error PolicyId
PolicyId.fromBytes ByteString
policy Either Error (AssetName -> AssetId)
-> Either Error AssetName -> Either Error AssetId
forall a b.
Either Error (a -> b) -> Either Error a -> Either Error b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ByteString -> Either Error AssetName
AssetName.fromBytes ByteString
name
  where
    size :: Int
size = ByteString -> Int
ByteString.length ByteString
bytes
    (ByteString
policy, ByteString
name) = Int -> ByteString -> (ByteString, ByteString)
ByteString.splitAt Int
28 ByteString
bytes

-- | Export the policy bytes followed by the exact name bytes. Ada returns
-- 'UnsupportedOperation'; an empty native name still exports 28 policy bytes.
toBytes :: AssetId -> Either Error ByteString
toBytes :: AssetId -> Either Error ByteString
toBytes AssetId
Ada = Error -> Either Error ByteString
forall a b. a -> Either a b
Left (Text -> Error
UnsupportedOperation Text
"AssetId.toBytes: Ada has no native asset encoding")
toBytes (Native PolicyId
policy AssetName
name) = ByteString -> Either Error ByteString
forall a b. b -> Either a b
Right (PolicyId -> ByteString
PolicyId.toBytes PolicyId
policy ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> AssetName -> ByteString
AssetName.toBytes AssetName
name)

-- | Import 56 through 120 ASCII hexadecimal digits as a native asset.
-- Either case is accepted, without a prefix or whitespace. Malformed hex
-- returns 'InvalidHex'; other decoded lengths return 'InvalidAssetIdLength'.
-- Empty text does not represent Ada.
fromHex :: Text -> Either Error AssetId
fromHex :: Text -> Either Error AssetId
fromHex Text
text = Either Error AssetId
-> (ByteString -> Either Error AssetId)
-> Maybe ByteString
-> Either Error AssetId
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Error -> Either Error AssetId
forall a b. a -> Either a b
Left Error
InvalidHex) ByteString -> Either Error AssetId
fromBytes (Text -> Maybe ByteString
Hex.decodeHex Text
text)

-- | Export the native asset as lowercase hexadecimal policy and name bytes,
-- without a separator. Ada returns 'UnsupportedOperation'.
toHex :: AssetId -> Either Error Text
toHex :: AssetId -> Either Error Text
toHex AssetId
asset = ByteString -> Text
Hex.encodeHex (ByteString -> Text)
-> Either Error ByteString -> Either Error Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> AssetId -> Either Error ByteString
toBytes AssetId
asset

-- | Compute the CIP-14 user-facing native asset fingerprint: BLAKE2b-160 of
-- the policy followed by the exact name bytes, encoded as Bech32 with the
-- @asset@ prefix. Ada returns 'UnsupportedOperation'. Hashing failures return
-- the corresponding 'NativeError'.
--
-- The fingerprint is a display checksum. It cannot be decoded back to the policy
-- and name, and does not replace the full asset identity or prove authenticity.
fingerprint :: AssetId -> IO (Either Error Text)
fingerprint :: AssetId -> IO (Either Error Text)
fingerprint AssetId
Ada = Either Error Text -> IO (Either Error Text)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Error -> Either Error Text
forall a b. a -> Either a b
Left (Text -> Error
UnsupportedOperation Text
"AssetId.fingerprint: Ada has no CIP-14 fingerprint"))
fingerprint AssetId
asset = case AssetId -> Either Error ByteString
toBytes AssetId
asset of
  Left Error
failure -> Either Error Text -> IO (Either Error Text)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Error -> Either Error Text
forall a b. a -> Either a b
Left Error
failure)
  Right ByteString
bytes -> do
    Either Error Blake2bHash
hashed <- Int -> ByteString -> IO (Either Error Blake2bHash)
Blake2b.compute Int
20 ByteString
bytes
    Either Error Text -> IO (Either Error Text)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Error Text -> IO (Either Error Text))
-> Either Error Text -> IO (Either Error Text)
forall a b. (a -> b) -> a -> b
$ do
      Blake2bHash
digest <- Either Error Blake2bHash
hashed
      HumanReadablePart
prefix <- (HumanReadablePartError -> Either Error HumanReadablePart)
-> (HumanReadablePart -> Either Error HumanReadablePart)
-> Either HumanReadablePartError HumanReadablePart
-> Either Error HumanReadablePart
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Either Error HumanReadablePart
-> HumanReadablePartError -> Either Error HumanReadablePart
forall a b. a -> b -> a
const (Error -> Either Error HumanReadablePart
forall a b. a -> Either a b
Left (Text -> Error
NativeContractViolation Text
"Invalid built-in asset fingerprint prefix"))) HumanReadablePart -> Either Error HumanReadablePart
forall a b. b -> Either a b
Right (Text -> Either HumanReadablePartError HumanReadablePart
Bech32.humanReadablePartFromText Text
"asset")
      Text -> Either Error Text
forall a. a -> Either Error a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (HumanReadablePart -> DataPart -> Text
Bech32.encodeLenient HumanReadablePart
prefix (ByteString -> DataPart
Bech32.dataPartFromBytes (Blake2bHash -> ByteString
Blake2b.toBytes Blake2bHash
digest)))