-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathmain.js
347 lines (314 loc) · 10.1 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
/*global __dirname, process, require */
(function () {
'use strict'
const fs = require('fs');
const electron = require('electron')
const app = electron.app
const path = require('path')
const os = require('os')
const BrowserWindow = electron.BrowserWindow
const Tray = electron.Tray
const appName = app.getName()
const appVersion = app.getVersion()
const dataDir = app.getPath('userData') + path.sep
const cacheDir = app.getPath('userCache') + path.sep
const tempDir = app.getPath('temp') + path.sep
const homeDir = app.getPath('home') + path.sep
const hostname = os.hostname()
const username = (process.platform === 'win32') ? process.env.USERNAME : process.env.USER
const {HWW_API, KEYSTORE_API} = require('./src/common/constants')
const {AuthDataFilesystemBackendV2} = require('./src/main/authdata.filesystem.v2')
const HardwareWallet = require('./src/main/hardwareWalletLedger');
const HardwareWalletLedger = HardwareWallet.Ledger;
const defaultMenu = require('electron-default-menu');
const { Menu, shell } = electron;
app.on('ready', () => {
const menu = defaultMenu(app, shell);
Menu.setApplicationMenu(Menu.buildFromTemplate(menu));
});
// report crashes to the Electron project
// require('crash-reporter').start()
// adds debug features like hotkeys for triggering dev tools and reload
process.on('uncaughtException', onCrash)
// add this switch for the notification window
app.commandLine.appendSwitch('--enable-transparent-visuals')
/**
* createMainWindow - create main application window
*
* @return {type} description
*/
function createMainWindow () {
var win = new BrowserWindow({
width: 1280,
height: 800,
frame: true,
minWidth: 800,
minHeight: 600
})
win.loadURL('file://' + __dirname + '/src/index.html')
win.on('closed', onClosed)
win.webContents.on('crashed', onCrash)
win.on('unresponsive', onCrash)
return win
}
/**
* onClosed - description
*
* @return {type} description
*/
function onClosed () {
// deref the window
// for multiple windows store them in an array
mainWindow = null
}
/**
* onCrash - description
*
* @param {type} exc description
* @return {type} description
*/
function onCrash (exc) {
console.log(exc)
}
/**
* handleStartupEvent function - description
*
* @return {type} description
*/
var handleStartupEvent = function () {
if (process.platform !== 'win32') {
return false
}
var cp = require('child_process')
var path = require('path')
var updateDotExe = path.resolve(path.dirname(process.execPath), '..', 'update.exe')
var target = path.basename(process.execPath)
var squirrelCommand = process.argv[1]
switch (squirrelCommand) {
case '--squirrel-install':
case '--squirrel-updated':
// Optionally do things such as:
//
// - Install desktop and start menu shortcuts
// - Add your .exe to the PATH
// - Write to the registry for things like file associations and
// explorer context menus
// create shortcuts
cp.spawnSync(updateDotExe, ['--createShortcut', target], {
detached: true
})
// Always quit when done
app.quit()
return true
case '--squirrel-uninstall':
// Undo anything you did in the --squirrel-install and
// --squirrel-updated handlers
cp.spawnSync(updateDotExe, ['--removeShortcut', target], {
detached: true
})
// Always quit when done
app.quit()
return true
case '--squirrel-obsolete':
// This is called on the outgoing version of your app before
// we update to the new version - it's the opposite of
// --squirrel-updated
app.quit()
return true
}
}
// check if we are being called by insaller routine
if (handleStartupEvent()) {
return
}
// prevent window being GC'd
var mainWindow
var trayIcon
/**
* activate function - description
*
* @return {type} description
*/
app.on('activate', function () {
if (!mainWindow) {
mainWindow = createMainWindow()
}
})
/**
* ready function - description
*
* @return {type} description
*/
app.on('ready', function () {
mainWindow = createMainWindow();
try {
const isDev = require('electron-is-dev')
if (isDev) {
require('electron-debug')()
const electronDevTools = require('electron-devtools-installer')
electronDevTools.default(electronDevTools.ANGULARJS_BATARANG);
Promise.resolve()
.then(()=>require('devtron').install())
.then(()=>console.log('Devtron installed'))
.catch(()=>console.log('Devtron failed to install'));
}
} catch(e) {
console.log(`Skipped dev dependencies`)
}
HardwareWalletLedger.listeners.push((...args) => mainWindow.webContents.send(HWW_API.LISTEN, ...args));
HardwareWalletLedger.init();
})
/**
* sysConfig function - description
*
* @return {type} description
*/
app.sysConfig = function () {
return {
app: {
name: appName,
version: appVersion,
icon: path.join(__dirname, 'assets', 'boilerplate.png')
},
host: hostname,
platform: process.platform,
user: username,
paths: {
home: homeDir,
temp: tempDir,
data: dataDir,
cache: cacheDir
}
}
}
/**
* getMainWindow function - description
*
* @return {type} description
*/
app.getMainWindow = function () {
return mainWindow
}
/**
* close function - description
*
* @return {type} description
*/
app.close = function () {
if (mainWindow) {
mainWindow.close()
}
app.quit()
}
/**
* toggleFullscreen function - description
*
* @return {type} description
*/
app.toggleFullscreen = function () {
if (mainWindow) {
mainWindow.setFullScreen(!mainWindow.isFullScreen())
}
}
/**
* minimizeAppToSysTray function - description
*
* @return {type} description
*/
app.minimizeAppToSysTray = function () {
trayIcon = new Tray(path.join(__dirname, 'assets', 'boilerplate_tray.png'))
trayIcon.setToolTip('App is running in background mode.')
trayIcon.on('click', () => {
if (mainWindow) {
mainWindow.show()
trayIcon.destroy()
}
})
if (mainWindow) {
mainWindow.hide()
}
}
const wrapIPC = (channel, routine, postCallback) => {
electron.ipcMain.on(channel, async (event, reqId, ...args) => {
// console.log(`Request<${channel}/${reqId}> arguments:`, ...args)
try {
if(!reqId) throw new Error(`No request ID provided. Consider passing a random number.`)
const res = await routine(...args);
if(postCallback) await postCallback(res);
event.sender.send(channel, reqId, null, res);
} catch(err) {
console.error(`Request<${channel}/${reqId}> error:`, err);
event.sender.send(channel, reqId, `Request<${channel}/${reqId}> failed: ${err.message}`);
}
});
}
//// authdata.filesystem.v1 API
wrapIPC('writeFile', (path, dataString) => {
return fs.writeFileSync(path, dataString);
})
wrapIPC('readFile', (path) => {
return fs.readFileSync(path, 'utf-8');
})
//// authdata.filesystem.v2 API
let authData;
wrapIPC(KEYSTORE_API.CREATE, async (opts) => {
authData = await AuthDataFilesystemBackendV2.create(opts);
return authData.toJSON();
})
wrapIPC(KEYSTORE_API.LOAD, async (opts) => {
authData = await AuthDataFilesystemBackendV2.load(opts)
return authData.toJSON();
})
wrapIPC(KEYSTORE_API.SAVE, async () => {
await authData.save();
return authData.toJSON();
})
wrapIPC(KEYSTORE_API.LOGOUT, async () => {
authData = undefined;
return;
})
wrapIPC(KEYSTORE_API.SIGN, async (publicKey, teHash) => {
return authData.signWithEncryptedSecret(publicKey, teHash).toXDR().toString('base64');
})
wrapIPC(KEYSTORE_API.ADDCONTACT, async (contact) => {
await authData.addContact(contact);
return authData.toJSON().contacts;
})
wrapIPC(KEYSTORE_API.UPDATECONTACT, async (name, contact) => {
await authData.updateContact(name, contact);
return authData.toJSON().contacts;
})
wrapIPC(KEYSTORE_API.DELETECONTACT, async (name) => {
await authData.deleteContact(name);
return authData.toJSON().contacts;
})
//// Hardware Wallet API
electron.ipcMain.on(HWW_API.SUPPORT, (event, reqId) => {
HardwareWalletLedger.isSupported()
.then((res) =>event.sender.send(HWW_API.SUPPORT, reqId, null, res))
.catch((err)=>event.sender.send(HWW_API.SUPPORT, reqId, `Request<${HWW_API.SUPPORT}/${reqId}> failed: ${err.message}`))
})
electron.ipcMain.on(HWW_API.SET_BIP44, (event, reqId, bip44) => {
HardwareWalletLedger.setBip44(bip44)
.then((res) =>event.sender.send(HWW_API.SET_BIP44, reqId, null, res))
.catch((err)=>event.sender.send(HWW_API.SET_BIP44, reqId, `Request<${HWW_API.SET_BIP44}/${reqId}> failed: ${err.message}`))
})
electron.ipcMain.on(HWW_API.LIST, (event, id) => {
event.sender.send(HWW_API.LIST, id, null, HardwareWalletLedger.list());
})
const wrapHwwMethod = (method, channel, callback) => {
return wrapIPC(channel, async (hwwId, ...args) => {
if(!hwwId) throw new Error(`No hardwallet ID provided. Consider using 'HWW_API.LIST'.`)
const ledger = HardwareWalletLedger.listOfLedgers.get(hwwId);
const res = await ledger[method](...args);
if(callback) callback(res);
return res;
});
};
wrapHwwMethod('getAppConfig' , HWW_API.CONFIG /*, (res)=>console.log(res) || res */)
wrapHwwMethod('selectSubaccount' , HWW_API.SELECT /*, (res)=>console.log(res) || res */)
wrapHwwMethod('deselectSubaccount', HWW_API.DESELECT /*, (res)=>console.log(res) || res */)
wrapHwwMethod('getPublicKey' , HWW_API.PK /*, (res)=>console.log(res) || res */)
wrapHwwMethod('signTe' , HWW_API.SIGN_TE /*, (res)=>console.log(res) || res */)
wrapHwwMethod('signHash' , HWW_API.SIGN_HASH /*, (res)=>console.log(res) || res */)
})()