2018-01-30 23:42:38 +01:00
|
|
|
/**
|
|
|
|
* @exports {AppStorage}
|
|
|
|
*/
|
2018-03-17 21:39:21 +01:00
|
|
|
import { AppStorage } from './class';
|
2020-05-24 12:27:08 +02:00
|
|
|
import DeviceQuickActions from './class/quick-actions';
|
2020-07-20 16:58:35 +02:00
|
|
|
import Biometric from './class/biometrics';
|
|
|
|
import { Platform } from 'react-native';
|
2020-07-20 15:38:46 +02:00
|
|
|
import loc from './loc';
|
2020-07-01 13:56:52 +02:00
|
|
|
const prompt = require('./blue_modules/prompt');
|
|
|
|
const EV = require('./blue_modules/events');
|
2020-06-09 16:08:18 +02:00
|
|
|
const currency = require('./blue_modules/currency');
|
2020-07-01 13:56:52 +02:00
|
|
|
const BlueElectrum = require('./blue_modules/BlueElectrum'); // eslint-disable-line no-unused-vars
|
2018-03-31 02:03:58 +02:00
|
|
|
/** @type {AppStorage} */
|
2019-05-02 22:33:03 +02:00
|
|
|
const BlueApp = new AppStorage();
|
2020-07-20 16:58:35 +02:00
|
|
|
// If attempt reaches 10, a wipe keychain option will be provided to the user.
|
|
|
|
let unlockAttempt = 0;
|
2018-03-31 02:03:58 +02:00
|
|
|
|
2018-03-31 15:43:08 +02:00
|
|
|
async function startAndDecrypt(retry) {
|
2018-12-31 17:20:49 +01:00
|
|
|
console.log('startAndDecrypt');
|
|
|
|
if (BlueApp.getWallets().length > 0) {
|
|
|
|
console.log('App already has some wallets, so we are in already started state, exiting startAndDecrypt');
|
2020-07-23 20:04:44 +02:00
|
|
|
return true;
|
2018-12-31 17:20:49 +01:00
|
|
|
}
|
2018-03-31 02:03:58 +02:00
|
|
|
let password = false;
|
|
|
|
if (await BlueApp.storageIsEncrypted()) {
|
2019-11-03 01:25:55 +01:00
|
|
|
DeviceQuickActions.clearShortcutItems();
|
2018-04-01 01:16:42 +02:00
|
|
|
do {
|
2018-10-09 06:25:36 +02:00
|
|
|
password = await prompt((retry && loc._.bad_password) || loc._.enter_password, loc._.storage_is_encrypted, false);
|
2018-04-01 01:16:42 +02:00
|
|
|
} while (!password);
|
2018-03-31 02:03:58 +02:00
|
|
|
}
|
2020-06-01 14:54:23 +02:00
|
|
|
const success = await BlueApp.loadFromDisk(password);
|
2018-03-31 02:03:58 +02:00
|
|
|
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
|
2019-02-10 02:08:33 +01:00
|
|
|
/* let hadToRefresh = false;
|
2018-07-02 15:51:24 +02:00
|
|
|
let noErr = true;
|
|
|
|
try {
|
|
|
|
let wallets = BlueApp.getWallets();
|
2018-07-14 22:32:36 +02:00
|
|
|
if (wallets && wallets[0] && wallets[0].timeToRefreshBalance()) {
|
2018-07-02 15:51:24 +02:00
|
|
|
console.log('time to refresh wallet #0');
|
|
|
|
let oldBalance = wallets[0].getBalance();
|
|
|
|
await wallets[0].fetchBalance();
|
2018-09-01 02:09:45 +02:00
|
|
|
if (oldBalance !== wallets[0].getBalance() || wallets[0].getUnconfirmedBalance() !== 0 || wallets[0].timeToRefreshTransaction()) {
|
2018-07-02 15:51:24 +02:00
|
|
|
// balance changed, thus txs too
|
2018-09-01 02:09:45 +02:00
|
|
|
// or wallet thinks its time to reload TX list
|
2018-07-02 15:51:24 +02:00
|
|
|
await wallets[0].fetchTransactions();
|
|
|
|
hadToRefresh = true;
|
|
|
|
EV(EV.enum.WALLETS_COUNT_CHANGED);
|
|
|
|
EV(EV.enum.TRANSACTIONS_COUNT_CHANGED);
|
2018-07-05 02:56:31 +02:00
|
|
|
} else {
|
|
|
|
console.log('balance not changed');
|
2018-07-02 15:51:24 +02:00
|
|
|
}
|
|
|
|
} // end of timeToRefresh
|
|
|
|
} catch (Err) {
|
|
|
|
noErr = false;
|
|
|
|
console.warn(Err);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hadToRefresh && noErr) {
|
|
|
|
await BlueApp.saveToDisk(); // caching
|
2019-02-10 02:08:33 +01:00
|
|
|
} */
|
2020-07-20 16:58:35 +02:00
|
|
|
// We want to return true to let the UnlockWith screen that its ok to proceed.
|
|
|
|
return true;
|
2018-03-31 02:03:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!success && password) {
|
|
|
|
// we had password and yet could not load/decrypt
|
2020-07-20 16:58:35 +02:00
|
|
|
unlockAttempt++;
|
|
|
|
if (unlockAttempt < 10 || Platform.OS !== 'ios') {
|
|
|
|
return startAndDecrypt(true);
|
|
|
|
} else {
|
|
|
|
unlockAttempt = 0;
|
|
|
|
Biometric.showKeychainWipeAlert();
|
|
|
|
// We want to return false to let the UnlockWith screen that it is NOT ok to proceed.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
2020-07-21 00:14:02 +02:00
|
|
|
unlockAttempt = 0;
|
2020-07-20 16:58:35 +02:00
|
|
|
// Return true because there was no wallet data in keychain. Proceed.
|
|
|
|
return true;
|
2018-03-31 02:03:58 +02:00
|
|
|
}
|
2018-03-31 15:43:08 +02:00
|
|
|
}
|
|
|
|
|
2018-12-11 23:52:46 +01:00
|
|
|
BlueApp.startAndDecrypt = startAndDecrypt;
|
2018-07-06 17:40:58 +02:00
|
|
|
currency.startUpdater();
|
2018-01-30 23:42:38 +01:00
|
|
|
|
2018-03-17 21:39:21 +01:00
|
|
|
module.exports = BlueApp;
|