From 60025aacb00456d84f1f5c33847650e532321bfb Mon Sep 17 00:00:00 2001 From: yihuang Date: Wed, 10 Jul 2024 12:29:25 +0800 Subject: [PATCH] Problem: no command to show current pubkey in e2ee (#1513) * Problem: no command to show current pubkey in e2ee * test e2ee pubkey * Update CHANGELOG.md Signed-off-by: yihuang --------- Signed-off-by: yihuang --- CHANGELOG.md | 1 + integration_tests/cosmoscli.py | 11 ++++--- integration_tests/test_e2ee.py | 13 +++++---- x/e2ee/client/cli/cmd.go | 1 + x/e2ee/client/cli/pubkey.go | 53 ++++++++++++++++++++++++++++++++++ 5 files changed, 69 insertions(+), 10 deletions(-) create mode 100644 x/e2ee/client/cli/pubkey.go diff --git a/CHANGELOG.md b/CHANGELOG.md index ff1035da5c..6ca3b89e5b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * (store) [#1510](https://github.com/crypto-org-chain/cronos/pull/1510) Upgrade rocksdb to `v9.1.1`. * (store) [#1511](https://github.com/crypto-org-chain/cronos/pull/1511) Upgrade rocksdb to `v9.2.1`. +* (e2ee) [#1513](https://github.com/crypto-org-chain/cronos/pull/1513) Add pubkey subcommand to e2ee cli. *Jul 7, 2024* diff --git a/integration_tests/cosmoscli.py b/integration_tests/cosmoscli.py index fbe826681d..71a5b64688 100644 --- a/integration_tests/cosmoscli.py +++ b/integration_tests/cosmoscli.py @@ -1922,10 +1922,13 @@ def register_e2ee_key(self, key, **kwargs): rsp = self.event_query_tx_for(rsp["txhash"]) return rsp - def keygen(self, **kwargs): + def e2ee_keygen(self, **kwargs): return self.raw("e2ee", "keygen", home=self.data_dir, **kwargs).strip().decode() - def encrypt(self, input, *recipients, **kwargs): + def e2ee_pubkey(self, **kwargs): + return self.raw("e2ee", "pubkey", home=self.data_dir, **kwargs).strip().decode() + + def e2ee_encrypt(self, input, *recipients, **kwargs): return ( self.raw( "e2ee", @@ -1939,7 +1942,7 @@ def encrypt(self, input, *recipients, **kwargs): .decode() ) - def decrypt(self, input, identity="e2ee-identity", **kwargs): + def e2ee_decrypt(self, input, identity="e2ee-identity", **kwargs): return ( self.raw( "e2ee", @@ -1953,7 +1956,7 @@ def decrypt(self, input, identity="e2ee-identity", **kwargs): .decode() ) - def encrypt_to_validators(self, input, **kwargs): + def e2ee_encrypt_to_validators(self, input, **kwargs): return ( self.raw( "e2ee", diff --git a/integration_tests/test_e2ee.py b/integration_tests/test_e2ee.py index 5d1bca97fa..471299bd56 100644 --- a/integration_tests/test_e2ee.py +++ b/integration_tests/test_e2ee.py @@ -11,7 +11,7 @@ def test_register(cronos: Cronos): cli = cronos.cosmos_cli() - pubkey0 = cli.keygen(keyring_name="key0") + pubkey0 = cli.e2ee_keygen(keyring_name="key0") with pytest.raises(AssertionError) as exc: cli.register_e2ee_key(pubkey0 + "malformed", _from="validator") assert "malformed recipient" in str(exc.value) @@ -23,7 +23,8 @@ def gen_validator_identity(cronos: Cronos): cli = cronos.cosmos_cli(i) if cli.query_e2ee_key(cli.address("validator")): return - pubkey = cli.keygen() + pubkey = cli.e2ee_keygen() + assert cli.e2ee_pubkey() == pubkey cli.register_e2ee_key(pubkey, _from="validator") assert cli.query_e2ee_key(cli.address("validator")) == pubkey @@ -54,15 +55,15 @@ def test_encrypt_decrypt(cronos): plainfile = cli0.data_dir / "plaintext" plainfile.write_text(content) cipherfile = cli0.data_dir / "ciphertext" - cli0.encrypt( + cli0.e2ee_encrypt( plainfile, cli0.address("validator"), cli1.address("validator"), output=cipherfile, ) - assert cli0.decrypt(cipherfile) == content - assert cli1.decrypt(cipherfile) == content + assert cli0.e2ee_decrypt(cipherfile) == content + assert cli1.e2ee_decrypt(cipherfile) == content def encrypt_to_validators(cli, content): @@ -70,7 +71,7 @@ def encrypt_to_validators(cli, content): plainfile = cli.data_dir / "plaintext" plainfile.write_text(blocklist) cipherfile = cli.data_dir / "ciphertext" - cli.encrypt_to_validators(plainfile, output=cipherfile) + cli.e2ee_encrypt_to_validators(plainfile, output=cipherfile) rsp = cli.store_blocklist(cipherfile, _from="validator") assert rsp["code"] == 0, rsp["raw_log"] diff --git a/x/e2ee/client/cli/cmd.go b/x/e2ee/client/cli/cmd.go index 73f8d69bb2..5e88ce5287 100644 --- a/x/e2ee/client/cli/cmd.go +++ b/x/e2ee/client/cli/cmd.go @@ -13,6 +13,7 @@ func E2EECommand() *cobra.Command { EncryptCommand(), DecryptCommand(), EncryptToValidatorsCommand(), + PubKeyCommand(), ) return cmd diff --git a/x/e2ee/client/cli/pubkey.go b/x/e2ee/client/cli/pubkey.go new file mode 100644 index 0000000000..45900fceab --- /dev/null +++ b/x/e2ee/client/cli/pubkey.go @@ -0,0 +1,53 @@ +package cli + +import ( + "fmt" + "os" + + "filippo.io/age" + "github.com/cosmos/cosmos-sdk/client" + "github.com/crypto-org-chain/cronos/v2/x/e2ee/keyring" + "github.com/crypto-org-chain/cronos/v2/x/e2ee/types" + "github.com/spf13/cobra" +) + +func PubKeyCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "pubkey", + Short: "Show the recipient of current identity stored in keyring", + Args: cobra.NoArgs, + RunE: func(cmd *cobra.Command, args []string) error { + clientCtx, err := client.GetClientQueryContext(cmd) + if err != nil { + return err + } + + krName, err := cmd.Flags().GetString(FlagKeyringName) + if err != nil { + return err + } + + kr, err := keyring.New("cronosd", clientCtx.Keyring.Backend(), clientCtx.HomeDir, os.Stdin) + if err != nil { + return err + } + + bz, err := kr.Get(krName) + if err != nil { + return err + } + + k, err := age.ParseX25519Identity(string(bz)) + if err != nil { + return err + } + + fmt.Println(k.Recipient()) + return nil + }, + } + + cmd.Flags().String(FlagKeyringName, types.DefaultKeyringName, "The keyring name to use") + + return cmd +}