Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Added --expected-hash to cardano-cli hash anchor-data
  • Loading branch information
palas committed Sep 18, 2024
commit 23f11f4532eddeca7b36291f63a6e878d05f435c
2 changes: 2 additions & 0 deletions cardano-cli/src/Cardano/CLI/Commands/Hash.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module Cardano.CLI.Commands.Hash
where

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

import Cardano.CLI.Types.Common

Expand All @@ -24,6 +25,7 @@ data HashCmds
data HashAnchorDataCmdArgs
= HashAnchorDataCmdArgs
{ toHash :: !AnchorDataHashSource
, mExpectedHash :: !(Maybe (L.SafeHash L.StandardCrypto L.AnchorData))
, mOutFile :: !(Maybe (File () Out))
-- ^ The output file to which the hash is written
}
Expand Down
13 changes: 13 additions & 0 deletions cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3618,6 +3618,19 @@ pAnchorUrl =
ProposalUrl
<$> pUrl "anchor-url" "Anchor URL"

pExpectedHash :: Parser (L.SafeHash L.StandardCrypto L.AnchorData)
pExpectedHash =
Opt.option readSafeHash $
mconcat
[ Opt.long "expected-hash"
, Opt.metavar "HASH"
, Opt.help $
mconcat
[ "Expected hash for the anchor data for verification purposes. "
, "If provided, the hash of the anchor data will be compared to this value."
]
]

pAnchorDataHash :: Parser (L.SafeHash L.StandardCrypto L.AnchorData)
pAnchorDataHash =
Opt.option readSafeHash $
Expand Down
1 change: 1 addition & 0 deletions cardano-cli/src/Cardano/CLI/Options/Hash.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pHashAnchorDataCmd = do
Cmd.HashAnchorDataCmd
( Cmd.HashAnchorDataCmdArgs
<$> pAnchorDataHashSource
<*> optional pExpectedHash
<*> optional pOutputFile
)
)
Expand Down
33 changes: 18 additions & 15 deletions cardano-cli/src/Cardano/CLI/Run/Hash.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,24 @@ runHashAnchorDataCmd
:: ()
=> Cmd.HashAnchorDataCmdArgs
-> ExceptT HashCmdError IO ()
runHashAnchorDataCmd Cmd.HashAnchorDataCmdArgs{toHash, mOutFile} =
case toHash of
Cmd.AnchorDataHashSourceBinaryFile fp -> do
let path = unFile fp
bytes <- handleIOExceptT (HashReadFileError path) $ BS.readFile path
let hash = L.hashAnchorData $ L.AnchorData bytes
writeHash hash
Cmd.AnchorDataHashSourceTextFile fp -> do
let path = unFile fp
text <- handleIOExceptT (HashReadFileError path) $ Text.readFile path
let hash = L.hashAnchorData $ L.AnchorData $ Text.encodeUtf8 text
writeHash hash
Cmd.AnchorDataHashSourceText text -> do
let hash = L.hashAnchorData $ L.AnchorData $ Text.encodeUtf8 text
writeHash hash
runHashAnchorDataCmd Cmd.HashAnchorDataCmdArgs{toHash, mExpectedHash, mOutFile} = do
anchorData <-
L.AnchorData <$> case toHash of
Cmd.AnchorDataHashSourceBinaryFile fp -> do
let path = unFile fp
handleIOExceptT (HashReadFileError path) $ BS.readFile path
Cmd.AnchorDataHashSourceTextFile fp -> do
let path = unFile fp
text <- handleIOExceptT (HashReadFileError path) $ Text.readFile path
return $ Text.encodeUtf8 text
Cmd.AnchorDataHashSourceText text -> return $ Text.encodeUtf8 text
let hash = L.hashAnchorData anchorData
case mExpectedHash of
Just expectedHash
| hash /= expectedHash ->
left $ HashMismatchedHashError expectedHash hash
| otherwise -> liftIO $ putStrLn "Hashes match!"
Nothing -> writeHash hash
where
writeHash :: L.SafeHash L.StandardCrypto i -> ExceptT HashCmdError IO ()
writeHash hash = do
Expand Down
13 changes: 12 additions & 1 deletion cardano-cli/src/Cardano/CLI/Types/Errors/HashCmdError.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,29 @@ module Cardano.CLI.Types.Errors.HashCmdError
where

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

import Cardano.CLI.Read (ScriptDecodeError)
import Cardano.Ledger.SafeHash (extractHash)
import Cardano.Prelude (Exception (displayException), IOException)

data HashCmdError
= HashReadFileError !FilePath !IOException
= HashMismatchedHashError
!(L.SafeHash L.StandardCrypto L.AnchorData)
!(L.SafeHash L.StandardCrypto L.AnchorData)
| HashReadFileError !FilePath !IOException
| HashWriteFileError !(FileError ())
| HashReadScriptError !FilePath !(FileError ScriptDecodeError)
deriving Show

instance Error HashCmdError where
prettyError = \case
HashMismatchedHashError expectedHash actualHash ->
"Hashes do not match! \n"
<> "Expected: "
<> pretty (show (extractHash expectedHash))
<> "\n Actual: "
<> pretty (show (extractHash actualHash))
HashReadFileError filepath exc ->
"Cannot read " <> pretty filepath <> ": " <> pretty (displayException exc)
HashWriteFileError fileErr ->
Expand Down
1 change: 1 addition & 0 deletions cardano-cli/test/cardano-cli-golden/files/golden/help.cli
Original file line number Diff line number Diff line change
Expand Up @@ -12537,6 +12537,7 @@ Usage: cardano-cli hash anchor-data
| --file-binary FILEPATH
| --file-text FILEPATH
)
[--expected-hash HASH]
[--out-file FILEPATH]

Compute the hash of some anchor data (to then pass it to other commands).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Usage: cardano-cli hash anchor-data
| --file-binary FILEPATH
| --file-text FILEPATH
)
[--expected-hash HASH]
[--out-file FILEPATH]

Compute the hash of some anchor data (to then pass it to other commands).
Expand All @@ -11,5 +12,8 @@ Available options:
--text TEXT Text to hash as UTF-8
--file-binary FILEPATH Binary file to hash
--file-text FILEPATH Text file to hash
--expected-hash HASH Expected hash for the anchor data for verification
purposes. If provided, the hash of the anchor data
will be compared to this value.
--out-file FILEPATH The output file.
-h,--help Show this help text