Read P2P Peer Key Properly (#3442)

* fix conflict

* fix conflict

* gaz

* fix test

* gaz
This commit is contained in:
Nishant Das 2019-09-11 20:28:23 +05:30 committed by GitHub
parent 1edeb8ec4c
commit b4975f2b9d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 19 deletions

View File

@ -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",

View File

@ -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)
}
}

View File

@ -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
}