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

-- | Transaction identifiers are exact 32-byte transaction-body hashes.
-- Importing an identifier does not hash or verify a transaction.
--
-- @
-- import qualified Cometa.TransactionId as TransactionId
-- import qualified Data.ByteString as ByteString
--
-- example = TransactionId.fromBytes (ByteString.replicate 32 0)
-- @
module Cometa.TransactionId
  ( TransactionId,
    fromBytes,
    toBytes,
    fromHex,
    toHex,
  )
where

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

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 transaction-body hash. Equality and ordering compare exact bytes
-- lexicographically. 'Show' is diagnostic, not an interchange format.
newtype TransactionId = TransactionId ByteString deriving (TransactionId -> TransactionId -> Bool
(TransactionId -> TransactionId -> Bool)
-> (TransactionId -> TransactionId -> Bool) -> Eq TransactionId
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TransactionId -> TransactionId -> Bool
== :: TransactionId -> TransactionId -> Bool
$c/= :: TransactionId -> TransactionId -> Bool
/= :: TransactionId -> TransactionId -> Bool
Eq, Eq TransactionId
Eq TransactionId =>
(TransactionId -> TransactionId -> Ordering)
-> (TransactionId -> TransactionId -> Bool)
-> (TransactionId -> TransactionId -> Bool)
-> (TransactionId -> TransactionId -> Bool)
-> (TransactionId -> TransactionId -> Bool)
-> (TransactionId -> TransactionId -> TransactionId)
-> (TransactionId -> TransactionId -> TransactionId)
-> Ord TransactionId
TransactionId -> TransactionId -> Bool
TransactionId -> TransactionId -> Ordering
TransactionId -> TransactionId -> TransactionId
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 :: TransactionId -> TransactionId -> Ordering
compare :: TransactionId -> TransactionId -> Ordering
$c< :: TransactionId -> TransactionId -> Bool
< :: TransactionId -> TransactionId -> Bool
$c<= :: TransactionId -> TransactionId -> Bool
<= :: TransactionId -> TransactionId -> Bool
$c> :: TransactionId -> TransactionId -> Bool
> :: TransactionId -> TransactionId -> Bool
$c>= :: TransactionId -> TransactionId -> Bool
>= :: TransactionId -> TransactionId -> Bool
$cmax :: TransactionId -> TransactionId -> TransactionId
max :: TransactionId -> TransactionId -> TransactionId
$cmin :: TransactionId -> TransactionId -> TransactionId
min :: TransactionId -> TransactionId -> TransactionId
Ord, Int -> TransactionId -> ShowS
[TransactionId] -> ShowS
TransactionId -> String
(Int -> TransactionId -> ShowS)
-> (TransactionId -> String)
-> ([TransactionId] -> ShowS)
-> Show TransactionId
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TransactionId -> ShowS
showsPrec :: Int -> TransactionId -> ShowS
$cshow :: TransactionId -> String
show :: TransactionId -> String
$cshowList :: [TransactionId] -> ShowS
showList :: [TransactionId] -> ShowS
Show)

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

-- | Import exactly 32 bytes. Other lengths return 'InvalidTransactionIdLength'
-- with the actual byte count.
fromBytes :: ByteString -> Either Error TransactionId
fromBytes :: ByteString -> Either Error TransactionId
fromBytes ByteString
bytes
  | ByteString -> Int
ByteString.length ByteString
bytes Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
32 = TransactionId -> Either Error TransactionId
forall a b. b -> Either a b
Right (ByteString -> TransactionId
TransactionId ByteString
bytes)
  | Bool
otherwise = Error -> Either Error TransactionId
forall a b. a -> Either a b
Left (Int -> Error
InvalidTransactionIdLength (ByteString -> Int
ByteString.length ByteString
bytes))

-- | Exact transaction hash bytes.
toBytes :: TransactionId -> ByteString
toBytes :: TransactionId -> ByteString
toBytes (TransactionId ByteString
bytes) = ByteString
bytes

-- | Import 64 ASCII hexadecimal digits, accepting either case. Invalid syntax
-- returns 'InvalidHex'; another decoded length returns 'InvalidTransactionIdLength'.
fromHex :: Text -> Either Error TransactionId
fromHex :: Text -> Either Error TransactionId
fromHex Text
text = Either Error TransactionId
-> (ByteString -> Either Error TransactionId)
-> Maybe ByteString
-> Either Error TransactionId
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Error -> Either Error TransactionId
forall a b. a -> Either a b
Left Error
InvalidHex) ByteString -> Either Error TransactionId
fromBytes (Text -> Maybe ByteString
Hex.decodeHex Text
text)

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