forked from BlueWallet/BlueWallet
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathBlueApp.js
More file actions
81 lines (71 loc) · 2.31 KB
/
BlueApp.js
File metadata and controls
81 lines (71 loc) · 2.31 KB
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
import { AppStorage } from './class';
import logger from './logger';
/**
* @exports {AppStorage}
*/
const EV = require('./events');
const loc = require('./loc');
const prompt = require('./prompt');
/** @type {AppStorage} */
const BlueApp = new AppStorage();
async function startAndDecrypt(retry) {
if (BlueApp.getWallets().length > 0) {
logger.info({
message: 'App already has some wallets, so we are in already started state, exiting startAndDecrypt',
category: 'BlueApp',
});
return;
}
let password = false;
if (await BlueApp.storageIsEncrypted()) {
do {
password = await prompt((retry && loc._.bad_password) || loc._.enter_password, loc._.storage_is_encrypted, false);
} while (!password);
}
const success = await BlueApp.loadFromDisk(password);
if (success) {
logger.info({
message: 'loaded from disk',
category: 'BlueApp',
});
EV(EV.enum.WALLETS_COUNT_CHANGED);
EV(EV.enum.TRANSACTIONS_COUNT_CHANGED);
// now, lets try to fetch balance and txs for first wallet if it is time for it
/* let hadToRefresh = false;
let noErr = true;
try {
let wallets = BlueApp.getWallets();
if (wallets && wallets[0] && wallets[0].timeToRefreshBalance()) {
console.log('time to refresh wallet #0');
let oldBalance = wallets[0].getBalance();
await wallets[0].fetchBalance();
if (oldBalance !== wallets[0].getBalance() || wallets[0].getUnconfirmedBalance() !== 0 || wallets[0].timeToRefreshTransaction()) {
// balance changed, thus txs too
// or wallet thinks its time to reload TX list
await wallets[0].fetchTransactions();
hadToRefresh = true;
EV(EV.enum.WALLETS_COUNT_CHANGED);
EV(EV.enum.TRANSACTIONS_COUNT_CHANGED);
} else {
console.log('balance not changed');
}
} // end of timeToRefresh
} catch (Err) {
noErr = false;
console.warn(Err);
} catch (Err) {
noErr = false;
console.warn(Err);
}
if (hadToRefresh && noErr) {
await BlueApp.saveToDisk(); // caching
} */
}
if (!success && password) {
// we had password and yet could not load/decrypt
return startAndDecrypt(true);
}
}
BlueApp.startAndDecrypt = startAndDecrypt;
module.exports = BlueApp;
export default BlueApp;