Skip to content

Commit

Permalink
cardano-cli: Expect AssetName in hex in --mint and --tx-out
Browse files Browse the repository at this point in the history
  • Loading branch information
cblp committed Sep 22, 2021
1 parent dc7145a commit 748de22
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 31 deletions.
1 change: 1 addition & 0 deletions cardano-cli/cardano-cli.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ test-suite cardano-cli-golden
type: exitcode-stdio-1.0

build-depends: aeson >= 1.5.6.0
, base16-bytestring
, bytestring
, cardano-api
, cardano-cli
Expand Down
19 changes: 13 additions & 6 deletions cardano-cli/src/Cardano/CLI/Mary/ValueParser.hs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
{-# LANGUAGE LambdaCase #-}

module Cardano.CLI.Mary.ValueParser
( parseValue
) where

import Prelude

import Control.Applicative (some, (<|>))
import qualified Data.ByteString.Char8 as BSC
import qualified Data.Char as Char
import Data.Functor (void, ($>))
import Data.List (foldl')
import qualified Data.Text as Text
import qualified Data.Text.Encoding as Text
import Data.Word (Word64)

import Control.Applicative (some, (<|>))

import Text.Parsec as Parsec (notFollowedBy, try, (<?>))
import Text.Parsec.Char (alphaNum, char, digit, hexDigit, space, spaces, string)
import Text.Parsec.Expr (Assoc (..), Operator (..), buildExpressionParser)
Expand Down Expand Up @@ -111,10 +113,15 @@ decimal = do

-- | Asset name parser.
assetName :: Parser AssetName
assetName =
toAssetName <$> some alphaNum
where
toAssetName = AssetName . Text.encodeUtf8 . Text.pack
assetName = do
hexText <- some hexDigit
note "AssetName deserisalisation failed" $
deserialiseFromRawBytesHex AsAssetName $ BSC.pack hexText

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

-- | Policy ID parser.
policyId :: Parser PolicyId
Expand Down
28 changes: 12 additions & 16 deletions cardano-cli/src/Cardano/CLI/Run/Friendly.hs
Original file line number Diff line number Diff line change
Expand Up @@ -149,25 +149,21 @@ friendlyFee = \case
friendlyLovelace :: Lovelace -> Aeson.Value
friendlyLovelace (Lovelace value) = String $ textShow value <> " Lovelace"

{-
current output:
52dc3d43b6d2465e96109ce75ab61abe5e9c1d8a3c9ce6ff8a3af528:
736b79: 142
TODO:
policy 52dc3d43b6d2465e96109ce75ab61abe5e9c1d8a3c9ce6ff8a3af528:
asset 736b79 (sky): 142
-}
friendlyMintValue :: TxMintValue ViewTx era -> Aeson.Value
friendlyMintValue = \case
TxMintNone -> Null
TxMintValue _ v _ ->
object
[ friendlyAssetId assetId .= quantity
| (assetId, quantity) <- valueToList v
]

friendlyAssetId :: AssetId -> Text
friendlyAssetId = \case
AdaAssetId -> "ADA"
AssetId policyId (AssetName assetName) ->
decodeUtf8 $ serialiseToRawBytesHex policyId <> suffix
where
suffix =
case assetName of
"" -> ""
_ -> "." <> assetName
TxMintValue _ v _ -> toJSON v

friendlyTxOutValue :: TxOutValue era -> Aeson.Value
friendlyTxOutValue = \case
Expand Down
8 changes: 5 additions & 3 deletions cardano-cli/test/Test/Golden/Shelley/Transaction/Build.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ module Test.Golden.Shelley.Transaction.Build
import Cardano.Prelude
import Prelude (String)

import qualified Data.ByteString.Base16 as Base16
import qualified Data.ByteString.Char8 as BSC
import Hedgehog (Property)
import Test.OptParse

Expand Down Expand Up @@ -76,7 +78,9 @@ golden_shelleyTransactionBuild_Minting =
, scriptWit
]

let dummyMA = filter (/= '\n') $ "50 " ++ polid ++ ".ethereum"
let dummyMA =
filter (/= '\n') $
"50 " ++ polid ++ "." ++ BSC.unpack (Base16.encode "ethereum")

txBodyOutFile <- noteTempFile tempDir "tx-body-out"

Expand Down Expand Up @@ -134,5 +138,3 @@ golden_shelleyTransactionBuild_TxInScriptWitnessed =
H.assertFileOccurences 1 "TxBodyMary" txBodyOutFile

H.assertEndsWithSingleNewline txBodyOutFile


16 changes: 13 additions & 3 deletions cardano-cli/test/Test/Golden/TxView.hs
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,21 @@ golden_view_mary =
, "--fee", "139"
, "--invalid-before", "140"
, "--mint"
, "42 d441227553a0f1a965fee7d60a0f724b368dd1bddbc208730fccebcf\
, "130 d441227553a0f1a965fee7d60a0f724b368dd1bddbc208730fccebcf\
\ + \
\43 52dc3d43b6d2465e96109ce75ab61abe5e9c1d8a3c9ce6ff8a3af528.snow\
\132 52dc3d43b6d2465e96109ce75ab61abe5e9c1d8a3c9ce6ff8a3af528.cafe\
\ + \
\44 d441227553a0f1a965fee7d60a0f724b368dd1bddbc208730fccebcf.sky"
\134 d441227553a0f1a965fee7d60a0f724b368dd1bddbc208730fccebcf.f00d\
\ + \
\136 52dc3d43b6d2465e96109ce75ab61abe5e9c1d8a3c9ce6ff8a3af528.dead\
\ + \
\138\
\ d441227553a0f1a965fee7d60a0f724b368dd1bddbc208730fccebcf\
\.736e6f77\
\ + \
\142\
\ 52dc3d43b6d2465e96109ce75ab61abe5e9c1d8a3c9ce6ff8a3af528\
\.736b79"
, "--minting-script-file", "test/data/golden/mary/scripts/mint.all"
, "--minting-script-file", "test/data/golden/mary/scripts/mint.any"
, "--out-file", transactionBodyFile
Expand Down
11 changes: 8 additions & 3 deletions cardano-cli/test/data/golden/mary/transaction-view.out
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ inputs:
- fe5dd07fb576bff960d6e066eade5b26cdb5afebe29f76ea58d0a098bce5d891#135
metadata: null
mint:
52dc3d43b6d2465e96109ce75ab61abe5e9c1d8a3c9ce6ff8a3af528.snow: 43
d441227553a0f1a965fee7d60a0f724b368dd1bddbc208730fccebcf: 42
d441227553a0f1a965fee7d60a0f724b368dd1bddbc208730fccebcf.sky: 44
52dc3d43b6d2465e96109ce75ab61abe5e9c1d8a3c9ce6ff8a3af528:
736b79: 142
cafe: 132
dead: 136
d441227553a0f1a965fee7d60a0f724b368dd1bddbc208730fccebcf:
'': 130
736e6f77: 138
f00d: 134
outputs:
- address: addr_test1qrefnr4k09pvge6dq83v6s67ruter8sftmky8qrmkqqsxy7q5psgn8tgqmupq4r79jmxlyk4eqt6z6hj5g8jd8393msqaw47f4
address era: Shelley
Expand Down

0 comments on commit 748de22

Please sign in to comment.