OSDN Git Service

add init consensus node as fed node (#97)
[bytom/vapor.git] / config / config.go
index bf23881..5991e91 100644 (file)
@@ -1,8 +1,8 @@
 package config
 
 import (
+       "encoding/hex"
        "io"
-       "io/ioutil"
        "os"
        "os/user"
        "path/filepath"
@@ -10,7 +10,6 @@ import (
 
        log "github.com/sirupsen/logrus"
 
-       "github.com/vapor/crypto/ed25519"
        "github.com/vapor/crypto/ed25519/chainkd"
 )
 
@@ -53,32 +52,32 @@ func (cfg *Config) SetRoot(root string) *Config {
 // 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
 }
 
 //-----------------------------------------------------------------------------
@@ -118,8 +117,9 @@ type BaseConfig struct {
        // log file name
        LogFile string `mapstructure:"log_file"`
 
-       // Cipher Service Provider
-       // CipherServiceProvider string `mapstructure:"csp"`
+       PrivateKeyFile string `mapstructure:"private_key_file"`
+       XPrv           *chainkd.XPrv
+       XPub           *chainkd.XPub
 
        // Federation file name
        FederationFileName string `mapstructure:"federation_file"`
@@ -134,6 +134,7 @@ func DefaultBaseConfig() BaseConfig {
                DBBackend:          "leveldb",
                DBPath:             "data",
                KeysPath:           "keystore",
+               PrivateKeyFile:     "node_key.txt",
                FederationFileName: "federation.json",
        }
 }
@@ -154,8 +155,6 @@ func (b BaseConfig) FederationFile() string {
 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"`
@@ -171,7 +170,6 @@ type P2PConfig struct {
 func DefaultP2PConfig() *P2PConfig {
        return &P2PConfig{
                ListenAddress:    "tcp://0.0.0.0:46656",
-               NodeKeyFile:      "nodekey",
                SkipUPNP:         false,
                LANDiscover:      true,
                MaxNumPeers:      50,
@@ -246,11 +244,8 @@ func DefaultFederationConfig() *FederationConfig {
                        xpub("7f23aae65ee4307c38d342699e328f21834488e18191ebd66823d220b5a58303496c9d09731784372bade78d5e9a4a6249b2cfe2e3a85464e5a4017aa5611e47"),
                        xpub("585e20143db413e45fbc82f03cb61f177e9916ef1df0012daa8cbf6dbb1025ce8f98e51ae319327b63505b64fdbbf6d36ef916d79e6dd67d51b0bfe76fe544c5"),
                        xpub("b58170b51ca61604028ba1cb412377dfc2bc6567c0afc84c83aae1c0c297d0227ccf568561df70851f4144bbf069b525129f2434133c145e35949375b22a6c9d"),
-                       xpub("983705ae71949c1a5d0fcf953658dd9ecc549f02c63e197b4d087ae31148097ece816bbc60d9012c316139fc550fa0f4b00beb0887f6b152f7a69bc8f392b9fa"),
-                       xpub("d72fb92fa13bf3e0deb39de3a47c8d6eef5584719f7877c82a4c009f78fddf924d9706d48f15b2c782ec80b6bdd621a1f7ba2a0044b0e6f92245de9436885cb9"),
-                       xpub("6798460919e8dc7095ee8b9f9d65033ef3da8c2334813149da5a1e52e9c6da07ba7d0e7379baaa0c8bdcb21890a54e6b7290bee077c645ee4b74b0c1ae9da59a"),
                },
-               Quorum: 4,
+               Quorum: 2,
        }
 }
 
@@ -282,17 +277,7 @@ func DefaultDataDir() string {
        }
        switch runtime.GOOS {
        case "darwin":
-               // In order to be compatible with old data path,
-               // copy the data from the old path to the new path
-               oldPath := filepath.Join(home, "Library", "Vapor")
-               newPath := filepath.Join(home, "Library", "Application Support", "Vapor")
-               if !isFolderNotExists(oldPath) && isFolderNotExists(newPath) {
-                       if err := os.Rename(oldPath, newPath); err != nil {
-                               log.Errorf("DefaultDataDir: %v", err)
-                               return oldPath
-                       }
-               }
-               return newPath
+               return filepath.Join(home, "Library", "Application Support", "Vapor")
        case "windows":
                return filepath.Join(home, "AppData", "Roaming", "Vapor")
        default: