fix: add monero rename related fixes into main (#1002)

Rafael Saes committed Jul 18, 2023 at 09:11 UTC 8996a467842a68a5206c57c39b12a1afabc3857f
2 files changed +59 -18
cw_monero/lib/monero_wallet.dart
+50 -17
@@ -269,26 +269,59 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance,
269 }
270
271 Future<void> renameWalletFiles(String newWalletName) async {
272 - final currentWalletPath = await pathForWallet(name: name, type: type);
273 - final currentCacheFile = File(currentWalletPath);
274 - final currentKeysFile = File('$currentWalletPath.keys');
275 - final currentAddressListFile = File('$currentWalletPath.address.txt');
272 + final currentWalletDirPath = await pathForWalletDir(name: name, type: type);
273
277 - final newWalletPath = await pathForWallet(name: newWalletName, type: type);
274 + try {
275 + // -- rename the waller folder --
276 + final currentWalletDir =
277 + Directory(await pathForWalletDir(name: name, type: type));
278 + final newWalletDirPath =
279 + await pathForWalletDir(name: newWalletName, type: type);
280 + await currentWalletDir.rename(newWalletDirPath);
281
279 - // Copies current wallet files into new wallet name's dir and files
280 - if (currentCacheFile.existsSync()) {
281 - await currentCacheFile.copy(newWalletPath);
282 - }
283 - if (currentKeysFile.existsSync()) {
284 - await currentKeysFile.copy('$newWalletPath.keys');
285 - }
286 - if (currentAddressListFile.existsSync()) {
287 - await currentAddressListFile.copy('$newWalletPath.address.txt');
288 - }
282 + // -- use new waller folder to rename files with old names still --
283 + final renamedWalletPath = newWalletDirPath + '/$name';
284 +
285 + final currentCacheFile = File(renamedWalletPath);
286 + final currentKeysFile = File('$renamedWalletPath.keys');
287 + final currentAddressListFile = File('$renamedWalletPath.address.txt');
288 +
289 + final newWalletPath =
290 + await pathForWallet(name: newWalletName, type: type);
291 +
292 + if (currentCacheFile.existsSync()) {
293 + await currentCacheFile.rename(newWalletPath);
294 + }
295 + if (currentKeysFile.existsSync()) {
296 + await currentKeysFile.rename('$newWalletPath.keys');
297 + }
298 + if (currentAddressListFile.existsSync()) {
299 + await currentAddressListFile.rename('$newWalletPath.address.txt');
300 + }
301 + } catch (e) {
302 + final currentWalletPath = await pathForWallet(name: name, type: type);
303 +
304 + final currentCacheFile = File(currentWalletPath);
305 + final currentKeysFile = File('$currentWalletPath.keys');
306 + final currentAddressListFile = File('$currentWalletPath.address.txt');
307
290 - // Delete old name's dir and files
291 - await Directory(currentWalletPath).delete(recursive: true);
308 + final newWalletPath =
309 + await pathForWallet(name: newWalletName, type: type);
310 +
311 + // Copies current wallet files into new wallet name's dir and files
312 + if (currentCacheFile.existsSync()) {
313 + await currentCacheFile.copy(newWalletPath);
314 + }
315 + if (currentKeysFile.existsSync()) {
316 + await currentKeysFile.copy('$newWalletPath.keys');
317 + }
318 + if (currentAddressListFile.existsSync()) {
319 + await currentAddressListFile.copy('$newWalletPath.address.txt');
320 + }
321 +
322 + // Delete old name's dir and files
323 + await Directory(currentWalletDirPath).delete(recursive: true);
324 + }
325 }
326
327 @override
lib/core/wallet_loading_service.dart
+9 -1
@@ -27,6 +27,14 @@ class WalletLoadingService {
27 await keyService.deleteWalletPassword(walletName: name);
28
29 await walletService.rename(name, password, newName);
30 +
31 + // set shared preferences flag based on previous wallet name
32 + if (type == WalletType.monero) {
33 + final oldNameKey = PreferencesKey.moneroWalletUpdateV1Key(name);
34 + final isPasswordUpdated = sharedPreferences.getBool(oldNameKey) ?? false;
35 + final newNameKey = PreferencesKey.moneroWalletUpdateV1Key(newName);
36 + await sharedPreferences.setBool(newNameKey, isPasswordUpdated);
37 + }
38 }
39
40 Future<WalletBase> load(WalletType type, String name) async {
@@ -61,4 +69,4 @@ class WalletLoadingService {
69 isPasswordUpdated = true;
70 await sharedPreferences.setBool(key, isPasswordUpdated);
71 }
64 -}
\ No newline at end of file
72 +}