-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Problem: no encrypt and decrypt cmds for message (#1411)
* Problem: no encrypt and decrypt cmds for message * fix doc * add gen * test * cleanup * move command to e2ee module move encrypt cmd to e2ee module move decrypt cmd to e2ee update integration test store key as string, to make autocli better fix integration test Update x/e2ee/client/cli/encrypt.go Signed-off-by: yihuang <[email protected]> fix lint --------- Signed-off-by: yihuang <[email protected]> Co-authored-by: yihuang <[email protected]> Co-authored-by: yihuang <[email protected]>
- Loading branch information
1 parent
36a6b02
commit 87efbec
Showing
21 changed files
with
458 additions
and
100 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
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 |
---|---|---|
@@ -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 |
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
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 @@ | ||
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 | ||
} |
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,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 | ||
} | ||
|
||
kr, err := keyring.New("cronosd", clientCtx.Keyring.Backend(), clientCtx.HomeDir, os.Stdin) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
outputFile, err := cmd.Flags().GetString(flags.FlagOutput) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
identityNames, err := cmd.Flags().GetStringArray(FlagIdentity) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(identityNames) == 0 { | ||
return fmt.Errorf("no identity provided") | ||
} | ||
|
||
identities := make([]age.Identity, len(identityNames)) | ||
for i, name := range identityNames { | ||
secret, err := kr.Get(name) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
identity, err := age.ParseX25519Identity(string(secret)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
identities[i] = identity | ||
} | ||
|
||
var input io.Reader | ||
inputFile := args[0] | ||
if inputFile == "-" { | ||
input = os.Stdin | ||
} else { | ||
f, err := os.Open(inputFile) | ||
if err != nil { | ||
return err | ||
} | ||
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 | ||
} | ||
defer f.Close() | ||
output = f | ||
} | ||
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 | ||
} | ||
if _, err := io.Copy(out, r); err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
Oops, something went wrong.