Skip to content

Commit

Permalink
refactor: embed address codec in auth keeper (#15929)
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt authored Apr 24, 2023
1 parent 7b56024 commit 866cfa4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 25 deletions.
20 changes: 4 additions & 16 deletions x/auth/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (ak AccountKeeper) Account(c context.Context, req *types.QueryAccountReques
}

ctx := sdk.UnwrapSDKContext(c)
addr, err := ak.addressCdc.StringToBytes(req.Address)
addr, err := ak.StringToBytes(req.Address)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -184,7 +184,7 @@ func (ak AccountKeeper) AddressBytesToString(ctx context.Context, req *types.Add
return nil, errors.New("empty address bytes is not allowed")
}

text, err := ak.addressCdc.BytesToString(req.AddressBytes)
text, err := ak.BytesToString(req.AddressBytes)
if err != nil {
return nil, err
}
Expand All @@ -203,7 +203,7 @@ func (ak AccountKeeper) AddressStringToBytes(ctx context.Context, req *types.Add
return nil, errors.New("empty address string is not allowed")
}

bz, err := ak.addressCdc.StringToBytes(req.AddressString)
bz, err := ak.StringToBytes(req.AddressString)
if err != nil {
return nil, err
}
Expand All @@ -222,7 +222,7 @@ func (ak AccountKeeper) AccountInfo(goCtx context.Context, req *types.QueryAccou
}

ctx := sdk.UnwrapSDKContext(goCtx)
addr, err := ak.addressCdc.StringToBytes(req.Address)
addr, err := ak.StringToBytes(req.Address)
if err != nil {
return nil, err
}
Expand All @@ -246,15 +246,3 @@ func (ak AccountKeeper) AccountInfo(goCtx context.Context, req *types.QueryAccou
},
}, nil
}

// BytesToString converts an address from bytes to string, using the
// keeper's bech32 prefix.
func (ak AccountKeeper) BytesToString(address []byte) (string, error) {
return ak.addressCdc.BytesToString(address)
}

// StringToBytes converts an address from string to bytes, using the
// keeper's bech32 prefix.
func (ak AccountKeeper) StringToBytes(address string) ([]byte, error) {
return ak.addressCdc.StringToBytes(address)
}
18 changes: 9 additions & 9 deletions x/auth/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (

// AccountKeeperI is the interface contract that x/auth's keeper implements.
type AccountKeeperI interface {
address.Codec

// Return a new account with the next account number and the specified address. Does not save the new account to the store.
NewAccountWithAddress(context.Context, sdk.AccAddress) sdk.AccountI

Expand Down Expand Up @@ -58,20 +60,20 @@ type AccountKeeperI interface {
// AccountKeeper encodes/decodes accounts using the go-amino (binary)
// encoding/decoding library.
type AccountKeeper struct {
address.Codec

storeService store.KVStoreService
cdc codec.BinaryCodec
permAddrs map[string]types.PermissionsForAddress

// The prototypical AccountI constructor.
proto func() sdk.AccountI
addressCdc address.Codec
proto func() sdk.AccountI

// the address capable of executing a MsgUpdateParams message. Typically, this
// should be the x/gov module account.
authority string

// State

ParamsState collections.Item[types.Params] // NOTE: name is this because it conflicts with the Params gRPC method impl
AccountNumber collections.Sequence
}
Expand All @@ -93,16 +95,14 @@ func NewAccountKeeper(
permAddrs[name] = types.NewPermissionsForAddress(name, perms)
}

bech32Codec := NewBech32Codec(bech32Prefix)

sb := collections.NewSchemaBuilder(storeService)

return AccountKeeper{
Codec: NewBech32Codec(bech32Prefix),
storeService: storeService,
proto: proto,
cdc: cdc,
permAddrs: permAddrs,
addressCdc: bech32Codec,
authority: authority,
ParamsState: collections.NewItem(sb, types.ParamsKey, "params", codec.CollValue[types.Params](cdc)),
AccountNumber: collections.NewSequence(sb, types.GlobalAccountNumberKey, "account_number"),
Expand All @@ -117,7 +117,7 @@ func (ak AccountKeeper) GetAuthority() string {
// GetAddressCodec returns the x/auth module's address.
// x/auth is tied to bech32 encoded user accounts
func (ak AccountKeeper) GetAddressCodec() address.Codec {
return ak.addressCdc
return ak.Codec
}

// Logger returns a module-specific logger.
Expand Down Expand Up @@ -256,9 +256,9 @@ func (ak AccountKeeper) GetCodec() codec.BinaryCodec { return ak.cdc }

// add getter for bech32Prefix
func (ak AccountKeeper) getBech32Prefix() (string, error) {
bech32Codec, ok := ak.addressCdc.(bech32Codec)
bech32Codec, ok := ak.Codec.(bech32Codec)
if !ok {
return "", fmt.Errorf("unable cast addressCdc to bech32Codec; expected %T got %T", bech32Codec, ak.addressCdc)
return "", fmt.Errorf("unable cast addressCdc to bech32Codec; expected %T got %T", bech32Codec, ak.Codec)
}

return bech32Codec.bech32Prefix, nil
Expand Down

0 comments on commit 866cfa4

Please sign in to comment.