Skip to content

Commit

Permalink
Problem: no command to show current pubkey in e2ee (#1513)
Browse files Browse the repository at this point in the history
* Problem: no command to show current pubkey in e2ee

* test e2ee pubkey

* Update CHANGELOG.md

Signed-off-by: yihuang <[email protected]>

---------

Signed-off-by: yihuang <[email protected]>
  • Loading branch information
yihuang authored Jul 10, 2024
1 parent ec2dc9e commit 60025aa
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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*

Expand Down
11 changes: 7 additions & 4 deletions integration_tests/cosmoscli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand Down
13 changes: 7 additions & 6 deletions integration_tests/test_e2ee.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down Expand Up @@ -54,23 +55,23 @@ 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):
blocklist = json.dumps(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"]

Expand Down
1 change: 1 addition & 0 deletions x/e2ee/client/cli/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func E2EECommand() *cobra.Command {
EncryptCommand(),
DecryptCommand(),
EncryptToValidatorsCommand(),
PubKeyCommand(),
)

return cmd
Expand Down
53 changes: 53 additions & 0 deletions x/e2ee/client/cli/pubkey.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 60025aa

Please sign in to comment.