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..30bfce3978 100644 --- a/cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs +++ b/cardano-cli/src/Cardano/CLI/EraBased/Options/Common.hs @@ -1723,6 +1723,22 @@ 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" False + , make OutputFormatText "TEXT" "text" True + ] + where + make format desc flag_ default_ = + Opt.flag' format $ + mconcat + [ Opt.help $ "Format output to " <> desc <> (if 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..467c202dd8 100644 --- a/cardano-cli/src/Cardano/CLI/Types/Common.hs +++ b/cardano-cli/src/Cardano/CLI/Types/Common.hs @@ -686,10 +686,10 @@ 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) instance FromJSON TxSubmissionResult -instance ToJSON TxSubmissionResult +instance ToJSON TxSubmissionResult \ No newline at end of file