forked from ethereum/mist
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
410 lines (310 loc) · 11.6 KB
/
main.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
402
403
404
405
406
407
408
409
410
"use strict";
global._ = require('./modules/utils/underscore');
const Q = require('bluebird');
Q.config({
cancellation: true,
});
const fs = require('fs');
const electron = require('electron');
const app = electron.app;
const dialog = electron.dialog;
const timesync = require("os-timesync");
const Minimongo = require('./modules/minimongoDb.js');
const syncMinimongo = require('./modules/syncMinimongo.js');
const ipc = electron.ipcMain;
const packageJson = require('./package.json');
const i18n = require('./modules/i18n.js');
const logger = require('./modules/utils/logger');
const Sockets = require('./modules/sockets');
const Windows = require('./modules/windows');
const Settings = require('./modules/settings');
Settings.init();
if (Settings.cli.version) {
console.log(Settings.appVersion);
process.exit(0);
}
if (Settings.cli.ignoreGpuBlacklist) {
app.commandLine.appendSwitch('ignore-gpu-blacklist', 'true');
}
// logging setup
const log = logger.create('main');
// GLOBAL Variables
global.path = {
HOME: app.getPath('home'),
APPDATA: app.getPath('appData'), // Application Support/
USERDATA: app.getPath('userData') // Application Aupport/Mist
};
global.dirname = __dirname;
global.version = Settings.appVersion;
global.license = Settings.appLicense;
global.production = Settings.inProductionMode;
log.info(`Running in production mode: ${global.production}`);
global.mode = Settings.uiMode;
global.appName = 'mist' === global.mode ? 'Mist' : 'Ethereum Wallet';
require('./modules/ipcCommunicator.js');
const appMenu = require('./modules/menuItems');
const ipcProviderBackend = require('./modules/ipc/ipcProviderBackend.js');
const ethereumNode = require('./modules/ethereumNode.js');
const nodeSync = require('./modules/nodeSync.js');
global.webviews = [];
global.mining = false;
global.icon = __dirname +'/icons/'+ global.mode +'/icon.png';
global.language = 'en';
global.i18n = i18n; // TODO: detect language switches somehow
global.Tabs = Minimongo('tabs');
// INTERFACE PATHS
global.interfaceAppUrl;
global.interfacePopupsUrl;
// WALLET
if(global.mode === 'wallet') {
log.info('Starting in Wallet mode');
global.interfaceAppUrl = (global.production)
? 'file://' + __dirname + '/interface/wallet/index.html'
: 'http://localhost:3050';
global.interfacePopupsUrl = (global.production)
? 'file://' + __dirname + '/interface/index.html'
: 'http://localhost:3000';
// MIST
} else {
log.info('Starting in Mist mode');
let url = (global.production)
? 'file://' + __dirname + '/interface/index.html'
: 'http://localhost:3000';
if (Settings.cli.resetTabs) {
url += '?reset-tabs=true'
}
global.interfaceAppUrl = global.interfacePopupsUrl = url;
}
// prevent crashed and close gracefully
process.on('uncaughtException', function(error){
log.error('UNCAUGHT EXCEPTION', error);
app.quit();
});
// Quit when all windows are closed.
app.on('window-all-closed', function() {
app.quit();
});
// Listen to custom protocole incoming messages, needs registering of URL schemes
app.on('open-url', function (e, url) {
log.info('Open URL', url);
});
var killedSockets = false;
app.on('before-quit', function(event){
if(!killedSockets) {
event.preventDefault();
}
// sockets manager
Sockets.destroyAll()
.catch((err) => {
log.error('Error shutting down sockets');
});
// delay quit, so the sockets can close
setTimeout(function(){
killedSockets = true;
ethereumNode.stop().then(function() {
app.quit();
});
}, 500);
});
const NODE_TYPE = 'geth';
var mainWindow;
var splashWindow;
// This method will be called when Electron has done everything
// initialization and ready for creating browser windows.
app.on('ready', function() {
// Initialise window mgr
Windows.init();
// check for update
require('./modules/updateChecker').run();
// initialize the web3 IPC provider backend
ipcProviderBackend.init();
// instantiate custom protocols
require('./customProtocols.js');
// add menu already here, so we have copy and past functionality
appMenu();
// Create the browser window.
// MIST
if(global.mode === 'mist') {
mainWindow = Windows.create('main', {
primary: true,
electronOptions: {
width: 1024 + 208,
height: 720,
webPreferences: {
preload: __dirname +'/modules/preloader/mistUI.js',
'overlay-fullscreen-video': true,
'overlay-scrollbars': true
}
}
});
syncMinimongo(Tabs, mainWindow.webContents);
// WALLET
} else {
mainWindow = Windows.create('main', {
primary: true,
electronOptions: {
width: 1100,
height: 720,
webPreferences: {
preload: __dirname +'/modules/preloader/wallet.js',
'overlay-fullscreen-video': true,
'overlay-scrollbars': true
}
}
});
}
splashWindow = Windows.create('splash', {
primary: true,
url: global.interfacePopupsUrl + '#splashScreen_'+ global.mode,
show: true,
electronOptions: {
width: 400,
height: 230,
resizable: false,
backgroundColor: '#F6F6F6',
useContentSize: true,
frame: false,
webPreferences: {
preload: __dirname +'/modules/preloader/splashScreen.js',
}
}
});
// check time sync
// var ntpClient = require('ntp-client');
// ntpClient.getNetworkTime("pool.ntp.org", 123, function(err, date) {
timesync.checkEnabled(function (err, enabled) {
if(err) {
log.error('Couldn\'t get time from NTP time sync server.', err);
return;
}
if(!enabled) {
dialog.showMessageBox({
type: "warning",
buttons: ['OK'],
message: global.i18n.t('mist.errors.timeSync.title'),
detail: global.i18n.t('mist.errors.timeSync.description') +"\n\n"+ global.i18n.t('mist.errors.timeSync.'+ process.platform)
}, function(){
});
}
});
splashWindow.on('ready', function() {
// node connection stuff
ethereumNode.on('nodeConnectionTimeout', function() {
Windows.broadcast('uiAction_nodeStatus', 'connectionTimeout');
});
ethereumNode.on('nodeLog', function(data) {
Windows.broadcast('uiAction_nodeLogText', data.replace(/^.*[0-9]\]/,''));
});
// state change
ethereumNode.on('state', function(state, stateAsText) {
Windows.broadcast('uiAction_nodeStatus', stateAsText,
ethereumNode.STATES.ERROR === state ? ethereumNode.lastError : null
);
});
// capture sync results
const syncResultPromise = new Q((resolve, reject) => {
nodeSync.on('nodeSyncing', function(result) {
Windows.broadcast('uiAction_nodeSyncStatus', 'inProgress', result);
});
nodeSync.on('stopped', function() {
Windows.broadcast('uiAction_nodeSyncStatus', 'stopped');
});
nodeSync.on('error', function(err) {
log.error('Error syncing node', err);
reject(err);
});
nodeSync.on('finished', function() {
nodeSync.removeAllListeners('error');
nodeSync.removeAllListeners('finished');
resolve();
});
});
// go!
ethereumNode.init()
.then(function sanityCheck() {
if (!ethereumNode.isIpcConnected) {
throw new Error('Either the node didn\'t start or IPC socket failed to connect.')
}
/* At this point Geth is running and the socket is connected. */
log.info('Connected via IPC to node.');
// update menu, to show node switching possibilities
appMenu();
})
.then(function getAccounts() {
return ethereumNode.send('eth_accounts', []);
})
.then(function onboarding(resultData) {
if (ethereumNode.isGeth && resultData.result && resultData.result.length === 0) {
log.info('No accounts setup yet, lets do onboarding first.');
return new Q((resolve, reject) => {
var onboardingWindow = Windows.createPopup('onboardingScreen', {
primary: true,
electronOptions: {
width: 576,
height: 442,
},
});
onboardingWindow.on('close', function(){
app.quit();
});
// change network types (mainnet, testnet)
ipc.on('onBoarding_changeNet', function(e, testnet) {
let newType = ethereumNode.type;
let newNetwork = testnet ? 'test' : 'main';
log.debug('Onboarding change network', newNetwork);
ethereumNode.restart(newType, newNetwork)
.then(function nodeRestarted() {
appMenu();
})
.catch((err) => {
log.error('Error restarting node', err);
reject(err);
});
});
// launch app
ipc.on('onBoarding_launchApp', function(e) {
// prevent that it closes the app
onboardingWindow.removeAllListeners('close');
onboardingWindow.close();
ipc.removeAllListeners('onBoarding_changeNet');
ipc.removeAllListeners('onBoarding_launchApp');
resolve();
});
splashWindow.hide();
});
}
})
.then(function doSync() {
// we're going to do the sync - so show splash
splashWindow.show();
return syncResultPromise;
})
.then(function allDone() {
startMainWindow();
})
.catch((err) => {
log.error('Error starting up node and/or syncing', err);
}); /* socket connected to geth */;
}); /* on splash screen loaded */
}); /* on app ready */
/**
Start the main window and all its processes
@method startMainWindow
*/
var startMainWindow = function() {
log.info('Loading Interface at '+ global.interfaceAppUrl);
mainWindow.on('ready', function() {
splashWindow.close();
mainWindow.show();
});
mainWindow.load(global.interfaceAppUrl);
// close app, when the main window is closed
mainWindow.on('closed', function() {
app.quit();
});
// instantiate the application menu
Tracker.autorun(function(){
global.webviews = Tabs.find({},{sort: {position: 1}, fields: {name: 1, _id: 1}}).fetch();
appMenu(global.webviews);
});
};