diff --git a/cardano-cli/cardano-cli.cabal b/cardano-cli/cardano-cli.cabal index d70597a13b..609cbc2a7a 100644 --- a/cardano-cli/cardano-cli.cabal +++ b/cardano-cli/cardano-cli.cabal @@ -477,6 +477,7 @@ test-suite cardano-cli-golden Test.Golden.Shelley.Transaction.Build Test.Golden.Shelley.Transaction.CalculateMinFee Test.Golden.Shelley.Transaction.CreateWitness + Test.Golden.Shelley.Transaction.Id Test.Golden.Shelley.Transaction.Sign Test.Golden.TxView Test.Golden.Version diff --git a/cardano-cli/src/Cardano/CLI/EraBased/Commands/Transaction.hs b/cardano-cli/src/Cardano/CLI/EraBased/Commands/Transaction.hs index de11edc37b..3405c6e415 100644 --- a/cardano-cli/src/Cardano/CLI/EraBased/Commands/Transaction.hs +++ b/cardano-cli/src/Cardano/CLI/EraBased/Commands/Transaction.hs @@ -243,8 +243,9 @@ newtype TransactionHashScriptDataCmdArgs = TransactionHashScriptDataCmdArgs } deriving Show -newtype TransactionTxIdCmdArgs = TransactionTxIdCmdArgs +data TransactionTxIdCmdArgs = TransactionTxIdCmdArgs { inputTxBodyOrTxFile :: InputTxBodyOrTxFile + , outputFormat :: !(Maybe OutputFormatJsonOrText) } deriving Show diff --git a/cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs b/cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs index 5e945d52d1..208367fc1d 100644 --- a/cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs +++ b/cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs @@ -1723,6 +1723,23 @@ pOutputFormatJsonOrText kind = , Opt.long ("output-" <> flag_) ] +-- | @pTxIdOutputFormatJsonOrText kind@ is a parser to specify in which format +-- to write @transaction txid@'s output on standard output. +pTxIdOutputFormatJsonOrText :: Parser OutputFormatJsonOrText +pTxIdOutputFormatJsonOrText = + asum + [ make OutputFormatJson "JSON" "json" + , make OutputFormatText "TEXT" "text" + ] + where + default_ = OutputFormatText + make format desc flag_ = + Opt.flag' format $ + mconcat + [ Opt.help $ "Format output as " <> desc <> (if format == default_ then " (the default)." else ".") + , Opt.long ("output-" <> flag_) + ] + pTxViewOutputFormat :: Parser ViewOutputFormat pTxViewOutputFormat = pViewOutputFormat "transaction" diff --git a/cardano-cli/src/Cardano/CLI/EraBased/Options/Transaction.hs b/cardano-cli/src/Cardano/CLI/EraBased/Options/Transaction.hs index 6b4a4d8e74..b8a3025f57 100644 --- a/cardano-cli/src/Cardano/CLI/EraBased/Options/Transaction.hs +++ b/cardano-cli/src/Cardano/CLI/EraBased/Options/Transaction.hs @@ -379,3 +379,4 @@ pTransactionId = fmap TransactionTxIdCmd $ TransactionTxIdCmdArgs <$> pInputTxOrTxBodyFile + <*> optional pTxIdOutputFormatJsonOrText diff --git a/cardano-cli/src/Cardano/CLI/EraBased/Run/Transaction.hs b/cardano-cli/src/Cardano/CLI/EraBased/Run/Transaction.hs index b19c5f25b6..1c3ac5f253 100644 --- a/cardano-cli/src/Cardano/CLI/EraBased/Run/Transaction.hs +++ b/cardano-cli/src/Cardano/CLI/EraBased/Run/Transaction.hs @@ -1732,6 +1732,7 @@ runTransactionTxIdCmd runTransactionTxIdCmd Cmd.TransactionTxIdCmdArgs { inputTxBodyOrTxFile + , outputFormat } = do InAnyShelleyBasedEra _era txbody <- case inputTxBodyOrTxFile of @@ -1746,7 +1747,16 @@ runTransactionTxIdCmd InAnyShelleyBasedEra era tx <- lift (readFileTx txFile) & onLeft (left . TxCmdTextEnvCddlError) return . InAnyShelleyBasedEra era $ getTxBody tx - liftIO $ BS.putStrLn $ serialiseToRawBytesHex (getTxId txbody) + let txId = getTxId txbody + bsToWrite = serialiseToRawBytesHex txId + + liftIO $ + case outputFormat of + Just OutputFormatJson -> LBS.putStrLn $ Aeson.encode $ TxSubmissionResult txId + Just OutputFormatText -> BS.putStrLn bsToWrite + Nothing -> + -- Stay compatible with output when there was no --output-format flag + BS.putStrLn bsToWrite -- ---------------------------------------------------------------------------- -- Witness commands diff --git a/cardano-cli/src/Cardano/CLI/Types/Common.hs b/cardano-cli/src/Cardano/CLI/Types/Common.hs index 1bba0198c4..d23ee97f2b 100644 --- a/cardano-cli/src/Cardano/CLI/Types/Common.hs +++ b/cardano-cli/src/Cardano/CLI/Types/Common.hs @@ -545,7 +545,7 @@ data TxMempoolQuery data OutputFormatJsonOrText = OutputFormatJson | OutputFormatText - deriving Show + deriving (Eq, Show) data ViewOutputFormat = ViewOutputFormatJson @@ -686,7 +686,7 @@ data PotentiallyCheckedAnchor anchorType anchor deriving (Eq, Show) -- | Type used for serialization when printing the hash of a transaction --- after having submitted it. +-- after having submitted it. Also used for printing JSON output of "transaction txid". newtype TxSubmissionResult = TxSubmissionResult {txhash :: TxId} deriving (Show, Generic) diff --git a/cardano-cli/test/cardano-cli-golden/Test/Golden/Shelley/Transaction/Id.hs b/cardano-cli/test/cardano-cli-golden/Test/Golden/Shelley/Transaction/Id.hs new file mode 100644 index 0000000000..7de4d39ad5 --- /dev/null +++ b/cardano-cli/test/cardano-cli-golden/Test/Golden/Shelley/Transaction/Id.hs @@ -0,0 +1,30 @@ +{-# LANGUAGE OverloadedStrings #-} + +module Test.Golden.Shelley.Transaction.Id where + +import Test.Cardano.CLI.Util + +import Hedgehog (Property) +import qualified Hedgehog.Extras.Test.Base as H +import qualified Hedgehog.Extras.Test.Golden as H + +{- HLINT ignore "Use camelCase" -} + +-- Execute this test with: +-- @cabal test cardano-cli-golden --test-options '-p "/golden shelley transaction id/"'@ +hprop_golden_shelley_transaction_id :: Property +hprop_golden_shelley_transaction_id = propertyOnce $ do + txFile <- noteInputFile "test/cardano-cli-golden/files/input/shelley/tx-for-txid.json" + + let baseCmd = ["latest", "transaction", "txid", "--tx-file", txFile] + + output1 <- execCardanoCLI baseCmd + goldenFile1 <- H.note "test/cardano-cli-golden/files/golden/shelley/transaction-id-flagless" + H.diffVsGoldenFile output1 goldenFile1 + + output2 <- execCardanoCLI $ baseCmd ++ ["--output-text"] + H.diffVsGoldenFile output2 goldenFile1 + + output3 <- execCardanoCLI $ baseCmd ++ ["--output-json"] + goldenFile2 <- H.note "test/cardano-cli-golden/files/golden/shelley/transaction-id.json" + H.diffVsGoldenFile output3 goldenFile2 diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/help.cli b/cardano-cli/test/cardano-cli-golden/files/golden/help.cli index 6ea71ec9c0..2be5e35559 100644 --- a/cardano-cli/test/cardano-cli-golden/files/golden/help.cli +++ b/cardano-cli/test/cardano-cli-golden/files/golden/help.cli @@ -1994,6 +1994,7 @@ Usage: cardano-cli shelley transaction txid ( --tx-body-file FILEPATH | --tx-file FILEPATH ) + [--output-json | --output-text] Print a transaction identifier. @@ -3045,6 +3046,7 @@ Usage: cardano-cli allegra transaction txid ( --tx-body-file FILEPATH | --tx-file FILEPATH ) + [--output-json | --output-text] Print a transaction identifier. @@ -4086,6 +4088,7 @@ Usage: cardano-cli mary transaction txid ( --tx-body-file FILEPATH | --tx-file FILEPATH ) + [--output-json | --output-text] Print a transaction identifier. @@ -5142,6 +5145,7 @@ Usage: cardano-cli alonzo transaction txid ( --tx-body-file FILEPATH | --tx-file FILEPATH ) + [--output-json | --output-text] Print a transaction identifier. @@ -6498,6 +6502,7 @@ Usage: cardano-cli babbage transaction txid ( --tx-body-file FILEPATH | --tx-file FILEPATH ) + [--output-json | --output-text] Print a transaction identifier. @@ -8519,6 +8524,7 @@ Usage: cardano-cli conway transaction txid ( --tx-body-file FILEPATH | --tx-file FILEPATH ) + [--output-json | --output-text] Print a transaction identifier. @@ -10540,6 +10546,7 @@ Usage: cardano-cli latest transaction txid ( --tx-body-file FILEPATH | --tx-file FILEPATH ) + [--output-json | --output-text] Print a transaction identifier. diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/help/allegra_transaction_txid.cli b/cardano-cli/test/cardano-cli-golden/files/golden/help/allegra_transaction_txid.cli index 23af8c06f2..c64099a3a1 100644 --- a/cardano-cli/test/cardano-cli-golden/files/golden/help/allegra_transaction_txid.cli +++ b/cardano-cli/test/cardano-cli-golden/files/golden/help/allegra_transaction_txid.cli @@ -2,10 +2,13 @@ Usage: cardano-cli allegra transaction txid ( --tx-body-file FILEPATH | --tx-file FILEPATH ) + [--output-json | --output-text] Print a transaction identifier. Available options: --tx-body-file FILEPATH Input filepath of the JSON TxBody. --tx-file FILEPATH Input filepath of the JSON Tx. + --output-json Format output as JSON. + --output-text Format output as TEXT (the default). -h,--help Show this help text diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/help/alonzo_transaction_txid.cli b/cardano-cli/test/cardano-cli-golden/files/golden/help/alonzo_transaction_txid.cli index 6dab1ac293..e55d5006b2 100644 --- a/cardano-cli/test/cardano-cli-golden/files/golden/help/alonzo_transaction_txid.cli +++ b/cardano-cli/test/cardano-cli-golden/files/golden/help/alonzo_transaction_txid.cli @@ -2,10 +2,13 @@ Usage: cardano-cli alonzo transaction txid ( --tx-body-file FILEPATH | --tx-file FILEPATH ) + [--output-json | --output-text] Print a transaction identifier. Available options: --tx-body-file FILEPATH Input filepath of the JSON TxBody. --tx-file FILEPATH Input filepath of the JSON Tx. + --output-json Format output as JSON. + --output-text Format output as TEXT (the default). -h,--help Show this help text diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/help/babbage_transaction_txid.cli b/cardano-cli/test/cardano-cli-golden/files/golden/help/babbage_transaction_txid.cli index 4e1440b275..d545ef334a 100644 --- a/cardano-cli/test/cardano-cli-golden/files/golden/help/babbage_transaction_txid.cli +++ b/cardano-cli/test/cardano-cli-golden/files/golden/help/babbage_transaction_txid.cli @@ -2,10 +2,13 @@ Usage: cardano-cli babbage transaction txid ( --tx-body-file FILEPATH | --tx-file FILEPATH ) + [--output-json | --output-text] Print a transaction identifier. Available options: --tx-body-file FILEPATH Input filepath of the JSON TxBody. --tx-file FILEPATH Input filepath of the JSON Tx. + --output-json Format output as JSON. + --output-text Format output as TEXT (the default). -h,--help Show this help text diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/help/conway_transaction_txid.cli b/cardano-cli/test/cardano-cli-golden/files/golden/help/conway_transaction_txid.cli index a4247b45b0..9e0b3d1fac 100644 --- a/cardano-cli/test/cardano-cli-golden/files/golden/help/conway_transaction_txid.cli +++ b/cardano-cli/test/cardano-cli-golden/files/golden/help/conway_transaction_txid.cli @@ -2,10 +2,13 @@ Usage: cardano-cli conway transaction txid ( --tx-body-file FILEPATH | --tx-file FILEPATH ) + [--output-json | --output-text] Print a transaction identifier. Available options: --tx-body-file FILEPATH Input filepath of the JSON TxBody. --tx-file FILEPATH Input filepath of the JSON Tx. + --output-json Format output as JSON. + --output-text Format output as TEXT (the default). -h,--help Show this help text diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/help/latest_transaction_txid.cli b/cardano-cli/test/cardano-cli-golden/files/golden/help/latest_transaction_txid.cli index 7ed80315d8..3d928931ef 100644 --- a/cardano-cli/test/cardano-cli-golden/files/golden/help/latest_transaction_txid.cli +++ b/cardano-cli/test/cardano-cli-golden/files/golden/help/latest_transaction_txid.cli @@ -2,10 +2,13 @@ Usage: cardano-cli latest transaction txid ( --tx-body-file FILEPATH | --tx-file FILEPATH ) + [--output-json | --output-text] Print a transaction identifier. Available options: --tx-body-file FILEPATH Input filepath of the JSON TxBody. --tx-file FILEPATH Input filepath of the JSON Tx. + --output-json Format output as JSON. + --output-text Format output as TEXT (the default). -h,--help Show this help text diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/help/mary_transaction_txid.cli b/cardano-cli/test/cardano-cli-golden/files/golden/help/mary_transaction_txid.cli index 70ec68a271..412461b2c1 100644 --- a/cardano-cli/test/cardano-cli-golden/files/golden/help/mary_transaction_txid.cli +++ b/cardano-cli/test/cardano-cli-golden/files/golden/help/mary_transaction_txid.cli @@ -2,10 +2,13 @@ Usage: cardano-cli mary transaction txid ( --tx-body-file FILEPATH | --tx-file FILEPATH ) + [--output-json | --output-text] Print a transaction identifier. Available options: --tx-body-file FILEPATH Input filepath of the JSON TxBody. --tx-file FILEPATH Input filepath of the JSON Tx. + --output-json Format output as JSON. + --output-text Format output as TEXT (the default). -h,--help Show this help text diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/help/shelley_transaction_txid.cli b/cardano-cli/test/cardano-cli-golden/files/golden/help/shelley_transaction_txid.cli index 78e17f7991..bc37e52f09 100644 --- a/cardano-cli/test/cardano-cli-golden/files/golden/help/shelley_transaction_txid.cli +++ b/cardano-cli/test/cardano-cli-golden/files/golden/help/shelley_transaction_txid.cli @@ -2,10 +2,13 @@ Usage: cardano-cli shelley transaction txid ( --tx-body-file FILEPATH | --tx-file FILEPATH ) + [--output-json | --output-text] Print a transaction identifier. Available options: --tx-body-file FILEPATH Input filepath of the JSON TxBody. --tx-file FILEPATH Input filepath of the JSON Tx. + --output-json Format output as JSON. + --output-text Format output as TEXT (the default). -h,--help Show this help text diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/shelley/transaction-id-flagless b/cardano-cli/test/cardano-cli-golden/files/golden/shelley/transaction-id-flagless new file mode 100644 index 0000000000..aef6d681db --- /dev/null +++ b/cardano-cli/test/cardano-cli-golden/files/golden/shelley/transaction-id-flagless @@ -0,0 +1 @@ +345b88a38821ce10b1a19c41cbc9b48a55c26001cee4a7d2ad83fd9ddb157667 diff --git a/cardano-cli/test/cardano-cli-golden/files/golden/shelley/transaction-id.json b/cardano-cli/test/cardano-cli-golden/files/golden/shelley/transaction-id.json new file mode 100644 index 0000000000..f0f491c300 --- /dev/null +++ b/cardano-cli/test/cardano-cli-golden/files/golden/shelley/transaction-id.json @@ -0,0 +1 @@ +{"txhash":"345b88a38821ce10b1a19c41cbc9b48a55c26001cee4a7d2ad83fd9ddb157667"} diff --git a/cardano-cli/test/cardano-cli-golden/files/input/shelley/tx-for-txid.json b/cardano-cli/test/cardano-cli-golden/files/input/shelley/tx-for-txid.json new file mode 100644 index 0000000000..3a1c0f6173 --- /dev/null +++ b/cardano-cli/test/cardano-cli-golden/files/input/shelley/tx-for-txid.json @@ -0,0 +1,5 @@ +{ + "type": "Unwitnessed Tx MaryEra", + "description": "Ledger Cddl Format", + "cborHex": "83a300818258202392d2b1200b5139fe555c81261697b29a8ccf561c5c783d46e78a479d977053000181825839016b837ca50316ee4e00033482ed128887d72c2bae5b0438d692dc1251b0c8b17595ebdb93c1f974be0a9b1ef26c474649d9c2ae766ed135cf1864020ca0f6" +}