From e82817a9909b5d34dc4fa632691da6be5eb2c8cf Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Wed, 16 Feb 2022 10:57:25 -0500 Subject: [PATCH 1/3] add debug addr cmd --- cmd/umeed/cmd/debug.go | 86 ++++++++++++++++++++++++++++++++++++++++++ cmd/umeed/cmd/root.go | 3 +- 2 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 cmd/umeed/cmd/debug.go diff --git a/cmd/umeed/cmd/debug.go b/cmd/umeed/cmd/debug.go new file mode 100644 index 0000000000..891cebcdff --- /dev/null +++ b/cmd/umeed/cmd/debug.go @@ -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 + ) + + bech32HRP, err := cmd.Flags().GetString(flagBech32HRP) + if err != nil { + return err + } + + // try hex then bech32 + bz, err = hex.DecodeString(addrStr) + if err != nil { + 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: %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 +} diff --git a/cmd/umeed/cmd/root.go b/cmd/umeed/cmd/root.go index 997944faa0..926b8255f7 100644 --- a/cmd/umeed/cmd/root.go +++ b/cmd/umeed/cmd/root.go @@ -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" @@ -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) From 17bebf76407dec654b3c0009ff49d55bab070620 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Wed, 16 Feb 2022 10:58:54 -0500 Subject: [PATCH 2/3] cl++ --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ce92f4f98..0ac43d1d08 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,6 +45,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 From 338e2a26f8a9a5eb4678b99628acacee82609d47 Mon Sep 17 00:00:00 2001 From: Aleksandr Bezobchuk Date: Wed, 16 Feb 2022 11:51:20 -0500 Subject: [PATCH 3/3] fixes --- CHANGELOG.md | 1 + cmd/umeed/cmd/debug.go | 14 +++++++------- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ac43d1d08..7f88ba8790 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ +