-
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.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
dffb7eb
commit 6a01db9
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