-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconfig.go
337 lines (337 loc) · 9.49 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package node
import (
"crypto/ecdsa"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
"sync"
"github.com/Cryptochain-VON/accounts"
"github.com/Cryptochain-VON/accounts/external"
"github.com/Cryptochain-VON/accounts/keystore"
"github.com/Cryptochain-VON/accounts/scwallet"
"github.com/Cryptochain-VON/accounts/usbwallet"
"github.com/Cryptochain-VON/common"
"github.com/Cryptochain-VON/crypto"
"github.com/Cryptochain-VON/log"
"github.com/Cryptochain-VON/p2p"
"github.com/Cryptochain-VON/p2p/enode"
"github.com/Cryptochain-VON/rpc"
)
const (
datadirPrivateKey = "nodekey"
datadirDefaultKeyStore = "keystore"
datadirStaticNodes = "static-nodes.json"
datadirTrustedNodes = "trusted-nodes.json"
datadirNodeDatabase = "nodes"
)
type Config struct {
Name string `toml:"-"`
UserIdent string `toml:",omitempty"`
Version string `toml:"-"`
DataDir string
P2P p2p.Config
KeyStoreDir string `toml:",omitempty"`
ExternalSigner string `toml:",omitempty"`
UseLightweightKDF bool `toml:",omitempty"`
InsecureUnlockAllowed bool `toml:",omitempty"`
NoUSB bool `toml:",omitempty"`
SmartCardDaemonPath string `toml:",omitempty"`
IPCPath string `toml:",omitempty"`
HTTPHost string `toml:",omitempty"`
HTTPPort int `toml:",omitempty"`
HTTPCors []string `toml:",omitempty"`
HTTPVirtualHosts []string `toml:",omitempty"`
HTTPModules []string `toml:",omitempty"`
HTTPTimeouts rpc.HTTPTimeouts
WSHost string `toml:",omitempty"`
WSPort int `toml:",omitempty"`
WSOrigins []string `toml:",omitempty"`
WSModules []string `toml:",omitempty"`
WSExposeAll bool `toml:",omitempty"`
GraphQLHost string `toml:",omitempty"`
GraphQLPort int `toml:",omitempty"`
GraphQLCors []string `toml:",omitempty"`
GraphQLVirtualHosts []string `toml:",omitempty"`
Logger log.Logger `toml:",omitempty"`
staticNodesWarning bool
trustedNodesWarning bool
oldGethResourceWarning bool
}
func (c *Config) IPCEndpoint() string {
if c.IPCPath == "" {
return ""
}
if runtime.GOOS == "windows" {
if strings.HasPrefix(c.IPCPath, `\\.\pipe\`) {
return c.IPCPath
}
return `\\.\pipe\` + c.IPCPath
}
if filepath.Base(c.IPCPath) == c.IPCPath {
if c.DataDir == "" {
return filepath.Join(os.TempDir(), c.IPCPath)
}
return filepath.Join(c.DataDir, c.IPCPath)
}
return c.IPCPath
}
func (c *Config) NodeDB() string {
if c.DataDir == "" {
return ""
}
return c.ResolvePath(datadirNodeDatabase)
}
func DefaultIPCEndpoint(clientIdentifier string) string {
if clientIdentifier == "" {
clientIdentifier = strings.TrimSuffix(filepath.Base(os.Args[0]), ".exe")
if clientIdentifier == "" {
panic("empty executable name")
}
}
config := &Config{DataDir: DefaultDataDir(), IPCPath: clientIdentifier + ".ipc"}
return config.IPCEndpoint()
}
func (c *Config) HTTPEndpoint() string {
if c.HTTPHost == "" {
return ""
}
return fmt.Sprintf("%s:%d", c.HTTPHost, c.HTTPPort)
}
func (c *Config) GraphQLEndpoint() string {
if c.GraphQLHost == "" {
return ""
}
return fmt.Sprintf("%s:%d", c.GraphQLHost, c.GraphQLPort)
}
func DefaultHTTPEndpoint() string {
config := &Config{HTTPHost: DefaultHTTPHost, HTTPPort: DefaultHTTPPort}
return config.HTTPEndpoint()
}
func (c *Config) WSEndpoint() string {
if c.WSHost == "" {
return ""
}
return fmt.Sprintf("%s:%d", c.WSHost, c.WSPort)
}
func DefaultWSEndpoint() string {
config := &Config{WSHost: DefaultWSHost, WSPort: DefaultWSPort}
return config.WSEndpoint()
}
func (c *Config) ExtRPCEnabled() bool {
return c.HTTPHost != "" || c.WSHost != "" || c.GraphQLHost != ""
}
func (c *Config) NodeName() string {
name := c.name()
if name == "geth" || name == "geth-testnet" {
name = "Geth"
}
if c.UserIdent != "" {
name += "/" + c.UserIdent
}
if c.Version != "" {
name += "/v" + c.Version
}
name += "/" + runtime.GOOS + "-" + runtime.GOARCH
name += "/" + runtime.Version()
return name
}
func (c *Config) name() string {
if c.Name == "" {
progname := strings.TrimSuffix(filepath.Base(os.Args[0]), ".exe")
if progname == "" {
panic("empty executable name, set Config.Name")
}
return progname
}
return c.Name
}
var isOldGethResource = map[string]bool{
"chaindata": true,
"nodes": true,
"nodekey": true,
"static-nodes.json": false,
"trusted-nodes.json": false,
}
func (c *Config) ResolvePath(path string) string {
if filepath.IsAbs(path) {
return path
}
if c.DataDir == "" {
return ""
}
if warn, isOld := isOldGethResource[path]; isOld {
oldpath := ""
if c.name() == "geth" {
oldpath = filepath.Join(c.DataDir, path)
}
if oldpath != "" && common.FileExist(oldpath) {
if warn {
c.warnOnce(&c.oldGethResourceWarning, "Using deprecated resource file %s, please move this file to the 'geth' subdirectory of datadir.", oldpath)
}
return oldpath
}
}
return filepath.Join(c.instanceDir(), path)
}
func (c *Config) instanceDir() string {
if c.DataDir == "" {
return ""
}
return filepath.Join(c.DataDir, c.name())
}
func (c *Config) NodeKey() *ecdsa.PrivateKey {
if c.P2P.PrivateKey != nil {
return c.P2P.PrivateKey
}
if c.DataDir == "" {
key, err := crypto.GenerateKey()
if err != nil {
log.Crit(fmt.Sprintf("Failed to generate ephemeral node key: %v", err))
}
return key
}
keyfile := c.ResolvePath(datadirPrivateKey)
if key, err := crypto.LoadECDSA(keyfile); err == nil {
return key
}
key, err := crypto.GenerateKey()
if err != nil {
log.Crit(fmt.Sprintf("Failed to generate node key: %v", err))
}
instanceDir := filepath.Join(c.DataDir, c.name())
if err := os.MkdirAll(instanceDir, 0700); err != nil {
log.Error(fmt.Sprintf("Failed to persist node key: %v", err))
return key
}
keyfile = filepath.Join(instanceDir, datadirPrivateKey)
if err := crypto.SaveECDSA(keyfile, key); err != nil {
log.Error(fmt.Sprintf("Failed to persist node key: %v", err))
}
return key
}
func (c *Config) StaticNodes() []*enode.Node {
return c.parsePersistentNodes(&c.staticNodesWarning, c.ResolvePath(datadirStaticNodes))
}
func (c *Config) TrustedNodes() []*enode.Node {
return c.parsePersistentNodes(&c.trustedNodesWarning, c.ResolvePath(datadirTrustedNodes))
}
func (c *Config) parsePersistentNodes(w *bool, path string) []*enode.Node {
if c.DataDir == "" {
return nil
}
if _, err := os.Stat(path); err != nil {
return nil
}
c.warnOnce(w, "Found deprecated node list file %s, please use the TOML config file instead.", path)
var nodelist []string
if err := common.LoadJSON(path, &nodelist); err != nil {
log.Error(fmt.Sprintf("Can't load node list file: %v", err))
return nil
}
var nodes []*enode.Node
for _, url := range nodelist {
if url == "" {
continue
}
node, err := enode.Parse(enode.ValidSchemes, url)
if err != nil {
log.Error(fmt.Sprintf("Node URL %s: %v\n", url, err))
continue
}
nodes = append(nodes, node)
}
return nodes
}
func (c *Config) AccountConfig() (int, int, string, error) {
scryptN := keystore.StandardScryptN
scryptP := keystore.StandardScryptP
if c.UseLightweightKDF {
scryptN = keystore.LightScryptN
scryptP = keystore.LightScryptP
}
var (
keydir string
err error
)
switch {
case filepath.IsAbs(c.KeyStoreDir):
keydir = c.KeyStoreDir
case c.DataDir != "":
if c.KeyStoreDir == "" {
keydir = filepath.Join(c.DataDir, datadirDefaultKeyStore)
} else {
keydir, err = filepath.Abs(c.KeyStoreDir)
}
case c.KeyStoreDir != "":
keydir, err = filepath.Abs(c.KeyStoreDir)
}
return scryptN, scryptP, keydir, err
}
func makeAccountManager(conf *Config) (*accounts.Manager, string, error) {
scryptN, scryptP, keydir, err := conf.AccountConfig()
var ephemeral string
if keydir == "" {
keydir, err = ioutil.TempDir("", "go-ethereum-keystore")
ephemeral = keydir
}
if err != nil {
return nil, "", err
}
if err := os.MkdirAll(keydir, 0700); err != nil {
return nil, "", err
}
var backends []accounts.Backend
if len(conf.ExternalSigner) > 0 {
log.Info("Using external signer", "url", conf.ExternalSigner)
if extapi, err := external.NewExternalBackend(conf.ExternalSigner); err == nil {
backends = append(backends, extapi)
} else {
return nil, "", fmt.Errorf("error connecting to external signer: %v", err)
}
}
if len(backends) == 0 {
backends = append(backends, keystore.NewKeyStore(keydir, scryptN, scryptP))
if !conf.NoUSB {
if ledgerhub, err := usbwallet.NewLedgerHub(); err != nil {
log.Warn(fmt.Sprintf("Failed to start Ledger hub, disabling: %v", err))
} else {
backends = append(backends, ledgerhub)
}
if trezorhub, err := usbwallet.NewTrezorHubWithHID(); err != nil {
log.Warn(fmt.Sprintf("Failed to start HID Trezor hub, disabling: %v", err))
} else {
backends = append(backends, trezorhub)
}
if trezorhub, err := usbwallet.NewTrezorHubWithWebUSB(); err != nil {
log.Warn(fmt.Sprintf("Failed to start WebUSB Trezor hub, disabling: %v", err))
} else {
backends = append(backends, trezorhub)
}
}
if len(conf.SmartCardDaemonPath) > 0 {
if schub, err := scwallet.NewHub(conf.SmartCardDaemonPath, scwallet.Scheme, keydir); err != nil {
log.Warn(fmt.Sprintf("Failed to start smart card hub, disabling: %v", err))
} else {
backends = append(backends, schub)
}
}
}
return accounts.NewManager(&accounts.Config{InsecureUnlockAllowed: conf.InsecureUnlockAllowed}, backends...), ephemeral, nil
}
var warnLock sync.Mutex
func (c *Config) warnOnce(w *bool, format string, args ...interface{}) {
warnLock.Lock()
defer warnLock.Unlock()
if *w {
return
}
l := c.Logger
if l == nil {
l = log.Root()
}
l.Warn(fmt.Sprintf(format, args...))
*w = true
}