cosmos-sdk/x/auth/client/cli/decode.go
SaReN 39f53ac22f
client: rename CliContext to Context (#6290)
* Refactor CliContext as Context

* Fix lint issues

* Fix goimports

* Fix gov tests

* Resolved ci-lint issues

* Add changelog

* Rename cliCtx to clientCtx

* Fix mocks and routes

* Add changelog

* Update changelog

* Apply suggestions from code review

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

* merge client/rpc/ro{ot,utes}.go

* Update docs

* client/rpc: remove redundant client/rpc.RegisterRPCRoutes

* regenerate mocks

* Update ADRs

Co-authored-by: Alessio Treglia <alessio@tendermint.com>
Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
2020-06-01 12:46:03 +00:00

55 lines
1.4 KiB
Go

package cli
import (
"encoding/base64"
"encoding/hex"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
)
const flagHex = "hex"
// GetDecodeCommand returns the decode command to take Amino-serialized bytes
// and turn it into a JSONified transaction.
func GetDecodeCommand(codec *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "decode [amino-byte-string]",
Short: "Decode an amino-encoded transaction string.",
Args: cobra.ExactArgs(1),
RunE: runDecodeTxString(codec),
}
cmd.Flags().BoolP(flagHex, "x", false, "Treat input as hexadecimal instead of base64")
return flags.PostCommands(cmd)[0]
}
func runDecodeTxString(codec *codec.Codec) func(cmd *cobra.Command, args []string) (err error) {
return func(cmd *cobra.Command, args []string) (err error) {
clientCtx := client.NewContext().WithCodec(codec).WithOutput(cmd.OutOrStdout())
var txBytes []byte
if viper.GetBool(flagHex) {
txBytes, err = hex.DecodeString(args[0])
} else {
txBytes, err = base64.StdEncoding.DecodeString(args[0])
}
if err != nil {
return err
}
var stdTx authtypes.StdTx
err = clientCtx.Codec.UnmarshalBinaryBare(txBytes, &stdTx)
if err != nil {
return err
}
return clientCtx.PrintOutput(stdTx)
}
}