package config
import (
+ "io"
+ "io/ioutil"
"os"
"os/user"
"path/filepath"
"runtime"
log "github.com/sirupsen/logrus"
+
+ "github.com/bytom/crypto/ed25519"
)
var (
return cfg
}
+// 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
+ }
+
+ 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
+ }
+ }
+
+ log.WithField("err", err).Warning("key file access failed")
+ _, privKey, err := ed25519.GenerateKey(nil)
+ if err != nil {
+ return "", err
+ }
+
+ if err = ioutil.WriteFile(keyFile, []byte(privKey.String()), 0600); err != nil {
+ return "", err
+ }
+ return privKey.String(), nil
+}
+
//-----------------------------------------------------------------------------
// BaseConfig
type BaseConfig struct {
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"`
MaxNumPeers int `mapstructure:"max_num_peers"`
HandshakeTimeout int `mapstructure:"handshake_timeout"`
DialTimeout int `mapstructure:"dial_timeout"`
+ ProxyAddress string `mapstructure:"proxy_address"`
+ ProxyUsername string `mapstructure:"proxy_username"`
+ ProxyPassword string `mapstructure:"proxy_password"`
}
// Default configurable p2p parameters.
func DefaultP2PConfig() *P2PConfig {
return &P2PConfig{
ListenAddress: "tcp://0.0.0.0:46656",
+ NodeKeyFile: "nodekey",
SkipUPNP: false,
MaxNumPeers: 50,
HandshakeTimeout: 30,
DialTimeout: 3,
+ ProxyAddress: "",
+ ProxyUsername: "",
+ ProxyPassword: "",
}
}