-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.go
86 lines (70 loc) · 2.09 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
package cmd
import (
"encoding/hex"
"errors"
"fmt"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/debug"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/version"
"github.com/spf13/cobra"
appparams "github.com/tessornetwork/nebula/app/params"
)
const (
flagBech32HRP = "bech32-hrp"
)
// debugCmd returns a command handler for debugging addresses and public keys.
// It is based off of the SDK's debug command root handler with modified
// sub-commands.
func debugCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "debug",
Short: "Commands to aid in debugging addresses and public keys",
RunE: client.ValidateCmd,
}
cmd.AddCommand(debug.PubkeyCmd())
cmd.AddCommand(debug.PubkeyRawCmd())
cmd.AddCommand(debugAddrCmd())
cmd.AddCommand(debug.RawBytesCmd())
return cmd
}
func debugAddrCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "addr [address]",
Short: "Convert an address between hex and bech32",
Long: fmt.Sprintf(`Convert an address between hex encoding and bech32.
Example:
$ %s debug addr cosmos1e0jnq2sun3dzjh8p2xq95kk0expwmd7shwjpfg
`, version.AppName),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
addrStr := args[0]
var (
bz []byte
err error
)
// try HEX then Bech32
bz, err = hex.DecodeString(addrStr)
if err != nil {
bech32HRP, err := cmd.Flags().GetString(flagBech32HRP)
if err != nil {
return err
}
bz, err = sdk.GetFromBech32(addrStr, bech32HRP)
if err != nil {
return errors.New("failed to decode address as HEX and Bech32")
}
}
if err := sdk.VerifyAddressFormat(bz); err != nil {
return fmt.Errorf("failed to verify converted address: %w", err)
}
cmd.Printf("Address (HEX): %X\n", bz)
cmd.Printf("Address Bech32 Account: %s\n", sdk.AccAddress(bz))
cmd.Printf("Address Bech32 Validator Operator: %s\n", sdk.ValAddress(bz))
return nil
},
}
cmd.Flags().String(flagBech32HRP, appparams.AccountAddressPrefix,
"Input Bech32 HRP (use only when address input is a Bech32 address")
return cmd
}