Skip to content

Commit

Permalink
move encrypt cmd to e2ee module
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang committed Apr 29, 2024
1 parent 38b6923 commit 92d9d0f
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 115 deletions.
111 changes: 0 additions & 111 deletions cmd/cronosd/cmd/encrypt.go

This file was deleted.

1 change: 0 additions & 1 deletion cmd/cronosd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,6 @@ func initRootCmd(
txCommand(),
ethermintclient.KeyCommands(app.DefaultNodeHome),
e2eecli.E2EECommand(),
EncryptMsgCommand(),
DecryptMsgCommand(),
)

Expand Down
6 changes: 4 additions & 2 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 @@ -1874,11 +1875,12 @@ def set_e2ee_key(self, key, **kwargs):
def keygen(self, **kwargs):
return self.raw("e2ee", "keygen", home=self.data_dir, **kwargs).strip().decode()

def encrypt(self, input, receipts, **kwargs):
def encrypt(self, input, *recipients, **kwargs):
return self.raw(
"e2ee",
"encrypt",
input,
*[val for a in [["--r", val] for val in receipts] for val in a],
*itertools.chain.from_iterable(("-r", r) for r in recipients),
**kwargs,
)

Expand Down
4 changes: 3 additions & 1 deletion integration_tests/test_e2ee.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ def test_encrypt_decrypt(cronos):
key1 = f"{key_dir}/key1"
pubkey0 = cli.keygen(o=key0)

Check failure on line 19 in integration_tests/test_e2ee.py

View workflow job for this annotation

GitHub Actions / integration_tests (unmarked)

test_encrypt_decrypt[True] AssertionError: Error: unknown flag: --o Usage: cronosd e2ee keygen [flags] Flags: -h, --help help for keygen --keyring-name string The keyring name to use (default "e2ee-identity") Global Flags: --home string directory for config and data (default "/home/runner/.cronos") --log_format string The logging format (json|plain) (default "plain") --log_level string The logging level (trace|debug|info|warn|error|fatal|panic|disabled or '*:<level>,<key>:<level>') (default "info") --log_no_color Disable colored logs --trace print out full stack trace on errors (cronosd e2ee keygen --home /tmp/pytest-of-runner/pytest-0/indexer0/cronos_777-1/node0 --o /tmp/pytest-of-runner/pytest-0/indexer0/cronos_777-1/tmp56cygzo_/key0)
pubkey1 = cli.keygen(o=key1)
# TODO
# save pubkey on chain
input = f"{key_dir}/input"
decrypted = f"{key_dir}/input.age"
data = "Hello, World!"
with open(input, "w") as file:
file.write(data)
cli.encrypt(input, [pubkey0, pubkey1], o=decrypted)
cli.encrypt(input, pubkey0, pubkey1, output=decrypted)
assert cli.decrypt(decrypted, i=key0) == cli.decrypt(decrypted, i=key1) == data
1 change: 1 addition & 0 deletions x/e2ee/client/cli/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ func E2EECommand() *cobra.Command {

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

return cmd
Expand Down
105 changes: 105 additions & 0 deletions x/e2ee/client/cli/encrypt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package cli

import (
"errors"
"io"
"os"

"filippo.io/age"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/spf13/cobra"

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

const (
FlagRecipient = "recipient"
flagO = "o"
)

func EncryptCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "encrypt [input-file]",
Short: "Encrypt input file to one or multiple recipients",
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 29 in x/e2ee/client/cli/encrypt.go

View check run for this annotation

Codecov / codecov/patch

x/e2ee/client/cli/encrypt.go#L27-L29

Added lines #L27 - L29 were not covered by tests
}

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

Check warning on line 34 in x/e2ee/client/cli/encrypt.go

View check run for this annotation

Codecov / codecov/patch

x/e2ee/client/cli/encrypt.go#L32-L34

Added lines #L32 - L34 were not covered by tests
}

recs, err := cmd.Flags().GetStringArray(FlagRecipient)
if err != nil {
return err

Check warning on line 39 in x/e2ee/client/cli/encrypt.go

View check run for this annotation

Codecov / codecov/patch

x/e2ee/client/cli/encrypt.go#L37-L39

Added lines #L37 - L39 were not covered by tests
}

// query encryption key from chain state
client := types.NewQueryClient(clientCtx.GRPCClient)
recipients := make([]age.Recipient, len(recs))
for i, rec := range recs {
rsp, err := client.Key(clientCtx.CmdContext, &types.KeyRequest{
Address: rec,
})
if err != nil {
return err

Check warning on line 50 in x/e2ee/client/cli/encrypt.go

View check run for this annotation

Codecov / codecov/patch

x/e2ee/client/cli/encrypt.go#L43-L50

Added lines #L43 - L50 were not covered by tests
}
recipient, err := age.ParseX25519Recipient(string(rsp.Key))
if err != nil {
return err

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

View check run for this annotation

Codecov / codecov/patch

x/e2ee/client/cli/encrypt.go#L52-L54

Added lines #L52 - L54 were not covered by tests
}
recipients[i] = recipient

Check warning on line 56 in x/e2ee/client/cli/encrypt.go

View check run for this annotation

Codecov / codecov/patch

x/e2ee/client/cli/encrypt.go#L56

Added line #L56 was not covered by tests
}

inputFile := args[0]
var input io.Reader
if inputFile == "-" {
input = os.Stdin
} else {
f, err := os.Open(inputFile)
if err != nil {
return err

Check warning on line 66 in x/e2ee/client/cli/encrypt.go

View check run for this annotation

Codecov / codecov/patch

x/e2ee/client/cli/encrypt.go#L59-L66

Added lines #L59 - L66 were not covered by tests
}
defer f.Close()
input = f

Check warning on line 69 in x/e2ee/client/cli/encrypt.go

View check run for this annotation

Codecov / codecov/patch

x/e2ee/client/cli/encrypt.go#L68-L69

Added lines #L68 - L69 were not covered by tests
}

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

Check warning on line 78 in x/e2ee/client/cli/encrypt.go

View check run for this annotation

Codecov / codecov/patch

x/e2ee/client/cli/encrypt.go#L72-L78

Added lines #L72 - L78 were not covered by tests
}
defer fp.Close()
output = fp

Check warning on line 81 in x/e2ee/client/cli/encrypt.go

View check run for this annotation

Codecov / codecov/patch

x/e2ee/client/cli/encrypt.go#L80-L81

Added lines #L80 - L81 were not covered by tests
}
return encrypt(recipients, input, output)

Check warning on line 83 in x/e2ee/client/cli/encrypt.go

View check run for this annotation

Codecov / codecov/patch

x/e2ee/client/cli/encrypt.go#L83

Added line #L83 was not covered by tests
},
}
f := cmd.Flags()
f.StringArrayP(FlagRecipient, "r", []string{}, "recipients")
f.StringP(flags.FlagOutput, "o", "-", "output file (default stdout)")
return cmd
}

func encrypt(recipients []age.Recipient, in io.Reader, out io.Writer) (err error) {
var w io.WriteCloser
w, err = age.Encrypt(out, recipients...)
if err != nil {
return err

Check warning on line 96 in x/e2ee/client/cli/encrypt.go

View check run for this annotation

Codecov / codecov/patch

x/e2ee/client/cli/encrypt.go#L92-L96

Added lines #L92 - L96 were not covered by tests
}

defer func() {
err = errors.Join(err, w.Close())
}()

Check warning on line 101 in x/e2ee/client/cli/encrypt.go

View check run for this annotation

Codecov / codecov/patch

x/e2ee/client/cli/encrypt.go#L99-L101

Added lines #L99 - L101 were not covered by tests

_, err = io.Copy(w, in)
return

Check warning on line 104 in x/e2ee/client/cli/encrypt.go

View check run for this annotation

Codecov / codecov/patch

x/e2ee/client/cli/encrypt.go#L103-L104

Added lines #L103 - L104 were not covered by tests
}

0 comments on commit 92d9d0f

Please sign in to comment.