{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeFamilies #-}

{- HLINT ignore "Avoid lambda using `infix`" -}

-- | Cardano addresses: payment and stake addresses.
--
module Cardano.Api.Address (
    -- * Payment addresses
    -- | Constructing and inspecting normal payment addresses
    Address(..),

    -- ** Byron addresses
    ByronAddr,
    makeByronAddress,

    -- ** Shelley addresses
    ShelleyAddr,
    makeShelleyAddress,
    PaymentCredential(..),
    StakeAddressReference(..),
    StakeAddressPointer(..),

    -- ** Addresses in any era
    AddressAny(..),
    lexPlausibleAddressString,
    parseAddressAny,

    -- ** Addresses in specific eras
    AddressInEra(..),
    AddressTypeInEra(..),
    byronAddressInEra,
    shelleyAddressInEra,
    anyAddressInShelleyBasedEra,
    anyAddressInEra,
    toAddressAny,
    makeByronAddressInEra,
    makeShelleyAddressInEra,

    -- * Stake addresses
    -- | Constructing and inspecting stake addresses
    StakeAddress(..),
    StakeCredential(..),
    makeStakeAddress,
    stakeAddressCredential,
    StakeKey,
    StakeExtendedKey,

    -- * Conversion functions
    shelleyPayAddrToPlutusPubKHash,

    -- * Internal conversion functions
    toShelleyAddr,
    toShelleyStakeAddr,
    toShelleyStakeCredential,
    fromShelleyAddr,
    fromShelleyAddrIsSbe,
    fromShelleyAddrToAny,
    fromShelleyPaymentCredential,
    fromShelleyStakeAddr,
    fromShelleyStakeCredential,
    fromShelleyStakeReference,

    -- * Serialising addresses
    SerialiseAddress(..),

    -- * Data family instances
    AsType(AsByronAddr, AsShelleyAddr, AsByronAddress, AsShelleyAddress,
           AsAddress, AsAddressAny, AsAddressInEra, AsStakeAddress),

    -- * Helpers
    isKeyAddress
  ) where

import           Control.Applicative ((<|>))
import           Data.Aeson (FromJSON (..), ToJSON (..), withText, (.=))
import qualified Data.Aeson as Aeson
import           Data.Bifunctor (first)
import qualified Data.ByteString.Base58 as Base58
import           Data.Char (isAsciiLower, isAsciiUpper, isDigit)
import           Data.Either.Combinators (rightToMaybe)
import           Data.Text (Text)
import qualified Data.Text as Text
import qualified Data.Text.Encoding as Text
import           Data.Typeable (Typeable)
import qualified Text.Parsec as Parsec
import qualified Text.Parsec.String as Parsec

import qualified Cardano.Chain.Common as Byron
import qualified Cardano.Ledger.Address as Shelley
import qualified Cardano.Ledger.Alonzo.TxInfo as Alonzo
import qualified Cardano.Ledger.BaseTypes as Shelley
import qualified Cardano.Ledger.Credential as Shelley
import           Cardano.Ledger.Crypto (StandardCrypto)
import qualified PlutusLedgerApi.V1 as Plutus

import           Cardano.Api.EraCast
import           Cardano.Api.Eras
import           Cardano.Api.Hash
import           Cardano.Api.HasTypeProxy
import           Cardano.Api.Keys.Byron
import           Cardano.Api.Keys.Shelley
import           Cardano.Api.NetworkId
import           Cardano.Api.Script
import           Cardano.Api.SerialiseBech32
import           Cardano.Api.SerialiseRaw
import           Cardano.Api.Utils
import           Control.DeepSeq (NFData (..), deepseq)



-- ----------------------------------------------------------------------------
-- Address Serialisation
--

-- | Address serialisation uses different serialisation formats for different
-- kinds of addresses, so it needs its own class.
--
-- In particular, Byron addresses are typically formatted in base 58, while
-- Shelley addresses (payment and stake) are formatted using Bech32.
--
class HasTypeProxy addr => SerialiseAddress addr where

    serialiseAddress :: addr -> Text

    deserialiseAddress :: AsType addr -> Text -> Maybe addr
    -- TODO: consider adding data AddressDecodeError


-- ----------------------------------------------------------------------------
-- Payment address types
--

-- | A type used as a tag to distinguish Byron addresses.
data ByronAddr

-- | A type used as a tag to distinguish Shelley addresses.
data ShelleyAddr

instance HasTypeProxy ByronAddr where
    data AsType ByronAddr = AsByronAddr
    proxyToAsType :: Proxy ByronAddr -> AsType ByronAddr
proxyToAsType Proxy ByronAddr
_ = AsType ByronAddr
AsByronAddr

instance HasTypeProxy ShelleyAddr where
    data AsType ShelleyAddr = AsShelleyAddr
    proxyToAsType :: Proxy ShelleyAddr -> AsType ShelleyAddr
proxyToAsType Proxy ShelleyAddr
_ = AsType ShelleyAddr
AsShelleyAddr


-- ----------------------------------------------------------------------------
-- Payment addresses
--

-- | Addresses are used as locations where assets live. The address determines
-- the rights needed to spend assets at the address: in particular holding some
-- signing key or being able to satisfy the conditions of a script.
--
-- There are currently two types of address:
--
-- * Byron addresses, which use the type tag 'ByronAddr'; and
-- * Shelley addresses, which use the type tag 'ShelleyAddr'. Notably, Shelley
--   addresses support scripts and stake delegation.
--
-- The /address type/ is subtly from the /ledger era/ in which each
-- address type is valid: while Byron addresses are the only choice in the
-- Byron era, the Shelley era and all subsequent eras support both Byron and
-- Shelley addresses. The 'Address' type param only says the type of the address
-- (either Byron or Shelley). The 'AddressInEra' type connects the address type
-- with the era in which it is supported.
--
data Address addrtype where

     -- | Byron addresses were the only supported address type in the original
     -- Byron era.
     --
     ByronAddress
       :: Byron.Address
       -> Address ByronAddr

     -- | Shelley addresses allow delegation. Shelley addresses were introduced
     -- in Shelley era and are thus supported from the Shelley era onwards
     --
     ShelleyAddress
       :: Shelley.Network
       -> Shelley.PaymentCredential StandardCrypto
       -> Shelley.StakeReference    StandardCrypto
       -> Address ShelleyAddr
       -- Note that the two ledger credential types here are parametrised by
       -- the era, but in fact this is a phantom type parameter and they are
       -- the same for all eras. See 'toShelleyAddr' below.

deriving instance Eq   (Address addrtype)
deriving instance Ord  (Address addrtype)
deriving instance Show (Address addrtype)

instance NFData (Address addrtype) where
  rnf :: Address addrtype -> ()
rnf = \case
    ByronAddress Address
address -> forall a b. NFData a => a -> b -> b
deepseq Address
address ()
    ShelleyAddress Network
n PaymentCredential StandardCrypto
pc StakeReference StandardCrypto
sr -> forall a b. NFData a => a -> b -> b
deepseq (forall a b. NFData a => a -> b -> b
deepseq (forall a b. NFData a => a -> b -> b
deepseq Network
n PaymentCredential StandardCrypto
pc) StakeReference StandardCrypto
sr) ()

instance HasTypeProxy addrtype => HasTypeProxy (Address addrtype) where
    data AsType (Address addrtype) = AsAddress (AsType addrtype)
    proxyToAsType :: Proxy (Address addrtype) -> AsType (Address addrtype)
proxyToAsType Proxy (Address addrtype)
_ = forall addrtype. AsType addrtype -> AsType (Address addrtype)
AsAddress (forall t. HasTypeProxy t => Proxy t -> AsType t
proxyToAsType (forall {k} (t :: k). Proxy t
Proxy :: Proxy addrtype))

pattern AsByronAddress :: AsType (Address ByronAddr)
pattern $bAsByronAddress :: AsType (Address ByronAddr)
$mAsByronAddress :: forall {r}.
AsType (Address ByronAddr) -> ((# #) -> r) -> ((# #) -> r) -> r
AsByronAddress   = AsAddress AsByronAddr
{-# COMPLETE AsByronAddress #-}

pattern AsShelleyAddress :: AsType (Address ShelleyAddr)
pattern $bAsShelleyAddress :: AsType (Address ShelleyAddr)
$mAsShelleyAddress :: forall {r}.
AsType (Address ShelleyAddr) -> ((# #) -> r) -> ((# #) -> r) -> r
AsShelleyAddress = AsAddress AsShelleyAddr
{-# COMPLETE AsShelleyAddress #-}

instance SerialiseAsRawBytes (Address ByronAddr) where
    serialiseToRawBytes :: Address ByronAddr -> ByteString
serialiseToRawBytes (ByronAddress Address
addr) =
        forall crypto. Addr crypto -> ByteString
Shelley.serialiseAddr
      forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall crypto. BootstrapAddress crypto -> Addr crypto
Shelley.AddrBootstrap
      forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall crypto. Address -> BootstrapAddress crypto
Shelley.BootstrapAddress
      forall a b. (a -> b) -> a -> b
$ Address
addr

    deserialiseFromRawBytes :: AsType (Address ByronAddr)
-> ByteString
-> Either SerialiseAsRawBytesError (Address ByronAddr)
deserialiseFromRawBytes (AsAddress AsType ByronAddr
R:AsTypeByronAddr
AsByronAddr) ByteString
bs =
        case forall crypto. Crypto crypto => ByteString -> Maybe (Addr crypto)
Shelley.deserialiseAddr ByteString
bs :: Maybe (Shelley.Addr StandardCrypto) of
          Maybe (Addr StandardCrypto)
Nothing             -> forall a b. a -> Either a b
Left (String -> SerialiseAsRawBytesError
SerialiseAsRawBytesError String
"Unable to deserialise Address ByronAddr")
          Just Shelley.Addr{} -> forall a b. a -> Either a b
Left (String -> SerialiseAsRawBytesError
SerialiseAsRawBytesError String
"Unable to deserialise Address ByronAddr")
          Just (Shelley.AddrBootstrap (Shelley.BootstrapAddress Address
addr)) ->
            forall a b. b -> Either a b
Right (Address -> Address ByronAddr
ByronAddress Address
addr)

instance SerialiseAsRawBytes (Address ShelleyAddr) where
    serialiseToRawBytes :: Address ShelleyAddr -> ByteString
serialiseToRawBytes (ShelleyAddress Network
nw PaymentCredential StandardCrypto
pc StakeReference StandardCrypto
scr) =
        forall crypto. Addr crypto -> ByteString
Shelley.serialiseAddr (forall crypto.
Network
-> PaymentCredential crypto -> StakeReference crypto -> Addr crypto
Shelley.Addr Network
nw PaymentCredential StandardCrypto
pc StakeReference StandardCrypto
scr)

    deserialiseFromRawBytes :: AsType (Address ShelleyAddr)
-> ByteString
-> Either SerialiseAsRawBytesError (Address ShelleyAddr)
deserialiseFromRawBytes (AsAddress AsType ShelleyAddr
R:AsTypeShelleyAddr
AsShelleyAddr) ByteString
bs =
        case forall crypto. Crypto crypto => ByteString -> Maybe (Addr crypto)
Shelley.deserialiseAddr ByteString
bs of
          Maybe (Addr StandardCrypto)
Nothing                       -> forall a b. a -> Either a b
Left (String -> SerialiseAsRawBytesError
SerialiseAsRawBytesError String
"Unable to deserialise bootstrap Address ShelleyAddr")
          Just Shelley.AddrBootstrap{}  -> forall a b. a -> Either a b
Left (String -> SerialiseAsRawBytesError
SerialiseAsRawBytesError String
"Unable to deserialise bootstrap Address ShelleyAddr")
          Just (Shelley.Addr Network
nw PaymentCredential StandardCrypto
pc StakeReference StandardCrypto
scr) -> forall a b. b -> Either a b
Right (Network
-> PaymentCredential StandardCrypto
-> StakeReference StandardCrypto
-> Address ShelleyAddr
ShelleyAddress Network
nw PaymentCredential StandardCrypto
pc StakeReference StandardCrypto
scr)

instance SerialiseAsBech32 (Address ShelleyAddr) where
    bech32PrefixFor :: Address ShelleyAddr -> Text
bech32PrefixFor (ShelleyAddress Network
Shelley.Mainnet PaymentCredential StandardCrypto
_ StakeReference StandardCrypto
_) = Text
"addr"
    bech32PrefixFor (ShelleyAddress Network
Shelley.Testnet PaymentCredential StandardCrypto
_ StakeReference StandardCrypto
_) = Text
"addr_test"

    bech32PrefixesPermitted :: AsType (Address ShelleyAddr) -> [Text]
bech32PrefixesPermitted (AsAddress AsType ShelleyAddr
R:AsTypeShelleyAddr
AsShelleyAddr) = [Text
"addr", Text
"addr_test"]


instance SerialiseAddress (Address ByronAddr) where
    serialiseAddress :: Address ByronAddr -> Text
serialiseAddress addr :: Address ByronAddr
addr@ByronAddress{} =
         ByteString -> Text
Text.decodeLatin1
       forall b c a. (b -> c) -> (a -> b) -> a -> c
. Alphabet -> ByteString -> ByteString
Base58.encodeBase58 Alphabet
Base58.bitcoinAlphabet
       forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. SerialiseAsRawBytes a => a -> ByteString
serialiseToRawBytes
       forall a b. (a -> b) -> a -> b
$ Address ByronAddr
addr

    deserialiseAddress :: AsType (Address ByronAddr) -> Text -> Maybe (Address ByronAddr)
deserialiseAddress (AsAddress AsType ByronAddr
R:AsTypeByronAddr
AsByronAddr) Text
txt = do
      ByteString
bs <- Alphabet -> ByteString -> Maybe ByteString
Base58.decodeBase58 Alphabet
Base58.bitcoinAlphabet (Text -> ByteString
Text.encodeUtf8 Text
txt)
      forall a b. Either a b -> Maybe b
rightToMaybe (forall a.
SerialiseAsRawBytes a =>
AsType a -> ByteString -> Either SerialiseAsRawBytesError a
deserialiseFromRawBytes (forall addrtype. AsType addrtype -> AsType (Address addrtype)
AsAddress AsType ByronAddr
AsByronAddr) ByteString
bs)

instance SerialiseAddress (Address ShelleyAddr) where
    serialiseAddress :: Address ShelleyAddr -> Text
serialiseAddress addr :: Address ShelleyAddr
addr@ShelleyAddress{} =
      forall a. SerialiseAsBech32 a => a -> Text
serialiseToBech32 Address ShelleyAddr
addr

    deserialiseAddress :: AsType (Address ShelleyAddr) -> Text -> Maybe (Address ShelleyAddr)
deserialiseAddress (AsAddress AsType ShelleyAddr
R:AsTypeShelleyAddr
AsShelleyAddr) Text
t =
      forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall a b. a -> b -> a
const forall a. Maybe a
Nothing) forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$
      forall a.
SerialiseAsBech32 a =>
AsType a -> Text -> Either Bech32DecodeError a
deserialiseFromBech32 (forall addrtype. AsType addrtype -> AsType (Address addrtype)
AsAddress AsType ShelleyAddr
AsShelleyAddr) Text
t

instance ToJSON (Address ShelleyAddr) where
    toJSON :: Address ShelleyAddr -> Value
toJSON = Text -> Value
Aeson.String forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall addr. SerialiseAddress addr => addr -> Text
serialiseAddress

instance ToJSON (Address ByronAddr) where
    toJSON :: Address ByronAddr -> Value
toJSON = Text -> Value
Aeson.String forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall addr. SerialiseAddress addr => addr -> Text
serialiseAddress

instance FromJSON (Address ByronAddr) where
    parseJSON :: Value -> Parser (Address ByronAddr)
parseJSON = forall a. String -> (Text -> Parser a) -> Value -> Parser a
Aeson.withText String
"Address" forall a b. (a -> b) -> a -> b
$ \Text
txt ->
      forall b a. b -> (a -> b) -> Maybe a -> b
maybe
        (forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Cardano.Api.Address.FromJSON: Invalid Byron address.")
        forall (f :: * -> *) a. Applicative f => a -> f a
pure
        (forall addr.
SerialiseAddress addr =>
AsType addr -> Text -> Maybe addr
deserialiseAddress AsType (Address ByronAddr)
AsByronAddress Text
txt)

instance FromJSON (Address ShelleyAddr) where
    parseJSON :: Value -> Parser (Address ShelleyAddr)
parseJSON = forall a. String -> (Text -> Parser a) -> Value -> Parser a
Aeson.withText String
"Address" forall a b. (a -> b) -> a -> b
$ \Text
txt ->
      forall b a. b -> (a -> b) -> Maybe a -> b
maybe
        (forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"Cardano.Api.Address.FromJSON: Invalid Shelley address.")
        forall (f :: * -> *) a. Applicative f => a -> f a
pure
        (forall addr.
SerialiseAddress addr =>
AsType addr -> Text -> Maybe addr
deserialiseAddress AsType (Address ShelleyAddr)
AsShelleyAddress Text
txt)

makeByronAddress :: NetworkId
                 -> VerificationKey ByronKey
                 -> Address ByronAddr
makeByronAddress :: NetworkId -> VerificationKey ByronKey -> Address ByronAddr
makeByronAddress NetworkId
nw (ByronVerificationKey VerificationKey
vk) =
    Address -> Address ByronAddr
ByronAddress forall a b. (a -> b) -> a -> b
$
      NetworkMagic -> VerificationKey -> Address
Byron.makeVerKeyAddress
        (NetworkId -> NetworkMagic
toByronNetworkMagic NetworkId
nw)
        VerificationKey
vk


makeShelleyAddress :: NetworkId
                   -> PaymentCredential
                   -> StakeAddressReference
                   -> Address ShelleyAddr
makeShelleyAddress :: NetworkId
-> PaymentCredential
-> StakeAddressReference
-> Address ShelleyAddr
makeShelleyAddress NetworkId
nw PaymentCredential
pc StakeAddressReference
scr =
    Network
-> PaymentCredential StandardCrypto
-> StakeReference StandardCrypto
-> Address ShelleyAddr
ShelleyAddress
      (NetworkId -> Network
toShelleyNetwork NetworkId
nw)
      (PaymentCredential -> PaymentCredential StandardCrypto
toShelleyPaymentCredential PaymentCredential
pc)
      (StakeAddressReference -> StakeReference StandardCrypto
toShelleyStakeReference StakeAddressReference
scr)


-- ----------------------------------------------------------------------------
-- Either type of address
--

-- | Either a Byron address or a Shelley address.
--
-- Sometimes we need to be able to work with either of the two types of
-- address (Byron or Shelley addresses), but without reference to an era in
-- which the address will be used. This type serves that purpose.
--
data AddressAny = AddressByron   !(Address ByronAddr)
                | AddressShelley !(Address ShelleyAddr)
  deriving (AddressAny -> AddressAny -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: AddressAny -> AddressAny -> Bool
$c/= :: AddressAny -> AddressAny -> Bool
== :: AddressAny -> AddressAny -> Bool
$c== :: AddressAny -> AddressAny -> Bool
Eq, Eq AddressAny
AddressAny -> AddressAny -> Bool
AddressAny -> AddressAny -> Ordering
AddressAny -> AddressAny -> AddressAny
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 :: AddressAny -> AddressAny -> AddressAny
$cmin :: AddressAny -> AddressAny -> AddressAny
max :: AddressAny -> AddressAny -> AddressAny
$cmax :: AddressAny -> AddressAny -> AddressAny
>= :: AddressAny -> AddressAny -> Bool
$c>= :: AddressAny -> AddressAny -> Bool
> :: AddressAny -> AddressAny -> Bool
$c> :: AddressAny -> AddressAny -> Bool
<= :: AddressAny -> AddressAny -> Bool
$c<= :: AddressAny -> AddressAny -> Bool
< :: AddressAny -> AddressAny -> Bool
$c< :: AddressAny -> AddressAny -> Bool
compare :: AddressAny -> AddressAny -> Ordering
$ccompare :: AddressAny -> AddressAny -> Ordering
Ord, Int -> AddressAny -> ShowS
[AddressAny] -> ShowS
AddressAny -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [AddressAny] -> ShowS
$cshowList :: [AddressAny] -> ShowS
show :: AddressAny -> String
$cshow :: AddressAny -> String
showsPrec :: Int -> AddressAny -> ShowS
$cshowsPrec :: Int -> AddressAny -> ShowS
Show)

instance HasTypeProxy AddressAny where
    data AsType AddressAny = AsAddressAny
    proxyToAsType :: Proxy AddressAny -> AsType AddressAny
proxyToAsType Proxy AddressAny
_ = AsType AddressAny
AsAddressAny

instance SerialiseAsRawBytes AddressAny where
    serialiseToRawBytes :: AddressAny -> ByteString
serialiseToRawBytes (AddressByron   Address ByronAddr
addr) = forall a. SerialiseAsRawBytes a => a -> ByteString
serialiseToRawBytes Address ByronAddr
addr
    serialiseToRawBytes (AddressShelley Address ShelleyAddr
addr) = forall a. SerialiseAsRawBytes a => a -> ByteString
serialiseToRawBytes Address ShelleyAddr
addr

    deserialiseFromRawBytes :: AsType AddressAny
-> ByteString -> Either SerialiseAsRawBytesError AddressAny
deserialiseFromRawBytes AsType AddressAny
R:AsTypeAddressAny
AsAddressAny ByteString
bs =
      case forall crypto. Crypto crypto => ByteString -> Maybe (Addr crypto)
Shelley.deserialiseAddr ByteString
bs of
        Maybe (Addr StandardCrypto)
Nothing -> forall a b. a -> Either a b
Left (String -> SerialiseAsRawBytesError
SerialiseAsRawBytesError String
"Unable to deserialise AddressAny")
        Just (Shelley.AddrBootstrap (Shelley.BootstrapAddress Address
addr)) ->
          forall a b. b -> Either a b
Right (Address ByronAddr -> AddressAny
AddressByron (Address -> Address ByronAddr
ByronAddress Address
addr))

        Just (Shelley.Addr Network
nw PaymentCredential StandardCrypto
pc StakeReference StandardCrypto
scr) ->
          forall a b. b -> Either a b
Right (Address ShelleyAddr -> AddressAny
AddressShelley (Network
-> PaymentCredential StandardCrypto
-> StakeReference StandardCrypto
-> Address ShelleyAddr
ShelleyAddress Network
nw PaymentCredential StandardCrypto
pc StakeReference StandardCrypto
scr))

instance SerialiseAddress AddressAny where
    serialiseAddress :: AddressAny -> Text
serialiseAddress (AddressByron   Address ByronAddr
addr) = forall addr. SerialiseAddress addr => addr -> Text
serialiseAddress Address ByronAddr
addr
    serialiseAddress (AddressShelley Address ShelleyAddr
addr) = forall addr. SerialiseAddress addr => addr -> Text
serialiseAddress Address ShelleyAddr
addr

    deserialiseAddress :: AsType AddressAny -> Text -> Maybe AddressAny
deserialiseAddress AsType AddressAny
R:AsTypeAddressAny
AsAddressAny Text
t =
          (Address ByronAddr -> AddressAny
AddressByron   forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall addr.
SerialiseAddress addr =>
AsType addr -> Text -> Maybe addr
deserialiseAddress (forall addrtype. AsType addrtype -> AsType (Address addrtype)
AsAddress AsType ByronAddr
AsByronAddr)   Text
t)
      forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> (Address ShelleyAddr -> AddressAny
AddressShelley forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall addr.
SerialiseAddress addr =>
AsType addr -> Text -> Maybe addr
deserialiseAddress (forall addrtype. AsType addrtype -> AsType (Address addrtype)
AsAddress AsType ShelleyAddr
AsShelleyAddr) Text
t)


fromShelleyAddrToAny :: Shelley.Addr StandardCrypto -> AddressAny
fromShelleyAddrToAny :: Addr StandardCrypto -> AddressAny
fromShelleyAddrToAny (Shelley.AddrBootstrap (Shelley.BootstrapAddress Address
addr)) =
  Address ByronAddr -> AddressAny
AddressByron forall a b. (a -> b) -> a -> b
$ Address -> Address ByronAddr
ByronAddress Address
addr
fromShelleyAddrToAny (Shelley.Addr Network
nw PaymentCredential StandardCrypto
pc StakeReference StandardCrypto
scr) =
  Address ShelleyAddr -> AddressAny
AddressShelley forall a b. (a -> b) -> a -> b
$ Network
-> PaymentCredential StandardCrypto
-> StakeReference StandardCrypto
-> Address ShelleyAddr
ShelleyAddress Network
nw PaymentCredential StandardCrypto
pc StakeReference StandardCrypto
scr

-- ----------------------------------------------------------------------------
-- Addresses in the context of a ledger era
--

-- | An 'Address' that can be used in a particular ledger era.
--
-- All current ledger eras support Byron addresses. Shelley addresses are
-- supported in the 'ShelleyEra' and later eras.
--
data AddressInEra era where
     AddressInEra :: AddressTypeInEra addrtype era
                  -> Address addrtype
                  -> AddressInEra era

instance NFData (AddressInEra era) where
  rnf :: AddressInEra era -> ()
rnf (AddressInEra AddressTypeInEra addrtype era
t Address addrtype
a) = forall a b. NFData a => a -> b -> b
deepseq (forall a b. NFData a => a -> b -> b
deepseq AddressTypeInEra addrtype era
t Address addrtype
a) ()

instance IsCardanoEra era => ToJSON (AddressInEra era) where
  toJSON :: AddressInEra era -> Value
toJSON = Text -> Value
Aeson.String forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall addr. SerialiseAddress addr => addr -> Text
serialiseAddress

instance IsShelleyBasedEra era => FromJSON (AddressInEra era) where
  parseJSON :: Value -> Parser (AddressInEra era)
parseJSON = forall a. String -> (Text -> Parser a) -> Value -> Parser a
withText String
"AddressInEra" forall a b. (a -> b) -> a -> b
$ \Text
txt -> do
    AddressAny
addressAny <- forall a. Parser a -> Text -> Parser a
runParsecParser Parser AddressAny
parseAddressAny Text
txt
    forall (f :: * -> *) a. Applicative f => a -> f a
pure forall a b. (a -> b) -> a -> b
$ forall era. IsShelleyBasedEra era => AddressAny -> AddressInEra era
anyAddressInShelleyBasedEra AddressAny
addressAny

instance EraCast AddressInEra where
  eraCast :: forall fromEra toEra.
(IsCardanoEra fromEra, IsCardanoEra toEra) =>
CardanoEra toEra
-> AddressInEra fromEra -> Either EraCastError (AddressInEra toEra)
eraCast CardanoEra toEra
toEra' (AddressInEra AddressTypeInEra addrtype fromEra
addressTypeInEra Address addrtype
address) = forall addrtype era.
AddressTypeInEra addrtype era
-> Address addrtype -> AddressInEra era
AddressInEra
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (f :: * -> *) fromEra toEra.
(EraCast f, IsCardanoEra fromEra, IsCardanoEra toEra) =>
CardanoEra toEra -> f fromEra -> Either EraCastError (f toEra)
eraCast CardanoEra toEra
toEra' AddressTypeInEra addrtype fromEra
addressTypeInEra
    forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> forall (f :: * -> *) a. Applicative f => a -> f a
pure Address addrtype
address

parseAddressAny :: Parsec.Parser AddressAny
parseAddressAny :: Parser AddressAny
parseAddressAny = do
    Text
str <- Parser Text
lexPlausibleAddressString
    case forall addr.
SerialiseAddress addr =>
AsType addr -> Text -> Maybe addr
deserialiseAddress AsType AddressAny
AsAddressAny Text
str of
      Maybe AddressAny
Nothing   -> forall (m :: * -> *) a. MonadFail m => String -> m a
fail forall a b. (a -> b) -> a -> b
$ String
"invalid address: " forall a. Semigroup a => a -> a -> a
<> Text -> String
Text.unpack Text
str
      Just AddressAny
addr -> forall (f :: * -> *) a. Applicative f => a -> f a
pure AddressAny
addr

lexPlausibleAddressString :: Parsec.Parser Text
lexPlausibleAddressString :: Parser Text
lexPlausibleAddressString =
    String -> Text
Text.pack forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> forall s (m :: * -> *) t u a.
Stream s m t =>
ParsecT s u m a -> ParsecT s u m [a]
Parsec.many1 (forall s (m :: * -> *) u.
Stream s m Char =>
(Char -> Bool) -> ParsecT s u m Char
Parsec.satisfy Char -> Bool
isPlausibleAddressChar)
  where
    -- Covers both base58 and bech32 (with constrained prefixes)
    isPlausibleAddressChar :: Char -> Bool
isPlausibleAddressChar Char
c =
         Char -> Bool
isAsciiLower Char
c
      Bool -> Bool -> Bool
|| Char -> Bool
isAsciiUpper Char
c
      Bool -> Bool -> Bool
|| Char -> Bool
isDigit Char
c
      Bool -> Bool -> Bool
|| Char
c forall a. Eq a => a -> a -> Bool
== Char
'_'

instance Eq (AddressInEra era) where
  == :: AddressInEra era -> AddressInEra era -> Bool
(==) (AddressInEra AddressTypeInEra addrtype era
ByronAddressInAnyEra Address addrtype
addr1)
       (AddressInEra AddressTypeInEra addrtype era
ByronAddressInAnyEra Address addrtype
addr2) = Address addrtype
addr1 forall a. Eq a => a -> a -> Bool
== Address addrtype
addr2

  (==) (AddressInEra ShelleyAddressInEra{} Address addrtype
addr1)
       (AddressInEra ShelleyAddressInEra{} Address addrtype
addr2) = Address addrtype
addr1 forall a. Eq a => a -> a -> Bool
== Address addrtype
addr2

  (==) (AddressInEra AddressTypeInEra addrtype era
ByronAddressInAnyEra Address addrtype
_)
       (AddressInEra ShelleyAddressInEra{} Address addrtype
_) = Bool
False

  (==) (AddressInEra ShelleyAddressInEra{} Address addrtype
_)
       (AddressInEra AddressTypeInEra addrtype era
ByronAddressInAnyEra Address addrtype
_) = Bool
False

instance Ord (AddressInEra era) where
  compare :: AddressInEra era -> AddressInEra era -> Ordering
compare (AddressInEra AddressTypeInEra addrtype era
ByronAddressInAnyEra Address addrtype
addr1)
          (AddressInEra AddressTypeInEra addrtype era
ByronAddressInAnyEra Address addrtype
addr2) = forall a. Ord a => a -> a -> Ordering
compare Address addrtype
addr1 Address addrtype
addr2

  compare (AddressInEra ShelleyAddressInEra{} Address addrtype
addr1)
          (AddressInEra ShelleyAddressInEra{} Address addrtype
addr2) = forall a. Ord a => a -> a -> Ordering
compare Address addrtype
addr1 Address addrtype
addr2

  compare (AddressInEra AddressTypeInEra addrtype era
ByronAddressInAnyEra Address addrtype
_)
          (AddressInEra ShelleyAddressInEra{} Address addrtype
_) = Ordering
LT

  compare (AddressInEra ShelleyAddressInEra{} Address addrtype
_)
          (AddressInEra AddressTypeInEra addrtype era
ByronAddressInAnyEra Address addrtype
_) = Ordering
GT

deriving instance Show (AddressInEra era)

data AddressTypeInEra addrtype era where

     ByronAddressInAnyEra :: AddressTypeInEra ByronAddr era

     ShelleyAddressInEra  :: ShelleyBasedEra era
                          -> AddressTypeInEra ShelleyAddr era

deriving instance Show (AddressTypeInEra addrtype era)

instance NFData (AddressTypeInEra addrtype era) where
  rnf :: AddressTypeInEra addrtype era -> ()
rnf = \case
    AddressTypeInEra addrtype era
ByronAddressInAnyEra -> ()
    ShelleyAddressInEra ShelleyBasedEra era
sbe -> forall a b. NFData a => a -> b -> b
deepseq ShelleyBasedEra era
sbe ()

instance HasTypeProxy era => HasTypeProxy (AddressInEra era) where
    data AsType (AddressInEra era) = AsAddressInEra (AsType era)
    proxyToAsType :: Proxy (AddressInEra era) -> AsType (AddressInEra era)
proxyToAsType Proxy (AddressInEra era)
_ = forall era. AsType era -> AsType (AddressInEra era)
AsAddressInEra (forall t. HasTypeProxy t => Proxy t -> AsType t
proxyToAsType (forall {k} (t :: k). Proxy t
Proxy :: Proxy era))

instance (IsCardanoEra era, Typeable era) => SerialiseAsRawBytes (AddressInEra era) where

    serialiseToRawBytes :: AddressInEra era -> ByteString
serialiseToRawBytes (AddressInEra AddressTypeInEra addrtype era
ByronAddressInAnyEra Address addrtype
addr) =
      forall a. SerialiseAsRawBytes a => a -> ByteString
serialiseToRawBytes Address addrtype
addr

    serialiseToRawBytes (AddressInEra ShelleyAddressInEra{} Address addrtype
addr) =
      forall a. SerialiseAsRawBytes a => a -> ByteString
serialiseToRawBytes Address addrtype
addr

    deserialiseFromRawBytes :: AsType (AddressInEra era)
-> ByteString -> Either SerialiseAsRawBytesError (AddressInEra era)
deserialiseFromRawBytes AsType (AddressInEra era)
_ ByteString
bs =
      forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first (forall a b. a -> b -> a
const (String -> SerialiseAsRawBytesError
SerialiseAsRawBytesError String
"Unable to deserialise AddressInEra era")) forall a b. (a -> b) -> a -> b
$
        forall era.
CardanoEra era -> AddressAny -> Either String (AddressInEra era)
anyAddressInEra forall era. IsCardanoEra era => CardanoEra era
cardanoEra forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first SerialiseAsRawBytesError -> String
unSerialiseAsRawBytesError (forall a.
SerialiseAsRawBytes a =>
AsType a -> ByteString -> Either SerialiseAsRawBytesError a
deserialiseFromRawBytes AsType AddressAny
AsAddressAny ByteString
bs)

instance IsCardanoEra era => SerialiseAddress (AddressInEra era) where
    serialiseAddress :: AddressInEra era -> Text
serialiseAddress (AddressInEra AddressTypeInEra addrtype era
ByronAddressInAnyEra Address addrtype
addr) =
      forall addr. SerialiseAddress addr => addr -> Text
serialiseAddress Address addrtype
addr

    serialiseAddress (AddressInEra ShelleyAddressInEra{} Address addrtype
addr) =
      forall addr. SerialiseAddress addr => addr -> Text
serialiseAddress Address addrtype
addr

    deserialiseAddress :: AsType (AddressInEra era) -> Text -> Maybe (AddressInEra era)
deserialiseAddress AsType (AddressInEra era)
_ Text
t =
      forall a b. Either a b -> Maybe b
rightToMaybe forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall era.
CardanoEra era -> AddressAny -> Either String (AddressInEra era)
anyAddressInEra forall era. IsCardanoEra era => CardanoEra era
cardanoEra forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall addr.
SerialiseAddress addr =>
AsType addr -> Text -> Maybe addr
deserialiseAddress AsType AddressAny
AsAddressAny Text
t

instance EraCast (AddressTypeInEra addrtype) where
  eraCast :: forall fromEra toEra.
(IsCardanoEra fromEra, IsCardanoEra toEra) =>
CardanoEra toEra
-> AddressTypeInEra addrtype fromEra
-> Either EraCastError (AddressTypeInEra addrtype toEra)
eraCast CardanoEra toEra
toEra' AddressTypeInEra addrtype fromEra
v = case AddressTypeInEra addrtype fromEra
v of
    AddressTypeInEra addrtype fromEra
ByronAddressInAnyEra -> forall (f :: * -> *) a. Applicative f => a -> f a
pure forall era. AddressTypeInEra ByronAddr era
ByronAddressInAnyEra
    ShelleyAddressInEra ShelleyBasedEra fromEra
previousEra ->
      case forall era. CardanoEra era -> CardanoEraStyle era
cardanoEraStyle CardanoEra toEra
toEra' of
        CardanoEraStyle toEra
LegacyByronEra -> forall a b. a -> Either a b
Left forall a b. (a -> b) -> a -> b
$ forall fromEra toEra value.
(IsCardanoEra fromEra, IsCardanoEra toEra, Show value) =>
value -> CardanoEra fromEra -> CardanoEra toEra -> EraCastError
EraCastError AddressTypeInEra addrtype fromEra
v (forall era. ShelleyBasedEra era -> CardanoEra era
shelleyBasedToCardanoEra ShelleyBasedEra fromEra
previousEra) CardanoEra toEra
toEra'
        ShelleyBasedEra ShelleyBasedEra toEra
newSbe -> forall a b. b -> Either a b
Right forall a b. (a -> b) -> a -> b
$ forall era. ShelleyBasedEra era -> AddressTypeInEra ShelleyAddr era
ShelleyAddressInEra ShelleyBasedEra toEra
newSbe

byronAddressInEra :: Address ByronAddr -> AddressInEra era
byronAddressInEra :: forall era. Address ByronAddr -> AddressInEra era
byronAddressInEra = forall addrtype era.
AddressTypeInEra addrtype era
-> Address addrtype -> AddressInEra era
AddressInEra forall era. AddressTypeInEra ByronAddr era
ByronAddressInAnyEra


shelleyAddressInEra :: IsShelleyBasedEra era
                    => Address ShelleyAddr -> AddressInEra era
shelleyAddressInEra :: forall era.
IsShelleyBasedEra era =>
Address ShelleyAddr -> AddressInEra era
shelleyAddressInEra = forall addrtype era.
AddressTypeInEra addrtype era
-> Address addrtype -> AddressInEra era
AddressInEra (forall era. ShelleyBasedEra era -> AddressTypeInEra ShelleyAddr era
ShelleyAddressInEra forall era. IsShelleyBasedEra era => ShelleyBasedEra era
shelleyBasedEra)


anyAddressInShelleyBasedEra :: IsShelleyBasedEra era
                            => AddressAny -> AddressInEra era
anyAddressInShelleyBasedEra :: forall era. IsShelleyBasedEra era => AddressAny -> AddressInEra era
anyAddressInShelleyBasedEra (AddressByron   Address ByronAddr
addr) = forall era. Address ByronAddr -> AddressInEra era
byronAddressInEra Address ByronAddr
addr
anyAddressInShelleyBasedEra (AddressShelley Address ShelleyAddr
addr) = forall era.
IsShelleyBasedEra era =>
Address ShelleyAddr -> AddressInEra era
shelleyAddressInEra Address ShelleyAddr
addr


anyAddressInEra :: CardanoEra era
                -> AddressAny
                -> Either String (AddressInEra era)
anyAddressInEra :: forall era.
CardanoEra era -> AddressAny -> Either String (AddressInEra era)
anyAddressInEra CardanoEra era
_ (AddressByron Address ByronAddr
addr) =
    forall a b. b -> Either a b
Right (forall addrtype era.
AddressTypeInEra addrtype era
-> Address addrtype -> AddressInEra era
AddressInEra forall era. AddressTypeInEra ByronAddr era
ByronAddressInAnyEra Address ByronAddr
addr)

anyAddressInEra CardanoEra era
era (AddressShelley Address ShelleyAddr
addr) =
    case forall era. CardanoEra era -> CardanoEraStyle era
cardanoEraStyle CardanoEra era
era of
      CardanoEraStyle era
LegacyByronEra       -> forall a b. a -> Either a b
Left String
"Expected Byron based era address"
      ShelleyBasedEra ShelleyBasedEra era
era' -> forall a b. b -> Either a b
Right (forall addrtype era.
AddressTypeInEra addrtype era
-> Address addrtype -> AddressInEra era
AddressInEra (forall era. ShelleyBasedEra era -> AddressTypeInEra ShelleyAddr era
ShelleyAddressInEra ShelleyBasedEra era
era') Address ShelleyAddr
addr)

toAddressAny :: Address addr -> AddressAny
toAddressAny :: forall addr. Address addr -> AddressAny
toAddressAny a :: Address addr
a@ShelleyAddress{} = Address ShelleyAddr -> AddressAny
AddressShelley Address addr
a
toAddressAny a :: Address addr
a@ByronAddress{}   = Address ByronAddr -> AddressAny
AddressByron Address addr
a

makeByronAddressInEra :: NetworkId
                      -> VerificationKey ByronKey
                      -> AddressInEra era
makeByronAddressInEra :: forall era.
NetworkId -> VerificationKey ByronKey -> AddressInEra era
makeByronAddressInEra NetworkId
nw VerificationKey ByronKey
vk =
    forall era. Address ByronAddr -> AddressInEra era
byronAddressInEra (NetworkId -> VerificationKey ByronKey -> Address ByronAddr
makeByronAddress NetworkId
nw VerificationKey ByronKey
vk)


makeShelleyAddressInEra :: IsShelleyBasedEra era
                        => NetworkId
                        -> PaymentCredential
                        -> StakeAddressReference
                        -> AddressInEra era
makeShelleyAddressInEra :: forall era.
IsShelleyBasedEra era =>
NetworkId
-> PaymentCredential -> StakeAddressReference -> AddressInEra era
makeShelleyAddressInEra NetworkId
nw PaymentCredential
pc StakeAddressReference
scr =
    forall era.
IsShelleyBasedEra era =>
Address ShelleyAddr -> AddressInEra era
shelleyAddressInEra (NetworkId
-> PaymentCredential
-> StakeAddressReference
-> Address ShelleyAddr
makeShelleyAddress NetworkId
nw PaymentCredential
pc StakeAddressReference
scr)


-- ----------------------------------------------------------------------------
-- Stake addresses
--

data StakeAddress where

     StakeAddress
       :: Shelley.Network
       -> Shelley.StakeCredential StandardCrypto
       -> StakeAddress
  deriving (StakeAddress -> StakeAddress -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: StakeAddress -> StakeAddress -> Bool
$c/= :: StakeAddress -> StakeAddress -> Bool
== :: StakeAddress -> StakeAddress -> Bool
$c== :: StakeAddress -> StakeAddress -> Bool
Eq, Eq StakeAddress
StakeAddress -> StakeAddress -> Bool
StakeAddress -> StakeAddress -> Ordering
StakeAddress -> StakeAddress -> StakeAddress
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 :: StakeAddress -> StakeAddress -> StakeAddress
$cmin :: StakeAddress -> StakeAddress -> StakeAddress
max :: StakeAddress -> StakeAddress -> StakeAddress
$cmax :: StakeAddress -> StakeAddress -> StakeAddress
>= :: StakeAddress -> StakeAddress -> Bool
$c>= :: StakeAddress -> StakeAddress -> Bool
> :: StakeAddress -> StakeAddress -> Bool
$c> :: StakeAddress -> StakeAddress -> Bool
<= :: StakeAddress -> StakeAddress -> Bool
$c<= :: StakeAddress -> StakeAddress -> Bool
< :: StakeAddress -> StakeAddress -> Bool
$c< :: StakeAddress -> StakeAddress -> Bool
compare :: StakeAddress -> StakeAddress -> Ordering
$ccompare :: StakeAddress -> StakeAddress -> Ordering
Ord, Int -> StakeAddress -> ShowS
[StakeAddress] -> ShowS
StakeAddress -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [StakeAddress] -> ShowS
$cshowList :: [StakeAddress] -> ShowS
show :: StakeAddress -> String
$cshow :: StakeAddress -> String
showsPrec :: Int -> StakeAddress -> ShowS
$cshowsPrec :: Int -> StakeAddress -> ShowS
Show)

data PaymentCredential
       = PaymentCredentialByKey    (Hash PaymentKey)
       | PaymentCredentialByScript  ScriptHash
  deriving (PaymentCredential -> PaymentCredential -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: PaymentCredential -> PaymentCredential -> Bool
$c/= :: PaymentCredential -> PaymentCredential -> Bool
== :: PaymentCredential -> PaymentCredential -> Bool
$c== :: PaymentCredential -> PaymentCredential -> Bool
Eq, Eq PaymentCredential
PaymentCredential -> PaymentCredential -> Bool
PaymentCredential -> PaymentCredential -> Ordering
PaymentCredential -> PaymentCredential -> PaymentCredential
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 :: PaymentCredential -> PaymentCredential -> PaymentCredential
$cmin :: PaymentCredential -> PaymentCredential -> PaymentCredential
max :: PaymentCredential -> PaymentCredential -> PaymentCredential
$cmax :: PaymentCredential -> PaymentCredential -> PaymentCredential
>= :: PaymentCredential -> PaymentCredential -> Bool
$c>= :: PaymentCredential -> PaymentCredential -> Bool
> :: PaymentCredential -> PaymentCredential -> Bool
$c> :: PaymentCredential -> PaymentCredential -> Bool
<= :: PaymentCredential -> PaymentCredential -> Bool
$c<= :: PaymentCredential -> PaymentCredential -> Bool
< :: PaymentCredential -> PaymentCredential -> Bool
$c< :: PaymentCredential -> PaymentCredential -> Bool
compare :: PaymentCredential -> PaymentCredential -> Ordering
$ccompare :: PaymentCredential -> PaymentCredential -> Ordering
Ord, Int -> PaymentCredential -> ShowS
[PaymentCredential] -> ShowS
PaymentCredential -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PaymentCredential] -> ShowS
$cshowList :: [PaymentCredential] -> ShowS
show :: PaymentCredential -> String
$cshow :: PaymentCredential -> String
showsPrec :: Int -> PaymentCredential -> ShowS
$cshowsPrec :: Int -> PaymentCredential -> ShowS
Show)

data StakeCredential
       = StakeCredentialByKey    (Hash StakeKey)
       | StakeCredentialByScript  ScriptHash
  deriving (StakeCredential -> StakeCredential -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: StakeCredential -> StakeCredential -> Bool
$c/= :: StakeCredential -> StakeCredential -> Bool
== :: StakeCredential -> StakeCredential -> Bool
$c== :: StakeCredential -> StakeCredential -> Bool
Eq, Eq StakeCredential
StakeCredential -> StakeCredential -> Bool
StakeCredential -> StakeCredential -> Ordering
StakeCredential -> StakeCredential -> StakeCredential
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 :: StakeCredential -> StakeCredential -> StakeCredential
$cmin :: StakeCredential -> StakeCredential -> StakeCredential
max :: StakeCredential -> StakeCredential -> StakeCredential
$cmax :: StakeCredential -> StakeCredential -> StakeCredential
>= :: StakeCredential -> StakeCredential -> Bool
$c>= :: StakeCredential -> StakeCredential -> Bool
> :: StakeCredential -> StakeCredential -> Bool
$c> :: StakeCredential -> StakeCredential -> Bool
<= :: StakeCredential -> StakeCredential -> Bool
$c<= :: StakeCredential -> StakeCredential -> Bool
< :: StakeCredential -> StakeCredential -> Bool
$c< :: StakeCredential -> StakeCredential -> Bool
compare :: StakeCredential -> StakeCredential -> Ordering
$ccompare :: StakeCredential -> StakeCredential -> Ordering
Ord, Int -> StakeCredential -> ShowS
[StakeCredential] -> ShowS
StakeCredential -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [StakeCredential] -> ShowS
$cshowList :: [StakeCredential] -> ShowS
show :: StakeCredential -> String
$cshow :: StakeCredential -> String
showsPrec :: Int -> StakeCredential -> ShowS
$cshowsPrec :: Int -> StakeCredential -> ShowS
Show)

instance ToJSON StakeCredential where
  toJSON :: StakeCredential -> Value
toJSON =
    [Pair] -> Value
Aeson.object
      forall b c a. (b -> c) -> (a -> b) -> a -> c
. \case
        StakeCredentialByKey Hash StakeKey
keyHash ->
          [Key
"stakingKeyHash" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= forall a. SerialiseAsRawBytes a => a -> Text
serialiseToRawBytesHexText Hash StakeKey
keyHash]
        StakeCredentialByScript ScriptHash
scriptHash ->
          [Key
"stakingScriptHash" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
.= forall a. SerialiseAsRawBytes a => a -> Text
serialiseToRawBytesHexText ScriptHash
scriptHash]

data StakeAddressReference
       = StakeAddressByValue   StakeCredential
       | StakeAddressByPointer StakeAddressPointer
       | NoStakeAddress
  deriving (StakeAddressReference -> StakeAddressReference -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: StakeAddressReference -> StakeAddressReference -> Bool
$c/= :: StakeAddressReference -> StakeAddressReference -> Bool
== :: StakeAddressReference -> StakeAddressReference -> Bool
$c== :: StakeAddressReference -> StakeAddressReference -> Bool
Eq, Int -> StakeAddressReference -> ShowS
[StakeAddressReference] -> ShowS
StakeAddressReference -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [StakeAddressReference] -> ShowS
$cshowList :: [StakeAddressReference] -> ShowS
show :: StakeAddressReference -> String
$cshow :: StakeAddressReference -> String
showsPrec :: Int -> StakeAddressReference -> ShowS
$cshowsPrec :: Int -> StakeAddressReference -> ShowS
Show)

newtype StakeAddressPointer = StakeAddressPointer
  { StakeAddressPointer -> Ptr
unStakeAddressPointer :: Shelley.Ptr
  }
  deriving (StakeAddressPointer -> StakeAddressPointer -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: StakeAddressPointer -> StakeAddressPointer -> Bool
$c/= :: StakeAddressPointer -> StakeAddressPointer -> Bool
== :: StakeAddressPointer -> StakeAddressPointer -> Bool
$c== :: StakeAddressPointer -> StakeAddressPointer -> Bool
Eq, Int -> StakeAddressPointer -> ShowS
[StakeAddressPointer] -> ShowS
StakeAddressPointer -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [StakeAddressPointer] -> ShowS
$cshowList :: [StakeAddressPointer] -> ShowS
show :: StakeAddressPointer -> String
$cshow :: StakeAddressPointer -> String
showsPrec :: Int -> StakeAddressPointer -> ShowS
$cshowsPrec :: Int -> StakeAddressPointer -> ShowS
Show)

instance HasTypeProxy StakeAddress where
    data AsType StakeAddress = AsStakeAddress
    proxyToAsType :: Proxy StakeAddress -> AsType StakeAddress
proxyToAsType Proxy StakeAddress
_ = AsType StakeAddress
AsStakeAddress


instance SerialiseAsRawBytes StakeAddress where
    serialiseToRawBytes :: StakeAddress -> ByteString
serialiseToRawBytes (StakeAddress Network
nw StakeCredential StandardCrypto
sc) =
        forall crypto. RewardAcnt crypto -> ByteString
Shelley.serialiseRewardAcnt (forall crypto.
Network -> Credential 'Staking crypto -> RewardAcnt crypto
Shelley.RewardAcnt Network
nw StakeCredential StandardCrypto
sc)

    deserialiseFromRawBytes :: AsType StakeAddress
-> ByteString -> Either SerialiseAsRawBytesError StakeAddress
deserialiseFromRawBytes AsType StakeAddress
R:AsTypeStakeAddress
AsStakeAddress ByteString
bs =
        case forall crypto.
Crypto crypto =>
ByteString -> Maybe (RewardAcnt crypto)
Shelley.deserialiseRewardAcnt ByteString
bs of
          Maybe (RewardAcnt StandardCrypto)
Nothing -> forall a b. a -> Either a b
Left (String -> SerialiseAsRawBytesError
SerialiseAsRawBytesError String
"Unable to deserialise StakeAddress")
          Just (Shelley.RewardAcnt Network
nw StakeCredential StandardCrypto
sc) -> forall a b. b -> Either a b
Right (Network -> StakeCredential StandardCrypto -> StakeAddress
StakeAddress Network
nw StakeCredential StandardCrypto
sc)


instance SerialiseAsBech32 StakeAddress where
    bech32PrefixFor :: StakeAddress -> Text
bech32PrefixFor (StakeAddress Network
Shelley.Mainnet StakeCredential StandardCrypto
_) = Text
"stake"
    bech32PrefixFor (StakeAddress Network
Shelley.Testnet StakeCredential StandardCrypto
_) = Text
"stake_test"

    bech32PrefixesPermitted :: AsType StakeAddress -> [Text]
bech32PrefixesPermitted AsType StakeAddress
R:AsTypeStakeAddress
AsStakeAddress = [Text
"stake", Text
"stake_test"]


instance SerialiseAddress StakeAddress where
    serialiseAddress :: StakeAddress -> Text
serialiseAddress addr :: StakeAddress
addr@StakeAddress{} =
      forall a. SerialiseAsBech32 a => a -> Text
serialiseToBech32 StakeAddress
addr

    deserialiseAddress :: AsType StakeAddress -> Text -> Maybe StakeAddress
deserialiseAddress AsType StakeAddress
R:AsTypeStakeAddress
AsStakeAddress Text
t =
      forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (forall a b. a -> b -> a
const forall a. Maybe a
Nothing) forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$
      forall a.
SerialiseAsBech32 a =>
AsType a -> Text -> Either Bech32DecodeError a
deserialiseFromBech32 AsType StakeAddress
AsStakeAddress Text
t

instance ToJSON StakeAddress where
  toJSON :: StakeAddress -> Value
toJSON StakeAddress
s = Text -> Value
Aeson.String forall a b. (a -> b) -> a -> b
$ forall addr. SerialiseAddress addr => addr -> Text
serialiseAddress StakeAddress
s

instance FromJSON StakeAddress where
  parseJSON :: Value -> Parser StakeAddress
parseJSON = forall a. String -> (Text -> Parser a) -> Value -> Parser a
withText String
"StakeAddress" forall a b. (a -> b) -> a -> b
$ \Text
str ->
    case forall addr.
SerialiseAddress addr =>
AsType addr -> Text -> Maybe addr
deserialiseAddress AsType StakeAddress
AsStakeAddress Text
str of
      Maybe StakeAddress
Nothing ->
        forall (m :: * -> *) a. MonadFail m => String -> m a
fail forall a b. (a -> b) -> a -> b
$ String
"Error while deserialising StakeAddress: " forall a. Semigroup a => a -> a -> a
<> Text -> String
Text.unpack Text
str
      Just StakeAddress
sAddr -> forall (f :: * -> *) a. Applicative f => a -> f a
pure StakeAddress
sAddr

makeStakeAddress :: NetworkId
                 -> StakeCredential
                 -> StakeAddress
makeStakeAddress :: NetworkId -> StakeCredential -> StakeAddress
makeStakeAddress NetworkId
nw StakeCredential
sc =
    Network -> StakeCredential StandardCrypto -> StakeAddress
StakeAddress
      (NetworkId -> Network
toShelleyNetwork NetworkId
nw)
      (StakeCredential -> StakeCredential StandardCrypto
toShelleyStakeCredential StakeCredential
sc)

-- ----------------------------------------------------------------------------
-- Helpers
--

-- | Is the UTxO at the address only spendable via a key witness.
isKeyAddress :: AddressInEra era -> Bool
isKeyAddress :: forall era. AddressInEra era -> Bool
isKeyAddress (AddressInEra AddressTypeInEra addrtype era
ByronAddressInAnyEra Address addrtype
_) = Bool
True
isKeyAddress (AddressInEra (ShelleyAddressInEra ShelleyBasedEra era
_) (ShelleyAddress Network
_ PaymentCredential StandardCrypto
pCred StakeReference StandardCrypto
_)) =
  case PaymentCredential StandardCrypto -> PaymentCredential
fromShelleyPaymentCredential PaymentCredential StandardCrypto
pCred of
    PaymentCredentialByKey Hash PaymentKey
_ -> Bool
True
    PaymentCredentialByScript ScriptHash
_ -> Bool
False

-- | Converts a Shelley payment address to a Plutus public key hash.
shelleyPayAddrToPlutusPubKHash :: Address ShelleyAddr -> Maybe Plutus.PubKeyHash
shelleyPayAddrToPlutusPubKHash :: Address ShelleyAddr -> Maybe PubKeyHash
shelleyPayAddrToPlutusPubKHash (ShelleyAddress Network
_ PaymentCredential StandardCrypto
payCred StakeReference StandardCrypto
_) =
  case PaymentCredential StandardCrypto
payCred of
    Shelley.ScriptHashObj ScriptHash StandardCrypto
_ -> forall a. Maybe a
Nothing
    Shelley.KeyHashObj KeyHash 'Payment StandardCrypto
kHash -> forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ forall (d :: KeyRole) c. KeyHash d c -> PubKeyHash
Alonzo.transKeyHash KeyHash 'Payment StandardCrypto
kHash

-- ----------------------------------------------------------------------------
-- Internal conversion functions
--

toShelleyAddr :: AddressInEra era -> Shelley.Addr StandardCrypto
toShelleyAddr :: forall era. AddressInEra era -> Addr StandardCrypto
toShelleyAddr (AddressInEra AddressTypeInEra addrtype era
ByronAddressInAnyEra (ByronAddress Address
addr)) =
    forall crypto. BootstrapAddress crypto -> Addr crypto
Shelley.AddrBootstrap (forall crypto. Address -> BootstrapAddress crypto
Shelley.BootstrapAddress Address
addr)
toShelleyAddr (AddressInEra (ShelleyAddressInEra ShelleyBasedEra era
_)
                            (ShelleyAddress Network
nw PaymentCredential StandardCrypto
pc StakeReference StandardCrypto
scr)) =
    forall crypto.
Network
-> PaymentCredential crypto -> StakeReference crypto -> Addr crypto
Shelley.Addr Network
nw PaymentCredential StandardCrypto
pc StakeReference StandardCrypto
scr

toShelleyStakeAddr :: StakeAddress -> Shelley.RewardAcnt StandardCrypto
toShelleyStakeAddr :: StakeAddress -> RewardAcnt StandardCrypto
toShelleyStakeAddr (StakeAddress Network
nw StakeCredential StandardCrypto
sc) =
    Shelley.RewardAcnt {
      getRwdNetwork :: Network
Shelley.getRwdNetwork = Network
nw,
      getRwdCred :: StakeCredential StandardCrypto
Shelley.getRwdCred    = StakeCredential StandardCrypto
sc
    }

toShelleyPaymentCredential :: PaymentCredential
                           -> Shelley.PaymentCredential StandardCrypto
toShelleyPaymentCredential :: PaymentCredential -> PaymentCredential StandardCrypto
toShelleyPaymentCredential (PaymentCredentialByKey (PaymentKeyHash KeyHash 'Payment StandardCrypto
kh)) =
    forall (kr :: KeyRole) crypto.
KeyHash kr crypto -> Credential kr crypto
Shelley.KeyHashObj KeyHash 'Payment StandardCrypto
kh
toShelleyPaymentCredential (PaymentCredentialByScript ScriptHash
sh) =
    forall (kr :: KeyRole) crypto.
ScriptHash crypto -> Credential kr crypto
Shelley.ScriptHashObj (ScriptHash -> ScriptHash StandardCrypto
toShelleyScriptHash ScriptHash
sh)

toShelleyStakeCredential :: StakeCredential
                         -> Shelley.StakeCredential StandardCrypto
toShelleyStakeCredential :: StakeCredential -> StakeCredential StandardCrypto
toShelleyStakeCredential (StakeCredentialByKey (StakeKeyHash KeyHash 'Staking StandardCrypto
kh)) =
    forall (kr :: KeyRole) crypto.
KeyHash kr crypto -> Credential kr crypto
Shelley.KeyHashObj KeyHash 'Staking StandardCrypto
kh
toShelleyStakeCredential (StakeCredentialByScript ScriptHash
sh) =
    forall (kr :: KeyRole) crypto.
ScriptHash crypto -> Credential kr crypto
Shelley.ScriptHashObj (ScriptHash -> ScriptHash StandardCrypto
toShelleyScriptHash ScriptHash
sh)

toShelleyStakeReference :: StakeAddressReference
                        -> Shelley.StakeReference StandardCrypto
toShelleyStakeReference :: StakeAddressReference -> StakeReference StandardCrypto
toShelleyStakeReference (StakeAddressByValue StakeCredential
stakecred) =
    forall crypto. StakeCredential crypto -> StakeReference crypto
Shelley.StakeRefBase (StakeCredential -> StakeCredential StandardCrypto
toShelleyStakeCredential StakeCredential
stakecred)
toShelleyStakeReference (StakeAddressByPointer StakeAddressPointer
ptr) =
    forall crypto. Ptr -> StakeReference crypto
Shelley.StakeRefPtr (StakeAddressPointer -> Ptr
unStakeAddressPointer StakeAddressPointer
ptr)
toShelleyStakeReference  StakeAddressReference
NoStakeAddress =
    forall crypto. StakeReference crypto
Shelley.StakeRefNull

fromShelleyAddrIsSbe :: IsShelleyBasedEra era
                     => Shelley.Addr StandardCrypto -> AddressInEra era
fromShelleyAddrIsSbe :: forall era.
IsShelleyBasedEra era =>
Addr StandardCrypto -> AddressInEra era
fromShelleyAddrIsSbe (Shelley.AddrBootstrap (Shelley.BootstrapAddress Address
addr)) =
  forall addrtype era.
AddressTypeInEra addrtype era
-> Address addrtype -> AddressInEra era
AddressInEra forall era. AddressTypeInEra ByronAddr era
ByronAddressInAnyEra (Address -> Address ByronAddr
ByronAddress Address
addr)

fromShelleyAddrIsSbe (Shelley.Addr Network
nw PaymentCredential StandardCrypto
pc StakeReference StandardCrypto
scr) =
  forall addrtype era.
AddressTypeInEra addrtype era
-> Address addrtype -> AddressInEra era
AddressInEra
    (forall era. ShelleyBasedEra era -> AddressTypeInEra ShelleyAddr era
ShelleyAddressInEra forall era. IsShelleyBasedEra era => ShelleyBasedEra era
shelleyBasedEra)
    (Network
-> PaymentCredential StandardCrypto
-> StakeReference StandardCrypto
-> Address ShelleyAddr
ShelleyAddress Network
nw PaymentCredential StandardCrypto
pc StakeReference StandardCrypto
scr)

fromShelleyAddr
  :: ShelleyBasedEra era
  -> Shelley.Addr StandardCrypto
  -> AddressInEra era
fromShelleyAddr :: forall era.
ShelleyBasedEra era -> Addr StandardCrypto -> AddressInEra era
fromShelleyAddr ShelleyBasedEra era
_ (Shelley.AddrBootstrap (Shelley.BootstrapAddress Address
addr)) =
    forall addrtype era.
AddressTypeInEra addrtype era
-> Address addrtype -> AddressInEra era
AddressInEra forall era. AddressTypeInEra ByronAddr era
ByronAddressInAnyEra (Address -> Address ByronAddr
ByronAddress Address
addr)

fromShelleyAddr ShelleyBasedEra era
sBasedEra (Shelley.Addr Network
nw PaymentCredential StandardCrypto
pc StakeReference StandardCrypto
scr) =
    forall addrtype era.
AddressTypeInEra addrtype era
-> Address addrtype -> AddressInEra era
AddressInEra
      (forall era. ShelleyBasedEra era -> AddressTypeInEra ShelleyAddr era
ShelleyAddressInEra ShelleyBasedEra era
sBasedEra)
      (Network
-> PaymentCredential StandardCrypto
-> StakeReference StandardCrypto
-> Address ShelleyAddr
ShelleyAddress Network
nw PaymentCredential StandardCrypto
pc StakeReference StandardCrypto
scr)

fromShelleyStakeAddr :: Shelley.RewardAcnt StandardCrypto -> StakeAddress
fromShelleyStakeAddr :: RewardAcnt StandardCrypto -> StakeAddress
fromShelleyStakeAddr (Shelley.RewardAcnt Network
nw StakeCredential StandardCrypto
sc) = Network -> StakeCredential StandardCrypto -> StakeAddress
StakeAddress Network
nw StakeCredential StandardCrypto
sc

fromShelleyStakeCredential :: Shelley.StakeCredential StandardCrypto
                           -> StakeCredential
fromShelleyStakeCredential :: StakeCredential StandardCrypto -> StakeCredential
fromShelleyStakeCredential (Shelley.KeyHashObj KeyHash 'Staking StandardCrypto
kh) =
    Hash StakeKey -> StakeCredential
StakeCredentialByKey (KeyHash 'Staking StandardCrypto -> Hash StakeKey
StakeKeyHash KeyHash 'Staking StandardCrypto
kh)
fromShelleyStakeCredential (Shelley.ScriptHashObj ScriptHash StandardCrypto
sh) =
    ScriptHash -> StakeCredential
StakeCredentialByScript (ScriptHash StandardCrypto -> ScriptHash
fromShelleyScriptHash ScriptHash StandardCrypto
sh)

fromShelleyPaymentCredential :: Shelley.PaymentCredential StandardCrypto
                             -> PaymentCredential
fromShelleyPaymentCredential :: PaymentCredential StandardCrypto -> PaymentCredential
fromShelleyPaymentCredential (Shelley.KeyHashObj KeyHash 'Payment StandardCrypto
kh) =
  Hash PaymentKey -> PaymentCredential
PaymentCredentialByKey (KeyHash 'Payment StandardCrypto -> Hash PaymentKey
PaymentKeyHash KeyHash 'Payment StandardCrypto
kh)
fromShelleyPaymentCredential (Shelley.ScriptHashObj ScriptHash StandardCrypto
sh) =
  ScriptHash -> PaymentCredential
PaymentCredentialByScript (ScriptHash StandardCrypto -> ScriptHash
ScriptHash ScriptHash StandardCrypto
sh)

fromShelleyStakeReference :: Shelley.StakeReference StandardCrypto
                          -> StakeAddressReference
fromShelleyStakeReference :: StakeReference StandardCrypto -> StakeAddressReference
fromShelleyStakeReference (Shelley.StakeRefBase StakeCredential StandardCrypto
stakecred) =
  StakeCredential -> StakeAddressReference
StakeAddressByValue (StakeCredential StandardCrypto -> StakeCredential
fromShelleyStakeCredential StakeCredential StandardCrypto
stakecred)
fromShelleyStakeReference (Shelley.StakeRefPtr Ptr
ptr) =
  StakeAddressPointer -> StakeAddressReference
StakeAddressByPointer (Ptr -> StakeAddressPointer
StakeAddressPointer Ptr
ptr)
fromShelleyStakeReference StakeReference StandardCrypto
Shelley.StakeRefNull =
  StakeAddressReference
NoStakeAddress

-- | Get a stake credential from a stake address.
-- This drops the network information.
stakeAddressCredential :: StakeAddress -> StakeCredential
stakeAddressCredential :: StakeAddress -> StakeCredential
stakeAddressCredential (StakeAddress Network
_ StakeCredential StandardCrypto
scred) = StakeCredential StandardCrypto -> StakeCredential
fromShelleyStakeCredential StakeCredential StandardCrypto
scred