Skip to content

Commit

Permalink
Merge pull request #194 from input-output-hk/newhoggy/new-governance-…
Browse files Browse the repository at this point in the history
…drep-id-command

New `governance drep id` command
  • Loading branch information
newhoggy authored Aug 21, 2023
2 parents dce9584 + 8be1bae commit 8446431
Show file tree
Hide file tree
Showing 19 changed files with 213 additions and 31 deletions.
18 changes: 13 additions & 5 deletions cardano-cli/src/Cardano/CLI/EraBased/Commands/Governance/DRep.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module Cardano.CLI.EraBased.Commands.Governance.DRep

import Cardano.Api

import Cardano.CLI.Types.Common
import Cardano.CLI.Types.Key

import Data.Text (Text)
Expand All @@ -17,21 +18,28 @@ data GovernanceDRepCmds era
StakeIdentifier
AnyDelegationTarget
(File () Out)
| GovernanceDRepRegistrationCertificateCmd
AnyRegistrationTarget
(File () Out)
| GovernanceDRepGenerateKey
(ConwayEraOnwards era)
(File (VerificationKey ()) Out)
(File (SigningKey ()) Out)
| GovernanceDRepIdCmd
(ConwayEraOnwards era)
(VerificationKeyOrFile DRepKey)
IdOutputFormat
(Maybe (File () Out))
| GovernanceDRepRegistrationCertificateCmd
AnyRegistrationTarget
(File () Out)

renderGovernanceDRepCmds :: ()
=> GovernanceDRepCmds era
-> Text
renderGovernanceDRepCmds = \case
GovernanceDRepDelegationCertificateCmd {} ->
"governance drep delegation-certificate"
GovernanceDRepRegistrationCertificateCmd {} ->
"governance drep registration-certificate"
GovernanceDRepGenerateKey{} ->
"governance drep key-gen"
GovernanceDRepIdCmd {} ->
"governance drep id"
GovernanceDRepRegistrationCertificateCmd {} ->
"governance drep registration-certificate"
6 changes: 3 additions & 3 deletions cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1388,16 +1388,16 @@ pKeyOutputFormat =
, Opt.value KeyOutputFormatTextEnvelope
]

pPoolIdOutputFormat :: Parser PoolIdOutputFormat
pPoolIdOutputFormat :: Parser IdOutputFormat
pPoolIdOutputFormat =
Opt.option readPoolIdOutputFormat $ mconcat
Opt.option readIdOutputFormat $ mconcat
[ Opt.long "output-format"
, Opt.metavar "STRING"
, Opt.help $ mconcat
[ "Optional pool id output format. Accepted output formats are \"hex\" "
, "and \"bech32\" (default is \"bech32\")."
]
, Opt.value PoolIdOutputFormatBech32
, Opt.value IdOutputFormatBech32
]

pMaybeOutputFile :: Parser (Maybe (File content Out))
Expand Down
33 changes: 33 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 @@ -2,6 +2,8 @@
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}

{-# OPTIONS_GHC -Wno-unused-imports #-}

module Cardano.CLI.EraBased.Options.Governance.DRep
( pGovernanceDRepCmds
) where
Expand All @@ -11,8 +13,11 @@ import Cardano.Api
import Cardano.CLI.Environment
import Cardano.CLI.EraBased.Commands.Governance.DRep
import Cardano.CLI.EraBased.Options.Common
import Cardano.CLI.Parser
import Cardano.CLI.Types.Common
import Cardano.CLI.Types.Key

import Control.Applicative
import Data.Foldable
import Data.String
import Options.Applicative (Parser)
Expand All @@ -30,6 +35,7 @@ pGovernanceDRepCmds envCli era =
]
)
[ pGovernanceDRepKeyGen era
, pGovernanceDRepKeyId era
, pEraBasedDelegationCertificateCmd envCli era
, pEraBasedRegistrationCertificateCmd envCli era
]
Expand All @@ -48,6 +54,33 @@ pGovernanceDRepKeyGen era = do
)
$ Opt.progDesc "Generate Delegate Representative verification and signing keys."

pGovernanceDRepKeyId :: ()
=> CardanoEra era
-> Maybe (Parser (GovernanceDRepCmds era))
pGovernanceDRepKeyId era = do
w <- maybeFeatureInEra era
pure
$ subParser "id"
$ Opt.info
( GovernanceDRepIdCmd w
<$> pDRepVerificationKeyOrFile
<*> pDRepIdOutputFormat
<*> optional pOutputFile
)
$ Opt.progDesc "Generate a drep id."

pDRepIdOutputFormat :: Parser IdOutputFormat
pDRepIdOutputFormat =
Opt.option readIdOutputFormat $ mconcat
[ Opt.long "output-format"
, Opt.metavar "STRING"
, Opt.help $ mconcat
[ "Optional drep id output format. Accepted output formats are \"hex\" "
, "and \"bech32\" (default is \"bech32\")."
]
, Opt.value IdOutputFormatBech32
]

-- Registration Certificate related

pEraBasedRegistrationCertificateCmd :: ()
Expand Down
36 changes: 32 additions & 4 deletions cardano-cli/src/Cardano/CLI/EraBased/Run/Governance/DRep.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}

{- HLINT ignore "Use let" -}

module Cardano.CLI.EraBased.Run.Governance.DRep
( runGovernanceDRepCmds
) where
Expand All @@ -16,13 +18,16 @@ import Cardano.Api.Shelley
import Cardano.CLI.EraBased.Commands.Governance.DRep
import Cardano.CLI.EraBased.Run.Governance
import Cardano.CLI.Read
import Cardano.CLI.Types.Common
import Cardano.CLI.Types.Errors.CmdError
import Cardano.CLI.Types.Errors.GovernanceCmdError
import Cardano.CLI.Types.Key

import Control.Monad.Trans.Class
import Control.Monad.Trans.Except
import Control.Monad.Trans.Except.Extra
import Data.Function
import qualified Data.Text.Encoding as Text

runGovernanceDRepCmds :: ()
=> GovernanceDRepCmds era
Expand All @@ -32,14 +37,18 @@ runGovernanceDRepCmds = \case
runGovernanceDelegationCertificateCmd stakeIdentifier delegationTarget outFp
& firstExceptT CmdEraDelegationError

GovernanceDRepRegistrationCertificateCmd regTarget outFp ->
runGovernanceRegistrationCertificateCmd regTarget outFp
& firstExceptT CmdEraBasedRegistrationError

GovernanceDRepGenerateKey w vrf sgn ->
runGovernanceDRepKeyGen w vrf sgn
& firstExceptT CmdGovernanceCmdError

GovernanceDRepIdCmd w vkey idOutputFormat mOutFp ->
runGovernanceDRepIdCmd w vkey idOutputFormat mOutFp
& firstExceptT CmdGovernanceCmdError

GovernanceDRepRegistrationCertificateCmd regTarget outFp ->
runGovernanceRegistrationCertificateCmd regTarget outFp
& firstExceptT CmdEraBasedRegistrationError

runGovernanceDelegationCertificateCmd
:: StakeIdentifier
-> AnyDelegationTarget
Expand Down Expand Up @@ -114,6 +123,25 @@ toLedgerDelegatee t =
error "TODO: Conway era - DRepScriptHash not exposed by ledger yet"
-- right $ Ledger.DelegVote $ Ledger.DRepScriptHash scriptHash

runGovernanceDRepIdCmd :: ()
=> ConwayEraOnwards era
-> VerificationKeyOrFile DRepKey
-> IdOutputFormat
-> Maybe (File () Out)
-> ExceptT GovernanceCmdError IO ()
runGovernanceDRepIdCmd _ vkOrFp idOutputFormat mOutFile = do
drepVerKey <-
lift (readVerificationKeyOrTextEnvFile AsDRepKey vkOrFp)
& onLeft (left . ReadFileError)

content <-
pure $ case idOutputFormat of
IdOutputFormatHex -> serialiseToRawBytesHex $ verificationKeyHash drepVerKey
IdOutputFormatBech32 -> Text.encodeUtf8 $ serialiseToBech32 $ verificationKeyHash drepVerKey

lift (writeByteStringOutput mOutFile content)
& onLeft (left . WriteFileError)

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

-- Registration Certificate related
Expand Down
5 changes: 4 additions & 1 deletion cardano-cli/src/Cardano/CLI/Legacy/Commands/Pool.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ data LegacyPoolCmds
EpochNo
-- ^ Epoch in which to retire the stake pool.
(File () Out)
| PoolGetId (VerificationKeyOrFile StakePoolKey) PoolIdOutputFormat (Maybe (File () Out))
| PoolGetId
(VerificationKeyOrFile StakePoolKey)
IdOutputFormat
(Maybe (File () Out))
| PoolMetadataHash (StakePoolMetadataFile In) (Maybe (File () Out))
deriving Show

Expand Down
6 changes: 5 additions & 1 deletion cardano-cli/src/Cardano/CLI/Legacy/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,11 @@ pPoolCmds envCli =
]
where
pId :: Parser LegacyPoolCmds
pId = PoolGetId <$> pStakePoolVerificationKeyOrFile <*> pPoolIdOutputFormat <*> pMaybeOutputFile
pId =
PoolGetId
<$> pStakePoolVerificationKeyOrFile
<*> pPoolIdOutputFormat
<*> pMaybeOutputFile

pPoolMetadataHashSubCmd :: Parser LegacyPoolCmds
pPoolMetadataHashSubCmd = PoolMetadataHash <$> pPoolMetadataFile <*> pMaybeOutputFile
Expand Down
6 changes: 3 additions & 3 deletions cardano-cli/src/Cardano/CLI/Legacy/Run/Pool.hs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ createStakePoolRetirementRequirements sbe pid epoch =

runPoolId
:: VerificationKeyOrFile StakePoolKey
-> PoolIdOutputFormat
-> IdOutputFormat
-> Maybe (File () Out)
-> ExceptT ShelleyPoolCmdError IO ()
runPoolId verKeyOrFile outputFormat mOutFile = do
Expand All @@ -214,12 +214,12 @@ runPoolId verKeyOrFile outputFormat mOutFile = do
$ readVerificationKeyOrFile AsStakePoolKey verKeyOrFile

case outputFormat of
PoolIdOutputFormatHex ->
IdOutputFormatHex ->
firstExceptT ShelleyPoolCmdWriteFileError
. newExceptT
$ writeByteStringOutput mOutFile
$ serialiseToRawBytesHex (verificationKeyHash stakePoolVerKey)
PoolIdOutputFormatBech32 ->
IdOutputFormatBech32 ->
firstExceptT ShelleyPoolCmdWriteFileError
. newExceptT
$ writeTextOutput mOutFile
Expand Down
10 changes: 5 additions & 5 deletions cardano-cli/src/Cardano/CLI/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Cardano.CLI.Parser
( readerFromAttoParser
, readFractionAsRational
, readKeyOutputFormat
, readPoolIdOutputFormat
, readIdOutputFormat
, readRational
, readRationalUnitInterval
, readStringOfMaxLength
Expand All @@ -25,12 +25,12 @@ import qualified Data.Text as Text
import qualified Data.Text.Encoding as Text
import qualified Options.Applicative as Opt

readPoolIdOutputFormat :: Opt.ReadM PoolIdOutputFormat
readPoolIdOutputFormat = do
readIdOutputFormat :: Opt.ReadM IdOutputFormat
readIdOutputFormat = do
s <- Opt.str @String
case s of
"hex" -> pure PoolIdOutputFormatHex
"bech32" -> pure PoolIdOutputFormatBech32
"hex" -> pure IdOutputFormatHex
"bech32" -> pure IdOutputFormatBech32
_ ->
fail $ mconcat
[ "Invalid output format: " <> show s
Expand Down
12 changes: 7 additions & 5 deletions cardano-cli/src/Cardano/CLI/Types/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module Cardano.CLI.Types.Common
, OpCertStartingKesPeriod (..)
, Params (..)
, ParserFileDirection (..)
, PoolIdOutputFormat (..)
, IdOutputFormat (..)
, PrivKeyFile(..)
, ProtocolParamsFile(..)
, ReferenceScriptAnyEra (..)
Expand Down Expand Up @@ -226,10 +226,12 @@ instance FromJSON GenesisFile where
parseJSON invalid = error $ "Parsing of GenesisFile failed due to type mismatch. "
<> "Encountered: " <> show invalid

-- | The desired output format.
data PoolIdOutputFormat
= PoolIdOutputFormatHex
| PoolIdOutputFormatBech32
-- | Some entities such as stake pools and dreps have a notion of an ID and that id can be
-- encoded as either a bech32 or hex string. This type is used to specify which encoding
-- to use.
data IdOutputFormat
= IdOutputFormatHex
| IdOutputFormatBech32
deriving (Eq, Show)

data KeyOutputFormat
Expand Down
40 changes: 36 additions & 4 deletions cardano-cli/test/cardano-cli-golden/Test/Golden/Governance/DRep.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ module Test.Golden.Governance.DRep where

import Control.Monad (void)

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

import Hedgehog (Property)
import Hedgehog
import qualified Hedgehog.Extras.Test.Base as H
import qualified Hedgehog.Extras.Test.File as H
import qualified Hedgehog.Extras.Test.Golden as H

hprop_golden_governanceDRepKeyGen :: Property
hprop_golden_governanceDRepKeyGen =
propertyOnce . H.moduleWorkspace "tmp" $ \tempDir -> do
verificationKeyFile <- noteTempFile tempDir "key-gen.vkey"
signingKeyFile <- noteTempFile tempDir "key-gen.skey"
verificationKeyFile <- H.noteTempFile tempDir "key-gen.vkey"
signingKeyFile <- H.noteTempFile tempDir "key-gen.skey"

void $ execCardanoCLI
[ "conway", "governance", "drep", "key-gen"
Expand All @@ -28,3 +29,34 @@ hprop_golden_governanceDRepKeyGen =
H.assertFileOccurences 1 "Delegate Representative Verification Key" verificationKeyFile
H.assertFileOccurences 1 "Delegate Representative Signing Key" signingKeyFile

hprop_golden_governance_drep_id_bech32 :: Property
hprop_golden_governance_drep_id_bech32 =
propertyOnce . H.moduleWorkspace "tmp" $ \tempDir -> do
vkeyFile <- noteInputFile "test/cardano-cli-golden/files/input/drep.vkey"
idFile <- H.noteTempFile tempDir "drep.id.bech32"
idGold <- H.note "test/cardano-cli-golden/files/golden/governance/drep/drep.id.bech32"

void $ execCardanoCLI
[ "conway", "governance", "drep", "id"
, "--drep-verification-key-file", vkeyFile
, "--output-format", "bech32"
, "--out-file", idFile
]

H.diffFileVsGoldenFile idFile idGold

hprop_golden_governance_drep_id_hex :: Property
hprop_golden_governance_drep_id_hex =
propertyOnce . H.moduleWorkspace "tmp" $ \tempDir -> do
vkeyFile <- noteInputFile "test/cardano-cli-golden/files/input/drep.vkey"
idFile <- H.noteTempFile tempDir "drep.id.hex"
idGold <- H.note "test/cardano-cli-golden/files/golden/governance/drep/drep.id.hex"

void $ execCardanoCLI
[ "conway", "governance", "drep", "id"
, "--drep-verification-key-file", vkeyFile
, "--output-format", "hex"
, "--out-file", idFile
]

H.diffFileVsGoldenFile idFile idGold
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
drep1avya24t2308yyyr5uw2dqtreemvkwst90dx00j5fj5556meyztm
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
eb09d5556a8bce421074e394d02c79ced96741657b4cf7ca8995294d
Loading

0 comments on commit 8446431

Please sign in to comment.