From dbef3638f71b687a6889cec8d3ccf968c52d73f2 Mon Sep 17 00:00:00 2001 From: Jacob Kim Date: Tue, 8 Nov 2022 18:37:21 +0900 Subject: [PATCH] refactor: remove client/keys.KeysCdc --- CHANGELOG.md | 1 + client/keys/add.go | 3 +- client/keys/codec.go | 27 ----------- client/keys/codec_test.go | 97 --------------------------------------- client/keys/parse.go | 3 +- client/keys/utils.go | 6 +-- 6 files changed, 8 insertions(+), 129 deletions(-) delete mode 100644 client/keys/codec.go delete mode 100644 client/keys/codec_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ece425deeb..405ef6ab4a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -85,6 +85,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * [#13236](https://github.com/cosmos/cosmos-sdk/pull/13236) Integrate Filter Logging * [#13528](https://github.com/cosmos/cosmos-sdk/pull/13528) Update `ValidateMemoDecorator` to only check memo against `MaxMemoCharacters` param when a memo is present. * [#13651](https://github.com/cosmos/cosmos-sdk/pull/13651) Update `server/config/config.GetConfig` function. +* [#13781](https://github.com/cosmos/cosmos-sdk/pull/13781) Remove `client/keys.KeysCdc`. ### State Machine Breaking diff --git a/client/keys/add.go b/client/keys/add.go index 62bdc67dbab..72a5e7cd08f 100644 --- a/client/keys/add.go +++ b/client/keys/add.go @@ -3,6 +3,7 @@ package keys import ( "bufio" "bytes" + "encoding/json" "errors" "fmt" "sort" @@ -313,7 +314,7 @@ func printCreate(cmd *cobra.Command, k *keyring.Record, showMnemonic bool, mnemo out.Mnemonic = mnemonic } - jsonString, err := KeysCdc.MarshalJSON(out) + jsonString, err := json.Marshal(out) if err != nil { return err } diff --git a/client/keys/codec.go b/client/keys/codec.go deleted file mode 100644 index 3b1f6fc38b3..00000000000 --- a/client/keys/codec.go +++ /dev/null @@ -1,27 +0,0 @@ -package keys - -import ( - "github.com/cosmos/cosmos-sdk/codec" - cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" -) - -// TODO: remove this file https://github.com/cosmos/cosmos-sdk/issues/8047 - -// KeysCdc defines codec to be used with key operations -var KeysCdc *codec.LegacyAmino - -func init() { - KeysCdc = codec.NewLegacyAmino() - cryptocodec.RegisterCrypto(KeysCdc) - KeysCdc.Seal() -} - -// marshal keys -func MarshalJSON(o interface{}) ([]byte, error) { - return KeysCdc.MarshalJSON(o) -} - -// unmarshal json -func UnmarshalJSON(bz []byte, ptr interface{}) error { - return KeysCdc.UnmarshalJSON(bz, ptr) -} diff --git a/client/keys/codec_test.go b/client/keys/codec_test.go deleted file mode 100644 index f61792e600d..00000000000 --- a/client/keys/codec_test.go +++ /dev/null @@ -1,97 +0,0 @@ -package keys_test - -import ( - "bytes" - "testing" - - "github.com/stretchr/testify/require" - - "github.com/cosmos/cosmos-sdk/client/keys" - "github.com/cosmos/cosmos-sdk/crypto/keyring" -) - -type testCases struct { - Keys []keyring.KeyOutput - Answers []keyring.KeyOutput - JSON [][]byte -} - -func getTestCases() testCases { - return testCases{ - // nolint:govet - []keyring.KeyOutput{ - {"A", "B", "C", "D", "E"}, - {"A", "B", "C", "D", ""}, - {"", "B", "C", "D", ""}, - {"", "", "", "", ""}, - }, - make([]keyring.KeyOutput, 4), - [][]byte{ - []byte(`{"name":"A","type":"B","address":"C","pubkey":"D","mnemonic":"E"}`), - []byte(`{"name":"A","type":"B","address":"C","pubkey":"D"}`), - []byte(`{"name":"","type":"B","address":"C","pubkey":"D"}`), - []byte(`{"name":"","type":"","address":"","pubkey":""}`), - }, - } -} - -func TestMarshalJSON(t *testing.T) { - type args struct { - o keyring.KeyOutput - } - - data := getTestCases() - - tests := []struct { - name string - args args - want []byte - wantErr bool - }{ - {"basic", args{data.Keys[0]}, data.JSON[0], false}, - {"mnemonic is optional", args{data.Keys[1]}, data.JSON[1], false}, - - // REVIEW: Are the next results expected?? - {"empty name", args{data.Keys[2]}, data.JSON[2], false}, - {"empty object", args{data.Keys[3]}, data.JSON[3], false}, - } - for _, tt := range tests { - tt := tt - t.Run(tt.name, func(t *testing.T) { - got, err := keys.MarshalJSON(tt.args.o) - require.Equal(t, tt.wantErr, err != nil) - require.True(t, bytes.Equal(got, tt.want)) - }) - } -} - -func TestUnmarshalJSON(t *testing.T) { - type args struct { - bz []byte - ptr interface{} - } - - data := getTestCases() - - tests := []struct { - name string - args args - wantErr bool - }{ - {"basic", args{data.JSON[0], &data.Answers[0]}, false}, - {"mnemonic is optional", args{data.JSON[1], &data.Answers[1]}, false}, - - // REVIEW: Are the next results expected?? - {"empty name", args{data.JSON[2], &data.Answers[2]}, false}, - {"empty object", args{data.JSON[3], &data.Answers[3]}, false}, - } - for idx, tt := range tests { - idx, tt := idx, tt - t.Run(tt.name, func(t *testing.T) { - err := keys.UnmarshalJSON(tt.args.bz, tt.args.ptr) - require.Equal(t, tt.wantErr, err != nil) - // Confirm deserialized objects are the same - require.Equal(t, data.Keys[idx], data.Answers[idx]) - }) - } -} diff --git a/client/keys/parse.go b/client/keys/parse.go index ba3d3c64b14..b616c5dcdd0 100644 --- a/client/keys/parse.go +++ b/client/keys/parse.go @@ -2,6 +2,7 @@ package keys import ( "encoding/hex" + "encoding/json" "errors" "fmt" "io" @@ -140,7 +141,7 @@ func displayParseKeyInfo(w io.Writer, stringer fmt.Stringer, output string) { out, err = yaml.Marshal(&stringer) case OutputFormatJSON: - out, err = KeysCdc.MarshalJSON(stringer) + out, err = json.Marshal(&stringer) } if err != nil { diff --git a/client/keys/utils.go b/client/keys/utils.go index 114e6315c9a..a5d8460c282 100644 --- a/client/keys/utils.go +++ b/client/keys/utils.go @@ -1,6 +1,7 @@ package keys import ( + "encoding/json" "fmt" "io" @@ -30,7 +31,7 @@ func printKeyringRecord(w io.Writer, k *cryptokeyring.Record, bechKeyOut bechKey } case OutputFormatJSON: - out, err := KeysCdc.MarshalJSON(ko) + out, err := json.Marshal(ko) if err != nil { return err } @@ -57,8 +58,7 @@ func printKeyringRecords(w io.Writer, records []*cryptokeyring.Record, output st case OutputFormatJSON: // TODO https://github.com/cosmos/cosmos-sdk/issues/8046 - // Replace AminoCdc with Proto JSON - out, err := KeysCdc.MarshalJSON(kos) + out, err := json.Marshal(kos) if err != nil { return err }