-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
decode.go
54 lines (44 loc) · 1.4 KB
/
decode.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package cli
import (
"encoding/base64"
"encoding/hex"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/tendermint/go-amino"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/client/flags"
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 *amino.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 *amino.Codec) func(cmd *cobra.Command, args []string) (err error) {
return func(cmd *cobra.Command, args []string) (err error) {
cliCtx := context.NewCLIContext().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 = cliCtx.Codec.UnmarshalBinaryLengthPrefixed(txBytes, &stdTx)
if err != nil {
return err
}
return cliCtx.PrintOutput(stdTx)
}
}