Skip to content

Commit

Permalink
getter/setter
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang committed Apr 26, 2024
1 parent da47987 commit f054777
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ****/

Expand Down
28 changes: 21 additions & 7 deletions x/e2ee/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,41 @@ 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 (
_ types.MsgServer = Keeper{}
_ 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,
}
}

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(
Expand All @@ -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
}
19 changes: 19 additions & 0 deletions x/e2ee/types/keys.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package types

import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

const (
// ModuleName defines the module name
ModuleName = "e2ee"
Expand All @@ -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
}

0 comments on commit f054777

Please sign in to comment.