diff --git a/abci/example/kvstore/helpers.go b/abci/example/kvstore/helpers.go index d1334b312..e59fee279 100644 --- a/abci/example/kvstore/helpers.go +++ b/abci/example/kvstore/helpers.go @@ -10,7 +10,7 @@ import ( func RandVal(i int) types.ValidatorUpdate { pubkey := tmrand.Bytes(32) power := tmrand.Uint16() + 1 - v := types.Ed25519ValidatorUpdate(pubkey, int64(power)) + v := types.UpdateValidator(pubkey, int64(power), "") return v } diff --git a/abci/example/kvstore/persistent_kvstore.go b/abci/example/kvstore/persistent_kvstore.go index c2232cc77..320918b5b 100644 --- a/abci/example/kvstore/persistent_kvstore.go +++ b/abci/example/kvstore/persistent_kvstore.go @@ -238,7 +238,7 @@ func (app *PersistentKVStoreApplication) execValidatorTx(tx []byte) types.Respon } // update - return app.updateValidator(types.Ed25519ValidatorUpdate(pubkey, power)) + return app.updateValidator(types.UpdateValidator(pubkey, power, "")) } // add, update, or remove a validator diff --git a/abci/tests/server/client.go b/abci/tests/server/client.go index 36989f6ac..beaa3da89 100644 --- a/abci/tests/server/client.go +++ b/abci/tests/server/client.go @@ -16,7 +16,7 @@ func InitChain(client abcicli.Client) error { for i := 0; i < total; i++ { pubkey := tmrand.Bytes(33) power := tmrand.Int() - vals[i] = types.Ed25519ValidatorUpdate(pubkey, int64(power)) + vals[i] = types.UpdateValidator(pubkey, int64(power), "") } _, err := client.InitChainSync(types.RequestInitChain{ Validators: vals, diff --git a/abci/types/pubkey.go b/abci/types/pubkey.go index aaff6debb..8530d9538 100644 --- a/abci/types/pubkey.go +++ b/abci/types/pubkey.go @@ -1,16 +1,16 @@ package types import ( + fmt "fmt" + "github.com/tendermint/tendermint/crypto/ed25519" cryptoenc "github.com/tendermint/tendermint/crypto/encoding" -) - -const ( - PubKeyEd25519 = "ed25519" + "github.com/tendermint/tendermint/crypto/secp256k1" ) func Ed25519ValidatorUpdate(pk []byte, power int64) ValidatorUpdate { pke := ed25519.PubKey(pk) + pkp, err := cryptoenc.PubKeyToProto(pke) if err != nil { panic(err) @@ -22,3 +22,23 @@ func Ed25519ValidatorUpdate(pk []byte, power int64) ValidatorUpdate { Power: power, } } + +func UpdateValidator(pk []byte, power int64, keyType string) ValidatorUpdate { + switch keyType { + case "", ed25519.KeyType: + return Ed25519ValidatorUpdate(pk, power) + case secp256k1.KeyType: + pke := secp256k1.PubKey(pk) + pkp, err := cryptoenc.PubKeyToProto(pke) + if err != nil { + panic(err) + } + return ValidatorUpdate{ + // Address: + PubKey: pkp, + Power: power, + } + default: + panic(fmt.Sprintf("key type %s not supported", keyType)) + } +} diff --git a/crypto/encoding/codec.go b/crypto/encoding/codec.go index f9c721131..3c552ed23 100644 --- a/crypto/encoding/codec.go +++ b/crypto/encoding/codec.go @@ -51,7 +51,7 @@ func PubKeyFromProto(k pc.PublicKey) (crypto.PubKey, error) { return pk, nil case *pc.PublicKey_Secp256K1: if len(k.Secp256K1) != secp256k1.PubKeySize { - return nil, fmt.Errorf("invalid size for PubKeyEd25519. Got %d, expected %d", + return nil, fmt.Errorf("invalid size for PubKeySecp256k1. Got %d, expected %d", len(k.Secp256K1), secp256k1.PubKeySize) } pk := make(secp256k1.PubKey, secp256k1.PubKeySize) diff --git a/test/e2e/app/app.go b/test/e2e/app/app.go index 415fc7ad7..13002a708 100644 --- a/test/e2e/app/app.go +++ b/test/e2e/app/app.go @@ -193,13 +193,15 @@ func (app *Application) validatorUpdates(height uint64) (abci.ValidatorUpdates, if len(updates) == 0 { return nil, nil } + valUpdates := abci.ValidatorUpdates{} for keyString, power := range updates { + keyBytes, err := base64.StdEncoding.DecodeString(keyString) if err != nil { return nil, fmt.Errorf("invalid base64 pubkey value %q: %w", keyString, err) } - valUpdates = append(valUpdates, abci.Ed25519ValidatorUpdate(keyBytes, int64(power))) + valUpdates = append(valUpdates, abci.UpdateValidator(keyBytes, int64(power), app.cfg.KeyType)) } return valUpdates, nil } diff --git a/test/e2e/app/config.go b/test/e2e/app/config.go index 281419160..38c967916 100644 --- a/test/e2e/app/config.go +++ b/test/e2e/app/config.go @@ -22,6 +22,7 @@ type Config struct { PrivValKey string `toml:"privval_key"` PrivValState string `toml:"privval_state"` Misbehaviors map[string]string `toml:"misbehaviors"` + KeyType string `toml:"key_type"` } // LoadConfig loads the configuration from disk. diff --git a/test/e2e/pkg/testnet.go b/test/e2e/pkg/testnet.go index fa559fe61..d4f788dbc 100644 --- a/test/e2e/pkg/testnet.go +++ b/test/e2e/pkg/testnet.go @@ -64,7 +64,8 @@ type Node struct { Name string Testnet *Testnet Mode Mode - Key crypto.PrivKey + PrivvalKey crypto.PrivKey + NodeKey crypto.PrivKey IP net.IP ProxyPort uint32 StartAt int64 @@ -135,7 +136,8 @@ func LoadTestnet(file string) (*Testnet, error) { node := &Node{ Name: name, Testnet: testnet, - Key: keyGen.Generate(), + PrivvalKey: keyGen.Generate(manifest.KeyType), + NodeKey: keyGen.Generate("ed25519"), IP: ipGen.Next(), ProxyPort: proxyPortGen.Next(), Mode: ModeValidator, @@ -435,7 +437,7 @@ func (n Node) AddressP2P(withID bool) string { } addr := fmt.Sprintf("%v:26656", ip) if withID { - addr = fmt.Sprintf("%x@%v", n.Key.PubKey().Address().Bytes(), addr) + addr = fmt.Sprintf("%x@%v", n.NodeKey.PubKey().Address().Bytes(), addr) } return addr } diff --git a/test/e2e/runner/setup.go b/test/e2e/runner/setup.go index 8c641d9f6..c1a7502db 100644 --- a/test/e2e/runner/setup.go +++ b/test/e2e/runner/setup.go @@ -96,12 +96,12 @@ func Setup(testnet *e2e.Testnet) error { return err } - err = (&p2p.NodeKey{PrivKey: node.Key}).SaveAs(filepath.Join(nodeDir, "config", "node_key.json")) + err = (&p2p.NodeKey{PrivKey: node.NodeKey}).SaveAs(filepath.Join(nodeDir, "config", "node_key.json")) if err != nil { return err } - (privval.NewFilePV(node.Key, + (privval.NewFilePV(node.PrivvalKey, filepath.Join(nodeDir, PrivvalKeyFile), filepath.Join(nodeDir, PrivvalStateFile), )).Save() @@ -194,8 +194,8 @@ func MakeGenesis(testnet *e2e.Testnet) (types.GenesisDoc, error) { for validator, power := range testnet.Validators { genesis.Validators = append(genesis.Validators, types.GenesisValidator{ Name: validator.Name, - Address: validator.Key.PubKey().Address(), - PubKey: validator.Key.PubKey(), + Address: validator.PrivvalKey.PubKey().Address(), + PubKey: validator.PrivvalKey.PubKey(), Power: power, }) } @@ -317,6 +317,7 @@ func MakeAppConfig(node *e2e.Node) ([]byte, error) { "persist_interval": node.PersistInterval, "snapshot_interval": node.SnapshotInterval, "retain_blocks": node.RetainBlocks, + "key_type": node.PrivvalKey.Type(), } switch node.ABCIProtocol { case e2e.ProtocolUNIX: @@ -359,7 +360,7 @@ func MakeAppConfig(node *e2e.Node) ([]byte, error) { for height, validators := range node.Testnet.ValidatorUpdates { updateVals := map[string]int64{} for node, power := range validators { - updateVals[base64.StdEncoding.EncodeToString(node.Key.PubKey().Bytes())] = power + updateVals[base64.StdEncoding.EncodeToString(node.PrivvalKey.PubKey().Bytes())] = power } validatorUpdates[fmt.Sprintf("%v", height)] = updateVals } diff --git a/test/e2e/tests/evidence_test.go b/test/e2e/tests/evidence_test.go index 8abb361c8..ea24b51e5 100644 --- a/test/e2e/tests/evidence_test.go +++ b/test/e2e/tests/evidence_test.go @@ -22,7 +22,7 @@ func TestEvidence_Misbehavior(t *testing.T) { for _, evidence := range block.Evidence.Evidence { switch evidence := evidence.(type) { case *types.DuplicateVoteEvidence: - if bytes.Equal(evidence.VoteA.ValidatorAddress, node.Key.PubKey().Address()) { + if bytes.Equal(evidence.VoteA.ValidatorAddress, node.PrivvalKey.PubKey().Address()) { nodeEvidence = evidence } default: diff --git a/test/e2e/tests/validator_test.go b/test/e2e/tests/validator_test.go index 29f63bd92..8a36bb55d 100644 --- a/test/e2e/tests/validator_test.go +++ b/test/e2e/tests/validator_test.go @@ -60,7 +60,7 @@ func TestValidator_Propose(t *testing.T) { if node.Mode != e2e.ModeValidator { return } - address := node.Key.PubKey().Address() + address := node.PrivvalKey.PubKey().Address() valSchedule := newValidatorSchedule(*node.Testnet) expectCount := 0 @@ -90,7 +90,7 @@ func TestValidator_Sign(t *testing.T) { if node.Mode != e2e.ModeValidator { return } - address := node.Key.PubKey().Address() + address := node.PrivvalKey.PubKey().Address() valSchedule := newValidatorSchedule(*node.Testnet) expectCount := 0 @@ -160,7 +160,7 @@ func (s *validatorSchedule) Increment(heights int64) { func makeVals(valMap map[*e2e.Node]int64) []*types.Validator { vals := make([]*types.Validator, 0, len(valMap)) for node, power := range valMap { - vals = append(vals, types.NewValidator(node.Key.PubKey(), power)) + vals = append(vals, types.NewValidator(node.PrivvalKey.PubKey(), power)) } return vals }