refactor: embed address codec in auth keeper (#15929)

This commit is contained in:
Julien Robert 2023-04-24 17:02:26 +02:00 committed by GitHub
parent 7b5602492c
commit 866cfa4860
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 25 deletions

View File

@ -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
}
@ -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
}
@ -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
}
@ -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
}
@ -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)
}

View File

@ -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
@ -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
}
@ -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"),
@ -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.
@ -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