module Bcc.CLI.Sophie.Run.Governance
  ( SophieGovernanceCmdError
  , renderSophieGovernanceError
  , runGovernanceCmd
  ) where

import           Bcc.Prelude

import qualified Data.Text as Text

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

import           Bcc.Api
import           Bcc.Api.Sophie

import           Bcc.CLI.Sophie.Key (InputDecodeError, VerificationKeyOrHashOrFile,
                   readVerificationKeyOrHashOrFile, readVerificationKeyOrHashOrTextEnvFile)
import           Bcc.CLI.Sophie.Parsers
import           Bcc.CLI.Types

import qualified Sophie.Spec.Ledger.TxBody as Sophie


data SophieGovernanceCmdError
  = SophieGovernanceCmdTextEnvReadError !(FileError TextEnvelopeError)
  | SophieGovernanceCmdKeyReadError !(FileError InputDecodeError)
  | SophieGovernanceCmdTextEnvWriteError !(FileError ())
  | SophieGovernanceCmdEmptyUpdateProposalError
  | SophieGovernanceCmdMIRCertificateKeyRewardMistmach
      !FilePath
      !Int
      -- ^ Number of stake verification keys
      !Int
      -- ^ Number of reward amounts
  deriving Int -> SophieGovernanceCmdError -> ShowS
[SophieGovernanceCmdError] -> ShowS
SophieGovernanceCmdError -> String
(Int -> SophieGovernanceCmdError -> ShowS)
-> (SophieGovernanceCmdError -> String)
-> ([SophieGovernanceCmdError] -> ShowS)
-> Show SophieGovernanceCmdError
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [SophieGovernanceCmdError] -> ShowS
$cshowList :: [SophieGovernanceCmdError] -> ShowS
show :: SophieGovernanceCmdError -> String
$cshow :: SophieGovernanceCmdError -> String
showsPrec :: Int -> SophieGovernanceCmdError -> ShowS
$cshowsPrec :: Int -> SophieGovernanceCmdError -> ShowS
Show

renderSophieGovernanceError :: SophieGovernanceCmdError -> Text
renderSophieGovernanceError :: SophieGovernanceCmdError -> Text
renderSophieGovernanceError SophieGovernanceCmdError
err =
  case SophieGovernanceCmdError
err of
    SophieGovernanceCmdTextEnvReadError FileError TextEnvelopeError
fileErr -> String -> Text
Text.pack (FileError TextEnvelopeError -> String
forall e. Error e => e -> String
displayError FileError TextEnvelopeError
fileErr)
    SophieGovernanceCmdKeyReadError FileError InputDecodeError
fileErr -> String -> Text
Text.pack (FileError InputDecodeError -> String
forall e. Error e => e -> String
displayError FileError InputDecodeError
fileErr)
    SophieGovernanceCmdTextEnvWriteError 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.
    SophieGovernanceCmdError
SophieGovernanceCmdEmptyUpdateProposalError ->
      Text
"Empty update proposals are not allowed"
    SophieGovernanceCmdMIRCertificateKeyRewardMistmach 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 SophieGovernanceCmdError IO ()
runGovernanceCmd :: GovernanceCmd -> ExceptT SophieGovernanceCmdError IO ()
runGovernanceCmd (GovernanceMIRPayStakeAddressesCertificate MIRPot
mirpot [StakeAddress]
vKeys [Entropic]
rewards OutputFile
out) =
  MIRPot
-> [StakeAddress]
-> [Entropic]
-> OutputFile
-> ExceptT SophieGovernanceCmdError IO ()
runGovernanceMIRCertificatePayStakeAddrs MIRPot
mirpot [StakeAddress]
vKeys [Entropic]
rewards OutputFile
out
runGovernanceCmd (GovernanceMIRTransfer Entropic
amt OutputFile
out TransferDirection
direction) =
  Entropic
-> OutputFile
-> TransferDirection
-> ExceptT SophieGovernanceCmdError IO ()
runGovernanceMIRCertificateTransfer Entropic
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 SophieGovernanceCmdError IO ()
runGovernanceGenesisKeyDelegationCertificate VerificationKeyOrHashOrFile GenesisKey
genVk VerificationKeyOrHashOrFile GenesisDelegateKey
genDelegVk VerificationKeyOrHashOrFile VrfKey
vrfVk OutputFile
out
runGovernanceCmd (GovernanceVestedKeyDelegationCertificate VerificationKeyOrHashOrFile VestedKey
genVk VerificationKeyOrHashOrFile VestedDelegateKey
genDelegVk VerificationKeyOrHashOrFile VrfKey
vrfVk OutputFile
out) =
  VerificationKeyOrHashOrFile VestedKey
-> VerificationKeyOrHashOrFile VestedDelegateKey
-> VerificationKeyOrHashOrFile VrfKey
-> OutputFile
-> ExceptT SophieGovernanceCmdError IO ()
runGovernanceVestedKeyDelegationCertificate VerificationKeyOrHashOrFile VestedKey
genVk VerificationKeyOrHashOrFile VestedDelegateKey
genDelegVk VerificationKeyOrHashOrFile VrfKey
vrfVk OutputFile
out
runGovernanceCmd (GovernanceUpdateProposal OutputFile
out EpochNo
eNo [VerificationKeyFile]
genVKeys ProtocolParametersUpdate
ppUp) =
  OutputFile
-> EpochNo
-> [VerificationKeyFile]
-> ProtocolParametersUpdate
-> ExceptT SophieGovernanceCmdError IO ()
runGovernanceUpdateProposal OutputFile
out EpochNo
eNo [VerificationKeyFile]
genVKeys ProtocolParametersUpdate
ppUp

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

    Bool
-> ExceptT SophieGovernanceCmdError IO ()
-> ExceptT SophieGovernanceCmdError 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
== [Entropic] -> Int
forall a. HasLength a => a -> Int
length [Entropic]
rwdAmts) (ExceptT SophieGovernanceCmdError IO ()
 -> ExceptT SophieGovernanceCmdError IO ())
-> ExceptT SophieGovernanceCmdError IO ()
-> ExceptT SophieGovernanceCmdError IO ()
forall a b. (a -> b) -> a -> b
$
      SophieGovernanceCmdError -> ExceptT SophieGovernanceCmdError IO ()
forall (m :: * -> *) x a. Monad m => x -> ExceptT x m a
left (SophieGovernanceCmdError
 -> ExceptT SophieGovernanceCmdError IO ())
-> SophieGovernanceCmdError
-> ExceptT SophieGovernanceCmdError IO ()
forall a b. (a -> b) -> a -> b
$ String -> Int -> Int -> SophieGovernanceCmdError
SophieGovernanceCmdMIRCertificateKeyRewardMistmach
               String
oFp ([StakeAddress] -> Int
forall a. HasLength a => a -> Int
length [StakeAddress]
sAddrs) ([Entropic] -> Int
forall a. HasLength a => a -> Int
length [Entropic]
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, Entropic)] -> MIRTarget
StakeAddressesMIR ([(StakeCredential, Entropic)] -> MIRTarget)
-> [(StakeCredential, Entropic)] -> MIRTarget
forall a b. (a -> b) -> a -> b
$ [StakeCredential] -> [Entropic] -> [(StakeCredential, Entropic)]
forall a b. [a] -> [b] -> [(a, b)]
zip [StakeCredential]
sCreds [Entropic]
rwdAmts)

    (FileError () -> SophieGovernanceCmdError)
-> ExceptT (FileError ()) IO ()
-> ExceptT SophieGovernanceCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError () -> SophieGovernanceCmdError
SophieGovernanceCmdTextEnvWriteError
      (ExceptT (FileError ()) IO ()
 -> ExceptT SophieGovernanceCmdError IO ())
-> (IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ())
-> IO (Either (FileError ()) ())
-> ExceptT SophieGovernanceCmdError 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 SophieGovernanceCmdError IO ())
-> IO (Either (FileError ()) ())
-> ExceptT SophieGovernanceCmdError 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
fromSophieStakeCredential StakeCredential StandardCrypto
scred

runGovernanceMIRCertificateTransfer
  :: Entropic
  -> OutputFile
  -> TransferDirection
  -> ExceptT SophieGovernanceCmdError IO ()
runGovernanceMIRCertificateTransfer :: Entropic
-> OutputFile
-> TransferDirection
-> ExceptT SophieGovernanceCmdError IO ()
runGovernanceMIRCertificateTransfer Entropic
ll (OutputFile String
oFp) TransferDirection
direction = do
  Certificate
mirCert <- case TransferDirection
direction of
                 TransferDirection
TransferToReserves ->
                   Certificate -> ExceptT SophieGovernanceCmdError IO Certificate
forall (m :: * -> *) a. Monad m => a -> m a
return (Certificate -> ExceptT SophieGovernanceCmdError IO Certificate)
-> (MIRTarget -> Certificate)
-> MIRTarget
-> ExceptT SophieGovernanceCmdError 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
Sophie.TreasuryMIR (MIRTarget -> ExceptT SophieGovernanceCmdError IO Certificate)
-> MIRTarget -> ExceptT SophieGovernanceCmdError IO Certificate
forall a b. (a -> b) -> a -> b
$ Entropic -> MIRTarget
SendToReservesMIR Entropic
ll
                 TransferDirection
TransferToTreasury ->
                   Certificate -> ExceptT SophieGovernanceCmdError IO Certificate
forall (m :: * -> *) a. Monad m => a -> m a
return (Certificate -> ExceptT SophieGovernanceCmdError IO Certificate)
-> (MIRTarget -> Certificate)
-> MIRTarget
-> ExceptT SophieGovernanceCmdError 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
Sophie.ReservesMIR (MIRTarget -> ExceptT SophieGovernanceCmdError IO Certificate)
-> MIRTarget -> ExceptT SophieGovernanceCmdError IO Certificate
forall a b. (a -> b) -> a -> b
$ Entropic -> MIRTarget
SendToTreasuryMIR Entropic
ll

  (FileError () -> SophieGovernanceCmdError)
-> ExceptT (FileError ()) IO ()
-> ExceptT SophieGovernanceCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError () -> SophieGovernanceCmdError
SophieGovernanceCmdTextEnvWriteError
    (ExceptT (FileError ()) IO ()
 -> ExceptT SophieGovernanceCmdError IO ())
-> (IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ())
-> IO (Either (FileError ()) ())
-> ExceptT SophieGovernanceCmdError 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 SophieGovernanceCmdError IO ())
-> IO (Either (FileError ()) ())
-> ExceptT SophieGovernanceCmdError 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 SophieGovernanceCmdError IO ()
runGovernanceGenesisKeyDelegationCertificate :: VerificationKeyOrHashOrFile GenesisKey
-> VerificationKeyOrHashOrFile GenesisDelegateKey
-> VerificationKeyOrHashOrFile VrfKey
-> OutputFile
-> ExceptT SophieGovernanceCmdError IO ()
runGovernanceGenesisKeyDelegationCertificate VerificationKeyOrHashOrFile GenesisKey
genVkOrHashOrFp
                                             VerificationKeyOrHashOrFile GenesisDelegateKey
genDelVkOrHashOrFp
                                             VerificationKeyOrHashOrFile VrfKey
vrfVkOrHashOrFp
                                             (OutputFile String
oFp) = do
    Hash GenesisKey
genesisVkHash <- (FileError InputDecodeError -> SophieGovernanceCmdError)
-> ExceptT (FileError InputDecodeError) IO (Hash GenesisKey)
-> ExceptT SophieGovernanceCmdError IO (Hash GenesisKey)
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError InputDecodeError -> SophieGovernanceCmdError
SophieGovernanceCmdKeyReadError
      (ExceptT (FileError InputDecodeError) IO (Hash GenesisKey)
 -> ExceptT SophieGovernanceCmdError IO (Hash GenesisKey))
-> (IO (Either (FileError InputDecodeError) (Hash GenesisKey))
    -> ExceptT (FileError InputDecodeError) IO (Hash GenesisKey))
-> IO (Either (FileError InputDecodeError) (Hash GenesisKey))
-> ExceptT SophieGovernanceCmdError 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 SophieGovernanceCmdError IO (Hash GenesisKey))
-> IO (Either (FileError InputDecodeError) (Hash GenesisKey))
-> ExceptT SophieGovernanceCmdError 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 -> SophieGovernanceCmdError)
-> ExceptT
     (FileError InputDecodeError) IO (Hash GenesisDelegateKey)
-> ExceptT SophieGovernanceCmdError IO (Hash GenesisDelegateKey)
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError InputDecodeError -> SophieGovernanceCmdError
SophieGovernanceCmdKeyReadError
      (ExceptT (FileError InputDecodeError) IO (Hash GenesisDelegateKey)
 -> ExceptT SophieGovernanceCmdError IO (Hash GenesisDelegateKey))
-> (IO
      (Either (FileError InputDecodeError) (Hash GenesisDelegateKey))
    -> ExceptT
         (FileError InputDecodeError) IO (Hash GenesisDelegateKey))
-> IO
     (Either (FileError InputDecodeError) (Hash GenesisDelegateKey))
-> ExceptT SophieGovernanceCmdError 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 SophieGovernanceCmdError IO (Hash GenesisDelegateKey))
-> IO
     (Either (FileError InputDecodeError) (Hash GenesisDelegateKey))
-> ExceptT SophieGovernanceCmdError 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 -> SophieGovernanceCmdError)
-> ExceptT (FileError InputDecodeError) IO (Hash VrfKey)
-> ExceptT SophieGovernanceCmdError IO (Hash VrfKey)
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError InputDecodeError -> SophieGovernanceCmdError
SophieGovernanceCmdKeyReadError
      (ExceptT (FileError InputDecodeError) IO (Hash VrfKey)
 -> ExceptT SophieGovernanceCmdError IO (Hash VrfKey))
-> (IO (Either (FileError InputDecodeError) (Hash VrfKey))
    -> ExceptT (FileError InputDecodeError) IO (Hash VrfKey))
-> IO (Either (FileError InputDecodeError) (Hash VrfKey))
-> ExceptT SophieGovernanceCmdError 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 SophieGovernanceCmdError IO (Hash VrfKey))
-> IO (Either (FileError InputDecodeError) (Hash VrfKey))
-> ExceptT SophieGovernanceCmdError 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 () -> SophieGovernanceCmdError)
-> ExceptT (FileError ()) IO ()
-> ExceptT SophieGovernanceCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError () -> SophieGovernanceCmdError
SophieGovernanceCmdTextEnvWriteError
      (ExceptT (FileError ()) IO ()
 -> ExceptT SophieGovernanceCmdError IO ())
-> (IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ())
-> IO (Either (FileError ()) ())
-> ExceptT SophieGovernanceCmdError 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 SophieGovernanceCmdError IO ())
-> IO (Either (FileError ()) ())
-> ExceptT SophieGovernanceCmdError 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"

runGovernanceVestedKeyDelegationCertificate
  :: VerificationKeyOrHashOrFile VestedKey
  -> VerificationKeyOrHashOrFile VestedDelegateKey
  -> VerificationKeyOrHashOrFile VrfKey
  -> OutputFile
  -> ExceptT SophieGovernanceCmdError IO ()
runGovernanceVestedKeyDelegationCertificate :: VerificationKeyOrHashOrFile VestedKey
-> VerificationKeyOrHashOrFile VestedDelegateKey
-> VerificationKeyOrHashOrFile VrfKey
-> OutputFile
-> ExceptT SophieGovernanceCmdError IO ()
runGovernanceVestedKeyDelegationCertificate VerificationKeyOrHashOrFile VestedKey
genVkOrHashOrFp
                                             VerificationKeyOrHashOrFile VestedDelegateKey
genDelVkOrHashOrFp
                                             VerificationKeyOrHashOrFile VrfKey
vrfVkOrHashOrFp
                                             (OutputFile String
oFp) = do
    Hash VestedKey
vestedVkHash <- (FileError InputDecodeError -> SophieGovernanceCmdError)
-> ExceptT (FileError InputDecodeError) IO (Hash VestedKey)
-> ExceptT SophieGovernanceCmdError IO (Hash VestedKey)
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError InputDecodeError -> SophieGovernanceCmdError
SophieGovernanceCmdKeyReadError
      (ExceptT (FileError InputDecodeError) IO (Hash VestedKey)
 -> ExceptT SophieGovernanceCmdError IO (Hash VestedKey))
-> (IO (Either (FileError InputDecodeError) (Hash VestedKey))
    -> ExceptT (FileError InputDecodeError) IO (Hash VestedKey))
-> IO (Either (FileError InputDecodeError) (Hash VestedKey))
-> ExceptT SophieGovernanceCmdError IO (Hash VestedKey)
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 VestedKey))
-> ExceptT (FileError InputDecodeError) IO (Hash VestedKey)
forall (m :: * -> *) x a. m (Either x a) -> ExceptT x m a
newExceptT
      (IO (Either (FileError InputDecodeError) (Hash VestedKey))
 -> ExceptT SophieGovernanceCmdError IO (Hash VestedKey))
-> IO (Either (FileError InputDecodeError) (Hash VestedKey))
-> ExceptT SophieGovernanceCmdError IO (Hash VestedKey)
forall a b. (a -> b) -> a -> b
$ AsType VestedKey
-> VerificationKeyOrHashOrFile VestedKey
-> IO (Either (FileError InputDecodeError) (Hash VestedKey))
forall keyrole.
Key keyrole =>
AsType keyrole
-> VerificationKeyOrHashOrFile keyrole
-> IO (Either (FileError InputDecodeError) (Hash keyrole))
readVerificationKeyOrHashOrTextEnvFile AsType VestedKey
AsVestedKey VerificationKeyOrHashOrFile VestedKey
genVkOrHashOrFp
    Hash VestedDelegateKey
vestedDelVkHash <-(FileError InputDecodeError -> SophieGovernanceCmdError)
-> ExceptT (FileError InputDecodeError) IO (Hash VestedDelegateKey)
-> ExceptT SophieGovernanceCmdError IO (Hash VestedDelegateKey)
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError InputDecodeError -> SophieGovernanceCmdError
SophieGovernanceCmdKeyReadError
      (ExceptT (FileError InputDecodeError) IO (Hash VestedDelegateKey)
 -> ExceptT SophieGovernanceCmdError IO (Hash VestedDelegateKey))
-> (IO
      (Either (FileError InputDecodeError) (Hash VestedDelegateKey))
    -> ExceptT
         (FileError InputDecodeError) IO (Hash VestedDelegateKey))
-> IO
     (Either (FileError InputDecodeError) (Hash VestedDelegateKey))
-> ExceptT SophieGovernanceCmdError IO (Hash VestedDelegateKey)
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 VestedDelegateKey))
-> ExceptT (FileError InputDecodeError) IO (Hash VestedDelegateKey)
forall (m :: * -> *) x a. m (Either x a) -> ExceptT x m a
newExceptT
      (IO (Either (FileError InputDecodeError) (Hash VestedDelegateKey))
 -> ExceptT SophieGovernanceCmdError IO (Hash VestedDelegateKey))
-> IO
     (Either (FileError InputDecodeError) (Hash VestedDelegateKey))
-> ExceptT SophieGovernanceCmdError IO (Hash VestedDelegateKey)
forall a b. (a -> b) -> a -> b
$ AsType VestedDelegateKey
-> VerificationKeyOrHashOrFile VestedDelegateKey
-> IO
     (Either (FileError InputDecodeError) (Hash VestedDelegateKey))
forall keyrole.
Key keyrole =>
AsType keyrole
-> VerificationKeyOrHashOrFile keyrole
-> IO (Either (FileError InputDecodeError) (Hash keyrole))
readVerificationKeyOrHashOrTextEnvFile AsType VestedDelegateKey
AsVestedDelegateKey VerificationKeyOrHashOrFile VestedDelegateKey
genDelVkOrHashOrFp
    Hash VrfKey
vrfVkHash <- (FileError InputDecodeError -> SophieGovernanceCmdError)
-> ExceptT (FileError InputDecodeError) IO (Hash VrfKey)
-> ExceptT SophieGovernanceCmdError IO (Hash VrfKey)
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError InputDecodeError -> SophieGovernanceCmdError
SophieGovernanceCmdKeyReadError
      (ExceptT (FileError InputDecodeError) IO (Hash VrfKey)
 -> ExceptT SophieGovernanceCmdError IO (Hash VrfKey))
-> (IO (Either (FileError InputDecodeError) (Hash VrfKey))
    -> ExceptT (FileError InputDecodeError) IO (Hash VrfKey))
-> IO (Either (FileError InputDecodeError) (Hash VrfKey))
-> ExceptT SophieGovernanceCmdError 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 SophieGovernanceCmdError IO (Hash VrfKey))
-> IO (Either (FileError InputDecodeError) (Hash VrfKey))
-> ExceptT SophieGovernanceCmdError 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 () -> SophieGovernanceCmdError)
-> ExceptT (FileError ()) IO ()
-> ExceptT SophieGovernanceCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError () -> SophieGovernanceCmdError
SophieGovernanceCmdTextEnvWriteError
      (ExceptT (FileError ()) IO ()
 -> ExceptT SophieGovernanceCmdError IO ())
-> (IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ())
-> IO (Either (FileError ()) ())
-> ExceptT SophieGovernanceCmdError 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 SophieGovernanceCmdError IO ())
-> IO (Either (FileError ()) ())
-> ExceptT SophieGovernanceCmdError 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 VestedKey
-> Hash VestedDelegateKey -> Hash VrfKey -> Certificate
makeVestedKeyDelegationCertificate Hash VestedKey
vestedVkHash Hash VestedDelegateKey
vestedDelVkHash Hash VrfKey
vrfVkHash
  where
    genKeyDelegCertDesc :: TextEnvelopeDescr
    genKeyDelegCertDesc :: TextEnvelopeDescr
genKeyDelegCertDesc = TextEnvelopeDescr
"Vested Key Delegation Certificate"

runGovernanceUpdateProposal
  :: OutputFile
  -> EpochNo
  -> [VerificationKeyFile]
  -- ^ Genesis verification keys
  -> ProtocolParametersUpdate
  -> ExceptT SophieGovernanceCmdError IO ()
runGovernanceUpdateProposal :: OutputFile
-> EpochNo
-> [VerificationKeyFile]
-> ProtocolParametersUpdate
-> ExceptT SophieGovernanceCmdError IO ()
runGovernanceUpdateProposal (OutputFile String
upFile) EpochNo
eNo [VerificationKeyFile]
genVerKeyFiles ProtocolParametersUpdate
upPprams = do
    Bool
-> ExceptT SophieGovernanceCmdError IO ()
-> ExceptT SophieGovernanceCmdError 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 SophieGovernanceCmdError IO ()
 -> ExceptT SophieGovernanceCmdError IO ())
-> ExceptT SophieGovernanceCmdError IO ()
-> ExceptT SophieGovernanceCmdError IO ()
forall a b. (a -> b) -> a -> b
$ SophieGovernanceCmdError -> ExceptT SophieGovernanceCmdError IO ()
forall (m :: * -> *) x a. Monad m => x -> ExceptT x m a
left SophieGovernanceCmdError
SophieGovernanceCmdEmptyUpdateProposalError
    [VerificationKey GenesisKey]
genVKeys <- [ExceptT SophieGovernanceCmdError IO (VerificationKey GenesisKey)]
-> ExceptT SophieGovernanceCmdError IO [VerificationKey GenesisKey]
forall (t :: * -> *) (m :: * -> *) a.
(Traversable t, Monad m) =>
t (m a) -> m (t a)
sequence
                  [ (FileError TextEnvelopeError -> SophieGovernanceCmdError)
-> ExceptT
     (FileError TextEnvelopeError) IO (VerificationKey GenesisKey)
-> ExceptT SophieGovernanceCmdError IO (VerificationKey GenesisKey)
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError TextEnvelopeError -> SophieGovernanceCmdError
SophieGovernanceCmdTextEnvReadError (ExceptT
   (FileError TextEnvelopeError) IO (VerificationKey GenesisKey)
 -> ExceptT
      SophieGovernanceCmdError IO (VerificationKey GenesisKey))
-> (IO
      (Either (FileError TextEnvelopeError) (VerificationKey GenesisKey))
    -> ExceptT
         (FileError TextEnvelopeError) IO (VerificationKey GenesisKey))
-> IO
     (Either (FileError TextEnvelopeError) (VerificationKey GenesisKey))
-> ExceptT SophieGovernanceCmdError 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
      SophieGovernanceCmdError IO (VerificationKey GenesisKey))
-> IO
     (Either (FileError TextEnvelopeError) (VerificationKey GenesisKey))
-> ExceptT SophieGovernanceCmdError 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
makeSophieUpdateProposal ProtocolParametersUpdate
upPprams [Hash GenesisKey]
genKeyHashes EpochNo
eNo
    (FileError () -> SophieGovernanceCmdError)
-> ExceptT (FileError ()) IO ()
-> ExceptT SophieGovernanceCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError () -> SophieGovernanceCmdError
SophieGovernanceCmdTextEnvWriteError (ExceptT (FileError ()) IO ()
 -> ExceptT SophieGovernanceCmdError IO ())
-> (IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ())
-> IO (Either (FileError ()) ())
-> ExceptT SophieGovernanceCmdError 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 SophieGovernanceCmdError IO ())
-> IO (Either (FileError ()) ())
-> ExceptT SophieGovernanceCmdError 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