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 authored and newhoggy committed Mar 17, 2023
1 parent 11db78f commit ea1a785
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 64 deletions.
3 changes: 2 additions & 1 deletion cardano-api/src/Cardano/Api/Address.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 @@ -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
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 @@ -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
Expand Down
110 changes: 47 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 @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

0 comments on commit ea1a785

Please sign in to comment.