forked from ethereum/mist
/
settings.js
executable file
·401 lines (337 loc) · 10 KB
/
settings.js
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
const { app } = require('electron');
const path = require('path');
const fs = require('fs');
const logger = require('./utils/logger');
const packageJson = require('../package.json');
const _ = require('./utils/underscore');
const lodash = require('lodash');
// try loading in config file
const defaultConfig = {
mode: 'mist',
production: false,
};
try {
_.extend(defaultConfig, require('../config.json'));
} catch (err) {
}
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: Gexp, 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:',
},
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 Gexp 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:',
},
logfile: {
demand: false,
describe: 'Logs will be written to this file in addition to the console.',
requiresArg: true,
nargs: 1,
type: 'string',
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: 'Gexp 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. Gexp) 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);
}
class Settings {
init() {
logger.setup(argv);
this._log = logger.create('Settings');
}
get userDataPath() {
// Application Aupport/Mist
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' : 'Expanse 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) {
this._log.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/Expanse/gexp.ipc';
} else if (process.platform === 'freebsd' ||
process.platform === 'linux' ||
process.platform === 'sunos') {
ipcPath += '/.expanse/gexp.ipc';
} else if (process.platform === 'win32') {
ipcPath = '\\\\.\\pipe\\gexp.ipc';
}
this._log.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 skiptimesynccheck() {
return argv.skiptimesynccheck;
}
initConfig() {
global.config.insert({
ui: {
i18n: i18n.getBestMatchedLangCode(app.getLocale())
}
});
}
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);
this._log.debug(`Settings: saveConfig('${key}', '${value}')`);
this._log.trace(global.config.data);
}
}
loadConfig(key) {
const obj = global.config.get(1);
if (!obj) {
this.initConfig();
return this.loadConfig(key);
}
this._log.trace(`Settings: loadConfig('${key}') = '${lodash.get(obj, key)}'`);
return lodash.get(obj, key);
}
loadUserData(path2) {
const fullPath = this.constructUserDataPath(path2);
this._log.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' });
this._log.debug(`Reading "${data}" from ${fullPath}`);
return data;
} catch (err) {
this._log.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 {
this._log.debug(`Saving "${data}" to ${fullPath}`);
fs.writeFileSync(fullPath, data, { encoding: 'utf8' });
} catch (err) {
this._log.warn(`Unable to write to ${fullPath}`, err);
}
}
constructUserDataPath(filePath) {
return path.join(this.userDataPath, filePath);
}
}
module.exports = new Settings();