Skip to content

Commit

Permalink
Add the option of emitting amino encoded json from the CLI (#7221)
Browse files Browse the repository at this point in the history
* Add the option of emitting amino encoded json

* Update AMINO JSON serialization with ConvertTxToStdTx

* Make the Amino flag more self documenting by serializing the BroadcastRequest type instead of StdTx

* Handle amino encoding error

* Update x/auth/client/cli/tx_multisign.go

Co-authored-by: Alessio Treglia <alessio@tendermint.com>

* Update x/auth/client/cli/tx_sign.go

Co-authored-by: Alessio Treglia <alessio@tendermint.com>

* Apply suggestions from code review

Co-authored-by: Alessio Treglia <alessio@tendermint.com>

* Fix go format

Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
Co-authored-by: Alessio Treglia <alessio@tendermint.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
4 people authored and clevinson committed Oct 19, 2020
1 parent 1b75a6d commit be81979
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
27 changes: 24 additions & 3 deletions x/auth/client/cli/tx_multisign.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
signingtypes "github.com/cosmos/cosmos-sdk/types/tx/signing"
"github.com/cosmos/cosmos-sdk/version"
authclient "github.com/cosmos/cosmos-sdk/x/auth/client"
"github.com/cosmos/cosmos-sdk/x/auth/client/rest"
"github.com/cosmos/cosmos-sdk/x/auth/signing"
)

Expand Down Expand Up @@ -52,6 +53,7 @@ recommended to set such parameters manually.

cmd.Flags().Bool(flagSigOnly, false, "Print only the generated signature, then exit")
cmd.Flags().String(flags.FlagOutputDocument, "", "The document will be written to the given file instead of STDOUT")
cmd.Flags().Bool(flagAmino, false, "Generate Amino encoded JSON suitable for submiting to the txs REST endpoint")
flags.AddTxFlagsToCmd(cmd)
cmd.Flags().String(flags.FlagChainID, "", "network chain ID")

Expand Down Expand Up @@ -147,9 +149,28 @@ func makeMultiSignCmd() func(cmd *cobra.Command, args []string) error {

sigOnly, _ := cmd.Flags().GetBool(flagSigOnly)

json, err := marshalSignatureJSON(txCfg, txBuilder, sigOnly)
if err != nil {
return err
aminoJSON, _ := cmd.Flags().GetBool(flagAmino)

var json []byte

if aminoJSON {
stdTx, err := tx.ConvertTxToStdTx(clientCtx.LegacyAmino, txBuilder.GetTx())
if err != nil {
return err
}

req := rest.BroadcastReq{
Tx: stdTx,
Mode: "block|sync|async",
}

json, _ = clientCtx.LegacyAmino.MarshalJSON(req)

} else {
json, err = marshalSignatureJSON(txCfg, txBuilder, sigOnly)
if err != nil {
return err
}
}

outputDoc, _ := cmd.Flags().GetString(flags.FlagOutputDocument)
Expand Down
30 changes: 29 additions & 1 deletion x/auth/client/cli/tx_sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
authclient "github.com/cosmos/cosmos-sdk/x/auth/client"
"github.com/cosmos/cosmos-sdk/x/auth/client/rest"
)

const (
flagMultisig = "multisig"
flagAppend = "append"
flagSigOnly = "signature-only"
flagAmino = "amino"
)

// GetSignBatchCommand returns the transaction sign-batch command.
Expand Down Expand Up @@ -187,6 +189,7 @@ be generated via the 'multisign' command.
cmd.Flags().Bool(flagSigOnly, false, "Print only the generated signature, then exit")
cmd.Flags().String(flags.FlagOutputDocument, "", "The document will be written to the given file instead of STDOUT")
cmd.Flags().String(flags.FlagChainID, "", "The network chain ID")
cmd.Flags().Bool(flagAmino, false, "Generate Amino encoded JSON suitable for submiting to the txs REST endpoint")
cmd.MarkFlagRequired(flags.FlagFrom)
flags.AddTxFlagsToCmd(cmd)

Expand Down Expand Up @@ -257,11 +260,36 @@ func makeSignCmd() func(cmd *cobra.Command, args []string) error {
return err
}

json, err := marshalSignatureJSON(txCfg, txBuilder, generateSignatureOnly)
aminoJSON, _ := cmd.Flags().GetBool(flagAmino)

if err != nil {
return err
}

var json []byte

if aminoJSON {
stdTx, err := tx.ConvertTxToStdTx(clientCtx.LegacyAmino, txBuilder.GetTx())
if err != nil {
return err
}

req := rest.BroadcastReq{
Tx: stdTx,
Mode: "block|sync|async",
}

json, err = clientCtx.LegacyAmino.MarshalJSON(req)
if err != nil {
return err
}
} else {
json, err = marshalSignatureJSON(txCfg, txBuilder, generateSignatureOnly)
if err != nil {
return err
}
}

outputDoc, _ := cmd.Flags().GetString(flags.FlagOutputDocument)
if outputDoc == "" {
cmd.Printf("%s\n", json)
Expand Down

0 comments on commit be81979

Please sign in to comment.