Skip to content

Commit

Permalink
Fix CryptoCdc inconsistent (cosmos#7987)
Browse files Browse the repository at this point in the history
* simple fix

* refactor crypto

* just use codec/legacy.Cdc

* revert armor

* add changelog entry

Co-authored-by: Aleksandr Bezobchuk <[email protected]>
  • Loading branch information
chengwenxi and alexanderbez authored Dec 2, 2020
1 parent a51eac4 commit f57828c
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 51 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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()
}

// 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
}
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)
}

// 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 @@ -213,7 +213,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 @@ -255,7 +255,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 @@ -304,7 +304,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 @@ -331,7 +331,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 @@ -711,7 +711,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

0 comments on commit f57828c

Please sign in to comment.