-- 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 #-}
{-# OPTIONS_HADDOCK hide #-}

-- | Pure Bech32 address encoding. The four fixed HRPs are validated by their
-- literal contents and tests. The sole invariant assertion checks four
-- source-controlled literals using the public package validator; it cannot be
-- reached from external input. No arbitrary HRP enters encoding.
module Cometa.Internal.AddressText (fromText, toText) where

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

import qualified Codec.Binary.Bech32 as Bech32
import Cometa.Error (Error (..))
import Data.ByteString (ByteString)
import Data.Text (Text)
import qualified Data.Text as Text

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

data Prefix = PaymentMainnet | PaymentTestnet | RewardMainnet | RewardTestnet

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

toText :: Bool -> Bool -> ByteString -> Text
toText :: Bool -> Bool -> ByteString -> Text
toText Bool
reward Bool
mainnet = HumanReadablePart -> DataPart -> Text
Bech32.encodeLenient HumanReadablePart
prefix (DataPart -> Text)
-> (ByteString -> DataPart) -> ByteString -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> DataPart
Bech32.dataPartFromBytes
  where
    prefix :: HumanReadablePart
prefix = case (Bool
reward, Bool
mainnet) of
      (Bool
False, Bool
True) -> HumanReadablePart
paymentMainnetPrefix
      (Bool
False, Bool
False) -> HumanReadablePart
paymentTestnetPrefix
      (Bool
True, Bool
True) -> HumanReadablePart
rewardMainnetPrefix
      (Bool
True, Bool
False) -> HumanReadablePart
rewardTestnetPrefix

-- Only these four static literals reach the assertion. Tests validate all four
-- through the public package API and pinned native text vectors.

paymentMainnetPrefix, paymentTestnetPrefix, rewardMainnetPrefix, rewardTestnetPrefix :: Bech32.HumanReadablePart
paymentMainnetPrefix :: HumanReadablePart
paymentMainnetPrefix = Prefix -> HumanReadablePart
builtInPrefix Prefix
PaymentMainnet
paymentTestnetPrefix :: HumanReadablePart
paymentTestnetPrefix = Prefix -> HumanReadablePart
builtInPrefix Prefix
PaymentTestnet
rewardMainnetPrefix :: HumanReadablePart
rewardMainnetPrefix = Prefix -> HumanReadablePart
builtInPrefix Prefix
RewardMainnet
rewardTestnetPrefix :: HumanReadablePart
rewardTestnetPrefix = Prefix -> HumanReadablePart
builtInPrefix Prefix
RewardTestnet

builtInPrefix :: Prefix -> Bech32.HumanReadablePart
builtInPrefix :: Prefix -> HumanReadablePart
builtInPrefix Prefix
prefix = case Text -> Either HumanReadablePartError HumanReadablePart
Bech32.humanReadablePartFromText Text
literal of
  Right HumanReadablePart
value -> HumanReadablePart
value
  Left HumanReadablePartError
_ -> [Char] -> HumanReadablePart
forall a. HasCallStack => [Char] -> a
error [Char]
"Cometa: invalid built-in Bech32 prefix"
  where
    literal :: Text
literal = case Prefix
prefix of
      Prefix
PaymentMainnet -> Text
"addr"
      Prefix
PaymentTestnet -> Text
"addr_test"
      Prefix
RewardMainnet -> Text
"stake"
      Prefix
RewardTestnet -> Text
"stake_test"

fromText :: (ByteString -> Either Error address) -> (address -> Text) -> Text -> Either Error address
fromText :: forall address.
(ByteString -> Either Error address)
-> (address -> Text) -> Text -> Either Error address
fromText ByteString -> Either Error address
parse address -> Text
encode Text
text
  | Text -> Bool
Text.null Text
text Bool -> Bool -> Bool
|| Text -> Int
Text.length Text
text Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
120 Bool -> Bool -> Bool
|| (Char -> Bool) -> Text -> Bool
Text.any (\Char
character -> Char
character Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
< Char
'!' Bool -> Bool -> Bool
|| Char
character Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
> Char
'~') Text
text Bool -> Bool -> Bool
|| (Text -> Text
Text.toLower Text
text Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
/= Text
text Bool -> Bool -> Bool
&& Text -> Text
Text.toUpper Text
text Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
/= Text
text) = Error -> Either Error address
forall a b. a -> Either a b
Left Error
InvalidShelleyAddressText
  | Bool
otherwise = do
      (HumanReadablePart
_, DataPart
payload) <- (DecodingError -> Either Error (HumanReadablePart, DataPart))
-> ((HumanReadablePart, DataPart)
    -> Either Error (HumanReadablePart, DataPart))
-> Either DecodingError (HumanReadablePart, DataPart)
-> Either Error (HumanReadablePart, DataPart)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Either Error (HumanReadablePart, DataPart)
-> DecodingError -> Either Error (HumanReadablePart, DataPart)
forall a b. a -> b -> a
const (Error -> Either Error (HumanReadablePart, DataPart)
forall a b. a -> Either a b
Left Error
InvalidShelleyAddressText)) (HumanReadablePart, DataPart)
-> Either Error (HumanReadablePart, DataPart)
forall a b. b -> Either a b
Right (Text -> Either DecodingError (HumanReadablePart, DataPart)
Bech32.decodeLenient Text
text)
      ByteString
bytes <- Either Error ByteString
-> (ByteString -> Either Error ByteString)
-> Maybe ByteString
-> Either Error ByteString
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Error -> Either Error ByteString
forall a b. a -> Either a b
Left Error
InvalidShelleyAddressText) ByteString -> Either Error ByteString
forall a b. b -> Either a b
Right (DataPart -> Maybe ByteString
Bech32.dataPartToBytes DataPart
payload)
      address
address <- ByteString -> Either Error address
parse ByteString
bytes
      if address -> Text
encode address
address Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
== Text -> Text
Text.toLower Text
text
        then address -> Either Error address
forall a b. b -> Either a b
Right address
address
        else Error -> Either Error address
forall a b. a -> Either a b
Left Error
InvalidShelleyAddressText