BlueWallet/BlueApp.js

73 lines
2.0 KiB
JavaScript
Raw Normal View History

2018-01-30 23:42:38 +01:00
/**
* @exports {AppStorage}
*/
2018-03-17 21:39:21 +01:00
import { AppStorage } from './class';
2018-06-25 00:20:08 +02:00
import { Amplitude } from 'expo';
2018-06-30 13:58:00 +02:00
import { Analytics, PageHit } from 'expo-analytics';
2018-03-31 02:03:58 +02:00
let prompt = require('./prompt');
2018-03-17 21:39:21 +01:00
let EV = require('./events');
2018-05-30 01:17:44 +02:00
let loc = require('./loc');
2018-01-30 23:42:38 +01:00
2018-03-31 02:03:58 +02:00
/** @type {AppStorage} */
2018-03-17 21:39:21 +01:00
let BlueApp = new AppStorage();
2018-03-31 02:03:58 +02:00
2018-03-31 15:43:08 +02:00
async function startAndDecrypt(retry) {
2018-03-31 02:03:58 +02:00
let password = false;
if (await BlueApp.storageIsEncrypted()) {
2018-04-01 01:16:42 +02:00
do {
password = await prompt(
2018-05-30 01:17:44 +02:00
(retry && loc._.bad_password) || loc._.enter_password,
loc._.storage_is_encrypted,
2018-04-01 01:16:42 +02:00
);
} while (!password);
2018-03-31 02:03:58 +02:00
}
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);
2018-07-02 15:51:24 +02:00
// 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();
2018-07-03 22:10:42 +02:00
if (oldBalance !== wallets[0].getBalance()) {
2018-07-02 15:51:24 +02:00
// balance changed, thus txs too
await wallets[0].fetchTransactions();
hadToRefresh = true;
EV(EV.enum.WALLETS_COUNT_CHANGED);
EV(EV.enum.TRANSACTIONS_COUNT_CHANGED);
}
} // end of timeToRefresh
} catch (Err) {
noErr = false;
console.warn(Err);
}
if (hadToRefresh && noErr) {
await BlueApp.saveToDisk(); // caching
}
2018-03-31 02:03:58 +02:00
}
if (!success && password) {
// we had password and yet could not load/decrypt
2018-04-01 01:16:42 +02:00
return startAndDecrypt(true);
2018-03-31 02:03:58 +02:00
}
2018-03-31 15:43:08 +02:00
}
2018-06-25 00:20:08 +02:00
Amplitude.initialize('8b7cf19e8eea3cdcf16340f5fbf16330');
2018-06-29 23:14:03 +02:00
Amplitude.logEvent('INIT');
2018-06-30 13:58:00 +02:00
const analytics = new Analytics('UA-121673546-1');
analytics
.hit(new PageHit('INIT'))
.then(() => console.log('success'))
.catch(e => console.log(e.message));
2018-03-31 15:43:08 +02:00
startAndDecrypt();
2018-01-30 23:42:38 +01:00
2018-03-17 21:39:21 +01:00
module.exports = BlueApp;