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)

import           Cardano.Api
import           Cardano.Api.Shelley

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."
  where
    textShow :: a -> Text
textShow a
x = String -> Text
Text.pack (a -> String
forall a b. (Show a, ConvertText String b) => a -> b
show a
x)


runGovernanceCmd :: GovernanceCmd -> ExceptT ShelleyGovernanceCmdError IO ()
runGovernanceCmd :: GovernanceCmd -> ExceptT ShelleyGovernanceCmdError IO ()
runGovernanceCmd (GovernanceMIRPayStakeAddressesCertificate MIRPot
mirpot [StakeAddress]
vKeys [Lovelace]
rewards OutputFile
out) =
  MIRPot
-> [StakeAddress]
-> [Lovelace]
-> OutputFile
-> ExceptT ShelleyGovernanceCmdError IO ()
runGovernanceMIRCertificatePayStakeAddrs MIRPot
mirpot [StakeAddress]
vKeys [Lovelace]
rewards OutputFile
out
runGovernanceCmd (GovernanceMIRTransfer Lovelace
amt OutputFile
out TransferDirection
direction) =
  Lovelace
-> OutputFile
-> TransferDirection
-> ExceptT ShelleyGovernanceCmdError IO ()
runGovernanceMIRCertificateTransfer Lovelace
amt OutputFile
out TransferDirection
direction
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

runGovernanceMIRCertificatePayStakeAddrs
  :: Shelley.MIRPot
  -> [StakeAddress] -- ^ Stake addresses
  -> [Lovelace]     -- ^ Corresponding reward amounts (same length)
  -> OutputFile
  -> ExceptT ShelleyGovernanceCmdError IO ()
runGovernanceMIRCertificatePayStakeAddrs :: MIRPot
-> [StakeAddress]
-> [Lovelace]
-> OutputFile
-> ExceptT ShelleyGovernanceCmdError IO ()
runGovernanceMIRCertificatePayStakeAddrs MIRPot
mirPot [StakeAddress]
sAddrs [Lovelace]
rwdAmts (OutputFile String
oFp) = do

    Bool
-> ExceptT ShelleyGovernanceCmdError IO ()
-> ExceptT ShelleyGovernanceCmdError IO ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ([StakeAddress] -> Int
forall a. HasLength a => a -> Int
length [StakeAddress]
sAddrs Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== [Lovelace] -> Int
forall a. HasLength a => a -> Int
length [Lovelace]
rwdAmts) (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
 -> ExceptT ShelleyGovernanceCmdError IO ())
-> ShelleyGovernanceCmdError
-> ExceptT ShelleyGovernanceCmdError IO ()
forall a b. (a -> b) -> a -> b
$ String -> Int -> Int -> ShelleyGovernanceCmdError
ShelleyGovernanceCmdMIRCertificateKeyRewardMistmach
               String
oFp ([StakeAddress] -> Int
forall a. HasLength a => a -> Int
length [StakeAddress]
sAddrs) ([Lovelace] -> Int
forall a. HasLength a => a -> Int
length [Lovelace]
rwdAmts)

    let sCreds :: [StakeCredential]
sCreds  = (StakeAddress -> StakeCredential)
-> [StakeAddress] -> [StakeCredential]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
map StakeAddress -> StakeCredential
stakeAddrToStakeCredential [StakeAddress]
sAddrs
        mirCert :: Certificate
mirCert = MIRPot -> MIRTarget -> Certificate
makeMIRCertificate MIRPot
mirPot ([(StakeCredential, Lovelace)] -> MIRTarget
StakeAddressesMIR ([(StakeCredential, Lovelace)] -> MIRTarget)
-> [(StakeCredential, Lovelace)] -> MIRTarget
forall a b. (a -> b) -> a -> b
$ [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 :: TextEnvelopeDescr
    mirCertDesc :: TextEnvelopeDescr
mirCertDesc = TextEnvelopeDescr
"Move Instantaneous Rewards Certificate"

    --TODO: expose a pattern for StakeAddress that give us the StakeCredential
    stakeAddrToStakeCredential :: StakeAddress -> StakeCredential
    stakeAddrToStakeCredential :: StakeAddress -> StakeCredential
stakeAddrToStakeCredential (StakeAddress Network
_ StakeCredential StandardCrypto
scred) =
      StakeCredential StandardCrypto -> StakeCredential
fromShelleyStakeCredential StakeCredential StandardCrypto
scred

runGovernanceMIRCertificateTransfer
  :: Lovelace
  -> OutputFile
  -> TransferDirection
  -> ExceptT ShelleyGovernanceCmdError IO ()
runGovernanceMIRCertificateTransfer :: Lovelace
-> OutputFile
-> TransferDirection
-> ExceptT ShelleyGovernanceCmdError IO ()
runGovernanceMIRCertificateTransfer Lovelace
ll (OutputFile String
oFp) TransferDirection
direction = do
  Certificate
mirCert <- case TransferDirection
direction of
                 TransferDirection
TransferToReserves ->
                   Certificate -> ExceptT ShelleyGovernanceCmdError IO Certificate
forall (m :: * -> *) a. Monad m => a -> m a
return (Certificate -> ExceptT ShelleyGovernanceCmdError IO Certificate)
-> (MIRTarget -> Certificate)
-> MIRTarget
-> ExceptT ShelleyGovernanceCmdError IO Certificate
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. MIRPot -> MIRTarget -> Certificate
makeMIRCertificate MIRPot
Shelley.TreasuryMIR (MIRTarget -> ExceptT ShelleyGovernanceCmdError IO Certificate)
-> MIRTarget -> ExceptT ShelleyGovernanceCmdError IO Certificate
forall a b. (a -> b) -> a -> b
$ Lovelace -> MIRTarget
SendToReservesMIR Lovelace
ll
                 TransferDirection
TransferToTreasury ->
                   Certificate -> ExceptT ShelleyGovernanceCmdError IO Certificate
forall (m :: * -> *) a. Monad m => a -> m a
return (Certificate -> ExceptT ShelleyGovernanceCmdError IO Certificate)
-> (MIRTarget -> Certificate)
-> MIRTarget
-> ExceptT ShelleyGovernanceCmdError IO Certificate
forall k (cat :: k -> k -> *) (b :: k) (c :: k) (a :: k).
Category cat =>
cat b c -> cat a b -> cat a c
. MIRPot -> MIRTarget -> Certificate
makeMIRCertificate MIRPot
Shelley.ReservesMIR (MIRTarget -> ExceptT ShelleyGovernanceCmdError IO Certificate)
-> MIRTarget -> ExceptT ShelleyGovernanceCmdError IO Certificate
forall a b. (a -> b) -> a -> b
$ Lovelace -> MIRTarget
SendToTreasuryMIR Lovelace
ll

  (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 -> Maybe TextEnvelopeDescr)
-> TextEnvelopeDescr -> Maybe TextEnvelopeDescr
forall a b. (a -> b) -> a -> b
$ TransferDirection -> TextEnvelopeDescr
mirCertDesc TransferDirection
direction) Certificate
mirCert
 where
  mirCertDesc :: TransferDirection -> TextEnvelopeDescr
  mirCertDesc :: TransferDirection -> TextEnvelopeDescr
mirCertDesc TransferDirection
TransferToTreasury = TextEnvelopeDescr
"MIR Certificate Send To Treasury"
  mirCertDesc TransferDirection
TransferToReserves = TextEnvelopeDescr
"MIR Certificate Send To Reserves"


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 :: TextEnvelopeDescr
    genKeyDelegCertDesc :: TextEnvelopeDescr
genKeyDelegCertDesc = TextEnvelopeDescr
"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