2019-08-21 06:08:30 +00:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/rand"
|
2019-09-12 00:04:35 +00:00
|
|
|
"encoding/hex"
|
2019-09-11 14:58:23 +00:00
|
|
|
"io/ioutil"
|
2019-08-21 06:08:30 +00:00
|
|
|
"net"
|
2019-09-16 22:09:16 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
2019-08-21 06:08:30 +00:00
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/btcec"
|
|
|
|
"github.com/libp2p/go-libp2p-core/crypto"
|
2019-09-12 00:04:35 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-08-21 06:08:30 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/iputils"
|
|
|
|
)
|
|
|
|
|
2019-09-16 22:09:16 +00:00
|
|
|
const keyPath = "network-keys"
|
|
|
|
|
2019-08-21 06:08:30 +00:00
|
|
|
func convertFromInterfacePrivKey(privkey crypto.PrivKey) *ecdsa.PrivateKey {
|
|
|
|
typeAssertedKey := (*ecdsa.PrivateKey)((*btcec.PrivateKey)(privkey.(*crypto.Secp256k1PrivateKey)))
|
|
|
|
return typeAssertedKey
|
|
|
|
}
|
|
|
|
|
|
|
|
func convertToInterfacePrivkey(privkey *ecdsa.PrivateKey) crypto.PrivKey {
|
|
|
|
typeAssertedKey := crypto.PrivKey((*crypto.Secp256k1PrivateKey)((*btcec.PrivateKey)(privkey)))
|
|
|
|
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) {
|
2019-09-16 22:09:16 +00:00
|
|
|
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 {
|
2019-08-21 06:08:30 +00:00
|
|
|
priv, _, err := crypto.GenerateSecp256k1Key(rand.Reader)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-16 22:09:16 +00:00
|
|
|
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
|
|
|
|
}
|
2019-08-21 06:08:30 +00:00
|
|
|
convertedKey := convertFromInterfacePrivKey(priv)
|
|
|
|
return convertedKey, nil
|
|
|
|
}
|
2019-09-16 22:09:16 +00:00
|
|
|
if defaultKeysExist && privateKeyPath == "" {
|
|
|
|
privateKeyPath = defaultKeyPath
|
|
|
|
}
|
|
|
|
return retrievePrivKeyFromFile(privateKeyPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func retrievePrivKeyFromFile(path string) (*ecdsa.PrivateKey, error) {
|
|
|
|
src, err := ioutil.ReadFile(path)
|
2019-08-21 06:08:30 +00:00
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("Error reading private key from file")
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-12 00:04:35 +00:00
|
|
|
dst := make([]byte, hex.DecodedLen(len(src)))
|
|
|
|
_, err = hex.Decode(dst, src)
|
2019-09-11 14:58:23 +00:00
|
|
|
if err != nil {
|
2019-09-12 00:04:35 +00:00
|
|
|
return nil, errors.Wrap(err, "failed to decode hex string")
|
2019-09-11 14:58:23 +00:00
|
|
|
}
|
2019-09-12 00:04:35 +00:00
|
|
|
unmarshalledKey, err := crypto.UnmarshalSecp256k1PrivateKey(dst)
|
2019-09-11 14:58:23 +00:00
|
|
|
if err != nil {
|
2019-09-12 00:04:35 +00:00
|
|
|
return nil, err
|
2019-09-11 14:58:23 +00:00
|
|
|
}
|
2019-09-16 22:09:16 +00:00
|
|
|
return convertFromInterfacePrivKey(unmarshalledKey), nil
|
2019-08-21 06:08:30 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 03:22:17 +00:00
|
|
|
func ipAddr() net.IP {
|
|
|
|
ip, err := iputils.ExternalIPv4()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Could not get IPv4 address: %v", err)
|
2019-08-21 06:08:30 +00:00
|
|
|
}
|
2019-10-04 03:22:17 +00:00
|
|
|
return net.ParseIP(ip)
|
2019-08-21 06:08:30 +00:00
|
|
|
}
|