diff --git a/lib/di.dart b/lib/di.dart index 05019a562..36aef6705 100644 --- a/lib/di.dart +++ b/lib/di.dart @@ -276,7 +276,6 @@ Future setup({ if (!_isSetupFinished) { getIt.registerSingletonAsync(() => SharedPreferences.getInstance()); - getIt.registerSingleton(secureStorage); } if (!_isSetupFinished) { getIt.registerFactory(() => BackgroundTasks()); @@ -332,8 +331,6 @@ Future setup({ getIt.registerSingleton(secretStore); - getIt.registerFactory(() => KeyService(getIt.get())); - getIt.registerFactoryParam((type, _) => WalletCreationService( initialType: type, diff --git a/lib/entities/default_settings_migration.dart b/lib/entities/default_settings_migration.dart index 68e76d423..397b4a8b9 100644 --- a/lib/entities/default_settings_migration.dart +++ b/lib/entities/default_settings_migration.dart @@ -20,6 +20,7 @@ import 'package:cw_core/wallet_info.dart'; import 'package:cake_wallet/exchange/trade.dart'; import 'package:encrypt/encrypt.dart' as encrypt; import 'package:collection/collection.dart'; +import 'package:cake_wallet/core/key_service.dart'; const newCakeWalletMoneroUri = 'xmr-node.cakewallet.com:18081'; const cakeWalletBitcoinElectrumUri = 'electrum.cakewallet.com:50002'; @@ -31,15 +32,17 @@ const cakeWalletBitcoinCashDefaultNodeUri = 'bitcoincash.stackwallet.com:50002'; const nanoDefaultNodeUri = 'rpc.nano.to'; const nanoDefaultPowNodeUri = 'rpc.nano.to'; -Future defaultSettingsMigration( - {required int version, - required SharedPreferences sharedPreferences, - required FlutterSecureStorage secureStorage, - required Box nodes, - required Box powNodes, - required Box walletInfoSource, - required Box tradeSource, - required Box contactSource}) async { +Future defaultSettingsMigration({ + required int version, + required SharedPreferences sharedPreferences, + required FlutterSecureStorage secureStorage, + required Box nodes, + required Box powNodes, + required Box walletInfoSource, + required Box tradeSource, + required Box contactSource, + required KeyService keyService, +}) async { if (Platform.isIOS) { await ios_migrate_v1(walletInfoSource, tradeSource, contactSource); } @@ -50,7 +53,7 @@ Future defaultSettingsMigration( final isNewInstall = sharedPreferences.getInt(PreferencesKey.currentDefaultSettingsMigrationVersion) == null; - await _validateWalletInfoBoxData(walletInfoSource); + await _validateWalletInfoBoxData(keyService, walletInfoSource); await sharedPreferences.setBool(PreferencesKey.isNewInstall, isNewInstall); @@ -186,6 +189,7 @@ Future defaultSettingsMigration( await rewriteSecureStoragePin(secureStorage: secureStorage); break; case 26: + /// commented out as it was a probable cause for some users to have white screen issues /// maybe due to multiple access on Secure Storage at once /// or long await time on start of the app @@ -205,7 +209,8 @@ Future defaultSettingsMigration( await sharedPreferences.setInt(PreferencesKey.currentDefaultSettingsMigrationVersion, version); } -Future _validateWalletInfoBoxData(Box walletInfoSource) async { +Future _validateWalletInfoBoxData( + KeyService keyService, Box walletInfoSource) async { try { final root = await getApplicationDocumentsDirectory(); @@ -232,17 +237,66 @@ Future _validateWalletInfoBoxData(Box walletInfoSource) async } final walletFiles = dir.listSync(); - final hasCacheFile = walletFiles.any((element) => element.path.contains("$name/$name")); + final hasCacheFile = + walletFiles.any((element) => element.path.endsWith("${dir.path}/#_$name")); + String? renamedFileName; if (!hasCacheFile) { - continue; + await Future.wait(walletFiles.map((file) async { + if (file.path.startsWith("${dir.path}/#_")) { + var rightPath = '${dir.path}/#_$name'; + if (file.path.endsWith(".keys")) { + rightPath += ".keys"; + } + // Create the new file with the right wallet name + File(file.path).copySync(rightPath); + + renamedFileName = file.path.replaceAll("${dir.path}/#_", "").replaceAll(".keys", ""); + final rest = file.path.replaceAll("${dir.path}/#_$renamedFileName", ""); + final renamedFileDirPath = + Directory(await pathForWalletDir(name: renamedFileName!, type: type)); + + // Move the renamed file to the proper wallet directory + File(file.path).copySync('${renamedFileDirPath.path}/#_$renamedFileName$rest'); + } + })); } if (type == WalletType.monero || type == WalletType.haven) { - final hasKeysFile = walletFiles.any((element) => element.path.contains(".keys")); + final hasKeysFile = + walletFiles.any((element) => element.path == "${dir.path}/$name.keys"); if (!hasKeysFile) { - continue; + final renamedKeysFile = walletFiles.firstWhereOrNull((element) => + !element.path.startsWith("${dir.path}/#_") && element.path.endsWith(".keys")); + + if (renamedKeysFile != null) { + renamedFileName = + renamedKeysFile.path.replaceAll("${dir.path}/", "").replaceAll(".keys", ""); + + await keyService.saveWalletPassword( + walletName: renamedFileName!, + password: await keyService.getWalletPassword(walletName: name)); + + // Create the new files with the right wallet name + File("${dir.path}/$renamedFileName").copySync('${dir.path}/$name'); + File("${dir.path}/$renamedFileName.keys").copySync('${dir.path}/$name.keys'); + File("${dir.path}/$renamedFileName.address.txt") + .copySync('${dir.path}/$name.address.txt'); + + final renamedFileDirPath = + Directory(await pathForWalletDir(name: renamedFileName!, type: type)); + + // Move the renamed files to the proper wallet directory + File("${dir.path}/$renamedFileName") + .copySync('${renamedFileDirPath.path}/$renamedFileName'); + File("${dir.path}/$renamedFileName.keys") + .copySync('${renamedFileDirPath.path}/$renamedFileName.keys'); + File("${dir.path}/$renamedFileName.address.txt") + .copySync('${renamedFileDirPath.path}/$renamedFileName.address.txt'); + } else { + continue; + } } } diff --git a/lib/main.dart b/lib/main.dart index 306b109a0..2781079cc 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,6 +1,7 @@ import 'dart:async'; import 'package:cake_wallet/anonpay/anonpay_invoice_info.dart'; import 'package:cake_wallet/core/auth_service.dart'; +import 'package:cake_wallet/core/key_service.dart'; import 'package:cake_wallet/entities/language_service.dart'; import 'package:cake_wallet/buy/order.dart'; import 'package:cake_wallet/locales/locale.dart'; @@ -183,15 +184,19 @@ Future initialSetup( required Box unspentCoinsInfoSource, int initialMigrationVersion = 15}) async { LanguageService.loadLocaleList(); + getIt.registerSingleton(secureStorage); + getIt.registerFactory(() => KeyService(getIt.get())); await defaultSettingsMigration( - secureStorage: secureStorage, - version: initialMigrationVersion, - sharedPreferences: sharedPreferences, - walletInfoSource: walletInfoSource, - contactSource: contactSource, - tradeSource: tradesSource, - nodes: nodes, - powNodes: powNodes); + secureStorage: secureStorage, + version: initialMigrationVersion, + sharedPreferences: sharedPreferences, + walletInfoSource: walletInfoSource, + contactSource: contactSource, + tradeSource: tradesSource, + nodes: nodes, + powNodes: powNodes, + keyService: getIt.get(), + ); await setup( walletInfoSource: walletInfoSource, nodeSource: nodes,