2019-08-21 06:08:30 +00:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
2019-09-11 14:58:23 +00:00
|
|
|
"bytes"
|
2019-08-21 06:08:30 +00:00
|
|
|
"crypto/rand"
|
2019-09-12 00:04:35 +00:00
|
|
|
"encoding/hex"
|
2019-08-21 06:08:30 +00:00
|
|
|
"io/ioutil"
|
2020-09-16 00:17:22 +00:00
|
|
|
"net"
|
2019-08-21 06:08:30 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-09-16 00:17:22 +00:00
|
|
|
gethCrypto "github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/enr"
|
2019-09-11 14:58:23 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/crypto"
|
2020-06-25 16:12:59 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2020-07-14 16:51:39 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
2019-08-21 06:08:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPrivateKeyLoading(t *testing.T) {
|
2020-11-10 22:45:17 +00:00
|
|
|
file, err := ioutil.TempFile(t.TempDir(), "key")
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-11 14:58:23 +00:00
|
|
|
key, _, err := crypto.GenerateSecp256k1Key(rand.Reader)
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err, "Could not generate key")
|
2019-09-12 00:04:35 +00:00
|
|
|
raw, err := key.Raw()
|
2019-09-11 14:58:23 +00:00
|
|
|
if err != nil {
|
2019-09-12 00:04:35 +00:00
|
|
|
panic(err)
|
2019-09-11 14:58:23 +00:00
|
|
|
}
|
2019-09-12 00:04:35 +00:00
|
|
|
out := hex.EncodeToString(raw)
|
2019-09-11 14:58:23 +00:00
|
|
|
|
2020-06-25 16:12:59 +00:00
|
|
|
err = ioutil.WriteFile(file.Name(), []byte(out), params.BeaconIoConfig().ReadWritePermissions)
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err, "Could not write key to file")
|
2019-09-12 00:04:35 +00:00
|
|
|
log.WithField("file", file.Name()).WithField("key", out).Info("Wrote key to file")
|
2019-08-21 06:08:30 +00:00
|
|
|
cfg := &Config{
|
|
|
|
PrivateKey: file.Name(),
|
|
|
|
}
|
|
|
|
pKey, err := privKey(cfg)
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err, "Could not apply option")
|
2019-09-11 14:58:23 +00:00
|
|
|
newPkey := convertToInterfacePrivkey(pKey)
|
|
|
|
rawBytes, err := key.Raw()
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-04-14 16:41:09 +00:00
|
|
|
newRaw, err := newPkey.Raw()
|
2020-07-14 16:51:39 +00:00
|
|
|
require.NoError(t, err)
|
2019-09-11 14:58:23 +00:00
|
|
|
if !bytes.Equal(newRaw, rawBytes) {
|
|
|
|
t.Errorf("Private keys do not match got %#x but wanted %#x", rawBytes, newRaw)
|
2019-08-21 06:08:30 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-16 00:17:22 +00:00
|
|
|
|
|
|
|
func TestIPV6Support(t *testing.T) {
|
|
|
|
key, err := gethCrypto.GenerateKey()
|
2020-10-01 18:53:36 +00:00
|
|
|
require.NoError(t, err)
|
2020-09-16 00:17:22 +00:00
|
|
|
db, err := enode.OpenDB("")
|
|
|
|
if err != nil {
|
|
|
|
log.Error("could not open node's peer database")
|
|
|
|
}
|
|
|
|
lNode := enode.NewLocalNode(db, key)
|
|
|
|
mockIPV6 := net.IP{0xff, 0x02, 0xAA, 0, 0x1F, 0, 0x2E, 0, 0, 0x36, 0x45, 0, 0, 0, 0, 0x02}
|
|
|
|
lNode.Set(enr.IP(mockIPV6))
|
|
|
|
ma, err := convertToSingleMultiAddr(lNode.Node())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
ipv6Exists := false
|
|
|
|
for _, p := range ma.Protocols() {
|
|
|
|
if p.Name == "ip4" {
|
|
|
|
t.Error("Got ip4 address instead of ip6")
|
|
|
|
}
|
|
|
|
if p.Name == "ip6" {
|
|
|
|
ipv6Exists = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !ipv6Exists {
|
|
|
|
t.Error("Multiaddress did not have ipv6 protocol")
|
|
|
|
}
|
|
|
|
}
|