Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CryptoCdc inconsistent #7987

Merged
merged 10 commits into from
Dec 2, 2020
14 changes: 13 additions & 1 deletion codec/legacy/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -15,5 +16,16 @@ func init() {
Cdc = codec.NewLegacyAmino()
cryptocodec.RegisterCrypto(Cdc)
codec.RegisterEvidences(Cdc)
Cdc.Seal()
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
}

// 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
chengwenxi marked this conversation as resolved.
Show resolved Hide resolved
}
3 changes: 2 additions & 1 deletion codec/types/any.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
19 changes: 0 additions & 19 deletions crypto/codec/amino.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
}
7 changes: 3 additions & 4 deletions crypto/armor.go → crypto/keyring/armor.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package crypto
package keyring

import (
"encoding/hex"
Expand All @@ -10,7 +10,6 @@ import (
"github.com/tendermint/tendermint/crypto/xsalsa20symmetric"

"github.com/cosmos/cosmos-sdk/codec/legacy"
cryptoAmino "github.com/cosmos/cosmos-sdk/crypto/codec"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
Expand Down Expand Up @@ -153,7 +152,7 @@ func encryptPrivKey(privKey cryptotypes.PrivKey, passphrase string) (saltBytes [
}

key = crypto.Sha256(key) // get 32 bytes
privKeyBytes := legacy.Cdc.Amino.MustMarshalBinaryBare(privKey)
privKeyBytes := legacy.Cdc.MustMarshalBinaryBare(privKey)

return saltBytes, xsalsa20symmetric.EncryptSymmetric(privKeyBytes, key)
}
Expand Down Expand Up @@ -206,5 +205,5 @@ func decryptPrivKey(saltBytes []byte, encBytes []byte, passphrase string) (privK
return privKey, err
}

return cryptoAmino.PrivKeyFromBytes(privKeyBytes)
return legacy.PrivKeyFromBytes(privKeyBytes)
}
48 changes: 23 additions & 25 deletions crypto/armor_test.go → crypto/keyring/armor_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package crypto_test
package keyring_test

import (
"bytes"
Expand All @@ -14,8 +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"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
Expand All @@ -25,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 cryptotypes.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)
Expand All @@ -64,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())
}
Expand All @@ -76,25 +74,25 @@ 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)
pub, err := legacy.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)
pub, err = legacy.PubKeyFromBytes(pubBytes)
require.NoError(t, err)
require.Equal(t, "unknown", algo)
require.True(t, pub.Equals(info.GetPubKey()))

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())

Expand All @@ -104,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)
Expand All @@ -114,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)
Expand All @@ -126,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)
Expand All @@ -135,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)
Expand All @@ -151,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())
Expand Down
10 changes: 2 additions & 8 deletions crypto/keyring/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +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"
)

// 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)
chengwenxi marked this conversation as resolved.
Show resolved Hide resolved
}

// RegisterLegacyAminoCodec registers concrete types and interfaces on the given codec.
Expand Down
7 changes: 4 additions & 3 deletions crypto/keyring/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
19 changes: 9 additions & 10 deletions crypto/keyring/keyring.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ import (
tmcrypto "github.com/tendermint/tendermint/crypto"

"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/codec/legacy"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/ledger"
"github.com/cosmos/cosmos-sdk/crypto/types"
Expand Down Expand Up @@ -198,7 +197,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(legacy.Cdc.MustMarshalBinaryBare(bz.GetPubKey()), string(bz.GetAlgo())), nil
}

func (ks keystore) ExportPubKeyArmorByAddress(address sdk.Address) (string, error) {
Expand All @@ -221,7 +220,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.
Expand All @@ -240,7 +239,7 @@ func (ks keystore) ExportPrivateKeyObject(uid string) (types.PrivKey, error) {
return nil, err
}

priv, err = cryptoamino.PrivKeyFromBytes([]byte(linfo.PrivKeyArmor))
priv, err = legacy.PrivKeyFromBytes([]byte(linfo.PrivKeyArmor))
if err != nil {
return nil, err
}
Expand All @@ -266,7 +265,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")
}
Expand All @@ -284,12 +283,12 @@ 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
}

pubKey, err := cryptoamino.PubKeyFromBytes(pubBytes)
pubKey, err := legacy.PubKeyFromBytes(pubBytes)
if err != nil {
return err
}
Expand All @@ -316,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 = cryptoamino.PrivKeyFromBytes([]byte(i.PrivKeyArmor))
priv, err = legacy.PrivKeyFromBytes([]byte(i.PrivKeyArmor))
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -696,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
}
Expand Down
3 changes: 1 addition & 2 deletions crypto/keyring/keyring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ 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"
Expand All @@ -25,7 +24,7 @@ const (
)

func init() {
crypto.BcryptSecurityParameter = 1
BcryptSecurityParameter = 1
}

func TestNewKeyring(t *testing.T) {
Expand Down
Loading