| 1 | import 'dart:io'; |
| 2 | import 'package:cw_core/root_dir.dart'; |
| 3 | import 'package:cw_core/wallet_type.dart'; |
| 4 | import 'package:path/path.dart' as p; |
| 5 | |
| 6 | Future<String> pathForWalletTypeDir({required WalletType type}) async { |
| 7 | final root = await getAppDir(); |
| 8 | final prefix = walletTypeToString(type).toLowerCase(); |
| 9 | final walletsDir = Directory('${root.path}/wallets'); |
| 10 | final walletDir = Directory('${walletsDir.path}/$prefix'); |
| 11 | |
| 12 | if (!walletDir.existsSync()) { |
| 13 | walletDir.createSync(recursive: true); |
| 14 | } |
| 15 | |
| 16 | return walletDir.path; |
| 17 | } |
| 18 | |
| 19 | Future<String> pathForWalletDir({required String name, required WalletType type}) async { |
| 20 | final typeRoot = await pathForWalletTypeDir(type: type); |
| 21 | final walletDire = Directory('${typeRoot}/$name'); |
| 22 | |
| 23 | if (!walletDire.existsSync()) { |
| 24 | walletDire.createSync(recursive: true); |
| 25 | } |
| 26 | |
| 27 | return walletDire.path; |
| 28 | } |
| 29 | |
| 30 | Future<String> pathForWallet({required String name, required WalletType type}) async => |
| 31 | await pathForWalletDir(name: name, type: type).then((path) => path + '/$name'); |
| 32 | |
| 33 | Future<String> outdatedAndroidPathForWalletDir({required String name}) async { |
| 34 | final directory = await getAppDir(); |
| 35 | final pathDir = directory.path + '/$name'; |
| 36 | |
| 37 | return pathDir; |
| 38 | } |
| 39 | |
| 40 | Future<void> copyWalletFilesTo({ |
| 41 | required String fromName, |
| 42 | required String toName, |
| 43 | required WalletType type, |
| 44 | }) async { |
| 45 | if (fromName == toName) return; |
| 46 | |
| 47 | final typeRoot = await pathForWalletTypeDir(type: type); |
| 48 | final sourceDir = Directory(p.join(typeRoot, fromName)); |
| 49 | if (!sourceDir.existsSync()) { |
| 50 | throw "Source wallet not found: $fromName $type"; |
| 51 | } |
| 52 | |
| 53 | if (Directory(p.join(typeRoot, toName)).existsSync()) { |
| 54 | throw Exception('Cannot rename wallet: "$toName" already exists'); |
| 55 | } |
| 56 | |
| 57 | final destinationDir = Directory(p.join(typeRoot, toName)); |
| 58 | await _copyDirectory(sourceDir, destinationDir); |
| 59 | |
| 60 | for (final suffix in const ['', '.keys', '.keys.backup']) { |
| 61 | final file = File(p.join(destinationDir.path, '$fromName$suffix')); |
| 62 | if (file.existsSync()) { |
| 63 | await file.rename(p.join(destinationDir.path, '$toName$suffix')); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | Future<void> _copyDirectory(Directory source, Directory destination) async { |
| 69 | await destination.create(recursive: true); |
| 70 | await for (final entity in source.list(followLinks: false)) { |
| 71 | final name = p.basename(entity.path); |
| 72 | if (entity is File) { |
| 73 | await entity.copy(p.join(destination.path, name)); |
| 74 | } else if (entity is Directory) { |
| 75 | await _copyDirectory(entity, Directory(p.join(destination.path, name))); |
| 76 | } |
| 77 | } |
| 78 | } |