Skip to content

Commit 1ee583f

Browse files
committed
Print hash of submitted transaction as JSON
1 parent 3d8aefa commit 1ee583f

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

cardano-cli/src/Cardano/CLI/EraBased/Run/Transaction.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ import Data.Maybe (catMaybes, fromMaybe, mapMaybe)
7575
import Data.Set (Set)
7676
import qualified Data.Set as Set
7777
import qualified Data.Text as Text
78+
import qualified Data.Text.Encoding as T
7879
import qualified Data.Text.IO as Text
7980
import Data.Type.Equality (TestEquality (..))
8081
import GHC.Exts (IsList (..))
@@ -1485,7 +1486,8 @@ runTransactionSubmitCmd
14851486
case res of
14861487
Net.Tx.SubmitSuccess -> do
14871488
liftIO $ Text.putStrLn "Transaction successfully submitted. Transaction hash is:"
1488-
liftIO $ BS.putStrLn $ serialiseToRawBytesHex (getTxId $ getTxBody tx)
1489+
let hashText = T.decodeUtf8 $ serialiseToRawBytesHex (getTxId $ getTxBody tx)
1490+
liftIO $ LBS.putStrLn $ Aeson.encode $ TxIdSubmission hashText
14891491
Net.Tx.SubmitFail reason ->
14901492
case reason of
14911493
TxValidationErrorInCardanoMode err -> left . TxCmdTxSubmitError . Text.pack $ show err

cardano-cli/src/Cardano/CLI/Types/Common.hs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{-# LANGUAGE DataKinds #-}
2+
{-# LANGUAGE DeriveGeneric #-}
23
{-# LANGUAGE DerivingStrategies #-}
34
{-# LANGUAGE GADTs #-}
45
{-# LANGUAGE GeneralisedNewtypeDeriving #-}
@@ -68,6 +69,7 @@ module Cardano.CLI.Types.Common
6869
, TxBuildOutputOptions (..)
6970
, TxByronWitnessCount (..)
7071
, TxFile
72+
, TxIdSubmission (..)
7173
, TxTreasuryDonation (..)
7274
, TxInCount (..)
7375
, TxMempoolQuery (..)
@@ -100,8 +102,10 @@ import Data.Aeson (FromJSON (..), ToJSON (..), object, pairs, (.=))
100102
import qualified Data.Aeson as Aeson
101103
import Data.String (IsString)
102104
import Data.Text (Text)
105+
import qualified Data.Text as T
103106
import qualified Data.Text as Text
104107
import Data.Word (Word64)
108+
import GHC.Generics (Generic)
105109

106110
-- | Determines the direction in which the MIR certificate will transfer ADA.
107111
data TransferDirection
@@ -663,3 +667,12 @@ data PotentiallyCheckedAnchor anchorType anchor
663667
-- ^ Whether to check the hash or not (CheckHash for checking or TrustHash for not checking)
664668
}
665669
deriving (Eq, Show)
670+
671+
-- | Type used for serialization when printing the hash of a transaction
672+
-- after having submitted it.
673+
newtype TxIdSubmission = TxIdSubmission {txhash :: T.Text}
674+
deriving (Show, Generic)
675+
676+
instance FromJSON TxIdSubmission
677+
678+
instance ToJSON TxIdSubmission

0 commit comments

Comments
 (0)