Skip to content
New issue

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

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

Already on GitHub? # to your account

Replace tmcrypto.PubKey by our own cryptotypes.PubKey #7419

Merged
merged 37 commits into from
Nov 9, 2020
Merged
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
735e283
WIP on removing tm pub/privkey
blushi Sep 25, 2020
a328fff
Merge branch 'master' into marie/7357-sdk-pubkey
blushi Sep 30, 2020
c35c504
Fix part of crypto tests
blushi Sep 30, 2020
597e52e
Add PrivKeyLedgerSecp256K1 proto type
blushi Sep 30, 2020
bc80e35
Merge branch 'master' into marie/7357-sdk-pubkey
blushi Sep 30, 2020
73554f8
Use BasePrivKey for ledger priv key type
blushi Oct 1, 2020
dac3a5f
Merge branch 'master' into marie/7357-sdk-pubkey
blushi Oct 1, 2020
40bf06d
Merge branch 'master' into marie/7357-sdk-pubkey
blushi Oct 14, 2020
8b53143
Merge branch 'master' into marie/7357-sdk-pubkey
blushi Oct 15, 2020
b486ced
Refacto continued
blushi Oct 16, 2020
e186e34
Merge branch 'master' of ssh://github.com/cosmos/cosmos-sdk into mari…
amaury1093 Nov 4, 2020
8d30ba4
First round
amaury1093 Nov 4, 2020
8f2d66e
x/staking
amaury1093 Nov 4, 2020
51a353f
Continue porting
amaury1093 Nov 4, 2020
7c9d764
x/* done
amaury1093 Nov 4, 2020
173ff81
Make build pass
amaury1093 Nov 4, 2020
1fdc967
More conversion
amaury1093 Nov 4, 2020
57582ed
Remove IntoTmPubKey
amaury1093 Nov 4, 2020
c4643b9
Fix test
amaury1093 Nov 4, 2020
c1b5137
Remove crypto.PubKey in some other places
amaury1093 Nov 4, 2020
019055e
Revert ledger changes
amaury1093 Nov 6, 2020
ea134ea
Fix comment
amaury1093 Nov 6, 2020
1599b14
Merge branch 'master' into marie/7357-sdk-pubkey
amaury1093 Nov 6, 2020
c9f0aef
Merge branch 'master' into marie/7357-sdk-pubkey
amaury1093 Nov 6, 2020
4684bf0
Remove useless function
amaury1093 Nov 6, 2020
27210a0
Merge branch 'marie/7357-sdk-pubkey' of ssh://github.com/cosmos/cosmo…
amaury1093 Nov 6, 2020
189590a
Add To/FromTmPublicKey
amaury1093 Nov 6, 2020
19be854
Add migrate tests
amaury1093 Nov 6, 2020
a35a905
Fix test
amaury1093 Nov 6, 2020
fffd3f4
Fix another test
amaury1093 Nov 6, 2020
545e619
Rename tm conversion functions
amaury1093 Nov 9, 2020
e422dbb
Less code
amaury1093 Nov 9, 2020
0048063
Rename BasePrivKey to LedgerPrivKey
amaury1093 Nov 9, 2020
f0d97b9
Merge branch 'master' into marie/7357-sdk-pubkey
amaury1093 Nov 9, 2020
62e8f69
Add changelog
amaury1093 Nov 9, 2020
3c72331
Rename functions
amaury1093 Nov 9, 2020
9800d82
Merge branch 'master' into marie/7357-sdk-pubkey
Nov 9, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -38,6 +38,10 @@ Ref: https://keepachangelog.com/en/1.0.0/

## [v0.40.0-rc3](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.40.0-rc3) - 2020-11-06

### Client Breaking

* (x/staking) [\#7419](https://github.com/cosmos/cosmos-sdk/pull/7419) The `TmConsPubKey` method on ValidatorI has been removed and replaced instead by `ConsPubKey` (which returns a SDK `cryptotypes.PubKey`) and `TmConsPublicKey` (which returns a Tendermint proto PublicKey).

### Improvements
* (tendermint) [\#7828](https://github.com/cosmos/cosmos-sdk/pull/7828) Update tendermint dependency to v0.34.0-rc6

5 changes: 2 additions & 3 deletions client/account_retriever.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package client

import (
"github.com/tendermint/tendermint/crypto"

cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

// Account defines a read-only version of the auth module's AccountI.
type Account interface {
GetAddress() sdk.AccAddress
GetPubKey() crypto.PubKey // can return nil.
GetPubKey() cryptotypes.PubKey // can return nil.
GetAccountNumber() uint64
GetSequence() uint64
}
4 changes: 2 additions & 2 deletions client/debug/main.go
Original file line number Diff line number Diff line change
@@ -8,10 +8,10 @@ import (
"strings"

"github.com/spf13/cobra"
"github.com/tendermint/tendermint/crypto"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/version"
@@ -34,7 +34,7 @@ func Cmd() *cobra.Command {
// getPubKeyFromString returns a Tendermint PubKey (PubKeyEd25519) by attempting
// to decode the pubkey string from hex, base64, and finally bech32. If all
// encodings fail, an error is returned.
func getPubKeyFromString(pkstr string) (crypto.PubKey, error) {
func getPubKeyFromString(pkstr string) (cryptotypes.PubKey, error) {
bz, err := hex.DecodeString(pkstr)
if err == nil {
if len(bz) == ed25519.PubKeySize {
6 changes: 3 additions & 3 deletions client/keys/add.go
Original file line number Diff line number Diff line change
@@ -9,7 +9,6 @@ import (

bip39 "github.com/cosmos/go-bip39"
"github.com/spf13/cobra"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/libs/cli"

"github.com/cosmos/cosmos-sdk/client"
@@ -18,6 +17,7 @@ import (
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/crypto/keys/multisig"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

@@ -152,7 +152,7 @@ func RunAddCmd(cmd *cobra.Command, args []string, kb keyring.Keyring, inBuf *buf

multisigKeys, _ := cmd.Flags().GetStringSlice(flagMultisig)
if len(multisigKeys) != 0 {
var pks []crypto.PubKey
var pks []cryptotypes.PubKey

multisigThreshold, _ := cmd.Flags().GetInt(flagMultiSigThreshold)
if err := validateMultisigThreshold(multisigThreshold, len(multisigKeys)); err != nil {
@@ -247,7 +247,7 @@ func RunAddCmd(cmd *cobra.Command, args []string, kb keyring.Keyring, inBuf *buf
}

if len(mnemonic) == 0 {
// read entropy seed straight from crypto.Rand and convert to mnemonic
// read entropy seed straight from tmcrypto.Rand and convert to mnemonic
entropySeed, err := bip39.NewEntropy(mnemonicEntropySize)
if err != nil {
return err
4 changes: 2 additions & 2 deletions client/keys/show.go
Original file line number Diff line number Diff line change
@@ -5,13 +5,13 @@ import (
"fmt"

"github.com/spf13/cobra"
tmcrypto "github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/libs/cli"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/crypto/keys/multisig"
"github.com/cosmos/cosmos-sdk/crypto/ledger"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

@@ -61,7 +61,7 @@ func runShowCmd(cmd *cobra.Command, args []string) (err error) {
return fmt.Errorf("%s is not a valid name or address: %v", args[0], err)
}
} else {
pks := make([]tmcrypto.PubKey, len(args))
pks := make([]cryptotypes.PubKey, len(args))
for i, keyref := range args {
info, err := fetchKey(clientCtx.Keyring, keyref)
if err != nil {
5 changes: 2 additions & 3 deletions client/keys/show_test.go
Original file line number Diff line number Diff line change
@@ -7,14 +7,13 @@ import (

"github.com/stretchr/testify/require"

"github.com/tendermint/tendermint/crypto"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/crypto/keys/multisig"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
)
@@ -23,7 +22,7 @@ func Test_multiSigKey_Properties(t *testing.T) {
tmpKey1 := secp256k1.GenPrivKeyFromSecret([]byte("mySecret"))
pk := multisig.NewLegacyAminoPubKey(
1,
[]crypto.PubKey{tmpKey1.PubKey()},
[]cryptotypes.PubKey{tmpKey1.PubKey()},
)
tmp := keyring.NewMultiInfo("myMultisig", pk)

7 changes: 6 additions & 1 deletion client/rpc/validators.go
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@ import (

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
)
@@ -99,7 +100,11 @@ func (rvo ResultValidatorsOutput) String() string {
}

func bech32ValidatorOutput(validator *tmtypes.Validator) (ValidatorOutput, error) {
bechValPubkey, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, validator.PubKey)
pk, err := cryptocodec.FromTmPubKeyInterface(validator.PubKey)
if err != nil {
return ValidatorOutput{}, err
}
bechValPubkey, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, pk)
if err != nil {
return ValidatorOutput{}, err
}
5 changes: 2 additions & 3 deletions client/test_helpers.go
Original file line number Diff line number Diff line change
@@ -3,8 +3,7 @@ package client
import (
"fmt"

"github.com/tendermint/tendermint/crypto"

cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

@@ -26,7 +25,7 @@ func (t TestAccount) GetAddress() sdk.AccAddress {
}

// GetPubKey implements client Account.GetPubKey
func (t TestAccount) GetPubKey() crypto.PubKey {
func (t TestAccount) GetPubKey() cryptotypes.PubKey {
return nil
}

4 changes: 2 additions & 2 deletions client/tx/tx.go
Original file line number Diff line number Diff line change
@@ -8,13 +8,13 @@ import (
"os"

"github.com/spf13/pflag"
"github.com/tendermint/tendermint/crypto"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/input"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/rest"
@@ -343,7 +343,7 @@ func PrepareFactory(clientCtx client.Context, txf Factory) (Factory, error) {
// corresponding SignatureV2 if the signing is successful.
func SignWithPrivKey(
signMode signing.SignMode, signerData authsigning.SignerData,
txBuilder client.TxBuilder, priv crypto.PrivKey, txConfig client.TxConfig,
txBuilder client.TxBuilder, priv cryptotypes.PrivKey, txConfig client.TxConfig,
accSeq uint64,
) (signing.SignatureV2, error) {
var sigV2 signing.SignatureV2
9 changes: 5 additions & 4 deletions crypto/armor.go
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ import (

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

@@ -124,7 +125,7 @@ func unarmorBytes(armorStr, blockType string) (bz []byte, header map[string]stri
// encrypt/decrypt with armor

// Encrypt and armor the private key.
func EncryptArmorPrivKey(privKey crypto.PrivKey, passphrase string, algo string) string {
func EncryptArmorPrivKey(privKey cryptotypes.PrivKey, passphrase string, algo string) string {
saltBytes, encBytes := encryptPrivKey(privKey, passphrase)
header := map[string]string{
"kdf": "bcrypt",
@@ -143,7 +144,7 @@ func EncryptArmorPrivKey(privKey crypto.PrivKey, passphrase string, algo string)
// encrypt the given privKey with the passphrase using a randomly
// generated salt and the xsalsa20 cipher. returns the salt and the
// encrypted priv key.
func encryptPrivKey(privKey crypto.PrivKey, passphrase string) (saltBytes []byte, encBytes []byte) {
func encryptPrivKey(privKey cryptotypes.PrivKey, passphrase string) (saltBytes []byte, encBytes []byte) {
saltBytes = crypto.CRandBytes(16)
key, err := bcrypt.GenerateFromPassword(saltBytes, []byte(passphrase), BcryptSecurityParameter)

@@ -158,7 +159,7 @@ func encryptPrivKey(privKey crypto.PrivKey, passphrase string) (saltBytes []byte
}

// UnarmorDecryptPrivKey returns the privkey byte slice, a string of the algo type, and an error
func UnarmorDecryptPrivKey(armorStr string, passphrase string) (privKey crypto.PrivKey, algo string, err error) {
func UnarmorDecryptPrivKey(armorStr string, passphrase string) (privKey cryptotypes.PrivKey, algo string, err error) {
blockType, header, encBytes, err := armor.DecodeArmor(armorStr)
if err != nil {
return privKey, "", err
@@ -190,7 +191,7 @@ func UnarmorDecryptPrivKey(armorStr string, passphrase string) (privKey crypto.P
return privKey, header[headerType], err
}

func decryptPrivKey(saltBytes []byte, encBytes []byte, passphrase string) (privKey crypto.PrivKey, err error) {
func decryptPrivKey(saltBytes []byte, encBytes []byte, passphrase string) (privKey cryptotypes.PrivKey, err error) {
key, err := bcrypt.GenerateFromPassword(saltBytes, []byte(passphrase), BcryptSecurityParameter)
if err != nil {
return privKey, sdkerrors.Wrap(err, "error generating bcrypt key from passphrase")
3 changes: 2 additions & 1 deletion crypto/armor_test.go
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@ import (
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/types"
)

@@ -46,7 +47,7 @@ func TestArmorUnarmorPrivKey(t *testing.T) {
require.Contains(t, err.Error(), "unrecognized armor type")

// armor key manually
encryptPrivKeyFn := func(privKey tmcrypto.PrivKey, passphrase string) (saltBytes []byte, encBytes []byte) {
encryptPrivKeyFn := func(privKey cryptotypes.PrivKey, passphrase string) (saltBytes []byte, encBytes []byte) {
saltBytes = tmcrypto.CRandBytes(16)
key, err := bcrypt.GenerateFromPassword(saltBytes, []byte(passphrase), crypto.BcryptSecurityParameter)
require.NoError(t, err)
18 changes: 3 additions & 15 deletions crypto/codec/amino.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package codec

import (
"github.com/tendermint/tendermint/crypto"
tmed25519 "github.com/tendermint/tendermint/crypto/ed25519"
"github.com/tendermint/tendermint/crypto/sr25519"

"github.com/cosmos/cosmos-sdk/codec"
@@ -22,43 +20,33 @@ func init() {
// RegisterCrypto registers all crypto dependency types with the provided Amino
// codec.
func RegisterCrypto(cdc *codec.LegacyAmino) {
// TODO We now register both Tendermint's PubKey and our own PubKey. In the
// long-term, we should move away from Tendermint's PubKey, and delete this
// first line.
cdc.RegisterInterface((*crypto.PubKey)(nil), nil)
cdc.RegisterInterface((*cryptotypes.PubKey)(nil), nil)
cdc.RegisterConcrete(sr25519.PubKey{},
sr25519.PubKeyName, nil)
// TODO Same as above, for ED25519
cdc.RegisterConcrete(tmed25519.PubKey{},
tmed25519.PubKeyName, nil)
cdc.RegisterConcrete(&ed25519.PubKey{},
ed25519.PubKeyName, nil)
cdc.RegisterConcrete(&secp256k1.PubKey{},
secp256k1.PubKeyName, nil)
cdc.RegisterConcrete(&kmultisig.LegacyAminoPubKey{},
kmultisig.PubKeyAminoRoute, nil)

cdc.RegisterInterface((*crypto.PrivKey)(nil), nil)
cdc.RegisterInterface((*cryptotypes.PrivKey)(nil), nil)
cdc.RegisterConcrete(sr25519.PrivKey{},
sr25519.PrivKeyName, nil)
// TODO Same as above
cdc.RegisterConcrete(tmed25519.PrivKey{},
tmed25519.PrivKeyName, nil)
cdc.RegisterConcrete(&ed25519.PrivKey{},
ed25519.PrivKeyName, nil)
cdc.RegisterConcrete(&secp256k1.PrivKey{},
secp256k1.PrivKeyName, nil)
}

// PrivKeyFromBytes unmarshals private key bytes and returns a PrivKey
func PrivKeyFromBytes(privKeyBytes []byte) (privKey crypto.PrivKey, err error) {
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 crypto.PubKey, err error) {
func PubKeyFromBytes(pubKeyBytes []byte) (pubKey cryptotypes.PubKey, err error) {
err = amino.UnmarshalBinaryBare(pubKeyBytes, &pubKey)
return
}
10 changes: 0 additions & 10 deletions crypto/codec/proto.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package codec

import (
tmcrypto "github.com/tendermint/tendermint/crypto"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"github.com/cosmos/cosmos-sdk/crypto/keys/multisig"
@@ -12,14 +10,6 @@ import (

// RegisterInterfaces registers the sdk.Tx interface.
func RegisterInterfaces(registry codectypes.InterfaceRegistry) {
// TODO We now register both Tendermint's PubKey and our own PubKey. In the
// long-term, we should move away from Tendermint's PubKey, and delete
// these lines.
registry.RegisterInterface("tendermint.crypto.PubKey", (*tmcrypto.PubKey)(nil))
registry.RegisterImplementations((*tmcrypto.PubKey)(nil), &ed25519.PubKey{})
registry.RegisterImplementations((*tmcrypto.PubKey)(nil), &secp256k1.PubKey{})
registry.RegisterImplementations((*tmcrypto.PubKey)(nil), &multisig.LegacyAminoPubKey{})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to stay for backwards compat for multisigs?


registry.RegisterInterface("cosmos.crypto.PubKey", (*cryptotypes.PubKey)(nil))
registry.RegisterImplementations((*cryptotypes.PubKey)(nil), &ed25519.PubKey{})
registry.RegisterImplementations((*cryptotypes.PubKey)(nil), &secp256k1.PubKey{})
57 changes: 57 additions & 0 deletions crypto/codec/tm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
default:
case tmsecp256k1.PubKey:
return &secp256k1.PubKey{Key: []byte(tmPk)}, nil
default:

secp was added in rc6

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to keep things nice and clean (this PR is getting big already), i created a separate one #7838

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah okay, thank you. Should this turn the pubkey straight into the tendermint proto public key?

does the tendermint pubkey get used anywhere in the sdk that makes it needed?

Copy link
Contributor

@amaury1093 amaury1093 Nov 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good question, and I was hesitating about this one. In the SDK, we use both:

  • tmcrypto.PubKey is used mainly with tmtypes.Validator
  • tendermint/proto/tendermint/crypto.PublicKey is used in abci.ValidatorUpdates

I'm happy for this function to turn it directly to a tmprotocrypto.PublicKey, but we would still need to import tendermint/crypto/encoding everywhere we we use tmtypes.Validator.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh I didnt know tmtypes.validator is used in the sdk other than sending updates to tm. maybe its fine for now and if the work to decouple that happens this can be done then.

Copy link
Contributor

@amaury1093 amaury1093 Nov 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually changed this function to return proto PublicKey, in To/FromTmPublicKey. Although I think we should keep To/FromTmPubKey too, it's quite useful to avoid importing tendermint/crypto/encoding a bit everywhere.

@marbar3778 btw question: why doesn't the proto PublicKey implement tmcrypto.PubKey?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question!

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)
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we use here the tendermint/crypto/ed25519? Public key is bytes we don't need to go through encoding here. We can just cast bytes to the right type using select.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TM supports secp256k1 too now. I could add a switch .(type) here, and do what you suggest. However, I feel it's a bit cleaner to have a central function to do conversion between sdk pubkey <-> tm pubkey (aka FromTmPublicKey). And other conversions (here between tm pubkey <-> tm proto public key) use tm's own functions.

Loading