fix: restore wallet not matching renamed file names

This commit is contained in:
Rafael Saes 2024-02-14 17:33:15 -03:00
parent d6af37df06
commit d9c471e4fb
3 changed files with 82 additions and 26 deletions

View file

@ -276,7 +276,6 @@ Future<void> setup({
if (!_isSetupFinished) { if (!_isSetupFinished) {
getIt.registerSingletonAsync<SharedPreferences>(() => SharedPreferences.getInstance()); getIt.registerSingletonAsync<SharedPreferences>(() => SharedPreferences.getInstance());
getIt.registerSingleton<FlutterSecureStorage>(secureStorage);
} }
if (!_isSetupFinished) { if (!_isSetupFinished) {
getIt.registerFactory(() => BackgroundTasks()); getIt.registerFactory(() => BackgroundTasks());
@ -332,8 +331,6 @@ Future<void> setup({
getIt.registerSingleton<SecretStore>(secretStore); getIt.registerSingleton<SecretStore>(secretStore);
getIt.registerFactory<KeyService>(() => KeyService(getIt.get<FlutterSecureStorage>()));
getIt.registerFactoryParam<WalletCreationService, WalletType, void>((type, _) => getIt.registerFactoryParam<WalletCreationService, WalletType, void>((type, _) =>
WalletCreationService( WalletCreationService(
initialType: type, initialType: type,

View file

@ -20,6 +20,7 @@ import 'package:cw_core/wallet_info.dart';
import 'package:cake_wallet/exchange/trade.dart'; import 'package:cake_wallet/exchange/trade.dart';
import 'package:encrypt/encrypt.dart' as encrypt; import 'package:encrypt/encrypt.dart' as encrypt;
import 'package:collection/collection.dart'; import 'package:collection/collection.dart';
import 'package:cake_wallet/core/key_service.dart';
const newCakeWalletMoneroUri = 'xmr-node.cakewallet.com:18081'; const newCakeWalletMoneroUri = 'xmr-node.cakewallet.com:18081';
const cakeWalletBitcoinElectrumUri = 'electrum.cakewallet.com:50002'; const cakeWalletBitcoinElectrumUri = 'electrum.cakewallet.com:50002';
@ -31,15 +32,17 @@ const cakeWalletBitcoinCashDefaultNodeUri = 'bitcoincash.stackwallet.com:50002';
const nanoDefaultNodeUri = 'rpc.nano.to'; const nanoDefaultNodeUri = 'rpc.nano.to';
const nanoDefaultPowNodeUri = 'rpc.nano.to'; const nanoDefaultPowNodeUri = 'rpc.nano.to';
Future<void> defaultSettingsMigration( Future<void> defaultSettingsMigration({
{required int version, required int version,
required SharedPreferences sharedPreferences, required SharedPreferences sharedPreferences,
required FlutterSecureStorage secureStorage, required FlutterSecureStorage secureStorage,
required Box<Node> nodes, required Box<Node> nodes,
required Box<Node> powNodes, required Box<Node> powNodes,
required Box<WalletInfo> walletInfoSource, required Box<WalletInfo> walletInfoSource,
required Box<Trade> tradeSource, required Box<Trade> tradeSource,
required Box<Contact> contactSource}) async { required Box<Contact> contactSource,
required KeyService keyService,
}) async {
if (Platform.isIOS) { if (Platform.isIOS) {
await ios_migrate_v1(walletInfoSource, tradeSource, contactSource); await ios_migrate_v1(walletInfoSource, tradeSource, contactSource);
} }
@ -50,7 +53,7 @@ Future<void> defaultSettingsMigration(
final isNewInstall = final isNewInstall =
sharedPreferences.getInt(PreferencesKey.currentDefaultSettingsMigrationVersion) == null; sharedPreferences.getInt(PreferencesKey.currentDefaultSettingsMigrationVersion) == null;
await _validateWalletInfoBoxData(walletInfoSource); await _validateWalletInfoBoxData(keyService, walletInfoSource);
await sharedPreferences.setBool(PreferencesKey.isNewInstall, isNewInstall); await sharedPreferences.setBool(PreferencesKey.isNewInstall, isNewInstall);
@ -186,6 +189,7 @@ Future<void> defaultSettingsMigration(
await rewriteSecureStoragePin(secureStorage: secureStorage); await rewriteSecureStoragePin(secureStorage: secureStorage);
break; break;
case 26: case 26:
/// commented out as it was a probable cause for some users to have white screen issues /// 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 /// maybe due to multiple access on Secure Storage at once
/// or long await time on start of the app /// or long await time on start of the app
@ -205,7 +209,8 @@ Future<void> defaultSettingsMigration(
await sharedPreferences.setInt(PreferencesKey.currentDefaultSettingsMigrationVersion, version); await sharedPreferences.setInt(PreferencesKey.currentDefaultSettingsMigrationVersion, version);
} }
Future<void> _validateWalletInfoBoxData(Box<WalletInfo> walletInfoSource) async { Future<void> _validateWalletInfoBoxData(
KeyService keyService, Box<WalletInfo> walletInfoSource) async {
try { try {
final root = await getApplicationDocumentsDirectory(); final root = await getApplicationDocumentsDirectory();
@ -232,17 +237,66 @@ Future<void> _validateWalletInfoBoxData(Box<WalletInfo> walletInfoSource) async
} }
final walletFiles = dir.listSync(); 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) { 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) { 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) { 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;
}
} }
} }

View file

@ -1,6 +1,7 @@
import 'dart:async'; import 'dart:async';
import 'package:cake_wallet/anonpay/anonpay_invoice_info.dart'; import 'package:cake_wallet/anonpay/anonpay_invoice_info.dart';
import 'package:cake_wallet/core/auth_service.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/entities/language_service.dart';
import 'package:cake_wallet/buy/order.dart'; import 'package:cake_wallet/buy/order.dart';
import 'package:cake_wallet/locales/locale.dart'; import 'package:cake_wallet/locales/locale.dart';
@ -183,15 +184,19 @@ Future<void> initialSetup(
required Box<UnspentCoinsInfo> unspentCoinsInfoSource, required Box<UnspentCoinsInfo> unspentCoinsInfoSource,
int initialMigrationVersion = 15}) async { int initialMigrationVersion = 15}) async {
LanguageService.loadLocaleList(); LanguageService.loadLocaleList();
getIt.registerSingleton<FlutterSecureStorage>(secureStorage);
getIt.registerFactory<KeyService>(() => KeyService(getIt.get<FlutterSecureStorage>()));
await defaultSettingsMigration( await defaultSettingsMigration(
secureStorage: secureStorage, secureStorage: secureStorage,
version: initialMigrationVersion, version: initialMigrationVersion,
sharedPreferences: sharedPreferences, sharedPreferences: sharedPreferences,
walletInfoSource: walletInfoSource, walletInfoSource: walletInfoSource,
contactSource: contactSource, contactSource: contactSource,
tradeSource: tradesSource, tradeSource: tradesSource,
nodes: nodes, nodes: nodes,
powNodes: powNodes); powNodes: powNodes,
keyService: getIt.get<KeyService>(),
);
await setup( await setup(
walletInfoSource: walletInfoSource, walletInfoSource: walletInfoSource,
nodeSource: nodes, nodeSource: nodes,