Skip to content

Commit

Permalink
Use updated deserialiseFromRawBytesHex
Browse files Browse the repository at this point in the history
  • Loading branch information
cblp committed Jan 11, 2022
1 parent 6a70da3 commit 794e8e9
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 98 deletions.
4 changes: 2 additions & 2 deletions bench/tx-generator/src/Cardano/Benchmarking/PlutusExample.hs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ readScript fp = do
toScriptHash :: String -> Hash ScriptData
toScriptHash str
= case deserialiseFromRawBytesHex (AsHash AsScriptData) (BSC.pack str) of
Just x -> x
Nothing -> error $ "Invalid datum hash: " ++ show str
Right x -> x
Left msg -> error $ "Invalid datum hash: " ++ msg

preExecuteScript ::
ProtocolParameters
Expand Down
40 changes: 16 additions & 24 deletions cardano-api/src/Cardano/Api/Script.hs
Original file line number Diff line number Diff line change
Expand Up @@ -97,47 +97,38 @@ module Cardano.Api.Script (

import Prelude

import Control.Applicative
import Control.Monad
import Data.Aeson (Value (..), object, (.:), (.=))
import qualified Data.Aeson as Aeson
import qualified Data.Aeson.Encoding as Aeson
import qualified Data.Aeson.Types as Aeson
import qualified Data.ByteString.Lazy as LBS
import Data.ByteString.Short (ShortByteString)
import qualified Data.ByteString.Short as SBS
import Data.Foldable (toList)
import Data.Scientific (toBoundedInteger)
import qualified Data.Sequence.Strict as Seq
import Data.String (IsString)
import Data.Text (Text)
import qualified Data.Text as Text
import qualified Data.Text.Encoding as Text
import Data.Type.Equality (TestEquality (..), (:~:) (Refl))
import Data.Typeable (Typeable)
import Numeric.Natural (Natural)

import Data.Aeson (Value (..), object, (.:), (.=))
import qualified Data.Aeson as Aeson
import qualified Data.Aeson.Encoding as Aeson
import qualified Data.Aeson.Types as Aeson
import qualified Data.Sequence.Strict as Seq
import Data.Vector (Vector)
import qualified Data.Vector as Vector

import Control.Applicative
import Control.Monad
import Numeric.Natural (Natural)

import qualified Cardano.Binary as CBOR

import qualified Cardano.Crypto.Hash.Class as Crypto

import Cardano.Slotting.Slot (SlotNo)

import qualified Cardano.Ledger.Alonzo.Language as Alonzo
import qualified Cardano.Ledger.Alonzo.Scripts as Alonzo
import qualified Cardano.Ledger.Core as Ledger
import qualified Cardano.Ledger.Era as Ledger

import qualified Cardano.Ledger.Keys as Shelley
import qualified Cardano.Ledger.Shelley.Scripts as Shelley
import qualified Cardano.Ledger.ShelleyMA.Timelocks as Timelock
import Cardano.Slotting.Slot (SlotNo)
import Ouroboros.Consensus.Shelley.Eras (StandardCrypto)

import qualified Cardano.Ledger.Alonzo.Language as Alonzo
import qualified Cardano.Ledger.Alonzo.Scripts as Alonzo

import qualified Plutus.V1.Ledger.Examples as Plutus

import Cardano.Api.Eras
Expand All @@ -150,6 +141,7 @@ import Cardano.Api.SerialiseJSON
import Cardano.Api.SerialiseRaw
import Cardano.Api.SerialiseTextEnvelope
import Cardano.Api.SerialiseUsing
import Cardano.Api.Utils (failEitherWith)

{- HLINT ignore "Use section" -}

Expand Down Expand Up @@ -1301,7 +1293,7 @@ parseScriptAfter lang =
_ -> fail "\"after\" script value not found"

parsePaymentKeyHash :: Text -> Aeson.Parser (Hash PaymentKey)
parsePaymentKeyHash txt =
case deserialiseFromRawBytesHex (AsHash AsPaymentKey) (Text.encodeUtf8 txt) of
Just payKeyHash -> return payKeyHash
Nothing -> fail $ "Error deserialising payment key hash: " <> Text.unpack txt
parsePaymentKeyHash =
failEitherWith ("Error deserialising payment key hash: " ++)
. deserialiseFromRawBytesHex (AsHash AsPaymentKey)
. Text.encodeUtf8
14 changes: 6 additions & 8 deletions cardano-api/src/Cardano/Api/TxBody.hs
Original file line number Diff line number Diff line change
Expand Up @@ -406,10 +406,9 @@ instance FromJSONKey TxIn where

parseTxId :: Parsec.Parser TxId
parseTxId = do
str <- Parsec.many1 Parsec.hexDigit Parsec.<?> "transaction id (hexadecimal)"
case deserialiseFromRawBytesHex AsTxId (BSC.pack str) of
Just addr -> return addr
Nothing -> fail $ "Incorrect transaction id format:: " ++ show str
str <- some Parsec.hexDigit <?> "transaction id (hexadecimal)"
failEitherWith ("Incorrect transaction id format: " ++) $
deserialiseFromRawBytesHex AsTxId $ BSC.pack str

parseTxIn :: Parsec.Parser TxIn
parseTxIn = TxIn <$> parseTxId <*> (Parsec.char '#' *> parseTxIx)
Expand Down Expand Up @@ -1161,10 +1160,9 @@ pattern TxOutDatum s d <- TxOutDatum' s _ d

parseHash :: SerialiseAsRawBytes (Hash a) => AsType (Hash a) -> Parsec.Parser (Hash a)
parseHash asType = do
str <- Parsec.many1 Parsec.hexDigit Parsec.<?> "hash"
case deserialiseFromRawBytesHex asType (BSC.pack str) of
Just sdh -> return sdh
Nothing -> fail $ "Failed to parse hash: " ++ show str
str <- some Parsec.hexDigit <?> "hash"
failEitherWith ("Failed to parse hash: " ++) $
deserialiseFromRawBytesHex asType (BSC.pack str)

-- ----------------------------------------------------------------------------
-- Transaction fees
Expand Down
14 changes: 7 additions & 7 deletions cardano-api/src/Cardano/Api/Utils.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
{-# LANGUAGE LambdaCase #-}

-- | Internal utils for the other Api modules
--
module Cardano.Api.Utils
Expand All @@ -8,7 +6,8 @@ module Cardano.Api.Utils
, formatParsecError
, noInlineMaybeToStrictMaybe
, runParsecParser
, note
, failEither
, failEitherWith
) where

import Prelude
Expand Down Expand Up @@ -46,7 +45,8 @@ runParsecParser parser input =
Right txin -> pure txin
Left parseError -> fail $ formatParsecError parseError

note :: MonadFail m => String -> Maybe a -> m a
note msg = \case
Nothing -> fail msg
Just a -> pure a
failEither :: MonadFail m => Either String a -> m a
failEither = either fail pure

failEitherWith :: MonadFail m => (e -> String) -> Either e a -> m a
failEitherWith f = either (fail . f) pure
9 changes: 3 additions & 6 deletions cardano-api/src/Cardano/Api/Value.hs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ import qualified Data.Text as Text
import qualified Data.Text.Encoding as Text

import qualified Cardano.Chain.Common as Byron

import qualified Cardano.Ledger.Coin as Shelley
import Cardano.Ledger.Crypto (StandardCrypto)
import qualified Cardano.Ledger.Mary.Value as Mary
Expand All @@ -83,7 +82,7 @@ import Cardano.Api.Script
import Cardano.Api.SerialiseCBOR
import Cardano.Api.SerialiseRaw
import Cardano.Api.SerialiseUsing
import Cardano.Api.Utils (note)
import Cardano.Api.Utils (failEitherWith)


-- ----------------------------------------------------------------------------
Expand Down Expand Up @@ -359,11 +358,9 @@ instance FromJSON ValueNestedRep where
parsePid ("lovelace", q) = ValueNestedBundleAda <$> parseJSON q
parsePid (pid, quantityBundleJson) = do
sHash <-
note ("Expected hex encoded PolicyId but got: " <> Text.unpack pid) $
failEitherWith ("Failure when deserialising PolicyId: " ++) $
deserialiseFromRawBytesHex AsScriptHash $ Text.encodeUtf8 pid
quantityBundle <- parseJSON quantityBundleJson
pure $ ValueNestedBundle (PolicyId sHash) quantityBundle

ValueNestedBundle (PolicyId sHash) <$> parseJSON quantityBundleJson

-- ----------------------------------------------------------------------------
-- Printing and pretty-printing
Expand Down
8 changes: 4 additions & 4 deletions cardano-api/src/Cardano/Api/ValueParser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import Text.Parsec.String (Parser)
import Text.ParserCombinators.Parsec.Combinator (many1)

import Cardano.Api.SerialiseRaw
import Cardano.Api.Utils (note)
import Cardano.Api.Utils (failEitherWith)
import Cardano.Api.Value

-- | Parse a 'Value' from its string representation.
Expand Down Expand Up @@ -115,16 +115,16 @@ decimal = do
assetName :: Parser AssetName
assetName = do
hexText <- many hexDigit
note "AssetName deserisalisation failed" $
failEitherWith ("AssetName deserisalisation failed: " ++) $
deserialiseFromRawBytesHex AsAssetName $ BSC.pack hexText

-- | Policy ID parser.
policyId :: Parser PolicyId
policyId = do
hexText <- many1 hexDigit
case textToPolicyId hexText of
Just p -> pure p
Nothing ->
Right p -> pure p
Left _ ->
fail $ "expecting a 56 hex-encoded policy ID, but found only "
++ show (length hexText) ++ " hex digits"
where
Expand Down
5 changes: 2 additions & 3 deletions cardano-cli/src/Cardano/CLI/Byron/Parsers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,8 @@ parseTxIdAtto :: Atto.Parser TxId
parseTxIdAtto = (<?> "Transaction ID (hexadecimal)") $ do
bstr <- Atto.takeWhile1 Char.isHexDigit
case deserialiseFromRawBytesHex AsTxId bstr of
Just addr -> return addr
Nothing -> fail $ "Incorrect transaction id format:: " ++ show bstr
Right addr -> return addr
Left msg -> fail $ "Incorrect transaction id format: " ++ msg

parseTxIxAtto :: Atto.Parser TxIx
parseTxIxAtto = toEnum <$> Atto.decimal
Expand Down Expand Up @@ -758,4 +758,3 @@ parseSigningKeyFile opt desc = SigningKeyFile <$> parseFilePath opt desc
parseGenesisFile :: String -> Parser GenesisFile
parseGenesisFile opt =
GenesisFile <$> parseFilePath opt "Genesis JSON file."

4 changes: 4 additions & 0 deletions cardano-cli/src/Cardano/CLI/Helpers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module Cardano.CLI.Helpers
, textShow
, validateCBOR
, hushM
, fmapL
) where

import Cardano.Prelude
Expand Down Expand Up @@ -126,3 +127,6 @@ hushM :: forall e m a. Monad m => Either e a -> (e -> m ()) -> m (Maybe a)
hushM r f = case r of
Right a -> return (Just a)
Left e -> f e >> return Nothing

fmapL :: (a -> b) -> Either a c -> Either b c
fmapL f = either (Left . f) Right
7 changes: 3 additions & 4 deletions cardano-cli/src/Cardano/CLI/Shelley/Key.hs
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,9 @@ deserialiseInput asType acceptedFormats inputBs =
deserialiseHex :: SerialiseAsRawBytes a => DeserialiseInputResult a
deserialiseHex
| isValidHex inputBs =
maybe
(DeserialiseInputError InputInvalidError)
DeserialiseInputSuccess
(deserialiseFromRawBytesHex asType inputBs)
case deserialiseFromRawBytesHex asType inputBs of
Left _ -> DeserialiseInputError InputInvalidError
Right x -> DeserialiseInputSuccess x
| otherwise = DeserialiseInputErrorFormatMismatch

isValidHex :: ByteString -> Bool
Expand Down
Loading

0 comments on commit 794e8e9

Please sign in to comment.