Skip to content

Commit

Permalink
feat(client): add consensus address for debug cmd (backport #20328) (#…
Browse files Browse the repository at this point in the history
…20366)

Co-authored-by: mmsqe <[email protected]>
Co-authored-by: Julien Robert <[email protected]>
  • Loading branch information
3 people authored May 13, 2024
1 parent 5b6045c commit 08fdfec
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Improvements

* (debug) [#20328](https://github.com/cosmos/cosmos-sdk/pull/20328) Add consensus address for debug cmd.
* (runtime) [#20264](https://github.com/cosmos/cosmos-sdk/pull/20264) Expose grpc query router via depinject.

### Bug Fixes
Expand Down
41 changes: 28 additions & 13 deletions client/debug/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,28 +253,43 @@ $ %s debug addr cosmos1e0jnq2sun3dzjh8p2xq95kk0expwmd7shwjpfg
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
addrString := args[0]
var addr []byte

// try hex, then bech32
var err error
addr, err = hex.DecodeString(addrString)
if err != nil {
var err2 error
addr, err2 = sdk.AccAddressFromBech32(addrString)
if err2 != nil {
var err3 error
addr, err3 = sdk.ValAddressFromBech32(addrString)

if err3 != nil {
return fmt.Errorf("expected hex or bech32. Got errors: hex: %v, bech32 acc: %v, bech32 val: %v", err, err2, err3)
var (
addr []byte
err error
)
decodeFns := []func(text string) ([]byte, error){
hex.DecodeString,
func(text string) ([]byte, error) { return sdk.AccAddressFromBech32(text) },
func(text string) ([]byte, error) { return sdk.ValAddressFromBech32(text) },
func(text string) ([]byte, error) { return sdk.ConsAddressFromBech32(text) },
}
errs := make([]any, 0, len(decodeFns))
for _, fn := range decodeFns {
if addr, err = fn(addrString); err == nil {
break
}
errs = append(errs, err)
}
if len(errs) == len(decodeFns) {
errTags := []string{
"hex", "bech32 acc", "bech32 val", "bech32 con",
}
format := ""
for i := range errs {
if format != "" {
format += ", "
}
format += errTags[i] + ": %w"
}
return fmt.Errorf("expected hex or bech32. Got errors: "+format, errs...)
}

cmd.Println("Address:", addr)
cmd.Printf("Address (hex): %X\n", addr)
cmd.Printf("Bech32 Acc: %s\n", sdk.AccAddress(addr))
cmd.Printf("Bech32 Val: %s\n", sdk.ValAddress(addr))
cmd.Printf("Bech32 Con: %s\n", sdk.ConsAddress(addr))
return nil
},
}
Expand Down

0 comments on commit 08fdfec

Please sign in to comment.