-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
cardano-cli/src/Cardano/CLI/EraBased/Commands/Governance/Hash.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
cardano-cli/src/Cardano/CLI/EraBased/Options/Governance/Hash.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
cardano-cli/src/Cardano/CLI/EraBased/Run/Governance/Hash.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
cardano-cli/src/Cardano/CLI/Types/Errors/GovernanceHashError.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |