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

-- | Off-chain governance metadata locations and hashes. Validate external text
-- and bytes once, then assemble 'Anchor' values with its total constructor.
-- Locations are not fetched or checked for URI syntax.
--
-- @
-- import qualified Cometa.Governance.Anchor as Anchor
--
-- attach :: Anchor.Url -> Anchor.DataHash -> Anchor.Anchor
-- attach = Anchor.Anchor
-- @
module Cometa.Governance.Anchor
  ( Url,
    mkUrl,
    urlText,
    DataHash,
    dataHashFromBytes,
    dataHashBytes,
    hashData,
    Anchor (..),
    encodeCbor,
    decodeCbor,
  )
where

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

import qualified Cometa.Crypto.Blake2b as Blake2b
import Cometa.Error (Error)
import Cometa.Internal.Governance (Anchor (..), DataHash, Url, dataHashBytes, dataHashFromBytes, mkUrl, urlText)
import qualified Cometa.Internal.Governance as Internal
import Data.ByteString (ByteString)

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

-- | Hash the exact off-chain payload with BLAKE2b-256. Empty input returns
-- 'Cometa.Error.NativeError' with the native out-of-bounds-memory-read code,
-- matching the BLAKE2b boundary. Native allocation or hashing failures also return
-- 'Cometa.Error.NativeError'. No JSON parsing or normalization occurs.
hashData :: ByteString -> IO (Either Error DataHash)
hashData :: ByteString -> IO (Either Error DataHash)
hashData ByteString
bytes = do
  Either Error Blake2bHash
result <- Int -> ByteString -> IO (Either Error Blake2bHash)
Blake2b.compute Int
32 ByteString
bytes
  Either Error DataHash -> IO (Either Error DataHash)
forall a. a -> IO a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either Error Blake2bHash
result Either Error Blake2bHash
-> (Blake2bHash -> Either Error DataHash) -> Either Error DataHash
forall a b.
Either Error a -> (a -> Either Error b) -> Either Error b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= ByteString -> Either Error DataHash
dataHashFromBytes (ByteString -> Either Error DataHash)
-> (Blake2bHash -> ByteString)
-> Blake2bHash
-> Either Error DataHash
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Blake2bHash -> ByteString
Blake2b.toBytes)

-- | Fresh definite CBOR @[URL text, hash bytes]@. Re-encoding imported semantic
-- data may change its containing transaction's encoding; preserve the original
-- enclosing CBOR when identity matters.
encodeCbor :: Anchor -> ByteString
encodeCbor :: Anchor -> ByteString
encodeCbor = Anchor -> ByteString
Internal.encodeAnchor

-- | Decode one complete anchor, accepting definite or indefinite arrays and
-- nonminimal lengths. URL validation, the exact 32-byte hash and array arity
-- are enforced; invalid input or trailing bytes returns
-- 'Cometa.Error.InvalidGovernanceCbor'. This returns semantic data, not an
-- encoding-preserving wrapper.
decodeCbor :: ByteString -> Either Error Anchor
decodeCbor :: ByteString -> Either Error Anchor
decodeCbor = ByteString -> Either Error Anchor
Internal.decodeAnchor