Skip to content

Commit

Permalink
Merge pull request #1005 from IntersectMBO/smelc/transaction-txid-add…
Browse files Browse the repository at this point in the history
…-output-flags

transaction id: add --output-[json,text] flag to control format of the output
  • Loading branch information
smelc authored Jan 10, 2025
2 parents 5c76870 + 12b76f5 commit 8495837
Show file tree
Hide file tree
Showing 18 changed files with 99 additions and 4 deletions.
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
3 changes: 2 additions & 1 deletion cardano-cli/src/Cardano/CLI/EraBased/Commands/Transaction.hs
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 :: !(Maybe OutputFormatJsonOrText)
}
deriving Show

Expand Down
17 changes: 17 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,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"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,3 +379,4 @@ pTransactionId =
fmap TransactionTxIdCmd $
TransactionTxIdCmdArgs
<$> pInputTxOrTxBodyFile
<*> optional pTxIdOutputFormatJsonOrText
12 changes: 11 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,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
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"
}

0 comments on commit 8495837

Please sign in to comment.