github.com/cosmos/cosmos-sdk@v0.50.10/x/auth/client/cli/decode.go (about) 1 package cli 2 3 import ( 4 "encoding/base64" 5 "encoding/hex" 6 7 "github.com/spf13/cobra" 8 9 "github.com/cosmos/cosmos-sdk/client" 10 "github.com/cosmos/cosmos-sdk/client/flags" 11 ) 12 13 const flagHex = "hex" 14 15 // GetDecodeCommand returns the decode command to take serialized bytes and turn 16 // it into a JSON-encoded transaction. 17 func GetDecodeCommand() *cobra.Command { 18 cmd := &cobra.Command{ 19 Use: "decode [protobuf-byte-string]", 20 Short: "Decode a binary encoded transaction string", 21 Args: cobra.ExactArgs(1), 22 RunE: func(cmd *cobra.Command, args []string) (err error) { 23 clientCtx := client.GetClientContextFromCmd(cmd) 24 var txBytes []byte 25 26 if useHex, _ := cmd.Flags().GetBool(flagHex); useHex { 27 txBytes, err = hex.DecodeString(args[0]) 28 } else { 29 txBytes, err = base64.StdEncoding.DecodeString(args[0]) 30 } 31 if err != nil { 32 return err 33 } 34 35 tx, err := clientCtx.TxConfig.TxDecoder()(txBytes) 36 if err != nil { 37 return err 38 } 39 40 json, err := clientCtx.TxConfig.TxJSONEncoder()(tx) 41 if err != nil { 42 return err 43 } 44 45 return clientCtx.PrintBytes(json) 46 }, 47 } 48 49 cmd.Flags().BoolP(flagHex, "x", false, "Treat input as hexadecimal instead of base64") 50 flags.AddTxFlagsToCmd(cmd) 51 _ = cmd.Flags().MarkHidden(flags.FlagOutput) // decoding makes sense to output only json 52 53 return cmd 54 }