cosmos-sdk/x/auth/client/cli/decode.go

49 lines
1.1 KiB
Go
Raw Normal View History

package cli
import (
"encoding/base64"
2019-11-11 06:54:32 -08:00
"encoding/hex"
"github.com/spf13/cobra"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
)
const flagHex = "hex"
// GetDecodeCommand returns the decode command to take serialized bytes and turn
// it into a JSON-encoded transaction.
func GetDecodeCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "decode [amino-byte-string]",
Short: "Decode an binary encoded transaction string.",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) (err error) {
clientCtx := client.GetClientContextFromCmd(cmd)
var txBytes []byte
if useHex, _ := cmd.Flags().GetBool(flagHex); useHex {
txBytes, err = hex.DecodeString(args[0])
} else {
txBytes, err = base64.StdEncoding.DecodeString(args[0])
}
if err != nil {
return err
}
tx, err := clientCtx.TxConfig.TxDecoder()(txBytes)
if err != nil {
return err
}
return clientCtx.PrintOutput(tx)
},
}
cmd.Flags().BoolP(flagHex, "x", false, "Treat input as hexadecimal instead of base64")
flags.AddTxFlagsToCmd(cmd)
return cmd
}