cosmos-sdk/x/auth/client/cli/decode.go
Ryan Christoffersen a70f39e145
docs: encode/decode transaction (#10133)
## Description

Closes: #10102

<!-- Add a description of the changes that this PR introduces and the files that
are the most critical to review. -->

Adds examples for encoding/decoding a transaction using the CLI. Also fixes CLI use and description.

---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [ ] included the correct `docs:` prefix in the PR title
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the [documentation writing guidelines](https://github.com/cosmos/cosmos-sdk/blob/master/docs/DOC_WRITING_GUIDELINES.md)
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct `docs:` prefix in the PR title
- [ ] confirmed all author checklist items have been addressed 
- [ ] confirmed that this PR only changes documentation
- [ ] reviewed content for consistency
- [ ] reviewed content for thoroughness
- [ ] reviewed content for spelling and grammar
- [ ] tested instructions (if applicable)
2021-09-14 17:42:21 +00:00

54 lines
1.2 KiB
Go

package cli
import (
"encoding/base64"
"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 [protobuf-byte-string]",
Short: "Decode a 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
}
json, err := clientCtx.TxConfig.TxJSONEncoder()(tx)
if err != nil {
return err
}
return clientCtx.PrintBytes(json)
},
}
cmd.Flags().BoolP(flagHex, "x", false, "Treat input as hexadecimal instead of base64")
flags.AddTxFlagsToCmd(cmd)
return cmd
}