Skip to content

Commit

Permalink
Merge pull request #308 from input-output-hk/ch/calculate-drep-metada…
Browse files Browse the repository at this point in the history
…ta-hash

Add a command to calculate DRep metadata hashes
  • Loading branch information
carlhammann authored Oct 12, 2023
2 parents 4001f53 + 0820946 commit 76f42d7
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 6 deletions.
15 changes: 11 additions & 4 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
import qualified Cardano.Api.Ledger as Ledger
Expand Down Expand Up @@ -36,16 +37,22 @@ data GovernanceDRepCmds era
(VerificationKeyOrHashOrFile DRepKey)
Lovelace
(File () Out)
| GovernanceDRepMetadataHashCmd
(ConwayEraOnwards era)
(DRepMetadataFile In)
(Maybe (File () Out))

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

pGovernanceDRepKeyGenCmd :: ()
Expand Down Expand Up @@ -122,7 +123,6 @@ pDrepMetadataHash =
, Opt.help "DRep anchor data hash."
]


pRetirementCertificateCmd :: ()
=> CardanoEra era
-> Maybe (Parser (GovernanceDRepCmds era))
Expand All @@ -138,6 +138,20 @@ pRetirementCertificateCmd era = do
)
$ Opt.progDesc "Create a DRep retirement certificate."

pGovernanceDrepMetadataHashCmd :: ()
=> CardanoEra era
-> Maybe (Parser (GovernanceDRepCmds era))
pGovernanceDrepMetadataHashCmd era = do
w <- forEraMaybeEon era
pure
$ subParser "metadata-hash"
$ Opt.info
( GovernanceDRepMetadataHashCmd w
<$> 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
20 changes: 20 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 @@ -51,6 +51,10 @@ runGovernanceDRepCmds = \case
runGovernanceDrepRetirementCertificateCmd w vkeyOrHashOrFile deposit outFp
& firstExceptT CmdGovernanceCmdError

GovernanceDRepMetadataHashCmd _ inFp mOutFp ->
runGovernanceDRepMetadataHashCmd inFp mOutFp
& firstExceptT CmdGovernanceCmdError

runGovernanceDRepIdCmd :: ()
=> ConwayEraOnwards era
-> VerificationKeyOrFile DRepKey
Expand Down Expand Up @@ -115,3 +119,19 @@ runGovernanceDrepRetirementCertificateCmd w vKeyOrHashOrFile deposit outFile =
where
genKeyDelegCertDesc :: TextEnvelopeDescr
genKeyDelegCertDesc = "DRep Retirement Certificate"

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
firstExceptT WriteFileError
. newExceptT
. writeByteStringOutput mOutFile
. 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 @@ -79,6 +79,7 @@ module Cardano.CLI.Types.Common
, VoteHashSource(..)
, WitnessFile(..)
, WitnessSigningData(..)
, DRepMetadataFile
) where

import Cardano.Api
Expand Down Expand Up @@ -503,6 +504,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
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ module Test.Golden.Governance.DRep where

import Control.Monad (void)

import Test.Cardano.CLI.Util (execCardanoCLI, noteInputFile, propertyOnce)
import Test.Cardano.CLI.Util (execCardanoCLI, noteInputFile, propertyOnce, noteTempFile)

import Hedgehog
import qualified Hedgehog as H
import qualified Hedgehog.Extras.Test.Base as H
import qualified Hedgehog.Extras.Test.File as H
import qualified Hedgehog.Extras.Test.Golden as H
Expand Down Expand Up @@ -76,3 +77,20 @@ hprop_golden_governance_drep_retirement_certificate =

H.assertFileOccurences 1 "CertificateShelley" certFile
H.assertFileOccurences 1 "DRep Retirement Certificate" certFile

hprop_golden_governance_drep_metadata_hash :: Property
hprop_golden_governance_drep_metadata_hash = propertyOnce . H.moduleWorkspace "tmp" $ \tempDir -> do
goldenDRepMetadataHash <- noteInputFile "test/cardano-cli-golden/files/golden/governance/drep/drep_metadata_hash"

drepMetadataFile <- noteTempFile tempDir "drep-metadata.json"
H.evalIO $ writeFile drepMetadataFile "{ \"Lorem\": \"ipsum\", \"dolor\": \"sit\", \"amet\": \"consectetur\" }"

outputDRepMetadataHash <- H.noteTempFile tempDir "drep-metadata-hash.txt"

void $ execCardanoCLI
[ "conway", "governance", "drep","metadata-hash"
, "--drep-metadata-file", drepMetadataFile
, "--out-file", outputDRepMetadataHash
]

H.diffFileVsGoldenFile goldenDRepMetadataHash outputDRepMetadataHash
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
c9c4e907d758c260823be6999dbbcf2826ac1cdc0fe5f0c9394ac7fa17c74811
6 changes: 6 additions & 0 deletions cardano-cli/test/cardano-cli-golden/files/golden/help.cli
Original file line number Diff line number Diff line change
Expand Up @@ -6287,6 +6287,7 @@ Usage: cardano-cli conway governance drep
| id
| registration-certificate
| retirement-certificate
| metadata-hash
)

DRep member commands.
Expand Down Expand Up @@ -6327,6 +6328,11 @@ Usage: cardano-cli conway governance drep retirement-certificate

Create a DRep retirement 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 | view)

Vote commands.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Usage: cardano-cli conway governance drep
| id
| registration-certificate
| retirement-certificate
| metadata-hash
)

DRep member commands.
Expand All @@ -16,3 +17,4 @@ Available commands:
id Generate a drep id.
registration-certificate Create a registration certificate.
retirement-certificate Create a DRep retirement 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 conway 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

0 comments on commit 76f42d7

Please sign in to comment.