// p2p flags
runNodeCmd.Flags().String("p2p.laddr", config.P2P.ListenAddress, "Node listen address. (0.0.0.0:0 means any interface, any port)")
runNodeCmd.Flags().String("p2p.seeds", config.P2P.Seeds, "Comma delimited host:port seed nodes")
- runNodeCmd.Flags().String("p2p.node_key", config.P2P.PrivateKey, "Node key for p2p communication")
runNodeCmd.Flags().Bool("p2p.skip_upnp", config.P2P.SkipUPNP, "Skip UPNP configuration")
runNodeCmd.Flags().Bool("p2p.lan_discoverable", config.P2P.LANDiscover, "Whether the node can be discovered by nodes in the LAN")
runNodeCmd.Flags().Int("p2p.max_num_peers", config.P2P.MaxNumPeers, "Set max num peers")
package config
import (
+ "encoding/hex"
"io"
- "io/ioutil"
"os"
"os/user"
"path/filepath"
log "github.com/sirupsen/logrus"
- "github.com/bytom/bytom/crypto/ed25519"
+ "github.com/bytom/bytom/crypto/ed25519/chainkd"
)
var (
// NodeKey retrieves the currently configured private key of the node, checking
// first any manually set key, falling back to the one found in the configured
// data folder. If no key can be found, a new one is generated.
-func (cfg *Config) NodeKey() (string, error) {
- // Use any specifically configured key.
- if cfg.P2P.PrivateKey != "" {
- return cfg.P2P.PrivateKey, nil
+func (cfg *Config) PrivateKey() *chainkd.XPrv {
+ if cfg.XPrv != nil {
+ return cfg.XPrv
}
- keyFile := rootify(cfg.P2P.NodeKeyFile, cfg.BaseConfig.RootDir)
- buf := make([]byte, ed25519.PrivateKeySize*2)
- fd, err := os.Open(keyFile)
- defer fd.Close()
- if err == nil {
- if _, err = io.ReadFull(fd, buf); err == nil {
- return string(buf), nil
- }
+ filePath := rootify(cfg.PrivateKeyFile, cfg.BaseConfig.RootDir)
+ fildReader, err := os.Open(filePath)
+ if err != nil {
+ log.WithField("err", err).Panic("fail on open private key file")
}
- log.WithField("err", err).Warning("key file access failed")
- _, privKey, err := ed25519.GenerateKey(nil)
- if err != nil {
- return "", err
+ defer fildReader.Close()
+ buf := make([]byte, 128)
+ if _, err = io.ReadFull(fildReader, buf); err != nil {
+ log.WithField("err", err).Panic("fail on read private key file")
}
- if err = ioutil.WriteFile(keyFile, []byte(privKey.String()), 0600); err != nil {
- return "", err
+ var xprv chainkd.XPrv
+ if _, err := hex.Decode(xprv[:], buf); err != nil {
+ log.WithField("err", err).Panic("fail on decode private key")
}
- return privKey.String(), nil
+
+ cfg.XPrv = &xprv
+ xpub := cfg.XPrv.XPub()
+ cfg.XPub = &xpub
+ return cfg.XPrv
}
//-----------------------------------------------------------------------------
// log file name
LogFile string `mapstructure:"log_file"`
+
+ PrivateKeyFile string `mapstructure:"private_key_file"`
+ XPrv *chainkd.XPrv
+ XPub *chainkd.XPub
}
// Default configurable base parameters.
KeysPath: "keystore",
NodeAlias: "",
LogFile: "log",
+ PrivateKeyFile: "node_key.txt",
}
}
type P2PConfig struct {
ListenAddress string `mapstructure:"laddr"`
Seeds string `mapstructure:"seeds"`
- PrivateKey string `mapstructure:"node_key"`
- NodeKeyFile string `mapstructure:"node_key_file"`
SkipUPNP bool `mapstructure:"skip_upnp"`
LANDiscover bool `mapstructure:"lan_discoverable"`
MaxNumPeers int `mapstructure:"max_num_peers"`
func DefaultP2PConfig() *P2PConfig {
return &P2PConfig{
ListenAddress: "tcp://0.0.0.0:46656",
- NodeKeyFile: "nodekey",
SkipUPNP: false,
LANDiscover: true,
MaxNumPeers: 50,
package config
import (
- "io/ioutil"
- "os"
- "strings"
"testing"
"github.com/stretchr/testify/assert"
assert.Equal("/opt/data", cfg.DBDir())
}
-
-func TestNodeKey(t *testing.T) {
- tmpDir, err := ioutil.TempDir(".", "")
- if err != nil {
- t.Fatalf("failed to create temporary data folder: %v", err)
- }
- defer os.RemoveAll(tmpDir)
- config := DefaultConfig()
- config.BaseConfig.RootDir = tmpDir
-
- config.P2P.PrivateKey = "0fcbd0be11e35c35c41c686b7ca597bbcf8ecb78e320d01a93349c8ce9420ea4f26d0fbe651bb2c248d6727801329b589ed19e384c9e906d1da4ab2360558bc0"
- privKey, err := config.NodeKey()
- if err != nil {
- t.Fatal("test node key error:", err)
- }
-
- if strings.Compare(privKey, config.P2P.PrivateKey) != 0 {
- t.Fatal("test node key error. want:", config.P2P.PrivateKey, "got:", privKey)
- }
-
- config.P2P.PrivateKey = ""
- writePrivKey, err := config.NodeKey()
- if err != nil {
- t.Fatal("test node key error:", err)
- }
-
- readPrivKey, err := config.NodeKey()
- if err != nil {
- t.Fatal("test node key error:", err)
- }
-
- if strings.Compare(writePrivKey, readPrivKey) != 0 {
- t.Fatal("test node key error. write:", writePrivKey, "read:", readPrivKey)
- }
-}
package p2p
import (
- "encoding/hex"
"fmt"
"net"
"sync"
var discv *dht.Network
var lanDiscv *mdns.LANDiscover
- config.P2P.PrivateKey, err = config.NodeKey()
- if err != nil {
- return nil, err
- }
-
- bytes, err := hex.DecodeString(config.P2P.PrivateKey)
- if err != nil {
- return nil, err
- }
-
+ bytes := config.PrivateKey().Bytes()
var newKey [64]byte
copy(newKey[:], bytes)
privKey := crypto.PrivKeyEd25519(newKey)
package p2p
import (
- "encoding/hex"
"io/ioutil"
"os"
"sync"
cfg.DBPath = dirPath
cfg.P2P.ListenAddress = "127.0.1.1:0"
swPrivKey := crypto.GenPrivKeyEd25519()
- cfg.P2P.PrivateKey = hex.EncodeToString(swPrivKey.Bytes())
s1 := MakeSwitch(&cfg, testDB, swPrivKey, initSwitchFunc)
s1.Start()
defer s1.Stop()
cfg.DBPath = dirPath
cfg.P2P.ListenAddress = "127.0.1.1:0"
swPrivKey := crypto.GenPrivKeyEd25519()
- cfg.P2P.PrivateKey = hex.EncodeToString(swPrivKey.Bytes())
s1 := MakeSwitch(&cfg, testDB, swPrivKey, initSwitchFunc)
s1.Start()
defer s1.Stop()
cfg.DBPath = dirPath
cfg.P2P.ListenAddress = "127.0.1.1:0"
swPrivKey := crypto.GenPrivKeyEd25519()
- cfg.P2P.PrivateKey = hex.EncodeToString(swPrivKey.Bytes())
s1 := MakeSwitch(&cfg, testDB, swPrivKey, initSwitchFunc)
s1.Start()
defer s1.Stop()
cfg.DBPath = dirPath
cfg.P2P.ListenAddress = "127.0.1.1:0"
swPrivKey := crypto.GenPrivKeyEd25519()
- cfg.P2P.PrivateKey = hex.EncodeToString(swPrivKey.Bytes())
s1 := MakeSwitch(&cfg, testDB, swPrivKey, initSwitchFunc)
s1.Start()
defer s1.Stop()
cfg.P2P.MaxNumPeers = 2
cfg.P2P.ListenAddress = "127.0.1.1:0"
swPrivKey := crypto.GenPrivKeyEd25519()
- cfg.P2P.PrivateKey = hex.EncodeToString(swPrivKey.Bytes())
s1 := MakeSwitch(&cfg, testDB, swPrivKey, initSwitchFunc)
s1.Start()
defer s1.Stop()
inpCfg := *testCfg
inpPrivKey := crypto.GenPrivKeyEd25519()
- inpCfg.P2P.PrivateKey = hex.EncodeToString(swPrivKey.Bytes())
inp := &inboundPeer{PrivKey: inpPrivKey, config: &inpCfg}
addr := NewNetAddress(s1.listeners[0].(*DefaultListener).NetListener().Addr())
if err != nil {
rpCfg := *testCfg
rpPrivKey := crypto.GenPrivKeyEd25519()
- rpCfg.P2P.PrivateKey = hex.EncodeToString(swPrivKey.Bytes())
rp := &remotePeer{PrivKey: rpPrivKey, Config: &rpCfg}
rp.Start()
defer rp.Stop()
inp2Cfg := *testCfg
inp2PrivKey := crypto.GenPrivKeyEd25519()
- inp2Cfg.P2P.PrivateKey = hex.EncodeToString(swPrivKey.Bytes())
inp2 := &inboundPeer{PrivKey: inp2PrivKey, config: &inp2Cfg}
go inp2.dial(addr)
cfg.P2P.MaxNumPeers = 2
cfg.P2P.ListenAddress = "127.0.1.1:0"
swPrivKey := crypto.GenPrivKeyEd25519()
- cfg.P2P.PrivateKey = hex.EncodeToString(swPrivKey.Bytes())
s1 := MakeSwitch(&cfg, testDB, swPrivKey, initSwitchFunc)
s1.Start()
defer s1.Stop()
inpCfg := *testCfg
inpPrivKey := crypto.GenPrivKeyEd25519()
- inpCfg.P2P.PrivateKey = hex.EncodeToString(swPrivKey.Bytes())
inp := &inboundPeer{PrivKey: inpPrivKey, config: &inpCfg}
addr := NewNetAddress(s1.listeners[0].(*DefaultListener).NetListener().Addr())
if err != nil {
rpCfg := *testCfg
rpPrivKey := crypto.GenPrivKeyEd25519()
- rpCfg.P2P.PrivateKey = hex.EncodeToString(swPrivKey.Bytes())
rp := &remotePeer{PrivKey: rpPrivKey, Config: &rpCfg}
rp.Start()
defer rp.Stop()