Skip to content

Commit

Permalink
governance: add hash command
Browse files Browse the repository at this point in the history
  • Loading branch information
smelc committed Nov 7, 2023
1 parent ba858ec commit d7e6509
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cardano-cli/cardano-cli.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ library
Cardano.CLI.EraBased.Commands.Governance.Actions
Cardano.CLI.EraBased.Commands.Governance.Committee
Cardano.CLI.EraBased.Commands.Governance.DRep
Cardano.CLI.EraBased.Commands.Governance.Hash
Cardano.CLI.EraBased.Commands.Governance.Poll
Cardano.CLI.EraBased.Commands.Governance.Vote
Cardano.CLI.EraBased.Commands.Key
Expand All @@ -90,6 +91,7 @@ library
Cardano.CLI.EraBased.Options.Governance.Actions
Cardano.CLI.EraBased.Options.Governance.Committee
Cardano.CLI.EraBased.Options.Governance.DRep
Cardano.CLI.EraBased.Options.Governance.Hash
Cardano.CLI.EraBased.Options.Governance.Poll
Cardano.CLI.EraBased.Options.Governance.Vote
Cardano.CLI.EraBased.Options.Key
Expand All @@ -107,6 +109,7 @@ library
Cardano.CLI.EraBased.Run.Governance.Actions
Cardano.CLI.EraBased.Run.Governance.Committee
Cardano.CLI.EraBased.Run.Governance.DRep
Cardano.CLI.EraBased.Run.Governance.Hash
Cardano.CLI.EraBased.Run.Governance.GenesisKeyDelegationCertificate
Cardano.CLI.EraBased.Run.Governance.Poll
Cardano.CLI.EraBased.Run.Governance.Vote
Expand Down Expand Up @@ -165,6 +168,7 @@ library
Cardano.CLI.Types.Errors.GovernanceActionsError
Cardano.CLI.Types.Errors.GovernanceCmdError
Cardano.CLI.Types.Errors.GovernanceCommitteeError
Cardano.CLI.Types.Errors.GovernanceHashError
Cardano.CLI.Types.Errors.GovernanceQueryError
Cardano.CLI.Types.Errors.GovernanceVoteCmdError
Cardano.CLI.Types.Errors.ItnKeyConversionError
Expand Down
5 changes: 5 additions & 0 deletions cardano-cli/src/Cardano/CLI/EraBased/Commands/Governance.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import Cardano.Api.Shelley (VrfKey)
import Cardano.CLI.EraBased.Commands.Governance.Actions
import Cardano.CLI.EraBased.Commands.Governance.Committee
import Cardano.CLI.EraBased.Commands.Governance.DRep
import Cardano.CLI.EraBased.Commands.Governance.Hash
import Cardano.CLI.EraBased.Commands.Governance.Poll
import Cardano.CLI.EraBased.Commands.Governance.Vote
import Cardano.CLI.Types.Key (VerificationKeyOrHashOrFile)
Expand Down Expand Up @@ -46,6 +47,8 @@ data GovernanceCmds era
(GovernanceCommitteeCmds era)
| GovernanceDRepCmds
(GovernanceDRepCmds era)
| GovernanceHashCmds
(GovernanceHashCmds era)
| GovernancePollCmds
(GovernancePollCmds era)
| GovernanceVoteCmds
Expand All @@ -67,6 +70,8 @@ renderGovernanceCmds = \case
renderGovernanceCommitteeCmds cmds
GovernanceDRepCmds cmds ->
renderGovernanceDRepCmds cmds
GovernanceHashCmds cmds ->
renderGovernanceHashCmds cmds
GovernancePollCmds cmds ->
renderGovernancePollCmds cmds
GovernanceVoteCmds cmds ->
Expand Down
29 changes: 29 additions & 0 deletions cardano-cli/src/Cardano/CLI/EraBased/Commands/Governance/Hash.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE LambdaCase #-}


module Cardano.CLI.EraBased.Commands.Governance.Hash
(
GovernanceHashCmds (..),
GovernanceHashCmdArgs (..),
renderGovernanceHashCmds

) where

import Cardano.Api

import Cardano.CLI.Types.Common

import Data.Text (Text)

newtype GovernanceHashCmds era = GovernanceHashCmd (GovernanceHashCmdArgs era)

data GovernanceHashCmdArgs era
= GovernanceHashCmdArgs {
eon :: !(ConwayEraOnwards era),
toHash :: !(Either (File ProposalText In) Text) -- ^ Either a file to hash (binary), or some string (text)
}

renderGovernanceHashCmds :: GovernanceHashCmds era -> Text
renderGovernanceHashCmds =
\case GovernanceHashCmd {} -> "governance hash"
2 changes: 2 additions & 0 deletions cardano-cli/src/Cardano/CLI/EraBased/Options/Governance.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Cardano.CLI.EraBased.Options.Common
import Cardano.CLI.EraBased.Options.Governance.Actions
import Cardano.CLI.EraBased.Options.Governance.Committee
import Cardano.CLI.EraBased.Options.Governance.DRep
import Cardano.CLI.EraBased.Options.Governance.Hash
import Cardano.CLI.EraBased.Options.Governance.Poll
import Cardano.CLI.EraBased.Options.Governance.Vote

Expand All @@ -36,6 +37,7 @@ pGovernanceCmds era =
, fmap GovernanceActionCmds <$> pGovernanceActionCmds era
, fmap GovernanceCommitteeCmds <$> pGovernanceCommitteeCmds era
, fmap GovernanceDRepCmds <$> pGovernanceDRepCmds era
, fmap GovernanceHashCmds <$> pGovernanceHashCmds era
, fmap GovernancePollCmds <$> pGovernancePollCmds era
, fmap GovernanceVoteCmds <$> pGovernanceVoteCmds era
]
Expand Down
51 changes: 51 additions & 0 deletions cardano-cli/src/Cardano/CLI/EraBased/Options/Governance/Hash.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}

module Cardano.CLI.EraBased.Options.Governance.Hash
(
pGovernanceHashCmds
) where

import Cardano.Api

import Cardano.CLI.EraBased.Commands.Governance.Hash
(GovernanceHashCmdArgs (GovernanceHashCmdArgs))
import qualified Cardano.CLI.EraBased.Commands.Governance.Hash as Cmd
import Cardano.CLI.EraBased.Options.Common

import Data.Foldable
import Options.Applicative
import qualified Options.Applicative as Opt

pGovernanceHashCmds :: ()
=> CardanoEra era
-> Maybe (Parser (Cmd.GovernanceHashCmds era))
pGovernanceHashCmds era =
subInfoParser "hash"
( Opt.progDesc "Governance hash commands.")
[ pGovernanceHashCmd era ]

pGovernanceHashCmd
:: CardanoEra era
-> Maybe (Parser (Cmd.GovernanceHashCmds era))
pGovernanceHashCmd era = do
eon <- forEraMaybeEon era
return
$ subParser "hash"
$ Opt.info
( asum $ map (fmap Cmd.GovernanceHashCmd)
[
GovernanceHashCmdArgs eon . Left
<$> pFileInDirection "file" "File to hash (treated as binary: text-encoding agnostic)",
GovernanceHashCmdArgs eon . Right
<$> Opt.strOption
( mconcat
[ Opt.long "text"
, Opt.metavar "TEXT"
, Opt.help "Text to hash as UTF-8"
]
)
]
)
$ Opt.progDesc "Hash governance data"
4 changes: 4 additions & 0 deletions cardano-cli/src/Cardano/CLI/EraBased/Run/Governance.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import Cardano.CLI.EraBased.Run.Governance.Actions
import Cardano.CLI.EraBased.Run.Governance.Committee
import Cardano.CLI.EraBased.Run.Governance.DRep
import Cardano.CLI.EraBased.Run.Governance.GenesisKeyDelegationCertificate
import Cardano.CLI.EraBased.Run.Governance.Hash
import Cardano.CLI.EraBased.Run.Governance.Poll
import Cardano.CLI.EraBased.Run.Governance.Vote
import Cardano.CLI.Types.Errors.CmdError
Expand Down Expand Up @@ -67,6 +68,9 @@ runGovernanceCmds = \case
Cmd.GovernanceDRepCmds cmds ->
runGovernanceDRepCmds cmds

Cmd.GovernanceHashCmds cmds ->
runGovernanceHashCmds cmds

Cmd.GovernancePollCmds cmds ->
runGovernancePollCmds cmds
& firstExceptT CmdGovernanceCmdError
Expand Down
57 changes: 57 additions & 0 deletions cardano-cli/src/Cardano/CLI/EraBased/Run/Governance/Hash.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}

{- HLINT ignore "Use let" -}

module Cardano.CLI.EraBased.Run.Governance.Hash
( runGovernanceHashCmds
) where

import Cardano.Api

import qualified Cardano.CLI.EraBased.Commands.Governance.Hash as Cmd
import Cardano.CLI.Types.Errors.CmdError
import Cardano.CLI.Types.Errors.GovernanceCmdError
import Cardano.CLI.Types.Errors.GovernanceHashError
import qualified Cardano.Ledger.BaseTypes as Ledger
import Cardano.Ledger.Crypto
import Cardano.Ledger.SafeHash (extractHash)
import qualified Cardano.Ledger.SafeHash as Ledger

import Control.Monad.IO.Class
import Control.Monad.Trans.Except
import Control.Monad.Trans.Except.Extra
import qualified Data.ByteString as BS
import Data.Function
import qualified Data.Text.Encoding as Text

runGovernanceHashCmds :: ()
=> Cmd.GovernanceHashCmds era
-> ExceptT CmdError IO ()
runGovernanceHashCmds (Cmd.GovernanceHashCmd args)=
runGovernanceHashCmd args
& firstExceptT (CmdGovernanceCmdError . GovernanceCmdHashError)

runGovernanceHashCmd :: ()
=> Cmd.GovernanceHashCmdArgs era
-> ExceptT GovernanceHashError IO ()
runGovernanceHashCmd Cmd.GovernanceHashCmdArgs { toHash } =
-- TODO @smelc we probably want an option to write the computed hash to a file
-- This can be done in a separate PR
case toHash of
Left fp -> do
bytes <- liftIO $ BS.readFile $ unFile fp
_utf8EncodedText <- firstExceptT GovernanceHashUnicodeError . hoistEither $ Text.decodeUtf8' bytes
let hash = Ledger.hashAnchorData $ Ledger.AnchorData bytes
printHash hash
Right text -> do
let hash = Ledger.hashAnchorData $ Ledger.AnchorData $ Text.encodeUtf8 text
printHash hash
where
printHash :: Ledger.SafeHash StandardCrypto i -> ExceptT GovernanceHashError IO ()
printHash = liftIO . print . extractHash
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Cardano.Api.Shelley

import Cardano.Binary (DecoderError)
import Cardano.CLI.Read
import Cardano.CLI.Types.Errors.GovernanceHashError (GovernanceHashError)
import Cardano.CLI.Types.Errors.StakeAddressCmdError

import qualified Data.List as List
Expand All @@ -26,6 +27,7 @@ data GovernanceCmdError
| ReadFileError (FileError InputDecodeError)
-- Governance action related
| GovernanceCmdConstitutionError ConstitutionError
| GovernanceCmdHashError !GovernanceHashError
| GovernanceCmdProposalError ProposalError
| GovernanceCmdTextEnvReadError !(FileError TextEnvelopeError)
| GovernanceCmdCddlError !CddlError
Expand Down Expand Up @@ -69,6 +71,8 @@ instance Error GovernanceCmdError where
displayError fileError
GovernanceCmdConstitutionError e ->
"Constitution error " <> show e -- TODO Conway render this properly
GovernanceCmdHashError e ->
"Hash error " <> displayError e
GovernanceCmdProposalError e ->
"Proposal error " <> show e -- TODO Conway render this properly
GovernanceCmdTextEnvReadError fileError ->
Expand Down
15 changes: 15 additions & 0 deletions cardano-cli/src/Cardano/CLI/Types/Errors/GovernanceHashError.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Cardano.CLI.Types.Errors.GovernanceHashError
( GovernanceHashError(..)
) where

import Cardano.Api

import Cardano.Prelude (UnicodeException)

data GovernanceHashError
= GovernanceHashReadFileError (FileError InputDecodeError)
| GovernanceHashUnicodeError UnicodeException
deriving Show

instance Error GovernanceHashError where
displayError = undefined

0 comments on commit d7e6509

Please sign in to comment.