Skip to content

Commit

Permalink
cardano-cli: Add option --stake-address to some subcommands:
Browse files Browse the repository at this point in the history
- address build
- stake-address build
- stake-address registration-certificate
- stake-address deregistration-certificate
- stake-address delegation-certificate

Fix #2723
  • Loading branch information
cblp committed Mar 17, 2022
1 parent aa49e91 commit ffe0d7d
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 63 deletions.
1 change: 1 addition & 0 deletions cardano-cli/src/Cardano/CLI/Shelley/Key.hs
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,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
Expand Down
1 change: 1 addition & 0 deletions cardano-cli/src/Cardano/CLI/Shelley/Parsers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ pStakeVerifier =
<|> StakeVerifierScriptFile <$>
pScriptFor "stake-script-file" Nothing
"Filepath of the staking script."
<|> StakeVerifierAddress <$> pStakeAddress

pPaymentVerificationKeyTextOrFile :: Parser VerificationKeyTextOrFile
pPaymentVerificationKeyTextOrFile =
Expand Down
3 changes: 3 additions & 0 deletions cardano-cli/src/Cardano/CLI/Shelley/Run/Address.hs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ makeStakeAddressRef mbStakeVerifier = do
let stakeCred = StakeCredentialByScript (hashScript script)
return (StakeAddressByValue stakeCred)

StakeVerifierAddress stakeAddr ->
pure $ StakeAddressByValue $ stakeAddressCredential stakeAddr

buildShelleyAddress
:: VerificationKey PaymentKey
-> Maybe StakeVerifier
Expand Down
123 changes: 60 additions & 63 deletions cardano-cli/src/Cardano/CLI/Shelley/Run/StakeAddress.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{-# LANGUAGE LambdaCase #-}

module Cardano.CLI.Shelley.Run.StakeAddress
( ShelleyStakeAddressCmdError(ShelleyStakeAddressCmdReadKeyFileError)
, renderShelleyStakeAddressCmdError
Expand Down Expand Up @@ -88,51 +90,24 @@ 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 <- loadStakeAddressFromVerifier 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 <- loadStakeCredentialFromVerifier stakeVerifier
writeRegistrationCert stakeCred
where

writeRegistrationCert
:: StakeCredential
-> ExceptT ShelleyStakeAddressCmdError IO ()
Expand All @@ -159,19 +134,9 @@ runStakeCredentialDelegationCert stakeVerifier poolVKeyOrHashOrFile (OutputFile
firstExceptT
ShelleyStakeAddressCmdReadKeyFileError
(newExceptT $ readVerificationKeyOrHashOrFile AsStakePoolKey poolVKeyOrHashOrFile)
stakeCred <- loadStakeCredentialFromVerifier 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
Expand All @@ -191,19 +156,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 <- loadStakeCredentialFromVerifier stakeVerifier
writeDeregistrationCert stakeCred

where
writeDeregistrationCert
:: StakeCredential
Expand All @@ -216,3 +172,44 @@ runStakeCredentialDeRegistrationCert stakeVerifier (OutputFile oFp) =

deregCertDesc :: TextEnvelopeDescr
deregCertDesc = "Stake Address Deregistration Certificate"


loadStakeCredentialFromVerifier
:: StakeVerifier -> ExceptT ShelleyStakeAddressCmdError IO StakeCredential
loadStakeCredentialFromVerifier stakeVerifier =
either stakeAddressCredential identity <$> loadStakeVerifier stakeVerifier

loadStakeAddressFromVerifier
:: NetworkId
-> StakeVerifier
-> ExceptT ShelleyStakeAddressCmdError IO StakeAddress
loadStakeAddressFromVerifier network stakeVerifier =
either identity (makeStakeAddress network) <$> loadStakeVerifier stakeVerifier

-- | Load 'StakeAddress' or 'StakeCredential' from 'StakeVerifier',
-- which one is closer.
--
-- For internal use in 'loadStakeCredentialFromVerifier' and
-- 'loadStakeAddressFromVerifier' only
loadStakeVerifier
:: StakeVerifier
-> ExceptT
ShelleyStakeAddressCmdError
IO
(Either StakeAddress StakeCredential)
loadStakeVerifier = \case

StakeVerifierScriptFile (ScriptFile sFile) -> do
ScriptInAnyLang _ script <-
firstExceptT ShelleyStakeAddressCmdReadScriptFileError $
readFileScriptInAnyLang sFile
pure $ Right $ StakeCredentialByScript $ hashScript script

StakeVerifierKey stakeVerKeyOrFile -> do
stakeVerKey <-
firstExceptT ShelleyStakeAddressCmdReadKeyFileError
. newExceptT
$ readVerificationKeyOrFile AsStakeKey stakeVerKeyOrFile
pure $ Right $ StakeCredentialByKey $ verificationKeyHash stakeVerKey

StakeVerifierAddress stakeAddr -> pure $ Left stakeAddr

0 comments on commit ffe0d7d

Please sign in to comment.