Skip to content

Commit

Permalink
transaction id: add --output-json and --output-text flags
Browse files Browse the repository at this point in the history
  • Loading branch information
smelc committed Jan 7, 2025
1 parent 24d817e commit b66d5ff
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 4 deletions.
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
16 changes: 16 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,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"

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 @@ -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

0 comments on commit b66d5ff

Please sign in to comment.