BlueWallet/blue_modules/start-and-decrypt.ts

71 lines
2.2 KiB
TypeScript
Raw Normal View History

2024-03-15 21:05:15 +01:00
import { Platform } from 'react-native';
2024-05-20 11:54:13 +02:00
2024-04-15 22:53:44 +02:00
import { BlueApp as BlueAppClass } from '../class/';
2024-05-20 11:54:13 +02:00
import prompt from '../helpers/prompt';
2024-05-18 03:10:45 +02:00
import { showKeychainWipeAlert } from '../hooks/useBiometrics';
2024-05-20 11:54:13 +02:00
import loc from '../loc';
2023-04-21 17:39:12 +02:00
2024-04-15 22:53:44 +02:00
const BlueApp = BlueAppClass.getInstance();
// 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
2024-03-15 21:05:15 +01:00
export const startAndDecrypt = async (retry?: boolean): Promise<boolean> => {
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;
}
2021-05-18 22:38:18 +02:00
await BlueApp.migrateKeys();
2024-03-15 21:05:15 +01:00
let password: undefined | string;
2018-03-31 02:03:58 +02:00
if (await BlueApp.storageIsEncrypted()) {
2018-04-01 01:16:42 +02:00
do {
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
}
2021-02-13 00:38:29 +01: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 14:16:03 +02:00
console.warn('exception loading from disk:', error);
2021-02-13 00:38:29 +01:00
wasException = true;
}
if (wasException) {
// retrying, but only once
2018-07-02 15:51:24 +02:00
try {
2021-02-13 00:38:29 +01:00
await new Promise(resolve => setTimeout(resolve, 3000)); // sleep
success = await BlueApp.loadFromDisk(password);
2021-05-24 14:16:03 +02:00
} catch (error) {
console.warn('second exception loading from disk:', error);
}
2021-02-13 00:38:29 +01:00
}
2018-07-02 15:51:24 +02:00
2021-02-13 00:38:29 +01:00
if (success) {
console.log('loaded from disk');
// 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
}
2021-02-13 00:38:29 +01:00
if (password) {
2018-03-31 02:03:58 +02:00
// we had password and yet could not load/decrypt
unlockAttempt++;
if (unlockAttempt < 10 || Platform.OS !== 'ios') {
return startAndDecrypt(true);
} else {
unlockAttempt = 0;
2024-05-18 00:34:39 +02:00
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;
// 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
2024-03-15 21:05:15 +01:00
export default BlueApp;