* WIP on removing tm pub/privkey * Fix part of crypto tests * Add PrivKeyLedgerSecp256K1 proto type * Use BasePrivKey for ledger priv key type * Refacto continued * First round * x/staking * Continue porting * x/* done * Make build pass * More conversion * Remove IntoTmPubKey * Fix test * Remove crypto.PubKey in some other places * Revert ledger changes * Fix comment * Remove useless function * Add To/FromTmPublicKey * Add migrate tests * Fix test * Fix another test * Rename tm conversion functions * Less code * Rename BasePrivKey to LedgerPrivKey * Add changelog * Rename functions Co-authored-by: Amaury Martiny <amaury.martiny@protonmail.com> Co-authored-by: Alessio Treglia <alessio@tendermint.com>
58 lines
1.8 KiB
Go
58 lines
1.8 KiB
Go
package codec
|
|
|
|
import (
|
|
tmcrypto "github.com/tendermint/tendermint/crypto"
|
|
"github.com/tendermint/tendermint/crypto/encoding"
|
|
tmprotocrypto "github.com/tendermint/tendermint/proto/tendermint/crypto"
|
|
|
|
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
|
|
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
|
|
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
|
|
)
|
|
|
|
// FromTmProtoPublicKey converts a TM's tmprotocrypto.PublicKey into our own PubKey.
|
|
func FromTmProtoPublicKey(protoPk tmprotocrypto.PublicKey) (cryptotypes.PubKey, error) {
|
|
switch protoPk := protoPk.Sum.(type) {
|
|
case *tmprotocrypto.PublicKey_Ed25519:
|
|
return &ed25519.PubKey{
|
|
Key: protoPk.Ed25519,
|
|
}, nil
|
|
default:
|
|
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "cannot convert %v from Tendermint public key", protoPk)
|
|
}
|
|
}
|
|
|
|
// ToTmProtoPublicKey converts our own PubKey to TM's tmprotocrypto.PublicKey.
|
|
func ToTmProtoPublicKey(pk cryptotypes.PubKey) (tmprotocrypto.PublicKey, error) {
|
|
switch pk := pk.(type) {
|
|
case *ed25519.PubKey:
|
|
return tmprotocrypto.PublicKey{
|
|
Sum: &tmprotocrypto.PublicKey_Ed25519{
|
|
Ed25519: pk.Key,
|
|
},
|
|
}, nil
|
|
default:
|
|
return tmprotocrypto.PublicKey{}, sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "cannot convert %v to Tendermint public key", pk)
|
|
}
|
|
}
|
|
|
|
// FromTmPubKeyInterface converts TM's tmcrypto.PubKey to our own PubKey.
|
|
func FromTmPubKeyInterface(tmPk tmcrypto.PubKey) (cryptotypes.PubKey, error) {
|
|
tmProtoPk, err := encoding.PubKeyToProto(tmPk)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return FromTmProtoPublicKey(tmProtoPk)
|
|
}
|
|
|
|
// ToTmPubKeyInterface converts our own PubKey to TM's tmcrypto.PubKey.
|
|
func ToTmPubKeyInterface(pk cryptotypes.PubKey) (tmcrypto.PubKey, error) {
|
|
tmProtoPk, err := ToTmProtoPublicKey(pk)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return encoding.PubKeyFromProto(tmProtoPk)
|
|
}
|