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
5 changes: 2 additions & 3 deletions crypto/armor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
5 changes: 2 additions & 3 deletions crypto/armor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (

"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 Down Expand Up @@ -79,15 +78,15 @@ func TestArmorUnarmorPubKey(t *testing.T) {
armored := crypto.ArmorPubKeyBytes(legacy.Cdc.Amino.MustMarshalBinaryBare(info.GetPubKey()), "")
pubBytes, algo, err := crypto.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)
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()))
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
}
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
12 changes: 6 additions & 6 deletions crypto/keyring/keyring.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ 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"
cryptoamino "github.com/cosmos/cosmos-sdk/crypto/codec"
"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 +198,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 crypto.ArmorPubKeyBytes(legacy.Cdc.MustMarshalBinaryBare(bz.GetPubKey()), string(bz.GetAlgo())), nil
}

func (ks keystore) ExportPubKeyArmorByAddress(address sdk.Address) (string, error) {
Expand Down Expand Up @@ -240,7 +240,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 Down Expand Up @@ -289,7 +289,7 @@ func (ks keystore) ImportPubKey(uid string, armor string) error {
return err
}

pubKey, err := cryptoamino.PubKeyFromBytes(pubBytes)
pubKey, err := legacy.PubKeyFromBytes(pubBytes)
if err != nil {
return err
}
Expand All @@ -316,7 +316,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 +696,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: 2 additions & 1 deletion crypto/keyring/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions crypto/ledger/ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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)
}
3 changes: 1 addition & 2 deletions types/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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.
Expand Down