Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: debug addr cmd #556

Merged
merged 3 commits into from
Feb 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<!-- markdownlint-disable MD013 -->
<!-- markdownlint-disable MD024 -->

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

## [Unreleased]

### Features
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved

- [#556](https://github.com/umee-network/umee/pull/556) Refactor the `debug addr` command to convert addresses between any Bech32 HRP.

## [v1.0.1](https://github.com/umee-network/umee/releases/tag/v1.0.1) - 2022-02-07

### Bug Fixes
Expand Down
86 changes: 86 additions & 0 deletions cmd/umeed/cmd/debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,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"

umeeapp "github.com/umee-network/umee/app"
)

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(debugAddrCmd())
cmd.AddCommand(debug.RawBytesCmd())

return cmd
}

// nolint: lll
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 := umeeapp.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, umeeapp.AccountAddressPrefix, "Input Bech32 HRP (use only when address input is a Bech32 address")

return cmd
}
3 changes: 1 addition & 2 deletions cmd/umeed/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

bridgecmd "github.com/Gravity-Bridge/Gravity-Bridge/module/cmd/gravity/cmd"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/debug"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/client/rpc"
Expand Down Expand Up @@ -136,7 +135,7 @@ func initRootCmd(rootCmd *cobra.Command, ac appCreator) {
),
bridgeGenTxCmd,
tmcli.NewCompletionCmd(rootCmd, true),
debug.Cmd(),
debugCmd(),
)

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