forked from ethereum/mist
/
ipcCommunicator.js
138 lines (111 loc) · 3.82 KB
/
ipcCommunicator.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
/**
Window communication
@module ipcCommunicator
*/
const app = require('app'); // Module to control application life.
const appMenu = require('./menuItems');
const popupWindow = require('./popupWindow.js');
const log = require('./utils/logger').create('ipcCommunicator');
const ipc = require('electron').ipcMain;
const _ = global._;
/*
// windows including webviews
windows = {
23: {
type: 'requestWindow',
window: obj,
owner: 12
},
12: {
type: 'webview'
window: obj
owner: null
}
}
*/
// UI ACTIONS
ipc.on('backendAction_closeApp', function() {
app.quit();
});
ipc.on('backendAction_closePopupWindow', function(e) {
var windowId = e.sender.getId(),
senderWindow = global.windows[windowId];
if(senderWindow) {
senderWindow.window.close();
delete global.windows[windowId];
}
});
ipc.on('backendAction_setWindowSize', function(e, width, height) {
var windowId = e.sender.getId(),
senderWindow = global.windows[windowId];
if(senderWindow) {
senderWindow.window.setSize(width, height);
senderWindow.window.center(); // ?
}
});
ipc.on('backendAction_sendToOwner', function(e, error, value) {
var windowId = e.sender.getId(),
senderWindow = global.windows[windowId];
var mainWindow = global.mainWindow;
if (_.get(senderWindow, 'owner')) {
senderWindow.owner.send('windowMessage', senderWindow.type, error, value);
if(mainWindow && mainWindow.webContents && !mainWindow.webContents.isDestroyed()) {
mainWindow.webContents.send('mistUI_windowMessage', senderWindow.type, senderWindow.owner.getId(), error, value);
}
}
});
ipc.on('backendAction_setLanguage', function(e, lang){
if(global.language !== lang) {
global.i18n.changeLanguage(lang.substr(0,2), function(err, t){
if(!err) {
global.language = global.i18n.language;
log.info('Backend language set to: ', global.language);
appMenu(global.webviews);
}
});
}
});
// import presale file
ipc.on('backendAction_importPresaleFile', function(e, path, pw) {
const spawn = require('child_process').spawn;
const getNodePath = require('./getNodePath.js');
var error = false;
// start import process
var nodeProcess = spawn(getNodePath('gexp'), ['wallet', 'import', path]);
nodeProcess.once('error',function(){
error = true;
e.sender.send('uiAction_importedPresaleFile', 'Couldn\'t start the "gexp wallet import <file.json>" process.');
});
nodeProcess.stdout.on('data', function(data) {
var data = data.toString();
if(data)
log.info('Imported presale: ', data);
if(/Decryption failed|not equal to expected addr|could not decrypt/.test(data)) {
e.sender.send('uiAction_importedPresaleFile', 'Decryption Failed');
// if imported, return the address
} else if(data.indexOf('Address:') !== -1) {
var find = data.match(/\{([a-f0-9]+)\}/i);
if(find.length && find[1])
e.sender.send('uiAction_importedPresaleFile', null, '0x'+ find[1]);
else
e.sender.send('uiAction_importedPresaleFile', data);
// if not stop, so we don't kill the process
} else {
return;
}
nodeProcess.stdout.removeAllListeners('data');
nodeProcess.removeAllListeners('error');
nodeProcess.kill('SIGINT');
});
// file password
setTimeout(function(){
if(!error) {
nodeProcess.stdin.write(pw +"\n");
pw = null;
}
}, 2000);
});
// MIST API
ipc.on('mistAPI_requestAccount', function(e){
popupWindow.show('requestAccount', {width: 400, height: 230, alwaysOnTop: true}, null, e);
});