From affed11328344d4cb5e5f723bd5de88334ebd4db Mon Sep 17 00:00:00 2001 From: chengwenxi Date: Fri, 13 Nov 2020 21:50:03 +0800 Subject: [PATCH 1/5] simple fix --- crypto/keyring/codec.go | 13 +++++++++++++ crypto/keyring/keyring.go | 7 +++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/crypto/keyring/codec.go b/crypto/keyring/codec.go index 6e6a254c87e3..1022571c4428 100644 --- a/crypto/keyring/codec.go +++ b/crypto/keyring/codec.go @@ -4,6 +4,7 @@ import ( "github.com/cosmos/cosmos-sdk/codec" cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" "github.com/cosmos/cosmos-sdk/crypto/hd" + "github.com/tendermint/tendermint/crypto" ) // CryptoCdc defines the codec required for keys and info @@ -25,3 +26,15 @@ func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { cdc.RegisterConcrete(offlineInfo{}, "crypto/keys/offlineInfo", nil) cdc.RegisterConcrete(multiInfo{}, "crypto/keys/multiInfo", nil) } + +// PrivKeyFromBytes unmarshals private key bytes and returns a PrivKey +func PrivKeyFromBytes(privKeyBytes []byte) (privKey crypto.PrivKey, err error) { + err = CryptoCdc.UnmarshalBinaryBare(privKeyBytes, &privKey) + return +} + +// PubKeyFromBytes unmarshals public key bytes and returns a PubKey +func PubKeyFromBytes(pubKeyBytes []byte) (pubKey crypto.PubKey, err error) { + err = CryptoCdc.UnmarshalBinaryBare(pubKeyBytes, &pubKey) + return +} diff --git a/crypto/keyring/keyring.go b/crypto/keyring/keyring.go index 0d0e41b7c3da..9f9ad79fb3ce 100644 --- a/crypto/keyring/keyring.go +++ b/crypto/keyring/keyring.go @@ -19,7 +19,6 @@ import ( "github.com/cosmos/cosmos-sdk/client/input" "github.com/cosmos/cosmos-sdk/crypto" - cryptoamino "github.com/cosmos/cosmos-sdk/crypto/codec" "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/crypto/ledger" sdk "github.com/cosmos/cosmos-sdk/types" @@ -239,7 +238,7 @@ func (ks keystore) ExportPrivateKeyObject(uid string) (tmcrypto.PrivKey, error) return nil, err } - priv, err = cryptoamino.PrivKeyFromBytes([]byte(linfo.PrivKeyArmor)) + priv, err = PrivKeyFromBytes([]byte(linfo.PrivKeyArmor)) if err != nil { return nil, err } @@ -288,7 +287,7 @@ func (ks keystore) ImportPubKey(uid string, armor string) error { return err } - pubKey, err := cryptoamino.PubKeyFromBytes(pubBytes) + pubKey, err := PubKeyFromBytes(pubBytes) if err != nil { return err } @@ -315,7 +314,7 @@ func (ks keystore) Sign(uid string, msg []byte) ([]byte, tmcrypto.PubKey, error) return nil, nil, fmt.Errorf("private key not available") } - priv, err = cryptoamino.PrivKeyFromBytes([]byte(i.PrivKeyArmor)) + priv, err = PrivKeyFromBytes([]byte(i.PrivKeyArmor)) if err != nil { return nil, nil, err } From 800f0933bed13ac94c81747f6f8c49cf1dcae52a Mon Sep 17 00:00:00 2001 From: chengwenxi Date: Tue, 17 Nov 2020 11:27:15 +0800 Subject: [PATCH 2/5] refactor crypto --- crypto/{ => keyring}/armor.go | 8 +++--- crypto/{ => keyring}/armor_test.go | 43 +++++++++++++++--------------- crypto/keyring/keyring.go | 9 +++---- crypto/keyring/keyring_test.go | 3 +-- crypto/keyring/legacy.go | 11 ++++---- 5 files changed, 34 insertions(+), 40 deletions(-) rename crypto/{ => keyring}/armor.go (96%) rename crypto/{ => keyring}/armor_test.go (78%) diff --git a/crypto/armor.go b/crypto/keyring/armor.go similarity index 96% rename from crypto/armor.go rename to crypto/keyring/armor.go index d0637c709d13..7d99f782c5db 100644 --- a/crypto/armor.go +++ b/crypto/keyring/armor.go @@ -1,4 +1,4 @@ -package crypto +package keyring import ( "encoding/hex" @@ -9,8 +9,6 @@ import ( "github.com/tendermint/tendermint/crypto/armor" "github.com/tendermint/tendermint/crypto/xsalsa20symmetric" - "github.com/cosmos/cosmos-sdk/codec/legacy" - cryptoAmino "github.com/cosmos/cosmos-sdk/crypto/codec" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" ) @@ -152,7 +150,7 @@ func encryptPrivKey(privKey crypto.PrivKey, passphrase string) (saltBytes []byte } key = crypto.Sha256(key) // get 32 bytes - privKeyBytes := legacy.Cdc.Amino.MustMarshalBinaryBare(privKey) + privKeyBytes := CryptoCdc.MustMarshalBinaryBare(privKey) return saltBytes, xsalsa20symmetric.EncryptSymmetric(privKeyBytes, key) } @@ -205,5 +203,5 @@ func decryptPrivKey(saltBytes []byte, encBytes []byte, passphrase string) (privK return privKey, err } - return cryptoAmino.PrivKeyFromBytes(privKeyBytes) + return PrivKeyFromBytes(privKeyBytes) } diff --git a/crypto/armor_test.go b/crypto/keyring/armor_test.go similarity index 78% rename from crypto/armor_test.go rename to crypto/keyring/armor_test.go index 3e5f256faa75..3d3a1765a2ed 100644 --- a/crypto/armor_test.go +++ b/crypto/keyring/armor_test.go @@ -1,4 +1,4 @@ -package crypto_test +package keyring_test import ( "bytes" @@ -14,7 +14,6 @@ import ( "github.com/tendermint/tendermint/crypto/xsalsa20symmetric" "github.com/cosmos/cosmos-sdk/codec/legacy" - "github.com/cosmos/cosmos-sdk/crypto" cryptoAmino "github.com/cosmos/cosmos-sdk/crypto/codec" "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/crypto/keyring" @@ -24,31 +23,31 @@ import ( func TestArmorUnarmorPrivKey(t *testing.T) { priv := secp256k1.GenPrivKey() - armored := crypto.EncryptArmorPrivKey(priv, "passphrase", "") - _, _, err := crypto.UnarmorDecryptPrivKey(armored, "wrongpassphrase") + armored := keyring.EncryptArmorPrivKey(priv, "passphrase", "") + _, _, err := keyring.UnarmorDecryptPrivKey(armored, "wrongpassphrase") require.Error(t, err) - decrypted, algo, err := crypto.UnarmorDecryptPrivKey(armored, "passphrase") + decrypted, algo, err := keyring.UnarmorDecryptPrivKey(armored, "passphrase") require.NoError(t, err) require.Equal(t, string(hd.Secp256k1Type), algo) require.True(t, priv.Equals(decrypted)) // empty string - decrypted, algo, err = crypto.UnarmorDecryptPrivKey("", "passphrase") + decrypted, algo, err = keyring.UnarmorDecryptPrivKey("", "passphrase") require.Error(t, err) require.True(t, errors.Is(io.EOF, err)) require.Nil(t, decrypted) require.Empty(t, algo) // wrong key type - armored = crypto.ArmorPubKeyBytes(priv.PubKey().Bytes(), "") - _, _, err = crypto.UnarmorDecryptPrivKey(armored, "passphrase") + armored = keyring.ArmorPubKeyBytes(priv.PubKey().Bytes(), "") + _, _, err = keyring.UnarmorDecryptPrivKey(armored, "passphrase") require.Error(t, err) require.Contains(t, err.Error(), "unrecognized armor type") // armor key manually encryptPrivKeyFn := func(privKey tmcrypto.PrivKey, passphrase string) (saltBytes []byte, encBytes []byte) { saltBytes = tmcrypto.CRandBytes(16) - key, err := bcrypt.GenerateFromPassword(saltBytes, []byte(passphrase), crypto.BcryptSecurityParameter) + key, err := bcrypt.GenerateFromPassword(saltBytes, []byte(passphrase), keyring.BcryptSecurityParameter) require.NoError(t, err) key = tmcrypto.Sha256(key) // get 32 bytes privKeyBytes := legacy.Cdc.Amino.MustMarshalBinaryBare(privKey) @@ -63,7 +62,7 @@ func TestArmorUnarmorPrivKey(t *testing.T) { "type": "secp256k", } armored = armor.EncodeArmor("TENDERMINT PRIVATE KEY", headerWrongKdf, encBytes) - _, _, err = crypto.UnarmorDecryptPrivKey(armored, "passphrase") + _, _, err = keyring.UnarmorDecryptPrivKey(armored, "passphrase") require.Error(t, err) require.Equal(t, "unrecognized KDF type: wrong", err.Error()) } @@ -75,16 +74,16 @@ func TestArmorUnarmorPubKey(t *testing.T) { // Add keys and see they return in alphabetical order info, _, err := cstore.NewMnemonic("Bob", keyring.English, types.FullFundraiserPath, hd.Secp256k1) require.NoError(t, err) - armored := crypto.ArmorPubKeyBytes(legacy.Cdc.Amino.MustMarshalBinaryBare(info.GetPubKey()), "") - pubBytes, algo, err := crypto.UnarmorPubKeyBytes(armored) + armored := keyring.ArmorPubKeyBytes(legacy.Cdc.Amino.MustMarshalBinaryBare(info.GetPubKey()), "") + pubBytes, algo, err := keyring.UnarmorPubKeyBytes(armored) require.NoError(t, err) pub, err := cryptoAmino.PubKeyFromBytes(pubBytes) require.NoError(t, err) require.Equal(t, string(hd.Secp256k1Type), algo) require.True(t, pub.Equals(info.GetPubKey())) - armored = crypto.ArmorPubKeyBytes(legacy.Cdc.Amino.MustMarshalBinaryBare(info.GetPubKey()), "unknown") - pubBytes, algo, err = crypto.UnarmorPubKeyBytes(armored) + armored = keyring.ArmorPubKeyBytes(legacy.Cdc.Amino.MustMarshalBinaryBare(info.GetPubKey()), "unknown") + pubBytes, algo, err = keyring.UnarmorPubKeyBytes(armored) require.NoError(t, err) pub, err = cryptoAmino.PubKeyFromBytes(pubBytes) require.NoError(t, err) @@ -93,7 +92,7 @@ func TestArmorUnarmorPubKey(t *testing.T) { armored, err = cstore.ExportPrivKeyArmor("Bob", "passphrase") require.NoError(t, err) - _, _, err = crypto.UnarmorPubKeyBytes(armored) + _, _, err = keyring.UnarmorPubKeyBytes(armored) require.Error(t, err) require.Equal(t, `couldn't unarmor bytes: unrecognized armor type "TENDERMINT PRIVATE KEY", expected: "TENDERMINT PUBLIC KEY"`, err.Error()) @@ -103,7 +102,7 @@ func TestArmorUnarmorPubKey(t *testing.T) { "type": "unknown", } armored = armor.EncodeArmor("TENDERMINT PUBLIC KEY", header, pubBytes) - _, algo, err = crypto.UnarmorPubKeyBytes(armored) + _, algo, err = keyring.UnarmorPubKeyBytes(armored) require.NoError(t, err) // return secp256k1 if version is 0.0.0 require.Equal(t, "secp256k1", algo) @@ -113,7 +112,7 @@ func TestArmorUnarmorPubKey(t *testing.T) { "type": "unknown", } armored = armor.EncodeArmor("TENDERMINT PUBLIC KEY", header, pubBytes) - bz, algo, err := crypto.UnarmorPubKeyBytes(armored) + bz, algo, err := keyring.UnarmorPubKeyBytes(armored) require.Nil(t, bz) require.Empty(t, algo) require.Error(t, err) @@ -125,7 +124,7 @@ func TestArmorUnarmorPubKey(t *testing.T) { "version": "unknown", } armored = armor.EncodeArmor("TENDERMINT PUBLIC KEY", header, pubBytes) - bz, algo, err = crypto.UnarmorPubKeyBytes(armored) + bz, algo, err = keyring.UnarmorPubKeyBytes(armored) require.Nil(t, bz) require.Empty(t, algo) require.Error(t, err) @@ -134,14 +133,14 @@ func TestArmorUnarmorPubKey(t *testing.T) { func TestArmorInfoBytes(t *testing.T) { bs := []byte("test") - armoredString := crypto.ArmorInfoBytes(bs) - unarmoredBytes, err := crypto.UnarmorInfoBytes(armoredString) + armoredString := keyring.ArmorInfoBytes(bs) + unarmoredBytes, err := keyring.UnarmorInfoBytes(armoredString) require.NoError(t, err) require.True(t, bytes.Equal(bs, unarmoredBytes)) } func TestUnarmorInfoBytesErrors(t *testing.T) { - unarmoredBytes, err := crypto.UnarmorInfoBytes("") + unarmoredBytes, err := keyring.UnarmorInfoBytes("") require.Error(t, err) require.True(t, errors.Is(io.EOF, err)) require.Nil(t, unarmoredBytes) @@ -150,7 +149,7 @@ func TestUnarmorInfoBytesErrors(t *testing.T) { "type": "Info", "version": "0.0.1", } - unarmoredBytes, err = crypto.UnarmorInfoBytes(armor.EncodeArmor( + unarmoredBytes, err = keyring.UnarmorInfoBytes(armor.EncodeArmor( "TENDERMINT KEY INFO", header, []byte("plain-text"))) require.Error(t, err) require.Equal(t, "unrecognized version: 0.0.1", err.Error()) diff --git a/crypto/keyring/keyring.go b/crypto/keyring/keyring.go index 9f9ad79fb3ce..407c5d07a4b7 100644 --- a/crypto/keyring/keyring.go +++ b/crypto/keyring/keyring.go @@ -18,7 +18,6 @@ import ( tmcrypto "github.com/tendermint/tendermint/crypto" "github.com/cosmos/cosmos-sdk/client/input" - "github.com/cosmos/cosmos-sdk/crypto" "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/crypto/ledger" sdk "github.com/cosmos/cosmos-sdk/types" @@ -196,7 +195,7 @@ func (ks keystore) ExportPubKeyArmor(uid string) (string, error) { return "", fmt.Errorf("no key to export with name: %s", uid) } - return crypto.ArmorPubKeyBytes(CryptoCdc.MustMarshalBinaryBare(bz.GetPubKey()), string(bz.GetAlgo())), nil + return ArmorPubKeyBytes(CryptoCdc.MustMarshalBinaryBare(bz.GetPubKey()), string(bz.GetAlgo())), nil } func (ks keystore) ExportPubKeyArmorByAddress(address sdk.Address) (string, error) { @@ -219,7 +218,7 @@ func (ks keystore) ExportPrivKeyArmor(uid, encryptPassphrase string) (armor stri return "", err } - return crypto.EncryptArmorPrivKey(priv, encryptPassphrase, string(info.GetAlgo())), nil + return EncryptArmorPrivKey(priv, encryptPassphrase, string(info.GetAlgo())), nil } // ExportPrivateKeyObject exports an armored private key object. @@ -264,7 +263,7 @@ func (ks keystore) ImportPrivKey(uid, armor, passphrase string) error { return fmt.Errorf("cannot overwrite key: %s", uid) } - privKey, algo, err := crypto.UnarmorDecryptPrivKey(armor, passphrase) + privKey, algo, err := UnarmorDecryptPrivKey(armor, passphrase) if err != nil { return errors.Wrap(err, "failed to decrypt private key") } @@ -282,7 +281,7 @@ func (ks keystore) ImportPubKey(uid string, armor string) error { return fmt.Errorf("cannot overwrite key: %s", uid) } - pubBytes, algo, err := crypto.UnarmorPubKeyBytes(armor) + pubBytes, algo, err := UnarmorPubKeyBytes(armor) if err != nil { return err } diff --git a/crypto/keyring/keyring_test.go b/crypto/keyring/keyring_test.go index 0cd75c9b44dc..c6946b4313ba 100644 --- a/crypto/keyring/keyring_test.go +++ b/crypto/keyring/keyring_test.go @@ -10,7 +10,6 @@ import ( "github.com/stretchr/testify/require" tmcrypto "github.com/tendermint/tendermint/crypto" - "github.com/cosmos/cosmos-sdk/crypto" "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" "github.com/cosmos/cosmos-sdk/crypto/keys/multisig" @@ -25,7 +24,7 @@ const ( ) func init() { - crypto.BcryptSecurityParameter = 1 + BcryptSecurityParameter = 1 } func TestNewKeyring(t *testing.T) { diff --git a/crypto/keyring/legacy.go b/crypto/keyring/legacy.go index 2332266d4563..c8bdfd75f8ac 100644 --- a/crypto/keyring/legacy.go +++ b/crypto/keyring/legacy.go @@ -10,7 +10,6 @@ import ( tmos "github.com/tendermint/tendermint/libs/os" dbm "github.com/tendermint/tm-db" - "github.com/cosmos/cosmos-sdk/crypto" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" ) @@ -117,7 +116,7 @@ func (kb dbKeybase) ExportPrivateKeyObject(name string, passphrase string) (tmcr return nil, err } - priv, _, err = crypto.UnarmorDecryptPrivKey(linfo.PrivKeyArmor, passphrase) + priv, _, err = UnarmorDecryptPrivKey(linfo.PrivKeyArmor, passphrase) if err != nil { return nil, err } @@ -139,7 +138,7 @@ func (kb dbKeybase) Export(name string) (armor string, err error) { return "", fmt.Errorf("no key to export with name %s", name) } - return crypto.ArmorInfoBytes(bz), nil + return ArmorInfoBytes(bz), nil } // ExportPubKey returns public keys in ASCII armored format. It retrieves a Info @@ -159,7 +158,7 @@ func (kb dbKeybase) ExportPubKey(name string) (armor string, err error) { return } - return crypto.ArmorPubKeyBytes(info.GetPubKey().Bytes(), string(info.GetAlgo())), nil + return ArmorPubKeyBytes(info.GetPubKey().Bytes(), string(info.GetAlgo())), nil } // ExportPrivKey returns a private key in ASCII armored format. @@ -177,7 +176,7 @@ func (kb dbKeybase) ExportPrivKey(name string, decryptPassphrase string, return "", err } - return crypto.EncryptArmorPrivKey(priv, encryptPassphrase, string(info.GetAlgo())), nil + return EncryptArmorPrivKey(priv, encryptPassphrase, string(info.GetAlgo())), nil } // Close the underlying storage. @@ -215,7 +214,7 @@ func (m keyringMigrator) Import(uid string, armor string) error { return fmt.Errorf("cannot overwrite key %q", uid) } - infoBytes, err := crypto.UnarmorInfoBytes(armor) + infoBytes, err := UnarmorInfoBytes(armor) if err != nil { return err } From 465762ed5060246dd40dd73f4f65371ca18a774c Mon Sep 17 00:00:00 2001 From: chengwenxi Date: Tue, 24 Nov 2020 20:58:46 +0800 Subject: [PATCH 3/5] just use codec/legacy.Cdc --- codec/legacy/codec.go | 14 +++++++++++++- codec/types/any.go | 3 ++- crypto/codec/amino.go | 19 ------------------- crypto/keyring/armor.go | 5 +++-- crypto/keyring/armor_test.go | 5 ++--- crypto/keyring/codec.go | 23 ++--------------------- crypto/keyring/info.go | 7 ++++--- crypto/keyring/keyring.go | 11 ++++++----- crypto/keyring/types_test.go | 3 ++- crypto/ledger/ledger_test.go | 8 ++++---- types/address.go | 3 +-- 11 files changed, 39 insertions(+), 62 deletions(-) diff --git a/codec/legacy/codec.go b/codec/legacy/codec.go index 09225998e0be..5ec6b2976cab 100644 --- a/codec/legacy/codec.go +++ b/codec/legacy/codec.go @@ -3,6 +3,7 @@ package legacy import ( "github.com/cosmos/cosmos-sdk/codec" cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" + cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" ) // Cdc defines a global generic sealed Amino codec to be used throughout sdk. It @@ -15,5 +16,16 @@ func init() { Cdc = codec.NewLegacyAmino() cryptocodec.RegisterCrypto(Cdc) codec.RegisterEvidences(Cdc) - Cdc.Seal() +} + +// PrivKeyFromBytes unmarshals private key bytes and returns a PrivKey +func PrivKeyFromBytes(privKeyBytes []byte) (privKey cryptotypes.PrivKey, err error) { + err = Cdc.UnmarshalBinaryBare(privKeyBytes, &privKey) + return +} + +// PubKeyFromBytes unmarshals public key bytes and returns a PubKey +func PubKeyFromBytes(pubKeyBytes []byte) (pubKey cryptotypes.PubKey, err error) { + err = Cdc.UnmarshalBinaryBare(pubKeyBytes, &pubKey) + return } diff --git a/codec/types/any.go b/codec/types/any.go index 38fe4b42aa72..4a30ddad4d0f 100644 --- a/codec/types/any.go +++ b/codec/types/any.go @@ -1,8 +1,9 @@ package types import ( - sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" "github.com/gogo/protobuf/proto" + + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" ) type Any struct { diff --git a/crypto/codec/amino.go b/crypto/codec/amino.go index 5cd61c3dd31f..d50a08864c24 100644 --- a/crypto/codec/amino.go +++ b/crypto/codec/amino.go @@ -10,13 +10,6 @@ import ( cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" ) -var amino *codec.LegacyAmino - -func init() { - amino = codec.NewLegacyAmino() - RegisterCrypto(amino) -} - // RegisterCrypto registers all crypto dependency types with the provided Amino // codec. func RegisterCrypto(cdc *codec.LegacyAmino) { @@ -38,15 +31,3 @@ func RegisterCrypto(cdc *codec.LegacyAmino) { cdc.RegisterConcrete(&secp256k1.PrivKey{}, secp256k1.PrivKeyName, nil) } - -// PrivKeyFromBytes unmarshals private key bytes and returns a PrivKey -func PrivKeyFromBytes(privKeyBytes []byte) (privKey cryptotypes.PrivKey, err error) { - err = amino.UnmarshalBinaryBare(privKeyBytes, &privKey) - return -} - -// PubKeyFromBytes unmarshals public key bytes and returns a PubKey -func PubKeyFromBytes(pubKeyBytes []byte) (pubKey cryptotypes.PubKey, err error) { - err = amino.UnmarshalBinaryBare(pubKeyBytes, &pubKey) - return -} diff --git a/crypto/keyring/armor.go b/crypto/keyring/armor.go index f26419721c52..1f76504a24a0 100644 --- a/crypto/keyring/armor.go +++ b/crypto/keyring/armor.go @@ -9,6 +9,7 @@ import ( "github.com/tendermint/tendermint/crypto/armor" "github.com/tendermint/tendermint/crypto/xsalsa20symmetric" + "github.com/cosmos/cosmos-sdk/codec/legacy" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" ) @@ -151,7 +152,7 @@ func encryptPrivKey(privKey cryptotypes.PrivKey, passphrase string) (saltBytes [ } key = crypto.Sha256(key) // get 32 bytes - privKeyBytes := CryptoCdc.MustMarshalBinaryBare(privKey) + privKeyBytes := legacy.Cdc.MustMarshalBinaryBare(privKey) return saltBytes, xsalsa20symmetric.EncryptSymmetric(privKeyBytes, key) } @@ -204,5 +205,5 @@ func decryptPrivKey(saltBytes []byte, encBytes []byte, passphrase string) (privK return privKey, err } - return PrivKeyFromBytes(privKeyBytes) + return legacy.PrivKeyFromBytes(privKeyBytes) } diff --git a/crypto/keyring/armor_test.go b/crypto/keyring/armor_test.go index 33e0142d19d7..5a422bd4b533 100644 --- a/crypto/keyring/armor_test.go +++ b/crypto/keyring/armor_test.go @@ -14,7 +14,6 @@ import ( "github.com/tendermint/tendermint/crypto/xsalsa20symmetric" "github.com/cosmos/cosmos-sdk/codec/legacy" - cryptoAmino "github.com/cosmos/cosmos-sdk/crypto/codec" "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/crypto/keyring" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" @@ -78,7 +77,7 @@ func TestArmorUnarmorPubKey(t *testing.T) { armored := keyring.ArmorPubKeyBytes(legacy.Cdc.Amino.MustMarshalBinaryBare(info.GetPubKey()), "") pubBytes, algo, err := keyring.UnarmorPubKeyBytes(armored) require.NoError(t, err) - pub, err := cryptoAmino.PubKeyFromBytes(pubBytes) + pub, err := legacy.PubKeyFromBytes(pubBytes) require.NoError(t, err) require.Equal(t, string(hd.Secp256k1Type), algo) require.True(t, pub.Equals(info.GetPubKey())) @@ -86,7 +85,7 @@ func TestArmorUnarmorPubKey(t *testing.T) { armored = keyring.ArmorPubKeyBytes(legacy.Cdc.Amino.MustMarshalBinaryBare(info.GetPubKey()), "unknown") pubBytes, algo, err = keyring.UnarmorPubKeyBytes(armored) require.NoError(t, err) - pub, err = cryptoAmino.PubKeyFromBytes(pubBytes) + pub, err = legacy.PubKeyFromBytes(pubBytes) require.NoError(t, err) require.Equal(t, "unknown", algo) require.True(t, pub.Equals(info.GetPubKey())) diff --git a/crypto/keyring/codec.go b/crypto/keyring/codec.go index 67adb1c5d1bb..558f377752d2 100644 --- a/crypto/keyring/codec.go +++ b/crypto/keyring/codec.go @@ -2,19 +2,12 @@ package keyring import ( "github.com/cosmos/cosmos-sdk/codec" - cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" + "github.com/cosmos/cosmos-sdk/codec/legacy" "github.com/cosmos/cosmos-sdk/crypto/hd" - cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" ) -// CryptoCdc defines the codec required for keys and info -var CryptoCdc *codec.LegacyAmino - func init() { - CryptoCdc = codec.NewLegacyAmino() - cryptocodec.RegisterCrypto(CryptoCdc) - RegisterLegacyAminoCodec(CryptoCdc) - CryptoCdc.Seal() + RegisterLegacyAminoCodec(legacy.Cdc) } // RegisterLegacyAminoCodec registers concrete types and interfaces on the given codec. @@ -26,15 +19,3 @@ func RegisterLegacyAminoCodec(cdc *codec.LegacyAmino) { cdc.RegisterConcrete(offlineInfo{}, "crypto/keys/offlineInfo", nil) cdc.RegisterConcrete(multiInfo{}, "crypto/keys/multiInfo", nil) } - -// PrivKeyFromBytes unmarshals private key bytes and returns a PrivKey -func PrivKeyFromBytes(privKeyBytes []byte) (privKey cryptotypes.PrivKey, err error) { - err = CryptoCdc.UnmarshalBinaryBare(privKeyBytes, &privKey) - return -} - -// PubKeyFromBytes unmarshals public key bytes and returns a PubKey -func PubKeyFromBytes(pubKeyBytes []byte) (pubKey cryptotypes.PubKey, err error) { - err = CryptoCdc.UnmarshalBinaryBare(pubKeyBytes, &pubKey) - return -} diff --git a/crypto/keyring/info.go b/crypto/keyring/info.go index f05e2f0ddef6..24024599bba7 100644 --- a/crypto/keyring/info.go +++ b/crypto/keyring/info.go @@ -3,6 +3,7 @@ package keyring import ( "fmt" + "github.com/cosmos/cosmos-sdk/codec/legacy" codectypes "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/crypto/keys/multisig" @@ -246,12 +247,12 @@ func (i multiInfo) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error { // encoding info func marshalInfo(i Info) []byte { - return CryptoCdc.MustMarshalBinaryLengthPrefixed(i) + return legacy.Cdc.MustMarshalBinaryLengthPrefixed(i) } // decoding info func unmarshalInfo(bz []byte) (info Info, err error) { - err = CryptoCdc.UnmarshalBinaryLengthPrefixed(bz, &info) + err = legacy.Cdc.UnmarshalBinaryLengthPrefixed(bz, &info) if err != nil { return nil, err } @@ -266,7 +267,7 @@ func unmarshalInfo(bz []byte) (info Info, err error) { _, ok := info.(multiInfo) if ok { var multi multiInfo - err = CryptoCdc.UnmarshalBinaryLengthPrefixed(bz, &multi) + err = legacy.Cdc.UnmarshalBinaryLengthPrefixed(bz, &multi) return multi, err } diff --git a/crypto/keyring/keyring.go b/crypto/keyring/keyring.go index 83066f049e64..f99bfff8b750 100644 --- a/crypto/keyring/keyring.go +++ b/crypto/keyring/keyring.go @@ -18,6 +18,7 @@ import ( tmcrypto "github.com/tendermint/tendermint/crypto" "github.com/cosmos/cosmos-sdk/client/input" + "github.com/cosmos/cosmos-sdk/codec/legacy" "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/crypto/ledger" "github.com/cosmos/cosmos-sdk/crypto/types" @@ -196,7 +197,7 @@ func (ks keystore) ExportPubKeyArmor(uid string) (string, error) { return "", fmt.Errorf("no key to export with name: %s", uid) } - return ArmorPubKeyBytes(CryptoCdc.MustMarshalBinaryBare(bz.GetPubKey()), string(bz.GetAlgo())), nil + return ArmorPubKeyBytes(legacy.Cdc.MustMarshalBinaryBare(bz.GetPubKey()), string(bz.GetAlgo())), nil } func (ks keystore) ExportPubKeyArmorByAddress(address sdk.Address) (string, error) { @@ -238,7 +239,7 @@ func (ks keystore) ExportPrivateKeyObject(uid string) (types.PrivKey, error) { return nil, err } - priv, err = PrivKeyFromBytes([]byte(linfo.PrivKeyArmor)) + priv, err = legacy.PrivKeyFromBytes([]byte(linfo.PrivKeyArmor)) if err != nil { return nil, err } @@ -287,7 +288,7 @@ func (ks keystore) ImportPubKey(uid string, armor string) error { return err } - pubKey, err := PubKeyFromBytes(pubBytes) + pubKey, err := legacy.PubKeyFromBytes(pubBytes) if err != nil { return err } @@ -314,7 +315,7 @@ func (ks keystore) Sign(uid string, msg []byte) ([]byte, types.PubKey, error) { return nil, nil, fmt.Errorf("private key not available") } - priv, err = PrivKeyFromBytes([]byte(i.PrivKeyArmor)) + priv, err = legacy.PrivKeyFromBytes([]byte(i.PrivKeyArmor)) if err != nil { return nil, nil, err } @@ -694,7 +695,7 @@ func (ks keystore) writeLocalKey(name string, priv types.PrivKey, algo hd.PubKey // encrypt private key using keyring pub := priv.PubKey() - info := newLocalInfo(name, pub, string(CryptoCdc.MustMarshalBinaryBare(priv)), algo) + info := newLocalInfo(name, pub, string(legacy.Cdc.MustMarshalBinaryBare(priv)), algo) if err := ks.writeInfo(info); err != nil { return nil, err } diff --git a/crypto/keyring/types_test.go b/crypto/keyring/types_test.go index 8c68bce670a5..b04aa4547964 100644 --- a/crypto/keyring/types_test.go +++ b/crypto/keyring/types_test.go @@ -4,10 +4,11 @@ import ( "encoding/hex" "testing" + "github.com/stretchr/testify/require" + "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/stretchr/testify/require" ) func Test_writeReadLedgerInfo(t *testing.T) { diff --git a/crypto/ledger/ledger_test.go b/crypto/ledger/ledger_test.go index 5fe9da8f55f3..ec1b8dbedc3b 100644 --- a/crypto/ledger/ledger_test.go +++ b/crypto/ledger/ledger_test.go @@ -6,7 +6,7 @@ import ( "github.com/stretchr/testify/require" - cryptoAmino "github.com/cosmos/cosmos-sdk/crypto/codec" + "github.com/cosmos/cosmos-sdk/codec/legacy" "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/crypto/types" "github.com/cosmos/cosmos-sdk/testutil" @@ -238,7 +238,7 @@ func TestRealDeviceSecp256k1(t *testing.T) { // now, let's serialize the public key and make sure it still works bs := cdc.Amino.MustMarshalBinaryBare(priv.PubKey()) - pub2, err := cryptoAmino.PubKeyFromBytes(bs) + pub2, err := legacy.PubKeyFromBytes(bs) require.Nil(t, err, "%+v", err) // make sure we get the same pubkey when we load from disk @@ -251,8 +251,8 @@ func TestRealDeviceSecp256k1(t *testing.T) { require.True(t, valid) // make sure pubkeys serialize properly as well - bs = cdc.Amino.MustMarshalBinaryBare(pub) - bpub, err := cryptoAmino.PubKeyFromBytes(bs) + bs = legacy.Cdc.MustMarshalBinaryBare(pub) + bpub, err := legacy.PubKeyFromBytes(bs) require.NoError(t, err) require.Equal(t, pub, bpub) } diff --git a/types/address.go b/types/address.go index 7e0408a84982..ba9e5b303b6a 100644 --- a/types/address.go +++ b/types/address.go @@ -11,7 +11,6 @@ import ( yaml "gopkg.in/yaml.v2" "github.com/cosmos/cosmos-sdk/codec/legacy" - cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" "github.com/cosmos/cosmos-sdk/types/bech32" ) @@ -663,7 +662,7 @@ func GetPubKeyFromBech32(pkt Bech32PubKeyType, pubkeyStr string) (cryptotypes.Pu return nil, err } - return cryptocodec.PubKeyFromBytes(bz) + return legacy.PubKeyFromBytes(bz) } // MustGetPubKeyFromBech32 calls GetPubKeyFromBech32 except it panics on error. From 45dfd0f0700cda9c35a54eb2e80cbbb658c3e46c Mon Sep 17 00:00:00 2001 From: chengwenxi Date: Tue, 24 Nov 2020 21:45:13 +0800 Subject: [PATCH 4/5] revert armor --- crypto/{keyring => }/armor.go | 2 +- crypto/{keyring => }/armor_test.go | 43 +++++++++++++++--------------- crypto/keyring/keyring.go | 9 ++++--- crypto/keyring/keyring_test.go | 3 ++- crypto/keyring/legacy.go | 11 ++++---- 5 files changed, 36 insertions(+), 32 deletions(-) rename crypto/{keyring => }/armor.go (99%) rename crypto/{keyring => }/armor_test.go (77%) diff --git a/crypto/keyring/armor.go b/crypto/armor.go similarity index 99% rename from crypto/keyring/armor.go rename to crypto/armor.go index 1f76504a24a0..35deb107798f 100644 --- a/crypto/keyring/armor.go +++ b/crypto/armor.go @@ -1,4 +1,4 @@ -package keyring +package crypto import ( "encoding/hex" diff --git a/crypto/keyring/armor_test.go b/crypto/armor_test.go similarity index 77% rename from crypto/keyring/armor_test.go rename to crypto/armor_test.go index 5a422bd4b533..abbc7870aaee 100644 --- a/crypto/keyring/armor_test.go +++ b/crypto/armor_test.go @@ -1,4 +1,4 @@ -package keyring_test +package crypto_test import ( "bytes" @@ -14,6 +14,7 @@ import ( "github.com/tendermint/tendermint/crypto/xsalsa20symmetric" "github.com/cosmos/cosmos-sdk/codec/legacy" + "github.com/cosmos/cosmos-sdk/crypto" "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/crypto/keyring" "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" @@ -23,31 +24,31 @@ import ( func TestArmorUnarmorPrivKey(t *testing.T) { priv := secp256k1.GenPrivKey() - armored := keyring.EncryptArmorPrivKey(priv, "passphrase", "") - _, _, err := keyring.UnarmorDecryptPrivKey(armored, "wrongpassphrase") + armored := crypto.EncryptArmorPrivKey(priv, "passphrase", "") + _, _, err := crypto.UnarmorDecryptPrivKey(armored, "wrongpassphrase") require.Error(t, err) - decrypted, algo, err := keyring.UnarmorDecryptPrivKey(armored, "passphrase") + decrypted, algo, err := crypto.UnarmorDecryptPrivKey(armored, "passphrase") require.NoError(t, err) require.Equal(t, string(hd.Secp256k1Type), algo) require.True(t, priv.Equals(decrypted)) // empty string - decrypted, algo, err = keyring.UnarmorDecryptPrivKey("", "passphrase") + decrypted, algo, err = crypto.UnarmorDecryptPrivKey("", "passphrase") require.Error(t, err) require.True(t, errors.Is(io.EOF, err)) require.Nil(t, decrypted) require.Empty(t, algo) // wrong key type - armored = keyring.ArmorPubKeyBytes(priv.PubKey().Bytes(), "") - _, _, err = keyring.UnarmorDecryptPrivKey(armored, "passphrase") + armored = crypto.ArmorPubKeyBytes(priv.PubKey().Bytes(), "") + _, _, err = crypto.UnarmorDecryptPrivKey(armored, "passphrase") require.Error(t, err) require.Contains(t, err.Error(), "unrecognized armor type") // armor key manually encryptPrivKeyFn := func(privKey cryptotypes.PrivKey, passphrase string) (saltBytes []byte, encBytes []byte) { saltBytes = tmcrypto.CRandBytes(16) - key, err := bcrypt.GenerateFromPassword(saltBytes, []byte(passphrase), keyring.BcryptSecurityParameter) + key, err := bcrypt.GenerateFromPassword(saltBytes, []byte(passphrase), crypto.BcryptSecurityParameter) require.NoError(t, err) key = tmcrypto.Sha256(key) // get 32 bytes privKeyBytes := legacy.Cdc.Amino.MustMarshalBinaryBare(privKey) @@ -62,7 +63,7 @@ func TestArmorUnarmorPrivKey(t *testing.T) { "type": "secp256k", } armored = armor.EncodeArmor("TENDERMINT PRIVATE KEY", headerWrongKdf, encBytes) - _, _, err = keyring.UnarmorDecryptPrivKey(armored, "passphrase") + _, _, err = crypto.UnarmorDecryptPrivKey(armored, "passphrase") require.Error(t, err) require.Equal(t, "unrecognized KDF type: wrong", err.Error()) } @@ -74,16 +75,16 @@ func TestArmorUnarmorPubKey(t *testing.T) { // Add keys and see they return in alphabetical order info, _, err := cstore.NewMnemonic("Bob", keyring.English, types.FullFundraiserPath, hd.Secp256k1) require.NoError(t, err) - armored := keyring.ArmorPubKeyBytes(legacy.Cdc.Amino.MustMarshalBinaryBare(info.GetPubKey()), "") - pubBytes, algo, err := keyring.UnarmorPubKeyBytes(armored) + armored := crypto.ArmorPubKeyBytes(legacy.Cdc.Amino.MustMarshalBinaryBare(info.GetPubKey()), "") + pubBytes, algo, err := crypto.UnarmorPubKeyBytes(armored) require.NoError(t, err) pub, err := legacy.PubKeyFromBytes(pubBytes) require.NoError(t, err) require.Equal(t, string(hd.Secp256k1Type), algo) require.True(t, pub.Equals(info.GetPubKey())) - armored = keyring.ArmorPubKeyBytes(legacy.Cdc.Amino.MustMarshalBinaryBare(info.GetPubKey()), "unknown") - pubBytes, algo, err = keyring.UnarmorPubKeyBytes(armored) + armored = crypto.ArmorPubKeyBytes(legacy.Cdc.Amino.MustMarshalBinaryBare(info.GetPubKey()), "unknown") + pubBytes, algo, err = crypto.UnarmorPubKeyBytes(armored) require.NoError(t, err) pub, err = legacy.PubKeyFromBytes(pubBytes) require.NoError(t, err) @@ -92,7 +93,7 @@ func TestArmorUnarmorPubKey(t *testing.T) { armored, err = cstore.ExportPrivKeyArmor("Bob", "passphrase") require.NoError(t, err) - _, _, err = keyring.UnarmorPubKeyBytes(armored) + _, _, err = crypto.UnarmorPubKeyBytes(armored) require.Error(t, err) require.Equal(t, `couldn't unarmor bytes: unrecognized armor type "TENDERMINT PRIVATE KEY", expected: "TENDERMINT PUBLIC KEY"`, err.Error()) @@ -102,7 +103,7 @@ func TestArmorUnarmorPubKey(t *testing.T) { "type": "unknown", } armored = armor.EncodeArmor("TENDERMINT PUBLIC KEY", header, pubBytes) - _, algo, err = keyring.UnarmorPubKeyBytes(armored) + _, algo, err = crypto.UnarmorPubKeyBytes(armored) require.NoError(t, err) // return secp256k1 if version is 0.0.0 require.Equal(t, "secp256k1", algo) @@ -112,7 +113,7 @@ func TestArmorUnarmorPubKey(t *testing.T) { "type": "unknown", } armored = armor.EncodeArmor("TENDERMINT PUBLIC KEY", header, pubBytes) - bz, algo, err := keyring.UnarmorPubKeyBytes(armored) + bz, algo, err := crypto.UnarmorPubKeyBytes(armored) require.Nil(t, bz) require.Empty(t, algo) require.Error(t, err) @@ -124,7 +125,7 @@ func TestArmorUnarmorPubKey(t *testing.T) { "version": "unknown", } armored = armor.EncodeArmor("TENDERMINT PUBLIC KEY", header, pubBytes) - bz, algo, err = keyring.UnarmorPubKeyBytes(armored) + bz, algo, err = crypto.UnarmorPubKeyBytes(armored) require.Nil(t, bz) require.Empty(t, algo) require.Error(t, err) @@ -133,14 +134,14 @@ func TestArmorUnarmorPubKey(t *testing.T) { func TestArmorInfoBytes(t *testing.T) { bs := []byte("test") - armoredString := keyring.ArmorInfoBytes(bs) - unarmoredBytes, err := keyring.UnarmorInfoBytes(armoredString) + armoredString := crypto.ArmorInfoBytes(bs) + unarmoredBytes, err := crypto.UnarmorInfoBytes(armoredString) require.NoError(t, err) require.True(t, bytes.Equal(bs, unarmoredBytes)) } func TestUnarmorInfoBytesErrors(t *testing.T) { - unarmoredBytes, err := keyring.UnarmorInfoBytes("") + unarmoredBytes, err := crypto.UnarmorInfoBytes("") require.Error(t, err) require.True(t, errors.Is(io.EOF, err)) require.Nil(t, unarmoredBytes) @@ -149,7 +150,7 @@ func TestUnarmorInfoBytesErrors(t *testing.T) { "type": "Info", "version": "0.0.1", } - unarmoredBytes, err = keyring.UnarmorInfoBytes(armor.EncodeArmor( + unarmoredBytes, err = crypto.UnarmorInfoBytes(armor.EncodeArmor( "TENDERMINT KEY INFO", header, []byte("plain-text"))) require.Error(t, err) require.Equal(t, "unrecognized version: 0.0.1", err.Error()) diff --git a/crypto/keyring/keyring.go b/crypto/keyring/keyring.go index f99bfff8b750..ede55dd5c15b 100644 --- a/crypto/keyring/keyring.go +++ b/crypto/keyring/keyring.go @@ -19,6 +19,7 @@ import ( "github.com/cosmos/cosmos-sdk/client/input" "github.com/cosmos/cosmos-sdk/codec/legacy" + "github.com/cosmos/cosmos-sdk/crypto" "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/crypto/ledger" "github.com/cosmos/cosmos-sdk/crypto/types" @@ -197,7 +198,7 @@ func (ks keystore) ExportPubKeyArmor(uid string) (string, error) { return "", fmt.Errorf("no key to export with name: %s", uid) } - return ArmorPubKeyBytes(legacy.Cdc.MustMarshalBinaryBare(bz.GetPubKey()), string(bz.GetAlgo())), nil + return crypto.ArmorPubKeyBytes(legacy.Cdc.MustMarshalBinaryBare(bz.GetPubKey()), string(bz.GetAlgo())), nil } func (ks keystore) ExportPubKeyArmorByAddress(address sdk.Address) (string, error) { @@ -220,7 +221,7 @@ func (ks keystore) ExportPrivKeyArmor(uid, encryptPassphrase string) (armor stri return "", err } - return EncryptArmorPrivKey(priv, encryptPassphrase, string(info.GetAlgo())), nil + return crypto.EncryptArmorPrivKey(priv, encryptPassphrase, string(info.GetAlgo())), nil } // ExportPrivateKeyObject exports an armored private key object. @@ -265,7 +266,7 @@ func (ks keystore) ImportPrivKey(uid, armor, passphrase string) error { return fmt.Errorf("cannot overwrite key: %s", uid) } - privKey, algo, err := UnarmorDecryptPrivKey(armor, passphrase) + privKey, algo, err := crypto.UnarmorDecryptPrivKey(armor, passphrase) if err != nil { return errors.Wrap(err, "failed to decrypt private key") } @@ -283,7 +284,7 @@ func (ks keystore) ImportPubKey(uid string, armor string) error { return fmt.Errorf("cannot overwrite key: %s", uid) } - pubBytes, algo, err := UnarmorPubKeyBytes(armor) + pubBytes, algo, err := crypto.UnarmorPubKeyBytes(armor) if err != nil { return err } diff --git a/crypto/keyring/keyring_test.go b/crypto/keyring/keyring_test.go index faab5a8ec6e6..540ea57bf3fe 100644 --- a/crypto/keyring/keyring_test.go +++ b/crypto/keyring/keyring_test.go @@ -9,6 +9,7 @@ import ( bip39 "github.com/cosmos/go-bip39" "github.com/stretchr/testify/require" + "github.com/cosmos/cosmos-sdk/crypto" "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" "github.com/cosmos/cosmos-sdk/crypto/keys/multisig" @@ -24,7 +25,7 @@ const ( ) func init() { - BcryptSecurityParameter = 1 + crypto.BcryptSecurityParameter = 1 } func TestNewKeyring(t *testing.T) { diff --git a/crypto/keyring/legacy.go b/crypto/keyring/legacy.go index ad2dd0d721dd..59bdc3fc5dad 100644 --- a/crypto/keyring/legacy.go +++ b/crypto/keyring/legacy.go @@ -9,6 +9,7 @@ import ( tmos "github.com/tendermint/tendermint/libs/os" dbm "github.com/tendermint/tm-db" + "github.com/cosmos/cosmos-sdk/crypto" "github.com/cosmos/cosmos-sdk/crypto/types" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" @@ -116,7 +117,7 @@ func (kb dbKeybase) ExportPrivateKeyObject(name string, passphrase string) (type return nil, err } - priv, _, err = UnarmorDecryptPrivKey(linfo.PrivKeyArmor, passphrase) + priv, _, err = crypto.UnarmorDecryptPrivKey(linfo.PrivKeyArmor, passphrase) if err != nil { return nil, err } @@ -138,7 +139,7 @@ func (kb dbKeybase) Export(name string) (armor string, err error) { return "", fmt.Errorf("no key to export with name %s", name) } - return ArmorInfoBytes(bz), nil + return crypto.ArmorInfoBytes(bz), nil } // ExportPubKey returns public keys in ASCII armored format. It retrieves a Info @@ -158,7 +159,7 @@ func (kb dbKeybase) ExportPubKey(name string) (armor string, err error) { return } - return ArmorPubKeyBytes(info.GetPubKey().Bytes(), string(info.GetAlgo())), nil + return crypto.ArmorPubKeyBytes(info.GetPubKey().Bytes(), string(info.GetAlgo())), nil } // ExportPrivKey returns a private key in ASCII armored format. @@ -176,7 +177,7 @@ func (kb dbKeybase) ExportPrivKey(name string, decryptPassphrase string, return "", err } - return EncryptArmorPrivKey(priv, encryptPassphrase, string(info.GetAlgo())), nil + return crypto.EncryptArmorPrivKey(priv, encryptPassphrase, string(info.GetAlgo())), nil } // Close the underlying storage. @@ -214,7 +215,7 @@ func (m keyringMigrator) Import(uid string, armor string) error { return fmt.Errorf("cannot overwrite key %q", uid) } - infoBytes, err := UnarmorInfoBytes(armor) + infoBytes, err := crypto.UnarmorInfoBytes(armor) if err != nil { return err } From 348425b530ed63940159558da811bd925831acb5 Mon Sep 17 00:00:00 2001 From: chengwenxi Date: Tue, 24 Nov 2020 22:02:33 +0800 Subject: [PATCH 5/5] add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b2c97295b0f..d4cf410ec645 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ## [Unreleased] ### Improvements +* (crypto) [\#7987](https://github.com/cosmos/cosmos-sdk/pull/7987) Fix the inconsistency of CryptoCdc, only use `codec/legacy.Cdc`. * (SDK) [\#7925](https://github.com/cosmos/cosmos-sdk/pull/7925) Updated dependencies to use gRPC v1.33.2 * Updated gRPC dependency to v1.33.2 * Updated iavl dependency to v0.15-rc2