2018-03-17 22:39:21 +02:00
|
|
|
import { AppStorage } from './class';
|
2020-07-20 10:58:35 -04:00
|
|
|
import Biometric from './class/biometrics';
|
|
|
|
import { Platform } from 'react-native';
|
2020-07-20 16:38:46 +03:00
|
|
|
import loc from './loc';
|
2020-07-01 12:56:52 +01:00
|
|
|
const prompt = require('./blue_modules/prompt');
|
2020-06-09 15:08:18 +01:00
|
|
|
const currency = require('./blue_modules/currency');
|
2021-07-17 22:58:24 +02:00
|
|
|
const BlueElectrum = require('./blue_modules/BlueElectrum'); // eslint-disable-line @typescript-eslint/no-unused-vars
|
2021-08-10 21:29:56 +02:00
|
|
|
BlueElectrum.connectMain();
|
2021-07-17 22:37:06 +02:00
|
|
|
const BlueApp = new AppStorage();
|
2020-07-20 10:58:35 -04:00
|
|
|
// If attempt reaches 10, a wipe keychain option will be provided to the user.
|
|
|
|
let unlockAttempt = 0;
|
2018-03-31 01:03:58 +01:00
|
|
|
|
2020-10-24 13:20:59 -04:00
|
|
|
const startAndDecrypt = async retry => {
|
2018-12-31 16:20:49 +00: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 19:04:44 +01:00
|
|
|
return true;
|
2018-12-31 16:20:49 +00:00
|
|
|
}
|
2021-05-18 21:38:18 +01:00
|
|
|
await BlueApp.migrateKeys();
|
2018-03-31 01:03:58 +01:00
|
|
|
let password = false;
|
|
|
|
if (await BlueApp.storageIsEncrypted()) {
|
2018-04-01 00:16:42 +01:00
|
|
|
do {
|
2018-10-09 00:25:36 -04:00
|
|
|
password = await prompt((retry && loc._.bad_password) || loc._.enter_password, loc._.storage_is_encrypted, false);
|
2018-04-01 00:16:42 +01:00
|
|
|
} while (!password);
|
2018-03-31 01:03:58 +01:00
|
|
|
}
|
2021-02-12 23:38:29 +00:00
|
|
|
let success = false;
|
|
|
|
let wasException = false;
|
|
|
|
try {
|
|
|
|
success = await BlueApp.loadFromDisk(password);
|
|
|
|
} catch (error) {
|
|
|
|
// in case of exception reading from keystore, lets retry instead of assuming there is no storage and
|
|
|
|
// proceeding with no wallets
|
2021-05-24 13:16:03 +01:00
|
|
|
console.warn('exception loading from disk:', error);
|
2021-02-12 23:38:29 +00:00
|
|
|
wasException = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wasException) {
|
|
|
|
// retrying, but only once
|
2018-07-02 14:51:24 +01:00
|
|
|
try {
|
2021-02-12 23:38:29 +00:00
|
|
|
await new Promise(resolve => setTimeout(resolve, 3000)); // sleep
|
|
|
|
success = await BlueApp.loadFromDisk(password);
|
2021-05-24 13:16:03 +01:00
|
|
|
} catch (error) {
|
|
|
|
console.warn('second exception loading from disk:', error);
|
|
|
|
}
|
2021-02-12 23:38:29 +00:00
|
|
|
}
|
2018-07-02 14:51:24 +01:00
|
|
|
|
2021-02-12 23:38:29 +00:00
|
|
|
if (success) {
|
|
|
|
console.log('loaded from disk');
|
2020-07-20 10:58:35 -04:00
|
|
|
// We want to return true to let the UnlockWith screen that its ok to proceed.
|
|
|
|
return true;
|
2018-03-31 01:03:58 +01:00
|
|
|
}
|
|
|
|
|
2021-02-12 23:38:29 +00:00
|
|
|
if (password) {
|
2018-03-31 01:03:58 +01:00
|
|
|
// we had password and yet could not load/decrypt
|
2020-07-20 10:58:35 -04: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-20 18:14:02 -04:00
|
|
|
unlockAttempt = 0;
|
2020-07-20 10:58:35 -04:00
|
|
|
// Return true because there was no wallet data in keychain. Proceed.
|
|
|
|
return true;
|
2018-03-31 01:03:58 +01:00
|
|
|
}
|
2020-10-24 13:20:59 -04:00
|
|
|
};
|
2018-03-31 14:43:08 +01:00
|
|
|
|
2018-12-11 22:52:46 +00:00
|
|
|
BlueApp.startAndDecrypt = startAndDecrypt;
|
2021-09-09 20:36:35 +01:00
|
|
|
currency.init();
|
2018-01-30 22:42:38 +00:00
|
|
|
|
2018-03-17 22:39:21 +02:00
|
|
|
module.exports = BlueApp;
|