| 1 | import 'dart:io'; |
| 2 | import 'package:cw_core/pathForWallet.dart'; |
| 3 | import 'package:cw_core/wallet_type.dart'; |
| 4 | |
| 5 | String backupFileName(String originalPath) { |
| 6 | final pathParts = originalPath.split('/'); |
| 7 | final newName = '#_${pathParts.last}'; |
| 8 | pathParts.removeLast(); |
| 9 | pathParts.add(newName); |
| 10 | return pathParts.join('/'); |
| 11 | } |
| 12 | |
| 13 | Future<void> backupWalletFiles(String name) async { |
| 14 | final path = await pathForWallet(name: name, type: WalletType.monero); |
| 15 | final cacheFile = File(path); |
| 16 | final keysFile = File('$path.keys'); |
| 17 | final addressListFile = File('$path.address.txt'); |
| 18 | final newCacheFilePath = backupFileName(cacheFile.path); |
| 19 | final newKeysFilePath = backupFileName(keysFile.path); |
| 20 | final newAddressListFilePath = backupFileName(addressListFile.path); |
| 21 | |
| 22 | if (cacheFile.existsSync() && !File(newCacheFilePath).existsSync()) { |
| 23 | await cacheFile.copy(newCacheFilePath); |
| 24 | } |
| 25 | |
| 26 | if (keysFile.existsSync() && !File(newKeysFilePath).existsSync()) { |
| 27 | await keysFile.copy(newKeysFilePath); |
| 28 | } |
| 29 | |
| 30 | if (addressListFile.existsSync() && !File(newAddressListFilePath).existsSync()) { |
| 31 | await addressListFile.copy(newAddressListFilePath); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | Future<void> restoreWalletFiles(String name) async { |
| 36 | final walletDirPath = await pathForWalletDir(name: name, type: WalletType.monero); |
| 37 | final cacheFilePath = '$walletDirPath/$name'; |
| 38 | final keysFilePath = '$walletDirPath/$name.keys'; |
| 39 | final addressListFilePath = '$walletDirPath/$name.address.txt'; |
| 40 | final backupCacheFile = File(backupFileName(cacheFilePath)); |
| 41 | final backupKeysFile = File(backupFileName(keysFilePath)); |
| 42 | final backupAddressListFile = File(backupFileName(addressListFilePath)); |
| 43 | |
| 44 | if (backupCacheFile.existsSync()) { |
| 45 | await backupCacheFile.copy(cacheFilePath); |
| 46 | } |
| 47 | |
| 48 | if (backupKeysFile.existsSync()) { |
| 49 | await backupKeysFile.copy(keysFilePath); |
| 50 | } |
| 51 | |
| 52 | if (backupAddressListFile.existsSync()) { |
| 53 | await backupAddressListFile.copy(addressListFilePath); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | Future<void> resetCache(String name) async { |
| 58 | await removeCache(name); |
| 59 | |
| 60 | final walletDirPath = await pathForWalletDir(name: name, type: WalletType.monero); |
| 61 | final cacheFilePath = '$walletDirPath/$name'; |
| 62 | final backupCacheFile = File(backupFileName(cacheFilePath)); |
| 63 | if (backupCacheFile.existsSync()) { |
| 64 | await backupCacheFile.copy(cacheFilePath); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | Future<bool> backupWalletFilesExists(String name) async { |
| 69 | final walletDirPath = await pathForWalletDir(name: name, type: WalletType.monero); |
| 70 | final cacheFilePath = '$walletDirPath/$name'; |
| 71 | final keysFilePath = '$walletDirPath/$name.keys'; |
| 72 | final addressListFilePath = '$walletDirPath/$name.address.txt'; |
| 73 | final backupCacheFile = File(backupFileName(cacheFilePath)); |
| 74 | final backupKeysFile = File(backupFileName(keysFilePath)); |
| 75 | final backupAddressListFile = File(backupFileName(addressListFilePath)); |
| 76 | |
| 77 | return backupCacheFile.existsSync() && |
| 78 | backupKeysFile.existsSync() && |
| 79 | backupAddressListFile.existsSync(); |
| 80 | } |
| 81 | |
| 82 | // WARNING: Transaction keys and your Polyseed CANNOT be recovered if this file is deleted |
| 83 | Future<void> removeCache(String name) async { |
| 84 | final path = await pathForWallet(name: name, type: WalletType.monero); |
| 85 | final cacheFile = File(path); |
| 86 | final backgroundCacheFile = File(path + ".background"); |
| 87 | if (cacheFile.existsSync()) { |
| 88 | cacheFile.deleteSync(); |
| 89 | } |
| 90 | if (backgroundCacheFile.existsSync()) { |
| 91 | backgroundCacheFile.deleteSync(); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | Future<void> restoreOrResetWalletFiles(String name) async { |
| 96 | final backupsExists = await backupWalletFilesExists(name); |
| 97 | |
| 98 | if (backupsExists) { |
| 99 | await removeCache(name); |
| 100 | // TODO(mrcyjanek): is this needed? |
| 101 | // If we remove cache then wallet should be restored from .keys file. |
| 102 | await restoreWalletFiles(name); |
| 103 | } |
| 104 | } |