Skip to content

Commit

Permalink
Add a command to calculate DRep metadata hashes
Browse files Browse the repository at this point in the history
  • Loading branch information
carlhammann committed Sep 26, 2023
1 parent 23b02a2 commit 5739699
Show file tree
Hide file tree
Showing 20 changed files with 265 additions and 13 deletions.
21 changes: 14 additions & 7 deletions cardano-cli/src/Cardano/CLI/EraBased/Commands/Governance/DRep.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
{-# LANGUAGE LambdaCase #-}

module Cardano.CLI.EraBased.Commands.Governance.DRep
( GovernanceDRepCmds(..)
, renderGovernanceDRepCmds
) where
( GovernanceDRepCmds (..),
renderGovernanceDRepCmds,
)
where

import Cardano.Api

Expand All @@ -26,14 +27,20 @@ data GovernanceDRepCmds era
| GovernanceDRepRegistrationCertificateCmd
AnyRegistrationTarget
(File () Out)
| GovernanceDRepMetadataHashCmd
(DRepMetadataFile In)
(Maybe (File () Out))

renderGovernanceDRepCmds :: ()
=> GovernanceDRepCmds era
-> Text
renderGovernanceDRepCmds ::
() =>
GovernanceDRepCmds era ->
Text
renderGovernanceDRepCmds = \case
GovernanceDRepGenerateKeyCmd{} ->
GovernanceDRepGenerateKeyCmd {} ->
"governance drep key-gen"
GovernanceDRepIdCmd {} ->
"governance drep id"
GovernanceDRepRegistrationCertificateCmd {} ->
"governance drep registration-certificate"
GovernanceDRepMetadataHashCmd {} ->
"governance drep metadata-hash"
13 changes: 13 additions & 0 deletions cardano-cli/src/Cardano/CLI/EraBased/Options/Governance/DRep.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pGovernanceDRepCmds era envCli =
[ pGovernanceDRepKeyGenCmd era
, pGovernanceDRepKeyIdCmd era
, pRegistrationCertificateCmd era envCli
, pGovernanceDrepMetadataHashCmd
]

pGovernanceDRepKeyGenCmd :: ()
Expand Down Expand Up @@ -117,6 +118,18 @@ pRegistrationCertificateCmd era envCli = do
]
<*> pOutputFile

pGovernanceDrepMetadataHashCmd :: ()
=> Maybe (Parser (GovernanceDRepCmds era))
pGovernanceDrepMetadataHashCmd = do
pure
$ subParser "metadata-hash"
$ Opt.info
( GovernanceDRepMetadataHashCmd
<$> pFileInDirection "drep-metadata-file" "JSON Metadata file to hash."
<*> pMaybeOutputFile
)
$ Opt.progDesc "Calculate the hash of a metadata file."

--------------------------------------------------------------------------------

data AnyEraDecider era where
Expand Down
22 changes: 22 additions & 0 deletions cardano-cli/src/Cardano/CLI/EraBased/Run/Governance/DRep.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ import Cardano.CLI.Types.Errors.GovernanceCmdError
import Cardano.CLI.Types.Errors.RegistrationError
import Cardano.CLI.Types.Key

import Control.Monad.Trans (liftIO)
import Control.Monad.Trans.Class
import Control.Monad.Trans.Except
import Control.Monad.Trans.Except.Extra
import qualified Data.ByteString.Char8 as BS
import Data.Function
import qualified Data.Text.Encoding as Text

Expand All @@ -46,6 +48,10 @@ runGovernanceDRepCmds = \case
runGovernanceRegistrationCertificateCmd regTarget outFp
& firstExceptT CmdRegistrationError

GovernanceDRepMetadataHashCmd inFp mOutFp ->
runGovernanceDRepMetadataHashCmd inFp mOutFp
& firstExceptT CmdGovernanceCmdError

runGovernanceDRepIdCmd :: ()
=> ConwayEraOnwards era
-> VerificationKeyOrFile DRepKey
Expand Down Expand Up @@ -220,3 +226,19 @@ runGovernanceRegistrationCertificateCmd anyReg outfp =
. writeLazyByteStringFile outfp
$ conwayEraOnwardsConstraints cOnwards
$ textEnvelopeToJSON description registrationCert

runGovernanceDRepMetadataHashCmd ::
DRepMetadataFile In
-> Maybe (File () Out)
-> ExceptT GovernanceCmdError IO ()
runGovernanceDRepMetadataHashCmd drepMDPath mOutFile = do
metadataBytes <- firstExceptT ReadFileError $ newExceptT (readByteStringFile drepMDPath)
(_metadata, metadataHash) <-
firstExceptT GovernanceCmdDRepMetadataValidationError
. hoistEither
$ validateAndHashDRepMetadata metadataBytes
case mOutFile of
Nothing -> liftIO $ BS.putStrLn (serialiseToRawBytesHex metadataHash)
Just (File fpath) ->
handleIOExceptT (WriteFileError . FileIOError fpath)
$ BS.writeFile fpath (serialiseToRawBytesHex metadataHash)
3 changes: 3 additions & 0 deletions cardano-cli/src/Cardano/CLI/Types/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ module Cardano.CLI.Types.Common
, VerificationKeyFile
, WitnessFile(..)
, WitnessSigningData(..)
, DRepMetadataFile
) where

import Cardano.Api
Expand Down Expand Up @@ -470,6 +471,8 @@ data MetadataFile = MetadataFileJSON (File () In)

type StakePoolMetadataFile = File StakePoolMetadata

type DRepMetadataFile = File DRepMetadata

newtype GenesisDir
= GenesisDir FilePath
deriving Show
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ data GovernanceCmdError
| GovernanceCmdDecoderError !DecoderError
| GovernanceCmdVerifyPollError !GovernancePollError
| GovernanceCmdWriteFileError !(FileError ())
| GovernanceCmdDRepMetadataValidationError !DRepMetadataValidationError
-- Legacy - remove me after cardano-cli transitions to new era based structure
| GovernanceCmdMIRCertNotSupportedInConway
| GovernanceCmdGenesisDelegationNotSupportedInConway
Expand Down Expand Up @@ -104,6 +105,8 @@ instance Error GovernanceCmdError where
Text.unpack (renderGovernancePollError pollError)
GovernanceCmdWriteFileError fileError ->
"Cannot write file: " <> displayError fileError
GovernanceCmdDRepMetadataValidationError e ->
"DRep metadata validation error: " <> displayError e
GovernanceCmdMIRCertNotSupportedInConway ->
"MIR certificates are not supported in Conway era onwards."
GovernanceCmdGenesisDelegationNotSupportedInConway ->
Expand Down
66 changes: 60 additions & 6 deletions cardano-cli/test/cardano-cli-golden/files/golden/help.cli
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,10 @@ Usage: cardano-cli shelley governance action create-protocol-parameters-update -

Create a protocol parameters update.

Usage: cardano-cli shelley governance drep registration-certificate
Usage: cardano-cli shelley governance drep
( registration-certificate
| metadata-hash
)

DRep member commands.

Expand Down Expand Up @@ -373,6 +376,11 @@ Usage: cardano-cli shelley governance drep registration-certificate

Create a registration certificate.

Usage: cardano-cli shelley governance drep metadata-hash --drep-metadata-file FILE
[--out-file FILE]

Calculate the hash of a metadata file.

Usage: cardano-cli shelley node
( key-gen
| key-gen-KES
Expand Down Expand Up @@ -1556,7 +1564,10 @@ Usage: cardano-cli allegra governance action create-protocol-parameters-update -

Create a protocol parameters update.

Usage: cardano-cli allegra governance drep registration-certificate
Usage: cardano-cli allegra governance drep
( registration-certificate
| metadata-hash
)

DRep member commands.

Expand Down Expand Up @@ -1597,6 +1608,11 @@ Usage: cardano-cli allegra governance drep registration-certificate

Create a registration certificate.

Usage: cardano-cli allegra governance drep metadata-hash --drep-metadata-file FILE
[--out-file FILE]

Calculate the hash of a metadata file.

Usage: cardano-cli allegra node
( key-gen
| key-gen-KES
Expand Down Expand Up @@ -2778,7 +2794,10 @@ Usage: cardano-cli mary governance action create-protocol-parameters-update --ep

Create a protocol parameters update.

Usage: cardano-cli mary governance drep registration-certificate
Usage: cardano-cli mary governance drep
( registration-certificate
| metadata-hash
)

DRep member commands.

Expand Down Expand Up @@ -2819,6 +2838,11 @@ Usage: cardano-cli mary governance drep registration-certificate

Create a registration certificate.

Usage: cardano-cli mary governance drep metadata-hash --drep-metadata-file FILE
[--out-file FILE]

Calculate the hash of a metadata file.

Usage: cardano-cli mary node
( key-gen
| key-gen-KES
Expand Down Expand Up @@ -3981,7 +4005,10 @@ Usage: cardano-cli alonzo governance action create-protocol-parameters-update --

Create a protocol parameters update.

Usage: cardano-cli alonzo governance drep registration-certificate
Usage: cardano-cli alonzo governance drep
( registration-certificate
| metadata-hash
)

DRep member commands.

Expand Down Expand Up @@ -4022,6 +4049,11 @@ Usage: cardano-cli alonzo governance drep registration-certificate

Create a registration certificate.

Usage: cardano-cli alonzo governance drep metadata-hash --drep-metadata-file FILE
[--out-file FILE]

Calculate the hash of a metadata file.

Usage: cardano-cli alonzo node
( key-gen
| key-gen-KES
Expand Down Expand Up @@ -5207,7 +5239,10 @@ Usage: cardano-cli babbage governance action create-protocol-parameters-update -

Create a protocol parameters update.

Usage: cardano-cli babbage governance drep registration-certificate
Usage: cardano-cli babbage governance drep
( registration-certificate
| metadata-hash
)

DRep member commands.

Expand Down Expand Up @@ -5248,6 +5283,11 @@ Usage: cardano-cli babbage governance drep registration-certificate

Create a registration certificate.

Usage: cardano-cli babbage governance drep metadata-hash --drep-metadata-file FILE
[--out-file FILE]

Calculate the hash of a metadata file.

Usage: cardano-cli babbage node
( key-gen
| key-gen-KES
Expand Down Expand Up @@ -6688,6 +6728,7 @@ Usage: cardano-cli conway governance drep
( key-gen
| id
| registration-certificate
| metadata-hash
)

DRep member commands.
Expand Down Expand Up @@ -6750,6 +6791,11 @@ Usage: cardano-cli conway governance drep registration-certificate

Create a registration certificate.

Usage: cardano-cli conway governance drep metadata-hash --drep-metadata-file FILE
[--out-file FILE]

Calculate the hash of a metadata file.

Usage: cardano-cli conway governance vote create

Vote commands.
Expand Down Expand Up @@ -7993,7 +8039,10 @@ Usage: cardano-cli latest governance action create-protocol-parameters-update --

Create a protocol parameters update.

Usage: cardano-cli latest governance drep registration-certificate
Usage: cardano-cli latest governance drep
( registration-certificate
| metadata-hash
)

DRep member commands.

Expand Down Expand Up @@ -8034,6 +8083,11 @@ Usage: cardano-cli latest governance drep registration-certificate

Create a registration certificate.

Usage: cardano-cli latest governance drep metadata-hash --drep-metadata-file FILE
[--out-file FILE]

Calculate the hash of a metadata file.

Usage: cardano-cli latest node
( key-gen
| key-gen-KES
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Usage: cardano-cli allegra governance drep
( registration-certificate
| metadata-hash
)

DRep member commands.

Available options:
-h,--help Show this help text

Available commands:
registration-certificate Create a registration certificate.
metadata-hash Calculate the hash of a metadata file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Usage: cardano-cli allegra governance drep metadata-hash --drep-metadata-file FILE
[--out-file FILE]

Calculate the hash of a metadata file.

Available options:
--drep-metadata-file FILE
JSON Metadata file to hash.
--out-file FILE Optional output file. Default is to write to stdout.
-h,--help Show this help text
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Usage: cardano-cli alonzo governance drep
( registration-certificate
| metadata-hash
)

DRep member commands.

Available options:
-h,--help Show this help text

Available commands:
registration-certificate Create a registration certificate.
metadata-hash Calculate the hash of a metadata file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Usage: cardano-cli alonzo governance drep metadata-hash --drep-metadata-file FILE
[--out-file FILE]

Calculate the hash of a metadata file.

Available options:
--drep-metadata-file FILE
JSON Metadata file to hash.
--out-file FILE Optional output file. Default is to write to stdout.
-h,--help Show this help text
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Usage: cardano-cli babbage governance drep
( registration-certificate
| metadata-hash
)

DRep member commands.

Available options:
-h,--help Show this help text

Available commands:
registration-certificate Create a registration certificate.
metadata-hash Calculate the hash of a metadata file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Usage: cardano-cli babbage governance drep metadata-hash --drep-metadata-file FILE
[--out-file FILE]

Calculate the hash of a metadata file.

Available options:
--drep-metadata-file FILE
JSON Metadata file to hash.
--out-file FILE Optional output file. Default is to write to stdout.
-h,--help Show this help text
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Usage: cardano-cli conway governance drep
( key-gen
| id
| registration-certificate
| metadata-hash
)

DRep member commands.
Expand All @@ -14,3 +15,4 @@ Available commands:
signing keys.
id Generate a drep id.
registration-certificate Create a registration certificate.
metadata-hash Calculate the hash of a metadata file.
Loading

0 comments on commit 5739699

Please sign in to comment.