Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transaction id: add --output-[json,text] flag to control format of the output #1005

Merged
merged 5 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions cardano-cli/cardano-cli.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,9 @@ newtype TransactionHashScriptDataCmdArgs = TransactionHashScriptDataCmdArgs
}
deriving Show

newtype TransactionTxIdCmdArgs = TransactionTxIdCmdArgs
data TransactionTxIdCmdArgs = TransactionTxIdCmdArgs
{ inputTxBodyOrTxFile :: InputTxBodyOrTxFile
, outputFormat :: !OutputFormatJsonOrText
}
deriving Show

Expand Down
15 changes: 15 additions & 0 deletions cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1723,6 +1723,21 @@ 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"]
<|> pure default_
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"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,3 +379,4 @@ pTransactionId =
fmap TransactionTxIdCmd $
TransactionTxIdCmdArgs
<$> pInputTxOrTxBodyFile
<*> pTxIdOutputFormatJsonOrText
8 changes: 7 additions & 1 deletion cardano-cli/src/Cardano/CLI/EraBased/Run/Transaction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1732,6 +1732,7 @@ runTransactionTxIdCmd
runTransactionTxIdCmd
Cmd.TransactionTxIdCmdArgs
{ inputTxBodyOrTxFile
, outputFormat
} = do
InAnyShelleyBasedEra _era txbody <-
case inputTxBodyOrTxFile of
Expand All @@ -1746,7 +1747,12 @@ 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

liftIO $
case outputFormat of
OutputFormatJson -> LBS.putStrLn $ Aeson.encode $ TxSubmissionResult txId
OutputFormatText -> BS.putStrLn $ serialiseToRawBytesHex txId

-- ----------------------------------------------------------------------------
-- Witness commands
Expand Down
4 changes: 2 additions & 2 deletions cardano-cli/src/Cardano/CLI/Types/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ data TxMempoolQuery
data OutputFormatJsonOrText
= OutputFormatJson
| OutputFormatText
deriving Show
deriving (Eq, Show)

data ViewOutputFormat
= ViewOutputFormatJson
Expand Down Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions cardano-cli/test/cardano-cli-golden/files/golden/help.cli
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
345b88a38821ce10b1a19c41cbc9b48a55c26001cee4a7d2ad83fd9ddb157667
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"txhash":"345b88a38821ce10b1a19c41cbc9b48a55c26001cee4a7d2ad83fd9ddb157667"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "Unwitnessed Tx MaryEra",
"description": "Ledger Cddl Format",
"cborHex": "83a300818258202392d2b1200b5139fe555c81261697b29a8ccf561c5c783d46e78a479d977053000181825839016b837ca50316ee4e00033482ed128887d72c2bae5b0438d692dc1251b0c8b17595ebdb93c1f974be0a9b1ef26c474649d9c2ae766ed135cf1864020ca0f6"
}
Loading