2019-08-21 06:08:30 +00:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
2019-09-12 00:04:35 +00:00
|
|
|
"encoding/hex"
|
2020-09-16 00:17:22 +00:00
|
|
|
"net"
|
2022-04-18 20:42:07 +00:00
|
|
|
"os"
|
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"
|
2021-09-21 19:59:25 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/config/params"
|
2021-09-23 18:53:46 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/testing/assert"
|
|
|
|
"github.com/prysmaticlabs/prysm/testing/require"
|
2019-08-21 06:08:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestPrivateKeyLoading(t *testing.T) {
|
2022-05-20 07:16:53 +00:00
|
|
|
params.SetupTestConfigCleanup(t)
|
2022-04-18 20:42:07 +00:00
|
|
|
file, err := os.CreateTemp(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
|
|
|
|
2022-04-18 20:42:07 +00:00
|
|
|
err = os.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)
|
2021-01-22 15:15:40 +00:00
|
|
|
assert.DeepEqual(t, rawBytes, newRaw, "Private keys do not match")
|
2019-08-21 06:08:30 +00:00
|
|
|
}
|
2020-09-16 00:17:22 +00:00
|
|
|
|
|
|
|
func TestIPV6Support(t *testing.T) {
|
2022-05-20 07:16:53 +00:00
|
|
|
params.SetupTestConfigCleanup(t)
|
2020-09-16 00:17:22 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|