-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #895 from IntersectMBO/add-hash-validation
Add hash validation and support for HTTP(S) and IPFS to command `hash anchor-data`
- Loading branch information
Showing
14 changed files
with
468 additions
and
36 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
cardano-cli/test/cardano-cli-test/files/input/example_anchor_data.txt -text |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,65 @@ | ||
{-# LANGUAGE InstanceSigs #-} | ||
{-# LANGUAGE LambdaCase #-} | ||
|
||
module Cardano.CLI.Types.Errors.HashCmdError | ||
( HashCmdError (..) | ||
, HttpRequestError (..) | ||
) | ||
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) | ||
|
||
import Network.HTTP.Client (HttpException) | ||
|
||
data HashCmdError | ||
= HashReadFileError !FilePath !IOException | ||
= HashMismatchedHashError | ||
!(L.SafeHash L.StandardCrypto L.AnchorData) | ||
-- ^ Expected hash | ||
!(L.SafeHash L.StandardCrypto L.AnchorData) | ||
-- ^ Actual hash | ||
| HashReadFileError !FilePath !IOException | ||
| HashWriteFileError !(FileError ()) | ||
| HashReadScriptError !FilePath !(FileError ScriptDecodeError) | ||
| HashInvalidURLError !String | ||
| HashReadEnvVarError !IOException | ||
| HashIpfsGatewayNotSetError | ||
| HashUnsupportedURLSchemeError !String | ||
| HashGetFileFromHttpError !HttpRequestError | ||
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 -> | ||
prettyError fileErr | ||
HashReadScriptError filepath err -> | ||
"Cannot read script at " <> pretty filepath <> ": " <> prettyError err | ||
HashInvalidURLError text -> "Cannot parse URI: " <> pretty text | ||
HashUnsupportedURLSchemeError text -> "Unsupported URL scheme: " <> pretty text | ||
HashReadEnvVarError exc -> "Cannot read environment variable: " <> pretty (displayException exc) | ||
HashIpfsGatewayNotSetError -> "IPFS schema requires IPFS_GATEWAY_URI environment variable to be set." | ||
HashGetFileFromHttpError err -> pretty $ displayException err | ||
|
||
data HttpRequestError | ||
= BadStatusCodeHRE !Int !String | ||
| HttpExceptionHRE !HttpException | ||
| IOExceptionHRE !IOException | ||
deriving Show | ||
|
||
instance Exception HttpRequestError where | ||
displayException :: HttpRequestError -> String | ||
displayException (BadStatusCodeHRE code description) = "Bad status code when downloading anchor data: " <> show code <> " (" <> description <> ")" | ||
displayException (HttpExceptionHRE exc) = "HTTP(S) request error when downloading anchor data: " <> displayException exc | ||
displayException (IOExceptionHRE exc) = "I/O error when downloading anchor data: " <> displayException exc |
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
Oops, something went wrong.