prysm-pulse/beacon-chain/p2p/utils.go
Nishant Das 3cb32c3792
Implement Discv5 in Prysm (#3211)
* add discovery

* gaz

* add build options

* add udpPort

* add more changes

* refactor private key

* added discovery loop

* add ttl

* add ttl

* use ip type instead of string

* tests pass

* gaz and new test file

* add test

* add more tests

* add one more test

* adding multiAddr tests

* adding new protocol , listener

* fix keys

* more fixes

* more changes dialing peers works now

* gaz

* add more changes

* add more changes

* gaz

* add new test helpers

* new test

* fixed all tests

* gaz

* reduce sleep

* lint

* new changes

* change formats

* fix all this stuff

* remove discv5 protocol

* remove protocol

* remove port condition,too restrictive

* preston's feedback

* preston's feedback

* close all peers

* gaz

* remove unused func

* Update beacon-chain/p2p/service.go

Co-Authored-By: Preston Van Loon <preston@prysmaticlabs.com>

* remove build options

* refactor tests
2019-08-21 11:38:30 +05:30

66 lines
1.9 KiB
Go

package p2p
import (
"crypto/ecdsa"
"crypto/rand"
"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"
)
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 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 == "" {
priv, _, err := crypto.GenerateSecp256k1Key(rand.Reader)
if err != nil {
return nil, err
}
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)
if err != nil {
log.WithError(err).Error("Error reading private key from file")
return nil, err
}
return priv, nil
}
func ipAddr(cfg *Config) net.IP {
if cfg.HostAddress == "" {
ip, err := iputils.ExternalIPv4()
if err != nil {
log.Fatalf("Could not get IPv4 address: %v", err)
}
return net.ParseIP(ip)
}
return net.ParseIP(cfg.HostAddress)
}