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

Add support for TM secp256k1 #7838

Merged
merged 6 commits into from
Nov 13, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
11 changes: 11 additions & 0 deletions crypto/codec/tm.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
tmprotocrypto "github.com/tendermint/tendermint/proto/tendermint/crypto"

"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
Expand All @@ -17,6 +18,10 @@ func FromTmProtoPublicKey(protoPk tmprotocrypto.PublicKey) (cryptotypes.PubKey,
return &ed25519.PubKey{
Key: protoPk.Ed25519,
}, nil
case *tmprotocrypto.PublicKey_Secp256K1:
return &secp256k1.PubKey{
Key: protoPk.Secp256K1,
}, nil
default:
return nil, sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "cannot convert %v from Tendermint public key", protoPk)
}
Expand All @@ -31,6 +36,12 @@ func ToTmProtoPublicKey(pk cryptotypes.PubKey) (tmprotocrypto.PublicKey, error)
Ed25519: pk.Key,
},
}, nil
case *secp256k1.PubKey:
return tmprotocrypto.PublicKey{
Sum: &tmprotocrypto.PublicKey_Secp256K1{
Secp256K1: pk.Key,
},
}, nil
default:
return tmprotocrypto.PublicKey{}, sdkerrors.Wrapf(sdkerrors.ErrInvalidType, "cannot convert %v to Tendermint public key", pk)
}
Expand Down
54 changes: 54 additions & 0 deletions crypto/keys/secp256k1/secp256k1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tendermint/tendermint/crypto"
tmsecp256k1 "github.com/tendermint/tendermint/crypto/secp256k1"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
Expand Down Expand Up @@ -267,3 +268,56 @@ func TestMarshalAmino(t *testing.T) {
})
}
}

func TestMarshalAmino_BackwardsCompatibility(t *testing.T) {
aminoCdc := codec.NewLegacyAmino()
// Create Tendermint keys.
tmPrivKey := tmsecp256k1.GenPrivKey()
tmPubKey := tmPrivKey.PubKey()
// Create our own keys, with the same private key as Tendermint's.
privKey := &secp256k1.PrivKey{Key: []byte(tmPrivKey)}
pubKey := privKey.PubKey().(*secp256k1.PubKey)

testCases := []struct {
desc string
tmKey interface{}
ourKey interface{}
marshalFn func(o interface{}) ([]byte, error)
}{
{
"secp256k1 private key, binary",
tmPrivKey,
privKey,
aminoCdc.MarshalBinaryBare,
},
{
"secp256k1 private key, JSON",
tmPrivKey,
privKey,
aminoCdc.MarshalJSON,
},
{
"secp256k1 public key, binary",
tmPubKey,
pubKey,
aminoCdc.MarshalBinaryBare,
},
{
"secp256k1 public key, JSON",
tmPubKey,
pubKey,
aminoCdc.MarshalJSON,
},
}

for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
// Make sure Amino encoding override is not breaking backwards compatibility.
bz1, err := tc.marshalFn(tc.tmKey)
require.NoError(t, err)
bz2, err := tc.marshalFn(tc.ourKey)
require.NoError(t, err)
require.Equal(t, bz1, bz2)
})
}
}
19 changes: 19 additions & 0 deletions x/staking/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
tmtypes "github.com/tendermint/tendermint/types"

cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
Expand Down Expand Up @@ -181,6 +182,24 @@ func TestInvalidPubKeyTypeMsgCreateValidator(t *testing.T) {
tstaking.CreateValidator(addr, invalidPk, 10, false)
}

func TestBothPubKeyTypeMsgCreateValidator(t *testing.T) {
app, ctx, _, valAddrs := bootstrapHandlerGenesisTest(t, 1000, 2, 1000)
ctx = ctx.WithConsensusParams(&abci.ConsensusParams{
Validator: &tmproto.ValidatorParams{PubKeyTypes: []string{tmtypes.ABCIPubKeyTypeEd25519, tmtypes.ABCIPubKeyTypeSecp256k1}},
})

tstaking := teststaking.NewHelper(t, ctx, app.StakingKeeper)

addrEd := valAddrs[0]
addrSecp := valAddrs[1]
pkEd := ed25519.GenPrivKey().PubKey()
pkSecp := secp256k1.GenPrivKey().PubKey()

// Create validators with both keys should work
tstaking.CreateValidator(addrEd, pkEd, 10, true)
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved
tstaking.CreateValidator(addrSecp, pkSecp, 10, true)
}

func TestLegacyValidatorDelegations(t *testing.T) {
app, ctx, delAddrs, valAddrs := bootstrapHandlerGenesisTest(t, 1000, 2, 100000000)

Expand Down