Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept script hash in stake-and-vote-delegation-certificate command #264

Merged
merged 2 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2738,11 +2738,10 @@ pVoterType =
pVotingCredential :: Parser (VerificationKeyOrFile StakePoolKey)
pVotingCredential = pStakePoolVerificationKeyOrFile

pVoteDelegationTarget
:: Parser VoteDelegationTarget
pVoteDelegationTarget :: Parser VoteDelegationTarget
pVoteDelegationTarget =
asum
[ VoteDelegationTargetOfDRep <$> pDRepVerificationKeyOrHashOrFile
[ VoteDelegationTargetOfDRep <$> pDRepHashSource
, VoteDelegationTargetOfAbstain <$ pAlwaysAbstain
, VoteDelegationTargetOfAbstain <$ pAlwaysNoConfidence
]
Expand All @@ -2761,6 +2760,23 @@ pAlwaysNoConfidence =
, Opt.help "Always vote no confidence"
]

pDRepHashSource :: Parser DRepHashSource
pDRepHashSource =
asum
[ DRepHashSourceScript <$> pDRepScriptHash
, DRepHashSourceVerificationKey <$> pDRepVerificationKeyOrHashOrFile
]

pDRepScriptHash :: Parser ScriptHash
pDRepScriptHash =
Opt.option scriptHashReader $ mconcat
[ Opt.long "drep-script-hash"
, Opt.metavar "HASH"
, Opt.help $ mconcat
[ "DRep script hash (hex-encoded)."
]
]

pDRepVerificationKeyOrHashOrFile
:: Parser (VerificationKeyOrHashOrFile DRepKey)
pDRepVerificationKeyOrHashOrFile =
Expand Down
13 changes: 0 additions & 13 deletions cardano-cli/src/Cardano/CLI/EraBased/Options/Governance/DRep.hs
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,3 @@ pStakeTarget cOnwards =

, TargetAlwaysNoConfidence cOnwards <$ pAlwaysNoConfidence
]

pDRepScriptHash :: Parser ScriptHash
pDRepScriptHash =
Opt.option scriptHashReader $ mconcat
[ Opt.long "drep-script-hash"
, Opt.metavar "HASH"
, Opt.help $ mconcat
[ "DRep script hash (hex-encoded). "
]
]

scriptHashReader :: Opt.ReadM ScriptHash
scriptHashReader = Opt.eitherReader $ Right . fromString
34 changes: 17 additions & 17 deletions cardano-cli/src/Cardano/CLI/EraBased/Run/StakeAddress.hs
Original file line number Diff line number Diff line change
Expand Up @@ -227,23 +227,23 @@ runStakeAddressStakeAndVoteDelegationCertificateCmd w stakeVerifier poolVKeyOrHa
getStakeCredentialFromIdentifier stakeVerifier
& firstExceptT ShelleyStakeAddressCmdStakeCredentialError

delegatee <-
case voteDelegationTarget of
VoteDelegationTargetOfDRep drepVKeyOrHashOrFile -> do
DRepKeyHash drepKeyHash <-
lift (readVerificationKeyOrHashOrTextEnvFile AsDRepKey drepVKeyOrHashOrFile)
& onLeft (left . StakeAddressDelegationError . DelegationDRepReadError)

let drepCred = Ledger.DRepCredential $ Ledger.KeyHashObj drepKeyHash

pure
$ Ledger.DelegStakeVote
(conwayEraOnwardsConstraints w poolStakeVKeyHash)
(conwayEraOnwardsConstraints w drepCred)
VoteDelegationTargetOfAbstain ->
pure $ Ledger.DelegVote Ledger.DRepAlwaysAbstain
VoteDelegationTargetOfNoConfidence ->
pure $ Ledger.DelegVote Ledger.DRepAlwaysNoConfidence
drep <- case voteDelegationTarget of
VoteDelegationTargetOfDRep drepHashSource -> do
drepHash <- case drepHashSource of
DRepHashSourceScript (ScriptHash scriptHash) ->
pure $ Ledger.ScriptHashObj scriptHash
DRepHashSourceVerificationKey drepVKeyOrHashOrFile -> do
DRepKeyHash drepKeyHash <-
lift (readVerificationKeyOrHashOrTextEnvFile AsDRepKey drepVKeyOrHashOrFile)
& onLeft (left . StakeAddressDelegationError . DelegationDRepReadError)
pure $ Ledger.KeyHashObj drepKeyHash
pure $ Ledger.DRepCredential drepHash
VoteDelegationTargetOfAbstain ->
pure Ledger.DRepAlwaysAbstain
VoteDelegationTargetOfNoConfidence ->
pure Ledger.DRepAlwaysNoConfidence

let delegatee = Ledger.DelegStakeVote poolStakeVKeyHash drep

let certificate =
ConwayCertificate w
Expand Down
6 changes: 6 additions & 0 deletions cardano-cli/src/Cardano/CLI/Read.hs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ module Cardano.CLI.Read
, ReadSafeHashError(..)
, readHexAsSafeHash
, readSafeHash

, scriptHashReader
) where

import Cardano.Api as Api
Expand Down Expand Up @@ -121,6 +123,7 @@ import Data.Function ((&))
import Data.IORef (IORef, newIORef, readIORef, writeIORef)
import qualified Data.List as List
import qualified Data.Map.Strict as Map
import Data.String
import Data.Text (Text)
import qualified Data.Text as Text
import qualified Data.Text.Encoding as Text
Expand Down Expand Up @@ -1028,3 +1031,6 @@ readSafeHash =
Opt.eitherReader $ \s ->
readHexAsSafeHash (Text.pack s)
& first (Text.unpack . renderReadSafeHashError)

scriptHashReader :: Opt.ReadM ScriptHash
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does the error look like if it's an invalid script hash?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure. It's a pre-existing function moved from Cardano.CLI.EraBased.Options.Governance.DRep

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to not fail at all.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be broken, but I'm merging this on the basis that it's not a regression. We're just moving this flag from the drep delegation-certificate command which will soon be deleted. Will need to follow up on whether this flag actually works.

scriptHashReader = Opt.eitherReader $ Right . fromString
5 changes: 3 additions & 2 deletions cardano-cli/src/Cardano/CLI/Types/Governance.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ module Cardano.CLI.Types.Governance where
import Cardano.Api
import Cardano.Api.Shelley

import Cardano.CLI.Types.Key (VerificationKeyOrFile, VerificationKeyOrHashOrFile)
import Cardano.CLI.Types.Key (DRepHashSource, VerificationKeyOrFile,
VerificationKeyOrHashOrFile)

import Data.Word

Expand Down Expand Up @@ -51,7 +52,7 @@ data AnyVotingStakeVerificationKeyOrHashOrFile where
-> AnyVotingStakeVerificationKeyOrHashOrFile

data VoteDelegationTarget
= VoteDelegationTargetOfDRep (VerificationKeyOrHashOrFile DRepKey)
= VoteDelegationTargetOfDRep DRepHashSource
| VoteDelegationTargetOfAbstain
| VoteDelegationTargetOfNoConfidence
deriving (Eq, Show)
31 changes: 30 additions & 1 deletion cardano-cli/src/Cardano/CLI/Types/Key.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE UndecidableInstances #-}
Expand Down Expand Up @@ -37,16 +38,25 @@ module Cardano.CLI.Types.Key
, RegistrationTarget(..)

, ColdVerificationKeyOrFile(..)

, DRepHashSource(..)

, readDRepCredential
) where

import Cardano.Api
import qualified Cardano.Api.Ledger as L
import Cardano.Api.Shelley

import Cardano.CLI.Types.Common
import Cardano.CLI.Types.Errors.DelegationError

import Control.Monad.IO.Class (MonadIO (..))
import Control.Monad.Trans
import Control.Monad.Trans.Except
import Control.Monad.Trans.Except.Extra
import Data.Bifunctor (Bifunctor (..))
import qualified Data.ByteString as BS
import Data.Function
import qualified Data.List.NonEmpty as NE
import Data.Text (Text)
import qualified Data.Text as Text
Expand Down Expand Up @@ -343,3 +353,22 @@ data ColdVerificationKeyOrFile
| ColdGenesisDelegateVerificationKey !(VerificationKey GenesisDelegateKey)
| ColdVerificationKeyFile !(VerificationKeyFile In)
deriving Show

data DRepHashSource
= DRepHashSourceScript
ScriptHash
| DRepHashSourceVerificationKey
(VerificationKeyOrHashOrFile DRepKey)
deriving (Eq, Show)

readDRepCredential :: ()
=> DRepHashSource
-> ExceptT DelegationError IO (L.Credential 'L.DRepRole L.StandardCrypto)
readDRepCredential = \case
DRepHashSourceScript (ScriptHash scriptHash) ->
pure (L.ScriptHashObj scriptHash)
DRepHashSourceVerificationKey drepVKeyOrHashOrFile -> do
DRepKeyHash drepKeyHash <-
lift (readVerificationKeyOrHashOrTextEnvFile AsDRepKey drepVKeyOrHashOrFile)
& onLeft (left . DelegationDRepReadError)
pure $ L.KeyHashObj drepKeyHash
3 changes: 2 additions & 1 deletion cardano-cli/test/cardano-cli-golden/files/golden/help.cli
Original file line number Diff line number Diff line change
Expand Up @@ -3706,7 +3706,8 @@ Usage: cardano-cli conway stake-address stake-and-vote-delegation-certificate
| --cold-verification-key-file FILE
| --stake-pool-id STAKE_POOL_ID
)
( --drep-verification-key STRING
( --drep-script-hash HASH
| --drep-verification-key STRING
| --drep-verification-key-file FILE
| --drep-key-hash HASH
| --always-abstain
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ Usage: cardano-cli conway stake-address stake-and-vote-delegation-certificate
| --cold-verification-key-file FILE
| --stake-pool-id STAKE_POOL_ID
)
( --drep-verification-key STRING
( --drep-script-hash HASH
| --drep-verification-key STRING
| --drep-verification-key-file FILE
| --drep-key-hash HASH
| --always-abstain
Expand All @@ -34,6 +35,7 @@ Available options:
Stake pool ID/verification key hash (either
Bech32-encoded or hex-encoded). Zero or more
occurences of this option is allowed.
--drep-script-hash HASH DRep script hash (hex-encoded).
--drep-verification-key STRING
DRep verification key (Bech32 or hex-encoded).
--drep-verification-key-file FILE
Expand Down