Skip to content

Commit

Permalink
Problem: no end-to-end encryption module (crypto-org-chain#1407)
Browse files Browse the repository at this point in the history
* Problem: no end-to-end encryption module

add keeper

add grpc query

signer option

getter/setter

genesis init/export

fix lint

* fix proto lint

* fix test

* register codec

* changelog

* fix build

* Update x/e2ee/types/keys.go

Co-authored-by: mmsqe <[email protected]>
Signed-off-by: yihuang <[email protected]>

* Update x/e2ee/types/codec.go

Co-authored-by: mmsqe <[email protected]>
Signed-off-by: yihuang <[email protected]>

---------

Signed-off-by: yihuang <[email protected]>
Co-authored-by: mmsqe <[email protected]>
  • Loading branch information
2 people authored and alienc0der committed Jun 8, 2024
1 parent 9001b48 commit b231edd
Show file tree
Hide file tree
Showing 17 changed files with 2,306 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

* [#1377](https://github.com/crypto-org-chain/cronos/pull/1377) Upgrade sdk to 0.50, and integrate block-stm parallel tx execution.
* [#1394](https://github.com/crypto-org-chain/cronos/pull/1394) Add icahost wirings but disable in parameters.
* [#1407](https://github.com/crypto-org-chain/cronos/pull/1407) Add end-to-end encryption module.

### Improvements

Expand Down
13 changes: 13 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ import (
icaauthkeeper "github.com/crypto-org-chain/cronos/v2/x/icaauth/keeper"
icaauthtypes "github.com/crypto-org-chain/cronos/v2/x/icaauth/types"

e2ee "github.com/crypto-org-chain/cronos/v2/x/e2ee"
e2eekeeper "github.com/crypto-org-chain/cronos/v2/x/e2ee/keeper"
e2eetypes "github.com/crypto-org-chain/cronos/v2/x/e2ee/types"

// force register the extension json-rpc.
_ "github.com/crypto-org-chain/cronos/v2/x/cronos/rpc"
_ "github.com/ethereum/go-ethereum/eth/tracers/js"
Expand Down Expand Up @@ -250,6 +254,8 @@ func StoreKeys() (
icahosttypes.StoreKey,
// ethermint keys
evmtypes.StoreKey, feemarkettypes.StoreKey,
// e2ee keys
e2eetypes.StoreKey,
// this line is used by starport scaffolding # stargate/app/storeKey
cronostypes.StoreKey,
}
Expand Down Expand Up @@ -320,6 +326,9 @@ type App struct {
EvmKeeper *evmkeeper.Keeper
FeeMarketKeeper feemarketkeeper.Keeper

// e2ee keeper
E2EEKeeper e2eekeeper.Keeper

// this line is used by starport scaffolding # stargate/app/keeperDeclaration

CronosKeeper cronoskeeper.Keeper
Expand Down Expand Up @@ -704,6 +713,8 @@ 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.AccountKeeper.AddressCodec())

/**** Module Options ****/

// NOTE: we may consider parsing `appOpts` inside module constructors. For the moment
Expand Down Expand Up @@ -754,6 +765,7 @@ func New(
// Ethermint app modules
feemarket.NewAppModule(app.FeeMarketKeeper, feeMarketS),
evm.NewAppModule(app.EvmKeeper, app.AccountKeeper, evmS),
e2ee.NewAppModule(app.E2EEKeeper),

// Cronos app modules
cronosModule,
Expand Down Expand Up @@ -864,6 +876,7 @@ func New(
consensusparamtypes.ModuleName,
// NOTE: crisis module must go at the end to check for invariants on each module
crisistypes.ModuleName,
e2eetypes.ModuleName,
}

app.ModuleManager.SetOrderPreBlockers(
Expand Down
2 changes: 2 additions & 0 deletions app/upgrades.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/cosmos/cosmos-sdk/types/module"
icahosttypes "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/host/types"
clientkeeper "github.com/cosmos/ibc-go/v8/modules/core/02-client/keeper"
e2eetypes "github.com/crypto-org-chain/cronos/v2/x/e2ee/types"
)

func (app *App) RegisterUpgradeHandlers(cdc codec.BinaryCodec, clientKeeper clientkeeper.Keeper) {
Expand Down Expand Up @@ -39,6 +40,7 @@ func (app *App) RegisterUpgradeHandlers(cdc codec.BinaryCodec, clientKeeper clie
app.SetStoreLoader(upgradetypes.UpgradeStoreLoader(upgradeInfo.Height, &storetypes.StoreUpgrades{
Added: []string{
icahosttypes.StoreKey,
e2eetypes.StoreKey,
},
}))
}
Expand Down
18 changes: 18 additions & 0 deletions proto/e2ee/genesis.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
syntax = "proto3";
package e2ee;

import "gogoproto/gogo.proto";

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

// 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 EncryptionKeyEntry keys = 1 [(gogoproto.nullable) = false];
}
24 changes: 24 additions & 0 deletions proto/e2ee/query.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
syntax = "proto3";
package e2ee;

import "google/api/annotations.proto";

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

// Query defines the gRPC querier service.
service Query {
// Key queries the encryption key of a given address
rpc Key(KeyRequest) returns (KeyResponse) {
option (google.api.http).get = "/e2ee/v1/key/{address}";
}
}

// KeyRequest is the request type for the Query/Key RPC method.
message KeyRequest {
string address = 1;
}

// KeyResponse is the response type for the Query/Key RPC method.
message KeyResponse {
bytes key = 1;
}
26 changes: 26 additions & 0 deletions proto/e2ee/tx.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
syntax = "proto3";
package e2ee;

import "cosmos/msg/v1/msg.proto";

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

// Msg defines the e2ee Msg service
service Msg {
option (cosmos.msg.v1.service) = true;

// RegisterEncryptionKey registers a new encryption key to a specific account
rpc RegisterEncryptionKey(MsgRegisterEncryptionKey) returns (MsgRegisterEncryptionKeyResponse);
}

// MsgRegisterEncryptionKey defines the Msg/RegisterEncryptionKey request type
message MsgRegisterEncryptionKey {
option (cosmos.msg.v1.signer) = "address";

string address = 1;
bytes key = 2;
}

// MsgRegisterEncryptionKeyResponse defines the Msg/RegisterEncryptionKey response type
message MsgRegisterEncryptionKeyResponse {
}
2 changes: 2 additions & 0 deletions x/e2ee/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
e2ee a module for end-to-end encrypted messaging, user can register encryption keys on chain, and receive encrypted
messages on/off chain.
22 changes: 22 additions & 0 deletions x/e2ee/autocli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package e2ee

import (
autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
)

// AutoCLIOptions implements the autocli.HasAutoCLIConfig interface.
func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions {
return &autocliv1.ModuleOptions{
Query: &autocliv1.ServiceCommandDescriptor{
Service: "e2ee.Query",
RpcCommandOptions: []*autocliv1.RpcCommandOptions{
{
RpcMethod: "Key",
Use: "key [address]",
Short: "Query an encryption key by address",
PositionalArgs: []*autocliv1.PositionalArgDescriptor{{ProtoField: "address"}},
},
},
},
}
}
87 changes: 87 additions & 0 deletions x/e2ee/keeper/keeper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package keeper

import (
"context"

"cosmossdk.io/core/address"
"cosmossdk.io/store/prefix"
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
addressCodec address.Codec
}

var (
_ types.MsgServer = Keeper{}
_ types.QueryServer = Keeper{}
)

func NewKeeper(storeKey storetypes.StoreKey, addressCodec address.Codec) Keeper {
return Keeper{
storeKey: storeKey,
addressCodec: addressCodec,
}
}

func (k Keeper) RegisterEncryptionKey(
ctx context.Context,
req *types.MsgRegisterEncryptionKey,
) (*types.MsgRegisterEncryptionKeyResponse, error) {
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(
ctx context.Context,
state *types.GenesisState,
) error {
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) {
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) {
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
}
Loading

0 comments on commit b231edd

Please sign in to comment.