Skip to content

Commit

Permalink
genesis init/export
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang committed Apr 26, 2024
1 parent f054777 commit 9f7a6cb
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 58 deletions.
8 changes: 4 additions & 4 deletions proto/e2ee/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import "gogoproto/gogo.proto";

option go_package = "github.com/crypto-org-chain/cronos/v2/x/e2ee/types";

// PublicKeyEntry is a type that contains the owner and the public key.
message PublicKeyEntry {
string owner = 1;
// EncryptionKeyEntry is a type that contains the owner and the public key.
message EncryptionKeyEntry {
string address = 1;
bytes key = 2;
}

// GenesisState defines the e2ee module's genesis state.
message GenesisState {
// params defines all the paramaters of the module.
repeated PublicKeyEntry keys = 1 [(gogoproto.nullable) = false];
repeated EncryptionKeyEntry keys = 1 [(gogoproto.nullable) = false];
}
29 changes: 26 additions & 3 deletions x/e2ee/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"cosmossdk.io/core/address"
"cosmossdk.io/store/prefix"
storetypes "cosmossdk.io/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"

Expand Down Expand Up @@ -44,13 +45,35 @@ func (k Keeper) InitGenesis(
ctx context.Context,
state *types.GenesisState,
) error {
// TODO
for _, key := range state.Keys {
if _, err := k.RegisterEncryptionKey(ctx, &types.MsgRegisterEncryptionKey{
Address: key.Address,
Key: key.Key,
}); err != nil {
return err
}
}
return nil
}

func (k Keeper) ExportGenesis(ctx context.Context) (*types.GenesisState, error) {
// TODO
return nil, nil
sdkCtx := sdk.UnwrapSDKContext(ctx)
iter := prefix.NewStore(sdkCtx.KVStore(k.storeKey), types.KeyPrefixEncryptionKey).Iterator(nil, nil)
defer iter.Close()

var keys []types.EncryptionKeyEntry
for ; iter.Valid(); iter.Next() {
address, err := k.addressCodec.BytesToString(iter.Key())
if err != nil {
return nil, err
}
key := iter.Value()
keys = append(keys, types.EncryptionKeyEntry{
Address: address,
Key: key,
})
}
return &types.GenesisState{Keys: keys}, nil
}

func (k Keeper) Key(ctx context.Context, req *types.KeyRequest) (*types.KeyResponse, error) {
Expand Down
102 changes: 51 additions & 51 deletions x/e2ee/types/genesis.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9f7a6cb

Please sign in to comment.