Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem: no encrypt and decrypt cmds for message #1411

Merged
merged 7 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@

### Features

* [#1406](https://github.com/crypto-org-chain/cronos/pull/1406) Add set-encryption-key for encryption module.
* [#1406](https://github.com/crypto-org-chain/cronos/pull/1406) Add set-encryption-key for encryption module.
* [#1411](https://github.com/crypto-org-chain/cronos/pull/1411) Add encrypt and decrypt cmds for message.
mmsqe marked this conversation as resolved.
Show resolved Hide resolved

*April 8, 2024*

Expand Down
7 changes: 1 addition & 6 deletions client/docs/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@
}
},
{
"url": "./tmp-swagger-gen/e2ee/query.swagger.json",
"operationIds": {
"rename": {
"Params": "Keys"
}
}
"url": "./tmp-swagger-gen/e2ee/query.swagger.json"
},
{
"url": "./tmp-swagger-gen/ethermint/evm/v1/query.swagger.json",
Expand Down
2 changes: 2 additions & 0 deletions cmd/cronosd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import (
"github.com/crypto-org-chain/cronos/v2/app"
"github.com/crypto-org-chain/cronos/v2/cmd/cronosd/opendb"
"github.com/crypto-org-chain/cronos/v2/x/cronos"
e2eecli "github.com/crypto-org-chain/cronos/v2/x/e2ee/client/cli"
// this line is used by starport scaffolding # stargate/root/import
)

Expand Down Expand Up @@ -189,6 +190,7 @@ func initRootCmd(
queryCommand(),
txCommand(),
ethermintclient.KeyCommands(app.DefaultNodeHome),
e2eecli.E2EECommand(),
)

// add rosetta
Expand Down
6 changes: 6 additions & 0 deletions gomod2nix.toml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ schema = 3
[mod."cosmossdk.io/x/upgrade"]
version = "v0.1.1"
hash = "sha256-bM9ybpaibMH7k4M6/QAXCZ3fJcADfJHxvMgp4AVUihs="
[mod."filippo.io/age"]
version = "v1.1.1"
hash = "sha256-LRxxJQLQkzoCNYGS/XBixVmYXoZ1mPHKvFicPGXYLcw="
[mod."filippo.io/edwards25519"]
version = "v1.1.0"
hash = "sha256-9ACANrgWZSd5HYPfDZHY8DVbPSC9LOMgy8deq3rDOoc="
Expand Down Expand Up @@ -575,6 +578,9 @@ schema = 3
[mod."github.com/tendermint/go-amino"]
version = "v0.16.0"
hash = "sha256-JW4zO/0vMzf1dXLePOqaMtiLUZgNbuIseh9GV+jQlf0="
[mod."github.com/test-go/testify"]
version = "v1.1.4"
hash = "sha256-8xygO1Rd4eTrmRe/g7zaifpNkeb6EmjNfUvTWbjDtPg="
[mod."github.com/tidwall/btree"]
version = "v0.0.0-20240406140148-2687063b042c"
hash = "sha256-8eDLGHhw4qXG6MEa7w5Q9KLwOobXr8Vn5qqyQhuipQw="
Expand Down
38 changes: 35 additions & 3 deletions integration_tests/cosmoscli.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import binascii
import enum
import hashlib
import itertools
import json
import os
import re
Expand Down Expand Up @@ -1851,16 +1852,16 @@ def query_e2ee_key(self, address):
home=self.data_dir,
output="json",
)
)
)["key"]

def set_e2ee_key(self, key, **kwargs):
def register_e2ee_key(self, key, **kwargs):
kwargs.setdefault("gas_prices", DEFAULT_GAS_PRICE)
kwargs.setdefault("gas", DEFAULT_GAS)
rsp = json.loads(
self.raw(
"tx",
"e2ee",
"set-encryption-key",
"register-encryption-key",
key,
"-y",
home=self.data_dir,
Expand All @@ -1870,3 +1871,34 @@ def set_e2ee_key(self, key, **kwargs):
if rsp["code"] == 0:
rsp = self.event_query_tx_for(rsp["txhash"])
return rsp

def keygen(self, **kwargs):
return self.raw("e2ee", "keygen", home=self.data_dir, **kwargs).strip().decode()

def encrypt(self, input, *recipients, **kwargs):
return (
self.raw(
"e2ee",
"encrypt",
input,
*itertools.chain.from_iterable(("-r", r) for r in recipients),
home=self.data_dir,
**kwargs,
)
.strip()
.decode()
)

def decrypt(self, input, identity="e2ee-identity", **kwargs):
return (
self.raw(
"e2ee",
"decrypt",
input,
home=self.data_dir,
identity=identity,
**kwargs,
)
.strip()
.decode()
)
32 changes: 24 additions & 8 deletions integration_tests/test_e2ee.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
import base64
def test_encrypt_decrypt(cronos):
cli = cronos.cosmos_cli()

# gen two keys for two accounts
pubkey0 = cli.keygen(keyring_name="key0")
cli.register_e2ee_key(pubkey0, _from="validator")
assert cli.query_e2ee_key(cli.address("validator")) == pubkey0
pubkey1 = cli.keygen(keyring_name="key1")
cli.register_e2ee_key(pubkey1, _from="community")
assert cli.query_e2ee_key(cli.address("community")) == pubkey1

def test_set_key(cronos):
cli = cronos.cosmos_cli()
key = base64.b64encode(b"new_key").decode("utf-8")
cli.set_e2ee_key(key, _from="community")
adr = cli.address("community")
p = cli.query_e2ee_key(adr)
assert p["key"] == key
# prepare data file to encrypt
content = "Hello World!"
plainfile = cli.data_dir / "plaintext"
plainfile.write_text(content)

cipherfile = cli.data_dir / "ciphertext"
cli.encrypt(
plainfile,
cli.address("validator"),
cli.address("community"),
output=cipherfile,
)

assert cli.decrypt(cipherfile, identity="key0") == content
assert cli.decrypt(cipherfile, identity="key1") == content
2 changes: 1 addition & 1 deletion proto/e2ee/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ 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;
string key = 2;
}

// GenesisState defines the e2ee module's genesis state.
Expand Down
2 changes: 1 addition & 1 deletion proto/e2ee/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ message KeyRequest {

// KeyResponse is the response type for the Query/Key RPC method.
message KeyResponse {
bytes key = 1;
string key = 1;
}
2 changes: 1 addition & 1 deletion proto/e2ee/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ message MsgRegisterEncryptionKey {
option (cosmos.msg.v1.signer) = "address";

string address = 1;
bytes key = 2;
string key = 2;
}

// MsgRegisterEncryptionKeyResponse defines the Msg/RegisterEncryptionKey response type
Expand Down
4 changes: 2 additions & 2 deletions x/e2ee/autocli.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ func (am AppModule) AutoCLIOptions() *autocliv1.ModuleOptions {
RpcCommandOptions: []*autocliv1.RpcCommandOptions{
{
RpcMethod: "RegisterEncryptionKey",
Use: "set-encryption-key [key]",
Short: "Set encryption key is stored associated with the user address.",
Use: "register-encryption-key [key]",
Short: "Register encryption key stores an public key for asymmetric encryption with the user address.",
PositionalArgs: []*autocliv1.PositionalArgDescriptor{
{ProtoField: "key"},
},
Expand Down
18 changes: 18 additions & 0 deletions x/e2ee/client/cli/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cli

import "github.com/spf13/cobra"

func E2EECommand() *cobra.Command {
cmd := &cobra.Command{
Use: "e2ee",
Short: "End-to-end encryption commands",
}

cmd.AddCommand(
KeygenCommand(),
EncryptCommand(),
DecryptCommand(),
)

return cmd
}
108 changes: 108 additions & 0 deletions x/e2ee/client/cli/decrypt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package cli

import (
"fmt"
"io"
"os"

"filippo.io/age"
"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"

"github.com/crypto-org-chain/cronos/v2/x/e2ee/keyring"
"github.com/crypto-org-chain/cronos/v2/x/e2ee/types"
)

const FlagIdentity = "identity"

func DecryptCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "decrypt [input-file]",
Short: "Decrypt input file to local identity",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err

Check warning on line 28 in x/e2ee/client/cli/decrypt.go

View check run for this annotation

Codecov / codecov/patch

x/e2ee/client/cli/decrypt.go#L28

Added line #L28 was not covered by tests
}

kr, err := keyring.New("cronosd", clientCtx.Keyring.Backend(), clientCtx.HomeDir, os.Stdin)
if err != nil {
return err

Check warning on line 33 in x/e2ee/client/cli/decrypt.go

View check run for this annotation

Codecov / codecov/patch

x/e2ee/client/cli/decrypt.go#L33

Added line #L33 was not covered by tests
}

outputFile, err := cmd.Flags().GetString(flags.FlagOutput)
if err != nil {
return err

Check warning on line 38 in x/e2ee/client/cli/decrypt.go

View check run for this annotation

Codecov / codecov/patch

x/e2ee/client/cli/decrypt.go#L38

Added line #L38 was not covered by tests
}

identityNames, err := cmd.Flags().GetStringArray(FlagIdentity)
if err != nil {
return err

Check warning on line 43 in x/e2ee/client/cli/decrypt.go

View check run for this annotation

Codecov / codecov/patch

x/e2ee/client/cli/decrypt.go#L43

Added line #L43 was not covered by tests
}

if len(identityNames) == 0 {
return fmt.Errorf("no identity provided")

Check warning on line 47 in x/e2ee/client/cli/decrypt.go

View check run for this annotation

Codecov / codecov/patch

x/e2ee/client/cli/decrypt.go#L47

Added line #L47 was not covered by tests
}

identities := make([]age.Identity, len(identityNames))
for i, name := range identityNames {
secret, err := kr.Get(name)
if err != nil {
return err

Check warning on line 54 in x/e2ee/client/cli/decrypt.go

View check run for this annotation

Codecov / codecov/patch

x/e2ee/client/cli/decrypt.go#L54

Added line #L54 was not covered by tests
}

identity, err := age.ParseX25519Identity(string(secret))
if err != nil {
return err

Check warning on line 59 in x/e2ee/client/cli/decrypt.go

View check run for this annotation

Codecov / codecov/patch

x/e2ee/client/cli/decrypt.go#L59

Added line #L59 was not covered by tests
}

identities[i] = identity
}

var input io.Reader
inputFile := args[0]
if inputFile == "-" {
input = os.Stdin

Check warning on line 68 in x/e2ee/client/cli/decrypt.go

View check run for this annotation

Codecov / codecov/patch

x/e2ee/client/cli/decrypt.go#L68

Added line #L68 was not covered by tests
} else {
f, err := os.Open(inputFile)
if err != nil {
return err

Check warning on line 72 in x/e2ee/client/cli/decrypt.go

View check run for this annotation

Codecov / codecov/patch

x/e2ee/client/cli/decrypt.go#L72

Added line #L72 was not covered by tests
}
defer f.Close()
input = f
}

var output io.Writer
if outputFile == "-" {
output = os.Stdout
} else {
f, err := os.Create(outputFile)
if err != nil {
return err

Check warning on line 84 in x/e2ee/client/cli/decrypt.go

View check run for this annotation

Codecov / codecov/patch

x/e2ee/client/cli/decrypt.go#L82-L84

Added lines #L82 - L84 were not covered by tests
}
defer f.Close()
output = f

Check warning on line 87 in x/e2ee/client/cli/decrypt.go

View check run for this annotation

Codecov / codecov/patch

x/e2ee/client/cli/decrypt.go#L86-L87

Added lines #L86 - L87 were not covered by tests
}
return decrypt(identities, input, output)
},
}

cmd.Flags().StringArrayP(FlagIdentity, "i", []string{types.DefaultKeyringName}, "identity (can be repeated)")
cmd.Flags().StringP(flags.FlagOutput, "o", "-", "output file (default stdout)")

return cmd
}

func decrypt(identities []age.Identity, in io.Reader, out io.Writer) error {
r, err := age.Decrypt(in, identities...)
if err != nil {
return err

Check warning on line 102 in x/e2ee/client/cli/decrypt.go

View check run for this annotation

Codecov / codecov/patch

x/e2ee/client/cli/decrypt.go#L102

Added line #L102 was not covered by tests
}
if _, err := io.Copy(out, r); err != nil {
return err

Check warning on line 105 in x/e2ee/client/cli/decrypt.go

View check run for this annotation

Codecov / codecov/patch

x/e2ee/client/cli/decrypt.go#L105

Added line #L105 was not covered by tests
}
return nil
}
Loading
Loading