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

-- | Signed native-range asset bundle encoding; no native references.
module Cometa.Internal.TransactionValue (encodeAssets, decodeAssets, validateOutput, validateMint) where

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

import qualified Codec.CBOR.Decoding as D
import qualified Codec.CBOR.Encoding as E
import Cometa.AssetId (AssetId (..))
import qualified Cometa.AssetName as Name
import Cometa.Error (Error (..))
import qualified Cometa.Internal.TransactionCbor as Cbor
import qualified Cometa.PolicyId as Policy
import qualified Cometa.Value as Value
import Control.Monad (unless)
import qualified Data.ByteString as B
import Data.Int (Int64)
import Data.List (sortOn)
import qualified Data.Map.Strict as Map
import qualified Data.Set as Set

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

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

validateOutput :: Value.Value -> Either Error ()
validateOutput :: Value -> Either Error ()
validateOutput Value
value = ((AssetId, Integer) -> Either Error ())
-> [(AssetId, Integer)] -> Either Error ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (AssetId, Integer) -> Either Error ()
forall {a}. (a, Integer) -> Either Error ()
valid (Value -> [(AssetId, Integer)]
Value.toList Value
value)
  where
    valid :: (a, Integer) -> Either Error ()
valid (a
_, Integer
quantity)
      | Integer
quantity Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Integer
0 Bool -> Bool -> Bool
|| Integer
quantity Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Int64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Int64
forall a. Bounded a => a
maxBound :: Int64) = Error -> Either Error ()
forall a b. a -> Either a b
Left (Integer -> Error
InvalidTransactionOutputValue Integer
quantity)
      | Bool
otherwise = () -> Either Error ()
forall a b. b -> Either a b
Right ()

validateMint :: Value.Value -> Either Error ()
validateMint :: Value -> Either Error ()
validateMint Value
value = ((AssetId, Integer) -> Either Error ())
-> [(AssetId, Integer)] -> Either Error ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
(a -> m b) -> t a -> m ()
mapM_ (AssetId, Integer) -> Either Error ()
valid (Value -> [(AssetId, Integer)]
Value.toList Value
value)
  where
    valid :: (AssetId, Integer) -> Either Error ()
valid (AssetId
asset, Integer
quantity)
      | AssetId
asset AssetId -> AssetId -> Bool
forall a. Eq a => a -> a -> Bool
== AssetId
Ada = Error -> Either Error ()
forall a b. a -> Either a b
Left (Text -> Error
InvalidTransactionField Text
"Mint cannot contain Ada")
      | Integer
quantity Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
< Int64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Int64
forall a. Bounded a => a
minBound :: Int64) Bool -> Bool -> Bool
|| Integer
quantity Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Int64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Int64
forall a. Bounded a => a
maxBound :: Int64) = Error -> Either Error ()
forall a b. a -> Either a b
Left (Integer -> Error
InvalidTransactionOutputValue Integer
quantity)
      | Bool
otherwise = () -> Either Error ()
forall a b. b -> Either a b
Right ()

encodeAssets :: Value.Value -> E.Encoding
encodeAssets :: Value -> Encoding
encodeAssets Value
value = Word -> Encoding
E.encodeMapLen (Int -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Map PolicyId [(AssetName, Integer)] -> Int
forall k a. Map k a -> Int
Map.size Map PolicyId [(AssetName, Integer)]
policies)) Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ((PolicyId, [(AssetName, Integer)]) -> Encoding)
-> [(PolicyId, [(AssetName, Integer)])] -> Encoding
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (PolicyId, [(AssetName, Integer)]) -> Encoding
encodePolicy (Map PolicyId [(AssetName, Integer)]
-> [(PolicyId, [(AssetName, Integer)])]
forall k a. Map k a -> [(k, a)]
Map.toAscList Map PolicyId [(AssetName, Integer)]
policies)
  where
    policies :: Map PolicyId [(AssetName, Integer)]
policies = ([(AssetName, Integer)]
 -> [(AssetName, Integer)] -> [(AssetName, Integer)])
-> [(PolicyId, [(AssetName, Integer)])]
-> Map PolicyId [(AssetName, Integer)]
forall k a. Ord k => (a -> a -> a) -> [(k, a)] -> Map k a
Map.fromListWith [(AssetName, Integer)]
-> [(AssetName, Integer)] -> [(AssetName, Integer)]
forall a. [a] -> [a] -> [a]
(++) [(PolicyId
policy, [(AssetName
name, Integer
amount)]) | (Native PolicyId
policy AssetName
name, Integer
amount) <- Value -> [(AssetId, Integer)]
Value.toList Value
value]
    encodePolicy :: (PolicyId, [(AssetName, Integer)]) -> Encoding
encodePolicy (PolicyId
policy, [(AssetName, Integer)]
assets) = ByteString -> Encoding
E.encodeBytes (PolicyId -> ByteString
Policy.toBytes PolicyId
policy) Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word -> Encoding
E.encodeMapLen (Int -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral ([(AssetName, Integer)] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(AssetName, Integer)]
assets)) Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ((AssetName, Integer) -> Encoding)
-> [(AssetName, Integer)] -> Encoding
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (\(AssetName
name, Integer
amount) -> ByteString -> Encoding
E.encodeBytes (AssetName -> ByteString
Name.toBytes AssetName
name) Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Integer -> Encoding
E.encodeInteger Integer
amount) (((AssetName, Integer) -> (Int, ByteString))
-> [(AssetName, Integer)] -> [(AssetName, Integer)]
forall b a. Ord b => (a -> b) -> [a] -> [a]
sortOn (\(AssetName
name, Integer
_) -> (ByteString -> Int
B.length (AssetName -> ByteString
Name.toBytes AssetName
name), AssetName -> ByteString
Name.toBytes AssetName
name)) (Map AssetName Integer -> [(AssetName, Integer)]
forall k a. Map k a -> [(k, a)]
Map.toAscList ([(AssetName, Integer)] -> Map AssetName Integer
forall k a. Ord k => [(k, a)] -> Map k a
Map.fromList [(AssetName, Integer)]
assets)))

decodeAssets :: D.Decoder s Value.Value
decodeAssets :: forall s. Decoder s Value
decodeAssets = do
  [(PolicyId, [(AssetName, Integer)])]
policies <- Decoder s (PolicyId, [(AssetName, Integer)])
-> Decoder s [(PolicyId, [(AssetName, Integer)])]
forall s a. Decoder s a -> Decoder s [a]
Cbor.entries (Decoder s (PolicyId, [(AssetName, Integer)])
 -> Decoder s [(PolicyId, [(AssetName, Integer)])])
-> Decoder s (PolicyId, [(AssetName, Integer)])
-> Decoder s [(PolicyId, [(AssetName, Integer)])]
forall a b. (a -> b) -> a -> b
$ do
    ByteString
bytes <- Decoder s ByteString
forall s. Decoder s ByteString
D.decodeBytes
    PolicyId
policy <- (Error -> Decoder s PolicyId)
-> (PolicyId -> Decoder s PolicyId)
-> Either Error PolicyId
-> Decoder s PolicyId
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> Decoder s PolicyId
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Decoder s PolicyId)
-> (Error -> String) -> Error -> Decoder s PolicyId
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Error -> String
forall a. Show a => a -> String
show) PolicyId -> Decoder s PolicyId
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> Either Error PolicyId
Policy.fromBytes ByteString
bytes)
    [(AssetName, Integer)]
assets <- Decoder s (AssetName, Integer) -> Decoder s [(AssetName, Integer)]
forall s a. Decoder s a -> Decoder s [a]
Cbor.entries (Decoder s (AssetName, Integer)
 -> Decoder s [(AssetName, Integer)])
-> Decoder s (AssetName, Integer)
-> Decoder s [(AssetName, Integer)]
forall a b. (a -> b) -> a -> b
$ do
      ByteString
nameBytes <- Decoder s ByteString
forall s. Decoder s ByteString
D.decodeBytes
      AssetName
name <- (Error -> Decoder s AssetName)
-> (AssetName -> Decoder s AssetName)
-> Either Error AssetName
-> Decoder s AssetName
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> Decoder s AssetName
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Decoder s AssetName)
-> (Error -> String) -> Error -> Decoder s AssetName
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Error -> String
forall a. Show a => a -> String
show) AssetName -> Decoder s AssetName
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> Either Error AssetName
Name.fromBytes ByteString
nameBytes)
      Integer
amount <- Decoder s Integer
forall s. Decoder s Integer
D.decodeInteger
      Bool -> Decoder s () -> Decoder s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Integer
amount Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
>= Int64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Int64
forall a. Bounded a => a
minBound :: Int64) Bool -> Bool -> Bool
&& Integer
amount Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
<= Int64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Int64
forall a. Bounded a => a
maxBound :: Int64)) (String -> Decoder s ()
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Asset quantity exceeds int64")
      (AssetName, Integer) -> Decoder s (AssetName, Integer)
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (AssetName
name, Integer
amount)
    Bool -> Decoder s () -> Decoder s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Set AssetName -> Int
forall a. Set a -> Int
Set.size ([AssetName] -> Set AssetName
forall a. Ord a => [a] -> Set a
Set.fromList (((AssetName, Integer) -> AssetName)
-> [(AssetName, Integer)] -> [AssetName]
forall a b. (a -> b) -> [a] -> [b]
map (AssetName, Integer) -> AssetName
forall a b. (a, b) -> a
fst [(AssetName, Integer)]
assets)) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== [(AssetName, Integer)] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(AssetName, Integer)]
assets) (String -> Decoder s ()
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Duplicate asset name")
    (PolicyId, [(AssetName, Integer)])
-> Decoder s (PolicyId, [(AssetName, Integer)])
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PolicyId
policy, [(AssetName, Integer)]
assets)
  Bool -> Decoder s () -> Decoder s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Set PolicyId -> Int
forall a. Set a -> Int
Set.size ([PolicyId] -> Set PolicyId
forall a. Ord a => [a] -> Set a
Set.fromList (((PolicyId, [(AssetName, Integer)]) -> PolicyId)
-> [(PolicyId, [(AssetName, Integer)])] -> [PolicyId]
forall a b. (a -> b) -> [a] -> [b]
map (PolicyId, [(AssetName, Integer)]) -> PolicyId
forall a b. (a, b) -> a
fst [(PolicyId, [(AssetName, Integer)])]
policies)) Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== [(PolicyId, [(AssetName, Integer)])] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [(PolicyId, [(AssetName, Integer)])]
policies) (String -> Decoder s ()
forall a. String -> Decoder s a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Duplicate policy")
  Value -> Decoder s Value
forall a. a -> Decoder s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([(AssetId, Integer)] -> Value
Value.fromList [(PolicyId -> AssetName -> AssetId
Native PolicyId
policy AssetName
name, Integer
amount) | (PolicyId
policy, [(AssetName, Integer)]
assets) <- [(PolicyId, [(AssetName, Integer)])]
policies, (AssetName
name, Integer
amount) <- [(AssetName, Integer)]
assets])