Skip to content

Commit

Permalink
Merge #1460
Browse files Browse the repository at this point in the history
1460: Accept either a pool verification key hash or file in StakeKeyDelegationCert command r=intricate a=intricate

Closes #1377 

Co-authored-by: Luke Nadur <19835357+intricate@users.noreply.github.com>
  • Loading branch information
iohk-bors[bot] and intricate authored Jul 15, 2020
2 parents 376a92d + 133cea0 commit c2666fd
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 17 deletions.
11 changes: 9 additions & 2 deletions cardano-cli/src/Cardano/CLI/Shelley/Commands.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@ module Cardano.CLI.Shelley.Commands
, PrivKeyFile (..)
, BlockId (..)
, QueryFilter (..)
, StakePoolVerificationKeyHashOrFile (..)
) where

import Prelude
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 (..))

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
27 changes: 22 additions & 5 deletions cardano-cli/src/Cardano/CLI/Shelley/Parsers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ pStakeAddress =
pStakeAddressDelegationCert :: Parser StakeAddressCmd
pStakeAddressDelegationCert = StakeKeyDelegationCert
<$> pStakeVerificationKeyFile
<*> pPoolStakingVerificationKeyFile
<*> pStakePoolVerificationKeyHashOrFile
<*> pOutputFile

pConvertITNKey :: Parser StakeAddressCmd
Expand Down Expand Up @@ -1310,8 +1310,8 @@ pStakeVerificationKeyFile =
)


pPoolStakingVerificationKeyFile :: Parser VerificationKeyFile
pPoolStakingVerificationKeyFile =
pPoolStakeVerificationKeyFile :: Parser VerificationKeyFile
pPoolStakeVerificationKeyFile =
VerificationKeyFile <$>
( Opt.strOption
( Opt.long "cold-verification-key-file"
Expand All @@ -1326,6 +1326,23 @@ pPoolStakingVerificationKeyFile =
)
)

pStakePoolVerificationKeyHash :: Parser (Hash StakePoolKey)
pStakePoolVerificationKeyHash =
Opt.option
(Opt.maybeReader spvkHash)
( Opt.long "stake-pool-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 <$>
Expand Down Expand Up @@ -1505,7 +1522,7 @@ pStakePoolMetadataHash =
pStakePoolRegistrationCert :: Parser PoolCmd
pStakePoolRegistrationCert =
PoolRegistrationCert
<$> pPoolStakingVerificationKeyFile
<$> pPoolStakeVerificationKeyFile
<*> pVRFVerificationKeyFile
<*> pPoolPledge
<*> pPoolCost
Expand All @@ -1520,7 +1537,7 @@ pStakePoolRegistrationCert =
pStakePoolRetirementCert :: Parser PoolCmd
pStakePoolRetirementCert =
PoolRetirementCert
<$> pPoolStakingVerificationKeyFile
<$> pPoolStakeVerificationKeyFile
<*> pEpochNo
<*> pOutputFile

Expand Down
26 changes: 16 additions & 10 deletions cardano-cli/src/Cardano/CLI/Shelley/Run/StakeAddress.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c2666fd

Please sign in to comment.