aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFelix Lange <fjl@twurst.com>2015-02-20 22:26:50 +0800
committerFelix Lange <fjl@twurst.com>2015-02-20 22:26:50 +0800
commite968928613a5d49ad67e921a52883268d4ad74d1 (patch)
tree16dfe1440e13fcfead20bc83ac550a7799cfe1ce
parente282ad25a83e93f7a5ac085e3580ee9051daad10 (diff)
downloadgo-tangerine-e968928613a5d49ad67e921a52883268d4ad74d1.tar
go-tangerine-e968928613a5d49ad67e921a52883268d4ad74d1.tar.gz
go-tangerine-e968928613a5d49ad67e921a52883268d4ad74d1.tar.bz2
go-tangerine-e968928613a5d49ad67e921a52883268d4ad74d1.tar.lz
go-tangerine-e968928613a5d49ad67e921a52883268d4ad74d1.tar.xz
go-tangerine-e968928613a5d49ad67e921a52883268d4ad74d1.tar.zst
go-tangerine-e968928613a5d49ad67e921a52883268d4ad74d1.zip
eth: persist node key between sessions (#304)
-rw-r--r--eth/backend.go33
1 files changed, 27 insertions, 6 deletions
diff --git a/eth/backend.go b/eth/backend.go
index f060ec3b6..0e5d24429 100644
--- a/eth/backend.go
+++ b/eth/backend.go
@@ -3,6 +3,8 @@ package eth
import (
"crypto/ecdsa"
"fmt"
+ "io/ioutil"
+ "path"
"strings"
"github.com/ethereum/go-ethereum/core"
@@ -80,6 +82,27 @@ func (cfg *Config) parseBootNodes() []*discover.Node {
return ns
}
+func (cfg *Config) nodeKey() (*ecdsa.PrivateKey, error) {
+ // use explicit key from command line args if set
+ if cfg.NodeKey != nil {
+ return cfg.NodeKey, nil
+ }
+ // use persistent key if present
+ keyfile := path.Join(cfg.DataDir, "nodekey")
+ key, err := crypto.LoadECDSA(keyfile)
+ if err == nil {
+ return key, nil
+ }
+ // no persistent key, generate and store a new one
+ if key, err = crypto.GenerateKey(); err != nil {
+ return nil, fmt.Errorf("could not generate server key: %v", err)
+ }
+ if err := ioutil.WriteFile(keyfile, crypto.FromECDSA(key), 0600); err != nil {
+ logger.Errorln("could not persist nodekey: ", err)
+ }
+ return key, nil
+}
+
type Ethereum struct {
// Channel for shutting down the ethereum
shutdownChan chan bool
@@ -164,18 +187,16 @@ func New(config *Config) (*Ethereum, error) {
insertChain := eth.chainManager.InsertChain
eth.blockPool = NewBlockPool(hasBlock, insertChain, ezp.Verify)
+ netprv, err := config.nodeKey()
+ if err != nil {
+ return nil, err
+ }
ethProto := EthProtocol(eth.txPool, eth.chainManager, eth.blockPool)
protocols := []p2p.Protocol{ethProto}
if config.Shh {
protocols = append(protocols, eth.whisper.Protocol())
}
- netprv := config.NodeKey
- if netprv == nil {
- if netprv, err = crypto.GenerateKey(); err != nil {
- return nil, fmt.Errorf("could not generate server key: %v", err)
- }
- }
eth.net = &p2p.Server{
PrivateKey: netprv,
Name: config.Name,