{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}

module Cardano.CLI.Shelley.Run.Address
  ( ShelleyAddressCmdError(..)
  , SomeAddressVerificationKey(..)
  , buildShelleyAddress
  , renderShelleyAddressCmdError
  , runAddressCmd
  , runAddressKeyGen
  , readAddressVerificationKeyTextOrFile
  ) where

import           Cardano.Prelude hiding (putStrLn)

import           System.Console.ANSI

import qualified Data.ByteString.Char8 as BS
import qualified Data.Text as Text
import qualified Data.Text.IO as Text
import qualified System.Console.ANSI as ANSI
import qualified System.IO as IO

import           Control.Monad.Trans.Except.Extra (firstExceptT, newExceptT)

import           Cardano.Api
import           Cardano.Api.Shelley

import           Cardano.CLI.Shelley.Key (InputDecodeError, PaymentVerifier (..),
                   StakeVerifier (..), VerificationKeyTextOrFile,
                   VerificationKeyTextOrFileError (..), readVerificationKeyOrFile,
                   readVerificationKeyTextOrFileAnyOf, renderVerificationKeyTextOrFileError)
import           Cardano.CLI.Shelley.Script
import           Cardano.CLI.Shelley.Parsers (AddressCmd (..), AddressKeyType (..), OutputFile (..))
import           Cardano.CLI.Shelley.Run.Address.Info (ShelleyAddressInfoError, runAddressInfo)
import           Cardano.CLI.Types

data ShelleyAddressCmdError
  = ShelleyAddressCmdAddressInfoError !ShelleyAddressInfoError
  | ShelleyAddressCmdReadKeyFileError !(FileError InputDecodeError)
  | ShelleyAddressCmdReadScriptFileError !(FileError ScriptDecodeError)
  | ShelleyAddressCmdVerificationKeyTextOrFileError !VerificationKeyTextOrFileError
  | ShelleyAddressCmdWriteFileError !(FileError ())
  deriving Int -> ShelleyAddressCmdError -> ShowS
[ShelleyAddressCmdError] -> ShowS
ShelleyAddressCmdError -> String
(Int -> ShelleyAddressCmdError -> ShowS)
-> (ShelleyAddressCmdError -> String)
-> ([ShelleyAddressCmdError] -> ShowS)
-> Show ShelleyAddressCmdError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ShelleyAddressCmdError] -> ShowS
$cshowList :: [ShelleyAddressCmdError] -> ShowS
show :: ShelleyAddressCmdError -> String
$cshow :: ShelleyAddressCmdError -> String
showsPrec :: Int -> ShelleyAddressCmdError -> ShowS
$cshowsPrec :: Int -> ShelleyAddressCmdError -> ShowS
Show

renderShelleyAddressCmdError :: ShelleyAddressCmdError -> Text
renderShelleyAddressCmdError :: ShelleyAddressCmdError -> Text
renderShelleyAddressCmdError ShelleyAddressCmdError
err =
  case ShelleyAddressCmdError
err of
    ShelleyAddressCmdAddressInfoError ShelleyAddressInfoError
addrInfoErr ->
      String -> Text
Text.pack (ShelleyAddressInfoError -> String
forall e. Error e => e -> String
displayError ShelleyAddressInfoError
addrInfoErr)
    ShelleyAddressCmdReadKeyFileError FileError InputDecodeError
fileErr ->
      String -> Text
Text.pack (FileError InputDecodeError -> String
forall e. Error e => e -> String
displayError FileError InputDecodeError
fileErr)
    ShelleyAddressCmdVerificationKeyTextOrFileError VerificationKeyTextOrFileError
vkTextOrFileErr ->
      VerificationKeyTextOrFileError -> Text
renderVerificationKeyTextOrFileError VerificationKeyTextOrFileError
vkTextOrFileErr
    ShelleyAddressCmdReadScriptFileError FileError ScriptDecodeError
fileErr ->
      String -> Text
Text.pack (FileError ScriptDecodeError -> String
forall e. Error e => e -> String
displayError FileError ScriptDecodeError
fileErr)
    ShelleyAddressCmdWriteFileError FileError ()
fileErr -> String -> Text
Text.pack (FileError () -> String
forall e. Error e => e -> String
displayError FileError ()
fileErr)

runAddressCmd :: AddressCmd -> ExceptT ShelleyAddressCmdError IO ()
runAddressCmd :: AddressCmd -> ExceptT ShelleyAddressCmdError IO ()
runAddressCmd AddressCmd
cmd =
  case AddressCmd
cmd of
    AddressKeyGen AddressKeyType
kt VerificationKeyFile
vkf SigningKeyFile
skf -> AddressKeyType
-> VerificationKeyFile
-> SigningKeyFile
-> ExceptT ShelleyAddressCmdError IO ()
runAddressKeyGen AddressKeyType
kt VerificationKeyFile
vkf SigningKeyFile
skf
    AddressKeyHash VerificationKeyTextOrFile
vkf Maybe OutputFile
mOFp -> VerificationKeyTextOrFile
-> Maybe OutputFile -> ExceptT ShelleyAddressCmdError IO ()
runAddressKeyHash VerificationKeyTextOrFile
vkf Maybe OutputFile
mOFp
    AddressBuild PaymentVerifier
paymentVerifier Maybe StakeVerifier
mbStakeVerifier NetworkId
nw Maybe OutputFile
mOutFp -> PaymentVerifier
-> Maybe StakeVerifier
-> NetworkId
-> Maybe OutputFile
-> ExceptT ShelleyAddressCmdError IO ()
runAddressBuild PaymentVerifier
paymentVerifier Maybe StakeVerifier
mbStakeVerifier NetworkId
nw Maybe OutputFile
mOutFp
    AddressBuildMultiSig ScriptFile
sFp NetworkId
nId Maybe OutputFile
mOutFp -> ScriptFile
-> NetworkId
-> Maybe OutputFile
-> ExceptT ShelleyAddressCmdError IO ()
runAddressBuildScript ScriptFile
sFp NetworkId
nId Maybe OutputFile
mOutFp
    AddressInfo Text
txt Maybe OutputFile
mOFp -> (ShelleyAddressInfoError -> ShelleyAddressCmdError)
-> ExceptT ShelleyAddressInfoError IO ()
-> ExceptT ShelleyAddressCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT ShelleyAddressInfoError -> ShelleyAddressCmdError
ShelleyAddressCmdAddressInfoError (ExceptT ShelleyAddressInfoError IO ()
 -> ExceptT ShelleyAddressCmdError IO ())
-> ExceptT ShelleyAddressInfoError IO ()
-> ExceptT ShelleyAddressCmdError IO ()
forall a b. (a -> b) -> a -> b
$ Text -> Maybe OutputFile -> ExceptT ShelleyAddressInfoError IO ()
runAddressInfo Text
txt Maybe OutputFile
mOFp

runAddressKeyGen :: AddressKeyType
                 -> VerificationKeyFile
                 -> SigningKeyFile
                 -> ExceptT ShelleyAddressCmdError IO ()
runAddressKeyGen :: AddressKeyType
-> VerificationKeyFile
-> SigningKeyFile
-> ExceptT ShelleyAddressCmdError IO ()
runAddressKeyGen AddressKeyType
kt (VerificationKeyFile String
vkeyPath) (SigningKeyFile String
skeyPath) =
    case AddressKeyType
kt of
      AddressKeyType
AddressKeyShelley         -> AsType PaymentKey -> ExceptT ShelleyAddressCmdError IO ()
forall keyrole.
Key keyrole =>
AsType keyrole -> ExceptT ShelleyAddressCmdError IO ()
generateAndWriteKeyFiles AsType PaymentKey
AsPaymentKey
      AddressKeyType
AddressKeyShelleyExtended -> AsType PaymentExtendedKey -> ExceptT ShelleyAddressCmdError IO ()
forall keyrole.
Key keyrole =>
AsType keyrole -> ExceptT ShelleyAddressCmdError IO ()
generateAndWriteKeyFiles AsType PaymentExtendedKey
AsPaymentExtendedKey
      AddressKeyType
AddressKeyByron           -> AsType ByronKey -> ExceptT ShelleyAddressCmdError IO ()
forall keyrole.
Key keyrole =>
AsType keyrole -> ExceptT ShelleyAddressCmdError IO ()
generateAndWriteKeyFiles AsType ByronKey
AsByronKey
  where
    generateAndWriteKeyFiles :: AsType keyrole -> ExceptT ShelleyAddressCmdError IO ()
generateAndWriteKeyFiles AsType keyrole
asType = do
      SigningKey keyrole
skey <- IO (SigningKey keyrole)
-> ExceptT ShelleyAddressCmdError IO (SigningKey keyrole)
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (SigningKey keyrole)
 -> ExceptT ShelleyAddressCmdError IO (SigningKey keyrole))
-> IO (SigningKey keyrole)
-> ExceptT ShelleyAddressCmdError IO (SigningKey keyrole)
forall a b. (a -> b) -> a -> b
$ AsType keyrole -> IO (SigningKey keyrole)
forall keyrole.
Key keyrole =>
AsType keyrole -> IO (SigningKey keyrole)
generateSigningKey AsType keyrole
asType
      let vkey :: VerificationKey keyrole
vkey = SigningKey keyrole -> VerificationKey keyrole
forall keyrole.
Key keyrole =>
SigningKey keyrole -> VerificationKey keyrole
getVerificationKey SigningKey keyrole
skey
      (FileError () -> ShelleyAddressCmdError)
-> ExceptT (FileError ()) IO ()
-> ExceptT ShelleyAddressCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError () -> ShelleyAddressCmdError
ShelleyAddressCmdWriteFileError
        (ExceptT (FileError ()) IO ()
 -> ExceptT ShelleyAddressCmdError IO ())
-> (IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ())
-> IO (Either (FileError ()) ())
-> ExceptT ShelleyAddressCmdError IO ()
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ()
forall (m :: * -> *) x a. m (Either x a) -> ExceptT x m a
newExceptT
        (IO (Either (FileError ()) ())
 -> ExceptT ShelleyAddressCmdError IO ())
-> IO (Either (FileError ()) ())
-> ExceptT ShelleyAddressCmdError IO ()
forall a b. (a -> b) -> a -> b
$ String
-> Maybe TextEnvelopeDescr
-> SigningKey keyrole
-> IO (Either (FileError ()) ())
forall a.
HasTextEnvelope a =>
String
-> Maybe TextEnvelopeDescr -> a -> IO (Either (FileError ()) ())
writeFileTextEnvelope String
skeyPath (TextEnvelopeDescr -> Maybe TextEnvelopeDescr
forall a. a -> Maybe a
Just TextEnvelopeDescr
skeyDesc) SigningKey keyrole
skey
      (FileError () -> ShelleyAddressCmdError)
-> ExceptT (FileError ()) IO ()
-> ExceptT ShelleyAddressCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError () -> ShelleyAddressCmdError
ShelleyAddressCmdWriteFileError
        (ExceptT (FileError ()) IO ()
 -> ExceptT ShelleyAddressCmdError IO ())
-> (IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ())
-> IO (Either (FileError ()) ())
-> ExceptT ShelleyAddressCmdError IO ()
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ()
forall (m :: * -> *) x a. m (Either x a) -> ExceptT x m a
newExceptT
        (IO (Either (FileError ()) ())
 -> ExceptT ShelleyAddressCmdError IO ())
-> IO (Either (FileError ()) ())
-> ExceptT ShelleyAddressCmdError IO ()
forall a b. (a -> b) -> a -> b
$ String
-> Maybe TextEnvelopeDescr
-> VerificationKey keyrole
-> IO (Either (FileError ()) ())
forall a.
HasTextEnvelope a =>
String
-> Maybe TextEnvelopeDescr -> a -> IO (Either (FileError ()) ())
writeFileTextEnvelope String
vkeyPath (TextEnvelopeDescr -> Maybe TextEnvelopeDescr
forall a. a -> Maybe a
Just TextEnvelopeDescr
vkeyDesc) VerificationKey keyrole
vkey

    skeyDesc, vkeyDesc :: TextEnvelopeDescr
    skeyDesc :: TextEnvelopeDescr
skeyDesc = TextEnvelopeDescr
"Payment Signing Key"
    vkeyDesc :: TextEnvelopeDescr
vkeyDesc = TextEnvelopeDescr
"Payment Verification Key"


runAddressKeyHash :: VerificationKeyTextOrFile
                  -> Maybe OutputFile
                  -> ExceptT ShelleyAddressCmdError IO ()
runAddressKeyHash :: VerificationKeyTextOrFile
-> Maybe OutputFile -> ExceptT ShelleyAddressCmdError IO ()
runAddressKeyHash VerificationKeyTextOrFile
vkeyTextOrFile Maybe OutputFile
mOutputFp = do
  SomeAddressVerificationKey
vkey <- (VerificationKeyTextOrFileError -> ShelleyAddressCmdError)
-> ExceptT
     VerificationKeyTextOrFileError IO SomeAddressVerificationKey
-> ExceptT ShelleyAddressCmdError IO SomeAddressVerificationKey
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT VerificationKeyTextOrFileError -> ShelleyAddressCmdError
ShelleyAddressCmdVerificationKeyTextOrFileError (ExceptT
   VerificationKeyTextOrFileError IO SomeAddressVerificationKey
 -> ExceptT ShelleyAddressCmdError IO SomeAddressVerificationKey)
-> ExceptT
     VerificationKeyTextOrFileError IO SomeAddressVerificationKey
-> ExceptT ShelleyAddressCmdError IO SomeAddressVerificationKey
forall a b. (a -> b) -> a -> b
$
            VerificationKeyTextOrFile
-> ExceptT
     VerificationKeyTextOrFileError IO SomeAddressVerificationKey
readAddressVerificationKeyTextOrFile VerificationKeyTextOrFile
vkeyTextOrFile

  let hexKeyHash :: ByteString
hexKeyHash = (forall keyrole.
 Key keyrole =>
 VerificationKey keyrole -> ByteString)
-> SomeAddressVerificationKey -> ByteString
forall a.
(forall keyrole. Key keyrole => VerificationKey keyrole -> a)
-> SomeAddressVerificationKey -> a
foldSomeAddressVerificationKey
                     (Hash keyrole -> ByteString
forall a. SerialiseAsRawBytes a => a -> ByteString
serialiseToRawBytesHex (Hash keyrole -> ByteString)
-> (VerificationKey keyrole -> Hash keyrole)
-> VerificationKey keyrole
-> ByteString
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. VerificationKey keyrole -> Hash keyrole
forall keyrole.
Key keyrole =>
VerificationKey keyrole -> Hash keyrole
verificationKeyHash) SomeAddressVerificationKey
vkey

  case Maybe OutputFile
mOutputFp of
    Just (OutputFile String
fpath) -> IO () -> ExceptT ShelleyAddressCmdError IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ExceptT ShelleyAddressCmdError IO ())
-> IO () -> ExceptT ShelleyAddressCmdError IO ()
forall a b. (a -> b) -> a -> b
$ String -> ByteString -> IO ()
BS.writeFile String
fpath ByteString
hexKeyHash
    Maybe OutputFile
Nothing -> IO () -> ExceptT ShelleyAddressCmdError IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ExceptT ShelleyAddressCmdError IO ())
-> IO () -> ExceptT ShelleyAddressCmdError IO ()
forall a b. (a -> b) -> a -> b
$ ByteString -> IO ()
BS.putStrLn ByteString
hexKeyHash


runAddressBuild :: PaymentVerifier
                -> Maybe StakeVerifier
                -> NetworkId
                -> Maybe OutputFile
                -> ExceptT ShelleyAddressCmdError IO ()
runAddressBuild :: PaymentVerifier
-> Maybe StakeVerifier
-> NetworkId
-> Maybe OutputFile
-> ExceptT ShelleyAddressCmdError IO ()
runAddressBuild PaymentVerifier
paymentVerifier Maybe StakeVerifier
mbStakeVerifier NetworkId
nw Maybe OutputFile
mOutFp = do
  Text
outText <- case PaymentVerifier
paymentVerifier of
    PaymentVerifierKey VerificationKeyTextOrFile
payVkeyTextOrFile -> do
      SomeAddressVerificationKey
payVKey <- (VerificationKeyTextOrFileError -> ShelleyAddressCmdError)
-> ExceptT
     VerificationKeyTextOrFileError IO SomeAddressVerificationKey
-> ExceptT ShelleyAddressCmdError IO SomeAddressVerificationKey
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT VerificationKeyTextOrFileError -> ShelleyAddressCmdError
ShelleyAddressCmdVerificationKeyTextOrFileError (ExceptT
   VerificationKeyTextOrFileError IO SomeAddressVerificationKey
 -> ExceptT ShelleyAddressCmdError IO SomeAddressVerificationKey)
-> ExceptT
     VerificationKeyTextOrFileError IO SomeAddressVerificationKey
-> ExceptT ShelleyAddressCmdError IO SomeAddressVerificationKey
forall a b. (a -> b) -> a -> b
$
        VerificationKeyTextOrFile
-> ExceptT
     VerificationKeyTextOrFileError IO SomeAddressVerificationKey
readAddressVerificationKeyTextOrFile VerificationKeyTextOrFile
payVkeyTextOrFile

      AddressAny
addr <- case SomeAddressVerificationKey
payVKey of
        AByronVerificationKey VerificationKey ByronKey
vk ->
          AddressAny -> ExceptT ShelleyAddressCmdError IO AddressAny
forall (m :: * -> *) a. Monad m => a -> m a
return (Address ByronAddr -> AddressAny
AddressByron (NetworkId -> VerificationKey ByronKey -> Address ByronAddr
makeByronAddress NetworkId
nw VerificationKey ByronKey
vk))

        APaymentVerificationKey VerificationKey PaymentKey
vk ->
          Address ShelleyAddr -> AddressAny
AddressShelley (Address ShelleyAddr -> AddressAny)
-> ExceptT ShelleyAddressCmdError IO (Address ShelleyAddr)
-> ExceptT ShelleyAddressCmdError IO AddressAny
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> VerificationKey PaymentKey
-> Maybe StakeVerifier
-> NetworkId
-> ExceptT ShelleyAddressCmdError IO (Address ShelleyAddr)
buildShelleyAddress VerificationKey PaymentKey
vk Maybe StakeVerifier
mbStakeVerifier NetworkId
nw

        APaymentExtendedVerificationKey VerificationKey PaymentExtendedKey
vk ->
          Address ShelleyAddr -> AddressAny
AddressShelley (Address ShelleyAddr -> AddressAny)
-> ExceptT ShelleyAddressCmdError IO (Address ShelleyAddr)
-> ExceptT ShelleyAddressCmdError IO AddressAny
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> VerificationKey PaymentKey
-> Maybe StakeVerifier
-> NetworkId
-> ExceptT ShelleyAddressCmdError IO (Address ShelleyAddr)
buildShelleyAddress (VerificationKey PaymentExtendedKey -> VerificationKey PaymentKey
forall keyroleA keyroleB.
CastVerificationKeyRole keyroleA keyroleB =>
VerificationKey keyroleA -> VerificationKey keyroleB
castVerificationKey VerificationKey PaymentExtendedKey
vk) Maybe StakeVerifier
mbStakeVerifier NetworkId
nw

        AGenesisUTxOVerificationKey VerificationKey GenesisUTxOKey
vk ->
          Address ShelleyAddr -> AddressAny
AddressShelley (Address ShelleyAddr -> AddressAny)
-> ExceptT ShelleyAddressCmdError IO (Address ShelleyAddr)
-> ExceptT ShelleyAddressCmdError IO AddressAny
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> VerificationKey PaymentKey
-> Maybe StakeVerifier
-> NetworkId
-> ExceptT ShelleyAddressCmdError IO (Address ShelleyAddr)
buildShelleyAddress (VerificationKey GenesisUTxOKey -> VerificationKey PaymentKey
forall keyroleA keyroleB.
CastVerificationKeyRole keyroleA keyroleB =>
VerificationKey keyroleA -> VerificationKey keyroleB
castVerificationKey VerificationKey GenesisUTxOKey
vk) Maybe StakeVerifier
mbStakeVerifier NetworkId
nw

      Text -> ExceptT ShelleyAddressCmdError IO Text
forall (m :: * -> *) a. Monad m => a -> m a
return (Text -> ExceptT ShelleyAddressCmdError IO Text)
-> Text -> ExceptT ShelleyAddressCmdError IO Text
forall a b. (a -> b) -> a -> b
$ AddressAny -> Text
forall addr. SerialiseAddress addr => addr -> Text
serialiseAddress (AddressAny
addr :: AddressAny)

    PaymentVerifierScriptFile (ScriptFile String
fp) -> do
      ScriptInAnyLang ScriptLanguage lang
_lang Script lang
script <-
        (FileError ScriptDecodeError -> ShelleyAddressCmdError)
-> ExceptT (FileError ScriptDecodeError) IO ScriptInAnyLang
-> ExceptT ShelleyAddressCmdError IO ScriptInAnyLang
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError ScriptDecodeError -> ShelleyAddressCmdError
ShelleyAddressCmdReadScriptFileError (ExceptT (FileError ScriptDecodeError) IO ScriptInAnyLang
 -> ExceptT ShelleyAddressCmdError IO ScriptInAnyLang)
-> ExceptT (FileError ScriptDecodeError) IO ScriptInAnyLang
-> ExceptT ShelleyAddressCmdError IO ScriptInAnyLang
forall a b. (a -> b) -> a -> b
$
          String -> ExceptT (FileError ScriptDecodeError) IO ScriptInAnyLang
readFileScriptInAnyLang String
fp

      let payCred :: PaymentCredential
payCred = ScriptHash -> PaymentCredential
PaymentCredentialByScript (Script lang -> ScriptHash
forall lang. Script lang -> ScriptHash
hashScript Script lang
script)

      Address ShelleyAddr -> Text
forall addr. SerialiseAddress addr => addr -> Text
serialiseAddress (Address ShelleyAddr -> Text)
-> (StakeAddressReference -> Address ShelleyAddr)
-> StakeAddressReference
-> Text
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. NetworkId
-> PaymentCredential
-> StakeAddressReference
-> Address ShelleyAddr
makeShelleyAddress NetworkId
nw PaymentCredential
payCred (StakeAddressReference -> Text)
-> ExceptT ShelleyAddressCmdError IO StakeAddressReference
-> ExceptT ShelleyAddressCmdError IO Text
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe StakeVerifier
-> ExceptT ShelleyAddressCmdError IO StakeAddressReference
makeStakeAddressRef Maybe StakeVerifier
mbStakeVerifier

  case Maybe OutputFile
mOutFp of
    Just (OutputFile String
fpath) -> IO () -> ExceptT ShelleyAddressCmdError IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ExceptT ShelleyAddressCmdError IO ())
-> IO () -> ExceptT ShelleyAddressCmdError IO ()
forall a b. (a -> b) -> a -> b
$ String -> Text -> IO ()
Text.writeFile String
fpath Text
outText
    Maybe OutputFile
Nothing                 -> IO () -> ExceptT ShelleyAddressCmdError IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> ExceptT ShelleyAddressCmdError IO ())
-> IO () -> ExceptT ShelleyAddressCmdError IO ()
forall a b. (a -> b) -> a -> b
$ Text -> IO ()
Text.putStr          Text
outText

makeStakeAddressRef
  :: Maybe StakeVerifier
  -> ExceptT ShelleyAddressCmdError IO StakeAddressReference
makeStakeAddressRef :: Maybe StakeVerifier
-> ExceptT ShelleyAddressCmdError IO StakeAddressReference
makeStakeAddressRef Maybe StakeVerifier
mbStakeVerifier = do
  case Maybe StakeVerifier
mbStakeVerifier of
    Maybe StakeVerifier
Nothing -> StakeAddressReference
-> ExceptT ShelleyAddressCmdError IO StakeAddressReference
forall (f :: * -> *) a. Applicative f => a -> f a
pure StakeAddressReference
NoStakeAddress
    Just StakeVerifier
stakeVerifier -> case StakeVerifier
stakeVerifier of
      StakeVerifierKey VerificationKeyOrFile StakeKey
stkVkeyOrFile -> do
        Maybe (VerificationKey StakeKey)
mstakeVKey <- (FileError InputDecodeError -> ShelleyAddressCmdError)
-> ExceptT
     (FileError InputDecodeError) IO (Maybe (VerificationKey StakeKey))
-> ExceptT
     ShelleyAddressCmdError IO (Maybe (VerificationKey StakeKey))
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError InputDecodeError -> ShelleyAddressCmdError
ShelleyAddressCmdReadKeyFileError (ExceptT
   (FileError InputDecodeError) IO (Maybe (VerificationKey StakeKey))
 -> ExceptT
      ShelleyAddressCmdError IO (Maybe (VerificationKey StakeKey)))
-> ExceptT
     (FileError InputDecodeError) IO (Maybe (VerificationKey StakeKey))
-> ExceptT
     ShelleyAddressCmdError IO (Maybe (VerificationKey StakeKey))
forall a b. (a -> b) -> a -> b
$
          (VerificationKey StakeKey -> Maybe (VerificationKey StakeKey))
-> ExceptT
     (FileError InputDecodeError) IO (VerificationKey StakeKey)
-> ExceptT
     (FileError InputDecodeError) IO (Maybe (VerificationKey StakeKey))
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap VerificationKey StakeKey -> Maybe (VerificationKey StakeKey)
forall a. a -> Maybe a
Just (ExceptT (FileError InputDecodeError) IO (VerificationKey StakeKey)
 -> ExceptT
      (FileError InputDecodeError) IO (Maybe (VerificationKey StakeKey)))
-> ExceptT
     (FileError InputDecodeError) IO (VerificationKey StakeKey)
-> ExceptT
     (FileError InputDecodeError) IO (Maybe (VerificationKey StakeKey))
forall a b. (a -> b) -> a -> b
$ IO (Either (FileError InputDecodeError) (VerificationKey StakeKey))
-> ExceptT
     (FileError InputDecodeError) IO (VerificationKey StakeKey)
forall (m :: * -> *) x a. m (Either x a) -> ExceptT x m a
newExceptT (IO
   (Either (FileError InputDecodeError) (VerificationKey StakeKey))
 -> ExceptT
      (FileError InputDecodeError) IO (VerificationKey StakeKey))
-> IO
     (Either (FileError InputDecodeError) (VerificationKey StakeKey))
-> ExceptT
     (FileError InputDecodeError) IO (VerificationKey StakeKey)
forall a b. (a -> b) -> a -> b
$ AsType StakeKey
-> VerificationKeyOrFile StakeKey
-> IO
     (Either (FileError InputDecodeError) (VerificationKey StakeKey))
forall keyrole.
(HasTextEnvelope (VerificationKey keyrole),
 SerialiseAsBech32 (VerificationKey keyrole)) =>
AsType keyrole
-> VerificationKeyOrFile keyrole
-> IO
     (Either (FileError InputDecodeError) (VerificationKey keyrole))
readVerificationKeyOrFile AsType StakeKey
AsStakeKey VerificationKeyOrFile StakeKey
stkVkeyOrFile

        StakeAddressReference
-> ExceptT ShelleyAddressCmdError IO StakeAddressReference
forall (m :: * -> *) a. Monad m => a -> m a
return (StakeAddressReference
 -> ExceptT ShelleyAddressCmdError IO StakeAddressReference)
-> StakeAddressReference
-> ExceptT ShelleyAddressCmdError IO StakeAddressReference
forall a b. (a -> b) -> a -> b
$ StakeAddressReference
-> (VerificationKey StakeKey -> StakeAddressReference)
-> Maybe (VerificationKey StakeKey)
-> StakeAddressReference
forall b a. b -> (a -> b) -> Maybe a -> b
maybe StakeAddressReference
NoStakeAddress
          (StakeCredential -> StakeAddressReference
StakeAddressByValue (StakeCredential -> StakeAddressReference)
-> (VerificationKey StakeKey -> StakeCredential)
-> VerificationKey StakeKey
-> StakeAddressReference
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. Hash StakeKey -> StakeCredential
StakeCredentialByKey (Hash StakeKey -> StakeCredential)
-> (VerificationKey StakeKey -> Hash StakeKey)
-> VerificationKey StakeKey
-> StakeCredential
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. VerificationKey StakeKey -> Hash StakeKey
forall keyrole.
Key keyrole =>
VerificationKey keyrole -> Hash keyrole
verificationKeyHash)
          Maybe (VerificationKey StakeKey)
mstakeVKey

      StakeVerifierScriptFile (ScriptFile String
fp) -> do
        ScriptInAnyLang ScriptLanguage lang
_lang Script lang
script <-
          (FileError ScriptDecodeError -> ShelleyAddressCmdError)
-> ExceptT (FileError ScriptDecodeError) IO ScriptInAnyLang
-> ExceptT ShelleyAddressCmdError IO ScriptInAnyLang
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError ScriptDecodeError -> ShelleyAddressCmdError
ShelleyAddressCmdReadScriptFileError (ExceptT (FileError ScriptDecodeError) IO ScriptInAnyLang
 -> ExceptT ShelleyAddressCmdError IO ScriptInAnyLang)
-> ExceptT (FileError ScriptDecodeError) IO ScriptInAnyLang
-> ExceptT ShelleyAddressCmdError IO ScriptInAnyLang
forall a b. (a -> b) -> a -> b
$
            String -> ExceptT (FileError ScriptDecodeError) IO ScriptInAnyLang
readFileScriptInAnyLang String
fp

        let stakeCred :: StakeCredential
stakeCred = ScriptHash -> StakeCredential
StakeCredentialByScript (Script lang -> ScriptHash
forall lang. Script lang -> ScriptHash
hashScript Script lang
script)
        StakeAddressReference
-> ExceptT ShelleyAddressCmdError IO StakeAddressReference
forall (m :: * -> *) a. Monad m => a -> m a
return (StakeCredential -> StakeAddressReference
StakeAddressByValue StakeCredential
stakeCred)

buildShelleyAddress
  :: VerificationKey PaymentKey
  -> Maybe StakeVerifier
  -> NetworkId
  -> ExceptT ShelleyAddressCmdError IO (Address ShelleyAddr)
buildShelleyAddress :: VerificationKey PaymentKey
-> Maybe StakeVerifier
-> NetworkId
-> ExceptT ShelleyAddressCmdError IO (Address ShelleyAddr)
buildShelleyAddress VerificationKey PaymentKey
vkey Maybe StakeVerifier
mbStakeVerifier NetworkId
nw =
  NetworkId
-> PaymentCredential
-> StakeAddressReference
-> Address ShelleyAddr
makeShelleyAddress NetworkId
nw (Hash PaymentKey -> PaymentCredential
PaymentCredentialByKey (VerificationKey PaymentKey -> Hash PaymentKey
forall keyrole.
Key keyrole =>
VerificationKey keyrole -> Hash keyrole
verificationKeyHash VerificationKey PaymentKey
vkey)) (StakeAddressReference -> Address ShelleyAddr)
-> ExceptT ShelleyAddressCmdError IO StakeAddressReference
-> ExceptT ShelleyAddressCmdError IO (Address ShelleyAddr)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Maybe StakeVerifier
-> ExceptT ShelleyAddressCmdError IO StakeAddressReference
makeStakeAddressRef Maybe StakeVerifier
mbStakeVerifier


--
-- Handling the variety of address key types
--

-- TODO: if we could make unions like this an instance of the Key class then
-- it would simplify some of the code above
data SomeAddressVerificationKey
  = AByronVerificationKey           (VerificationKey ByronKey)
  | APaymentVerificationKey         (VerificationKey PaymentKey)
  | APaymentExtendedVerificationKey (VerificationKey PaymentExtendedKey)
  | AGenesisUTxOVerificationKey     (VerificationKey GenesisUTxOKey)
  deriving (Int -> SomeAddressVerificationKey -> ShowS
[SomeAddressVerificationKey] -> ShowS
SomeAddressVerificationKey -> String
(Int -> SomeAddressVerificationKey -> ShowS)
-> (SomeAddressVerificationKey -> String)
-> ([SomeAddressVerificationKey] -> ShowS)
-> Show SomeAddressVerificationKey
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SomeAddressVerificationKey] -> ShowS
$cshowList :: [SomeAddressVerificationKey] -> ShowS
show :: SomeAddressVerificationKey -> String
$cshow :: SomeAddressVerificationKey -> String
showsPrec :: Int -> SomeAddressVerificationKey -> ShowS
$cshowsPrec :: Int -> SomeAddressVerificationKey -> ShowS
Show)

foldSomeAddressVerificationKey :: (forall keyrole. Key keyrole =>
                                   VerificationKey keyrole -> a)
                               -> SomeAddressVerificationKey -> a
foldSomeAddressVerificationKey :: (forall keyrole. Key keyrole => VerificationKey keyrole -> a)
-> SomeAddressVerificationKey -> a
foldSomeAddressVerificationKey forall keyrole. Key keyrole => VerificationKey keyrole -> a
f (AByronVerificationKey           VerificationKey ByronKey
vk) = VerificationKey ByronKey -> a
forall keyrole. Key keyrole => VerificationKey keyrole -> a
f VerificationKey ByronKey
vk
foldSomeAddressVerificationKey forall keyrole. Key keyrole => VerificationKey keyrole -> a
f (APaymentVerificationKey         VerificationKey PaymentKey
vk) = VerificationKey PaymentKey -> a
forall keyrole. Key keyrole => VerificationKey keyrole -> a
f VerificationKey PaymentKey
vk
foldSomeAddressVerificationKey forall keyrole. Key keyrole => VerificationKey keyrole -> a
f (APaymentExtendedVerificationKey VerificationKey PaymentExtendedKey
vk) = VerificationKey PaymentExtendedKey -> a
forall keyrole. Key keyrole => VerificationKey keyrole -> a
f VerificationKey PaymentExtendedKey
vk
foldSomeAddressVerificationKey forall keyrole. Key keyrole => VerificationKey keyrole -> a
f (AGenesisUTxOVerificationKey     VerificationKey GenesisUTxOKey
vk) = VerificationKey GenesisUTxOKey -> a
forall keyrole. Key keyrole => VerificationKey keyrole -> a
f VerificationKey GenesisUTxOKey
vk

readAddressVerificationKeyTextOrFile
  :: VerificationKeyTextOrFile
  -> ExceptT VerificationKeyTextOrFileError IO SomeAddressVerificationKey
readAddressVerificationKeyTextOrFile :: VerificationKeyTextOrFile
-> ExceptT
     VerificationKeyTextOrFileError IO SomeAddressVerificationKey
readAddressVerificationKeyTextOrFile VerificationKeyTextOrFile
vkTextOrFile =
    IO
  (Either VerificationKeyTextOrFileError SomeAddressVerificationKey)
-> ExceptT
     VerificationKeyTextOrFileError IO SomeAddressVerificationKey
forall (m :: * -> *) x a. m (Either x a) -> ExceptT x m a
newExceptT (IO
   (Either VerificationKeyTextOrFileError SomeAddressVerificationKey)
 -> ExceptT
      VerificationKeyTextOrFileError IO SomeAddressVerificationKey)
-> IO
     (Either VerificationKeyTextOrFileError SomeAddressVerificationKey)
-> ExceptT
     VerificationKeyTextOrFileError IO SomeAddressVerificationKey
forall a b. (a -> b) -> a -> b
$
      [FromSomeType SerialiseAsBech32 SomeAddressVerificationKey]
-> [FromSomeType HasTextEnvelope SomeAddressVerificationKey]
-> VerificationKeyTextOrFile
-> IO
     (Either VerificationKeyTextOrFileError SomeAddressVerificationKey)
forall b.
[FromSomeType SerialiseAsBech32 b]
-> [FromSomeType HasTextEnvelope b]
-> VerificationKeyTextOrFile
-> IO (Either VerificationKeyTextOrFileError b)
readVerificationKeyTextOrFileAnyOf [FromSomeType SerialiseAsBech32 SomeAddressVerificationKey]
bech32Types [FromSomeType HasTextEnvelope SomeAddressVerificationKey]
textEnvTypes VerificationKeyTextOrFile
vkTextOrFile
  where
    bech32Types :: [FromSomeType SerialiseAsBech32 SomeAddressVerificationKey]
bech32Types =
      [ AsType (VerificationKey PaymentKey)
-> (VerificationKey PaymentKey -> SomeAddressVerificationKey)
-> FromSomeType SerialiseAsBech32 SomeAddressVerificationKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType PaymentKey -> AsType (VerificationKey PaymentKey)
forall a. AsType a -> AsType (VerificationKey a)
AsVerificationKey AsType PaymentKey
AsPaymentKey)
                     VerificationKey PaymentKey -> SomeAddressVerificationKey
APaymentVerificationKey
      , AsType (VerificationKey PaymentExtendedKey)
-> (VerificationKey PaymentExtendedKey
    -> SomeAddressVerificationKey)
-> FromSomeType SerialiseAsBech32 SomeAddressVerificationKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType PaymentExtendedKey
-> AsType (VerificationKey PaymentExtendedKey)
forall a. AsType a -> AsType (VerificationKey a)
AsVerificationKey AsType PaymentExtendedKey
AsPaymentExtendedKey)
                     VerificationKey PaymentExtendedKey -> SomeAddressVerificationKey
APaymentExtendedVerificationKey
      ]

    textEnvTypes :: [FromSomeType HasTextEnvelope SomeAddressVerificationKey]
textEnvTypes =
      [ AsType (VerificationKey ByronKey)
-> (VerificationKey ByronKey -> SomeAddressVerificationKey)
-> FromSomeType HasTextEnvelope SomeAddressVerificationKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType ByronKey -> AsType (VerificationKey ByronKey)
forall a. AsType a -> AsType (VerificationKey a)
AsVerificationKey AsType ByronKey
AsByronKey)
                     VerificationKey ByronKey -> SomeAddressVerificationKey
AByronVerificationKey
      , AsType (VerificationKey PaymentKey)
-> (VerificationKey PaymentKey -> SomeAddressVerificationKey)
-> FromSomeType HasTextEnvelope SomeAddressVerificationKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType PaymentKey -> AsType (VerificationKey PaymentKey)
forall a. AsType a -> AsType (VerificationKey a)
AsVerificationKey AsType PaymentKey
AsPaymentKey)
                     VerificationKey PaymentKey -> SomeAddressVerificationKey
APaymentVerificationKey
      , AsType (VerificationKey PaymentExtendedKey)
-> (VerificationKey PaymentExtendedKey
    -> SomeAddressVerificationKey)
-> FromSomeType HasTextEnvelope SomeAddressVerificationKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType PaymentExtendedKey
-> AsType (VerificationKey PaymentExtendedKey)
forall a. AsType a -> AsType (VerificationKey a)
AsVerificationKey AsType PaymentExtendedKey
AsPaymentExtendedKey)
                     VerificationKey PaymentExtendedKey -> SomeAddressVerificationKey
APaymentExtendedVerificationKey
      , AsType (VerificationKey GenesisUTxOKey)
-> (VerificationKey GenesisUTxOKey -> SomeAddressVerificationKey)
-> FromSomeType HasTextEnvelope SomeAddressVerificationKey
forall (c :: * -> Constraint) a b.
c a =>
AsType a -> (a -> b) -> FromSomeType c b
FromSomeType (AsType GenesisUTxOKey -> AsType (VerificationKey GenesisUTxOKey)
forall a. AsType a -> AsType (VerificationKey a)
AsVerificationKey AsType GenesisUTxOKey
AsGenesisUTxOKey)
                     VerificationKey GenesisUTxOKey -> SomeAddressVerificationKey
AGenesisUTxOVerificationKey
      ]

--
-- Multisig addresses
--

runAddressBuildScript
  :: ScriptFile
  -> NetworkId
  -> Maybe OutputFile
  -> ExceptT ShelleyAddressCmdError IO ()
runAddressBuildScript :: ScriptFile
-> NetworkId
-> Maybe OutputFile
-> ExceptT ShelleyAddressCmdError IO ()
runAddressBuildScript ScriptFile
scriptFile NetworkId
networkId Maybe OutputFile
mOutputFile = do
  IO () -> ExceptT ShelleyAddressCmdError IO ()
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO ()
deprecationWarning
  PaymentVerifier
-> Maybe StakeVerifier
-> NetworkId
-> Maybe OutputFile
-> ExceptT ShelleyAddressCmdError IO ()
runAddressBuild (ScriptFile -> PaymentVerifier
PaymentVerifierScriptFile ScriptFile
scriptFile) Maybe StakeVerifier
forall a. Maybe a
Nothing NetworkId
networkId Maybe OutputFile
mOutputFile

deprecationWarning :: IO ()
deprecationWarning :: IO ()
deprecationWarning = do
  Handle -> [SGR] -> IO ()
ANSI.hSetSGR Handle
IO.stderr [ConsoleLayer -> ColorIntensity -> Color -> SGR
SetColor ConsoleLayer
Foreground ColorIntensity
Vivid Color
Yellow]
  Handle -> String -> IO ()
IO.hPutStrLn Handle
IO.stderr String
"WARNING: This CLI command is deprecated.  Please use 'address build' command instead."
  Handle -> [SGR] -> IO ()
ANSI.hSetSGR Handle
IO.stderr [SGR
Reset]