From 866cfa486092653ae45b6021eabfbd1be17e66e3 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Mon, 24 Apr 2023 17:02:26 +0200 Subject: [PATCH] refactor: embed address codec in auth keeper (#15929) --- x/auth/keeper/grpc_query.go | 20 ++++---------------- x/auth/keeper/keeper.go | 18 +++++++++--------- 2 files changed, 13 insertions(+), 25 deletions(-) diff --git a/x/auth/keeper/grpc_query.go b/x/auth/keeper/grpc_query.go index 482890de7a..3fac71bc0e 100644 --- a/x/auth/keeper/grpc_query.go +++ b/x/auth/keeper/grpc_query.go @@ -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) -} diff --git a/x/auth/keeper/keeper.go b/x/auth/keeper/keeper.go index e5db0183bb..f4dc162d33 100644 --- a/x/auth/keeper/keeper.go +++ b/x/auth/keeper/keeper.go @@ -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