Skip to content

Commit 9f44cb7

Browse files
committed
dcr: Always fetch the current dir path.
On ios devices the path will change between updates breaking decred. Never save the path and always check to ensure it is up to date.
1 parent 4448adb commit 9f44cb7

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed

cw_decred/lib/wallet.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import 'dart:io';
44
import 'package:cw_core/exceptions.dart';
55
import 'package:cw_core/transaction_direction.dart';
66
import 'package:cw_core/utils/print_verbose.dart';
7+
import 'package:cw_core/pathForWallet.dart';
8+
import 'package:cw_core/wallet_type.dart';
79
import 'package:cw_decred/amount_format.dart';
810
import 'package:cw_decred/pending_transaction.dart';
911
import 'package:cw_decred/transaction_credentials.dart';
@@ -306,9 +308,10 @@ abstract class DecredWalletBase
306308
persistantPeer = addr;
307309
await _libwallet.closeWallet(walletInfo.name);
308310
final network = isTestnet ? "testnet" : "mainnet";
311+
final dirPath = await pathForWalletDir(name: walletInfo.name, type: WalletType.decred);
309312
final config = {
310313
"name": walletInfo.name,
311-
"datadir": walletInfo.dirPath,
314+
"datadir": dirPath,
312315
"net": network,
313316
"unsyncedaddrs": true,
314317
};

cw_decred/lib/wallet_service.dart

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'package:cw_core/wallet_service.dart';
88
import 'package:cw_core/pathForWallet.dart';
99
import 'package:cw_core/wallet_info.dart';
1010
import 'package:cw_core/wallet_type.dart';
11+
import 'package:path/path.dart';
1112
import 'package:hive/hive.dart';
1213
import 'package:collection/collection.dart';
1314
import 'package:cw_core/unspent_coins_info.dart';
@@ -57,9 +58,10 @@ class DecredWalletService extends WalletService<
5758
@override
5859
Future<DecredWallet> create(DecredNewWalletCredentials credentials, {bool? isTestnet}) async {
5960
await this.init();
61+
final dirPath = await pathForWalletDir(name: credentials.walletInfo!.name, type: getType());
6062
final config = {
6163
"name": credentials.walletInfo!.name,
62-
"datadir": credentials.walletInfo!.dirPath,
64+
"datadir": dirPath,
6365
"pass": credentials.password!,
6466
"net": isTestnet == true ? testnet : mainnet,
6567
"unsyncedaddrs": true,
@@ -74,6 +76,23 @@ class DecredWalletService extends WalletService<
7476
return wallet;
7577
}
7678

79+
void copyDirectorySync(Directory source, Directory destination) {
80+
/// create destination folder if not exist
81+
if (!destination.existsSync()) {
82+
destination.createSync(recursive: true);
83+
}
84+
85+
/// get all files from source (recursive: false is important here)
86+
source.listSync(recursive: false).forEach((entity) {
87+
final newPath = destination.path + Platform.pathSeparator + basename(entity.path);
88+
if (entity is File) {
89+
entity.copySync(newPath);
90+
} else if (entity is Directory) {
91+
copyDirectorySync(entity, Directory(newPath));
92+
}
93+
});
94+
}
95+
7796
@override
7897
Future<DecredWallet> openWallet(String name, String password) async {
7998
final walletInfo = walletInfoSource.values
@@ -84,14 +103,22 @@ class DecredWalletService extends WalletService<
84103
: mainnet;
85104

86105
await this.init();
87-
final walletDirExists = Directory(walletInfo.dirPath).existsSync();
88-
if (!walletDirExists) {
89-
walletInfo.dirPath = await pathForWalletDir(name: name, type: getType());
106+
107+
// Cake wallet version 4.27.0 and earlier gave a wallet dir that did not
108+
// match the name. Move those to the correct place.
109+
final dirPath = await pathForWalletDir(name: name, type: getType());
110+
if (walletInfo.dirPath != "" && walletInfo.dirPath != dirPath) {
111+
final oldWalletDir = new Directory(walletInfo.dirPath);
112+
final newWalletDir = new Directory(dirPath);
113+
copyDirectorySync(oldWalletDir, newWalletDir);
114+
await oldWalletDir.delete(recursive: true);
115+
// Clear the path so this does not trigger again.
116+
walletInfo.dirPath = "";
90117
}
91118

92119
final config = {
93-
"name": walletInfo.name,
94-
"datadir": walletInfo.dirPath,
120+
"name": name,
121+
"datadir": dirPath,
95122
"net": network,
96123
"unsyncedaddrs": true,
97124
};
@@ -123,11 +150,9 @@ class DecredWalletService extends WalletService<
123150

124151
await currentWallet.renameWalletFiles(newName);
125152

126-
final newDirPath = await pathForWalletDir(name: newName, type: getType());
127153
final newWalletInfo = currentWalletInfo;
128154
newWalletInfo.id = WalletBase.idFor(newName, getType());
129155
newWalletInfo.name = newName;
130-
newWalletInfo.dirPath = newDirPath;
131156
newWalletInfo.network = network;
132157

133158
await walletInfoSource.put(currentWalletInfo.key, newWalletInfo);
@@ -137,9 +162,10 @@ class DecredWalletService extends WalletService<
137162
Future<DecredWallet> restoreFromSeed(DecredRestoreWalletFromSeedCredentials credentials,
138163
{bool? isTestnet}) async {
139164
await this.init();
165+
final dirPath = await pathForWalletDir(name: credentials.walletInfo!.name, type: getType());
140166
final config = {
141167
"name": credentials.walletInfo!.name,
142-
"datadir": credentials.walletInfo!.dirPath,
168+
"datadir": dirPath,
143169
"pass": credentials.password!,
144170
"mnemonic": credentials.mnemonic,
145171
"net": isTestnet == true ? testnet : mainnet,
@@ -161,9 +187,10 @@ class DecredWalletService extends WalletService<
161187
Future<DecredWallet> restoreFromKeys(DecredRestoreWalletFromPubkeyCredentials credentials,
162188
{bool? isTestnet}) async {
163189
await this.init();
190+
final dirPath = await pathForWalletDir(name: credentials.walletInfo!.name, type: getType());
164191
final config = {
165192
"name": credentials.walletInfo!.name,
166-
"datadir": credentials.walletInfo!.dirPath,
193+
"datadir": dirPath,
167194
"pubkey": credentials.pubkey,
168195
"net": isTestnet == true ? testnet : mainnet,
169196
"unsyncedaddrs": true,

0 commit comments

Comments
 (0)