Fix CryptoCdc inconsistent (#7987)
* simple fix * refactor crypto * just use codec/legacy.Cdc * revert armor * add changelog entry Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
This commit is contained in:
parent
a51eac4f15
commit
f57828c091
@ -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
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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"
|
||||
)
|
||||
@ -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)
|
||||
}
|
||||
@ -206,5 +205,5 @@ func decryptPrivKey(saltBytes []byte, encBytes []byte, passphrase string) (privK
|
||||
return privKey, err
|
||||
}
|
||||
|
||||
return cryptoAmino.PrivKeyFromBytes(privKeyBytes)
|
||||
return legacy.PrivKeyFromBytes(privKeyBytes)
|
||||
}
|
||||
|
||||
@ -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"
|
||||
@ -79,7 +78,7 @@ 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()))
|
||||
@ -87,7 +86,7 @@ func TestArmorUnarmorPubKey(t *testing.T) {
|
||||
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()))
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -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.
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -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"
|
||||
@ -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) {
|
||||
@ -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
|
||||
}
|
||||
@ -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
|
||||
}
|
||||
@ -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
|
||||
}
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -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) {
|
||||
|
||||
@ -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)
|
||||
}
|
||||
|
||||
@ -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.
|
||||
|
||||
Loading…
Reference in New Issue
Block a user