Skip to content

Commit

Permalink
New transaction echo command
Browse files Browse the repository at this point in the history
  • Loading branch information
newhoggy committed Apr 9, 2024
1 parent 210a5f1 commit 0accede
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
8 changes: 8 additions & 0 deletions cardano-cli/src/Cardano/CLI/EraBased/Commands/Transaction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module Cardano.CLI.EraBased.Commands.Transaction
( TransactionCmds (..)
, TransactionBuildRawCmdArgs(..)
, TransactionBuildCmdArgs(..)
, TransactionEchoCmdArgs(..)
, TransactionSignCmdArgs(..)
, TransactionWitnessCmdArgs(..)
, TransactionSignWitnessCmdArgs(..)
Expand All @@ -31,6 +32,7 @@ import Data.Text (Text)
data TransactionCmds era
= TransactionBuildRawCmd !(TransactionBuildRawCmdArgs era)
| TransactionBuildCmd !(TransactionBuildCmdArgs era)
| TransactionEchoCmd !TransactionEchoCmdArgs
| TransactionSignCmd !TransactionSignCmdArgs
| TransactionWitnessCmd !TransactionWitnessCmdArgs
| TransactionSignWitnessCmd !TransactionSignWitnessCmdArgs
Expand Down Expand Up @@ -127,6 +129,11 @@ data TransactionBuildCmdArgs era = TransactionBuildCmdArgs
, buildOutputOptions :: !TxBuildOutputOptions
} deriving Show

data TransactionEchoCmdArgs = TransactionEchoCmdArgs
{ txOrTxBodyFile :: !InputTxBodyOrTxFile
, outTxFile :: !(TxFile Out)
} deriving Show

data TransactionSignCmdArgs = TransactionSignCmdArgs
{ txOrTxBodyFile :: !InputTxBodyOrTxFile
, witnessSigningData :: ![WitnessSigningData]
Expand Down Expand Up @@ -191,6 +198,7 @@ renderTransactionCmds :: TransactionCmds era -> Text
renderTransactionCmds = \case
TransactionBuildCmd {} -> "transaction build"
TransactionBuildRawCmd {} -> "transaction build-raw"
TransactionEchoCmd {} -> "transaction echo"
TransactionSignCmd {} -> "transaction sign"
TransactionWitnessCmd {} -> "transaction witness"
TransactionSignWitnessCmd {} -> "transaction sign-witness"
Expand Down
13 changes: 12 additions & 1 deletion cardano-cli/src/Cardano/CLI/EraBased/Options/Transaction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ pTransactionCmds era envCli =
$ subParser "sign"
$ Opt.info (pTransactionSign envCli)
$ Opt.progDesc "Sign a transaction"
, Just
$ subParser "echo"
$ Opt.info pTransactionEcho
$ Opt.progDesc "Echo a transaction"
, Just
$ subParser "witness"
$ Opt.info (pTransactionCreateWitness envCli)
Expand Down Expand Up @@ -219,7 +223,7 @@ pTransactionBuildRaw era =
<*> pProposalFiles era ManualBalance
<*> pTxBodyFileOut

pTransactionSign :: EnvCli -> Parser (TransactionCmds era)
pTransactionSign :: EnvCli -> Parser (TransactionCmds era)
pTransactionSign envCli =
fmap TransactionSignCmd $
TransactionSignCmdArgs
Expand All @@ -228,6 +232,13 @@ pTransactionSign envCli =
<*> optional (pNetworkId envCli)
<*> pTxFileOut

pTransactionEcho :: Parser (TransactionCmds era)
pTransactionEcho =
fmap TransactionEchoCmd $
TransactionEchoCmdArgs
<$> pInputTxOrTxBodyFile
<*> pTxFileOut

pTransactionCreateWitness :: EnvCli -> Parser (TransactionCmds era)
pTransactionCreateWitness envCli =
fmap TransactionWitnessCmd $
Expand Down
47 changes: 47 additions & 0 deletions cardano-cli/src/Cardano/CLI/EraBased/Run/Transaction.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module Cardano.CLI.EraBased.Run.Transaction
( runTransactionCmds
, runTransactionBuildCmd
, runTransactionBuildRawCmd
, runTransactionEchoCmd
, runTransactionSignCmd
, runTransactionSubmitCmd
, runTransactionCalculateMinFeeCmd
Expand Down Expand Up @@ -79,6 +80,7 @@ runTransactionCmds :: Cmd.TransactionCmds era -> ExceptT TxCmdError IO ()
runTransactionCmds = \case
Cmd.TransactionBuildCmd args -> runTransactionBuildCmd args
Cmd.TransactionBuildRawCmd args -> runTransactionBuildRawCmd args
Cmd.TransactionEchoCmd args -> runTransactionEchoCmd args
Cmd.TransactionSignCmd args -> runTransactionSignCmd args
Cmd.TransactionSubmitCmd args -> runTransactionSubmitCmd args
Cmd.TransactionCalculateMinFeeCmd args -> runTransactionCalculateMinFeeCmd args
Expand Down Expand Up @@ -974,6 +976,51 @@ runTransactionSignCmd
$ shelleyBasedEraConstraints sbe
$ textEnvelopeToJSON Nothing tx

-- ----------------------------------------------------------------------------
-- Transaction echoing
--

runTransactionEchoCmd :: ()
=> Cmd.TransactionEchoCmdArgs
-> ExceptT TxCmdError IO ()
runTransactionEchoCmd
Cmd.TransactionEchoCmdArgs
{ txOrTxBodyFile = txOrTxBody
, outTxFile = outTxFile
} = do

case txOrTxBody of
InputTxFile (File inputTxFilePath) -> do
inputTxFile <- liftIO $ fileOrPipe inputTxFilePath
anyTx <- lift (readFileTx inputTxFile) & onLeft (left . TxCmdCddlError)

InAnyShelleyBasedEra sbe tx <- pure anyTx

lift (writeTxFileTextEnvelopeCddl sbe outTxFile tx)
& onLeft (left . TxCmdWriteFileError)

InputTxBodyFile (File txbodyFilePath) -> do
txbodyFile <- liftIO $ fileOrPipe txbodyFilePath
unwitnessed <- firstExceptT TxCmdCddlError . newExceptT
$ readFileTxBody txbodyFile

case unwitnessed of
IncompleteCddlFormattedTx anyTx -> do
InAnyShelleyBasedEra sbe unwitTx <- pure anyTx

lift (writeTxFileTextEnvelopeCddl sbe outTxFile unwitTx)
& onLeft (left . TxCmdWriteFileError)

UnwitnessedCliFormattedTxBody anyTxbody -> do
InAnyShelleyBasedEra sbe txbody <- pure anyTxbody

let tx = makeSignedTransaction [] txbody

firstExceptT TxCmdWriteFileError . newExceptT
$ writeLazyByteStringFile outTxFile
$ shelleyBasedEraConstraints sbe
$ textEnvelopeToJSON Nothing tx

-- ----------------------------------------------------------------------------
-- Transaction submission
--
Expand Down

0 comments on commit 0accede

Please sign in to comment.