From b4975f2b9d97f2c4035d9924d9e7dd97835f38a6 Mon Sep 17 00:00:00 2001 From: Nishant Das Date: Wed, 11 Sep 2019 20:28:23 +0530 Subject: [PATCH] Read P2P Peer Key Properly (#3442) * fix conflict * fix conflict * gaz * fix test * gaz --- beacon-chain/p2p/BUILD.bazel | 3 +-- beacon-chain/p2p/options_test.go | 32 ++++++++++++++++++++++---------- beacon-chain/p2p/utils.go | 18 +++++++++++------- 3 files changed, 34 insertions(+), 19 deletions(-) diff --git a/beacon-chain/p2p/BUILD.bazel b/beacon-chain/p2p/BUILD.bazel index 573e69077..216b333fd 100644 --- a/beacon-chain/p2p/BUILD.bazel +++ b/beacon-chain/p2p/BUILD.bazel @@ -28,7 +28,6 @@ go_library( "//shared:go_default_library", "//shared/iputils:go_default_library", "@com_github_btcsuite_btcd//btcec:go_default_library", - "@com_github_ethereum_go_ethereum//crypto:go_default_library", "@com_github_ethereum_go_ethereum//p2p/discover:go_default_library", "@com_github_ethereum_go_ethereum//p2p/enode:go_default_library", "@com_github_ethereum_go_ethereum//p2p/enr:go_default_library", @@ -68,11 +67,11 @@ go_test( "//proto/testing:go_default_library", "//shared/iputils:go_default_library", "//shared/testutil:go_default_library", - "@com_github_ethereum_go_ethereum//crypto:go_default_library", "@com_github_ethereum_go_ethereum//p2p/discover:go_default_library", "@com_github_ethereum_go_ethereum//p2p/enode:go_default_library", "@com_github_gogo_protobuf//proto:go_default_library", "@com_github_libp2p_go_libp2p//:go_default_library", + "@com_github_libp2p_go_libp2p_core//crypto:go_default_library", "@com_github_libp2p_go_libp2p_core//host:go_default_library", "@com_github_libp2p_go_libp2p_core//network:go_default_library", "@com_github_libp2p_go_libp2p_core//peer:go_default_library", diff --git a/beacon-chain/p2p/options_test.go b/beacon-chain/p2p/options_test.go index 733181602..21cd41c3f 100644 --- a/beacon-chain/p2p/options_test.go +++ b/beacon-chain/p2p/options_test.go @@ -1,14 +1,13 @@ package p2p import ( - "crypto/ecdsa" + "bytes" "crypto/rand" - "encoding/hex" "io/ioutil" "os" "testing" - curve "github.com/ethereum/go-ethereum/crypto" + "github.com/libp2p/go-libp2p-core/crypto" "github.com/prysmaticlabs/prysm/shared/testutil" ) @@ -18,16 +17,21 @@ func TestPrivateKeyLoading(t *testing.T) { log.Fatal(err) } defer os.Remove(file.Name()) - key, err := ecdsa.GenerateKey(curve.S256(), rand.Reader) + key, _, err := crypto.GenerateSecp256k1Key(rand.Reader) if err != nil { t.Fatalf("Could not generate key: %v", err) } - keyStr := hex.EncodeToString(curve.FromECDSA(key)) - err = ioutil.WriteFile(file.Name(), []byte(keyStr), 0600) + marshalledKey, err := crypto.MarshalPrivateKey(key) + if err != nil { + t.Fatalf("Could not marshal key %v", err) + } + encodedKey := crypto.ConfigEncodeKey(marshalledKey) + + err = ioutil.WriteFile(file.Name(), []byte(encodedKey), 0600) if err != nil { t.Fatalf("Could not write key to file: %v", err) } - log.WithField("file", file.Name()).WithField("key", keyStr).Info("Wrote key to file") + log.WithField("file", file.Name()).WithField("key", encodedKey).Info("Wrote key to file") cfg := &Config{ PrivateKey: file.Name(), Encoding: "ssz", @@ -36,8 +40,16 @@ func TestPrivateKeyLoading(t *testing.T) { if err != nil { t.Fatalf("Could not apply option: %v", err) } - newEncoded := hex.EncodeToString(curve.FromECDSA(pKey)) - if newEncoded != keyStr { - t.Error("Private keys do not match") + newPkey := convertToInterfacePrivkey(pKey) + rawBytes, err := key.Raw() + if err != nil { + t.Fatal(err) + } + newRaw, _ := newPkey.Raw() + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(newRaw, rawBytes) { + t.Errorf("Private keys do not match got %#x but wanted %#x", rawBytes, newRaw) } } diff --git a/beacon-chain/p2p/utils.go b/beacon-chain/p2p/utils.go index 8baf6e9b5..0a89bb8a0 100644 --- a/beacon-chain/p2p/utils.go +++ b/beacon-chain/p2p/utils.go @@ -3,11 +3,10 @@ package p2p import ( "crypto/ecdsa" "crypto/rand" + "io/ioutil" "net" - "os" "github.com/btcsuite/btcd/btcec" - curve "github.com/ethereum/go-ethereum/crypto" "github.com/libp2p/go-libp2p-core/crypto" "github.com/prysmaticlabs/prysm/shared/iputils" ) @@ -41,15 +40,20 @@ func privKey(cfg *Config) (*ecdsa.PrivateKey, error) { convertedKey := convertFromInterfacePrivKey(priv) return convertedKey, nil } - if _, err := os.Stat(cfg.PrivateKey); os.IsNotExist(err) { - log.WithField("private key file", cfg.PrivateKey).Warn("Could not read private key, file is missing or unreadable") - return nil, err - } - priv, err := curve.LoadECDSA(cfg.PrivateKey) + privateKey, err := ioutil.ReadFile(cfg.PrivateKey) if err != nil { log.WithError(err).Error("Error reading private key from file") return nil, err } + b, err := crypto.ConfigDecodeKey(string(privateKey)) + if err != nil { + panic(err) + } + unmarshalledKey, err := crypto.UnmarshalPrivateKey(b) + if err != nil { + panic(err) + } + priv := (*ecdsa.PrivateKey)((*btcec.PrivateKey)(unmarshalledKey.(*crypto.Secp256k1PrivateKey))) return priv, nil }