-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpopup.mjs
441 lines (352 loc) · 12.2 KB
/
popup.mjs
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/**
* @name ScaleWallet - Everscale browser wallet and injector
* @copyright SVOI.dev Labs - https://svoi.dev
* @license Apache-2.0
* @version 1.0
*/
import ExtensionMessenger from "./modules/ExtensionMessenger.mjs";
import {default as theme} from "./modules/ui/theme.mjs";
import {default as popups} from "./modules/ui/popups.mjs";
import ROUTES from "./modules/ui/routes.mjs";
import EXCEPTIONS from "./modules/const/Exceptions.mjs";
import walletWidget from "./modules/ui/widgets/walletWidget.mjs";
import networkWidget from "./modules/ui/widgets/networkWidget.mjs";
import accountWidget from "./modules/ui/widgets/accountWidget.mjs";
import Utils from "./modules/utils.mjs";
import LOCALIZATION from "./modules/Localization.mjs";
import MISC from "./modules/const/Misc.mjs";
import LocalStorage from "./modules/LocalStorage.mjs";
import Browser from "./modules/internalBrowser/Browser.mjs";
import FingerprintAuth from "./modules/ui/FingerprintAuth.mjs";
import PluginManager from "./modules/PluginManager.mjs";
async function startPopup() {
const _ = LOCALIZATION._;
const RPC = {
'popup_test': async (a, b) => {
return a * b;
},
'popup_fall': async () => {
throw EXCEPTIONS.testException;
},
popup_testSign: (message, publicKey) => {
return new Promise((resolve, reject) => {
app.dialog.confirm(`${message} Pubkey: ${publicKey}`, _(`Action required`), () => {
resolve(true)
}, () => {
resolve(false)
});
})
},
/**
* Show toast message
* @param message
* @returns {Toast.Toast}
*/
popup_showToast: (message) => {
return new Promise((resolve, reject) => {
app.toast.create({
closeTimeout: 3000,
destroyOnClose: true,
text: message
}).open();
resolve(true);
});
},
/**
* Show password input window
* @param message
* @param publicKey
* @returns {Promise<unknown>}
*/
popup_password: (message, publicKey) => {
return new Promise((resolve, reject) => {
app.dialog.password(`${message} \n${_('Action password required for public key')}: ${Utils.shortenPubkey(publicKey)}`, _('Password required'), (password) => {
resolve(password)
}, () => {
resolve(false)
});
})
},
/**
* Show alert
* @param message
* @returns {Promise<unknown>}
*/
popup_alert: (message) => {
return new Promise((resolve, reject) => {
return app.dialog.alert(message, () => {
resolve()
})
});
},
/**
* Close current window
* @returns {Promise<boolean>}
*/
popup_close: async () => {
setTimeout(() => {
window.close();
}, 10);
return true;
},
/**
* Network changed event
* @returns {Promise<boolean>}
*/
popup_networkChanged: async () => {
console.log('NETWORK CHANGED');
await network.updateNetworkWidget();
await wallet.updateWalletWidget();
return true;
},
/**
* Account changed event
* @returns {Promise<boolean>}
*/
popup_accountChanged: async () => {
console.log('ACCOUNT CHANGED');
await account.updateAccountWidget();
await wallet.updateWalletWidget();
return true;
},
/**
* Show accept signing window
* @param publicKey
* @param type
* @param callingData
* @param acceptMessage
* @returns {Promise<*>}
*/
popup_acceptSignMessage: async (publicKey, type = 'run', callingData, acceptMessage) => {
callingData.additionalMessage = !callingData.additionalMessage ? acceptMessage : callingData.additionalMessage;
return popups.acceptTransaction(publicKey, type, callingData);
}
}
let messenger = new ExtensionMessenger('popup', RPC);
popups.messenger = messenger;
window.messenger = messenger;
if(location.hash === '#popNewWindow') {
try {
await messenger.rpcCall('mainOpenPopup', [{left: window.screenLeft, top: window.screenTop}], 'background');
// window.close();
// return;
} catch (e) {
console.error(`Can't open new window: ${e}`);
}
}
// Dom7
const $ = Dom7;
let appTheme = "aurora";
if(window._isApp) {
if(navigator.userAgent.toLowerCase().includes('android')) {
appTheme = 'md';
}
if(navigator.platform.toLowerCase().includes('ios')) {
appTheme = 'ios';
}
}
// Init App
const app = new Framework7({
id: "tonwallet",
root: "#app",
theme: appTheme,
autoDarkTheme: true,
view: {stackPages: true,},
dialog: {
title: 'ScaleWallet',
},
data: function () {
return {
user: {
firstName: "John",
lastName: "Doe",
},
};
},
methods: {
helloWorld: function () {
app.dialog.alert("Hello World!");
},
},
on: {
pageAfterIn: async function (event, page) {
await theme.updateState();
},
pageInit: async function (event, page) {
},
},
routes: ROUTES,
popup: {
closeOnEscape: true,
},
sheet: {
closeOnEscape: true,
},
popover: {
closeOnEscape: true,
},
actions: {
closeOnEscape: true,
},
});
window.app = app;
let localStorage = new LocalStorage();
await theme.updateState();
await theme.loadState();
window.theme = theme;
window.popups = popups;
let walletsObj = (await messenger.rpcCall('main_getAccount', undefined, 'background')).public;
// console.log(walletsObj, "<<<<<<");
// popups.initPage();
if(walletsObj == null) {
popups.initPage();
}
LOCALIZATION.updateLocalization();
//Glue code
let account = accountWidget(messenger, app);
await account.updateAccountWidget();
let network = networkWidget(messenger, app);
await network.updateNetworkWidget();
let wallet = walletWidget(messenger, app);
await wallet.updateWalletWidget();
LOCALIZATION.updateLocalization();
LOCALIZATION.startTimer();
let manifest = await Utils.fetchJSON('/package.json');
console.log('manifest', manifest);
$('#version').text('Version ' + manifest.version);
let currentLang = LOCALIZATION.currentLang;
console.log(currentLang);
$(`#${currentLang}`).attr("style", "color: blue");
$("#languageMenu li a").on("click", function (e) {
// $(this).attr("style", "color: blue");
// $("#languageMenu li a").find(".colour-change").attr("style", "");
// $(`#${e.target.id}`).attr("style", "color: blue");
// LOCALIZATION.changeLang(`${e.target.id}`);
// console.log(e.target);
// console.log(Dom7(this));
$("#languageMenu").find(".colour-change").attr("style", "");
$(this).attr("style", "color: blue");
LOCALIZATION.changeLang($(this).attr("id"));
})
$('.sendMoneyButton').click(async () => {
try {
await popups.createTransaction();
} catch (e) {
//app.dialog.alert(`Transaction error: <br> ${JSON.stringify(e)}`);
}
console.log('Transaction created');
})
//EVERWallet emulation
let everWalletEmulationToggle = app.toggle.get('#everWalletEmulation');
let everWalletEmulation = await localStorage.get('everWalletEmulation', true);
everWalletEmulationToggle.checked = !!everWalletEmulation;
everWalletEmulationToggle.on('change', async function () {
await localStorage.set('everWalletEmulation', everWalletEmulationToggle.checked);
});
let pluginManager = window.pluginManager = await (new PluginManager()).init();
await pluginManager.runUIPlugins();
/**
* Update sidebar accounts list
* @returns {Promise<void>}
*/
async function updateAccountsInSettings() {
$('#accountList').empty();
let pubKeys = await messenger.rpcCall('main_getPublicKeys', [], 'background');
for (let pubKey of pubKeys) {
let accHaveName = await messenger.rpcCall('main_getAccountName', [pubKey], 'background');
let buttonText = Utils.shortenPubkey(pubKey);
if(accHaveName !== "") {
buttonText = accHaveName;
}
let appendStr = `<li><a href="" id="${pubKey}">${buttonText}</a></li>`;
$('#accountList').append(appendStr);
$(`#${pubKey}`).on('click', () => {
popups.accSettings(pubKey);
app.panel.close('right', true);
});
}
}
window.updateAccounsInSettings = updateAccountsInSettings
$('#openSettings').on('click', async () => {
await updateAccountsInSettings();
app.panel.open($('.panel-right'));
})
//Cancel pending TX on window closing
window.addEventListener("beforeunload", function (e) {
$('#txCancelButton').click();
}, false);
$(window).on('focus', async () => {
//console.log('FOCUS IN');
clearInterval(window.focusOutTimer);
clearTimeout(window.closeReminderTimer);
if(window.closeReminder) {
window.closeReminder.close();
delete window.closeReminder;
}
})
$(window).on('mousemove', async () => {
if(document.hasFocus()) {
//console.log('MOUSE MOVE');
clearInterval(window.focusOutTimer);
clearTimeout(window.closeReminderTimer);
if(window.closeReminder) {
window.closeReminder.close();
delete window.closeReminder;
}
}
})
$(window).on('blur', async () => {
//console.log('FOCUS OUT');
window.focusOutTimer = setInterval(async () => {
let activeActions = await messenger.rpcCall('main_getActiveActions', undefined, 'background');
if(activeActions.length === 0) {
console.log('NO ACTIVITY DETECTED. CLOSING');
window.closeReminder = app.dialog.alert(_('The wallet popup will be closed in 5 seconds due to inactivity'))
window.closeReminderTimer = setTimeout(() => {
$('#txCancelButton').click();
window.close();
}, 5000);
}
}, MISC.POPUP_FOCUSOUT_CLOSE_TIMER);
})
//Heartbeat to background for saving popup info
const popupHeartbeat = async () => {
try {
await messenger.rpcCall('main_popupHeartbeat', undefined, 'background');
} catch (e) {
}
};
setInterval(popupHeartbeat, 500);
}
if(!window._isApp) {
startPopup();
} else {
//If app is started from popup, start popup
let fingerprint = new FingerprintAuth();
await fingerprint.init();
window.finger = fingerprint;
let internalBrowser;
let messenger = new ExtensionMessenger('page', {
page_hideBrowser: () => {
internalBrowser.hideBrowser();
},
page_showBrowser: () => {
internalBrowser.showBrowser();
},
});
internalBrowser = new Browser(messenger);
window.internalBrowser = internalBrowser;
$('.mobileHide').hide();
$('.mobileShow').show();
$('#openBrowser').click(() => {
if(internalBrowser.tabs.length === 0) {
internalBrowser.newTab();
} else {
internalBrowser.showBrowser();
}
})
document.addEventListener("backbutton", () => {
Utils.appBack();
}, false);
}
export default startPopup;