forked from Macroshock/WalletShell
/
main.js
315 lines (276 loc) · 9.22 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
const {app, BrowserWindow, dialog, Tray, Menu, NativeImage} = require('electron');
const path = require('path');
const url = require('url');
const https = require('https');
const platform = require('os').platform();
const crypto = require('crypto');
const Store = require('electron-store');
const settings = new Store({name: 'Settings'});
const IS_DEBUG = (process.argv[1] === 'debug' || process.argv[2] === 'debug');
const SERVICE_FILENAME = (platform === 'win32' ? 'turtle-service.exe' : 'turtle-service' );
const DEFAULT_SERVICE_BIN = path.join(process.resourcesPath, SERVICE_FILENAME);
const DEFAULT_TITLE = 'TurtleCoin Wallet';
const PUBLIC_NODES_URL = 'https://raw.githubusercontent.com/turtlecoin/turtlecoin-nodes-json/master/turtlecoin-nodes.json';
const FALLBACK_NODES = [
'public.turtlenode.io:11898',
'public.turtlenode.net:11898',
];
const DEFAULT_SETTINGS = {
service_bin: DEFAULT_SERVICE_BIN,
service_host: '127.0.0.1',
service_port: 8070,
service_password: crypto.randomBytes(32).toString('hex'),
daemon_host: '127.0.0.1',
daemon_port: 11898,
pubnodes_date: null,
pubnodes_data: FALLBACK_NODES,
pubnodes_custom: ['127.0.0.1:11898'],
tray_minimize: false,
tray_close: false
}
let win;
app.prompExit = true;
app.needToExit = false;
let trayIcon = path.join(__dirname,'src/assets/tray_24x24.png');
if(platform === 'darwin'){
trayIcon = path.join(__dirname,'src/assets/tray.icns');
}else if(platform === 'win32'){
trayIcon = path.join(__dirname,'src/assets/tray.ico');
}
let trayIconHide = path.join(__dirname,'src/assets/trayon_24x24.png');
if(platform === 'darwin'){
trayIconHide = path.join(__dirname,'src/assets/trayon.icns');
}else if(platform === 'win32'){
trayIconHide = path.join(__dirname,'src/assets/trayon.ico');
}
function createWindow () {
// Create the browser window.
win = new BrowserWindow({
title: DEFAULT_TITLE,
icon: path.join(__dirname,'src/assets/walletshell_icon.png'),
frame: true,//frame: false,
width: 800,
height: 680,
minWidth: 800,
minHeight: 680,
show: false,
backgroundColor: '#02853e',
});
let contextMenu = Menu.buildFromTemplate([
{ label: 'Minimize to tray', click: () => { win.hide(); }},
{ label: 'Quit', click: ()=> {
app.needToExit = true;
win.close();
}
}
]);
// linux default;
const tray = new Tray(trayIcon);
tray.setPressedImage(trayIconHide);
tray.setTitle(DEFAULT_TITLE);
tray.setToolTip('Slow and steady wins the race!');
tray.setContextMenu(contextMenu);
tray.on('click', () => {
win.isVisible() ? win.hide() : win.show();
});
win.on('show', () => {
tray.setHighlightMode('always');
tray.setImage(trayIcon);
contextMenu = Menu.buildFromTemplate([
{ label: 'Minimize to tray', click: () => { win.hide();} },
{ label: 'Quit', click: ()=> {
app.needToExit = true;
win.close();
}
}
]);
tray.setContextMenu(contextMenu);
});
win.on('hide', () => {
tray.setHighlightMode('never');
tray.setImage(trayIconHide);
contextMenu = Menu.buildFromTemplate([
{ label: 'Restore', click: () => { win.show();} },
{ label: 'Quit', click: ()=> {
app.needToExit = true;
win.close();
}
}
]);
tray.setContextMenu(contextMenu);
});
win.on('minimize', (event) => {
if(settings.get('tray_minimize')){
event.preventDefault();
win.hide();
}
});
//load the index.html of the app.
win.loadURL(url.format({
pathname: path.join(__dirname, 'src/html/index.html'),
protocol: 'file:',
slashes: true
}));
// open devtools
if(IS_DEBUG ) win.webContents.openDevTools();
// show windosw
win.once('ready-to-show', () => {
win.show();
win.setTitle(DEFAULT_TITLE);
})
win.on('close', (e) => {
if(settings.get('tray_close') && !app.needToExit){
e.preventDefault();
win.hide();
}else if(app.prompExit){
e.preventDefault();
let msg = 'Are you sure want to exit?';
if(wsession.loadedWalletAddress !== '') msg = 'Are you sure want to close your wallet and exit?';
dialog.showMessageBox({
type: 'question',
buttons: ['Yes', 'No'],
title: 'Exit Confirmation',
message: msg
}, function (response) {
if (response === 0) {
app.prompExit = false;
win.webContents.send('cleanup','Clean it up, Dad!');
}else{
app.prompExit = true;
app.needToExit = false;
}
});
}
});
win.on('closed', () => {
win = null;
});
win.setMenu(null);
// misc handler
win.webContents.on('crashed', (event, killed) => {
// todo: prompt to restart
if(IS_DEBUG) console.log('webcontent was crashed', event, killed);
});
win.on('unresponsive', (even) => {
// todo: prompt to restart
if(IS_DEBUG) console.log('unresponsive!');
});
}
function storeNodeList(pnodes){
pnodes = pnodes || settings.get('pubnodes_data');
if( pnodes.hasOwnProperty('nodes')){
pnodes.nodes.forEach(element => {
let item = `${element.url}:${element.port}`;
global.wsession.nodeChoices.push(item);
});
}
settings.set('pubnodes_data', global.wsession.nodeChoices);
}
function doNodeListUpdate(){
https.get(PUBLIC_NODES_URL, (res) => {
var result = '';
res.setEncoding('utf8');
res.on('data', (chunk) => {
result += chunk;
});
res.on('end', () => {
try{
var pnodes = JSON.parse(result);
let today = new Date();
storeNodeList(pnodes);
if(IS_DEBUG) console.log('nodelist has been updated');
let mo = (today.getMonth()+1);
settings.set('pubnodes_date', `${today.getFullYear()}-${mo}-${today.getDate()}`);
}catch(e){
if(IS_DEBUG) console.log('failed to parse json data', e);
if(IS_DEBUG) console.log('type of input', typeof d);
if(IS_DEBUG) console.log(JSON.stringify(d));
storeNodeList();
}
});
}).on('error', (e) => {
console.error(e);
});
}
function initSettings(){
Object.keys(DEFAULT_SETTINGS).forEach((k) => {
if(!settings.has(k) || settings.get(k) === null){
settings.set(k, DEFAULT_SETTINGS[k]);
}
});
}
const silock = app.requestSingleInstanceLock()
app.on('second-instance', (commandLine, workingDirectory) => {
if (win) {
if (!win.isVisible()) win.show();
if (win.isMinimized()) win.restore();
win.focus();
}
});
if (!silock) app.quit();
app.on('ready', () => {
initSettings();
global.wsession = {
loadedWalletAddress: '',
walletHash: '',
synchronized: false,
syncStarted: false,
serviceReady: false,
txList: [],
txLen: 0,
txLastHash: '',
txLastTimestamp: '',
txNew: [],
tmpPath: app.getPath('temp'),
dataPath: app.getPath('userData'),
nodeFee: null,
nodeChoices: settings.get('pubnodes_data'),
servicePath: settings.get('service_bin'),
configUpdated: false,
uiStateChanged: false,
defaultTitle: DEFAULT_TITLE,
debug: IS_DEBUG
};
if(IS_DEBUG) console.log('Running in debug mode');
let today = new Date();
let last_checked = new Date(settings.get('pubnodes_date'));
diff_d = parseInt((today-last_checked)/(1000*60*60*24),10);
if(diff_d >= 1){
console.log('Performing daily node list update.');
doNodeListUpdate();
}else{
console.log('Node list is up to date.');
storeNodeList(false); // from local cache
}
createWindow();
});
// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') app.quit();
});
app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (win === null) createWindow();
});
process.on('uncaughtException', function (err) {
if(IS_DEBUG) console.log(err);
process.exit(1);
});
process.on('beforeExit', (code) => {
if(IS_DEBUG) console.log(`beforeExit code: ${code}`);
});
process.on('exit', (code) => {
if(IS_DEBUG) console.log(`exit with code: ${code}`);
});
process.on('warning', (warning) => {
if(IS_DEBUG){
console.warn(warning.name);
console.warn(warning.message);
console.warn(warning.code);
console.warn(warning.stack);
console.warn(warning.detail);
}
});