2019-09-06 19:20:20 +00:00
|
|
|
// This binary is a simple rest API endpoint to calculate
|
|
|
|
// the ENR value of a node given its private key,ip address and port.
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
2019-09-18 21:27:34 +00:00
|
|
|
"encoding/hex"
|
2019-09-06 19:20:20 +00:00
|
|
|
"flag"
|
2019-09-08 18:08:46 +00:00
|
|
|
"io/ioutil"
|
2019-09-06 19:20:20 +00:00
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/btcec"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/enr"
|
|
|
|
"github.com/libp2p/go-libp2p-core/crypto"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
_ "go.uber.org/automaxprocs"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-09-18 21:27:34 +00:00
|
|
|
privateKey = flag.String("private", "", "Hex encoded Private key to use for calculation of ENR")
|
2019-09-15 17:47:23 +00:00
|
|
|
udpPort = flag.Int("udp-port", 0, "UDP Port to use for calculation of ENR")
|
|
|
|
tcpPort = flag.Int("tcp-port", 0, "TCP Port to use for calculation of ENR")
|
2019-09-06 19:20:20 +00:00
|
|
|
ipAddr = flag.String("ipAddress", "", "IP to use in calculation of ENR")
|
2019-09-08 18:08:46 +00:00
|
|
|
outfile = flag.String("out", "", "Filepath to write ENR")
|
2019-09-06 19:20:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if len(*privateKey) == 0 {
|
|
|
|
log.Fatal("No private key given")
|
|
|
|
}
|
2019-09-18 21:27:34 +00:00
|
|
|
dst, err := hex.DecodeString(*privateKey)
|
2019-09-06 19:20:20 +00:00
|
|
|
if err != nil {
|
2019-09-18 21:27:34 +00:00
|
|
|
panic(err)
|
2019-09-06 19:20:20 +00:00
|
|
|
}
|
2019-09-18 21:27:34 +00:00
|
|
|
unmarshalledKey, err := crypto.UnmarshalSecp256k1PrivateKey(dst)
|
2019-09-06 19:20:20 +00:00
|
|
|
if err != nil {
|
2019-09-18 21:27:34 +00:00
|
|
|
panic(err)
|
2019-09-06 19:20:20 +00:00
|
|
|
}
|
2019-09-18 21:27:34 +00:00
|
|
|
ecdsaPrivKey := (*ecdsa.PrivateKey)((*btcec.PrivateKey)(unmarshalledKey.(*crypto.Secp256k1PrivateKey)))
|
2019-09-06 19:20:20 +00:00
|
|
|
|
|
|
|
if net.ParseIP(*ipAddr).To4() == nil {
|
|
|
|
log.Fatalf("Invalid ipv4 address given: %v\n", err)
|
|
|
|
}
|
|
|
|
|
2019-09-12 04:52:27 +00:00
|
|
|
if *udpPort == 0 {
|
2019-09-06 19:20:20 +00:00
|
|
|
log.Fatalf("Invalid udp port given: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
db, err := enode.OpenDB("")
|
|
|
|
defer db.Close()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Could not open node's peer database: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
localNode := enode.NewLocalNode(db, ecdsaPrivKey)
|
|
|
|
ipEntry := enr.IP(net.ParseIP(*ipAddr))
|
2019-09-12 04:52:27 +00:00
|
|
|
udpEntry := enr.UDP(*udpPort)
|
2019-09-06 19:20:20 +00:00
|
|
|
localNode.Set(ipEntry)
|
|
|
|
localNode.Set(udpEntry)
|
2019-09-12 04:52:27 +00:00
|
|
|
if *tcpPort != 0 {
|
|
|
|
tcpEntry := enr.TCP(*tcpPort)
|
|
|
|
localNode.Set(tcpEntry)
|
|
|
|
}
|
2019-09-06 19:20:20 +00:00
|
|
|
log.Info(localNode.Node().String())
|
2019-09-08 18:08:46 +00:00
|
|
|
|
|
|
|
if *outfile != "" {
|
|
|
|
err := ioutil.WriteFile(*outfile, []byte(localNode.Node().String()), 0644)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
log.Infof("Wrote to %s", *outfile)
|
|
|
|
}
|
2019-09-06 19:20:20 +00:00
|
|
|
}
|