From dff6c33800afd1206984811cb1b662ff29f45a96 Mon Sep 17 00:00:00 2001 From: Yuriy Syrovetskiy Date: Tue, 30 Nov 2021 18:35:17 +0300 Subject: [PATCH] cardano-cli: Add option `--stake-address` to some subcommands: - address build - stake-address build - stake-address registration-certificate - stake-address deregistration-certificate - stake-address delegation-certificate Fix #2723 --- cardano-api/src/Cardano/Api/Address.hs | 3 +- cardano-cli/src/Cardano/CLI/Shelley/Key.hs | 1 + .../src/Cardano/CLI/Shelley/Parsers.hs | 1 + .../src/Cardano/CLI/Shelley/Run/Address.hs | 3 + .../Cardano/CLI/Shelley/Run/StakeAddress.hs | 110 ++++++++---------- 5 files changed, 54 insertions(+), 64 deletions(-) diff --git a/cardano-api/src/Cardano/Api/Address.hs b/cardano-api/src/Cardano/Api/Address.hs index 89f8831ee61..8c0c1e00a73 100644 --- a/cardano-api/src/Cardano/Api/Address.hs +++ b/cardano-api/src/Cardano/Api/Address.hs @@ -719,6 +719,7 @@ fromShelleyStakeReference (Shelley.StakeRefPtr ptr) = fromShelleyStakeReference Shelley.StakeRefNull = NoStakeAddress --- | Get credential from a stake address. This drops the network information. +-- | Get a stake credential from a stake address. +-- This drops the network information. stakeAddressCredential :: StakeAddress -> StakeCredential stakeAddressCredential (StakeAddress _ scred) = fromShelleyStakeCredential scred diff --git a/cardano-cli/src/Cardano/CLI/Shelley/Key.hs b/cardano-cli/src/Cardano/CLI/Shelley/Key.hs index 762a042a0a7..f5fdf0c8936 100644 --- a/cardano-cli/src/Cardano/CLI/Shelley/Key.hs +++ b/cardano-cli/src/Cardano/CLI/Shelley/Key.hs @@ -102,6 +102,7 @@ data PaymentVerifier data StakeVerifier = StakeVerifierKey (VerificationKeyOrFile StakeKey) | StakeVerifierScriptFile ScriptFile + | StakeVerifierAddress StakeAddress deriving (Eq, Show) -- | Either an unvalidated text representation of a verification key or a path diff --git a/cardano-cli/src/Cardano/CLI/Shelley/Parsers.hs b/cardano-cli/src/Cardano/CLI/Shelley/Parsers.hs index 7b24fa49f22..6ae43523873 100644 --- a/cardano-cli/src/Cardano/CLI/Shelley/Parsers.hs +++ b/cardano-cli/src/Cardano/CLI/Shelley/Parsers.hs @@ -178,6 +178,7 @@ pStakeVerifier = <|> StakeVerifierScriptFile <$> pScriptFor "stake-script-file" Nothing "Filepath of the staking script." + <|> StakeVerifierAddress <$> pStakeAddress pPaymentVerificationKeyTextOrFile :: Parser VerificationKeyTextOrFile pPaymentVerificationKeyTextOrFile = diff --git a/cardano-cli/src/Cardano/CLI/Shelley/Run/Address.hs b/cardano-cli/src/Cardano/CLI/Shelley/Run/Address.hs index 91c921475ba..8a61c5a820a 100644 --- a/cardano-cli/src/Cardano/CLI/Shelley/Run/Address.hs +++ b/cardano-cli/src/Cardano/CLI/Shelley/Run/Address.hs @@ -173,6 +173,9 @@ makeStakeAddressRef stakeVerifier = case stakeVerifier of let stakeCred = StakeCredentialByScript (hashScript script) return (StakeAddressByValue stakeCred) + StakeVerifierAddress stakeAddr -> + pure $ StakeAddressByValue $ stakeAddressCredential stakeAddr + buildShelleyAddress :: VerificationKey PaymentKey -> Maybe StakeVerifier diff --git a/cardano-cli/src/Cardano/CLI/Shelley/Run/StakeAddress.hs b/cardano-cli/src/Cardano/CLI/Shelley/Run/StakeAddress.hs index 3972f196913..fa435d6a923 100644 --- a/cardano-cli/src/Cardano/CLI/Shelley/Run/StakeAddress.hs +++ b/cardano-cli/src/Cardano/CLI/Shelley/Run/StakeAddress.hs @@ -1,3 +1,5 @@ +{-# LANGUAGE LambdaCase #-} + module Cardano.CLI.Shelley.Run.StakeAddress ( ShelleyStakeAddressCmdError(ShelleyStakeAddressCmdReadKeyFileError) , renderShelleyStakeAddressCmdError @@ -89,50 +91,23 @@ runStakeAddressBuild -> NetworkId -> Maybe OutputFile -> ExceptT ShelleyStakeAddressCmdError IO () -runStakeAddressBuild stakeVerifier network mOutputFp = - case stakeVerifier of - StakeVerifierScriptFile (ScriptFile sFile) -> do - ScriptInAnyLang _ script <- firstExceptT ShelleyStakeAddressCmdReadScriptFileError - $ readFileScriptInAnyLang sFile - let stakeCred = StakeCredentialByScript $ hashScript script - stakeAddr = makeStakeAddress network stakeCred - stakeAddrText = serialiseAddress stakeAddr - - case mOutputFp of - Just (OutputFile fpath) -> liftIO $ Text.writeFile fpath stakeAddrText - Nothing -> liftIO $ Text.putStrLn stakeAddrText - - StakeVerifierKey stakeVerKeyOrFile -> do - stakeVerKey <- firstExceptT ShelleyStakeAddressCmdReadKeyFileError - . newExceptT - $ readVerificationKeyOrFile AsStakeKey stakeVerKeyOrFile - - let stakeCred = StakeCredentialByKey (verificationKeyHash stakeVerKey) - stakeAddr = makeStakeAddress network stakeCred - stakeAddrText = serialiseAddress stakeAddr - - case mOutputFp of - Just (OutputFile fpath) -> liftIO $ Text.writeFile fpath stakeAddrText - Nothing -> liftIO $ Text.putStrLn stakeAddrText +runStakeAddressBuild stakeVerifier network mOutputFp = do + stakeAddr <- getStakeAddressFromVerifier network stakeVerifier + let stakeAddrText = serialiseAddress stakeAddr + liftIO $ + case mOutputFp of + Just (OutputFile fpath) -> Text.writeFile fpath stakeAddrText + Nothing -> Text.putStrLn stakeAddrText runStakeCredentialRegistrationCert :: StakeVerifier -> OutputFile -> ExceptT ShelleyStakeAddressCmdError IO () -runStakeCredentialRegistrationCert stakeVerifier (OutputFile oFp) = - case stakeVerifier of - StakeVerifierScriptFile (ScriptFile sFile) -> do - ScriptInAnyLang _ script <- firstExceptT ShelleyStakeAddressCmdReadScriptFileError - $ readFileScriptInAnyLang sFile - let stakeCred = StakeCredentialByScript $ hashScript script - writeRegistrationCert stakeCred - StakeVerifierKey stakeVerKeyOrFile -> do - stakeVerKey <- firstExceptT ShelleyStakeAddressCmdReadKeyFileError - . newExceptT - $ readVerificationKeyOrFile AsStakeKey stakeVerKeyOrFile - let stakeCred = StakeCredentialByKey (verificationKeyHash stakeVerKey) - writeRegistrationCert stakeCred +runStakeCredentialRegistrationCert stakeVerifier (OutputFile oFp) = do + stakeCred <- getStakeCredentialFromVerifier stakeVerifier + writeRegistrationCert stakeCred + where writeRegistrationCert :: StakeCredential @@ -160,19 +135,9 @@ runStakeCredentialDelegationCert stakeVerifier poolVKeyOrHashOrFile (OutputFile firstExceptT ShelleyStakeAddressCmdReadKeyFileError (newExceptT $ readVerificationKeyOrHashOrFile AsStakePoolKey poolVKeyOrHashOrFile) + stakeCred <- getStakeCredentialFromVerifier stakeVerifier + writeDelegationCert stakeCred poolStakeVKeyHash - case stakeVerifier of - StakeVerifierScriptFile (ScriptFile sFile) -> do - ScriptInAnyLang _ script <- firstExceptT ShelleyStakeAddressCmdReadScriptFileError - $ readFileScriptInAnyLang sFile - let stakeCred = StakeCredentialByScript $ hashScript script - writeDelegationCert stakeCred poolStakeVKeyHash - StakeVerifierKey stakeVerKeyOrFile -> do - stakeVkey <- firstExceptT ShelleyStakeAddressCmdReadKeyFileError - . newExceptT - $ readVerificationKeyOrFile AsStakeKey stakeVerKeyOrFile - let stakeCred = StakeCredentialByKey (verificationKeyHash stakeVkey) - writeDelegationCert stakeCred poolStakeVKeyHash where writeDelegationCert :: StakeCredential @@ -192,19 +157,10 @@ runStakeCredentialDeRegistrationCert :: StakeVerifier -> OutputFile -> ExceptT ShelleyStakeAddressCmdError IO () -runStakeCredentialDeRegistrationCert stakeVerifier (OutputFile oFp) = - case stakeVerifier of - StakeVerifierScriptFile (ScriptFile sFile) -> do - ScriptInAnyLang _ script <- firstExceptT ShelleyStakeAddressCmdReadScriptFileError - $ readFileScriptInAnyLang sFile - let stakeCred = StakeCredentialByScript $ hashScript script - writeDeregistrationCert stakeCred - StakeVerifierKey stakeVerKeyOrFile -> do - stakeVkey <- firstExceptT ShelleyStakeAddressCmdReadKeyFileError - . newExceptT - $ readVerificationKeyOrFile AsStakeKey stakeVerKeyOrFile - let stakeCred = StakeCredentialByKey (verificationKeyHash stakeVkey) - writeDeregistrationCert stakeCred +runStakeCredentialDeRegistrationCert stakeVerifier (OutputFile oFp) = do + stakeCred <- getStakeCredentialFromVerifier stakeVerifier + writeDeregistrationCert stakeCred + where writeDeregistrationCert :: StakeCredential @@ -217,3 +173,31 @@ runStakeCredentialDeRegistrationCert stakeVerifier (OutputFile oFp) = deregCertDesc :: TextEnvelopeDescr deregCertDesc = "Stake Address Deregistration Certificate" + + +getStakeCredentialFromVerifier + :: StakeVerifier -> ExceptT ShelleyStakeAddressCmdError IO StakeCredential +getStakeCredentialFromVerifier = \case + StakeVerifierScriptFile (ScriptFile sFile) -> do + ScriptInAnyLang _ script <- + firstExceptT ShelleyStakeAddressCmdReadScriptFileError $ + readFileScriptInAnyLang sFile + pure $ StakeCredentialByScript $ hashScript script + + StakeVerifierKey stakeVerKeyOrFile -> do + stakeVerKey <- + firstExceptT ShelleyStakeAddressCmdReadKeyFileError + . newExceptT + $ readVerificationKeyOrFile AsStakeKey stakeVerKeyOrFile + pure $ StakeCredentialByKey $ verificationKeyHash stakeVerKey + + StakeVerifierAddress stakeAddr -> pure $ stakeAddressCredential stakeAddr + +getStakeAddressFromVerifier + :: NetworkId + -> StakeVerifier + -> ExceptT ShelleyStakeAddressCmdError IO StakeAddress +getStakeAddressFromVerifier networkId = \case + StakeVerifierAddress stakeAddr -> pure stakeAddr + stakeVerifier -> + makeStakeAddress networkId <$> getStakeCredentialFromVerifier stakeVerifier