-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a9b7d1c
commit aec021e
Showing
3 changed files
with
92 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters