-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.go
127 lines (112 loc) · 3.81 KB
/
debug.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package debug
import (
"encoding/hex"
"fmt"
"strconv"
"strings"
"github.com/spf13/cobra"
"github.com/tendermint/tendermint/libs/bytes"
"github.com/ethereum/go-ethereum/common"
"github.com/cosmos/cosmos-sdk/client"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"
)
// Cmd creates a main CLI command
func Cmd() *cobra.Command {
cmd := &cobra.Command{
Use: "debug",
Short: "Tool for helping with debugging your application",
RunE: client.ValidateCmd,
}
cmd.AddCommand(PubkeyCmd())
cmd.AddCommand(AddrCmd())
cmd.AddCommand(RawBytesCmd())
return cmd
}
// getPubKeyFromString decodes SDK PubKey using JSON marshaler.
func getPubKeyFromString(ctx client.Context, pkstr string) (cryptotypes.PubKey, error) {
var pk cryptotypes.PubKey
err := ctx.Codec.UnmarshalInterfaceJSON([]byte(pkstr), &pk)
return pk, err
}
func PubkeyCmd() *cobra.Command {
return &cobra.Command{
Use: "pubkey [pubkey]",
Short: "Decode a pubkey from proto JSON",
Long: "Decode a pubkey from proto JSON and display it's address",
Example: fmt.Sprintf(
`"$ %s debug pubkey '{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"AurroA7jvfPd1AadmmOvWM2rJSwipXfRf8yD6pLbA2DJ"}'`,
version.AppName,
),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx := client.GetClientContextFromCmd(cmd)
pk, err := getPubKeyFromString(clientCtx, args[0])
if err != nil {
return err
}
addr := pk.Address()
cmd.Printf("Address (EIP-55): %s\n", common.BytesToAddress(addr))
cmd.Printf("Bech32 Acc: %s\n", sdk.AccAddress(addr))
cmd.Println("PubKey Hex:", hex.EncodeToString(pk.Bytes()))
return nil
},
}
}
func AddrCmd() *cobra.Command {
return &cobra.Command{
Use: "addr [address]",
Short: "Convert an address between hex and bech32",
Long: "Convert an address between hex encoding and bech32.",
Example: fmt.Sprintf(
`$ %s debug addr ethm10jmp6sgh4cc6zt3e8gw05wavvejgr5pw2unfju
$ %s debug addr 0xA588C66983a81e800Db4dF74564F09f91c026351`, version.AppName, version.AppName),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
addrString := args[0]
cfg := sdk.GetConfig()
var addr []byte
switch {
case common.IsHexAddress(addrString):
addr = common.HexToAddress(addrString).Bytes()
case strings.HasPrefix(addrString, cfg.GetBech32ValidatorAddrPrefix()):
addr, _ = sdk.ValAddressFromBech32(addrString)
case strings.HasPrefix(addrString, cfg.GetBech32AccountAddrPrefix()):
addr, _ = sdk.AccAddressFromBech32(addrString)
default:
return fmt.Errorf("expected a valid hex or bech32 address (acc prefix %s), got '%s'", cfg.GetBech32AccountAddrPrefix(), addrString)
}
cmd.Println("Address bytes:", addr)
cmd.Printf("Address (hex): %s\n", bytes.HexBytes(addr))
cmd.Printf("Address (EIP-55): %s\n", common.BytesToAddress(addr))
cmd.Printf("Bech32 Acc: %s\n", sdk.AccAddress(addr))
cmd.Printf("Bech32 Val: %s\n", sdk.ValAddress(addr))
return nil
},
}
}
func RawBytesCmd() *cobra.Command {
return &cobra.Command{
Use: "raw-bytes [raw-bytes]",
Short: "Convert raw bytes output (eg. [10 21 13 255]) to hex",
Example: fmt.Sprintf(`$ %s debug raw-bytes [72 101 108 108 111 44 32 112 108 97 121 103 114 111 117 110 100]`, version.AppName),
Args: cobra.ExactArgs(1),
RunE: func(_ *cobra.Command, args []string) error {
stringBytes := args[0]
stringBytes = strings.Trim(stringBytes, "[")
stringBytes = strings.Trim(stringBytes, "]")
spl := strings.Split(stringBytes, " ")
byteArray := []byte{}
for _, s := range spl {
b, err := strconv.ParseInt(s, 10, 8)
if err != nil {
return err
}
byteArray = append(byteArray, byte(b))
}
fmt.Printf("%X\n", byteArray)
return nil
},
}
}