{-# LANGUAGE OverloadedStrings #-}
{-# OPTIONS_HADDOCK hide #-}
module Cometa.Internal.AddressText (fromText, toText) where
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
data Prefix = PaymentMainnet | PaymentTestnet | RewardMainnet | RewardTestnet
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
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