Merge PR #5427: Remove code duplication in x/auth/client/cli

This commit is contained in:
Alessio Treglia 2019-12-27 16:23:18 +01:00 committed by Alexander Bezobchuk
parent b314b851e4
commit 3c82b66347
3 changed files with 29 additions and 68 deletions

View File

@ -90,6 +90,7 @@ if the provided arguments are invalid.
increased significantly due to modular `AnteHandler` support. Increase GasLimit accordingly. increased significantly due to modular `AnteHandler` support. Increase GasLimit accordingly.
* (rest) [\#5336](https://github.com/cosmos/cosmos-sdk/issues/5336) `MsgEditValidator` uses `description` instead of `Description` as a JSON key. * (rest) [\#5336](https://github.com/cosmos/cosmos-sdk/issues/5336) `MsgEditValidator` uses `description` instead of `Description` as a JSON key.
* (keys) [\#5097](https://github.com/cosmos/cosmos-sdk/pull/5097) Due to the keybase -> keyring transition, keys need to be migrated. See `keys migrate` command for more info. * (keys) [\#5097](https://github.com/cosmos/cosmos-sdk/pull/5097) Due to the keybase -> keyring transition, keys need to be migrated. See `keys migrate` command for more info.
* (x/auth) [\#5424](https://github.com/cosmos/cosmos-sdk/issues/5424) Drop `decode-tx` command from x/auth/client/cli, duplicate of the `decode` command.
### Features ### Features

View File

@ -84,6 +84,8 @@ func GetCommands(cmds ...*cobra.Command) []*cobra.Command {
viper.BindPFlag(FlagNode, c.Flags().Lookup(FlagNode)) viper.BindPFlag(FlagNode, c.Flags().Lookup(FlagNode))
c.MarkFlagRequired(FlagChainID) c.MarkFlagRequired(FlagChainID)
c.SetErr(c.ErrOrStderr())
} }
return cmds return cmds
} }
@ -119,6 +121,8 @@ func PostCommands(cmds ...*cobra.Command) []*cobra.Command {
viper.BindPFlag(FlagKeyringBackend, c.Flags().Lookup(FlagKeyringBackend)) viper.BindPFlag(FlagKeyringBackend, c.Flags().Lookup(FlagKeyringBackend))
c.MarkFlagRequired(FlagChainID) c.MarkFlagRequired(FlagChainID)
c.SetErr(c.ErrOrStderr())
} }
return cmds return cmds
} }

View File

@ -1,98 +1,54 @@
package cli package cli
import ( import (
"bytes"
"encoding/base64" "encoding/base64"
"encoding/hex" "encoding/hex"
"encoding/json"
"fmt"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/tendermint/go-amino" "github.com/tendermint/go-amino"
"github.com/cosmos/cosmos-sdk/client/context" "github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/version"
"github.com/cosmos/cosmos-sdk/x/auth/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
) )
const flagHex = "hex"
// GetDecodeCommand returns the decode command to take Amino-serialized bytes // GetDecodeCommand returns the decode command to take Amino-serialized bytes
// and turn it into a JSONified transaction. // and turn it into a JSONified transaction.
func GetDecodeCommand(codec *amino.Codec) *cobra.Command { func GetDecodeCommand(codec *amino.Codec) *cobra.Command {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "decode [amino-byte-string]", Use: "decode [amino-byte-string]",
Short: "Decode an amino-encoded transaction string", Short: "Decode an amino-encoded transaction string.",
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) (err error) { RunE: runDecodeTxString(codec),
cliCtx := context.NewCLIContext().WithCodec(codec)
txBytes, err := base64.StdEncoding.DecodeString(args[0])
if err != nil {
return err
}
var stdTx authtypes.StdTx
err = cliCtx.Codec.UnmarshalBinaryLengthPrefixed(txBytes, &stdTx)
if err != nil {
return err
}
return cliCtx.PrintOutput(stdTx)
},
} }
cmd.Flags().BoolP(flagHex, "x", false, "Treat input as hexadecimal instead of base64")
return flags.PostCommands(cmd)[0] return flags.PostCommands(cmd)[0]
} }
// GetDecodeTxCmd - returns the command to decode a tx from hex or base64 func runDecodeTxString(codec *amino.Codec) func(cmd *cobra.Command, args []string) (err error) {
func GetDecodeTxCmd(cdc *codec.Codec) *cobra.Command { return func(cmd *cobra.Command, args []string) (err error) {
return &cobra.Command{ cliCtx := context.NewCLIContext().WithCodec(codec).WithOutput(cmd.OutOrStdout())
Use: "decode-tx [tx]", var txBytes []byte
Short: "Decode a tx from hex or base64",
Long: fmt.Sprintf(`Decode a tx from hex, base64.
Example:
$ %s tx decode-tx TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz
`, version.ClientName),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
txString := args[0] if viper.GetBool(flagHex) {
txBytes, err = hex.DecodeString(args[0])
} else {
txBytes, err = base64.StdEncoding.DecodeString(args[0])
}
if err != nil {
return err
}
// try hex, then base64 var stdTx authtypes.StdTx
txBytes, err := hex.DecodeString(txString) err = cliCtx.Codec.UnmarshalBinaryLengthPrefixed(txBytes, &stdTx)
if err != nil { if err != nil {
var err2 error return err
txBytes, err2 = base64.StdEncoding.DecodeString(txString) }
if err2 != nil {
return fmt.Errorf(`expected hex or base64. Got errors:
hex: %v,
base64: %v
`, err, err2)
}
}
var tx = types.StdTx{} return cliCtx.PrintOutput(stdTx)
err = cdc.UnmarshalBinaryLengthPrefixed(txBytes, &tx)
if err != nil {
return err
}
bz, err := cdc.MarshalJSON(tx)
if err != nil {
return err
}
buf := bytes.NewBuffer([]byte{})
if err = json.Indent(buf, bz, "", " "); err != nil {
return err
}
fmt.Println(buf.String())
return nil
},
} }
} }