module Cardano.CLI.Shelley.Run.Governance
  ( ShelleyGovernanceCmdError
  , renderShelleyGovernanceError
  , runGovernanceCmd
  ) where

import           Cardano.Prelude

import qualified Data.Text as Text

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

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

import           Cardano.CLI.Shelley.Key (InputDecodeError, VerificationKeyOrHashOrFile,
                     readVerificationKeyOrHashOrFile, readVerificationKeyOrHashOrTextEnvFile)
import           Cardano.CLI.Shelley.Parsers
import           Cardano.CLI.Types

import qualified Shelley.Spec.Ledger.TxBody as Shelley


data ShelleyGovernanceCmdError
  = ShelleyGovernanceCmdTextEnvReadError !(FileError TextEnvelopeError)
  | ShelleyGovernanceCmdKeyReadError !(FileError InputDecodeError)
  | ShelleyGovernanceCmdTextEnvWriteError !(FileError ())
  | ShelleyGovernanceCmdEmptyUpdateProposalError
  | ShelleyGovernanceCmdMIRCertificateKeyRewardMistmach
      !FilePath
      !Int
      -- ^ Number of stake verification keys
      !Int
      -- ^ Number of reward amounts
  deriving Int -> ShelleyGovernanceCmdError -> ShowS
[ShelleyGovernanceCmdError] -> ShowS
ShelleyGovernanceCmdError -> String
(Int -> ShelleyGovernanceCmdError -> ShowS)
-> (ShelleyGovernanceCmdError -> String)
-> ([ShelleyGovernanceCmdError] -> ShowS)
-> Show ShelleyGovernanceCmdError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [ShelleyGovernanceCmdError] -> ShowS
$cshowList :: [ShelleyGovernanceCmdError] -> ShowS
show :: ShelleyGovernanceCmdError -> String
$cshow :: ShelleyGovernanceCmdError -> String
showsPrec :: Int -> ShelleyGovernanceCmdError -> ShowS
$cshowsPrec :: Int -> ShelleyGovernanceCmdError -> ShowS
Show

renderShelleyGovernanceError :: ShelleyGovernanceCmdError -> Text
renderShelleyGovernanceError :: ShelleyGovernanceCmdError -> Text
renderShelleyGovernanceError ShelleyGovernanceCmdError
err =
  case ShelleyGovernanceCmdError
err of
    ShelleyGovernanceCmdTextEnvReadError FileError TextEnvelopeError
fileErr -> String -> Text
Text.pack (FileError TextEnvelopeError -> String
forall e. Error e => e -> String
displayError FileError TextEnvelopeError
fileErr)
    ShelleyGovernanceCmdKeyReadError FileError InputDecodeError
fileErr -> String -> Text
Text.pack (FileError InputDecodeError -> String
forall e. Error e => e -> String
displayError FileError InputDecodeError
fileErr)
    ShelleyGovernanceCmdTextEnvWriteError FileError ()
fileErr -> String -> Text
Text.pack (FileError () -> String
forall e. Error e => e -> String
displayError FileError ()
fileErr)
    -- TODO: The equality check is still not working for empty update proposals.
    ShelleyGovernanceCmdError
ShelleyGovernanceCmdEmptyUpdateProposalError ->
      Text
"Empty update proposals are not allowed"
    ShelleyGovernanceCmdMIRCertificateKeyRewardMistmach String
fp Int
numVKeys Int
numRwdAmts ->
       Text
"Error creating the MIR certificate at: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
forall a. Show a => a -> Text
textShow String
fp
       Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" The number of staking keys: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int -> Text
forall a. Show a => a -> Text
textShow Int
numVKeys
       Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" and the number of reward amounts: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Int -> Text
forall a. Show a => a -> Text
textShow Int
numRwdAmts
       Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" are not equivalent."



runGovernanceCmd :: GovernanceCmd -> ExceptT ShelleyGovernanceCmdError IO ()
runGovernanceCmd :: GovernanceCmd -> ExceptT ShelleyGovernanceCmdError IO ()
runGovernanceCmd (GovernanceMIRCertificate MIRPot
mirpot [VerificationKeyFile]
vKeys [Lovelace]
rewards OutputFile
out) =
  MIRPot
-> [VerificationKeyFile]
-> [Lovelace]
-> OutputFile
-> ExceptT ShelleyGovernanceCmdError IO ()
runGovernanceMIRCertificate MIRPot
mirpot [VerificationKeyFile]
vKeys [Lovelace]
rewards OutputFile
out
runGovernanceCmd (GovernanceGenesisKeyDelegationCertificate VerificationKeyOrHashOrFile GenesisKey
genVk VerificationKeyOrHashOrFile GenesisDelegateKey
genDelegVk VerificationKeyOrHashOrFile VrfKey
vrfVk OutputFile
out) =
  VerificationKeyOrHashOrFile GenesisKey
-> VerificationKeyOrHashOrFile GenesisDelegateKey
-> VerificationKeyOrHashOrFile VrfKey
-> OutputFile
-> ExceptT ShelleyGovernanceCmdError IO ()
runGovernanceGenesisKeyDelegationCertificate VerificationKeyOrHashOrFile GenesisKey
genVk VerificationKeyOrHashOrFile GenesisDelegateKey
genDelegVk VerificationKeyOrHashOrFile VrfKey
vrfVk OutputFile
out
runGovernanceCmd (GovernanceUpdateProposal OutputFile
out EpochNo
eNo [VerificationKeyFile]
genVKeys ProtocolParametersUpdate
ppUp) =
  OutputFile
-> EpochNo
-> [VerificationKeyFile]
-> ProtocolParametersUpdate
-> ExceptT ShelleyGovernanceCmdError IO ()
runGovernanceUpdateProposal OutputFile
out EpochNo
eNo [VerificationKeyFile]
genVKeys ProtocolParametersUpdate
ppUp

runGovernanceMIRCertificate
  :: Shelley.MIRPot
  -> [VerificationKeyFile]
  -- ^ Stake verification keys
  -> [Lovelace]
  -- ^ Reward amounts
  -> OutputFile
  -> ExceptT ShelleyGovernanceCmdError IO ()
runGovernanceMIRCertificate :: MIRPot
-> [VerificationKeyFile]
-> [Lovelace]
-> OutputFile
-> ExceptT ShelleyGovernanceCmdError IO ()
runGovernanceMIRCertificate MIRPot
mirPot [VerificationKeyFile]
vKeys [Lovelace]
rwdAmts (OutputFile String
oFp) = do
    [StakeCredential]
sCreds <- (VerificationKeyFile
 -> ExceptT ShelleyGovernanceCmdError IO StakeCredential)
-> [VerificationKeyFile]
-> ExceptT ShelleyGovernanceCmdError IO [StakeCredential]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM VerificationKeyFile
-> ExceptT ShelleyGovernanceCmdError IO StakeCredential
readStakeKeyToCred [VerificationKeyFile]
vKeys

    [VerificationKeyFile]
-> [Lovelace] -> ExceptT ShelleyGovernanceCmdError IO ()
checkEqualKeyRewards [VerificationKeyFile]
vKeys [Lovelace]
rwdAmts

    let mirCert :: Certificate
mirCert = MIRPot -> [(StakeCredential, Lovelace)] -> Certificate
makeMIRCertificate MIRPot
mirPot ([StakeCredential] -> [Lovelace] -> [(StakeCredential, Lovelace)]
forall a b. [a] -> [b] -> [(a, b)]
zip [StakeCredential]
sCreds [Lovelace]
rwdAmts)

    (FileError () -> ShelleyGovernanceCmdError)
-> ExceptT (FileError ()) IO ()
-> ExceptT ShelleyGovernanceCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError () -> ShelleyGovernanceCmdError
ShelleyGovernanceCmdTextEnvWriteError
      (ExceptT (FileError ()) IO ()
 -> ExceptT ShelleyGovernanceCmdError IO ())
-> (IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ())
-> IO (Either (FileError ()) ())
-> ExceptT ShelleyGovernanceCmdError 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 ShelleyGovernanceCmdError IO ())
-> IO (Either (FileError ()) ())
-> ExceptT ShelleyGovernanceCmdError IO ()
forall a b. (a -> b) -> a -> b
$ String
-> Maybe TextEnvelopeDescr
-> Certificate
-> IO (Either (FileError ()) ())
forall a.
HasTextEnvelope a =>
String
-> Maybe TextEnvelopeDescr -> a -> IO (Either (FileError ()) ())
writeFileTextEnvelope String
oFp (TextEnvelopeDescr -> Maybe TextEnvelopeDescr
forall a. a -> Maybe a
Just TextEnvelopeDescr
mirCertDesc) Certificate
mirCert
  where
    mirCertDesc :: TextViewDescription
    mirCertDesc :: TextEnvelopeDescr
mirCertDesc = ByteString -> TextEnvelopeDescr
TextViewDescription ByteString
"Move Instantaneous Rewards Certificate"

    checkEqualKeyRewards :: [VerificationKeyFile] -> [Lovelace] -> ExceptT ShelleyGovernanceCmdError IO ()
    checkEqualKeyRewards :: [VerificationKeyFile]
-> [Lovelace] -> ExceptT ShelleyGovernanceCmdError IO ()
checkEqualKeyRewards [VerificationKeyFile]
keys [Lovelace]
rwds = do
       let numVKeys :: Int
numVKeys = [VerificationKeyFile] -> Int
forall a. HasLength a => a -> Int
length [VerificationKeyFile]
keys
           numRwdAmts :: Int
numRwdAmts = [Lovelace] -> Int
forall a. HasLength a => a -> Int
length [Lovelace]
rwds
       if Int
numVKeys Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
numRwdAmts
       then () -> ExceptT ShelleyGovernanceCmdError IO ()
forall (m :: * -> *) a. Monad m => a -> m a
return () else ShelleyGovernanceCmdError
-> ExceptT ShelleyGovernanceCmdError IO ()
forall (m :: * -> *) x a. Monad m => x -> ExceptT x m a
left (ShelleyGovernanceCmdError
 -> ExceptT ShelleyGovernanceCmdError IO ())
-> ShelleyGovernanceCmdError
-> ExceptT ShelleyGovernanceCmdError IO ()
forall a b. (a -> b) -> a -> b
$ String -> Int -> Int -> ShelleyGovernanceCmdError
ShelleyGovernanceCmdMIRCertificateKeyRewardMistmach String
oFp Int
numVKeys Int
numRwdAmts

    readStakeKeyToCred :: VerificationKeyFile -> ExceptT ShelleyGovernanceCmdError IO StakeCredential
    readStakeKeyToCred :: VerificationKeyFile
-> ExceptT ShelleyGovernanceCmdError IO StakeCredential
readStakeKeyToCred (VerificationKeyFile String
stVKey) = do
      VerificationKey StakeKey
stakeVkey <- (FileError TextEnvelopeError -> ShelleyGovernanceCmdError)
-> ExceptT
     (FileError TextEnvelopeError) IO (VerificationKey StakeKey)
-> ExceptT ShelleyGovernanceCmdError IO (VerificationKey StakeKey)
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError TextEnvelopeError -> ShelleyGovernanceCmdError
ShelleyGovernanceCmdTextEnvReadError
        (ExceptT
   (FileError TextEnvelopeError) IO (VerificationKey StakeKey)
 -> ExceptT ShelleyGovernanceCmdError IO (VerificationKey StakeKey))
-> (IO
      (Either (FileError TextEnvelopeError) (VerificationKey StakeKey))
    -> ExceptT
         (FileError TextEnvelopeError) IO (VerificationKey StakeKey))
-> IO
     (Either (FileError TextEnvelopeError) (VerificationKey StakeKey))
-> ExceptT ShelleyGovernanceCmdError IO (VerificationKey StakeKey)
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 TextEnvelopeError) (VerificationKey StakeKey))
-> ExceptT
     (FileError TextEnvelopeError) IO (VerificationKey StakeKey)
forall (m :: * -> *) x a. m (Either x a) -> ExceptT x m a
newExceptT
        (IO
   (Either (FileError TextEnvelopeError) (VerificationKey StakeKey))
 -> ExceptT ShelleyGovernanceCmdError IO (VerificationKey StakeKey))
-> IO
     (Either (FileError TextEnvelopeError) (VerificationKey StakeKey))
-> ExceptT ShelleyGovernanceCmdError IO (VerificationKey StakeKey)
forall a b. (a -> b) -> a -> b
$ AsType (VerificationKey StakeKey)
-> String
-> IO
     (Either (FileError TextEnvelopeError) (VerificationKey StakeKey))
forall a.
HasTextEnvelope a =>
AsType a -> String -> IO (Either (FileError TextEnvelopeError) a)
readFileTextEnvelope (AsType StakeKey -> AsType (VerificationKey StakeKey)
forall a. AsType a -> AsType (VerificationKey a)
AsVerificationKey AsType StakeKey
AsStakeKey) String
stVKey
      StakeCredential
-> ExceptT ShelleyGovernanceCmdError IO StakeCredential
forall (m :: * -> *) a x. Monad m => a -> ExceptT x m a
right (StakeCredential
 -> ExceptT ShelleyGovernanceCmdError IO StakeCredential)
-> (Hash StakeKey -> StakeCredential)
-> Hash StakeKey
-> ExceptT ShelleyGovernanceCmdError IO StakeCredential
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
 -> ExceptT ShelleyGovernanceCmdError IO StakeCredential)
-> Hash StakeKey
-> ExceptT ShelleyGovernanceCmdError IO StakeCredential
forall a b. (a -> b) -> a -> b
$ VerificationKey StakeKey -> Hash StakeKey
forall keyrole.
Key keyrole =>
VerificationKey keyrole -> Hash keyrole
verificationKeyHash VerificationKey StakeKey
stakeVkey

runGovernanceGenesisKeyDelegationCertificate
  :: VerificationKeyOrHashOrFile GenesisKey
  -> VerificationKeyOrHashOrFile GenesisDelegateKey
  -> VerificationKeyOrHashOrFile VrfKey
  -> OutputFile
  -> ExceptT ShelleyGovernanceCmdError IO ()
runGovernanceGenesisKeyDelegationCertificate :: VerificationKeyOrHashOrFile GenesisKey
-> VerificationKeyOrHashOrFile GenesisDelegateKey
-> VerificationKeyOrHashOrFile VrfKey
-> OutputFile
-> ExceptT ShelleyGovernanceCmdError IO ()
runGovernanceGenesisKeyDelegationCertificate VerificationKeyOrHashOrFile GenesisKey
genVkOrHashOrFp
                                             VerificationKeyOrHashOrFile GenesisDelegateKey
genDelVkOrHashOrFp
                                             VerificationKeyOrHashOrFile VrfKey
vrfVkOrHashOrFp
                                             (OutputFile String
oFp) = do
    Hash GenesisKey
genesisVkHash <- (FileError InputDecodeError -> ShelleyGovernanceCmdError)
-> ExceptT (FileError InputDecodeError) IO (Hash GenesisKey)
-> ExceptT ShelleyGovernanceCmdError IO (Hash GenesisKey)
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError InputDecodeError -> ShelleyGovernanceCmdError
ShelleyGovernanceCmdKeyReadError
      (ExceptT (FileError InputDecodeError) IO (Hash GenesisKey)
 -> ExceptT ShelleyGovernanceCmdError IO (Hash GenesisKey))
-> (IO (Either (FileError InputDecodeError) (Hash GenesisKey))
    -> ExceptT (FileError InputDecodeError) IO (Hash GenesisKey))
-> IO (Either (FileError InputDecodeError) (Hash GenesisKey))
-> ExceptT ShelleyGovernanceCmdError IO (Hash GenesisKey)
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 InputDecodeError) (Hash GenesisKey))
-> ExceptT (FileError InputDecodeError) IO (Hash GenesisKey)
forall (m :: * -> *) x a. m (Either x a) -> ExceptT x m a
newExceptT
      (IO (Either (FileError InputDecodeError) (Hash GenesisKey))
 -> ExceptT ShelleyGovernanceCmdError IO (Hash GenesisKey))
-> IO (Either (FileError InputDecodeError) (Hash GenesisKey))
-> ExceptT ShelleyGovernanceCmdError IO (Hash GenesisKey)
forall a b. (a -> b) -> a -> b
$ AsType GenesisKey
-> VerificationKeyOrHashOrFile GenesisKey
-> IO (Either (FileError InputDecodeError) (Hash GenesisKey))
forall keyrole.
Key keyrole =>
AsType keyrole
-> VerificationKeyOrHashOrFile keyrole
-> IO (Either (FileError InputDecodeError) (Hash keyrole))
readVerificationKeyOrHashOrTextEnvFile AsType GenesisKey
AsGenesisKey VerificationKeyOrHashOrFile GenesisKey
genVkOrHashOrFp
    Hash GenesisDelegateKey
genesisDelVkHash <-(FileError InputDecodeError -> ShelleyGovernanceCmdError)
-> ExceptT
     (FileError InputDecodeError) IO (Hash GenesisDelegateKey)
-> ExceptT ShelleyGovernanceCmdError IO (Hash GenesisDelegateKey)
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError InputDecodeError -> ShelleyGovernanceCmdError
ShelleyGovernanceCmdKeyReadError
      (ExceptT (FileError InputDecodeError) IO (Hash GenesisDelegateKey)
 -> ExceptT ShelleyGovernanceCmdError IO (Hash GenesisDelegateKey))
-> (IO
      (Either (FileError InputDecodeError) (Hash GenesisDelegateKey))
    -> ExceptT
         (FileError InputDecodeError) IO (Hash GenesisDelegateKey))
-> IO
     (Either (FileError InputDecodeError) (Hash GenesisDelegateKey))
-> ExceptT ShelleyGovernanceCmdError IO (Hash GenesisDelegateKey)
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 InputDecodeError) (Hash GenesisDelegateKey))
-> ExceptT
     (FileError InputDecodeError) IO (Hash GenesisDelegateKey)
forall (m :: * -> *) x a. m (Either x a) -> ExceptT x m a
newExceptT
      (IO (Either (FileError InputDecodeError) (Hash GenesisDelegateKey))
 -> ExceptT ShelleyGovernanceCmdError IO (Hash GenesisDelegateKey))
-> IO
     (Either (FileError InputDecodeError) (Hash GenesisDelegateKey))
-> ExceptT ShelleyGovernanceCmdError IO (Hash GenesisDelegateKey)
forall a b. (a -> b) -> a -> b
$ AsType GenesisDelegateKey
-> VerificationKeyOrHashOrFile GenesisDelegateKey
-> IO
     (Either (FileError InputDecodeError) (Hash GenesisDelegateKey))
forall keyrole.
Key keyrole =>
AsType keyrole
-> VerificationKeyOrHashOrFile keyrole
-> IO (Either (FileError InputDecodeError) (Hash keyrole))
readVerificationKeyOrHashOrTextEnvFile AsType GenesisDelegateKey
AsGenesisDelegateKey VerificationKeyOrHashOrFile GenesisDelegateKey
genDelVkOrHashOrFp
    Hash VrfKey
vrfVkHash <- (FileError InputDecodeError -> ShelleyGovernanceCmdError)
-> ExceptT (FileError InputDecodeError) IO (Hash VrfKey)
-> ExceptT ShelleyGovernanceCmdError IO (Hash VrfKey)
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError InputDecodeError -> ShelleyGovernanceCmdError
ShelleyGovernanceCmdKeyReadError
      (ExceptT (FileError InputDecodeError) IO (Hash VrfKey)
 -> ExceptT ShelleyGovernanceCmdError IO (Hash VrfKey))
-> (IO (Either (FileError InputDecodeError) (Hash VrfKey))
    -> ExceptT (FileError InputDecodeError) IO (Hash VrfKey))
-> IO (Either (FileError InputDecodeError) (Hash VrfKey))
-> ExceptT ShelleyGovernanceCmdError IO (Hash VrfKey)
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 InputDecodeError) (Hash VrfKey))
-> ExceptT (FileError InputDecodeError) IO (Hash VrfKey)
forall (m :: * -> *) x a. m (Either x a) -> ExceptT x m a
newExceptT
      (IO (Either (FileError InputDecodeError) (Hash VrfKey))
 -> ExceptT ShelleyGovernanceCmdError IO (Hash VrfKey))
-> IO (Either (FileError InputDecodeError) (Hash VrfKey))
-> ExceptT ShelleyGovernanceCmdError IO (Hash VrfKey)
forall a b. (a -> b) -> a -> b
$ AsType VrfKey
-> VerificationKeyOrHashOrFile VrfKey
-> IO (Either (FileError InputDecodeError) (Hash VrfKey))
forall keyrole.
(Key keyrole, SerialiseAsBech32 (VerificationKey keyrole)) =>
AsType keyrole
-> VerificationKeyOrHashOrFile keyrole
-> IO (Either (FileError InputDecodeError) (Hash keyrole))
readVerificationKeyOrHashOrFile AsType VrfKey
AsVrfKey VerificationKeyOrHashOrFile VrfKey
vrfVkOrHashOrFp
    (FileError () -> ShelleyGovernanceCmdError)
-> ExceptT (FileError ()) IO ()
-> ExceptT ShelleyGovernanceCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError () -> ShelleyGovernanceCmdError
ShelleyGovernanceCmdTextEnvWriteError
      (ExceptT (FileError ()) IO ()
 -> ExceptT ShelleyGovernanceCmdError IO ())
-> (IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ())
-> IO (Either (FileError ()) ())
-> ExceptT ShelleyGovernanceCmdError 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 ShelleyGovernanceCmdError IO ())
-> IO (Either (FileError ()) ())
-> ExceptT ShelleyGovernanceCmdError IO ()
forall a b. (a -> b) -> a -> b
$ String
-> Maybe TextEnvelopeDescr
-> Certificate
-> IO (Either (FileError ()) ())
forall a.
HasTextEnvelope a =>
String
-> Maybe TextEnvelopeDescr -> a -> IO (Either (FileError ()) ())
writeFileTextEnvelope String
oFp (TextEnvelopeDescr -> Maybe TextEnvelopeDescr
forall a. a -> Maybe a
Just TextEnvelopeDescr
genKeyDelegCertDesc)
      (Certificate -> IO (Either (FileError ()) ()))
-> Certificate -> IO (Either (FileError ()) ())
forall a b. (a -> b) -> a -> b
$ Hash GenesisKey
-> Hash GenesisDelegateKey -> Hash VrfKey -> Certificate
makeGenesisKeyDelegationCertificate Hash GenesisKey
genesisVkHash Hash GenesisDelegateKey
genesisDelVkHash Hash VrfKey
vrfVkHash
  where
    genKeyDelegCertDesc :: TextViewDescription
    genKeyDelegCertDesc :: TextEnvelopeDescr
genKeyDelegCertDesc = ByteString -> TextEnvelopeDescr
TextViewDescription ByteString
"Genesis Key Delegation Certificate"

runGovernanceUpdateProposal
  :: OutputFile
  -> EpochNo
  -> [VerificationKeyFile]
  -- ^ Genesis verification keys
  -> ProtocolParametersUpdate
  -> ExceptT ShelleyGovernanceCmdError IO ()
runGovernanceUpdateProposal :: OutputFile
-> EpochNo
-> [VerificationKeyFile]
-> ProtocolParametersUpdate
-> ExceptT ShelleyGovernanceCmdError IO ()
runGovernanceUpdateProposal (OutputFile String
upFile) EpochNo
eNo [VerificationKeyFile]
genVerKeyFiles ProtocolParametersUpdate
upPprams = do
    Bool
-> ExceptT ShelleyGovernanceCmdError IO ()
-> ExceptT ShelleyGovernanceCmdError IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (ProtocolParametersUpdate
upPprams ProtocolParametersUpdate -> ProtocolParametersUpdate -> Bool
forall a. Eq a => a -> a -> Bool
== ProtocolParametersUpdate
forall a. Monoid a => a
mempty) (ExceptT ShelleyGovernanceCmdError IO ()
 -> ExceptT ShelleyGovernanceCmdError IO ())
-> ExceptT ShelleyGovernanceCmdError IO ()
-> ExceptT ShelleyGovernanceCmdError IO ()
forall a b. (a -> b) -> a -> b
$ ShelleyGovernanceCmdError
-> ExceptT ShelleyGovernanceCmdError IO ()
forall (m :: * -> *) x a. Monad m => x -> ExceptT x m a
left ShelleyGovernanceCmdError
ShelleyGovernanceCmdEmptyUpdateProposalError
    [VerificationKey GenesisKey]
genVKeys <- [ExceptT ShelleyGovernanceCmdError IO (VerificationKey GenesisKey)]
-> ExceptT
     ShelleyGovernanceCmdError IO [VerificationKey GenesisKey]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence
                  [ (FileError TextEnvelopeError -> ShelleyGovernanceCmdError)
-> ExceptT
     (FileError TextEnvelopeError) IO (VerificationKey GenesisKey)
-> ExceptT
     ShelleyGovernanceCmdError IO (VerificationKey GenesisKey)
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError TextEnvelopeError -> ShelleyGovernanceCmdError
ShelleyGovernanceCmdTextEnvReadError (ExceptT
   (FileError TextEnvelopeError) IO (VerificationKey GenesisKey)
 -> ExceptT
      ShelleyGovernanceCmdError IO (VerificationKey GenesisKey))
-> (IO
      (Either (FileError TextEnvelopeError) (VerificationKey GenesisKey))
    -> ExceptT
         (FileError TextEnvelopeError) IO (VerificationKey GenesisKey))
-> IO
     (Either (FileError TextEnvelopeError) (VerificationKey GenesisKey))
-> ExceptT
     ShelleyGovernanceCmdError IO (VerificationKey GenesisKey)
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 TextEnvelopeError) (VerificationKey GenesisKey))
-> ExceptT
     (FileError TextEnvelopeError) IO (VerificationKey GenesisKey)
forall (m :: * -> *) x a. m (Either x a) -> ExceptT x m a
newExceptT (IO
   (Either (FileError TextEnvelopeError) (VerificationKey GenesisKey))
 -> ExceptT
      ShelleyGovernanceCmdError IO (VerificationKey GenesisKey))
-> IO
     (Either (FileError TextEnvelopeError) (VerificationKey GenesisKey))
-> ExceptT
     ShelleyGovernanceCmdError IO (VerificationKey GenesisKey)
forall a b. (a -> b) -> a -> b
$
                      AsType (VerificationKey GenesisKey)
-> String
-> IO
     (Either (FileError TextEnvelopeError) (VerificationKey GenesisKey))
forall a.
HasTextEnvelope a =>
AsType a -> String -> IO (Either (FileError TextEnvelopeError) a)
readFileTextEnvelope
                        (AsType GenesisKey -> AsType (VerificationKey GenesisKey)
forall a. AsType a -> AsType (VerificationKey a)
AsVerificationKey AsType GenesisKey
AsGenesisKey)
                        String
vkeyFile
                  | VerificationKeyFile String
vkeyFile <- [VerificationKeyFile]
genVerKeyFiles ]
    let genKeyHashes :: [Hash GenesisKey]
genKeyHashes = (VerificationKey GenesisKey -> Hash GenesisKey)
-> [VerificationKey GenesisKey] -> [Hash GenesisKey]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
map VerificationKey GenesisKey -> Hash GenesisKey
forall keyrole.
Key keyrole =>
VerificationKey keyrole -> Hash keyrole
verificationKeyHash [VerificationKey GenesisKey]
genVKeys
        upProp :: UpdateProposal
upProp = ProtocolParametersUpdate
-> [Hash GenesisKey] -> EpochNo -> UpdateProposal
makeShelleyUpdateProposal ProtocolParametersUpdate
upPprams [Hash GenesisKey]
genKeyHashes EpochNo
eNo
    (FileError () -> ShelleyGovernanceCmdError)
-> ExceptT (FileError ()) IO ()
-> ExceptT ShelleyGovernanceCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError () -> ShelleyGovernanceCmdError
ShelleyGovernanceCmdTextEnvWriteError (ExceptT (FileError ()) IO ()
 -> ExceptT ShelleyGovernanceCmdError IO ())
-> (IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ())
-> IO (Either (FileError ()) ())
-> ExceptT ShelleyGovernanceCmdError 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 ShelleyGovernanceCmdError IO ())
-> IO (Either (FileError ()) ())
-> ExceptT ShelleyGovernanceCmdError IO ()
forall a b. (a -> b) -> a -> b
$
      String
-> Maybe TextEnvelopeDescr
-> UpdateProposal
-> IO (Either (FileError ()) ())
forall a.
HasTextEnvelope a =>
String
-> Maybe TextEnvelopeDescr -> a -> IO (Either (FileError ()) ())
writeFileTextEnvelope String
upFile Maybe TextEnvelopeDescr
forall a. Maybe a
Nothing UpdateProposal
upProp