forked from crypto-org-chain/cronos
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Problem: no end-to-end encryption module (crypto-org-chain#1407)
* 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
1 parent
9001b48
commit b231edd
Showing
17 changed files
with
2,306 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.