Skip to content

Commit

Permalink
refactor(debug): use address codec in debug command (cosmos#17512)
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt authored Aug 24, 2023
1 parent eb16bd1 commit 5b02261
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 55 deletions.
32 changes: 23 additions & 9 deletions client/debug/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (
errorsmod "cosmossdk.io/errors"

"github.com/cosmos/cosmos-sdk/client"
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
legacybech32 "github.com/cosmos/cosmos-sdk/types/bech32/legacybech32" //nolint:staticcheck // we do old keys, they're keys after all.
"github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/version"
Expand Down Expand Up @@ -213,17 +213,19 @@ func AddrCmd() *cobra.Command {
if err2 != nil {
var err3 error
addr, err3 = clientCtx.ValidatorAddressCodec.StringToBytes(addrString)

if err3 != nil {
return fmt.Errorf("expected hex or bech32. Got errors: hex: %v, bech32 acc: %v, bech32 val: %v", err, err2, err3)
return fmt.Errorf("expected hex or bech32. Got errors: hex: %w, bech32 acc: %w, bech32 val: %w", err, err2, err3)
}
}
}

acc, _ := clientCtx.AddressCodec.BytesToString(addr)
val, _ := clientCtx.ValidatorAddressCodec.BytesToString(addr)

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 Acc: %s\n", acc)
cmd.Printf("Bech32 Val: %s\n", val)
return nil
},
}
Expand Down Expand Up @@ -263,12 +265,24 @@ func PrefixesCmd() *cobra.Command {
return &cobra.Command{
Use: "prefixes",
Short: "List prefixes used for Human-Readable Part (HRP) in Bech32",
Long: "List prefixes used in Bech32 addresses. NOTE, if the chain does not use the Cosmos SDK global config, this will not be accurate.",
Example: fmt.Sprintf("$ %s debug prefixes", version.AppName),
RunE: func(cmd *cobra.Command, args []string) error {
cmd.Printf("Bech32 Acc: %s\n", sdk.GetConfig().GetBech32AccountAddrPrefix())
cmd.Printf("Bech32 Val: %s\n", sdk.GetConfig().GetBech32ValidatorAddrPrefix())
cmd.Printf("Bech32 Con: %s\n", sdk.GetConfig().GetBech32ConsensusAddrPrefix())
clientCtx := client.GetClientContextFromCmd(cmd)

acc, _ := clientCtx.AddressCodec.BytesToString([]byte{})
val, _ := clientCtx.ValidatorAddressCodec.BytesToString([]byte{})
cons, _ := clientCtx.ConsensusAddressCodec.BytesToString([]byte{})

checksumLen := 7
if _, ok := clientCtx.AddressCodec.(addresscodec.Bech32Codec); !ok {
cmd.Printf("%s uses custom address codec, this command may not work as expected.\n", version.AppName)
checksumLen = 0
}

cmd.Printf("Bech32 Acc: %s\n", acc[:len(acc)-checksumLen])
cmd.Printf("Bech32 Val: %s\n", val[:len(val)-checksumLen])
cmd.Printf("Bech32 Con: %s\n", cons[:len(cons)-checksumLen])

return nil
},
}
Expand Down
49 changes: 3 additions & 46 deletions x/auth/codec/bech32_codec.go
Original file line number Diff line number Diff line change
@@ -1,55 +1,12 @@
package codec

import (
"errors"
"strings"

"cosmossdk.io/core/address"
errorsmod "cosmossdk.io/errors"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/bech32"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
addresscodec "github.com/cosmos/cosmos-sdk/codec/address"
)

type bech32Codec struct {
bech32Prefix string
}

var _ address.Codec = &bech32Codec{}

func NewBech32Codec(prefix string) address.Codec {
return bech32Codec{prefix}
}

// StringToBytes encodes text to bytes
func (bc bech32Codec) StringToBytes(text string) ([]byte, error) {
if len(strings.TrimSpace(text)) == 0 {
return []byte{}, errors.New("empty address string is not allowed")
}

hrp, bz, err := bech32.DecodeAndConvert(text)
if err != nil {
return nil, err
}

if hrp != bc.bech32Prefix {
return nil, errorsmod.Wrapf(sdkerrors.ErrLogic, "hrp does not match bech32 prefix: expected '%s' got '%s'", bc.bech32Prefix, hrp)
}

if err := sdk.VerifyAddressFormat(bz); err != nil {
return nil, err
}

return bz, nil
}

// BytesToString decodes bytes to text
func (bc bech32Codec) BytesToString(bz []byte) (string, error) {
text, err := bech32.ConvertAndEncode(bc.bech32Prefix, bz)
if err != nil {
return "", err
}

return text, nil
// Host custom bech32 address codec here, if auth ever do not depend on the Cosmos SDK.
return addresscodec.NewBech32Codec(prefix)
}

0 comments on commit 5b02261

Please sign in to comment.