Skip to content

Commit

Permalink
Add --verification-key option to committee key-hash command
Browse files Browse the repository at this point in the history
  • Loading branch information
newhoggy committed Aug 18, 2023
1 parent f8ec332 commit ac35314
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 24 deletions.
1 change: 1 addition & 0 deletions cardano-cli/cardano-cli.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ library
Cardano.CLI.Types.Common
Cardano.CLI.Types.Governance
Cardano.CLI.Types.Key
Cardano.CLI.Types.Key.VerificationKey
Cardano.CLI.Types.Output

other-modules: Paths_cardano_cli
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module Cardano.CLI.EraBased.Commands.Governance.Committee
import Cardano.Api

import Cardano.CLI.Types.Key
import Cardano.CLI.Types.Key.VerificationKey

import Data.Text (Text)

Expand All @@ -23,7 +24,7 @@ data GovernanceCommitteeCmds era
(File (SigningKey ()) Out)
| GovernanceCommitteeKeyHash -- TODO to be moved under the top-level command group "key"
(ConwayEraOnwards era)
(File (VerificationKey ()) In)
AnyVerificationKeySource
| GovernanceCommitteeCreateHotKeyAuthorizationCertificate -- TODO to be moved under the top-level command group "key"
(ConwayEraOnwards era)
(VerificationKeyOrHashOrFile CommitteeColdKey)
Expand Down
25 changes: 25 additions & 0 deletions cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import Cardano.CLI.Parser
import Cardano.CLI.Types.Common
import Cardano.CLI.Types.Governance
import Cardano.CLI.Types.Key
import Cardano.CLI.Types.Key.VerificationKey
import qualified Cardano.Ledger.Shelley.TxBody as Shelley

import Control.Monad (mfilter)
Expand Down Expand Up @@ -696,6 +697,30 @@ pVerificationKeyFileIn =
, Opt.completer (Opt.bashCompleter "file")
]

pAnyVerificationKeyFileIn :: String -> Parser (VerificationKeyFile In)
pAnyVerificationKeyFileIn helpText =
fmap File $ Opt.strOption $ mconcat
[ Opt.long "verification-key-file"
, Opt.metavar "FILE"
, Opt.help $ "Input filepath of the " <> helpText <> "."
, Opt.completer (Opt.bashCompleter "file")
]

pAnyVerificationKeyText :: String -> Parser AnyVerificationKeyText
pAnyVerificationKeyText helpText =
fmap (AnyVerificationKeyText . Text.pack) $ Opt.strOption $ mconcat
[ Opt.long "verification-key"
, Opt.metavar "STRING"
, Opt.help $ helpText <> " (Bech32-encoded)"
]

pAnyVerificationKeySource :: String -> Parser AnyVerificationKeySource
pAnyVerificationKeySource helpText =
asum
[ AnyVerificationKeySourceOfText <$> pAnyVerificationKeyText helpText
, AnyVerificationKeySourceOfFile <$> pAnyVerificationKeyFileIn helpText
]

pCommitteeHotKey :: Parser (VerificationKey CommitteeHotKey)
pCommitteeHotKey =
Opt.option (Opt.eitherReader deserialiseFromHex) $ mconcat
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,14 @@ pGovernanceCommitteeKeyHash era = do
w <- maybeFeatureInEra era
pure
$ subParser "key-hash"
$ Opt.info (pCmd w)
$ Opt.info
( GovernanceCommitteeKeyHash w
<$> pAnyVerificationKeySource "Constitutional Committee Member key (hot or cold)"
)
$ Opt.progDesc
$ mconcat
[ "Print the identifier (hash) of a public key"
]
where
pCmd :: ()
=> ConwayEraOnwards era
-> Parser (GovernanceCommitteeCmds era)
pCmd w =
GovernanceCommitteeKeyHash w
<$> pVerificationKeyFileIn

pGovernanceCommitteeCreateHotKeyAuthorizationCertificate :: ()
=> CardanoEra era
Expand Down
31 changes: 22 additions & 9 deletions cardano-cli/src/Cardano/CLI/EraBased/Run/Governance/Committee.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import Cardano.Api.Shelley

import Cardano.CLI.EraBased.Commands.Governance.Committee
import Cardano.CLI.Types.Key
import Cardano.CLI.Types.Key.VerificationKey

import Control.Monad.Except (ExceptT)
import Control.Monad.IO.Class (liftIO)
Expand All @@ -21,14 +22,17 @@ import qualified Data.ByteString.Char8 as BS
import Data.Function

data GovernanceCommitteeError
= GovernanceCommitteeCmdKeyReadError (FileError InputDecodeError)
= GovernanceCommitteeCmdKeyDecodeError InputDecodeError
| GovernanceCommitteeCmdKeyReadError (FileError InputDecodeError)
| GovernanceCommitteeCmdTextEnvReadFileError (FileError TextEnvelopeError)
| GovernanceCommitteeCmdTextEnvWriteError (FileError ())
| GovernanceCommitteeCmdWriteFileError (FileError ())
deriving Show

instance Error GovernanceCommitteeError where
displayError = \case
GovernanceCommitteeCmdKeyDecodeError e ->
"Cannot decode key: " <> displayError e
GovernanceCommitteeCmdKeyReadError e ->
"Cannot read key: " <> displayError e
GovernanceCommitteeCmdWriteFileError e ->
Expand Down Expand Up @@ -109,16 +113,25 @@ data SomeCommitteeKey f

runGovernanceCommitteeKeyHash :: ()
=> ConwayEraOnwards era
-> File (VerificationKey ()) In
-> AnyVerificationKeySource
-> ExceptT GovernanceCommitteeError IO ()
runGovernanceCommitteeKeyHash _w vkeyPath = do
runGovernanceCommitteeKeyHash _w vkeySource = do
vkey <-
readFileTextEnvelopeAnyOf
[ FromSomeType (AsVerificationKey AsCommitteeHotKey ) ACommitteeHotKey
, FromSomeType (AsVerificationKey AsCommitteeColdKey) ACommitteeColdKey
]
vkeyPath
& firstExceptT GovernanceCommitteeCmdTextEnvReadFileError . newExceptT
case vkeySource of
AnyVerificationKeySourceOfText vkText -> do
let asTypes =
[ FromSomeType (AsVerificationKey AsCommitteeHotKey ) ACommitteeHotKey
, FromSomeType (AsVerificationKey AsCommitteeColdKey) ACommitteeColdKey
]
pure (deserialiseAnyOfFromBech32 asTypes (unAnyVerificationKeyText vkText))
& onLeft (left . GovernanceCommitteeCmdKeyDecodeError . InputBech32DecodeError)
AnyVerificationKeySourceOfFile vkeyPath -> do
let asTypes =
[ FromSomeType (AsVerificationKey AsCommitteeHotKey ) ACommitteeHotKey
, FromSomeType (AsVerificationKey AsCommitteeColdKey) ACommitteeColdKey
]
readFileTextEnvelopeAnyOf asTypes vkeyPath
& firstExceptT GovernanceCommitteeCmdTextEnvReadFileError . newExceptT

liftIO $ BS.putStrLn (renderKeyHash vkey)

Expand Down
23 changes: 23 additions & 0 deletions cardano-cli/src/Cardano/CLI/Types/Key/VerificationKey.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{-# LANGUAGE DataKinds #-}

module Cardano.CLI.Types.Key.VerificationKey
( AnyVerificationKeySource(..)
, AnyVerificationKeyText(..)
) where

import Cardano.Api

import Data.Text (Text)

-- | A bech32 text encoded verification key of an unspecified key role.
newtype AnyVerificationKeyText = AnyVerificationKeyText
{ unAnyVerificationKeyText :: Text
}
deriving (Eq, Show)

-- | The source from which a verification key of an unspecified key role can be
-- derived.
data AnyVerificationKeySource
= AnyVerificationKeySourceOfText !AnyVerificationKeyText
| AnyVerificationKeySourceOfFile !(File (VerificationKey ()) In)
deriving (Eq, Show)
10 changes: 8 additions & 2 deletions cardano-cli/test/cardano-cli-golden/files/golden/help.cli
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,10 @@ Usage: cardano-cli conway governance committee key-gen-hot --verification-key-fi

Create a cold key pair for a Constitutional Committee Member

Usage: cardano-cli conway governance committee key-hash --verification-key-file FILE
Usage: cardano-cli conway governance committee key-hash
( --verification-key STRING
| --verification-key-file FILE
)

Print the identifier (hash) of a public key

Expand Down Expand Up @@ -1200,7 +1203,10 @@ Usage: cardano-cli experimental governance committee key-gen-hot --verification-

Create a cold key pair for a Constitutional Committee Member

Usage: cardano-cli experimental governance committee key-hash --verification-key-file FILE
Usage: cardano-cli experimental governance committee key-hash
( --verification-key STRING
| --verification-key-file FILE
)

Print the identifier (hash) of a public key

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
Usage: cardano-cli conway governance committee key-hash --verification-key-file FILE
Usage: cardano-cli conway governance committee key-hash
( --verification-key STRING
| --verification-key-file FILE
)

Print the identifier (hash) of a public key

Available options:
--verification-key STRING
Constitutional Committee Member key (hot or cold)
(Bech32-encoded)
--verification-key-file FILE
Input filepath of the verification key.
Input filepath of the Constitutional Committee Member
key (hot or cold).
-h,--help Show this help text
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
Usage: cardano-cli experimental governance committee key-hash --verification-key-file FILE
Usage: cardano-cli experimental governance committee key-hash
( --verification-key STRING
| --verification-key-file FILE
)

Print the identifier (hash) of a public key

Available options:
--verification-key STRING
Constitutional Committee Member key (hot or cold)
(Bech32-encoded)
--verification-key-file FILE
Input filepath of the verification key.
Input filepath of the Constitutional Committee Member
key (hot or cold).
-h,--help Show this help text

0 comments on commit ac35314

Please sign in to comment.