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