/
decode.go
53 lines (42 loc) · 1.21 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
package cli
import (
"encoding/base64"
"encoding/hex"
"github.com/spf13/cobra"
"github.com/mycodeku/transtionhelper/client"
"github.com/mycodeku/transtionhelper/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
}