-- SPDX-FileCopyrightText: 2021 Oxhead Alpha
-- SPDX-License-Identifier: LicenseRef-MIT-OA

-- | Address in Tezos.

{-# LANGUAGE DeriveLift #-}

module Morley.Tezos.Address
  ( ContractHash
  , KindedAddress (..)
  , mkKeyAddress
  , detGenKeyAddress
  , isImplicitAddress
  , ImplicitAddress
  , ContractAddress
  , SmartRollupAddress
  , L1Address
  , L1AddressKind
  , ConstrainAddressKind
  , Address
  , ConstrainedAddress
  , Constrained(.., MkAddress)

  , GlobalCounter(..)
  , mkContractHashHack
  , parseConstrainedAddress

  -- * Formatting
  , ParseAddressError (..)
  , ParseAddressRawError (..)
  , formatAddress
  , mformatAddress
  , parseAddressRaw
  , parseKindedAddress
  , parseAddress
  , ta

  -- * Utilities
  , addressKindSanity
  , usingImplicitOrContractKind
  , unImplicitAddress
  , addressKindTag
  ) where

import Control.Monad.Except (mapExceptT, throwError)
import Data.Aeson (FromJSON(..), FromJSONKey, ToJSON(..), ToJSONKey)
import Data.Aeson qualified as Aeson
import Data.Aeson.Encoding qualified as Aeson
import Data.Aeson.Types qualified as AesonTypes
import Data.Binary.Get qualified as Get
import Data.ByteString qualified as BS
import Data.ByteString.Lazy qualified as LBS
import Data.Char (toUpper)
import Data.Constraint (Bottom(..), Dict(..), (\\))
import Data.Constraint.Extras (has)
import Data.Constraint.Extras.TH (deriveArgDict)
import Data.GADT.Compare.TH (deriveGCompare, deriveGEq)
import Data.List.Singletons (SList(..))
import Data.Singletons (Sing, SingI(..), demote)
import Data.Some (Some(..))
import Data.Text (strip)
import Data.Type.Equality (testEquality, (:~:)(..))
import Fmt (Buildable(build), hexF, pretty)
import Instances.TH.Lift ()
import Language.Haskell.TH.Quote qualified as TH
import Language.Haskell.TH.Syntax (Lift)
import Language.Haskell.TH.Syntax qualified as TH
import Options.Applicative (ReadM)
import Text.PrettyPrint.Leijen.Text (backslash, dquotes, int, (<+>))

import Morley.Michelson.Printer.Util (RenderDoc(..), buildRenderDoc, renderAnyBuildable)
import Morley.Michelson.Text
import Morley.Tezos.Address.Kinds
import Morley.Tezos.Crypto
import Morley.Util.Binary
import Morley.Util.CLI
import Morley.Util.Constrained
import Morley.Util.Sing
import Morley.Util.TH
import Morley.Util.TypeLits

-- | A "kinded" address. This type carries 'AddressKind' on the type-level.
-- Useful in the internal API, not as much when we have to interact with the
-- network. See 'Address' for a type that is isomorphic to a Michelson
-- @address@.
data KindedAddress (kind :: AddressKind) where
  -- | @tz1@, @tz2@ or @tz3@ address which is a hash of a public key.
  ImplicitAddress :: KeyHash -> KindedAddress 'AddressKindImplicit
  -- | @KT1@ address which corresponds to a callable contract.
  ContractAddress :: ContractHash -> KindedAddress 'AddressKindContract
  SmartRollupAddress :: SmartRollupHash -> KindedAddress 'AddressKindSmartRollup

deriving stock instance Show (KindedAddress kind)
deriving stock instance Eq (KindedAddress kind)
deriving stock instance Ord (KindedAddress kind)
deriving stock instance Lift (KindedAddress kind)

deriveGADTNFData ''KindedAddress
deriveGEq ''KindedAddress
deriveGCompare ''KindedAddress
deriveArgDict ''KindedAddress

-- | A type only allowing v'ImplicitAddress'
type ImplicitAddress = KindedAddress 'AddressKindImplicit

-- | A type only allowing v'ContractAddress'
type ContractAddress = KindedAddress 'AddressKindContract

-- | A type only allowing v'SmartRollupAddress'
type SmartRollupAddress = KindedAddress 'AddressKindSmartRollup

-- | Data type corresponding to @address@ structure in Tezos.
type Address = Constrained NullConstraint KindedAddress

-- | 'Constrained' specialized to 'Address'
pattern MkAddress :: KindedAddress kind -> Address
pattern $bMkAddress :: forall (kind :: AddressKind). KindedAddress kind -> Address
$mMkAddress :: forall {r}.
Address
-> (forall {kind :: AddressKind}. KindedAddress kind -> r)
-> (Void# -> r)
-> r
MkAddress x = Constrained x
{-# COMPLETE MkAddress #-}

type family ConstrainAddressKindHelper (ks :: [AddressKind]) kind where
  ConstrainAddressKindHelper (x ': _) x = 'True
  ConstrainAddressKindHelper (_ ': xs) x = ConstrainAddressKindHelper xs x
  ConstrainAddressKindHelper '[] _ = 'False

type family CheckConstrainAddressKindError k b :: Constraint where
  CheckConstrainAddressKindError _ 'True = ()
  CheckConstrainAddressKindError k 'False =
    TypeError ('ShowType k ':<>: 'Text " is forbidden in this context")

-- | Constrain address kind to be one of the kinds in the list.
type ConstrainAddressKind :: [AddressKind] -> AddressKind -> Constraint
class ( CheckConstrainAddressKindError k (ConstrainAddressKindHelper ks k)
  , ConstrainAddressKindHelper ks k ~ 'True) => ConstrainAddressKind ks k
instance ( CheckConstrainAddressKindError k (ConstrainAddressKindHelper ks k)
  , ConstrainAddressKindHelper ks k ~ 'True) => ConstrainAddressKind ks k

-- | An existential of 'KindedAddress' constrained by its type argument.
type ConstrainedAddress (ks :: [AddressKind]) =
  Constrained (ConstrainAddressKind ks) KindedAddress

-- | A convenience synonym for 'ConstrainedAddress' allowing only implicit and
-- contract addresses.
--
-- 'L1Address' is named as such because in addition to implicit and contract
-- addresses, Michelson's @address@ type can contain @txr1@ or @sr1@ addresses,
-- identifying respectively transaction rollups and smart rollups. While they
-- are technically also level-1 (level-2 being @tx_rollup_l2_address@), in
-- practice It's level-1 identifiers for bundles of level-2 operations. Hence,
-- to keep type names concise, we use 'L1Address'.
type L1Address =
  ConstrainedAddress '[ 'AddressKindImplicit, 'AddressKindContract ]

-- | Convenience synonym for 'ConstrainAddressKind' allowing only implicit and
-- contract addresses.
--
-- For a note on the naming convention, refer to 'L1Address'.
type L1AddressKind = ConstrainAddressKind '[ 'AddressKindImplicit, 'AddressKindContract ]

-- | A trick to avoid bogus redundant constraint warnings
usingImplicitOrContractKind :: forall kind a. L1AddressKind kind => a -> a
usingImplicitOrContractKind :: forall (kind :: AddressKind) a. L1AddressKind kind => a -> a
usingImplicitOrContractKind = a -> a
forall a. a -> a
id
  where Dict (L1AddressKind kind)
_ = Dict (L1AddressKind kind)
forall (a :: Constraint). a => Dict a
Dict :: Dict (L1AddressKind kind)

-- | Given any (non-bottom) 'KindedAddress', prove that @kind@ is well-defined
-- (i.e. has a 'SingI' instance)
addressKindSanity :: KindedAddress kind -> Dict (SingI kind)
addressKindSanity :: forall (kind :: AddressKind).
KindedAddress kind -> Dict (SingI kind)
addressKindSanity KindedAddress kind
a = forall {k} (c :: k -> Constraint) (f :: k -> *) (a :: k) r.
Has c f =>
f a -> (c a => r) -> r
forall (c :: AddressKind -> Constraint) (f :: AddressKind -> *)
       (a :: AddressKind) r.
Has c f =>
f a -> (c a => r) -> r
has @SingI KindedAddress kind
a SingI kind => Dict (SingI kind)
forall (a :: Constraint). a => Dict a
Dict

-- | Checks if the provided 'KindedAddress' is an implicit address and returns
-- proof of the fact if it is.
isImplicitAddress :: KindedAddress kind -> Maybe (kind :~: 'AddressKindImplicit)
isImplicitAddress :: forall (kind :: AddressKind).
KindedAddress kind -> Maybe (kind :~: 'AddressKindImplicit)
isImplicitAddress = \case
  ImplicitAddress{} -> (kind :~: kind) -> Maybe (kind :~: kind)
forall a. a -> Maybe a
Just kind :~: kind
forall {k} (a :: k). a :~: a
Refl
  KindedAddress kind
_ -> Maybe (kind :~: 'AddressKindImplicit)
forall a. Maybe a
Nothing

-- | Smart constructor for t'ImplicitAddress'.
mkKeyAddress :: PublicKey -> ImplicitAddress
mkKeyAddress :: PublicKey -> ImplicitAddress
mkKeyAddress = KeyHash -> ImplicitAddress
ImplicitAddress (KeyHash -> ImplicitAddress)
-> (PublicKey -> KeyHash) -> PublicKey -> ImplicitAddress
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PublicKey -> KeyHash
hashKey

unImplicitAddress :: ImplicitAddress -> KeyHash
unImplicitAddress :: ImplicitAddress -> KeyHash
unImplicitAddress (ImplicitAddress KeyHash
kh) = KeyHash
kh

-- | Deterministically generate a random t'ImplicitAddress' and discard its
-- secret key.
detGenKeyAddress :: ByteString -> ImplicitAddress
detGenKeyAddress :: ByteString -> ImplicitAddress
detGenKeyAddress = PublicKey -> ImplicitAddress
mkKeyAddress (PublicKey -> ImplicitAddress)
-> (ByteString -> PublicKey) -> ByteString -> ImplicitAddress
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SecretKey -> PublicKey
toPublic (SecretKey -> PublicKey)
-> (ByteString -> SecretKey) -> ByteString -> PublicKey
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> SecretKey
detSecretKey

-- | Represents the network's global counter.
--
-- We store the current value of this counter in the operation at the time of its creation
-- for the following reasons:
-- * to guarantee the uniqueness of contract addresses upon origination
--   (see 'Morley.Michelson.Typed.Operation.mkContractAddress)
-- * to prevent replay attacks by checking that an operation with the same counter value
--   con't be performed twice.
--
-- The counter is incremented after every operation execution and interpretation of instructions
-- @CREATE_CONTRACT@ and @TRANSFER_TOKENS@, and thus ensures that these addresses are unique
-- (i.e. origination of identical contracts with identical metadata will result in
-- different addresses.)
--
-- Our counter is represented as 'Word64', while in Tezos it is unbounded. We believe that
-- for our interpreter it should not matter.
newtype GlobalCounter = GlobalCounter { GlobalCounter -> Word64
unGlobalCounter :: Word64 }
  deriving stock (Int -> GlobalCounter -> ShowS
[GlobalCounter] -> ShowS
GlobalCounter -> String
(Int -> GlobalCounter -> ShowS)
-> (GlobalCounter -> String)
-> ([GlobalCounter] -> ShowS)
-> Show GlobalCounter
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [GlobalCounter] -> ShowS
$cshowList :: [GlobalCounter] -> ShowS
show :: GlobalCounter -> String
$cshow :: GlobalCounter -> String
showsPrec :: Int -> GlobalCounter -> ShowS
$cshowsPrec :: Int -> GlobalCounter -> ShowS
Show, GlobalCounter -> GlobalCounter -> Bool
(GlobalCounter -> GlobalCounter -> Bool)
-> (GlobalCounter -> GlobalCounter -> Bool) -> Eq GlobalCounter
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: GlobalCounter -> GlobalCounter -> Bool
$c/= :: GlobalCounter -> GlobalCounter -> Bool
== :: GlobalCounter -> GlobalCounter -> Bool
$c== :: GlobalCounter -> GlobalCounter -> Bool
Eq, (forall x. GlobalCounter -> Rep GlobalCounter x)
-> (forall x. Rep GlobalCounter x -> GlobalCounter)
-> Generic GlobalCounter
forall x. Rep GlobalCounter x -> GlobalCounter
forall x. GlobalCounter -> Rep GlobalCounter x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep GlobalCounter x -> GlobalCounter
$cfrom :: forall x. GlobalCounter -> Rep GlobalCounter x
Generic)
  deriving anyclass (GlobalCounter -> ()
(GlobalCounter -> ()) -> NFData GlobalCounter
forall a. (a -> ()) -> NFData a
rnf :: GlobalCounter -> ()
$crnf :: GlobalCounter -> ()
NFData)
  deriving newtype ([GlobalCounter] -> Encoding
[GlobalCounter] -> Value
GlobalCounter -> Encoding
GlobalCounter -> Value
(GlobalCounter -> Value)
-> (GlobalCounter -> Encoding)
-> ([GlobalCounter] -> Value)
-> ([GlobalCounter] -> Encoding)
-> ToJSON GlobalCounter
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> ToJSON a
toEncodingList :: [GlobalCounter] -> Encoding
$ctoEncodingList :: [GlobalCounter] -> Encoding
toJSONList :: [GlobalCounter] -> Value
$ctoJSONList :: [GlobalCounter] -> Value
toEncoding :: GlobalCounter -> Encoding
$ctoEncoding :: GlobalCounter -> Encoding
toJSON :: GlobalCounter -> Value
$ctoJSON :: GlobalCounter -> Value
ToJSON, Value -> Parser [GlobalCounter]
Value -> Parser GlobalCounter
(Value -> Parser GlobalCounter)
-> (Value -> Parser [GlobalCounter]) -> FromJSON GlobalCounter
forall a.
(Value -> Parser a) -> (Value -> Parser [a]) -> FromJSON a
parseJSONList :: Value -> Parser [GlobalCounter]
$cparseJSONList :: Value -> Parser [GlobalCounter]
parseJSON :: Value -> Parser GlobalCounter
$cparseJSON :: Value -> Parser GlobalCounter
FromJSON, Integer -> GlobalCounter
GlobalCounter -> GlobalCounter
GlobalCounter -> GlobalCounter -> GlobalCounter
(GlobalCounter -> GlobalCounter -> GlobalCounter)
-> (GlobalCounter -> GlobalCounter -> GlobalCounter)
-> (GlobalCounter -> GlobalCounter -> GlobalCounter)
-> (GlobalCounter -> GlobalCounter)
-> (GlobalCounter -> GlobalCounter)
-> (GlobalCounter -> GlobalCounter)
-> (Integer -> GlobalCounter)
-> Num GlobalCounter
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
fromInteger :: Integer -> GlobalCounter
$cfromInteger :: Integer -> GlobalCounter
signum :: GlobalCounter -> GlobalCounter
$csignum :: GlobalCounter -> GlobalCounter
abs :: GlobalCounter -> GlobalCounter
$cabs :: GlobalCounter -> GlobalCounter
negate :: GlobalCounter -> GlobalCounter
$cnegate :: GlobalCounter -> GlobalCounter
* :: GlobalCounter -> GlobalCounter -> GlobalCounter
$c* :: GlobalCounter -> GlobalCounter -> GlobalCounter
- :: GlobalCounter -> GlobalCounter -> GlobalCounter
$c- :: GlobalCounter -> GlobalCounter -> GlobalCounter
+ :: GlobalCounter -> GlobalCounter -> GlobalCounter
$c+ :: GlobalCounter -> GlobalCounter -> GlobalCounter
Num, GlobalCounter -> Builder
(GlobalCounter -> Builder) -> Buildable GlobalCounter
forall p. (p -> Builder) -> Buildable p
build :: GlobalCounter -> Builder
$cbuild :: GlobalCounter -> Builder
Buildable, Int -> GlobalCounter -> Int
GlobalCounter -> Int
(Int -> GlobalCounter -> Int)
-> (GlobalCounter -> Int) -> Hashable GlobalCounter
forall a. (Int -> a -> Int) -> (a -> Int) -> Hashable a
hash :: GlobalCounter -> Int
$chash :: GlobalCounter -> Int
hashWithSalt :: Int -> GlobalCounter -> Int
$chashWithSalt :: Int -> GlobalCounter -> Int
Hashable)

-- | Create a dummy 'ContractHash' value by hashing given 'ByteString'.
--
-- Use in tests **only**.
mkContractHashHack :: ByteString -> ContractHash
mkContractHashHack :: ByteString -> ContractHash
mkContractHashHack = HashTag 'HashKindContract -> ByteString -> ContractHash
forall (kind :: HashKind). HashTag kind -> ByteString -> Hash kind
Hash HashTag 'HashKindContract
HashContract (ByteString -> ContractHash)
-> (ByteString -> ByteString) -> ByteString -> ContractHash
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
blake2b160

----------------------------------------------------------------------------
-- Formatting/parsing
----------------------------------------------------------------------------

formatAddress :: KindedAddress kind -> Text
formatAddress :: forall (kind :: AddressKind). KindedAddress kind -> Text
formatAddress =
  \case
    ImplicitAddress KeyHash
h -> KeyHash -> Text
forall (kind :: HashKind). Hash kind -> Text
formatHash KeyHash
h
    ContractAddress ContractHash
h -> ContractHash -> Text
forall (kind :: HashKind). Hash kind -> Text
formatHash ContractHash
h
    SmartRollupAddress SmartRollupHash
h -> SmartRollupHash -> Text
forall (kind :: HashKind). Hash kind -> Text
formatHash SmartRollupHash
h

mformatAddress :: KindedAddress kind -> MText
mformatAddress :: forall (kind :: AddressKind). KindedAddress kind -> MText
mformatAddress = Either Text MText -> MText
forall a b. (HasCallStack, Buildable a) => Either a b -> b
unsafe (Either Text MText -> MText)
-> (KindedAddress kind -> Either Text MText)
-> KindedAddress kind
-> MText
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Either Text MText
mkMText (Text -> Either Text MText)
-> (KindedAddress kind -> Text)
-> KindedAddress kind
-> Either Text MText
forall b c a. (b -> c) -> (a -> b) -> a -> c
. KindedAddress kind -> Text
forall (kind :: AddressKind). KindedAddress kind -> Text
formatAddress

instance Buildable (KindedAddress kind) where
  build :: KindedAddress kind -> Builder
build = Text -> Builder
forall p. Buildable p => p -> Builder
build (Text -> Builder)
-> (KindedAddress kind -> Text) -> KindedAddress kind -> Builder
forall b c a. (b -> c) -> (a -> b) -> a -> c
. KindedAddress kind -> Text
forall (kind :: AddressKind). KindedAddress kind -> Text
formatAddress

-- | Errors that can happen during address parsing.
data ParseAddressError
  = ParseAddressCryptoError CryptoParseError
  -- ^ The address parsers failed with some error.
  | ParseAddressWrongKind [AddressKind] Address
  -- ^ The parsed address is of wrong kind
  deriving stock (Int -> ParseAddressError -> ShowS
[ParseAddressError] -> ShowS
ParseAddressError -> String
(Int -> ParseAddressError -> ShowS)
-> (ParseAddressError -> String)
-> ([ParseAddressError] -> ShowS)
-> Show ParseAddressError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ParseAddressError] -> ShowS
$cshowList :: [ParseAddressError] -> ShowS
show :: ParseAddressError -> String
$cshow :: ParseAddressError -> String
showsPrec :: Int -> ParseAddressError -> ShowS
$cshowsPrec :: Int -> ParseAddressError -> ShowS
Show, ParseAddressError -> ParseAddressError -> Bool
(ParseAddressError -> ParseAddressError -> Bool)
-> (ParseAddressError -> ParseAddressError -> Bool)
-> Eq ParseAddressError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ParseAddressError -> ParseAddressError -> Bool
$c/= :: ParseAddressError -> ParseAddressError -> Bool
== :: ParseAddressError -> ParseAddressError -> Bool
$c== :: ParseAddressError -> ParseAddressError -> Bool
Eq, (forall x. ParseAddressError -> Rep ParseAddressError x)
-> (forall x. Rep ParseAddressError x -> ParseAddressError)
-> Generic ParseAddressError
forall x. Rep ParseAddressError x -> ParseAddressError
forall x. ParseAddressError -> Rep ParseAddressError x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep ParseAddressError x -> ParseAddressError
$cfrom :: forall x. ParseAddressError -> Rep ParseAddressError x
Generic)

instance NFData ParseAddressError

instance Buildable ParseAddressError where
  build :: ParseAddressError -> Builder
build = ParseAddressError -> Builder
forall a. RenderDoc a => a -> Builder
buildRenderDoc

instance RenderDoc ParseAddressError where
  renderDoc :: RenderContext -> ParseAddressError -> Doc
renderDoc RenderContext
context =
    \case
      ParseAddressCryptoError CryptoParseError
pkErr -> Doc
"Address failed to parse: " Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> RenderContext -> CryptoParseError -> Doc
forall a. RenderDoc a => RenderContext -> a -> Doc
renderDoc RenderContext
context CryptoParseError
pkErr
      ParseAddressWrongKind [AddressKind]
expected (Constrained KindedAddress a
a) -> [Doc] -> Doc
forall a. Monoid a => [a] -> a
mconcat
        [ Doc
"Expected address of kind ", [AddressKind] -> Doc
forall {a}. Buildable a => [a] -> Doc
renderAddressKinds [AddressKind]
expected
        , Doc
", but got ", KindedAddress a -> Doc
forall a. Buildable a => a -> Doc
renderAnyBuildable KindedAddress a
a
        ]
    where
      renderAddressKinds :: [a] -> Doc
renderAddressKinds [a]
as = [Doc] -> Doc
forall a. Monoid a => [a] -> a
mconcat ([Doc] -> Doc) -> [Doc] -> Doc
forall a b. (a -> b) -> a -> b
$ Doc -> [Doc] -> [Doc]
forall a. a -> [a] -> [a]
intersperse Doc
", " (a -> Doc
forall a. Buildable a => a -> Doc
renderAnyBuildable (a -> Doc) -> [a] -> [Doc]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [a]
as)

-- | Parse an address of a particular kind from its human-readable textual
-- representation used by Tezos (e. g. "tz1faswCTDciRzE4oJ9jn2Vm2dvjeyA9fUzU").
-- Or fail if it's invalid.
parseKindedAddress
  :: forall kind. SingI kind
  => Text -> Either ParseAddressError (KindedAddress kind)
parseKindedAddress :: forall (kind :: AddressKind).
SingI kind =>
Text -> Either ParseAddressError (KindedAddress kind)
parseKindedAddress Text
addressText = do
  Constrained KindedAddress a
a <- forall (kinds :: [AddressKind]).
SingI kinds =>
Text -> Either ParseAddressError (ConstrainedAddress kinds)
parseConstrainedAddress @'[kind] Text
addressText
  KindedAddress a -> Maybe (KindedAddress kind)
forall {k} (a :: k) (b :: k) (t :: k -> *).
(SingI a, SingI b, SDecide k) =>
t a -> Maybe (t b)
castSing KindedAddress a
a (SingI a => Maybe (KindedAddress kind))
-> Dict (SingI a) -> Maybe (KindedAddress kind)
forall (c :: Constraint) e r. HasDict c e => (c => r) -> e -> r
\\ KindedAddress a -> Dict (SingI a)
forall (kind :: AddressKind).
KindedAddress kind -> Dict (SingI kind)
addressKindSanity KindedAddress a
a Maybe (KindedAddress kind)
-> (Maybe (KindedAddress kind)
    -> Either ParseAddressError (KindedAddress kind))
-> Either ParseAddressError (KindedAddress kind)
forall a b. a -> (a -> b) -> b
&
    ParseAddressError
-> Maybe (KindedAddress kind)
-> Either ParseAddressError (KindedAddress kind)
forall l r. l -> Maybe r -> Either l r
maybeToRight ([AddressKind] -> Address -> ParseAddressError
ParseAddressWrongKind [forall {k} (a :: k). (SingKind k, SingI a) => Demote k
forall (a :: AddressKind).
(SingKind AddressKind, SingI a) =>
Demote AddressKind
demote @kind] (Address -> ParseAddressError) -> Address -> ParseAddressError
forall a b. (a -> b) -> a -> b
$ KindedAddress a -> Address
forall {k} (c :: k -> Constraint) (f :: k -> *) (a :: k).
c a =>
f a -> Constrained c f
Constrained KindedAddress a
a)

-- | Parse an 'ConstrainedAddress' of the given kinds from its human-readable textual
--  representation. Maybe fail with a 'ParseAddressWrongKind' in case the address parsed
--  is of wrong kind.
parseConstrainedAddress
  :: forall kinds . (SingI kinds)
  => Text -> Either ParseAddressError (ConstrainedAddress kinds)
parseConstrainedAddress :: forall (kinds :: [AddressKind]).
SingI kinds =>
Text -> Either ParseAddressError (ConstrainedAddress kinds)
parseConstrainedAddress Text
addressText =
  Text -> Either ParseAddressError Address
parseAddress Text
addressText Either ParseAddressError Address
-> (Address -> Either ParseAddressError (ConstrainedAddress kinds))
-> Either ParseAddressError (ConstrainedAddress kinds)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [AddressKind]
-> SList kinds
-> Address
-> Either ParseAddressError (ConstrainedAddress kinds)
forall (kinds :: [AddressKind]).
[AddressKind]
-> SList kinds
-> Address
-> Either ParseAddressError (ConstrainedAddress kinds)
castConstrainedAddress (forall (a :: [AddressKind]).
(SingKind [AddressKind], SingI a) =>
Demote [AddressKind]
forall {k} (a :: k). (SingKind k, SingI a) => Demote k
demote @kinds) (forall (a :: [AddressKind]). SingI a => Sing a
forall {k} (a :: k). SingI a => Sing a
sing @kinds)

castConstrainedAddress
  :: [AddressKind]
  -> SList kinds
  -> Address
  -> Either ParseAddressError (ConstrainedAddress kinds)
castConstrainedAddress :: forall (kinds :: [AddressKind]).
[AddressKind]
-> SList kinds
-> Address
-> Either ParseAddressError (ConstrainedAddress kinds)
castConstrainedAddress [AddressKind]
allowed = \case
  SList kinds
SNil -> ParseAddressError
-> Either ParseAddressError (ConstrainedAddress kinds)
forall a b. a -> Either a b
Left (ParseAddressError
 -> Either ParseAddressError (ConstrainedAddress kinds))
-> (Address -> ParseAddressError)
-> Address
-> Either ParseAddressError (ConstrainedAddress kinds)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [AddressKind] -> Address -> ParseAddressError
ParseAddressWrongKind [AddressKind]
allowed
  SCons Sing n1
kind Sing n2
ks -> \case
    Constrained (KindedAddress a
a :: KindedAddress kind')
      | Just n1 :~: a
Refl <- SingAddressKind n1 -> SingAddressKind a -> Maybe (n1 :~: a)
forall {k} (f :: k -> *) (a :: k) (b :: k).
TestEquality f =>
f a -> f b -> Maybe (a :~: b)
testEquality Sing n1
SingAddressKind n1
kind (forall {k} (a :: k). SingI a => Sing a
forall (a :: AddressKind). SingI a => Sing a
sing @kind') (SingI a => Maybe (n1 :~: a)) -> Dict (SingI a) -> Maybe (n1 :~: a)
forall (c :: Constraint) e r. HasDict c e => (c => r) -> e -> r
\\ KindedAddress a -> Dict (SingI a)
forall (kind :: AddressKind).
KindedAddress kind -> Dict (SingI kind)
addressKindSanity KindedAddress a
a
      -> ConstrainedAddress kinds
-> Either ParseAddressError (ConstrainedAddress kinds)
forall a b. b -> Either a b
Right (KindedAddress a -> ConstrainedAddress kinds
forall {k} (c :: k -> Constraint) (f :: k -> *) (a :: k).
c a =>
f a -> Constrained c f
Constrained KindedAddress a
a)
    Address
a -> Sing n1 -> ConstrainedAddress n2 -> ConstrainedAddress (n1 : n2)
forall (xs :: [AddressKind]) (x :: AddressKind).
Sing x -> ConstrainedAddress xs -> ConstrainedAddress (x : xs)
recastAddress Sing n1
kind (ConstrainedAddress n2 -> ConstrainedAddress (n1 : n2))
-> Either ParseAddressError (ConstrainedAddress n2)
-> Either ParseAddressError (ConstrainedAddress (n1 : n2))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [AddressKind]
-> SList n2
-> Address
-> Either ParseAddressError (ConstrainedAddress n2)
forall (kinds :: [AddressKind]).
[AddressKind]
-> SList kinds
-> Address
-> Either ParseAddressError (ConstrainedAddress kinds)
castConstrainedAddress [AddressKind]
allowed Sing n2
SList n2
ks Address
a

recastAddress
  :: forall xs x. Sing x -> ConstrainedAddress xs -> ConstrainedAddress (x ': xs)
recastAddress :: forall (xs :: [AddressKind]) (x :: AddressKind).
Sing x -> ConstrainedAddress xs -> ConstrainedAddress (x : xs)
recastAddress Sing x
sx (Constrained (KindedAddress a
x :: KindedAddress k)) =
  KindedAddress a -> ConstrainedAddress (x : xs)
forall {k} (c :: k -> Constraint) (f :: k -> *) (a :: k).
c a =>
f a -> Constrained c f
Constrained KindedAddress a
x (ConstrainAddressKind (x : xs) a => ConstrainedAddress (x : xs))
-> Dict (ConstrainAddressKind (x : xs) a)
-> ConstrainedAddress (x : xs)
forall (c :: Constraint) e r. HasDict c e => (c => r) -> e -> r
\\ forall (ks :: [AddressKind]) (k :: AddressKind) (x :: AddressKind).
ConstrainAddressKind ks k =>
Sing k -> Sing x -> Dict (ConstrainAddressKind (x : ks) k)
proofAddressCast @xs (forall {k} (a :: k). SingI a => Sing a
forall (a :: AddressKind). SingI a => Sing a
sing @k) Sing x
sx (SingI a => ConstrainedAddress (x : xs))
-> Dict (SingI a) -> ConstrainedAddress (x : xs)
forall (c :: Constraint) e r. HasDict c e => (c => r) -> e -> r
\\ KindedAddress a -> Dict (SingI a)
forall (kind :: AddressKind).
KindedAddress kind -> Dict (SingI kind)
addressKindSanity KindedAddress a
x

proofAddressCast
  :: forall ks k x. ConstrainAddressKind ks k
  => Sing k -> Sing x -> Dict (ConstrainAddressKind (x ': ks) k)
proofAddressCast :: forall (ks :: [AddressKind]) (k :: AddressKind) (x :: AddressKind).
ConstrainAddressKind ks k =>
Sing k -> Sing x -> Dict (ConstrainAddressKind (x : ks) k)
proofAddressCast = $(forEachAddressKind $ forEachAddressKind [|Dict|])

-- | Parse an address of arbitrary kind from its human-readable textual
-- representation, or fail if it's invalid.
parseAddress :: Text -> Either ParseAddressError Address
parseAddress :: Text -> Either ParseAddressError Address
parseAddress Text
a = (CryptoParseError -> ParseAddressError)
-> Either CryptoParseError Address
-> Either ParseAddressError Address
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first CryptoParseError -> ParseAddressError
ParseAddressCryptoError (Either CryptoParseError Address
 -> Either ParseAddressError Address)
-> Either CryptoParseError Address
-> Either ParseAddressError Address
forall a b. (a -> b) -> a -> b
$ Text -> Either CryptoParseError (Some Hash)
parseSomeHashBase58 Text
a Either CryptoParseError (Some Hash)
-> (Some Hash -> Address) -> Either CryptoParseError Address
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
  Some h :: Hash a
h@(Hash HashTag a
hk ByteString
_) -> case HashTag a
hk of
    HashKey{}     -> ImplicitAddress -> Address
forall {k} (c :: k -> Constraint) (f :: k -> *) (a :: k).
c a =>
f a -> Constrained c f
Constrained (ImplicitAddress -> Address) -> ImplicitAddress -> Address
forall a b. (a -> b) -> a -> b
$ KeyHash -> ImplicitAddress
ImplicitAddress Hash a
KeyHash
h
    HashTag a
HashContract  -> KindedAddress 'AddressKindContract -> Address
forall {k} (c :: k -> Constraint) (f :: k -> *) (a :: k).
c a =>
f a -> Constrained c f
Constrained (KindedAddress 'AddressKindContract -> Address)
-> KindedAddress 'AddressKindContract -> Address
forall a b. (a -> b) -> a -> b
$ ContractHash -> KindedAddress 'AddressKindContract
ContractAddress Hash a
ContractHash
h
    HashTag a
HashSR        -> KindedAddress 'AddressKindSmartRollup -> Address
forall {k} (c :: k -> Constraint) (f :: k -> *) (a :: k).
c a =>
f a -> Constrained c f
Constrained (KindedAddress 'AddressKindSmartRollup -> Address)
-> KindedAddress 'AddressKindSmartRollup -> Address
forall a b. (a -> b) -> a -> b
$ SmartRollupHash -> KindedAddress 'AddressKindSmartRollup
SmartRollupAddress Hash a
SmartRollupHash
h

data ParseAddressRawError
  = ParseAddressRawWrongSize ByteString
  -- ^ Raw bytes representation of an address has invalid length.
  | ParseAddressRawInvalidPrefix Word8
  -- ^ Raw bytes representation of an address has incorrect prefix.
  | ParseAddressRawUnsupportedPrefix Text Word8
  -- ^ Unsupported address type.
  | ParseAddressRawMalformedSeparator Word8
  -- ^ Raw bytes representation of an address does not end with "\00".
  | ParseAddressRawBinaryError Text
  -- ^ General binary decoding error.
  | ParseAddressRawCryptoError CryptoParseError
  -- ^ Crypto error in parsing key hash.
  deriving stock (ParseAddressRawError -> ParseAddressRawError -> Bool
(ParseAddressRawError -> ParseAddressRawError -> Bool)
-> (ParseAddressRawError -> ParseAddressRawError -> Bool)
-> Eq ParseAddressRawError
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ParseAddressRawError -> ParseAddressRawError -> Bool
$c/= :: ParseAddressRawError -> ParseAddressRawError -> Bool
== :: ParseAddressRawError -> ParseAddressRawError -> Bool
$c== :: ParseAddressRawError -> ParseAddressRawError -> Bool
Eq, Int -> ParseAddressRawError -> ShowS
[ParseAddressRawError] -> ShowS
ParseAddressRawError -> String
(Int -> ParseAddressRawError -> ShowS)
-> (ParseAddressRawError -> String)
-> ([ParseAddressRawError] -> ShowS)
-> Show ParseAddressRawError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ParseAddressRawError] -> ShowS
$cshowList :: [ParseAddressRawError] -> ShowS
show :: ParseAddressRawError -> String
$cshow :: ParseAddressRawError -> String
showsPrec :: Int -> ParseAddressRawError -> ShowS
$cshowsPrec :: Int -> ParseAddressRawError -> ShowS
Show, (forall x. ParseAddressRawError -> Rep ParseAddressRawError x)
-> (forall x. Rep ParseAddressRawError x -> ParseAddressRawError)
-> Generic ParseAddressRawError
forall x. Rep ParseAddressRawError x -> ParseAddressRawError
forall x. ParseAddressRawError -> Rep ParseAddressRawError x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep ParseAddressRawError x -> ParseAddressRawError
$cfrom :: forall x. ParseAddressRawError -> Rep ParseAddressRawError x
Generic)

instance NFData ParseAddressRawError

instance RenderDoc ParseAddressRawError where
  renderDoc :: RenderContext -> ParseAddressRawError -> Doc
renderDoc RenderContext
_ =
    \case
      ParseAddressRawInvalidPrefix Word8
prefix ->
        Doc
"Invalid prefix for raw address" Doc -> Doc -> Doc
<+> (Doc -> Doc
dquotes (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ Builder -> Doc
forall a. Buildable a => a -> Doc
renderAnyBuildable (Builder -> Doc) -> Builder -> Doc
forall a b. (a -> b) -> a -> b
$ Word8 -> Builder
forall a. FormatAsHex a => a -> Builder
hexF Word8
prefix) Doc -> Doc -> Doc
<+> Doc
"provided"
      ParseAddressRawUnsupportedPrefix Text
name Word8
prefix ->
        Doc
"Unsupported raw address prefix type" Doc -> Doc -> Doc
<+> Text -> Doc
forall a. Buildable a => a -> Doc
renderAnyBuildable Text
name
          Doc -> Doc -> Doc
<+> (Doc -> Doc
dquotes (Doc -> Doc) -> Doc -> Doc
forall a b. (a -> b) -> a -> b
$ Builder -> Doc
forall a. Buildable a => a -> Doc
renderAnyBuildable (Builder -> Doc) -> Builder -> Doc
forall a b. (a -> b) -> a -> b
$ Word8 -> Builder
forall a. FormatAsHex a => a -> Builder
hexF Word8
prefix) Doc -> Doc -> Doc
<+> Doc
"found"
      ParseAddressRawWrongSize ByteString
addr -> Doc
"Given raw address+" Doc -> Doc -> Doc
<+>
        (Builder -> Doc
forall a. Buildable a => a -> Doc
renderAnyBuildable (Builder -> Doc) -> Builder -> Doc
forall a b. (a -> b) -> a -> b
$ ByteString -> Builder
forall a. FormatAsHex a => a -> Builder
hexF ByteString
addr) Doc -> Doc -> Doc
<+> Doc
"has invalid length" Doc -> Doc -> Doc
<+> Int -> Doc
int (ByteString -> Int
forall t. Container t => t -> Int
length ByteString
addr)
      ParseAddressRawMalformedSeparator Word8
addr -> Doc
"Given raw address" Doc -> Doc -> Doc
<+> (Builder -> Doc
forall a. Buildable a => a -> Doc
renderAnyBuildable (Builder -> Doc) -> Builder -> Doc
forall a b. (a -> b) -> a -> b
$ Word8 -> Builder
forall a. FormatAsHex a => a -> Builder
hexF Word8
addr) Doc -> Doc -> Doc
<+>
        Doc
"does not end with" Doc -> Doc -> Doc
<+> Doc -> Doc
dquotes (Doc
backslash Doc -> Doc -> Doc
forall a. Semigroup a => a -> a -> a
<> Doc
"00")
      ParseAddressRawBinaryError Text
err -> Doc
"Binary error during decoding address:" Doc -> Doc -> Doc
<+> Text -> Doc
forall a. Buildable a => a -> Doc
renderAnyBuildable Text
err
      ParseAddressRawCryptoError CryptoParseError
err -> Doc
"Key hash decoding error:" Doc -> Doc -> Doc
<+> CryptoParseError -> Doc
forall a. Buildable a => a -> Doc
renderAnyBuildable CryptoParseError
err

instance Buildable ParseAddressRawError where
  build :: ParseAddressRawError -> Builder
build = ParseAddressRawError -> Builder
forall a. RenderDoc a => a -> Builder
buildRenderDoc

-- | Parse the given address in its raw byte form used by Tezos
-- (e.g "01521139f84791537d54575df0c74a8084cc68861c00")) . Or fail otherwise
-- if it's invalid.
parseAddressRaw :: ByteString -> Either ParseAddressRawError Address
parseAddressRaw :: ByteString -> Either ParseAddressRawError Address
parseAddressRaw ByteString
bytes
  -- NB: conveniently, the byte count is the same for 'KeyAddress',
  -- 'ContractAddress' and 'TransactionRollupAddress'. However, with
  -- 'KeyAddress' it's two tag bytes, while with the other two it's one tag byte
  -- and one separator byte.
  | ByteString -> Int
BS.length ByteString
bytes Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
/= Int
forall n. Integral n => n
hashLengthBytes Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2 = ParseAddressRawError -> Either ParseAddressRawError Address
forall a b. a -> Either a b
Left (ParseAddressRawError -> Either ParseAddressRawError Address)
-> ParseAddressRawError -> Either ParseAddressRawError Address
forall a b. (a -> b) -> a -> b
$ ByteString -> ParseAddressRawError
ParseAddressRawWrongSize ByteString
bytes
  | Bool
otherwise
  = ((ByteString, ByteOffset, String)
 -> Either ParseAddressRawError Address)
-> ((ByteString, ByteOffset, Either ParseAddressRawError Address)
    -> Either ParseAddressRawError Address)
-> Either
     (ByteString, ByteOffset, String)
     (ByteString, ByteOffset, Either ParseAddressRawError Address)
-> Either ParseAddressRawError Address
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (ParseAddressRawError -> Either ParseAddressRawError Address
forall a b. a -> Either a b
Left (ParseAddressRawError -> Either ParseAddressRawError Address)
-> ((ByteString, ByteOffset, String) -> ParseAddressRawError)
-> (ByteString, ByteOffset, String)
-> Either ParseAddressRawError Address
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> ParseAddressRawError
ParseAddressRawBinaryError (Text -> ParseAddressRawError)
-> ((ByteString, ByteOffset, String) -> Text)
-> (ByteString, ByteOffset, String)
-> ParseAddressRawError
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
forall a. IsString a => String -> a
fromString (String -> Text)
-> ((ByteString, ByteOffset, String) -> String)
-> (ByteString, ByteOffset, String)
-> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Getting String (ByteString, ByteOffset, String) String
-> (ByteString, ByteOffset, String) -> String
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting String (ByteString, ByteOffset, String) String
forall s t a b. Field3 s t a b => Lens s t a b
_3) (Getting
  (Either ParseAddressRawError Address)
  (ByteString, ByteOffset, Either ParseAddressRawError Address)
  (Either ParseAddressRawError Address)
-> (ByteString, ByteOffset, Either ParseAddressRawError Address)
-> Either ParseAddressRawError Address
forall s (m :: * -> *) a. MonadReader s m => Getting a s a -> m a
view Getting
  (Either ParseAddressRawError Address)
  (ByteString, ByteOffset, Either ParseAddressRawError Address)
  (Either ParseAddressRawError Address)
forall s t a b. Field3 s t a b => Lens s t a b
_3)
  (Either
   (ByteString, ByteOffset, String)
   (ByteString, ByteOffset, Either ParseAddressRawError Address)
 -> Either ParseAddressRawError Address)
-> Either
     (ByteString, ByteOffset, String)
     (ByteString, ByteOffset, Either ParseAddressRawError Address)
-> Either ParseAddressRawError Address
forall a b. (a -> b) -> a -> b
$ (Get (Either ParseAddressRawError Address)
 -> ByteString
 -> Either
      (ByteString, ByteOffset, String)
      (ByteString, ByteOffset, Either ParseAddressRawError Address))
-> ByteString
-> Get (Either ParseAddressRawError Address)
-> Either
     (ByteString, ByteOffset, String)
     (ByteString, ByteOffset, Either ParseAddressRawError Address)
forall a b c. (a -> b -> c) -> b -> a -> c
flip Get (Either ParseAddressRawError Address)
-> ByteString
-> Either
     (ByteString, ByteOffset, String)
     (ByteString, ByteOffset, Either ParseAddressRawError Address)
forall a.
Get a
-> ByteString
-> Either
     (ByteString, ByteOffset, String) (ByteString, ByteOffset, a)
Get.runGetOrFail (ByteString -> ByteString
LBS.fromStrict ByteString
bytes) (Get (Either ParseAddressRawError Address)
 -> Either
      (ByteString, ByteOffset, String)
      (ByteString, ByteOffset, Either ParseAddressRawError Address))
-> Get (Either ParseAddressRawError Address)
-> Either
     (ByteString, ByteOffset, String)
     (ByteString, ByteOffset, Either ParseAddressRawError Address)
forall a b. (a -> b) -> a -> b
$ ExceptT ParseAddressRawError Get Address
-> Get (Either ParseAddressRawError Address)
forall e (m :: * -> *) a. ExceptT e m a -> m (Either e a)
runExceptT
  (ExceptT ParseAddressRawError Get Address
 -> Get (Either ParseAddressRawError Address))
-> ExceptT ParseAddressRawError Get Address
-> Get (Either ParseAddressRawError Address)
forall a b. (a -> b) -> a -> b
$ String
-> (Word8 -> ExceptT ParseAddressRawError Get Address)
-> [TaggedDecoderM (ExceptT ParseAddressRawError) Address]
-> ExceptT ParseAddressRawError Get Address
forall (t :: (* -> *) -> * -> *) a.
(MonadTrans t, Monad (t Get)) =>
String -> (Word8 -> t Get a) -> [TaggedDecoderM t a] -> t Get a
decodeWithTagM String
"address" (ParseAddressRawError -> ExceptT ParseAddressRawError Get Address
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (ParseAddressRawError -> ExceptT ParseAddressRawError Get Address)
-> (Word8 -> ParseAddressRawError)
-> Word8
-> ExceptT ParseAddressRawError Get Address
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> ParseAddressRawError
ParseAddressRawInvalidPrefix) [TaggedDecoderM (ExceptT ParseAddressRawError) Address]
decoders
  where
    decoders :: [TaggedDecoderM (ExceptT ParseAddressRawError) Address]
decoders = TaggedDecoderM (ExceptT ParseAddressRawError) Address
forall {a}. TaggedDecoderM (ExceptT ParseAddressRawError) a
txr1error TaggedDecoderM (ExceptT ParseAddressRawError) Address
-> [TaggedDecoderM (ExceptT ParseAddressRawError) Address]
-> [TaggedDecoderM (ExceptT ParseAddressRawError) Address]
forall a. a -> [a] -> [a]
: (AddressKind
 -> TaggedDecoderM (ExceptT ParseAddressRawError) Address)
-> [AddressKind]
-> [TaggedDecoderM (ExceptT ParseAddressRawError) Address]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
map AddressKind
-> TaggedDecoderM (ExceptT ParseAddressRawError) Address
mkDecoder [AddressKind
forall a. Bounded a => a
minBound..]
    txr1error :: TaggedDecoderM (ExceptT ParseAddressRawError) a
txr1error = Word8
0x02 Word8
-> ExceptT ParseAddressRawError Get a
-> TaggedDecoderM (ExceptT ParseAddressRawError) a
forall {k} (t :: (* -> *) -> k -> *) (a :: k).
Word8 -> t Get a -> TaggedDecoderM t a
##: ParseAddressRawError -> ExceptT ParseAddressRawError Get a
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (Text -> Word8 -> ParseAddressRawError
ParseAddressRawUnsupportedPrefix Text
"txr1" Word8
0x02)
    mkDecoder :: AddressKind
-> TaggedDecoderM (ExceptT ParseAddressRawError) Address
mkDecoder AddressKind
ak = AddressKind -> Word8
addressKindTag AddressKind
ak Word8
-> ExceptT ParseAddressRawError Get Address
-> TaggedDecoderM (ExceptT ParseAddressRawError) Address
forall {k} (t :: (* -> *) -> k -> *) (a :: k).
Word8 -> t Get a -> TaggedDecoderM t a
##: case AddressKind
ak of
      AddressKind
AddressKindImplicit    -> ImplicitAddress -> Address
forall (kind :: AddressKind). KindedAddress kind -> Address
MkAddress (ImplicitAddress -> Address)
-> (KeyHash -> ImplicitAddress) -> KeyHash -> Address
forall b c a. (b -> c) -> (a -> b) -> a -> c
. KeyHash -> ImplicitAddress
ImplicitAddress (KeyHash -> Address)
-> ExceptT ParseAddressRawError Get KeyHash
-> ExceptT ParseAddressRawError Get Address
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ExceptT ParseAddressRawError Get KeyHash
keyHash
      AddressKind
AddressKindContract    -> KindedAddress 'AddressKindContract -> Address
forall (kind :: AddressKind). KindedAddress kind -> Address
MkAddress (KindedAddress 'AddressKindContract -> Address)
-> (ContractHash -> KindedAddress 'AddressKindContract)
-> ContractHash
-> Address
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ContractHash -> KindedAddress 'AddressKindContract
ContractAddress (ContractHash -> Address)
-> ExceptT ParseAddressRawError Get ContractHash
-> ExceptT ParseAddressRawError Get Address
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> HashTag 'HashKindContract
-> ExceptT ParseAddressRawError Get ContractHash
forall (kind :: HashKind).
HashTag kind -> ExceptT ParseAddressRawError Get (Hash kind)
sepHash HashTag 'HashKindContract
HashContract
      AddressKind
AddressKindSmartRollup -> KindedAddress 'AddressKindSmartRollup -> Address
forall (kind :: AddressKind). KindedAddress kind -> Address
MkAddress (KindedAddress 'AddressKindSmartRollup -> Address)
-> (SmartRollupHash -> KindedAddress 'AddressKindSmartRollup)
-> SmartRollupHash
-> Address
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SmartRollupHash -> KindedAddress 'AddressKindSmartRollup
SmartRollupAddress (SmartRollupHash -> Address)
-> ExceptT ParseAddressRawError Get SmartRollupHash
-> ExceptT ParseAddressRawError Get Address
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> HashTag 'HashKindSmartRollup
-> ExceptT ParseAddressRawError Get SmartRollupHash
forall (kind :: HashKind).
HashTag kind -> ExceptT ParseAddressRawError Get (Hash kind)
sepHash HashTag 'HashKindSmartRollup
HashSR
    sep :: ExceptT ParseAddressRawError Get ()
sep = Get Word8 -> ExceptT ParseAddressRawError Get Word8
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift Get Word8
Get.getWord8 ExceptT ParseAddressRawError Get Word8
-> (Word8 -> ExceptT ParseAddressRawError Get ())
-> ExceptT ParseAddressRawError Get ()
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
      Word8
0x00 -> ExceptT ParseAddressRawError Get ()
forall (f :: * -> *). Applicative f => f ()
pass
      Word8
x -> ParseAddressRawError -> ExceptT ParseAddressRawError Get ()
forall e (m :: * -> *) a. MonadError e m => e -> m a
throwError (ParseAddressRawError -> ExceptT ParseAddressRawError Get ())
-> ParseAddressRawError -> ExceptT ParseAddressRawError Get ()
forall a b. (a -> b) -> a -> b
$ Word8 -> ParseAddressRawError
ParseAddressRawMalformedSeparator Word8
x
    keyHash :: ExceptT ParseAddressRawError Get KeyHash
keyHash = (Get (Either CryptoParseError KeyHash)
 -> Get (Either ParseAddressRawError KeyHash))
-> ExceptT CryptoParseError Get KeyHash
-> ExceptT ParseAddressRawError Get KeyHash
forall (m :: * -> *) e a (n :: * -> *) e' b.
(m (Either e a) -> n (Either e' b))
-> ExceptT e m a -> ExceptT e' n b
mapExceptT ((Either CryptoParseError KeyHash
 -> Either ParseAddressRawError KeyHash)
-> Get (Either CryptoParseError KeyHash)
-> Get (Either ParseAddressRawError KeyHash)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Either CryptoParseError KeyHash
  -> Either ParseAddressRawError KeyHash)
 -> Get (Either CryptoParseError KeyHash)
 -> Get (Either ParseAddressRawError KeyHash))
-> (Either CryptoParseError KeyHash
    -> Either ParseAddressRawError KeyHash)
-> Get (Either CryptoParseError KeyHash)
-> Get (Either ParseAddressRawError KeyHash)
forall a b. (a -> b) -> a -> b
$ (CryptoParseError -> ParseAddressRawError)
-> Either CryptoParseError KeyHash
-> Either ParseAddressRawError KeyHash
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first CryptoParseError -> ParseAddressRawError
ParseAddressRawCryptoError) ExceptT CryptoParseError Get KeyHash
decodeKeyHash
    sepHash :: HashTag kind -> ExceptT ParseAddressRawError Get.Get (Hash kind)
    sepHash :: forall (kind :: HashKind).
HashTag kind -> ExceptT ParseAddressRawError Get (Hash kind)
sepHash HashTag kind
kind = HashTag kind -> ByteString -> Hash kind
forall (kind :: HashKind). HashTag kind -> ByteString -> Hash kind
Hash HashTag kind
kind (ByteString -> Hash kind)
-> ExceptT ParseAddressRawError Get ByteString
-> ExceptT ParseAddressRawError Get (Hash kind)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get ByteString -> ExceptT ParseAddressRawError Get ByteString
forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift (Int -> Get ByteString
getByteStringCopy Int
forall n. Integral n => n
hashLengthBytes) ExceptT ParseAddressRawError Get (Hash kind)
-> ExceptT ParseAddressRawError Get ()
-> ExceptT ParseAddressRawError Get (Hash kind)
forall (f :: * -> *) a b. Applicative f => f a -> f b -> f a
<* ExceptT ParseAddressRawError Get ()
sep

addressKindTag :: AddressKind -> Word8
addressKindTag :: AddressKind -> Word8
addressKindTag = \case
  AddressKind
AddressKindImplicit    -> Word8
0x00
  AddressKind
AddressKindContract    -> Word8
0x01
  -- 0x02 is txr1 which we do not support
  AddressKind
AddressKindSmartRollup -> Word8
0x03

-- | QuasiQuoter for constructing Tezos addresses.
--
-- Validity of result will be checked at compile time.
ta :: TH.QuasiQuoter
ta :: QuasiQuoter
ta = QuasiQuoter :: (String -> Q Exp)
-> (String -> Q Pat)
-> (String -> Q Type)
-> (String -> Q [Dec])
-> QuasiQuoter
TH.QuasiQuoter
  { quoteExp :: String -> Q Exp
TH.quoteExp = \String
s ->
      case Text -> Either ParseAddressError Address
parseAddress (Text -> Either ParseAddressError Address)
-> (Text -> Text) -> Text -> Either ParseAddressError Address
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Text
strip (Text -> Either ParseAddressError Address)
-> Text -> Either ParseAddressError Address
forall a b. (a -> b) -> a -> b
$ String -> Text
forall a. ToText a => a -> Text
toText String
s of
        Left   ParseAddressError
err -> String -> Q Exp
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Q Exp) -> String -> Q Exp
forall a b. (a -> b) -> a -> b
$ ParseAddressError -> String
forall a b. (Buildable a, FromBuilder b) => a -> b
pretty ParseAddressError
err
        Right (MkAddress KindedAddress kind
addr) -> KindedAddress kind -> Q Exp
forall t (m :: * -> *). (Lift t, Quote m) => t -> m Exp
TH.lift KindedAddress kind
addr
  , quotePat :: String -> Q Pat
TH.quotePat = \String
_ ->
      String -> Q Pat
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Cannot use this QuasiQuotation at pattern position"
  , quoteType :: String -> Q Type
TH.quoteType = \String
_ ->
      String -> Q Type
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Cannot use this QuasiQuotation at type position"
  , quoteDec :: String -> Q [Dec]
TH.quoteDec = \String
_ ->
      String -> Q [Dec]
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Cannot use this QuasiQuotation at declaration position"
  }


instance
    ( Bottom
    , TypeError ('Text "There is no instance defined for (IsString Address)" ':$$:
                 'Text "Consider using QuasiQuotes: `[ta|some text...|]`"
                )) =>
    IsString (KindedAddress kind) where
  fromString :: String -> KindedAddress kind
fromString = String -> KindedAddress kind
forall a. Bottom => a
no


----------------------------------------------------------------------------
-- Unsafe
----------------------------------------------------------------------------

instance SingI kind => HasCLReader (KindedAddress kind) where
  getMetavar :: String
getMetavar = Char -> Char
toUpper (Char -> Char) -> ShowS
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> AddressKind -> String
forall a b. (Buildable a, FromBuilder b) => a -> b
pretty (forall {k} (a :: k). (SingKind k, SingI a) => Demote k
forall (a :: AddressKind).
(SingKind AddressKind, SingI a) =>
Demote AddressKind
demote @kind) String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
" ADDRESS"
  getReader :: ReadM (KindedAddress kind)
getReader = (Text -> Either ParseAddressError (KindedAddress kind))
-> ReadM (KindedAddress kind)
forall addr. (Text -> Either ParseAddressError addr) -> ReadM addr
getAddressReader Text -> Either ParseAddressError (KindedAddress kind)
forall (kind :: AddressKind).
SingI kind =>
Text -> Either ParseAddressError (KindedAddress kind)
parseKindedAddress

instance SingI ks => HasCLReader (ConstrainedAddress ks) where
  getMetavar :: String
getMetavar = String -> [String] -> String
forall a. [a] -> [[a]] -> [a]
intercalate String
" OR " ((Char -> Char) -> ShowS
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Char -> Char
toUpper ShowS -> (AddressKind -> String) -> AddressKind -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AddressKind -> String
forall a b. (Buildable a, FromBuilder b) => a -> b
pretty (AddressKind -> String) -> [AddressKind] -> [String]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (a :: [AddressKind]).
(SingKind [AddressKind], SingI a) =>
Demote [AddressKind]
forall {k} (a :: k). (SingKind k, SingI a) => Demote k
demote @ks) String -> ShowS
forall a. Semigroup a => a -> a -> a
<> String
" ADDRESS"
  getReader :: ReadM (ConstrainedAddress ks)
getReader = (Text -> Either ParseAddressError (ConstrainedAddress ks))
-> ReadM (ConstrainedAddress ks)
forall addr. (Text -> Either ParseAddressError addr) -> ReadM addr
getAddressReader Text -> Either ParseAddressError (ConstrainedAddress ks)
forall (kinds :: [AddressKind]).
SingI kinds =>
Text -> Either ParseAddressError (ConstrainedAddress kinds)
parseConstrainedAddress

instance HasCLReader Address where
  getMetavar :: String
getMetavar = String
"ADDRESS"
  getReader :: ReadM Address
getReader = (Text -> Either ParseAddressError Address) -> ReadM Address
forall addr. (Text -> Either ParseAddressError addr) -> ReadM addr
getAddressReader Text -> Either ParseAddressError Address
parseAddress

getAddressReader :: (Text -> Either ParseAddressError addr) -> ReadM addr
getAddressReader :: forall addr. (Text -> Either ParseAddressError addr) -> ReadM addr
getAddressReader Text -> Either ParseAddressError addr
parser = (String -> Either String addr) -> ReadM addr
forall a. (String -> Either String a) -> ReadM a
eitherReader ((String -> Either String addr) -> ReadM addr)
-> (String -> Either String addr) -> ReadM addr
forall a b. (a -> b) -> a -> b
$
  (ParseAddressError -> String)
-> Either ParseAddressError addr -> Either String addr
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (String -> ShowS
forall a. Monoid a => a -> a -> a
mappend String
"Failed to parse address: " ShowS
-> (ParseAddressError -> String) -> ParseAddressError -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ParseAddressError -> String
forall a b. (Buildable a, FromBuilder b) => a -> b
pretty) (Either ParseAddressError addr -> Either String addr)
-> (String -> Either ParseAddressError addr)
-> String
-> Either String addr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Either ParseAddressError addr
parser (Text -> Either ParseAddressError addr)
-> (String -> Text) -> String -> Either ParseAddressError addr
forall b c a. (b -> c) -> (a -> b) -> a -> c
. String -> Text
forall a. ToText a => a -> Text
toText

----------------------------------------------------------------------------
-- Aeson instances
----------------------------------------------------------------------------

instance SingI kinds => FromJSON (ConstrainedAddress kinds) where
  parseJSON :: Value -> Parser (ConstrainedAddress kinds)
parseJSON = String
-> (Text -> Parser (ConstrainedAddress kinds))
-> Value
-> Parser (ConstrainedAddress kinds)
forall a. String -> (Text -> Parser a) -> Value -> Parser a
Aeson.withText String
"Address" ((Text -> Parser (ConstrainedAddress kinds))
 -> Value -> Parser (ConstrainedAddress kinds))
-> (Text -> Parser (ConstrainedAddress kinds))
-> Value
-> Parser (ConstrainedAddress kinds)
forall a b. (a -> b) -> a -> b
$
    (ParseAddressError -> Parser (ConstrainedAddress kinds))
-> (ConstrainedAddress kinds -> Parser (ConstrainedAddress kinds))
-> Either ParseAddressError (ConstrainedAddress kinds)
-> Parser (ConstrainedAddress kinds)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> Parser (ConstrainedAddress kinds)
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Parser (ConstrainedAddress kinds))
-> (ParseAddressError -> String)
-> ParseAddressError
-> Parser (ConstrainedAddress kinds)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ParseAddressError -> String
forall a b. (Buildable a, FromBuilder b) => a -> b
pretty) ConstrainedAddress kinds -> Parser (ConstrainedAddress kinds)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either ParseAddressError (ConstrainedAddress kinds)
 -> Parser (ConstrainedAddress kinds))
-> (Text -> Either ParseAddressError (ConstrainedAddress kinds))
-> Text
-> Parser (ConstrainedAddress kinds)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Either ParseAddressError (ConstrainedAddress kinds)
forall (kinds :: [AddressKind]).
SingI kinds =>
Text -> Either ParseAddressError (ConstrainedAddress kinds)
parseConstrainedAddress

instance ToJSON (KindedAddress kind) where
  toJSON :: KindedAddress kind -> Value
toJSON = Text -> Value
Aeson.String (Text -> Value)
-> (KindedAddress kind -> Text) -> KindedAddress kind -> Value
forall b c a. (b -> c) -> (a -> b) -> a -> c
. KindedAddress kind -> Text
forall (kind :: AddressKind). KindedAddress kind -> Text
formatAddress
  toEncoding :: KindedAddress kind -> Encoding
toEncoding = Text -> Encoding
forall a. Text -> Encoding' a
Aeson.text (Text -> Encoding)
-> (KindedAddress kind -> Text) -> KindedAddress kind -> Encoding
forall b c a. (b -> c) -> (a -> b) -> a -> c
. KindedAddress kind -> Text
forall (kind :: AddressKind). KindedAddress kind -> Text
formatAddress

instance ToJSONKey (KindedAddress kind) where
  toJSONKey :: ToJSONKeyFunction (KindedAddress kind)
toJSONKey = (KindedAddress kind -> Text)
-> ToJSONKeyFunction (KindedAddress kind)
forall a. (a -> Text) -> ToJSONKeyFunction a
AesonTypes.toJSONKeyText KindedAddress kind -> Text
forall (kind :: AddressKind). KindedAddress kind -> Text
formatAddress

instance SingI kind => FromJSON (KindedAddress kind) where
  parseJSON :: Value -> Parser (KindedAddress kind)
parseJSON =
    String
-> (Text -> Parser (KindedAddress kind))
-> Value
-> Parser (KindedAddress kind)
forall a. String -> (Text -> Parser a) -> Value -> Parser a
Aeson.withText String
"Address" ((Text -> Parser (KindedAddress kind))
 -> Value -> Parser (KindedAddress kind))
-> (Text -> Parser (KindedAddress kind))
-> Value
-> Parser (KindedAddress kind)
forall a b. (a -> b) -> a -> b
$
    (ParseAddressError -> Parser (KindedAddress kind))
-> (KindedAddress kind -> Parser (KindedAddress kind))
-> Either ParseAddressError (KindedAddress kind)
-> Parser (KindedAddress kind)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> Parser (KindedAddress kind)
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Parser (KindedAddress kind))
-> (ParseAddressError -> String)
-> ParseAddressError
-> Parser (KindedAddress kind)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ParseAddressError -> String
forall a b. (Buildable a, FromBuilder b) => a -> b
pretty) KindedAddress kind -> Parser (KindedAddress kind)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either ParseAddressError (KindedAddress kind)
 -> Parser (KindedAddress kind))
-> (Text -> Either ParseAddressError (KindedAddress kind))
-> Text
-> Parser (KindedAddress kind)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Either ParseAddressError (KindedAddress kind)
forall (kind :: AddressKind).
SingI kind =>
Text -> Either ParseAddressError (KindedAddress kind)
parseKindedAddress

instance SingI kind => FromJSONKey (KindedAddress kind) where
  fromJSONKey :: FromJSONKeyFunction (KindedAddress kind)
fromJSONKey =
    (Text -> Parser (KindedAddress kind))
-> FromJSONKeyFunction (KindedAddress kind)
forall a. (Text -> Parser a) -> FromJSONKeyFunction a
AesonTypes.FromJSONKeyTextParser
      ((ParseAddressError -> Parser (KindedAddress kind))
-> (KindedAddress kind -> Parser (KindedAddress kind))
-> Either ParseAddressError (KindedAddress kind)
-> Parser (KindedAddress kind)
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> Parser (KindedAddress kind)
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Parser (KindedAddress kind))
-> (ParseAddressError -> String)
-> ParseAddressError
-> Parser (KindedAddress kind)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ParseAddressError -> String
forall a b. (Buildable a, FromBuilder b) => a -> b
pretty) KindedAddress kind -> Parser (KindedAddress kind)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either ParseAddressError (KindedAddress kind)
 -> Parser (KindedAddress kind))
-> (Text -> Either ParseAddressError (KindedAddress kind))
-> Text
-> Parser (KindedAddress kind)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Either ParseAddressError (KindedAddress kind)
forall (kind :: AddressKind).
SingI kind =>
Text -> Either ParseAddressError (KindedAddress kind)
parseKindedAddress)

instance ToJSON (Constrained c KindedAddress) where
  toJSON :: Constrained c KindedAddress -> Value
toJSON = (forall (t :: AddressKind). c t => KindedAddress t -> Value)
-> Constrained c KindedAddress -> Value
forall {k} (c :: k -> Constraint) (f :: k -> *) r.
(forall (t :: k). c t => f t -> r) -> Constrained c f -> r
foldConstrained forall a. ToJSON a => a -> Value
forall (t :: AddressKind). c t => KindedAddress t -> Value
toJSON
  toEncoding :: Constrained c KindedAddress -> Encoding
toEncoding = (forall (t :: AddressKind). c t => KindedAddress t -> Encoding)
-> Constrained c KindedAddress -> Encoding
forall {k} (c :: k -> Constraint) (f :: k -> *) r.
(forall (t :: k). c t => f t -> r) -> Constrained c f -> r
foldConstrained forall a. ToJSON a => a -> Encoding
forall (t :: AddressKind). c t => KindedAddress t -> Encoding
toEncoding

instance ToJSONKey (Constrained c KindedAddress) where
  toJSONKey :: ToJSONKeyFunction (Constrained c KindedAddress)
toJSONKey = (Constrained c KindedAddress -> Text)
-> ToJSONKeyFunction (Constrained c KindedAddress)
forall a. (a -> Text) -> ToJSONKeyFunction a
AesonTypes.toJSONKeyText ((Constrained c KindedAddress -> Text)
 -> ToJSONKeyFunction (Constrained c KindedAddress))
-> (Constrained c KindedAddress -> Text)
-> ToJSONKeyFunction (Constrained c KindedAddress)
forall a b. (a -> b) -> a -> b
$ (forall (t :: AddressKind). c t => KindedAddress t -> Text)
-> Constrained c KindedAddress -> Text
forall {k} (c :: k -> Constraint) (f :: k -> *) r.
(forall (t :: k). c t => f t -> r) -> Constrained c f -> r
foldConstrained forall (t :: AddressKind). c t => KindedAddress t -> Text
forall (kind :: AddressKind). KindedAddress kind -> Text
formatAddress

instance FromJSON Address where
  parseJSON :: Value -> Parser Address
parseJSON =
    String -> (Text -> Parser Address) -> Value -> Parser Address
forall a. String -> (Text -> Parser a) -> Value -> Parser a
Aeson.withText String
"Address" ((Text -> Parser Address) -> Value -> Parser Address)
-> (Text -> Parser Address) -> Value -> Parser Address
forall a b. (a -> b) -> a -> b
$
    (ParseAddressError -> Parser Address)
-> (Address -> Parser Address)
-> Either ParseAddressError Address
-> Parser Address
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> Parser Address
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Parser Address)
-> (ParseAddressError -> String)
-> ParseAddressError
-> Parser Address
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ParseAddressError -> String
forall a b. (Buildable a, FromBuilder b) => a -> b
pretty) Address -> Parser Address
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either ParseAddressError Address -> Parser Address)
-> (Text -> Either ParseAddressError Address)
-> Text
-> Parser Address
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Either ParseAddressError Address
parseAddress

instance FromJSONKey Address where
  fromJSONKey :: FromJSONKeyFunction Address
fromJSONKey =
    (Text -> Parser Address) -> FromJSONKeyFunction Address
forall a. (Text -> Parser a) -> FromJSONKeyFunction a
AesonTypes.FromJSONKeyTextParser
      ((ParseAddressError -> Parser Address)
-> (Address -> Parser Address)
-> Either ParseAddressError Address
-> Parser Address
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (String -> Parser Address
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Parser Address)
-> (ParseAddressError -> String)
-> ParseAddressError
-> Parser Address
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ParseAddressError -> String
forall a b. (Buildable a, FromBuilder b) => a -> b
pretty) Address -> Parser Address
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Either ParseAddressError Address -> Parser Address)
-> (Text -> Either ParseAddressError Address)
-> Text
-> Parser Address
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Either ParseAddressError Address
parseAddress)