Save Network Keys in Data Directory (#3488)

* change marshalling

* add networkkeys

* gaz

* fix test

* add new function

* resolve comments, rename to datadir
This commit is contained in:
Nishant Das 2019-09-17 03:39:16 +05:30 committed by Raul Jordan
parent 7e7941b0af
commit 7eba8da9d2
4 changed files with 50 additions and 10 deletions

View File

@ -228,6 +228,7 @@ func (b *BeaconNode) registerP2P(ctx *cli.Context) error {
StaticPeers: sliceutil.SplitCommaSeparated(ctx.GlobalStringSlice(cmd.StaticPeers.Name)),
BootstrapNodeAddr: bootnodeENR,
RelayNodeAddr: ctx.GlobalString(cmd.RelayNode.Name),
DataDir: ctx.GlobalString(cmd.DataDirFlag.Name),
HostAddress: ctx.GlobalString(cmd.P2PHost.Name),
PrivateKey: ctx.GlobalString(cmd.P2PPrivKey.Name),
TCPPort: ctx.GlobalUint(cmd.P2PTCPPort.Name),

View File

@ -9,6 +9,7 @@ type Config struct {
RelayNodeAddr string
HostAddress string
PrivateKey string
DataDir string
TCPPort uint
UDPPort uint
MaxPeers uint

View File

@ -3,7 +3,11 @@ package p2p
import (
"crypto/ecdsa"
"fmt"
"math/rand"
"net"
"os"
"path"
"strconv"
"testing"
"time"
@ -17,13 +21,24 @@ import (
var discoveryWaitTime = 1 * time.Second
func init() {
rand.Seed(time.Now().Unix())
}
func createAddrAndPrivKey(t *testing.T) (net.IP, *ecdsa.PrivateKey) {
ip, err := iputils.ExternalIPv4()
if err != nil {
t.Fatalf("Could not get ip: %v", err)
}
ipAddr := net.ParseIP(ip)
pkey, err := privKey(&Config{Encoding: "ssz"})
temp := testutil.TempDir()
randNum := rand.Int()
tempPath := path.Join(temp, strconv.Itoa(randNum))
err = os.Mkdir(tempPath, 0700)
if err != nil {
t.Fatal(err)
}
pkey, err := privKey(&Config{Encoding: "ssz", DataDir: tempPath})
if err != nil {
t.Fatalf("Could not get private key: %v", err)
}

View File

@ -6,6 +6,8 @@ import (
"encoding/hex"
"io/ioutil"
"net"
"os"
"path"
"github.com/btcsuite/btcd/btcec"
"github.com/libp2p/go-libp2p-core/crypto"
@ -13,6 +15,8 @@ import (
"github.com/prysmaticlabs/prysm/shared/iputils"
)
const keyPath = "network-keys"
func convertFromInterfacePrivKey(privkey crypto.PrivKey) *ecdsa.PrivateKey {
typeAssertedKey := (*ecdsa.PrivateKey)((*btcec.PrivateKey)(privkey.(*crypto.Secp256k1PrivateKey)))
return typeAssertedKey
@ -23,26 +27,46 @@ func convertToInterfacePrivkey(privkey *ecdsa.PrivateKey) crypto.PrivKey {
return typeAssertedKey
}
func convertFromInterfacePubKey(pubkey crypto.PubKey) *ecdsa.PublicKey {
typeAssertedKey := (*ecdsa.PublicKey)((*btcec.PublicKey)(pubkey.(*crypto.Secp256k1PublicKey)))
return typeAssertedKey
}
func convertToInterfacePubkey(pubkey *ecdsa.PublicKey) crypto.PubKey {
typeAssertedKey := crypto.PubKey((*crypto.Secp256k1PublicKey)((*btcec.PublicKey)(pubkey)))
return typeAssertedKey
}
func privKey(cfg *Config) (*ecdsa.PrivateKey, error) {
if cfg.PrivateKey == "" {
defaultKeyPath := path.Join(cfg.DataDir, keyPath)
privateKeyPath := cfg.PrivateKey
_, err := os.Stat(defaultKeyPath)
defaultKeysExist := !os.IsNotExist(err)
if err != nil && defaultKeysExist {
return nil, err
}
if privateKeyPath == "" && !defaultKeysExist {
priv, _, err := crypto.GenerateSecp256k1Key(rand.Reader)
if err != nil {
return nil, err
}
rawbytes, err := priv.Raw()
if err != nil {
return nil, err
}
dst := make([]byte, hex.EncodedLen(len(rawbytes)))
hex.Encode(dst, rawbytes)
if err = ioutil.WriteFile(defaultKeyPath, dst, 0600); err != nil {
return nil, err
}
convertedKey := convertFromInterfacePrivKey(priv)
return convertedKey, nil
}
src, err := ioutil.ReadFile(cfg.PrivateKey)
if defaultKeysExist && privateKeyPath == "" {
privateKeyPath = defaultKeyPath
}
return retrievePrivKeyFromFile(privateKeyPath)
}
func retrievePrivKeyFromFile(path string) (*ecdsa.PrivateKey, error) {
src, err := ioutil.ReadFile(path)
if err != nil {
log.WithError(err).Error("Error reading private key from file")
return nil, err
@ -56,8 +80,7 @@ func privKey(cfg *Config) (*ecdsa.PrivateKey, error) {
if err != nil {
return nil, err
}
priv := (*ecdsa.PrivateKey)((*btcec.PrivateKey)(unmarshalledKey.(*crypto.Secp256k1PrivateKey)))
return priv, nil
return convertFromInterfacePrivKey(unmarshalledKey), nil
}
func ipAddr(cfg *Config) net.IP {