forked from ethereum/mist
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsettings.js
More file actions
476 lines (395 loc) · 12.3 KB
/
Copy pathsettings.js
File metadata and controls
476 lines (395 loc) · 12.3 KB
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
const { app } = require('electron');
const path = require('path');
const fs = require('fs');
const packageJson = require('../package.json');
const _ = require('./utils/underscore');
const lodash = require('lodash');
import { syncBuildConfig, syncFlags, setSwarmEnableOnStart } from './core/settings/actions';
import logger from './utils/logger';
const settingsLog = logger.create('Settings');
let instance = null;
class Settings {
constructor() {
if (!instance) {
instance = this;
}
return instance;
}
init() {
const logLevel = {logLevel: argv.loglevel};
const logFolder = {logFolder: path.join(this.userDataPath, 'logs')};
const loggerOptions = Object.assign(argv, logLevel, logFolder);
logger.setup(loggerOptions);
store.dispatch(syncFlags(argv));
// If -v flag provided, log the Mist version and exit
if (argv.version) {
settingsLog.info(`Mist v${this.appVersion}`);
process.exit(0);
}
// Some Linux installations require this setting:
if (argv.ignoreGpuBlacklist) {
app.commandLine.appendSwitch('ignore-gpu-blacklist', 'true');
store.dispatch({ type: '[MAIN]:IGNORE_GPU_BLACKLIST:SET' });
}
if (this.inAutoTestMode) {
settingsLog.info('AUTOMATED TESTING');
store.dispatch({ type: '[MAIN]:TEST_MODE:SET' });
}
settingsLog.info(`Running in production mode: ${this.inProductionMode}`);
if (this.rpcMode === 'http') {
settingsLog.warn('Connecting to a node via HTTP instead of ipcMain. This is less secure!!!!'.toUpperCase());
}
store.dispatch(syncBuildConfig('appVersion', packageJson.version));
store.dispatch(syncBuildConfig('rpcMode', this.rpcMode));
store.dispatch(syncBuildConfig('productionMode', this.inProductionMode));
store.dispatch(syncBuildConfig('uiMode', this.uiMode));
}
// @returns "Application Support/Mist" in production mode
// @returns "Application Support/Electron" in development mode
get userDataPath() {
return app.getPath('userData');
}
get dbFilePath() {
const dbFileName = (this.inAutoTestMode) ? 'mist.test.lokidb' : 'mist.lokidb';
return path.join(this.userDataPath, dbFileName);
}
get appDataPath() {
// Application Support/
return app.getPath('appData');
}
get userHomePath() {
return app.getPath('home');
}
get cli() {
return argv;
}
get appVersion() {
return packageJson.version;
}
get appName() {
return this.uiMode === 'mist' ? 'Mist' : 'Ethereum Wallet';
}
get appLicense() {
return packageJson.license;
}
get uiMode() {
return (_.isString(argv.mode)) ? argv.mode.toLowerCase() : argv.mode;
}
get inProductionMode() {
return defaultConfig.production;
}
get inAutoTestMode() {
return !!process.env.TEST_MODE;
}
get swarmURL() {
return argv.swarmurl;
}
get gethPath() {
return argv.gethpath;
}
get ethPath() {
return argv.ethpath;
}
get rpcMode() {
if (argv.rpc && argv.rpc.indexOf('http') === 0)
return 'http';
if (argv.rpc && argv.rpc.indexOf('ws:') === 0) {
settingsLog.warn('Websockets are not yet supported by Mist, using default IPC connection');
argv.rpc = null;
return 'ipc';
} else
return 'ipc';
}
get rpcConnectConfig() {
if (this.rpcMode === 'ipc') {
return {
path: this.rpcIpcPath,
};
}
return {
hostPort: this.rpcHttpPath,
};
}
get rpcHttpPath() {
return (this.rpcMode === 'http') ? argv.rpc : null;
}
get rpcIpcPath() {
let ipcPath = (this.rpcMode === 'ipc') ? argv.rpc : null;
if (ipcPath) {
return ipcPath;
}
ipcPath = this.userHomePath;
if (process.platform === 'darwin') {
ipcPath += '/Library/Ethereum/geth.ipc';
} else if (process.platform === 'freebsd' ||
process.platform === 'linux' ||
process.platform === 'sunos') {
ipcPath += '/.ethereum/geth.ipc';
} else if (process.platform === 'win32') {
ipcPath = '\\\\.\\pipe\\geth.ipc';
}
settingsLog.debug(`IPC path: ${ipcPath}`);
return ipcPath;
}
get nodeType() {
return argv.node;
}
get network() {
return argv.network;
}
get syncmode() {
return argv.syncmode;
}
get nodeOptions() {
return argv.nodeOptions;
}
get language() {
return this.loadConfig('ui.i18n');
}
set language(langCode) {
this.saveConfig('ui.i18n', langCode);
}
get enableSwarmOnStart() {
if (global.mode === 'wallet') {
return false;
}
if (argv.swarm) {
return true;
}
const enableOnStart = this.loadConfig('swarm.enableOnStart');
// Sync to redux
if (enableOnStart) {
store.dispatch(setSwarmEnableOnStart());
}
return enableOnStart;
}
set enableSwarmOnStart(bool) {
this.saveConfig('swarm.enableOnStart', bool);
}
get skiptimesynccheck() {
return argv.skiptimesynccheck;
}
initConfig() {
global.config.insert({
ui: {
i18n: i18n.getBestMatchedLangCode(app.getLocale())
},
swarm: {
enableOnStart: argv.swarm
}
});
}
saveConfig(key, value) {
let obj = global.config.get(1);
if (!obj) {
this.initConfig();
obj = global.config.get(1);
}
if (lodash.get(obj, key) !== value) {
lodash.set(obj, key, value);
global.config.update(obj);
settingsLog.debug(`Settings: saveConfig('${key}', '${value}')`);
settingsLog.trace(global.config.data);
}
}
loadConfig(key) {
const obj = global.config.get(1);
if (!obj) {
this.initConfig();
return this.loadConfig(key);
}
settingsLog.trace(`Settings: loadConfig('${key}') = '${lodash.get(obj, key)}'`);
return lodash.get(obj, key);
}
loadUserData(path2) {
const fullPath = this.constructUserDataPath(path2);
settingsLog.trace('Load user data', fullPath);
// check if the file exists
try {
fs.accessSync(fullPath, fs.R_OK);
} catch (err) {
return null;
}
// try to read it
try {
const data = fs.readFileSync(fullPath, { encoding: 'utf8' });
settingsLog.debug(`Reading "${data}" from ${fullPath}`);
return data;
} catch (err) {
settingsLog.warn(`File not readable: ${fullPath}`, err);
}
return null;
}
saveUserData(path2, data) {
if (!data) return; // return so we dont write null, or other invalid data
const fullPath = this.constructUserDataPath(path2);
try {
settingsLog.debug(`Saving "${data}" to ${fullPath}`);
fs.writeFileSync(fullPath, data, { encoding: 'utf8' });
} catch (err) {
settingsLog.warn(`Unable to write to ${fullPath}`, err);
}
}
constructUserDataPath(filePath) {
return path.join(this.userDataPath, filePath);
}
}
module.exports = new Settings();
/* ==========================
Command line argument parsing
============================= */
// Load config
const defaultConfig = {
mode: 'mist',
production: false,
};
try {
_.extend(defaultConfig, require('../config.json'));
} catch (error) {
settingsLog.error(error);
}
const argv = require('yargs')
.usage('Usage: $0 [Mist options] [Node options]')
.option({
mode: {
alias: 'm',
demand: false,
default: defaultConfig.mode,
describe: 'App UI mode: wallet, mist.',
requiresArg: true,
nargs: 1,
type: 'string',
group: 'Mist options:',
},
node: {
demand: false,
default: null,
describe: 'Node to use: geth, eth',
requiresArg: true,
nargs: 1,
type: 'string',
group: 'Mist options:',
},
network: {
demand: false,
default: null,
describe: 'Network to connect to: main, test',
requiresArg: true,
nargs: 1,
type: 'string',
group: 'Mist options:',
},
rpc: {
demand: false,
describe: 'Path to node IPC socket file OR HTTP RPC hostport (if IPC socket file then --node-ipcpath will be set with this value).',
requiresArg: true,
nargs: 1,
type: 'string',
group: 'Mist options:',
},
swarm: {
describe: 'Enable Swarm on start.',
requiresArg: false,
nargs: 0,
type: 'boolean',
group: 'Mist options:',
},
swarmurl: {
demand: false,
default: 'http://localhost:8500',
describe: 'URL serving the Swarm HTTP API. If null, Mist will open a local node.',
requiresArg: true,
nargs: 1,
type: 'string',
group: 'Mist options:',
},
gethpath: {
demand: false,
describe: 'Path to Geth executable to use instead of default.',
requiresArg: true,
nargs: 1,
type: 'string',
group: 'Mist options:',
},
ethpath: {
demand: false,
describe: 'Path to Eth executable to use instead of default.',
requiresArg: true,
nargs: 1,
type: 'string',
group: 'Mist options:',
},
'ignore-gpu-blacklist': {
demand: false,
describe: 'Ignores GPU blacklist (needed for some Linux installations).',
requiresArg: false,
nargs: 0,
type: 'boolean',
group: 'Mist options:',
},
'reset-tabs': {
demand: false,
describe: 'Reset Mist tabs to their default settings.',
requiresArg: false,
nargs: 0,
type: 'boolean',
group: 'Mist options:',
},
loglevel: {
demand: false,
default: 'info',
describe: 'Minimum logging threshold: info, debug, error, trace (shows all logs, including possible passwords over IPC!).',
requiresArg: true,
nargs: 1,
type: 'string',
group: 'Mist options:',
},
syncmode: {
demand: false,
requiresArg: true,
describe: 'Geth synchronization mode: [fast|light|full]',
nargs: 1,
type: 'string',
group: 'Mist options:',
},
version: {
alias: 'v',
demand: false,
requiresArg: false,
nargs: 0,
describe: 'Display Mist version.',
group: 'Mist options:',
type: 'boolean',
},
skiptimesynccheck: {
demand: false,
requiresArg: false,
nargs: 0,
describe: 'Disable checks for the presence of automatic time sync on your OS.',
group: 'Mist options:',
type: 'boolean',
},
'': {
describe: 'To pass options to the underlying node (e.g. Geth) use the --node- prefix, e.g. --node-datadir',
group: 'Node options:',
},
})
.help('h')
.alias('h', 'help')
.parse(process.argv.slice(1));
argv.nodeOptions = [];
for (const optIdx in argv) {
if (optIdx.indexOf('node-') === 0) {
argv.nodeOptions.push(`--${optIdx.substr(5)}`);
if (argv[optIdx] !== true) {
argv.nodeOptions.push(argv[optIdx]);
}
}
}
// some options are shared
if (argv.ipcpath) {
argv.nodeOptions.push('--ipcpath', argv.ipcpath);
}
if (argv.nodeOptions && argv.nodeOptions.syncmode) {
argv.push('--syncmode', argv.nodeOptions.syncmode);
}