diff --git a/cardano-cli/cardano-cli.cabal b/cardano-cli/cardano-cli.cabal index 1d379c955e..112da8819a 100644 --- a/cardano-cli/cardano-cli.cabal +++ b/cardano-cli/cardano-cli.cabal @@ -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 diff --git a/cardano-cli/src/Cardano/CLI/EraBased/Commands/Governance/Committee.hs b/cardano-cli/src/Cardano/CLI/EraBased/Commands/Governance/Committee.hs index 7c55090884..d4b23891c1 100644 --- a/cardano-cli/src/Cardano/CLI/EraBased/Commands/Governance/Committee.hs +++ b/cardano-cli/src/Cardano/CLI/EraBased/Commands/Governance/Committee.hs @@ -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) @@ -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) diff --git a/cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs b/cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs index ce8eb5761c..e808c715ff 100644 --- a/cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs +++ b/cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs @@ -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) @@ -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 diff --git a/cardano-cli/src/Cardano/CLI/EraBased/Options/Governance/Committee.hs b/cardano-cli/src/Cardano/CLI/EraBased/Options/Governance/Committee.hs index a745a328b5..f6b94895d7 100644 --- a/cardano-cli/src/Cardano/CLI/EraBased/Options/Governance/Committee.hs +++ b/cardano-cli/src/Cardano/CLI/EraBased/Options/Governance/Committee.hs @@ -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 diff --git a/cardano-cli/src/Cardano/CLI/EraBased/Run/Governance/Committee.hs b/cardano-cli/src/Cardano/CLI/EraBased/Run/Governance/Committee.hs index 1c6af555d5..32c0f287fa 100644 --- a/cardano-cli/src/Cardano/CLI/EraBased/Run/Governance/Committee.hs +++ b/cardano-cli/src/Cardano/CLI/EraBased/Run/Governance/Committee.hs @@ -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) @@ -21,7 +22,8 @@ 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 ()) @@ -29,6 +31,8 @@ data GovernanceCommitteeError instance Error GovernanceCommitteeError where displayError = \case + GovernanceCommitteeCmdKeyDecodeError e -> + "Cannot decode key: " <> displayError e GovernanceCommitteeCmdKeyReadError e -> "Cannot read key: " <> displayError e GovernanceCommitteeCmdWriteFileError e -> @@ -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) diff --git a/cardano-cli/src/Cardano/CLI/Types/Key/VerificationKey.hs b/cardano-cli/src/Cardano/CLI/Types/Key/VerificationKey.hs new file mode 100644 index 0000000000..7f7e4915af --- /dev/null +++ b/cardano-cli/src/Cardano/CLI/Types/Key/VerificationKey.hs @@ -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) diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/help.cli b/cardano-cli/test/cardano-cli-golden/files/golden/help.cli index 98b3540365..c731a48e3f 100644 --- a/cardano-cli/test/cardano-cli-golden/files/golden/help.cli +++ b/cardano-cli/test/cardano-cli-golden/files/golden/help.cli @@ -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 @@ -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 diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/help/conway_governance_committee_key-hash.cli b/cardano-cli/test/cardano-cli-golden/files/golden/help/conway_governance_committee_key-hash.cli index a2b20e3a9a..62ca9af520 100644 --- a/cardano-cli/test/cardano-cli-golden/files/golden/help/conway_governance_committee_key-hash.cli +++ b/cardano-cli/test/cardano-cli-golden/files/golden/help/conway_governance_committee_key-hash.cli @@ -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 diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/help/experimental_governance_committee_key-hash.cli b/cardano-cli/test/cardano-cli-golden/files/golden/help/experimental_governance_committee_key-hash.cli index 1c2dc8658d..74bbb284b7 100644 --- a/cardano-cli/test/cardano-cli-golden/files/golden/help/experimental_governance_committee_key-hash.cli +++ b/cardano-cli/test/cardano-cli-golden/files/golden/help/experimental_governance_committee_key-hash.cli @@ -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