Skip to content

Commit

Permalink
Accept either a pool vkey hash or file in StakeKeyDelegationCert command
Browse files Browse the repository at this point in the history
  • Loading branch information
intricate committed Jul 15, 2020
1 parent 3afec3d commit 751b610
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 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)
19 changes: 18 additions & 1 deletion 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
<*> pPoolStakeVerificationKeyFile
<*> pStakePoolVerificationKeyHashOrFile
<*> pOutputFile

pConvertITNKey :: Parser StakeAddressCmd
Expand Down Expand Up @@ -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 <$>
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 751b610

Please sign in to comment.