From 751b61017ace8f5464004b5a5bb612dbcc2a870b Mon Sep 17 00:00:00 2001 From: Luke Nadur <19835357+intricate@users.noreply.github.com> Date: Tue, 14 Jul 2020 14:21:39 -0400 Subject: [PATCH] Accept either a pool vkey hash or file in StakeKeyDelegationCert command --- .../src/Cardano/CLI/Shelley/Commands.hs | 11 ++++++-- .../src/Cardano/CLI/Shelley/Parsers.hs | 19 +++++++++++++- .../Cardano/CLI/Shelley/Run/StakeAddress.hs | 26 ++++++++++++------- 3 files changed, 43 insertions(+), 13 deletions(-) diff --git a/cardano-cli/src/Cardano/CLI/Shelley/Commands.hs b/cardano-cli/src/Cardano/CLI/Shelley/Commands.hs index 184b64abc6a..0c1c0eee9f0 100644 --- a/cardano-cli/src/Cardano/CLI/Shelley/Commands.hs +++ b/cardano-cli/src/Cardano/CLI/Shelley/Commands.hs @@ -39,6 +39,7 @@ module Cardano.CLI.Shelley.Commands , PrivKeyFile (..) , BlockId (..) , QueryFilter (..) + , StakePoolVerificationKeyHashOrFile (..) ) where import Prelude @@ -46,7 +47,7 @@ import Data.Set (Set) import Data.Text (Text) import Cardano.Api.Protocol (Protocol) -import Cardano.Api.Typed hiding (PoolId, Hash) +import Cardano.Api.Typed hiding (PoolId) import Ouroboros.Consensus.BlockchainTime (SystemStart (..)) @@ -93,7 +94,7 @@ data StakeAddressCmd | StakeKeyDelegate PrivKeyFile PoolId Lovelace NodeAddress | StakeKeyDeRegister PrivKeyFile NodeAddress | StakeKeyRegistrationCert VerificationKeyFile OutputFile - | StakeKeyDelegationCert VerificationKeyFile VerificationKeyFile OutputFile + | StakeKeyDelegationCert VerificationKeyFile StakePoolVerificationKeyHashOrFile OutputFile | StakeKeyDeRegistrationCert VerificationKeyFile OutputFile | StakeKeyITNConversion ITNKeyFile (Maybe OutputFile) deriving (Eq, Show) @@ -318,3 +319,9 @@ data QueryFilter = FilterByAddress !(Set (Address Shelley)) | NoFilter deriving (Eq, Show) + +-- | Either a stake pool verification key hash or verification key file. +data StakePoolVerificationKeyHashOrFile + = StakePoolVerificationKeyHash !(Hash StakePoolKey) + | StakePoolVerificationKeyFile !VerificationKeyFile + deriving (Eq, Show) diff --git a/cardano-cli/src/Cardano/CLI/Shelley/Parsers.hs b/cardano-cli/src/Cardano/CLI/Shelley/Parsers.hs index 7e39a29c562..d256bc18761 100644 --- a/cardano-cli/src/Cardano/CLI/Shelley/Parsers.hs +++ b/cardano-cli/src/Cardano/CLI/Shelley/Parsers.hs @@ -245,7 +245,7 @@ pStakeAddress = pStakeAddressDelegationCert :: Parser StakeAddressCmd pStakeAddressDelegationCert = StakeKeyDelegationCert <$> pStakeVerificationKeyFile - <*> pPoolStakeVerificationKeyFile + <*> pStakePoolVerificationKeyHashOrFile <*> pOutputFile pConvertITNKey :: Parser StakeAddressCmd @@ -1326,6 +1326,23 @@ pPoolStakeVerificationKeyFile = ) ) +pStakePoolVerificationKeyHash :: Parser (Hash StakePoolKey) +pStakePoolVerificationKeyHash = + Opt.option + (Opt.maybeReader spvkHash) + ( Opt.long "cold-verification-key-hash" + <> Opt.metavar "HASH" + <> Opt.help "Stake pool verification key hash (hex-encoded)." + ) + where + spvkHash :: String -> Maybe (Hash StakePoolKey) + spvkHash = deserialiseFromRawBytesHex (AsHash AsStakePoolKey) . BSC.pack + +pStakePoolVerificationKeyHashOrFile :: Parser StakePoolVerificationKeyHashOrFile +pStakePoolVerificationKeyHashOrFile = + StakePoolVerificationKeyFile <$> pPoolStakeVerificationKeyFile + <|> StakePoolVerificationKeyHash <$> pStakePoolVerificationKeyHash + pVRFVerificationKeyFile :: Parser VerificationKeyFile pVRFVerificationKeyFile = VerificationKeyFile <$> diff --git a/cardano-cli/src/Cardano/CLI/Shelley/Run/StakeAddress.hs b/cardano-cli/src/Cardano/CLI/Shelley/Run/StakeAddress.hs index 56ffd5b62ea..8f95542fec4 100644 --- a/cardano-cli/src/Cardano/CLI/Shelley/Run/StakeAddress.hs +++ b/cardano-cli/src/Cardano/CLI/Shelley/Run/StakeAddress.hs @@ -11,7 +11,8 @@ import qualified Data.Text as Text import qualified Data.Text.IO as Text import Control.Monad.Trans.Except (ExceptT) -import Control.Monad.Trans.Except.Extra (firstExceptT, hoistEither, newExceptT) +import Control.Monad.Trans.Except.Extra (bimapExceptT, firstExceptT, hoistEither, + newExceptT) import Cardano.Api.TextView (TextViewDescription (..)) import Cardano.Api.Typed @@ -47,8 +48,8 @@ runStakeAddressCmd (StakeAddressKeyHash vk mOutputFp) = runStakeAddressKeyHash v runStakeAddressCmd (StakeAddressBuild vk nw mOutputFp) = runStakeAddressBuild vk nw mOutputFp runStakeAddressCmd (StakeKeyRegistrationCert stkKeyVerKeyFp outputFp) = runStakeKeyRegistrationCert stkKeyVerKeyFp outputFp -runStakeAddressCmd (StakeKeyDelegationCert stkKeyVerKeyFp stkPoolVerKeyFp outputFp) = - runStakeKeyDelegationCert stkKeyVerKeyFp stkPoolVerKeyFp outputFp +runStakeAddressCmd (StakeKeyDelegationCert stkKeyVerKeyFp stkPoolVerKeyHashOrFp outputFp) = + runStakeKeyDelegationCert stkKeyVerKeyFp stkPoolVerKeyHashOrFp outputFp runStakeAddressCmd (StakeKeyDeRegistrationCert stkKeyVerKeyFp outputFp) = runStakeKeyDeRegistrationCert stkKeyVerKeyFp outputFp runStakeAddressCmd (StakeKeyITNConversion itnKeyFile mOutFile) = runSingleITNKeyConversion itnKeyFile mOutFile @@ -120,23 +121,28 @@ runStakeKeyRegistrationCert (VerificationKeyFile vkFp) (OutputFile oFp) = do runStakeKeyDelegationCert :: VerificationKeyFile -- ^ Delegator stake verification key file. - -> VerificationKeyFile - -- ^ Delegatee stake pool verification key file. + -> StakePoolVerificationKeyHashOrFile + -- ^ Delegatee stake pool verification key hash or file. -> OutputFile -> ExceptT ShelleyStakeAddressCmdError IO () -runStakeKeyDelegationCert (VerificationKeyFile stkKey) (VerificationKeyFile poolVKey) (OutputFile outFp) = do +runStakeKeyDelegationCert (VerificationKeyFile stkKey) poolVKeyHashOrFile (OutputFile outFp) = do stakeVkey <- firstExceptT ShelleyStakeAddressReadFileError . newExceptT $ readFileTextEnvelope (AsVerificationKey AsStakeKey) stkKey - poolStakeVkey <- firstExceptT ShelleyStakeAddressReadFileError - . newExceptT - $ readFileTextEnvelope (AsVerificationKey AsStakePoolKey) poolVKey + poolStakeVKeyHash <- + case poolVKeyHashOrFile of + StakePoolVerificationKeyHash hash -> pure hash + StakePoolVerificationKeyFile (VerificationKeyFile fp) -> + bimapExceptT + ShelleyStakeAddressReadFileError + verificationKeyHash + (newExceptT $ readFileTextEnvelope (AsVerificationKey AsStakePoolKey) fp) let stakeCred = StakeCredentialByKey (verificationKeyHash stakeVkey) delegCert = makeStakeAddressDelegationCertificate stakeCred - (verificationKeyHash poolStakeVkey) + poolStakeVKeyHash firstExceptT ShelleyStakeAddressWriteFileError . newExceptT $ writeFileTextEnvelope outFp (Just delegCertDesc) delegCert