Skip to content

Commit 8466888

Browse files
alexanderbezmergify-bot
authored andcommitted
refactor: debug addr cmd (#556)
(cherry picked from commit aec021e)
1 parent dffb7eb commit 8466888

File tree

3 files changed

+92
-2
lines changed

3 files changed

+92
-2
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<!-- markdownlint-disable MD013 -->
2+
<!-- markdownlint-disable MD024 -->
23

34
<!--
45
Changelog Guiding Principles:
@@ -45,6 +46,10 @@ Ref: https://keepachangelog.com/en/1.0.0/
4546

4647
## [Unreleased]
4748

49+
### Features
50+
51+
- [#556](https://github.com/umee-network/umee/pull/556) Refactor the `debug addr` command to convert addresses between any Bech32 HRP.
52+
4853
## [v1.0.1](https://github.com/umee-network/umee/releases/tag/v1.0.1) - 2022-02-07
4954

5055
### Bug Fixes

cmd/umeed/cmd/debug.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package cmd
2+
3+
import (
4+
"encoding/hex"
5+
"errors"
6+
"fmt"
7+
8+
"github.com/cosmos/cosmos-sdk/client"
9+
"github.com/cosmos/cosmos-sdk/client/debug"
10+
sdk "github.com/cosmos/cosmos-sdk/types"
11+
"github.com/cosmos/cosmos-sdk/version"
12+
"github.com/spf13/cobra"
13+
14+
umeeapp "github.com/umee-network/umee/app"
15+
)
16+
17+
const (
18+
flagBech32HRP = "bech32-hrp"
19+
)
20+
21+
// debugCmd returns a command handler for debugging addresses and public keys.
22+
// It is based off of the SDK's debug command root handler with modified
23+
// sub-commands.
24+
func debugCmd() *cobra.Command {
25+
cmd := &cobra.Command{
26+
Use: "debug",
27+
Short: "Commands to aid in debugging addresses and public keys",
28+
RunE: client.ValidateCmd,
29+
}
30+
31+
cmd.AddCommand(debug.PubkeyCmd())
32+
cmd.AddCommand(debugAddrCmd())
33+
cmd.AddCommand(debug.RawBytesCmd())
34+
35+
return cmd
36+
}
37+
38+
// nolint: lll
39+
func debugAddrCmd() *cobra.Command {
40+
cmd := &cobra.Command{
41+
Use: "addr [address]",
42+
Short: "Convert an address between hex and bech32",
43+
Long: fmt.Sprintf(`Convert an address between hex encoding and bech32.
44+
45+
Example:
46+
$ %s debug addr cosmos1e0jnq2sun3dzjh8p2xq95kk0expwmd7shwjpfg
47+
`, version.AppName),
48+
Args: cobra.ExactArgs(1),
49+
RunE: func(cmd *cobra.Command, args []string) error {
50+
addrStr := args[0]
51+
52+
var (
53+
bz []byte
54+
err error
55+
)
56+
57+
// try HEX then Bech32
58+
bz, err = hex.DecodeString(addrStr)
59+
if err != nil {
60+
bech32HRP, err := cmd.Flags().GetString(flagBech32HRP)
61+
if err != nil {
62+
return err
63+
}
64+
65+
bz, err = sdk.GetFromBech32(addrStr, bech32HRP)
66+
if err != nil {
67+
return errors.New("failed to decode address as HEX and Bech32")
68+
}
69+
}
70+
71+
if err := umeeapp.VerifyAddressFormat(bz); err != nil {
72+
return fmt.Errorf("failed to verify converted address: %w", err)
73+
}
74+
75+
cmd.Printf("Address (HEX): %X\n", bz)
76+
cmd.Printf("Address Bech32 Account: %s\n", sdk.AccAddress(bz))
77+
cmd.Printf("Address Bech32 Validator Operator: %s\n", sdk.ValAddress(bz))
78+
79+
return nil
80+
},
81+
}
82+
83+
cmd.Flags().String(flagBech32HRP, umeeapp.AccountAddressPrefix, "Input Bech32 HRP (use only when address input is a Bech32 address")
84+
85+
return cmd
86+
}

cmd/umeed/cmd/root.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
bridgecmd "github.com/Gravity-Bridge/Gravity-Bridge/module/cmd/gravity/cmd"
1010
"github.com/cosmos/cosmos-sdk/client"
11-
"github.com/cosmos/cosmos-sdk/client/debug"
1211
"github.com/cosmos/cosmos-sdk/client/flags"
1312
"github.com/cosmos/cosmos-sdk/client/keys"
1413
"github.com/cosmos/cosmos-sdk/client/rpc"
@@ -136,7 +135,7 @@ func initRootCmd(rootCmd *cobra.Command, ac appCreator) {
136135
),
137136
bridgeGenTxCmd,
138137
tmcli.NewCompletionCmd(rootCmd, true),
139-
debug.Cmd(),
138+
debugCmd(),
140139
)
141140

142141
server.AddCommands(rootCmd, app.DefaultNodeHome, ac.newApp, ac.appExport, addModuleInitFlags)

0 commit comments

Comments
 (0)