@@ -8,6 +8,7 @@ import 'package:cw_core/wallet_service.dart';
8
8
import 'package:cw_core/pathForWallet.dart' ;
9
9
import 'package:cw_core/wallet_info.dart' ;
10
10
import 'package:cw_core/wallet_type.dart' ;
11
+ import 'package:path/path.dart' ;
11
12
import 'package:hive/hive.dart' ;
12
13
import 'package:collection/collection.dart' ;
13
14
import 'package:cw_core/unspent_coins_info.dart' ;
@@ -57,42 +58,78 @@ class DecredWalletService extends WalletService<
57
58
@override
58
59
Future <DecredWallet > create (DecredNewWalletCredentials credentials, {bool ? isTestnet}) async {
59
60
await this .init ();
61
+ final dirPath = await pathForWalletDir (name: credentials.walletInfo! .name, type: getType ());
62
+ final network = isTestnet == true ? testnet : mainnet;
60
63
final config = {
61
64
"name" : credentials.walletInfo! .name,
62
- "datadir" : credentials.walletInfo ! . dirPath,
65
+ "datadir" : dirPath,
63
66
"pass" : credentials.password! ,
64
- "net" : isTestnet == true ? testnet : mainnet ,
67
+ "net" : network ,
65
68
"unsyncedaddrs" : true ,
66
69
};
67
70
await libwallet! .createWallet (jsonEncode (config));
68
71
final di = DerivationInfo (
69
72
derivationPath: isTestnet == true ? seedRestorePathTestnet : seedRestorePath);
70
73
credentials.walletInfo! .derivationInfo = di;
74
+ credentials.walletInfo! .network = network;
75
+ credentials.walletInfo! .dirPath = "" ;
71
76
final wallet = DecredWallet (credentials.walletInfo! , credentials.password! ,
72
77
this .unspentCoinsInfoSource, libwallet! , closeLibwallet);
73
78
await wallet.init ();
74
79
return wallet;
75
80
}
76
81
82
+ void copyDirectorySync (Directory source, Directory destination) {
83
+ /// create destination folder if not exist
84
+ if (! destination.existsSync ()) {
85
+ destination.createSync (recursive: true );
86
+ }
87
+
88
+ /// get all files from source (recursive: false is important here)
89
+ source.listSync (recursive: false ).forEach ((entity) {
90
+ final newPath = destination.path + Platform .pathSeparator + basename (entity.path);
91
+ if (entity is File ) {
92
+ entity.copySync (newPath);
93
+ } else if (entity is Directory ) {
94
+ copyDirectorySync (entity, Directory (newPath));
95
+ }
96
+ });
97
+ }
98
+
99
+ Future <void > moveWallet (String fromPath, String toPath) async {
100
+ final oldWalletDir = new Directory (fromPath);
101
+ final newWalletDir = new Directory (toPath);
102
+ copyDirectorySync (oldWalletDir, newWalletDir);
103
+ await oldWalletDir.delete (recursive: true );
104
+ }
105
+
77
106
@override
78
107
Future <DecredWallet > openWallet (String name, String password) async {
79
108
final walletInfo = walletInfoSource.values
80
109
.firstWhereOrNull ((info) => info.id == WalletBase .idFor (name, getType ()))! ;
81
- final network = walletInfo.derivationInfo? .derivationPath == seedRestorePathTestnet ||
82
- walletInfo.derivationInfo? .derivationPath == pubkeyRestorePathTestnet
83
- ? testnet
84
- : mainnet;
85
110
86
111
await this .init ();
87
- final walletDirExists = Directory (walletInfo.dirPath).existsSync ();
88
- if (! walletDirExists) {
89
- walletInfo.dirPath = await pathForWalletDir (name: name, type: getType ());
112
+
113
+ // Cake wallet version 4.27.0 and earlier gave a wallet dir that did not
114
+ // match the name. Move those to the correct place.
115
+ final dirPath = await pathForWalletDir (name: name, type: getType ());
116
+ if (walletInfo.dirPath != "" && walletInfo.dirPath != dirPath) {
117
+ this .moveWallet (walletInfo.dirPath, dirPath);
118
+ // Clear the path so this does not trigger again.
119
+ walletInfo.dirPath = "" ;
120
+ }
121
+
122
+ if (walletInfo.network == null || walletInfo.network == "" ) {
123
+ walletInfo.network = walletInfo.derivationInfo? .derivationPath == seedRestorePathTestnet ||
124
+ walletInfo.derivationInfo? .derivationPath == pubkeyRestorePathTestnet
125
+ ? testnet
126
+ : mainnet;
90
127
}
91
128
92
129
final config = {
93
- "name" : walletInfo. name,
94
- "datadir" : walletInfo. dirPath,
95
- "net" : network,
130
+ "name" : name,
131
+ "datadir" : dirPath,
132
+ "net" : walletInfo. network,
96
133
"unsyncedaddrs" : true ,
97
134
};
98
135
await libwallet! .loadWallet (jsonEncode (config));
@@ -114,21 +151,15 @@ class DecredWalletService extends WalletService<
114
151
Future <void > rename (String currentName, String password, String newName) async {
115
152
final currentWalletInfo = walletInfoSource.values
116
153
.firstWhereOrNull ((info) => info.id == WalletBase .idFor (currentName, getType ()))! ;
117
- final network = currentWalletInfo.derivationInfo? .derivationPath == seedRestorePathTestnet ||
118
- currentWalletInfo.derivationInfo? .derivationPath == pubkeyRestorePathTestnet
119
- ? testnet
120
- : mainnet;
121
- final currentWallet = DecredWallet (
122
- currentWalletInfo, password, this .unspentCoinsInfoSource, libwallet! , closeLibwallet);
123
-
124
- await currentWallet.renameWalletFiles (newName);
125
154
155
+ final currentDirPath = await pathForWalletDir (name: currentName, type: getType ());
126
156
final newDirPath = await pathForWalletDir (name: newName, type: getType ());
157
+ this .moveWallet (currentDirPath, newDirPath);
158
+
127
159
final newWalletInfo = currentWalletInfo;
128
160
newWalletInfo.id = WalletBase .idFor (newName, getType ());
129
161
newWalletInfo.name = newName;
130
- newWalletInfo.dirPath = newDirPath;
131
- newWalletInfo.network = network;
162
+ newWalletInfo.dirPath = "" ;
132
163
133
164
await walletInfoSource.put (currentWalletInfo.key, newWalletInfo);
134
165
}
@@ -137,18 +168,22 @@ class DecredWalletService extends WalletService<
137
168
Future <DecredWallet > restoreFromSeed (DecredRestoreWalletFromSeedCredentials credentials,
138
169
{bool ? isTestnet}) async {
139
170
await this .init ();
171
+ final network = isTestnet == true ? testnet : mainnet;
172
+ final dirPath = await pathForWalletDir (name: credentials.walletInfo! .name, type: getType ());
140
173
final config = {
141
174
"name" : credentials.walletInfo! .name,
142
- "datadir" : credentials.walletInfo ! . dirPath,
175
+ "datadir" : dirPath,
143
176
"pass" : credentials.password! ,
144
177
"mnemonic" : credentials.mnemonic,
145
- "net" : isTestnet == true ? testnet : mainnet ,
178
+ "net" : network ,
146
179
"unsyncedaddrs" : true ,
147
180
};
148
181
await libwallet! .createWallet (jsonEncode (config));
149
182
final di = DerivationInfo (
150
183
derivationPath: isTestnet == true ? seedRestorePathTestnet : seedRestorePath);
151
184
credentials.walletInfo! .derivationInfo = di;
185
+ credentials.walletInfo! .network = network;
186
+ credentials.walletInfo! .dirPath = "" ;
152
187
final wallet = DecredWallet (credentials.walletInfo! , credentials.password! ,
153
188
this .unspentCoinsInfoSource, libwallet! , closeLibwallet);
154
189
await wallet.init ();
@@ -161,17 +196,21 @@ class DecredWalletService extends WalletService<
161
196
Future <DecredWallet > restoreFromKeys (DecredRestoreWalletFromPubkeyCredentials credentials,
162
197
{bool ? isTestnet}) async {
163
198
await this .init ();
199
+ final network = isTestnet == true ? testnet : mainnet;
200
+ final dirPath = await pathForWalletDir (name: credentials.walletInfo! .name, type: getType ());
164
201
final config = {
165
202
"name" : credentials.walletInfo! .name,
166
- "datadir" : credentials.walletInfo ! . dirPath,
203
+ "datadir" : dirPath,
167
204
"pubkey" : credentials.pubkey,
168
- "net" : isTestnet == true ? testnet : mainnet ,
205
+ "net" : network ,
169
206
"unsyncedaddrs" : true ,
170
207
};
171
208
await libwallet! .createWatchOnlyWallet (jsonEncode (config));
172
209
final di = DerivationInfo (
173
210
derivationPath: isTestnet == true ? pubkeyRestorePathTestnet : pubkeyRestorePath);
174
211
credentials.walletInfo! .derivationInfo = di;
212
+ credentials.walletInfo! .network = network;
213
+ credentials.walletInfo! .dirPath = "" ;
175
214
final wallet = DecredWallet (credentials.walletInfo! , credentials.password! ,
176
215
this .unspentCoinsInfoSource, libwallet! , closeLibwallet);
177
216
await wallet.init ();
0 commit comments