{-# LANGUAGE DeriveLift #-}
module Morley.Tezos.Address
( ContractHash
, KindedAddress (..)
, TxRollupL2Address (..)
, mkKeyAddress
, detGenKeyAddress
, isImplicitAddress
, ImplicitAddress
, ContractAddress
, TxRollupAddress
, L1Address
, L1AddressKind
, ConstrainAddressKind
, Address
, ConstrainedAddress
, AnyParsableAddress(..)
, Constrained(.., MkConstrainedAddress, MkAddress)
, GlobalCounter(..)
, mkContractHashHack
, parseConstrainedAddress
, parseAnyAddress
, ParseAddressError (..)
, ParseAddressRawError (..)
, formatAddress
, mformatAddress
, parseAddressRaw
, parseKindedAddress
, parseAddress
, ta
, addressKindSanity
, usingImplicitOrContractKind
, unImplicitAddress
) 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.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 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
data KindedAddress (kind :: AddressKind) where
ImplicitAddress :: KeyHash -> KindedAddress 'AddressKindImplicit
ContractAddress :: ContractHash -> KindedAddress 'AddressKindContract
TxRollupAddress :: TxRollupHash -> KindedAddress 'AddressKindTxRollup
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
type ImplicitAddress = KindedAddress 'AddressKindImplicit
type ContractAddress = KindedAddress 'AddressKindContract
type TxRollupAddress = KindedAddress 'AddressKindTxRollup
type Address = Constrained NullConstraint KindedAddress
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")
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
type ConstrainedAddress (ks :: [AddressKind]) =
Constrained (ConstrainAddressKind ks) KindedAddress
pattern MkConstrainedAddress
:: forall ks. ()
=> forall kind. ConstrainAddressKind ks kind
=> KindedAddress kind -> ConstrainedAddress ks
pattern $bMkConstrainedAddress :: forall (ks :: [AddressKind]) (kind :: AddressKind).
ConstrainAddressKind ks kind =>
KindedAddress kind -> ConstrainedAddress ks
$mMkConstrainedAddress :: forall {r} {ks :: [AddressKind]}.
ConstrainedAddress ks
-> (forall {kind :: AddressKind}.
ConstrainAddressKind ks kind =>
KindedAddress kind -> r)
-> (Void# -> r)
-> r
MkConstrainedAddress a = Constrained a
{-# COMPLETE MkConstrainedAddress #-}
{-# DEPRECATED MkConstrainedAddress "Use Constrained instead" #-}
type L1Address =
ConstrainedAddress '[ 'AddressKindImplicit, 'AddressKindContract ]
type L1AddressKind kind =
ConstrainAddressKind '[ 'AddressKindImplicit, 'AddressKindContract ] kind
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)
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
newtype TxRollupL2Address = TxRollupL2Address KeyHashL2
deriving stock (Int -> TxRollupL2Address -> ShowS
[TxRollupL2Address] -> ShowS
TxRollupL2Address -> String
(Int -> TxRollupL2Address -> ShowS)
-> (TxRollupL2Address -> String)
-> ([TxRollupL2Address] -> ShowS)
-> Show TxRollupL2Address
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TxRollupL2Address] -> ShowS
$cshowList :: [TxRollupL2Address] -> ShowS
show :: TxRollupL2Address -> String
$cshow :: TxRollupL2Address -> String
showsPrec :: Int -> TxRollupL2Address -> ShowS
$cshowsPrec :: Int -> TxRollupL2Address -> ShowS
Show, TxRollupL2Address -> TxRollupL2Address -> Bool
(TxRollupL2Address -> TxRollupL2Address -> Bool)
-> (TxRollupL2Address -> TxRollupL2Address -> Bool)
-> Eq TxRollupL2Address
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: TxRollupL2Address -> TxRollupL2Address -> Bool
$c/= :: TxRollupL2Address -> TxRollupL2Address -> Bool
== :: TxRollupL2Address -> TxRollupL2Address -> Bool
$c== :: TxRollupL2Address -> TxRollupL2Address -> Bool
Eq, Eq TxRollupL2Address
Eq TxRollupL2Address
-> (TxRollupL2Address -> TxRollupL2Address -> Ordering)
-> (TxRollupL2Address -> TxRollupL2Address -> Bool)
-> (TxRollupL2Address -> TxRollupL2Address -> Bool)
-> (TxRollupL2Address -> TxRollupL2Address -> Bool)
-> (TxRollupL2Address -> TxRollupL2Address -> Bool)
-> (TxRollupL2Address -> TxRollupL2Address -> TxRollupL2Address)
-> (TxRollupL2Address -> TxRollupL2Address -> TxRollupL2Address)
-> Ord TxRollupL2Address
TxRollupL2Address -> TxRollupL2Address -> Bool
TxRollupL2Address -> TxRollupL2Address -> Ordering
TxRollupL2Address -> TxRollupL2Address -> TxRollupL2Address
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
min :: TxRollupL2Address -> TxRollupL2Address -> TxRollupL2Address
$cmin :: TxRollupL2Address -> TxRollupL2Address -> TxRollupL2Address
max :: TxRollupL2Address -> TxRollupL2Address -> TxRollupL2Address
$cmax :: TxRollupL2Address -> TxRollupL2Address -> TxRollupL2Address
>= :: TxRollupL2Address -> TxRollupL2Address -> Bool
$c>= :: TxRollupL2Address -> TxRollupL2Address -> Bool
> :: TxRollupL2Address -> TxRollupL2Address -> Bool
$c> :: TxRollupL2Address -> TxRollupL2Address -> Bool
<= :: TxRollupL2Address -> TxRollupL2Address -> Bool
$c<= :: TxRollupL2Address -> TxRollupL2Address -> Bool
< :: TxRollupL2Address -> TxRollupL2Address -> Bool
$c< :: TxRollupL2Address -> TxRollupL2Address -> Bool
compare :: TxRollupL2Address -> TxRollupL2Address -> Ordering
$ccompare :: TxRollupL2Address -> TxRollupL2Address -> Ordering
Ord, (forall x. TxRollupL2Address -> Rep TxRollupL2Address x)
-> (forall x. Rep TxRollupL2Address x -> TxRollupL2Address)
-> Generic TxRollupL2Address
forall x. Rep TxRollupL2Address x -> TxRollupL2Address
forall x. TxRollupL2Address -> Rep TxRollupL2Address x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep TxRollupL2Address x -> TxRollupL2Address
$cfrom :: forall x. TxRollupL2Address -> Rep TxRollupL2Address x
Generic, (forall (m :: * -> *). Quote m => TxRollupL2Address -> m Exp)
-> (forall (m :: * -> *).
Quote m =>
TxRollupL2Address -> Code m TxRollupL2Address)
-> Lift TxRollupL2Address
forall t.
(forall (m :: * -> *). Quote m => t -> m Exp)
-> (forall (m :: * -> *). Quote m => t -> Code m t) -> Lift t
forall (m :: * -> *). Quote m => TxRollupL2Address -> m Exp
forall (m :: * -> *).
Quote m =>
TxRollupL2Address -> Code m TxRollupL2Address
liftTyped :: forall (m :: * -> *).
Quote m =>
TxRollupL2Address -> Code m TxRollupL2Address
$cliftTyped :: forall (m :: * -> *).
Quote m =>
TxRollupL2Address -> Code m TxRollupL2Address
lift :: forall (m :: * -> *). Quote m => TxRollupL2Address -> m Exp
$clift :: forall (m :: * -> *). Quote m => TxRollupL2Address -> m Exp
Lift)
deriving newtype TxRollupL2Address -> ()
(TxRollupL2Address -> ()) -> NFData TxRollupL2Address
forall a. (a -> ()) -> NFData a
rnf :: TxRollupL2Address -> ()
$crnf :: TxRollupL2Address -> ()
NFData
data AnyParsableAddress
= AnyParsableL2Address TxRollupL2Address
| AnyParsableAddress Address
deriving stock (Int -> AnyParsableAddress -> ShowS
[AnyParsableAddress] -> ShowS
AnyParsableAddress -> String
(Int -> AnyParsableAddress -> ShowS)
-> (AnyParsableAddress -> String)
-> ([AnyParsableAddress] -> ShowS)
-> Show AnyParsableAddress
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [AnyParsableAddress] -> ShowS
$cshowList :: [AnyParsableAddress] -> ShowS
show :: AnyParsableAddress -> String
$cshow :: AnyParsableAddress -> String
showsPrec :: Int -> AnyParsableAddress -> ShowS
$cshowsPrec :: Int -> AnyParsableAddress -> ShowS
Show, AnyParsableAddress -> AnyParsableAddress -> Bool
(AnyParsableAddress -> AnyParsableAddress -> Bool)
-> (AnyParsableAddress -> AnyParsableAddress -> Bool)
-> Eq AnyParsableAddress
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: AnyParsableAddress -> AnyParsableAddress -> Bool
$c/= :: AnyParsableAddress -> AnyParsableAddress -> Bool
== :: AnyParsableAddress -> AnyParsableAddress -> Bool
$c== :: AnyParsableAddress -> AnyParsableAddress -> Bool
Eq, (forall x. AnyParsableAddress -> Rep AnyParsableAddress x)
-> (forall x. Rep AnyParsableAddress x -> AnyParsableAddress)
-> Generic AnyParsableAddress
forall x. Rep AnyParsableAddress x -> AnyParsableAddress
forall x. AnyParsableAddress -> Rep AnyParsableAddress x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep AnyParsableAddress x -> AnyParsableAddress
$cfrom :: forall x. AnyParsableAddress -> Rep AnyParsableAddress x
Generic)
instance NFData AnyParsableAddress
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
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
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
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)
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
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
TxRollupAddress TxRollupHash
h -> TxRollupHash -> Text
forall (kind :: HashKind). Hash kind -> Text
formatHash TxRollupHash
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
instance Buildable TxRollupL2Address where
build :: TxRollupL2Address -> Builder
build (TxRollupL2Address KeyHashL2
kh) = Text -> Builder
forall p. Buildable p => p -> Builder
build (Text -> Builder) -> Text -> Builder
forall a b. (a -> b) -> a -> b
$ KeyHashL2 -> Text
forall (kind :: HashKind). Hash kind -> Text
formatHash KeyHashL2
kh
data ParseAddressError
= ParseAddressCryptoError CryptoParseError
| ParseAddressWrongKind [AddressKind] AnyParsableAddress
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 (AnyParsableAddress (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
]
ParseAddressWrongKind [AddressKind]
expected (AnyParsableL2Address TxRollupL2Address
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 unexpected TxRollupL2Address ", TxRollupL2Address -> Doc
forall a. Buildable a => a -> Doc
renderAnyBuildable TxRollupL2Address
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)
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] -> AnyParsableAddress -> ParseAddressError
ParseAddressWrongKind [forall {k} (a :: k). (SingKind k, SingI a) => Demote k
forall (a :: AddressKind).
(SingKind AddressKind, SingI a) =>
Demote AddressKind
demote @kind] (AnyParsableAddress -> ParseAddressError)
-> (Address -> AnyParsableAddress) -> Address -> ParseAddressError
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Address -> AnyParsableAddress
AnyParsableAddress (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)
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] -> AnyParsableAddress -> ParseAddressError
ParseAddressWrongKind [AddressKind]
allowed (AnyParsableAddress -> ParseAddressError)
-> (Address -> AnyParsableAddress) -> Address -> ParseAddressError
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Address -> AnyParsableAddress
AnyParsableAddress
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
:: 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 Sing k
k Sing x
x = case Sing x
x of
Sing x
SingAddressKind x
SAddressKindImplicit -> case Sing k
k of
Sing k
SingAddressKind k
SAddressKindImplicit -> Dict (ConstrainAddressKind (x : ks) k)
forall (a :: Constraint). a => Dict a
Dict
Sing k
SingAddressKind k
SAddressKindContract -> Dict (ConstrainAddressKind (x : ks) k)
forall (a :: Constraint). a => Dict a
Dict
Sing k
SingAddressKind k
SAddressKindTxRollup -> Dict (ConstrainAddressKind (x : ks) k)
forall (a :: Constraint). a => Dict a
Dict
Sing x
SingAddressKind x
SAddressKindContract -> case Sing k
k of
Sing k
SingAddressKind k
SAddressKindImplicit -> Dict (ConstrainAddressKind (x : ks) k)
forall (a :: Constraint). a => Dict a
Dict
Sing k
SingAddressKind k
SAddressKindContract -> Dict (ConstrainAddressKind (x : ks) k)
forall (a :: Constraint). a => Dict a
Dict
Sing k
SingAddressKind k
SAddressKindTxRollup -> Dict (ConstrainAddressKind (x : ks) k)
forall (a :: Constraint). a => Dict a
Dict
Sing x
SingAddressKind x
SAddressKindTxRollup -> case Sing k
k of
Sing k
SingAddressKind k
SAddressKindImplicit -> Dict (ConstrainAddressKind (x : ks) k)
forall (a :: Constraint). a => Dict a
Dict
Sing k
SingAddressKind k
SAddressKindContract -> Dict (ConstrainAddressKind (x : ks) k)
forall (a :: Constraint). a => Dict a
Dict
Sing k
SingAddressKind k
SAddressKindTxRollup -> Dict (ConstrainAddressKind (x : ks) k)
forall (a :: Constraint). a => Dict a
Dict
parseAddress :: Text -> Either ParseAddressError Address
parseAddress :: Text -> Either ParseAddressError Address
parseAddress Text
x = Text -> Either ParseAddressError AnyParsableAddress
parseAnyAddress Text
x Either ParseAddressError AnyParsableAddress
-> (AnyParsableAddress -> Either ParseAddressError Address)
-> Either ParseAddressError Address
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
a :: AnyParsableAddress
a@AnyParsableL2Address{} -> ParseAddressError -> Either ParseAddressError Address
forall a b. a -> Either a b
Left (ParseAddressError -> Either ParseAddressError Address)
-> ParseAddressError -> Either ParseAddressError Address
forall a b. (a -> b) -> a -> b
$ [AddressKind] -> AnyParsableAddress -> ParseAddressError
ParseAddressWrongKind [AddressKind
forall a. Bounded a => a
minBound..] AnyParsableAddress
a
AnyParsableAddress Address
a -> Address -> Either ParseAddressError Address
forall a b. b -> Either a b
Right Address
a
parseAnyAddress :: Text -> Either ParseAddressError AnyParsableAddress
parseAnyAddress :: Text -> Either ParseAddressError AnyParsableAddress
parseAnyAddress Text
a = (CryptoParseError -> ParseAddressError)
-> Either CryptoParseError AnyParsableAddress
-> Either ParseAddressError AnyParsableAddress
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first CryptoParseError -> ParseAddressError
ParseAddressCryptoError (Either CryptoParseError AnyParsableAddress
-> Either ParseAddressError AnyParsableAddress)
-> Either CryptoParseError AnyParsableAddress
-> Either ParseAddressError AnyParsableAddress
forall a b. (a -> b) -> a -> b
$ Text -> Either CryptoParseError (Some Hash)
parseSomeHashBase58 Text
a Either CryptoParseError (Some Hash)
-> (Some Hash -> AnyParsableAddress)
-> Either CryptoParseError AnyParsableAddress
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> \case
Some h :: Hash a
h@(Hash HashTag a
HashBLS ByteString
_) -> TxRollupL2Address -> AnyParsableAddress
AnyParsableL2Address (TxRollupL2Address -> AnyParsableAddress)
-> TxRollupL2Address -> AnyParsableAddress
forall a b. (a -> b) -> a -> b
$ KeyHashL2 -> TxRollupL2Address
TxRollupL2Address Hash a
KeyHashL2
h
Some h :: Hash a
h@(Hash HashTag a
hk ByteString
_) -> Address -> AnyParsableAddress
AnyParsableAddress (Address -> AnyParsableAddress) -> Address -> AnyParsableAddress
forall a b. (a -> b) -> a -> b
$ case HashTag a
hk of
HashKey KeyType
KeyTypeEd25519 -> 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
HashKey KeyType
KeyTypeSecp256k1 -> 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
HashKey KeyType
KeyTypeP256 -> 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
HashTXR -> KindedAddress 'AddressKindTxRollup -> Address
forall {k} (c :: k -> Constraint) (f :: k -> *) (a :: k).
c a =>
f a -> Constrained c f
Constrained (KindedAddress 'AddressKindTxRollup -> Address)
-> KindedAddress 'AddressKindTxRollup -> Address
forall a b. (a -> b) -> a -> b
$ TxRollupHash -> KindedAddress 'AddressKindTxRollup
TxRollupAddress Hash a
TxRollupHash
h
data ParseAddressRawError
= ParseAddressRawWrongSize ByteString
| ParseAddressRawInvalidPrefix Word8
| ParseAddressRawMalformedSeparator Word8
| ParseAddressRawBinaryError Text
| ParseAddressRawCryptoError CryptoParseError
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"
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
parseAddressRaw :: ByteString -> Either ParseAddressRawError Address
parseAddressRaw :: ByteString -> Either ParseAddressRawError Address
parseAddressRaw ByteString
bytes
| 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)
[ Word8
0x00 Word8
-> ExceptT ParseAddressRawError Get Address
-> TaggedDecoderM (ExceptT ParseAddressRawError) Address
forall {k} (t :: (* -> *) -> k -> *) (a :: k).
Word8 -> t Get a -> TaggedDecoderM t a
##: 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
, Word8
0x01 Word8
-> ExceptT ParseAddressRawError Get Address
-> TaggedDecoderM (ExceptT ParseAddressRawError) Address
forall {k} (t :: (* -> *) -> k -> *) (a :: k).
Word8 -> t Get a -> TaggedDecoderM t a
##: 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
, Word8
0x02 Word8
-> ExceptT ParseAddressRawError Get Address
-> TaggedDecoderM (ExceptT ParseAddressRawError) Address
forall {k} (t :: (* -> *) -> k -> *) (a :: k).
Word8 -> t Get a -> TaggedDecoderM t a
##: KindedAddress 'AddressKindTxRollup -> Address
forall (kind :: AddressKind). KindedAddress kind -> Address
MkAddress (KindedAddress 'AddressKindTxRollup -> Address)
-> (TxRollupHash -> KindedAddress 'AddressKindTxRollup)
-> TxRollupHash
-> Address
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TxRollupHash -> KindedAddress 'AddressKindTxRollup
TxRollupAddress (TxRollupHash -> Address)
-> ExceptT ParseAddressRawError Get TxRollupHash
-> ExceptT ParseAddressRawError Get Address
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> HashTag 'HashKindTxRollup
-> ExceptT ParseAddressRawError Get TxRollupHash
forall (kind :: HashKind).
HashTag kind -> ExceptT ParseAddressRawError Get (Hash kind)
sepHash HashTag 'HashKindTxRollup
HashTXR
]
where
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
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
instance SingI kind => HasCLReader (KindedAddress kind) where
getReader :: ReadM (KindedAddress kind)
getReader = (String -> Either String (KindedAddress kind))
-> ReadM (KindedAddress kind)
forall a. (String -> Either String a) -> ReadM a
eitherReader String -> Either String (KindedAddress kind)
forall {b} {kind :: AddressKind} {a}.
(Monoid b, IsString b, FromBuilder b, SingI kind, ToText a) =>
a -> Either b (KindedAddress kind)
parseAddrDo
where
parseAddrDo :: a -> Either b (KindedAddress kind)
parseAddrDo a
addr =
(ParseAddressError -> b)
-> Either ParseAddressError (KindedAddress kind)
-> Either b (KindedAddress kind)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (b -> b -> b
forall a. Monoid a => a -> a -> a
mappend b
"Failed to parse address: " (b -> b) -> (ParseAddressError -> b) -> ParseAddressError -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ParseAddressError -> b
forall a b. (Buildable a, FromBuilder b) => a -> b
pretty) (Either ParseAddressError (KindedAddress kind)
-> Either b (KindedAddress kind))
-> Either ParseAddressError (KindedAddress kind)
-> Either b (KindedAddress kind)
forall a b. (a -> b) -> a -> b
$
Text -> Either ParseAddressError (KindedAddress kind)
forall (kind :: AddressKind).
SingI kind =>
Text -> Either ParseAddressError (KindedAddress kind)
parseKindedAddress (Text -> Either ParseAddressError (KindedAddress kind))
-> Text -> Either ParseAddressError (KindedAddress kind)
forall a b. (a -> b) -> a -> b
$ a -> Text
forall a. ToText a => a -> Text
toText a
addr
getMetavar :: String
getMetavar = String
"ADDRESS"
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)