BlueWallet/BlueApp.js
2018-07-07 14:04:32 +01:00

66 lines
1.9 KiB
JavaScript

/**
* @exports {AppStorage}
*/
import { AppStorage } from './class';
let prompt = require('./prompt');
let EV = require('./events');
let currency = require('./currency');
let loc = require('./loc');
let A = require('./analytics');
/** @type {AppStorage} */
let BlueApp = new AppStorage();
async function startAndDecrypt(retry) {
let password = false;
if (await BlueApp.storageIsEncrypted()) {
do {
password = await prompt((retry && loc._.bad_password) || loc._.enter_password, loc._.storage_is_encrypted);
} while (!password);
}
let success = await BlueApp.loadFromDisk(password);
if (success) {
console.log('loaded from disk');
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].timeToRefresh()) {
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) {
// balance changed, thus txs too
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);
}
if (hadToRefresh && noErr) {
await BlueApp.saveToDisk(); // caching
}
}
if (!success && password) {
// we had password and yet could not load/decrypt
return startAndDecrypt(true);
}
}
A(A.ENUM.INIT);
startAndDecrypt();
currency.startUpdater();
module.exports = BlueApp;