From c247238035fd846d298c151767930884bde5c54b Mon Sep 17 00:00:00 2001 From: mmsqe Date: Fri, 26 Apr 2024 18:39:08 +0800 Subject: [PATCH 1/6] Problem: no encrypt and decrypt cmds for message --- cmd/cronosd/cmd/decrypt.go | 100 +++++++++++++++++++++++++++++++++++ cmd/cronosd/cmd/encrypt.go | 103 +++++++++++++++++++++++++++++++++++++ cmd/cronosd/cmd/root.go | 2 + go.mod | 1 + go.sum | 4 ++ gomod2nix.toml | 3 ++ 6 files changed, 213 insertions(+) create mode 100644 cmd/cronosd/cmd/decrypt.go create mode 100644 cmd/cronosd/cmd/encrypt.go diff --git a/cmd/cronosd/cmd/decrypt.go b/cmd/cronosd/cmd/decrypt.go new file mode 100644 index 0000000000..44709a9ebb --- /dev/null +++ b/cmd/cronosd/cmd/decrypt.go @@ -0,0 +1,100 @@ +package cmd + +import ( + "bufio" + "fmt" + "io" + "os" + "strings" + + "filippo.io/age" + "github.com/spf13/cobra" +) + +const flagI = "i" + +func DecryptMsgCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "decrypt [input]", + Short: "Decrypt input messages to user", + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + inputs, err := cmd.Flags().GetStringArray(flagI) + if err != nil { + return err + } + var in io.Reader = os.Stdin + var out io.Writer = os.Stdout + if name := args[0]; name != "" && name != "-" { + f, err := os.Open(name) + if err != nil { + return err + } + defer f.Close() + in = f + } + return decrypt(inputs, in, out) + }, + } + cmd.Flags().StringArray(flagI, []string{}, "identity (can be repeated)") + return cmd +} + +func decrypt(inputs []string, in io.Reader, out io.Writer) error { + identities := []age.Identity{} + for _, input := range inputs { + ids, err := parseIdentitiesFile(input) + if err != nil { + return err + } + identities = append(identities, ids...) + } + r, err := age.Decrypt(in, identities...) + if err != nil { + return err + } + if _, err := io.Copy(out, r); err != nil { + return err + } + return nil +} + +func parseIdentitiesFile(name string) ([]age.Identity, error) { + f, err := os.Open(name) + if err != nil { + return nil, fmt.Errorf("failed to open file: %v", err) + } + defer f.Close() + b := bufio.NewReader(f) + ids, err := parseIdentities(b) + if err != nil { + return nil, fmt.Errorf("failed to read %q: %v", name, err) + } + return ids, nil +} + +func parseIdentities(f io.Reader) ([]age.Identity, error) { + const privateKeySizeLimit = 1 << 24 // 16 MiB + var ids []age.Identity + scanner := bufio.NewScanner(io.LimitReader(f, privateKeySizeLimit)) + var n int + for scanner.Scan() { + n++ + line := scanner.Text() + if strings.HasPrefix(line, "#") || line == "" { + continue + } + i, err := age.ParseX25519Identity(line) + if err != nil { + return nil, fmt.Errorf("error at line %d: %v", n, err) + } + ids = append(ids, i) + } + if err := scanner.Err(); err != nil { + return nil, fmt.Errorf("failed to read secret keys file: %v", err) + } + if len(ids) == 0 { + return nil, fmt.Errorf("no secret keys found") + } + return ids, nil +} diff --git a/cmd/cronosd/cmd/encrypt.go b/cmd/cronosd/cmd/encrypt.go new file mode 100644 index 0000000000..8ed8d3de82 --- /dev/null +++ b/cmd/cronosd/cmd/encrypt.go @@ -0,0 +1,103 @@ +package cmd + +import ( + "io" + "os" + + "filippo.io/age" + "github.com/spf13/cobra" +) + +const ( + flagR = "r" + flagO = "o" +) + +func EncryptMsgCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "encrypt [input]", + Short: "Encrypt input messages to multiple users", + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + f := cmd.Flags() + output, err := f.GetString(flagO) + if err != nil { + return err + } + recs, err := f.GetStringArray(flagR) + if err != nil { + return err + } + var in io.Reader = os.Stdin + var out io.Writer = os.Stdout + if name := args[0]; name != "" && name != "-" { + f, err := os.Open(name) + if err != nil { + return err + } + defer f.Close() + in = f + } + if name := output; name != "" && name != "-" { + f := newLazyOpener(name) + defer f.Close() + out = f + } + return encrypt(recs, in, out) + }, + } + f := cmd.Flags() + f.StringArray(flagR, []string{}, "recipient (can be repeated)") + f.StringArray(flagI, []string{}, "identity (can be repeated)") + f.String(flagO, "", "output to `FILE`") + return cmd +} + +func encrypt(recs []string, in io.Reader, out io.Writer) error { + var recipients []age.Recipient + for _, arg := range recs { + r, err := age.ParseX25519Recipient(arg) + if err != nil { + return err + } + recipients = append(recipients, r) + } + w, err := age.Encrypt(out, recipients...) + if err != nil { + return err + } + if _, err := io.Copy(w, in); err != nil { + return err + } + if err := w.Close(); err != nil { + return err + } + return nil +} + +type lazyOpener struct { + name string + f *os.File + err error +} + +func newLazyOpener(name string) io.WriteCloser { + return &lazyOpener{name: name} +} + +func (l *lazyOpener) Write(p []byte) (n int, err error) { + if l.f == nil && l.err == nil { + l.f, l.err = os.Create(l.name) + } + if l.err != nil { + return 0, l.err + } + return l.f.Write(p) +} + +func (l *lazyOpener) Close() error { + if l.f != nil { + return l.f.Close() + } + return nil +} diff --git a/cmd/cronosd/cmd/root.go b/cmd/cronosd/cmd/root.go index 18c64c2392..33fd75809f 100644 --- a/cmd/cronosd/cmd/root.go +++ b/cmd/cronosd/cmd/root.go @@ -189,6 +189,8 @@ func initRootCmd( queryCommand(), txCommand(), ethermintclient.KeyCommands(app.DefaultNodeHome), + EncryptMsgCommand(), + DecryptMsgCommand(), ) // add rosetta diff --git a/go.mod b/go.mod index ecc3544869..386256dddb 100644 --- a/go.mod +++ b/go.mod @@ -17,6 +17,7 @@ require ( cosmossdk.io/x/evidence v0.1.0 cosmossdk.io/x/feegrant v0.1.0 cosmossdk.io/x/upgrade v0.1.1 + filippo.io/age v1.1.2-0.20240110114017-29b68c20fc24 github.com/cometbft/cometbft v0.38.7-0.20240412124004-1f67e396cf45 github.com/cosmos/cosmos-db v1.0.3-0.20240408151834-e75f6e4b28d8 github.com/cosmos/cosmos-proto v1.0.0-beta.4 diff --git a/go.sum b/go.sum index dafe0d1901..ece56a94c8 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +c2sp.org/CCTV/age v0.0.0-20221230231406-5ea85644bd03 h1:0e2QjhWG02SgzlUOvNYaFraf04OBsUPOLxf+K+Ae/yM= +c2sp.org/CCTV/age v0.0.0-20221230231406-5ea85644bd03/go.mod h1:FomMrUJ2Lxt5jCLmZkG3FHa72zUprnhd3v/Z18Snm4w= cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSRU= @@ -211,6 +213,8 @@ cosmossdk.io/x/feegrant v0.1.0/go.mod h1:4r+FsViJRpcZif/yhTn+E0E6OFfg4n0Lx+6cCtn cosmossdk.io/x/upgrade v0.1.1 h1:aoPe2gNvH+Gwt/Pgq3dOxxQVU3j5P6Xf+DaUJTDZATc= cosmossdk.io/x/upgrade v0.1.1/go.mod h1:MNLptLPcIFK9CWt7Ra//8WUZAxweyRDNcbs5nkOcQy0= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= +filippo.io/age v1.1.2-0.20240110114017-29b68c20fc24 h1:vQIe2pCVvdZjX8OtZjbJ33nBKPjTnmy0zbdJxRjhH3w= +filippo.io/age v1.1.2-0.20240110114017-29b68c20fc24/go.mod h1:y3Zb/i2jHg/kL8xc3ocrI0Wd0Vm+VWV6DKfsKzSGUmU= filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 h1:/vQbFIOMbk2FiG/kXiLl8BRyzTWDw7gX/Hz7Dd5eDMs= diff --git a/gomod2nix.toml b/gomod2nix.toml index 97955bd296..ba935d9e77 100644 --- a/gomod2nix.toml +++ b/gomod2nix.toml @@ -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.2-0.20240110114017-29b68c20fc24" + hash = "sha256-LZ6W2ZNQyA1s6SAY5rXOhztgF0pmCKQR7B+73V1tB4Q=" [mod."filippo.io/edwards25519"] version = "v1.1.0" hash = "sha256-9ACANrgWZSd5HYPfDZHY8DVbPSC9LOMgy8deq3rDOoc=" From cb569b829cf482b25522561c4182f7d0748f6612 Mon Sep 17 00:00:00 2001 From: mmsqe Date: Fri, 26 Apr 2024 18:39:13 +0800 Subject: [PATCH 2/6] fix doc --- CHANGELOG.md | 3 ++- client/docs/config.json | 7 +------ 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c8613f9153..f93b47e527 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,7 +21,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. +* [#](https://github.com/crypto-org-chain/cronos/pull/) Add encrypt and decrypt cmds for message. *April 8, 2024* diff --git a/client/docs/config.json b/client/docs/config.json index dfa434bdd8..9c76549202 100644 --- a/client/docs/config.json +++ b/client/docs/config.json @@ -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", From 94186734a2d8a3a4f71e059786ba2ac4fdc0f7fe Mon Sep 17 00:00:00 2001 From: mmsqe Date: Fri, 26 Apr 2024 18:39:19 +0800 Subject: [PATCH 3/6] add gen --- cmd/cronosd/cmd/generate.go | 50 +++++++++++++++++++++++++++++++++++++ cmd/cronosd/cmd/root.go | 1 + 2 files changed, 51 insertions(+) create mode 100644 cmd/cronosd/cmd/generate.go diff --git a/cmd/cronosd/cmd/generate.go b/cmd/cronosd/cmd/generate.go new file mode 100644 index 0000000000..12c05d66e6 --- /dev/null +++ b/cmd/cronosd/cmd/generate.go @@ -0,0 +1,50 @@ +package cmd + +import ( + "fmt" + "os" + "time" + + "filippo.io/age" + "github.com/spf13/cobra" +) + +func KeygenCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "keygen", + Short: "Generates a new native X25519 key pair", + Args: cobra.NoArgs, + RunE: func(cmd *cobra.Command, args []string) error { + f := cmd.Flags() + output, err := f.GetString(flagO) + if err != nil { + return err + } + out := os.Stdout + if output != "" { + f, err := os.OpenFile(output, os.O_WRONLY|os.O_CREATE|os.O_EXCL, os.ModePerm) + if err != nil { + return err + } + defer f.Close() + out = f + } + return generate(out) + }, + } + cmd.Flags().String(flagO, "", "output to `FILE`") + return cmd +} + +func generate(out *os.File) error { + k, err := age.GenerateX25519Identity() + if err != nil { + return err + } + pubkey := k.Recipient() + fmt.Fprintf(out, "# created: %s\n", time.Now().Format(time.RFC3339)) + fmt.Fprintf(out, "# public key: %s\n", pubkey) + fmt.Fprintf(out, "%s\n", k) + fmt.Println(pubkey) + return nil +} diff --git a/cmd/cronosd/cmd/root.go b/cmd/cronosd/cmd/root.go index 33fd75809f..2e2dc03f52 100644 --- a/cmd/cronosd/cmd/root.go +++ b/cmd/cronosd/cmd/root.go @@ -189,6 +189,7 @@ func initRootCmd( queryCommand(), txCommand(), ethermintclient.KeyCommands(app.DefaultNodeHome), + KeygenCommand(), EncryptMsgCommand(), DecryptMsgCommand(), ) From a1a173765da1f3a97d2e6d18b687b94d25208dc9 Mon Sep 17 00:00:00 2001 From: mmsqe Date: Fri, 26 Apr 2024 18:47:37 +0800 Subject: [PATCH 4/6] test --- integration_tests/cosmoscli.py | 14 ++++++++++++++ integration_tests/test_e2ee.py | 17 +++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/integration_tests/cosmoscli.py b/integration_tests/cosmoscli.py index 06f459590a..6b3fc6df64 100644 --- a/integration_tests/cosmoscli.py +++ b/integration_tests/cosmoscli.py @@ -1870,3 +1870,17 @@ 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("keygen", home=self.data_dir, **kwargs).strip().decode() + + def encrypt(self, input, receipts, **kwargs): + return self.raw( + "encrypt", + input, + *[val for a in [["--r", val] for val in receipts] for val in a], + **kwargs, + ) + + def decrypt(self, input, **kwargs): + return self.raw("decrypt", input, home=self.data_dir, **kwargs).strip().decode() diff --git a/integration_tests/test_e2ee.py b/integration_tests/test_e2ee.py index 9b20796d56..1f41b590e0 100644 --- a/integration_tests/test_e2ee.py +++ b/integration_tests/test_e2ee.py @@ -1,4 +1,5 @@ import base64 +import tempfile def test_set_key(cronos): @@ -8,3 +9,19 @@ def test_set_key(cronos): adr = cli.address("community") p = cli.query_e2ee_key(adr) assert p["key"] == key + + +def test_encrypt_decrypt(cronos): + cli = cronos.cosmos_cli() + key_dir = tempfile.mkdtemp(dir=cronos.base_dir) + key0 = f"{key_dir}/key0" + key1 = f"{key_dir}/key1" + pubkey0 = cli.keygen(o=key0) + pubkey1 = cli.keygen(o=key1) + 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) + assert cli.decrypt(decrypted, i=key0) == cli.decrypt(decrypted, i=key1) == data From 64eea096945eb375f0a0e7bc5265bd5cdb5f3a29 Mon Sep 17 00:00:00 2001 From: mmsqe Date: Mon, 29 Apr 2024 09:31:36 +0800 Subject: [PATCH 5/6] cleanup --- CHANGELOG.md | 2 +- cmd/cronosd/cmd/decrypt.go | 6 +++++- cmd/cronosd/cmd/encrypt.go | 14 +++++++++++--- cmd/cronosd/cmd/generate.go | 8 +++++--- 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f93b47e527..db687770aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,7 +22,7 @@ ### Features * [#1406](https://github.com/crypto-org-chain/cronos/pull/1406) Add set-encryption-key for encryption module. -* [#](https://github.com/crypto-org-chain/cronos/pull/) Add encrypt and decrypt cmds for message. +* [#1411](https://github.com/crypto-org-chain/cronos/pull/1411) Add encrypt and decrypt cmds for message. *April 8, 2024* diff --git a/cmd/cronosd/cmd/decrypt.go b/cmd/cronosd/cmd/decrypt.go index 44709a9ebb..1fef3960ea 100644 --- a/cmd/cronosd/cmd/decrypt.go +++ b/cmd/cronosd/cmd/decrypt.go @@ -30,7 +30,11 @@ func DecryptMsgCommand() *cobra.Command { if err != nil { return err } - defer f.Close() + defer func() { + if err := f.Close(); err != nil { + fmt.Println("file close error", err) + } + }() in = f } return decrypt(inputs, in, out) diff --git a/cmd/cronosd/cmd/encrypt.go b/cmd/cronosd/cmd/encrypt.go index 8ed8d3de82..dd20884b5f 100644 --- a/cmd/cronosd/cmd/encrypt.go +++ b/cmd/cronosd/cmd/encrypt.go @@ -1,6 +1,7 @@ package cmd import ( + "fmt" "io" "os" @@ -35,12 +36,20 @@ func EncryptMsgCommand() *cobra.Command { if err != nil { return err } - defer f.Close() + defer func() { + if err := f.Close(); err != nil { + fmt.Println("file close error", err) + } + }() in = f } if name := output; name != "" && name != "-" { f := newLazyOpener(name) - defer f.Close() + defer func() { + if err := f.Close(); err != nil { + fmt.Println("file close error", err) + } + }() out = f } return encrypt(recs, in, out) @@ -48,7 +57,6 @@ func EncryptMsgCommand() *cobra.Command { } f := cmd.Flags() f.StringArray(flagR, []string{}, "recipient (can be repeated)") - f.StringArray(flagI, []string{}, "identity (can be repeated)") f.String(flagO, "", "output to `FILE`") return cmd } diff --git a/cmd/cronosd/cmd/generate.go b/cmd/cronosd/cmd/generate.go index 12c05d66e6..4cbcb3af56 100644 --- a/cmd/cronosd/cmd/generate.go +++ b/cmd/cronosd/cmd/generate.go @@ -3,7 +3,6 @@ package cmd import ( "fmt" "os" - "time" "filippo.io/age" "github.com/spf13/cobra" @@ -26,7 +25,11 @@ func KeygenCommand() *cobra.Command { if err != nil { return err } - defer f.Close() + defer func() { + if err := f.Close(); err != nil { + fmt.Println("file close error", err) + } + }() out = f } return generate(out) @@ -42,7 +45,6 @@ func generate(out *os.File) error { return err } pubkey := k.Recipient() - fmt.Fprintf(out, "# created: %s\n", time.Now().Format(time.RFC3339)) fmt.Fprintf(out, "# public key: %s\n", pubkey) fmt.Fprintf(out, "%s\n", k) fmt.Println(pubkey) From c3e60d184b2a136915553e90a61f14873b27612f Mon Sep 17 00:00:00 2001 From: yihuang Date: Mon, 29 Apr 2024 14:13:13 +0800 Subject: [PATCH 6/6] 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 fix lint --- cmd/cronosd/cmd/decrypt.go | 104 ------------------------------ cmd/cronosd/cmd/encrypt.go | 111 --------------------------------- cmd/cronosd/cmd/generate.go | 52 --------------- cmd/cronosd/cmd/root.go | 5 +- go.sum | 2 - gomod2nix.toml | 7 ++- integration_tests/cosmoscli.py | 42 +++++++++---- integration_tests/test_e2ee.py | 45 +++++++------ proto/e2ee/genesis.proto | 2 +- proto/e2ee/query.proto | 2 +- proto/e2ee/tx.proto | 2 +- x/e2ee/autocli.go | 4 +- x/e2ee/client/cli/cmd.go | 18 ++++++ x/e2ee/client/cli/decrypt.go | 108 ++++++++++++++++++++++++++++++++ x/e2ee/client/cli/encrypt.go | 104 ++++++++++++++++++++++++++++++ x/e2ee/client/cli/generate.go | 56 +++++++++++++++++ x/e2ee/keeper/keeper.go | 6 +- x/e2ee/module.go | 1 + x/e2ee/types/genesis.pb.go | 40 ++++++------ x/e2ee/types/keys.go | 6 +- x/e2ee/types/msg.go | 21 +++++++ x/e2ee/types/query.pb.go | 56 ++++++++--------- x/e2ee/types/tx.pb.go | 44 +++++++------ 23 files changed, 447 insertions(+), 391 deletions(-) delete mode 100644 cmd/cronosd/cmd/decrypt.go delete mode 100644 cmd/cronosd/cmd/encrypt.go delete mode 100644 cmd/cronosd/cmd/generate.go create mode 100644 x/e2ee/client/cli/cmd.go create mode 100644 x/e2ee/client/cli/decrypt.go create mode 100644 x/e2ee/client/cli/encrypt.go create mode 100644 x/e2ee/client/cli/generate.go create mode 100644 x/e2ee/types/msg.go diff --git a/cmd/cronosd/cmd/decrypt.go b/cmd/cronosd/cmd/decrypt.go deleted file mode 100644 index 1fef3960ea..0000000000 --- a/cmd/cronosd/cmd/decrypt.go +++ /dev/null @@ -1,104 +0,0 @@ -package cmd - -import ( - "bufio" - "fmt" - "io" - "os" - "strings" - - "filippo.io/age" - "github.com/spf13/cobra" -) - -const flagI = "i" - -func DecryptMsgCommand() *cobra.Command { - cmd := &cobra.Command{ - Use: "decrypt [input]", - Short: "Decrypt input messages to user", - Args: cobra.ExactArgs(1), - RunE: func(cmd *cobra.Command, args []string) error { - inputs, err := cmd.Flags().GetStringArray(flagI) - if err != nil { - return err - } - var in io.Reader = os.Stdin - var out io.Writer = os.Stdout - if name := args[0]; name != "" && name != "-" { - f, err := os.Open(name) - if err != nil { - return err - } - defer func() { - if err := f.Close(); err != nil { - fmt.Println("file close error", err) - } - }() - in = f - } - return decrypt(inputs, in, out) - }, - } - cmd.Flags().StringArray(flagI, []string{}, "identity (can be repeated)") - return cmd -} - -func decrypt(inputs []string, in io.Reader, out io.Writer) error { - identities := []age.Identity{} - for _, input := range inputs { - ids, err := parseIdentitiesFile(input) - if err != nil { - return err - } - identities = append(identities, ids...) - } - r, err := age.Decrypt(in, identities...) - if err != nil { - return err - } - if _, err := io.Copy(out, r); err != nil { - return err - } - return nil -} - -func parseIdentitiesFile(name string) ([]age.Identity, error) { - f, err := os.Open(name) - if err != nil { - return nil, fmt.Errorf("failed to open file: %v", err) - } - defer f.Close() - b := bufio.NewReader(f) - ids, err := parseIdentities(b) - if err != nil { - return nil, fmt.Errorf("failed to read %q: %v", name, err) - } - return ids, nil -} - -func parseIdentities(f io.Reader) ([]age.Identity, error) { - const privateKeySizeLimit = 1 << 24 // 16 MiB - var ids []age.Identity - scanner := bufio.NewScanner(io.LimitReader(f, privateKeySizeLimit)) - var n int - for scanner.Scan() { - n++ - line := scanner.Text() - if strings.HasPrefix(line, "#") || line == "" { - continue - } - i, err := age.ParseX25519Identity(line) - if err != nil { - return nil, fmt.Errorf("error at line %d: %v", n, err) - } - ids = append(ids, i) - } - if err := scanner.Err(); err != nil { - return nil, fmt.Errorf("failed to read secret keys file: %v", err) - } - if len(ids) == 0 { - return nil, fmt.Errorf("no secret keys found") - } - return ids, nil -} diff --git a/cmd/cronosd/cmd/encrypt.go b/cmd/cronosd/cmd/encrypt.go deleted file mode 100644 index dd20884b5f..0000000000 --- a/cmd/cronosd/cmd/encrypt.go +++ /dev/null @@ -1,111 +0,0 @@ -package cmd - -import ( - "fmt" - "io" - "os" - - "filippo.io/age" - "github.com/spf13/cobra" -) - -const ( - flagR = "r" - flagO = "o" -) - -func EncryptMsgCommand() *cobra.Command { - cmd := &cobra.Command{ - Use: "encrypt [input]", - Short: "Encrypt input messages to multiple users", - Args: cobra.ExactArgs(1), - RunE: func(cmd *cobra.Command, args []string) error { - f := cmd.Flags() - output, err := f.GetString(flagO) - if err != nil { - return err - } - recs, err := f.GetStringArray(flagR) - if err != nil { - return err - } - var in io.Reader = os.Stdin - var out io.Writer = os.Stdout - if name := args[0]; name != "" && name != "-" { - f, err := os.Open(name) - if err != nil { - return err - } - defer func() { - if err := f.Close(); err != nil { - fmt.Println("file close error", err) - } - }() - in = f - } - if name := output; name != "" && name != "-" { - f := newLazyOpener(name) - defer func() { - if err := f.Close(); err != nil { - fmt.Println("file close error", err) - } - }() - out = f - } - return encrypt(recs, in, out) - }, - } - f := cmd.Flags() - f.StringArray(flagR, []string{}, "recipient (can be repeated)") - f.String(flagO, "", "output to `FILE`") - return cmd -} - -func encrypt(recs []string, in io.Reader, out io.Writer) error { - var recipients []age.Recipient - for _, arg := range recs { - r, err := age.ParseX25519Recipient(arg) - if err != nil { - return err - } - recipients = append(recipients, r) - } - w, err := age.Encrypt(out, recipients...) - if err != nil { - return err - } - if _, err := io.Copy(w, in); err != nil { - return err - } - if err := w.Close(); err != nil { - return err - } - return nil -} - -type lazyOpener struct { - name string - f *os.File - err error -} - -func newLazyOpener(name string) io.WriteCloser { - return &lazyOpener{name: name} -} - -func (l *lazyOpener) Write(p []byte) (n int, err error) { - if l.f == nil && l.err == nil { - l.f, l.err = os.Create(l.name) - } - if l.err != nil { - return 0, l.err - } - return l.f.Write(p) -} - -func (l *lazyOpener) Close() error { - if l.f != nil { - return l.f.Close() - } - return nil -} diff --git a/cmd/cronosd/cmd/generate.go b/cmd/cronosd/cmd/generate.go deleted file mode 100644 index 4cbcb3af56..0000000000 --- a/cmd/cronosd/cmd/generate.go +++ /dev/null @@ -1,52 +0,0 @@ -package cmd - -import ( - "fmt" - "os" - - "filippo.io/age" - "github.com/spf13/cobra" -) - -func KeygenCommand() *cobra.Command { - cmd := &cobra.Command{ - Use: "keygen", - Short: "Generates a new native X25519 key pair", - Args: cobra.NoArgs, - RunE: func(cmd *cobra.Command, args []string) error { - f := cmd.Flags() - output, err := f.GetString(flagO) - if err != nil { - return err - } - out := os.Stdout - if output != "" { - f, err := os.OpenFile(output, os.O_WRONLY|os.O_CREATE|os.O_EXCL, os.ModePerm) - if err != nil { - return err - } - defer func() { - if err := f.Close(); err != nil { - fmt.Println("file close error", err) - } - }() - out = f - } - return generate(out) - }, - } - cmd.Flags().String(flagO, "", "output to `FILE`") - return cmd -} - -func generate(out *os.File) error { - k, err := age.GenerateX25519Identity() - if err != nil { - return err - } - pubkey := k.Recipient() - fmt.Fprintf(out, "# public key: %s\n", pubkey) - fmt.Fprintf(out, "%s\n", k) - fmt.Println(pubkey) - return nil -} diff --git a/cmd/cronosd/cmd/root.go b/cmd/cronosd/cmd/root.go index 2e2dc03f52..82beda0f3e 100644 --- a/cmd/cronosd/cmd/root.go +++ b/cmd/cronosd/cmd/root.go @@ -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 ) @@ -189,9 +190,7 @@ func initRootCmd( queryCommand(), txCommand(), ethermintclient.KeyCommands(app.DefaultNodeHome), - KeygenCommand(), - EncryptMsgCommand(), - DecryptMsgCommand(), + e2eecli.E2EECommand(), ) // add rosetta diff --git a/go.sum b/go.sum index 25d8d34772..1884d9987a 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,3 @@ -c2sp.org/CCTV/age v0.0.0-20221230231406-5ea85644bd03 h1:0e2QjhWG02SgzlUOvNYaFraf04OBsUPOLxf+K+Ae/yM= -c2sp.org/CCTV/age v0.0.0-20221230231406-5ea85644bd03/go.mod h1:FomMrUJ2Lxt5jCLmZkG3FHa72zUprnhd3v/Z18Snm4w= cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSRU= diff --git a/gomod2nix.toml b/gomod2nix.toml index 318bf1a94d..e032605394 100644 --- a/gomod2nix.toml +++ b/gomod2nix.toml @@ -65,8 +65,8 @@ schema = 3 version = "v0.1.1" hash = "sha256-bM9ybpaibMH7k4M6/QAXCZ3fJcADfJHxvMgp4AVUihs=" [mod."filippo.io/age"] - version = "v1.1.2-0.20240110114017-29b68c20fc24" - hash = "sha256-LZ6W2ZNQyA1s6SAY5rXOhztgF0pmCKQR7B+73V1tB4Q=" + version = "v1.1.1" + hash = "sha256-LRxxJQLQkzoCNYGS/XBixVmYXoZ1mPHKvFicPGXYLcw=" [mod."filippo.io/edwards25519"] version = "v1.1.0" hash = "sha256-9ACANrgWZSd5HYPfDZHY8DVbPSC9LOMgy8deq3rDOoc=" @@ -578,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=" diff --git a/integration_tests/cosmoscli.py b/integration_tests/cosmoscli.py index 6b3fc6df64..d2b9acaf5d 100644 --- a/integration_tests/cosmoscli.py +++ b/integration_tests/cosmoscli.py @@ -1,6 +1,7 @@ import binascii import enum import hashlib +import itertools import json import os import re @@ -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, @@ -1872,15 +1873,32 @@ def set_e2ee_key(self, key, **kwargs): return rsp def keygen(self, **kwargs): - return self.raw("keygen", home=self.data_dir, **kwargs).strip().decode() + return self.raw("e2ee", "keygen", home=self.data_dir, **kwargs).strip().decode() - def encrypt(self, input, receipts, **kwargs): - return self.raw( - "encrypt", - input, - *[val for a in [["--r", val] for val in receipts] for val in a], - **kwargs, + 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, **kwargs): - return self.raw("decrypt", input, 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() + ) diff --git a/integration_tests/test_e2ee.py b/integration_tests/test_e2ee.py index 1f41b590e0..99c1dc6c48 100644 --- a/integration_tests/test_e2ee.py +++ b/integration_tests/test_e2ee.py @@ -1,27 +1,26 @@ -import base64 -import tempfile +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, + ) -def test_encrypt_decrypt(cronos): - cli = cronos.cosmos_cli() - key_dir = tempfile.mkdtemp(dir=cronos.base_dir) - key0 = f"{key_dir}/key0" - key1 = f"{key_dir}/key1" - pubkey0 = cli.keygen(o=key0) - pubkey1 = cli.keygen(o=key1) - 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) - assert cli.decrypt(decrypted, i=key0) == cli.decrypt(decrypted, i=key1) == data + assert cli.decrypt(cipherfile, identity="key0") == content + assert cli.decrypt(cipherfile, identity="key1") == content diff --git a/proto/e2ee/genesis.proto b/proto/e2ee/genesis.proto index cf69a6b70c..12f4948149 100644 --- a/proto/e2ee/genesis.proto +++ b/proto/e2ee/genesis.proto @@ -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. diff --git a/proto/e2ee/query.proto b/proto/e2ee/query.proto index cccb307a10..b1789a1af8 100644 --- a/proto/e2ee/query.proto +++ b/proto/e2ee/query.proto @@ -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; } diff --git a/proto/e2ee/tx.proto b/proto/e2ee/tx.proto index 611e373472..358a45743a 100644 --- a/proto/e2ee/tx.proto +++ b/proto/e2ee/tx.proto @@ -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 diff --git a/x/e2ee/autocli.go b/x/e2ee/autocli.go index f83bedd8d0..9b9bb4904a 100644 --- a/x/e2ee/autocli.go +++ b/x/e2ee/autocli.go @@ -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"}, }, diff --git a/x/e2ee/client/cli/cmd.go b/x/e2ee/client/cli/cmd.go new file mode 100644 index 0000000000..e7358085a4 --- /dev/null +++ b/x/e2ee/client/cli/cmd.go @@ -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 +} diff --git a/x/e2ee/client/cli/decrypt.go b/x/e2ee/client/cli/decrypt.go new file mode 100644 index 0000000000..89f26f3298 --- /dev/null +++ b/x/e2ee/client/cli/decrypt.go @@ -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 +} diff --git a/x/e2ee/client/cli/encrypt.go b/x/e2ee/client/cli/encrypt.go new file mode 100644 index 0000000000..cd7b071fe2 --- /dev/null +++ b/x/e2ee/client/cli/encrypt.go @@ -0,0 +1,104 @@ +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" +) + +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 + } + + outputFile, err := cmd.Flags().GetString(flags.FlagOutput) + if err != nil { + return err + } + + recs, err := cmd.Flags().GetStringArray(FlagRecipient) + if err != nil { + return err + } + + // query encryption key from chain state + client := types.NewQueryClient(clientCtx) + 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 + } + recipient, err := age.ParseX25519Recipient(rsp.Key) + if err != nil { + return err + } + recipients[i] = recipient + } + + inputFile := args[0] + var input io.Reader + 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 { + fp, err := os.Create(outputFile) + if err != nil { + return err + } + defer fp.Close() + output = fp + } + return encrypt(recipients, input, output) + }, + } + 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 + } + + defer func() { + err = errors.Join(err, w.Close()) + }() + + _, err = io.Copy(w, in) + return +} diff --git a/x/e2ee/client/cli/generate.go b/x/e2ee/client/cli/generate.go new file mode 100644 index 0000000000..2d694d7088 --- /dev/null +++ b/x/e2ee/client/cli/generate.go @@ -0,0 +1,56 @@ +package cli + +import ( + "fmt" + "os" + + "filippo.io/age" + "github.com/spf13/cobra" + + "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" +) + +const FlagKeyringName = "keyring-name" + +func KeygenCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "keygen", + Short: "Generates a new native X25519 key pair", + 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 + } + + k, err := age.GenerateX25519Identity() + if err != nil { + return err + } + + if err := kr.Set(krName, []byte(k.String())); err != nil { + return err + } + + fmt.Println(k.Recipient()) + return nil + }, + } + + cmd.Flags().String(FlagKeyringName, types.DefaultKeyringName, "The keyring name to use") + + return cmd +} diff --git a/x/e2ee/keeper/keeper.go b/x/e2ee/keeper/keeper.go index de1c3acab3..84ccd5be5b 100644 --- a/x/e2ee/keeper/keeper.go +++ b/x/e2ee/keeper/keeper.go @@ -37,7 +37,7 @@ func (k Keeper) RegisterEncryptionKey( return nil, err } sdkCtx := sdk.UnwrapSDKContext(ctx) - sdkCtx.KVStore(k.storeKey).Set(types.KeyPrefix(bz), req.Key) + sdkCtx.KVStore(k.storeKey).Set(types.KeyPrefix(bz), []byte(req.Key)) return &types.MsgRegisterEncryptionKeyResponse{}, nil } @@ -70,7 +70,7 @@ func (k Keeper) ExportGenesis(ctx context.Context) (*types.GenesisState, error) key := iter.Value() keys = append(keys, types.EncryptionKeyEntry{ Address: address, - Key: key, + Key: string(key), }) } return &types.GenesisState{Keys: keys}, nil @@ -83,5 +83,5 @@ func (k Keeper) Key(ctx context.Context, req *types.KeyRequest) (*types.KeyRespo } sdkCtx := sdk.UnwrapSDKContext(ctx) value := sdkCtx.KVStore(k.storeKey).Get(types.KeyPrefix(bz)) - return &types.KeyResponse{Key: value}, nil + return &types.KeyResponse{Key: string(value)}, nil } diff --git a/x/e2ee/module.go b/x/e2ee/module.go index e2e33ccb42..c91f233d6e 100644 --- a/x/e2ee/module.go +++ b/x/e2ee/module.go @@ -24,6 +24,7 @@ var ( _ module.AppModule = AppModule{} _ module.AppModuleBasic = AppModuleBasic{} _ module.HasGenesisBasics = AppModuleBasic{} + _ module.HasName = AppModuleBasic{} // this line is used by starport scaffolding # ibc/module/interface ) diff --git a/x/e2ee/types/genesis.pb.go b/x/e2ee/types/genesis.pb.go index 28bd2085ee..69f52bc690 100644 --- a/x/e2ee/types/genesis.pb.go +++ b/x/e2ee/types/genesis.pb.go @@ -26,7 +26,7 @@ const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package // EncryptionKeyEntry is a type that contains the owner and the public key. type EncryptionKeyEntry struct { Address string `protobuf:"bytes,1,opt,name=address,proto3" json:"address,omitempty"` - Key []byte `protobuf:"bytes,2,opt,name=key,proto3" json:"key,omitempty"` + Key string `protobuf:"bytes,2,opt,name=key,proto3" json:"key,omitempty"` } func (m *EncryptionKeyEntry) Reset() { *m = EncryptionKeyEntry{} } @@ -69,11 +69,11 @@ func (m *EncryptionKeyEntry) GetAddress() string { return "" } -func (m *EncryptionKeyEntry) GetKey() []byte { +func (m *EncryptionKeyEntry) GetKey() string { if m != nil { return m.Key } - return nil + return "" } // GenesisState defines the e2ee module's genesis state. @@ -130,22 +130,22 @@ func init() { func init() { proto.RegisterFile("e2ee/genesis.proto", fileDescriptor_e81aee24edfec633) } var fileDescriptor_e81aee24edfec633 = []byte{ - // 239 bytes of a gzipped FileDescriptorProto + // 236 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x4a, 0x35, 0x4a, 0x4d, 0xd5, 0x4f, 0x4f, 0xcd, 0x4b, 0x2d, 0xce, 0x2c, 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0x01, 0x89, 0x49, 0x89, 0xa4, 0xe7, 0xa7, 0xe7, 0x83, 0x05, 0xf4, 0x41, 0x2c, 0x88, 0x9c, 0x92, 0x03, 0x97, 0x90, 0x6b, 0x5e, 0x72, 0x51, 0x65, 0x41, 0x49, 0x66, 0x7e, 0x9e, 0x77, 0x6a, 0xa5, 0x6b, 0x5e, 0x49, 0x51, 0xa5, 0x90, 0x04, 0x17, 0x7b, 0x62, 0x4a, 0x4a, 0x51, 0x6a, 0x71, 0xb1, 0x04, 0xa3, 0x02, 0xa3, 0x06, 0x67, 0x10, 0x8c, 0x2b, 0x24, 0xc0, 0xc5, 0x9c, 0x9d, 0x5a, 0x29, - 0xc1, 0xa4, 0xc0, 0xa8, 0xc1, 0x13, 0x04, 0x62, 0x2a, 0x39, 0x71, 0xf1, 0xb8, 0x43, 0xac, 0x0b, - 0x2e, 0x49, 0x2c, 0x49, 0x15, 0x32, 0xe2, 0x62, 0xc9, 0x4e, 0xad, 0x04, 0x69, 0x64, 0xd6, 0xe0, - 0x36, 0x92, 0xd0, 0x03, 0x59, 0xae, 0x87, 0x69, 0x87, 0x13, 0xcb, 0x89, 0x7b, 0xf2, 0x0c, 0x41, - 0x60, 0xb5, 0x4e, 0x3e, 0x27, 0x1e, 0xc9, 0x31, 0x5e, 0x78, 0x24, 0xc7, 0xf8, 0xe0, 0x91, 0x1c, - 0xe3, 0x84, 0xc7, 0x72, 0x0c, 0x17, 0x1e, 0xcb, 0x31, 0xdc, 0x78, 0x2c, 0xc7, 0x10, 0x65, 0x94, - 0x9e, 0x59, 0x92, 0x51, 0x9a, 0xa4, 0x97, 0x9c, 0x9f, 0xab, 0x0f, 0x36, 0x22, 0x5f, 0x37, 0xbf, - 0x28, 0x5d, 0x37, 0x39, 0x23, 0x31, 0x33, 0x4f, 0x3f, 0xb9, 0x28, 0x3f, 0x2f, 0xbf, 0x58, 0xbf, - 0xcc, 0x48, 0xbf, 0x42, 0x1f, 0xec, 0xef, 0x92, 0xca, 0x82, 0xd4, 0xe2, 0x24, 0x36, 0xb0, 0xd7, - 0x8c, 0x01, 0x01, 0x00, 0x00, 0xff, 0xff, 0x1b, 0xe5, 0x9f, 0x8e, 0x0c, 0x01, 0x00, 0x00, + 0xc1, 0x04, 0x16, 0x05, 0x31, 0x95, 0x9c, 0xb8, 0x78, 0xdc, 0x21, 0xd6, 0x05, 0x97, 0x24, 0x96, + 0xa4, 0x0a, 0x19, 0x71, 0xb1, 0x64, 0xa7, 0x56, 0x82, 0x34, 0x32, 0x6b, 0x70, 0x1b, 0x49, 0xe8, + 0x81, 0x2c, 0xd7, 0xc3, 0xb4, 0xc3, 0x89, 0xe5, 0xc4, 0x3d, 0x79, 0x86, 0x20, 0xb0, 0x5a, 0x27, + 0x9f, 0x13, 0x8f, 0xe4, 0x18, 0x2f, 0x3c, 0x92, 0x63, 0x7c, 0xf0, 0x48, 0x8e, 0x71, 0xc2, 0x63, + 0x39, 0x86, 0x0b, 0x8f, 0xe5, 0x18, 0x6e, 0x3c, 0x96, 0x63, 0x88, 0x32, 0x4a, 0xcf, 0x2c, 0xc9, + 0x28, 0x4d, 0xd2, 0x4b, 0xce, 0xcf, 0xd5, 0x07, 0x1b, 0x91, 0xaf, 0x9b, 0x5f, 0x94, 0xae, 0x9b, + 0x9c, 0x91, 0x98, 0x99, 0xa7, 0x9f, 0x5c, 0x94, 0x9f, 0x97, 0x5f, 0xac, 0x5f, 0x66, 0xa4, 0x5f, + 0xa1, 0x0f, 0xf6, 0x77, 0x49, 0x65, 0x41, 0x6a, 0x71, 0x12, 0x1b, 0xd8, 0x6b, 0xc6, 0x80, 0x00, + 0x00, 0x00, 0xff, 0xff, 0xf9, 0x3d, 0x79, 0x56, 0x0c, 0x01, 0x00, 0x00, } func (m *EncryptionKeyEntry) Marshal() (dAtA []byte, err error) { @@ -336,7 +336,7 @@ func (m *EncryptionKeyEntry) Unmarshal(dAtA []byte) error { if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field Key", wireType) } - var byteLen int + var stringLen uint64 for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowGenesis @@ -346,25 +346,23 @@ func (m *EncryptionKeyEntry) Unmarshal(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - byteLen |= int(b&0x7F) << shift + stringLen |= uint64(b&0x7F) << shift if b < 0x80 { break } } - if byteLen < 0 { + intStringLen := int(stringLen) + if intStringLen < 0 { return ErrInvalidLengthGenesis } - postIndex := iNdEx + byteLen + postIndex := iNdEx + intStringLen if postIndex < 0 { return ErrInvalidLengthGenesis } if postIndex > l { return io.ErrUnexpectedEOF } - m.Key = append(m.Key[:0], dAtA[iNdEx:postIndex]...) - if m.Key == nil { - m.Key = []byte{} - } + m.Key = string(dAtA[iNdEx:postIndex]) iNdEx = postIndex default: iNdEx = preIndex diff --git a/x/e2ee/types/keys.go b/x/e2ee/types/keys.go index c5744d7c7a..5941cbf264 100644 --- a/x/e2ee/types/keys.go +++ b/x/e2ee/types/keys.go @@ -17,6 +17,10 @@ const ( RouterKey = ModuleName ) +const ( + DefaultKeyringName = "e2ee-identity" +) + const ( prefixEncryptionKey = iota + 1 ) @@ -35,7 +39,7 @@ func (e EncryptionKeyEntry) Validate() error { if _, err := sdk.AccAddressFromBech32(e.Address); err != nil { return err } - if e.Key == nil { + if len(e.Key) == 0 { return errors.New("key can't be nil") } return nil diff --git a/x/e2ee/types/msg.go b/x/e2ee/types/msg.go new file mode 100644 index 0000000000..a18c0b0b4b --- /dev/null +++ b/x/e2ee/types/msg.go @@ -0,0 +1,21 @@ +package types + +import ( + fmt "fmt" + + sdk "github.com/cosmos/cosmos-sdk/types" +) + +func (m *MsgRegisterEncryptionKey) ValidateBasic() error { + if m.Address == "" { + return fmt.Errorf("address cannot be empty") + } + if len(m.Key) == 0 { + return fmt.Errorf("key cannot be nil") + } + // validate bech32 format of Address + if _, err := sdk.AccAddressFromBech32(m.Address); err != nil { + return fmt.Errorf("invalid address: %s", err) + } + return nil +} diff --git a/x/e2ee/types/query.pb.go b/x/e2ee/types/query.pb.go index 0505dcde42..4758e396e5 100644 --- a/x/e2ee/types/query.pb.go +++ b/x/e2ee/types/query.pb.go @@ -75,7 +75,7 @@ func (m *KeyRequest) GetAddress() string { // KeyResponse is the response type for the Query/Key RPC method. type KeyResponse struct { - Key []byte `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"` + Key string `protobuf:"bytes,1,opt,name=key,proto3" json:"key,omitempty"` } func (m *KeyResponse) Reset() { *m = KeyResponse{} } @@ -111,11 +111,11 @@ func (m *KeyResponse) XXX_DiscardUnknown() { var xxx_messageInfo_KeyResponse proto.InternalMessageInfo -func (m *KeyResponse) GetKey() []byte { +func (m *KeyResponse) GetKey() string { if m != nil { return m.Key } - return nil + return "" } func init() { @@ -126,24 +126,24 @@ func init() { func init() { proto.RegisterFile("e2ee/query.proto", fileDescriptor_1e8b28e605d00558) } var fileDescriptor_1e8b28e605d00558 = []byte{ - // 264 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x4c, 0x8f, 0xbf, 0x4a, 0xc4, 0x40, - 0x10, 0xc6, 0x13, 0xcf, 0x3f, 0xb8, 0x5a, 0xc4, 0x2d, 0x24, 0x1c, 0xb2, 0x4a, 0x0a, 0xb1, 0xb9, - 0x0c, 0xc6, 0x37, 0xb0, 0xf4, 0x6c, 0x8c, 0x9d, 0x5d, 0x2e, 0x37, 0xe4, 0xc2, 0xe9, 0xce, 0xde, - 0xee, 0xe6, 0x70, 0x11, 0x1b, 0x9f, 0x40, 0xf0, 0xa5, 0x2c, 0x0f, 0x6c, 0x2c, 0x25, 0xf1, 0x41, - 0x24, 0x89, 0xe2, 0x75, 0x33, 0x1f, 0x1f, 0xbf, 0xdf, 0x0c, 0x0b, 0x30, 0x41, 0x84, 0x45, 0x85, - 0xda, 0xc5, 0x4a, 0x93, 0x25, 0xbe, 0xd9, 0x26, 0xc3, 0xa3, 0x82, 0xa8, 0xb8, 0x47, 0xc8, 0x54, - 0x09, 0x99, 0x94, 0x64, 0x33, 0x5b, 0x92, 0x34, 0x7d, 0x27, 0x3a, 0x65, 0x6c, 0x8c, 0x2e, 0xc5, - 0x45, 0x85, 0xc6, 0xf2, 0x90, 0xed, 0x64, 0xd3, 0xa9, 0x46, 0x63, 0x42, 0xff, 0xc4, 0x3f, 0xdb, - 0x4d, 0xff, 0xd6, 0xe8, 0x98, 0xed, 0x75, 0x3d, 0xa3, 0x48, 0x1a, 0xe4, 0x01, 0x1b, 0xcc, 0xd1, - 0x75, 0xa5, 0xfd, 0xb4, 0x1d, 0x93, 0x5b, 0xb6, 0x75, 0xd3, 0xba, 0xf9, 0x15, 0x1b, 0x8c, 0xd1, - 0xf1, 0x20, 0x6e, 0xed, 0xf1, 0x3f, 0x7c, 0x78, 0xb0, 0x96, 0xf4, 0x98, 0x48, 0xbc, 0x7c, 0x7c, - 0xbf, 0x6d, 0x84, 0xfc, 0x10, 0xba, 0xe3, 0x97, 0xe7, 0x30, 0x47, 0x07, 0x4f, 0xbf, 0xd2, 0xe7, - 0xcb, 0xeb, 0xf7, 0x5a, 0xf8, 0xab, 0x5a, 0xf8, 0x5f, 0xb5, 0xf0, 0x5f, 0x1b, 0xe1, 0xad, 0x1a, - 0xe1, 0x7d, 0x36, 0xc2, 0xbb, 0x4b, 0x8a, 0xd2, 0xce, 0xaa, 0x49, 0x9c, 0xd3, 0x03, 0xe4, 0xda, - 0x29, 0x4b, 0x23, 0xd2, 0xc5, 0x28, 0x9f, 0x65, 0xa5, 0x84, 0x5c, 0x93, 0x24, 0x03, 0xcb, 0x04, - 0x1e, 0x7b, 0xb0, 0x75, 0x0a, 0xcd, 0x64, 0xbb, 0x7b, 0xf9, 0xe2, 0x27, 0x00, 0x00, 0xff, 0xff, - 0x41, 0x96, 0x8b, 0x79, 0x2a, 0x01, 0x00, 0x00, + // 263 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x48, 0x35, 0x4a, 0x4d, + 0xd5, 0x2f, 0x2c, 0x4d, 0x2d, 0xaa, 0xd4, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0x01, 0x89, + 0x48, 0xc9, 0xa4, 0xe7, 0xe7, 0xa7, 0xe7, 0xa4, 0xea, 0x27, 0x16, 0x64, 0xea, 0x27, 0xe6, 0xe5, + 0xe5, 0x97, 0x24, 0x96, 0x64, 0xe6, 0xe7, 0x15, 0x43, 0xd4, 0x28, 0xa9, 0x71, 0x71, 0x79, 0xa7, + 0x56, 0x06, 0xa5, 0x16, 0x96, 0xa6, 0x16, 0x97, 0x08, 0x49, 0x70, 0xb1, 0x27, 0xa6, 0xa4, 0x14, + 0xa5, 0x16, 0x17, 0x4b, 0x30, 0x2a, 0x30, 0x6a, 0x70, 0x06, 0xc1, 0xb8, 0x4a, 0xf2, 0x5c, 0xdc, + 0x60, 0x75, 0xc5, 0x05, 0xf9, 0x79, 0xc5, 0xa9, 0x42, 0x02, 0x5c, 0xcc, 0xd9, 0xa9, 0x95, 0x50, + 0x45, 0x20, 0xa6, 0x51, 0x30, 0x17, 0x6b, 0x20, 0xc8, 0x6e, 0x21, 0x2f, 0x2e, 0x66, 0xef, 0xd4, + 0x4a, 0x21, 0x01, 0x3d, 0x90, 0xed, 0x7a, 0x08, 0xc3, 0xa5, 0x04, 0x91, 0x44, 0x20, 0xc6, 0x28, + 0xc9, 0x35, 0x5d, 0x7e, 0x32, 0x99, 0x49, 0x42, 0x48, 0x4c, 0x1f, 0xec, 0xf8, 0x32, 0x43, 0xfd, + 0xec, 0xd4, 0x4a, 0xfd, 0x6a, 0xa8, 0xa5, 0xb5, 0x4e, 0x3e, 0x27, 0x1e, 0xc9, 0x31, 0x5e, 0x78, + 0x24, 0xc7, 0xf8, 0xe0, 0x91, 0x1c, 0xe3, 0x84, 0xc7, 0x72, 0x0c, 0x17, 0x1e, 0xcb, 0x31, 0xdc, + 0x78, 0x2c, 0xc7, 0x10, 0x65, 0x94, 0x9e, 0x59, 0x92, 0x51, 0x9a, 0xa4, 0x97, 0x9c, 0x9f, 0xab, + 0x9f, 0x5c, 0x54, 0x59, 0x50, 0x92, 0xaf, 0x9b, 0x5f, 0x94, 0xae, 0x9b, 0x9c, 0x91, 0x98, 0x99, + 0xa7, 0x9f, 0x5c, 0x94, 0x9f, 0x97, 0x5f, 0xac, 0x5f, 0x66, 0xa4, 0x5f, 0x01, 0x31, 0xb8, 0xa4, + 0xb2, 0x20, 0xb5, 0x38, 0x89, 0x0d, 0xec, 0x65, 0x63, 0x40, 0x00, 0x00, 0x00, 0xff, 0xff, 0x5c, + 0x7b, 0x0b, 0x3a, 0x2a, 0x01, 0x00, 0x00, } // Reference imports to suppress errors if they are not otherwise used. @@ -446,7 +446,7 @@ func (m *KeyResponse) Unmarshal(dAtA []byte) error { if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field Key", wireType) } - var byteLen int + var stringLen uint64 for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowQuery @@ -456,25 +456,23 @@ func (m *KeyResponse) Unmarshal(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - byteLen |= int(b&0x7F) << shift + stringLen |= uint64(b&0x7F) << shift if b < 0x80 { break } } - if byteLen < 0 { + intStringLen := int(stringLen) + if intStringLen < 0 { return ErrInvalidLengthQuery } - postIndex := iNdEx + byteLen + postIndex := iNdEx + intStringLen if postIndex < 0 { return ErrInvalidLengthQuery } if postIndex > l { return io.ErrUnexpectedEOF } - m.Key = append(m.Key[:0], dAtA[iNdEx:postIndex]...) - if m.Key == nil { - m.Key = []byte{} - } + m.Key = string(dAtA[iNdEx:postIndex]) iNdEx = postIndex default: iNdEx = preIndex diff --git a/x/e2ee/types/tx.pb.go b/x/e2ee/types/tx.pb.go index 9a7cc7d9ce..938a155bca 100644 --- a/x/e2ee/types/tx.pb.go +++ b/x/e2ee/types/tx.pb.go @@ -31,7 +31,7 @@ const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package // MsgRegisterEncryptionKey defines the Msg/RegisterEncryptionKey request type type MsgRegisterEncryptionKey struct { Address string `protobuf:"bytes,1,opt,name=address,proto3" json:"address,omitempty"` - Key []byte `protobuf:"bytes,2,opt,name=key,proto3" json:"key,omitempty"` + Key string `protobuf:"bytes,2,opt,name=key,proto3" json:"key,omitempty"` } func (m *MsgRegisterEncryptionKey) Reset() { *m = MsgRegisterEncryptionKey{} } @@ -74,11 +74,11 @@ func (m *MsgRegisterEncryptionKey) GetAddress() string { return "" } -func (m *MsgRegisterEncryptionKey) GetKey() []byte { +func (m *MsgRegisterEncryptionKey) GetKey() string { if m != nil { return m.Key } - return nil + return "" } // MsgRegisterEncryptionKeyResponse defines the Msg/RegisterEncryptionKey response type @@ -126,24 +126,24 @@ func init() { func init() { proto.RegisterFile("e2ee/tx.proto", fileDescriptor_85e46bdbb1c358a8) } var fileDescriptor_85e46bdbb1c358a8 = []byte{ - // 260 bytes of a gzipped FileDescriptorProto + // 257 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0x4d, 0x35, 0x4a, 0x4d, 0xd5, 0x2f, 0xa9, 0xd0, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0x01, 0x71, 0xa5, 0xc4, 0x93, 0xf3, 0x8b, 0x73, 0xf3, 0x8b, 0xf5, 0x73, 0x8b, 0xd3, 0xf5, 0xcb, 0x0c, 0x41, 0x14, 0x44, 0x5a, 0x29, 0x84, 0x4b, 0xc2, 0xb7, 0x38, 0x3d, 0x28, 0x35, 0x3d, 0xb3, 0xb8, 0x24, 0xb5, 0xc8, 0x35, 0x2f, 0xb9, 0xa8, 0xb2, 0xa0, 0x24, 0x33, 0x3f, 0xcf, 0x3b, 0xb5, 0x52, 0x48, 0x82, 0x8b, 0x3d, 0x31, 0x25, 0xa5, 0x28, 0xb5, 0xb8, 0x58, 0x82, 0x51, 0x81, 0x51, 0x83, 0x33, 0x08, 0xc6, 0x15, - 0x12, 0xe0, 0x62, 0xce, 0x4e, 0xad, 0x94, 0x60, 0x52, 0x60, 0xd4, 0xe0, 0x09, 0x02, 0x31, 0xad, - 0x78, 0x9a, 0x9e, 0x6f, 0xd0, 0x82, 0xc9, 0x2b, 0x29, 0x71, 0x29, 0xe0, 0x32, 0x35, 0x28, 0xb5, - 0xb8, 0x20, 0x3f, 0xaf, 0x38, 0xd5, 0x28, 0x97, 0x8b, 0xd9, 0xb7, 0x38, 0x5d, 0x28, 0x9e, 0x4b, - 0x14, 0xbb, 0xed, 0x72, 0x7a, 0x20, 0x97, 0xeb, 0xe1, 0x32, 0x47, 0x4a, 0x0d, 0xbf, 0x3c, 0xcc, - 0x1e, 0x29, 0xd6, 0x86, 0xe7, 0x1b, 0xb4, 0x18, 0x9d, 0x7c, 0x4e, 0x3c, 0x92, 0x63, 0xbc, 0xf0, - 0x48, 0x8e, 0xf1, 0xc1, 0x23, 0x39, 0xc6, 0x09, 0x8f, 0xe5, 0x18, 0x2e, 0x3c, 0x96, 0x63, 0xb8, - 0xf1, 0x58, 0x8e, 0x21, 0xca, 0x28, 0x3d, 0xb3, 0x24, 0xa3, 0x34, 0x49, 0x2f, 0x39, 0x3f, 0x57, - 0x1f, 0x6c, 0x40, 0xbe, 0x6e, 0x7e, 0x51, 0xba, 0x6e, 0x72, 0x46, 0x62, 0x66, 0x9e, 0x7e, 0x72, - 0x51, 0x7e, 0x5e, 0x7e, 0xb1, 0x7e, 0x99, 0x91, 0x7e, 0x85, 0x3e, 0x24, 0x60, 0x2b, 0x0b, 0x52, - 0x8b, 0x93, 0xd8, 0xc0, 0xa1, 0x67, 0x0c, 0x08, 0x00, 0x00, 0xff, 0xff, 0x38, 0xff, 0xe3, 0x1c, - 0x6d, 0x01, 0x00, 0x00, + 0x12, 0xe0, 0x62, 0xce, 0x4e, 0xad, 0x94, 0x60, 0x02, 0x8b, 0x82, 0x98, 0x56, 0x3c, 0x4d, 0xcf, + 0x37, 0x68, 0xc1, 0xe4, 0x95, 0x94, 0xb8, 0x14, 0x70, 0x99, 0x1a, 0x94, 0x5a, 0x5c, 0x90, 0x9f, + 0x57, 0x9c, 0x6a, 0x94, 0xcb, 0xc5, 0xec, 0x5b, 0x9c, 0x2e, 0x14, 0xcf, 0x25, 0x8a, 0xdd, 0x76, + 0x39, 0x3d, 0x90, 0xcb, 0xf5, 0x70, 0x99, 0x23, 0xa5, 0x86, 0x5f, 0x1e, 0x66, 0x8f, 0x14, 0x6b, + 0xc3, 0xf3, 0x0d, 0x5a, 0x8c, 0x4e, 0x3e, 0x27, 0x1e, 0xc9, 0x31, 0x5e, 0x78, 0x24, 0xc7, 0xf8, + 0xe0, 0x91, 0x1c, 0xe3, 0x84, 0xc7, 0x72, 0x0c, 0x17, 0x1e, 0xcb, 0x31, 0xdc, 0x78, 0x2c, 0xc7, + 0x10, 0x65, 0x94, 0x9e, 0x59, 0x92, 0x51, 0x9a, 0xa4, 0x97, 0x9c, 0x9f, 0xab, 0x0f, 0x36, 0x20, + 0x5f, 0x37, 0xbf, 0x28, 0x5d, 0x37, 0x39, 0x23, 0x31, 0x33, 0x4f, 0x3f, 0xb9, 0x28, 0x3f, 0x2f, + 0xbf, 0x58, 0xbf, 0xcc, 0x48, 0xbf, 0x42, 0x1f, 0x12, 0xb0, 0x95, 0x05, 0xa9, 0xc5, 0x49, 0x6c, + 0xe0, 0xd0, 0x33, 0x06, 0x04, 0x00, 0x00, 0xff, 0xff, 0xc8, 0x1e, 0x5a, 0x2c, 0x6d, 0x01, 0x00, + 0x00, } // Reference imports to suppress errors if they are not otherwise used. @@ -396,7 +396,7 @@ func (m *MsgRegisterEncryptionKey) Unmarshal(dAtA []byte) error { if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field Key", wireType) } - var byteLen int + var stringLen uint64 for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowTx @@ -406,25 +406,23 @@ func (m *MsgRegisterEncryptionKey) Unmarshal(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - byteLen |= int(b&0x7F) << shift + stringLen |= uint64(b&0x7F) << shift if b < 0x80 { break } } - if byteLen < 0 { + intStringLen := int(stringLen) + if intStringLen < 0 { return ErrInvalidLengthTx } - postIndex := iNdEx + byteLen + postIndex := iNdEx + intStringLen if postIndex < 0 { return ErrInvalidLengthTx } if postIndex > l { return io.ErrUnexpectedEOF } - m.Key = append(m.Key[:0], dAtA[iNdEx:postIndex]...) - if m.Key == nil { - m.Key = []byte{} - } + m.Key = string(dAtA[iNdEx:postIndex]) iNdEx = postIndex default: iNdEx = preIndex