Skip to content

Commit b0de7e7

Browse files
committed
Move genesis hash to hash genesis-file
1 parent 8216024 commit b0de7e7

File tree

8 files changed

+51
-5
lines changed

8 files changed

+51
-5
lines changed

cardano-cli/src/Cardano/CLI/Commands/Hash.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{-# LANGUAGE DataKinds #-}
2+
{-# LANGUAGE DerivingStrategies #-}
23
{-# LANGUAGE DuplicateRecordFields #-}
34
{-# LANGUAGE LambdaCase #-}
45

@@ -22,6 +23,7 @@ import Data.Text (Text)
2223
data HashCmds
2324
= HashAnchorDataCmd !HashAnchorDataCmdArgs
2425
| HashScriptCmd !HashScriptCmdArgs
26+
| HashGenesisFile !GenesisFile
2527

2628
data HashGoal hash
2729
= -- | The hash is written to stdout
@@ -58,3 +60,4 @@ renderHashCmds :: HashCmds -> Text
5860
renderHashCmds = \case
5961
HashAnchorDataCmd{} -> "hash anchor-data"
6062
HashScriptCmd{} -> "hash script"
63+
HashGenesisFile{} -> "hash genesis-file"

cardano-cli/src/Cardano/CLI/Options/Hash.hs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pHashCmds :: Parser Cmd.HashCmds
2020
pHashCmds =
2121
subParser "hash" $
2222
Opt.info
23-
(asum [pHashAnchorDataCmd, pHashScriptCmd])
23+
(asum [pHashAnchorDataCmd, pHashScriptCmd, pHashGenesisHashCmd])
2424
( Opt.progDesc $
2525
mconcat
2626
[ "Compute the hash to pass to the various --*-hash arguments of commands."
@@ -79,3 +79,13 @@ pHashScriptCmd = do
7979
)
8080
)
8181
$ Opt.progDesc "Compute the hash of a script (to then pass it to other commands)."
82+
83+
pHashGenesisHashCmd :: Parser Cmd.HashCmds
84+
pHashGenesisHashCmd =
85+
subParser "genesis-file" $
86+
Opt.info pGenesisHash $
87+
Opt.progDesc "Compute the hash of a genesis file."
88+
89+
pGenesisHash :: Parser Cmd.HashCmds
90+
pGenesisHash =
91+
Cmd.HashGenesisFile <$> pGenesisFile "The genesis file."

cardano-cli/src/Cardano/CLI/Run/Hash.hs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ import qualified Cardano.Api.Ledger as L
2121
import qualified Cardano.CLI.Commands.Hash as Cmd
2222
import Cardano.CLI.Parser (stringToAnchorScheme)
2323
import Cardano.CLI.Read
24-
import Cardano.CLI.Types.Common (AnchorScheme (..), MustCheckHash (..),
24+
import Cardano.CLI.Types.Common (AnchorScheme (..), GenesisFile (..), MustCheckHash (..),
2525
PotentiallyCheckedAnchor (..), SupportedSchemes)
2626
import Cardano.CLI.Types.Errors.HashCmdError
2727
import Cardano.Crypto.Hash (hashToTextAsHex)
28-
import Cardano.Prelude (first)
28+
import qualified Cardano.Crypto.Hash as Crypto
29+
import Cardano.Prelude (ByteString, first)
2930

3031
import Control.Exception (throw)
3132
import Control.Monad (when)
@@ -55,6 +56,7 @@ runHashCmds
5556
runHashCmds = \case
5657
Cmd.HashAnchorDataCmd args -> runHashAnchorDataCmd args
5758
Cmd.HashScriptCmd args -> runHashScriptCmd args
59+
Cmd.HashGenesisFile args -> runHashGenesisFile args
5860

5961
runHashAnchorDataCmd
6062
:: ()
@@ -217,3 +219,12 @@ carryHashChecks potentiallyCheckedAnchor =
217219
TrustHash -> pure ()
218220
where
219221
anchor = pcaAnchor potentiallyCheckedAnchor
222+
223+
runHashGenesisFile :: GenesisFile -> ExceptT HashCmdError IO ()
224+
runHashGenesisFile (GenesisFile fpath) = do
225+
content <-
226+
handleIOExceptT (HashGenesisCmdGenesisFileError . FileIOError fpath) $
227+
BS.readFile fpath
228+
let gh :: Crypto.Hash Crypto.Blake2b_256 ByteString
229+
gh = Crypto.hashWith id content
230+
liftIO $ Text.putStrLn (Crypto.hashToTextAsHex gh)

cardano-cli/src/Cardano/CLI/Types/Errors/HashCmdError.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ data HashCmdError
2727
| HashWriteFileError !(FileError ())
2828
| HashReadScriptError !FilePath !(FileError ScriptDecodeError)
2929
| HashFetchURLError !FetchURLError
30+
| HashGenesisCmdGenesisFileError !(FileError ())
3031
deriving Show
3132

3233
instance Error HashCmdError where
@@ -45,6 +46,8 @@ instance Error HashCmdError where
4546
"Cannot read script at" <+> pretty filepath <> ":" <+> prettyError err
4647
HashFetchURLError fetchErr ->
4748
pretty (displayException fetchErr)
49+
HashGenesisCmdGenesisFileError fe ->
50+
prettyError fe
4851

4952
data FetchURLError
5053
= FetchURLInvalidURLError !String

cardano-cli/test/cardano-cli-golden/files/golden/help.cli

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10589,7 +10589,7 @@ Usage: cardano-cli latest transaction txid
1058910589

1059010590
Print a transaction identifier.
1059110591

10592-
Usage: cardano-cli hash (anchor-data | script)
10592+
Usage: cardano-cli hash (anchor-data | script | genesis-file)
1059310593

1059410594
Compute the hash to pass to the various --*-hash arguments of commands.
1059510595

@@ -10609,6 +10609,10 @@ Usage: cardano-cli hash script --script-file FILEPATH [--out-file FILEPATH]
1060910609

1061010610
Compute the hash of a script (to then pass it to other commands).
1061110611

10612+
Usage: cardano-cli hash genesis-file --genesis FILEPATH
10613+
10614+
Compute the hash of a genesis file.
10615+
1061210616
Usage: cardano-cli ping [-c|--count COUNT]
1061310617
((-h|--host HOST) | (-u|--unixsock SOCKET))
1061410618
[-p|--port PORT]

cardano-cli/test/cardano-cli-golden/files/golden/help/hash.cli

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Usage: cardano-cli hash (anchor-data | script)
1+
Usage: cardano-cli hash (anchor-data | script | genesis-file)
22

33
Compute the hash to pass to the various --*-hash arguments of commands.
44

@@ -10,3 +10,4 @@ Available commands:
1010
to other commands).
1111
script Compute the hash of a script (to then pass it to
1212
other commands).
13+
genesis-file Compute the hash of a genesis file.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Usage: cardano-cli hash genesis-file --genesis FILEPATH
2+
3+
Compute the hash of a genesis file.
4+
5+
Available options:
6+
--genesis FILEPATH The genesis file.
7+
-h,--help Show this help text
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Usage: cardano-cli hash hash --genesis FILEPATH
2+
3+
Compute the hash of a genesis file
4+
5+
Available options:
6+
--genesis FILEPATH The genesis file.
7+
-h,--help Show this help text

0 commit comments

Comments
 (0)