{-# LANGUAGE RankNTypes #-}

module Cardano.CLI.Shelley.Run.Address
  ( ShelleyAddressCmdError
  , renderShelleyAddressCmdError
  , runAddressCmd
  ) where

import           Cardano.Prelude hiding (putStrLn)
import           Prelude (String)

import           Data.Aeson
import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString.Lazy as LB
import qualified Data.Text as Text
import qualified Data.Text.IO as Text

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

import           Cardano.Api.TextView (TextViewDescription (..))
import           Cardano.Api.Typed

import           Cardano.CLI.Shelley.Key (InputDecodeError, VerificationKeyOrFile,
                     VerificationKeyTextOrFile, VerificationKeyTextOrFileError (..),
                     readVerificationKeyOrFile, readVerificationKeyTextOrFileAnyOf,
                     renderVerificationKeyTextOrFileError)
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
  | ShelleyAddressCmdAesonDecodeError !FilePath !Text
  | ShelleyAddressCmdReadKeyFileError !(FileError InputDecodeError)
  | ShelleyAddressCmdReadFileException !(FileError ())
  | 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
    ShelleyAddressCmdWriteFileError FileError ()
fileErr -> String -> Text
Text.pack (FileError () -> String
forall e. Error e => e -> String
displayError FileError ()
fileErr)
    ShelleyAddressCmdAesonDecodeError String
fp Text
decErr -> Text
"Error decoding multisignature JSON object at: "
                                                   Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
Text.pack String
fp Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" Error: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
decErr
    ShelleyAddressCmdReadFileException 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 VerificationKeyTextOrFile
payVk Maybe (VerificationKeyOrFile StakeKey)
stkVk NetworkId
nw Maybe OutputFile
mOutFp -> VerificationKeyTextOrFile
-> Maybe (VerificationKeyOrFile StakeKey)
-> NetworkId
-> Maybe OutputFile
-> ExceptT ShelleyAddressCmdError IO ()
runAddressBuild VerificationKeyTextOrFile
payVk Maybe (VerificationKeyOrFile StakeKey)
stkVk 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 :: TextViewDescription
    skeyDesc :: TextEnvelopeDescr
skeyDesc = ByteString -> TextEnvelopeDescr
TextViewDescription ByteString
"Payment Signing Key"
    vkeyDesc :: TextEnvelopeDescr
vkeyDesc = ByteString -> TextEnvelopeDescr
TextViewDescription ByteString
"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 :: VerificationKeyTextOrFile
                -> Maybe (VerificationKeyOrFile StakeKey)
                -> NetworkId
                -> Maybe OutputFile
                -> ExceptT ShelleyAddressCmdError IO ()
runAddressBuild :: VerificationKeyTextOrFile
-> Maybe (VerificationKeyOrFile StakeKey)
-> NetworkId
-> Maybe OutputFile
-> ExceptT ShelleyAddressCmdError IO ()
runAddressBuild VerificationKeyTextOrFile
payVkeyTextOrFile Maybe (VerificationKeyOrFile StakeKey)
mbStkVkeyOrFile NetworkId
nw Maybe OutputFile
mOutFp = 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

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

              APaymentVerificationKey VerificationKey PaymentKey
vk ->
                VerificationKey PaymentKey
-> ExceptT ShelleyAddressCmdError IO (Address Shelley)
buildShelleyAddress VerificationKey PaymentKey
vk

              APaymentExtendedVerificationKey VerificationKey PaymentExtendedKey
vk ->
                VerificationKey PaymentKey
-> ExceptT ShelleyAddressCmdError IO (Address Shelley)
buildShelleyAddress (VerificationKey PaymentExtendedKey -> VerificationKey PaymentKey
forall keyroleA keyroleB.
CastVerificationKeyRole keyroleA keyroleB =>
VerificationKey keyroleA -> VerificationKey keyroleB
castVerificationKey VerificationKey PaymentExtendedKey
vk)

              AGenesisUTxOVerificationKey VerificationKey GenesisUTxOKey
vk ->
                VerificationKey PaymentKey
-> ExceptT ShelleyAddressCmdError IO (Address Shelley)
buildShelleyAddress (VerificationKey GenesisUTxOKey -> VerificationKey PaymentKey
forall keyroleA keyroleB.
CastVerificationKeyRole keyroleA keyroleB =>
VerificationKey keyroleA -> VerificationKey keyroleB
castVerificationKey VerificationKey GenesisUTxOKey
vk)

    let addrText :: Text
addrText = Address Shelley -> Text
forall addr. SerialiseAddress addr => addr -> Text
serialiseAddress Address Shelley
addr

    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
addrText
      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.putStrLn        Text
addrText

  where
    buildShelleyAddress :: VerificationKey PaymentKey
-> ExceptT ShelleyAddressCmdError IO (Address Shelley)
buildShelleyAddress VerificationKey PaymentKey
vkey = do
      Maybe (VerificationKey StakeKey)
mstakeVKey <-
        case Maybe (VerificationKeyOrFile StakeKey)
mbStkVkeyOrFile of
          Maybe (VerificationKeyOrFile StakeKey)
Nothing -> Maybe (VerificationKey StakeKey)
-> ExceptT
     ShelleyAddressCmdError IO (Maybe (VerificationKey StakeKey))
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (VerificationKey StakeKey)
forall a. Maybe a
Nothing
          Just VerificationKeyOrFile StakeKey
stkVkeyOrFile ->
            (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

      let paymentCred :: PaymentCredential
paymentCred  = Hash PaymentKey -> PaymentCredential
PaymentCredentialByKey (VerificationKey PaymentKey -> Hash PaymentKey
forall keyrole.
Key keyrole =>
VerificationKey keyrole -> Hash keyrole
verificationKeyHash VerificationKey PaymentKey
vkey)
          stakeAddrRef :: StakeAddressReference
stakeAddrRef = 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
          address :: Address Shelley
address      = NetworkId
-> PaymentCredential -> StakeAddressReference -> Address Shelley
makeShelleyAddress NetworkId
nw PaymentCredential
paymentCred StakeAddressReference
stakeAddrRef

      Address Shelley
-> ExceptT ShelleyAddressCmdError IO (Address Shelley)
forall (m :: * -> *) a. Monad m => a -> m a
return Address Shelley
address


--
-- 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)

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 ByronKey)
-> (VerificationKey ByronKey -> SomeAddressVerificationKey)
-> FromSomeType SerialiseAsBech32 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 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 String
fp) NetworkId
nId Maybe OutputFile
mOutFp = do
  ByteString
scriptLB <- (IOException -> ShelleyAddressCmdError)
-> IO ByteString -> ExceptT ShelleyAddressCmdError IO ByteString
forall (m :: * -> *) x a.
MonadIO m =>
(IOException -> x) -> IO a -> ExceptT x m a
handleIOExceptT (FileError () -> ShelleyAddressCmdError
ShelleyAddressCmdReadFileException (FileError () -> ShelleyAddressCmdError)
-> (IOException -> FileError ())
-> IOException
-> ShelleyAddressCmdError
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. String -> IOException -> FileError ()
forall e. String -> IOException -> FileError e
FileIOError String
fp)
                (IO ByteString -> ExceptT ShelleyAddressCmdError IO ByteString)
-> IO ByteString -> ExceptT ShelleyAddressCmdError IO ByteString
forall a b. (a -> b) -> a -> b
$ String -> IO ByteString
LB.readFile String
fp
  Script
script <- case ByteString -> Either String MultiSigScript
forall a. FromJSON a => ByteString -> Either String a
eitherDecode ByteString
scriptLB :: Either String MultiSigScript of
               Right MultiSigScript
mss -> Script -> ExceptT ShelleyAddressCmdError IO Script
forall (m :: * -> *) a. Monad m => a -> m a
return (Script -> ExceptT ShelleyAddressCmdError IO Script)
-> Script -> ExceptT ShelleyAddressCmdError IO Script
forall a b. (a -> b) -> a -> b
$ MultiSigScript -> Script
makeMultiSigScript MultiSigScript
mss
               Left String
err -> ShelleyAddressCmdError -> ExceptT ShelleyAddressCmdError IO Script
forall (m :: * -> *) x a. Monad m => x -> ExceptT x m a
left (ShelleyAddressCmdError
 -> ExceptT ShelleyAddressCmdError IO Script)
-> (Text -> ShelleyAddressCmdError)
-> Text
-> ExceptT ShelleyAddressCmdError IO Script
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. String -> Text -> ShelleyAddressCmdError
ShelleyAddressCmdAesonDecodeError String
fp (Text -> ExceptT ShelleyAddressCmdError IO Script)
-> Text -> ExceptT ShelleyAddressCmdError IO Script
forall a b. (a -> b) -> a -> b
$ String -> Text
Text.pack String
err
  let payCred :: PaymentCredential
payCred = Hash Script -> PaymentCredential
PaymentCredentialByScript (Hash Script -> PaymentCredential)
-> Hash Script -> PaymentCredential
forall a b. (a -> b) -> a -> b
$ Script -> Hash Script
scriptHash Script
script
      scriptAddr :: Text
scriptAddr = Address Shelley -> Text
forall addr. SerialiseAddress addr => addr -> Text
serialiseAddress (Address Shelley -> Text) -> Address Shelley -> Text
forall a b. (a -> b) -> a -> b
$ NetworkId
-> PaymentCredential -> StakeAddressReference -> Address Shelley
makeShelleyAddress NetworkId
nId PaymentCredential
payCred StakeAddressReference
NoStakeAddress
  case Maybe OutputFile
mOutFp of
    Just (OutputFile String
oFp) -> 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
oFp Text
scriptAddr
    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
scriptAddr