From f0547774e487bb948462854b48ed20f23c2c8574 Mon Sep 17 00:00:00 2001 From: yihuang Date: Fri, 26 Apr 2024 12:15:52 +0800 Subject: [PATCH] getter/setter --- app/app.go | 2 +- x/e2ee/keeper/keeper.go | 28 +++++++++++++++++++++------- x/e2ee/types/keys.go | 19 +++++++++++++++++++ 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/app/app.go b/app/app.go index 67c8319fa6..be1ab44896 100644 --- a/app/app.go +++ b/app/app.go @@ -713,7 +713,7 @@ func New( // If evidence needs to be handled for the app, set routes in router here and seal app.EvidenceKeeper = *evidenceKeeper - app.E2EEKeeper = e2eekeeper.NewKeeper(keys[e2eetypes.StoreKey]) + app.E2EEKeeper = e2eekeeper.NewKeeper(keys[e2eetypes.StoreKey], app.AccountKeeper.AddressCodec()) /**** Module Options ****/ diff --git a/x/e2ee/keeper/keeper.go b/x/e2ee/keeper/keeper.go index d571b30293..281f4d370b 100644 --- a/x/e2ee/keeper/keeper.go +++ b/x/e2ee/keeper/keeper.go @@ -3,13 +3,16 @@ package keeper import ( "context" + "cosmossdk.io/core/address" storetypes "cosmossdk.io/store/types" + sdk "github.com/cosmos/cosmos-sdk/types" "github.com/crypto-org-chain/cronos/v2/x/e2ee/types" ) type Keeper struct { - storeKey storetypes.StoreKey + storeKey storetypes.StoreKey + addressCodec address.Codec } var ( @@ -17,9 +20,10 @@ var ( _ types.QueryServer = Keeper{} ) -func NewKeeper(storeKey storetypes.StoreKey) Keeper { +func NewKeeper(storeKey storetypes.StoreKey, addressCodec address.Codec) Keeper { return Keeper{ - storeKey: storeKey, + storeKey: storeKey, + addressCodec: addressCodec, } } @@ -27,8 +31,13 @@ func (k Keeper) RegisterEncryptionKey( ctx context.Context, req *types.MsgRegisterEncryptionKey, ) (*types.MsgRegisterEncryptionKeyResponse, error) { - // TODO - return nil, nil + bz, err := k.addressCodec.StringToBytes(req.Address) + if err != nil { + return nil, err + } + sdkCtx := sdk.UnwrapSDKContext(ctx) + sdkCtx.KVStore(k.storeKey).Set(types.KeyPrefix(bz), req.Key) + return &types.MsgRegisterEncryptionKeyResponse{}, nil } func (k Keeper) InitGenesis( @@ -45,6 +54,11 @@ func (k Keeper) ExportGenesis(ctx context.Context) (*types.GenesisState, error) } func (k Keeper) Key(ctx context.Context, req *types.KeyRequest) (*types.KeyResponse, error) { - // TODO - return nil, nil + bz, err := k.addressCodec.StringToBytes(req.Address) + if err != nil { + return nil, err + } + sdkCtx := sdk.UnwrapSDKContext(ctx) + value := sdkCtx.KVStore(k.storeKey).Get(types.KeyPrefix(bz)) + return &types.KeyResponse{Key: value}, nil } diff --git a/x/e2ee/types/keys.go b/x/e2ee/types/keys.go index b05a4fff7e..c0ce0a4e99 100644 --- a/x/e2ee/types/keys.go +++ b/x/e2ee/types/keys.go @@ -1,5 +1,9 @@ package types +import ( + sdk "github.com/cosmos/cosmos-sdk/types" +) + const ( // ModuleName defines the module name ModuleName = "e2ee" @@ -10,3 +14,18 @@ const ( // RouterKey is the message route for slashing RouterKey = ModuleName ) + +const ( + prefixEncryptionKey = iota + 1 +) + +var ( + KeyPrefixEncryptionKey = []byte{prefixEncryptionKey} +) + +func KeyPrefix(addr sdk.AccAddress) []byte { + key := make([]byte, 1+len(addr)) + key[0] = prefixEncryptionKey + copy(key[1:], addr) + return key +}