{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}

module Cardano.CLI.EraBased.Run.Governance.GenesisKeyDelegationCertificate
  ( runGovernanceGenesisKeyDelegationCertificate
  )
where

import           Cardano.Api
import           Cardano.Api.Shelley

import           Cardano.CLI.Types.Errors.GovernanceCmdError
import           Cardano.CLI.Types.Key

runGovernanceGenesisKeyDelegationCertificate
  :: ShelleyToBabbageEra era
  -> VerificationKeyOrHashOrFile GenesisKey
  -> VerificationKeyOrHashOrFile GenesisDelegateKey
  -> VerificationKeyOrHashOrFile VrfKey
  -> File () Out
  -> ExceptT GovernanceCmdError IO ()
runGovernanceGenesisKeyDelegationCertificate :: forall era.
ShelleyToBabbageEra era
-> VerificationKeyOrHashOrFile GenesisKey
-> VerificationKeyOrHashOrFile GenesisDelegateKey
-> VerificationKeyOrHashOrFile VrfKey
-> File () 'Out
-> ExceptT GovernanceCmdError IO ()
runGovernanceGenesisKeyDelegationCertificate
  ShelleyToBabbageEra era
stb
  VerificationKeyOrHashOrFile GenesisKey
genVkOrHashOrFp
  VerificationKeyOrHashOrFile GenesisDelegateKey
genDelVkOrHashOrFp
  VerificationKeyOrHashOrFile VrfKey
vrfVkOrHashOrFp
  File () 'Out
oFp = do
    Hash GenesisKey
genesisVkHash <-
      (FileError InputDecodeError -> GovernanceCmdError)
-> ExceptT (FileError InputDecodeError) IO (Hash GenesisKey)
-> ExceptT GovernanceCmdError IO (Hash GenesisKey)
forall e' (t :: (* -> *) -> * -> *) (m :: * -> *) e a.
MonadTransError e' t m =>
(e -> e') -> ExceptT e m a -> t m a
modifyError FileError InputDecodeError -> GovernanceCmdError
GovernanceCmdKeyReadError (ExceptT (FileError InputDecodeError) IO (Hash GenesisKey)
 -> ExceptT GovernanceCmdError IO (Hash GenesisKey))
-> ExceptT (FileError InputDecodeError) IO (Hash GenesisKey)
-> ExceptT GovernanceCmdError IO (Hash GenesisKey)
forall a b. (a -> b) -> a -> b
$
        AsType GenesisKey
-> VerificationKeyOrHashOrFile GenesisKey
-> ExceptT (FileError InputDecodeError) IO (Hash GenesisKey)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) keyrole.
(MonadIOTransError (FileError InputDecodeError) t m,
 Key keyrole) =>
AsType keyrole
-> VerificationKeyOrHashOrFile keyrole -> t m (Hash keyrole)
readVerificationKeyOrHashOrTextEnvFile AsType GenesisKey
AsGenesisKey VerificationKeyOrHashOrFile GenesisKey
genVkOrHashOrFp
    Hash GenesisDelegateKey
genesisDelVkHash <-
      (FileError InputDecodeError -> GovernanceCmdError)
-> ExceptT
     (FileError InputDecodeError) IO (Hash GenesisDelegateKey)
-> ExceptT GovernanceCmdError IO (Hash GenesisDelegateKey)
forall e' (t :: (* -> *) -> * -> *) (m :: * -> *) e a.
MonadTransError e' t m =>
(e -> e') -> ExceptT e m a -> t m a
modifyError FileError InputDecodeError -> GovernanceCmdError
GovernanceCmdKeyReadError (ExceptT (FileError InputDecodeError) IO (Hash GenesisDelegateKey)
 -> ExceptT GovernanceCmdError IO (Hash GenesisDelegateKey))
-> ExceptT
     (FileError InputDecodeError) IO (Hash GenesisDelegateKey)
-> ExceptT GovernanceCmdError IO (Hash GenesisDelegateKey)
forall a b. (a -> b) -> a -> b
$
        AsType GenesisDelegateKey
-> VerificationKeyOrHashOrFile GenesisDelegateKey
-> ExceptT
     (FileError InputDecodeError) IO (Hash GenesisDelegateKey)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) keyrole.
(MonadIOTransError (FileError InputDecodeError) t m,
 Key keyrole) =>
AsType keyrole
-> VerificationKeyOrHashOrFile keyrole -> t m (Hash keyrole)
readVerificationKeyOrHashOrTextEnvFile AsType GenesisDelegateKey
AsGenesisDelegateKey VerificationKeyOrHashOrFile GenesisDelegateKey
genDelVkOrHashOrFp
    Hash VrfKey
vrfVkHash <-
      (FileError InputDecodeError -> GovernanceCmdError)
-> ExceptT (FileError InputDecodeError) IO (Hash VrfKey)
-> ExceptT GovernanceCmdError IO (Hash VrfKey)
forall e' (t :: (* -> *) -> * -> *) (m :: * -> *) e a.
MonadTransError e' t m =>
(e -> e') -> ExceptT e m a -> t m a
modifyError FileError InputDecodeError -> GovernanceCmdError
GovernanceCmdKeyReadError (ExceptT (FileError InputDecodeError) IO (Hash VrfKey)
 -> ExceptT GovernanceCmdError IO (Hash VrfKey))
-> ExceptT (FileError InputDecodeError) IO (Hash VrfKey)
-> ExceptT GovernanceCmdError IO (Hash VrfKey)
forall a b. (a -> b) -> a -> b
$
        AsType VrfKey
-> VerificationKeyOrHashOrFile VrfKey
-> ExceptT (FileError InputDecodeError) IO (Hash VrfKey)
forall (t :: (* -> *) -> * -> *) (m :: * -> *) keyrole.
(MonadIOTransError (FileError InputDecodeError) t m, Key keyrole,
 SerialiseAsBech32 (VerificationKey keyrole)) =>
AsType keyrole
-> VerificationKeyOrHashOrFile keyrole -> t m (Hash keyrole)
readVerificationKeyOrHashOrFile AsType VrfKey
AsVrfKey VerificationKeyOrHashOrFile VrfKey
vrfVkOrHashOrFp

    let req :: GenesisKeyDelegationRequirements era
req = ShelleyToBabbageEra era
-> Hash GenesisKey
-> Hash GenesisDelegateKey
-> Hash VrfKey
-> GenesisKeyDelegationRequirements era
forall ere.
ShelleyToBabbageEra ere
-> Hash GenesisKey
-> Hash GenesisDelegateKey
-> Hash VrfKey
-> GenesisKeyDelegationRequirements ere
GenesisKeyDelegationRequirements ShelleyToBabbageEra era
stb Hash GenesisKey
genesisVkHash Hash GenesisDelegateKey
genesisDelVkHash Hash VrfKey
vrfVkHash
        genKeyDelegCert :: Certificate era
genKeyDelegCert = GenesisKeyDelegationRequirements era -> Certificate era
forall era. GenesisKeyDelegationRequirements era -> Certificate era
makeGenesisKeyDelegationCertificate GenesisKeyDelegationRequirements era
req

    (FileError () -> GovernanceCmdError)
-> ExceptT (FileError ()) IO () -> ExceptT GovernanceCmdError IO ()
forall (m :: * -> *) x y a.
Functor m =>
(x -> y) -> ExceptT x m a -> ExceptT y m a
firstExceptT FileError () -> GovernanceCmdError
GovernanceCmdTextEnvWriteError
      (ExceptT (FileError ()) IO () -> ExceptT GovernanceCmdError IO ())
-> (IO (Either (FileError ()) ()) -> ExceptT (FileError ()) IO ())
-> IO (Either (FileError ()) ())
-> ExceptT GovernanceCmdError IO ()
forall b c a. (b -> c) -> (a -> b) -> 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 GovernanceCmdError IO ())
-> IO (Either (FileError ()) ())
-> ExceptT GovernanceCmdError IO ()
forall a b. (a -> b) -> a -> b
$ File () 'Out -> ByteString -> IO (Either (FileError ()) ())
forall (m :: * -> *) content e.
MonadIO m =>
File content 'Out -> ByteString -> m (Either (FileError e) ())
writeLazyByteStringFile File () 'Out
oFp
      (ByteString -> IO (Either (FileError ()) ()))
-> ByteString -> IO (Either (FileError ()) ())
forall a b. (a -> b) -> a -> b
$ ShelleyBasedEra era
-> (ShelleyBasedEraConstraints era => ByteString) -> ByteString
forall era a.
ShelleyBasedEra era -> (ShelleyBasedEraConstraints era => a) -> a
shelleyBasedEraConstraints (ShelleyToBabbageEra era -> ShelleyBasedEra era
forall era. ShelleyToBabbageEra era -> ShelleyBasedEra era
shelleyToBabbageEraToShelleyBasedEra ShelleyToBabbageEra era
stb)
      ((ShelleyBasedEraConstraints era => ByteString) -> ByteString)
-> (ShelleyBasedEraConstraints era => ByteString) -> ByteString
forall a b. (a -> b) -> a -> b
$ Maybe TextEnvelopeDescr -> Certificate era -> ByteString
forall a.
HasTextEnvelope a =>
Maybe TextEnvelopeDescr -> a -> ByteString
textEnvelopeToJSON (TextEnvelopeDescr -> Maybe TextEnvelopeDescr
forall a. a -> Maybe a
Just TextEnvelopeDescr
genKeyDelegCertDesc) Certificate era
genKeyDelegCert
   where
    genKeyDelegCertDesc :: TextEnvelopeDescr
    genKeyDelegCertDesc :: TextEnvelopeDescr
genKeyDelegCertDesc = TextEnvelopeDescr
"Genesis Key Delegation Certificate"