Skip to content

Commit

Permalink
refactor: debug addr cmd (#556) (#557)
Browse files Browse the repository at this point in the history
  • Loading branch information
mergify[bot] authored Feb 16, 2022
1 parent dffb7eb commit 6a01db9
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 2 deletions.
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

- [#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

0 comments on commit 6a01db9

Please sign in to comment.