REF: load from disk

This commit is contained in:
Overtorment 2021-02-12 23:38:29 +00:00
parent 6c47651d63
commit ade43b3eb2
2 changed files with 112 additions and 132 deletions

View File

@ -21,42 +21,32 @@ const startAndDecrypt = async retry => {
password = await prompt((retry && loc._.bad_password) || loc._.enter_password, loc._.storage_is_encrypted, false);
} while (!password);
}
const success = await BlueApp.loadFromDisk(password);
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
console.warn(error);
wasException = true;
}
if (wasException) {
// retrying, but only once
try {
await new Promise(resolve => setTimeout(resolve, 3000)); // sleep
success = await BlueApp.loadFromDisk(password);
} catch (_) {}
}
if (success) {
console.log('loaded from disk');
// 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].timeToRefreshBalance()) {
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 || wallets[0].timeToRefreshTransaction()) {
// balance changed, thus txs too
// or wallet thinks its time to reload TX list
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
} */
// We want to return true to let the UnlockWith screen that its ok to proceed.
return true;
}
if (!success && password) {
if (password) {
// we had password and yet could not load/decrypt
unlockAttempt++;
if (unlockAttempt < 10 || Platform.OS !== 'ios') {

View File

@ -76,7 +76,6 @@ export class AppStorage {
}
};
storageIsEncrypted = async () => {
let data;
try {
@ -111,11 +110,7 @@ export class AppStorage {
let decrypted;
let num = 0;
for (const value of data) {
try {
decrypted = encryption.decrypt(value, password);
} catch (e) {
console.log(e.message);
}
decrypted = encryption.decrypt(value, password);
if (decrypted) {
usedBucketNum = num;
@ -221,109 +216,104 @@ export class AppStorage {
* @returns {Promise.<boolean>}
*/
async loadFromDisk(password) {
try {
let data = await this.getItem('data');
if (password) {
data = this.decryptData(data, password);
if (data) {
// password is good, cache it
this.cachedPassword = password;
}
let data = await this.getItem('data');
if (password) {
data = this.decryptData(data, password);
if (data) {
// password is good, cache it
this.cachedPassword = password;
}
if (data !== null) {
const realm = await this.getRealm();
data = JSON.parse(data);
if (!data.wallets) return false;
const wallets = data.wallets;
for (const key of wallets) {
// deciding which type is wallet and instatiating correct object
const tempObj = JSON.parse(key);
let unserializedWallet;
switch (tempObj.type) {
case PlaceholderWallet.type:
}
if (data !== null) {
const realm = await this.getRealm();
data = JSON.parse(data);
if (!data.wallets) return false;
const wallets = data.wallets;
for (const key of wallets) {
// deciding which type is wallet and instatiating correct object
const tempObj = JSON.parse(key);
let unserializedWallet;
switch (tempObj.type) {
case PlaceholderWallet.type:
continue;
case SegwitBech32Wallet.type:
unserializedWallet = SegwitBech32Wallet.fromJson(key);
break;
case SegwitP2SHWallet.type:
unserializedWallet = SegwitP2SHWallet.fromJson(key);
break;
case WatchOnlyWallet.type:
unserializedWallet = WatchOnlyWallet.fromJson(key);
unserializedWallet.init();
if (unserializedWallet.isHd() && !unserializedWallet.isXpubValid()) {
continue;
case SegwitBech32Wallet.type:
unserializedWallet = SegwitBech32Wallet.fromJson(key);
break;
case SegwitP2SHWallet.type:
unserializedWallet = SegwitP2SHWallet.fromJson(key);
break;
case WatchOnlyWallet.type:
unserializedWallet = WatchOnlyWallet.fromJson(key);
unserializedWallet.init();
if (unserializedWallet.isHd() && !unserializedWallet.isXpubValid()) {
continue;
}
break;
case HDLegacyP2PKHWallet.type:
unserializedWallet = HDLegacyP2PKHWallet.fromJson(key);
break;
case HDSegwitP2SHWallet.type:
unserializedWallet = HDSegwitP2SHWallet.fromJson(key);
break;
case HDSegwitBech32Wallet.type:
unserializedWallet = HDSegwitBech32Wallet.fromJson(key);
break;
case HDLegacyBreadwalletWallet.type:
unserializedWallet = HDLegacyBreadwalletWallet.fromJson(key);
break;
case HDLegacyElectrumSeedP2PKHWallet.type:
unserializedWallet = HDLegacyElectrumSeedP2PKHWallet.fromJson(key);
break;
case HDSegwitElectrumSeedP2WPKHWallet.type:
unserializedWallet = HDSegwitElectrumSeedP2WPKHWallet.fromJson(key);
break;
case MultisigHDWallet.type:
unserializedWallet = MultisigHDWallet.fromJson(key);
break;
case HDAezeedWallet.type:
unserializedWallet = HDAezeedWallet.fromJson(key);
break;
case LightningCustodianWallet.type: {
/** @type {LightningCustodianWallet} */
unserializedWallet = LightningCustodianWallet.fromJson(key);
let lndhub = false;
try {
lndhub = await AsyncStorage.getItem(AppStorage.LNDHUB);
} catch (Error) {
console.warn(Error);
}
if (unserializedWallet.baseURI) {
unserializedWallet.setBaseURI(unserializedWallet.baseURI); // not really necessary, just for the sake of readability
console.log('using saved uri for for ln wallet:', unserializedWallet.baseURI);
} else if (lndhub) {
console.log('using wallet-wide settings ', lndhub, 'for ln wallet');
unserializedWallet.setBaseURI(lndhub);
} else {
console.log('using default', LightningCustodianWallet.defaultBaseUri, 'for ln wallet');
unserializedWallet.setBaseURI(LightningCustodianWallet.defaultBaseUri);
}
unserializedWallet.init();
break;
}
case LegacyWallet.type:
default:
unserializedWallet = LegacyWallet.fromJson(key);
break;
}
break;
case HDLegacyP2PKHWallet.type:
unserializedWallet = HDLegacyP2PKHWallet.fromJson(key);
break;
case HDSegwitP2SHWallet.type:
unserializedWallet = HDSegwitP2SHWallet.fromJson(key);
break;
case HDSegwitBech32Wallet.type:
unserializedWallet = HDSegwitBech32Wallet.fromJson(key);
break;
case HDLegacyBreadwalletWallet.type:
unserializedWallet = HDLegacyBreadwalletWallet.fromJson(key);
break;
case HDLegacyElectrumSeedP2PKHWallet.type:
unserializedWallet = HDLegacyElectrumSeedP2PKHWallet.fromJson(key);
break;
case HDSegwitElectrumSeedP2WPKHWallet.type:
unserializedWallet = HDSegwitElectrumSeedP2WPKHWallet.fromJson(key);
break;
case MultisigHDWallet.type:
unserializedWallet = MultisigHDWallet.fromJson(key);
break;
case HDAezeedWallet.type:
unserializedWallet = HDAezeedWallet.fromJson(key);
break;
case LightningCustodianWallet.type: {
/** @type {LightningCustodianWallet} */
unserializedWallet = LightningCustodianWallet.fromJson(key);
let lndhub = false;
try {
lndhub = await AsyncStorage.getItem(AppStorage.LNDHUB);
} catch (Error) {
console.warn(Error);
}
this.inflateWalletFromRealm(realm, unserializedWallet);
// done
if (!this.wallets.some(wallet => wallet.getSecret() === unserializedWallet.secret)) {
this.wallets.push(unserializedWallet);
this.tx_metadata = data.tx_metadata;
if (unserializedWallet.baseURI) {
unserializedWallet.setBaseURI(unserializedWallet.baseURI); // not really necessary, just for the sake of readability
console.log('using saved uri for for ln wallet:', unserializedWallet.baseURI);
} else if (lndhub) {
console.log('using wallet-wide settings ', lndhub, 'for ln wallet');
unserializedWallet.setBaseURI(lndhub);
} else {
console.log('using default', LightningCustodianWallet.defaultBaseUri, 'for ln wallet');
unserializedWallet.setBaseURI(LightningCustodianWallet.defaultBaseUri);
}
unserializedWallet.init();
break;
}
case LegacyWallet.type:
default:
unserializedWallet = LegacyWallet.fromJson(key);
break;
}
this.inflateWalletFromRealm(realm, unserializedWallet);
// done
if (!this.wallets.some(wallet => wallet.getSecret() === unserializedWallet.secret)) {
this.wallets.push(unserializedWallet);
this.tx_metadata = data.tx_metadata;
}
realm.close();
return true;
} else {
return false; // failed loading data or loading/decryptin data
}
} catch (error) {
console.warn(error.message);
return false;
realm.close();
return true;
} else {
return false; // failed loading data or loading/decryptin data
}
}