-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathmigrate_wallets_to_isar.dart
221 lines (192 loc) · 6.91 KB
/
migrate_wallets_to_isar.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import 'dart:convert';
import 'package:isar/isar.dart';
import '../app_config.dart';
import '../models/isar/models/isar_models.dart';
import '../utilities/flutter_secure_storage_interface.dart';
import '../wallets/crypto_currency/crypto_currency.dart';
import '../wallets/isar/models/token_wallet_info.dart';
import '../wallets/isar/models/wallet_info.dart';
import '../wallets/isar/models/wallet_info_meta.dart';
import '../wallets/wallet/supporting/epiccash_wallet_info_extension.dart';
import 'hive/db.dart';
import 'isar/main_db.dart';
Future<void> migrateWalletsToIsar({
required SecureStorageInterface secureStore,
}) async {
await MainDB.instance.initMainDB();
// ensure fresh
await MainDB.instance.isar
.writeTxn(() async => await MainDB.instance.isar.transactionV2s.clear());
final allWalletsBox =
await DB.instance.hive.openBox<dynamic>(DB.boxNameAllWalletsData);
final names = DB.instance
.get<dynamic>(boxName: DB.boxNameAllWalletsData, key: 'names') as Map?;
if (names == null) {
// no wallets to migrate
return;
}
//
// Parse the old data from the Hive map into a nice list
//
final List<
({
String coinIdentifier,
String name,
String walletId,
})> oldInfo = Map<String, dynamic>.from(names).values.map((e) {
final map = e as Map;
return (
coinIdentifier: map["coin"] as String,
walletId: map["id"] as String,
name: map["name"] as String,
);
}).toList();
//
// Get current ordered list of favourite wallet Ids
//
final List<String> favourites =
(await DB.instance.hive.openBox<String>(DB.boxNameFavoriteWallets))
.values
.toList();
final List<(WalletInfo, WalletInfoMeta)> newInfo = [];
final List<TokenWalletInfo> tokenInfo = [];
final List<TransactionNote> migratedNotes = [];
//
// Convert each old info into the new Isar WalletInfo
//
for (final old in oldInfo) {
final walletBox = await DB.instance.hive.openBox<dynamic>(old.walletId);
//
// First handle transaction notes
//
final newNoteCount = await MainDB.instance.isar.transactionNotes
.where()
.walletIdEqualTo(old.walletId)
.count();
if (newNoteCount == 0) {
final map = walletBox.get('notes') as Map?;
if (map != null) {
final notes = Map<String, String>.from(map);
for (final txid in notes.keys) {
final note = notes[txid];
if (note != null && note.isNotEmpty) {
final newNote = TransactionNote(
walletId: old.walletId,
txid: txid,
value: note,
);
migratedNotes.add(newNote);
}
}
}
}
// reset stellar + tezos address type
if (old.coinIdentifier == Stellar(CryptoCurrencyNetwork.main).identifier ||
old.coinIdentifier == Stellar(CryptoCurrencyNetwork.test).identifier ||
old.coinIdentifier == Tezos(CryptoCurrencyNetwork.main).identifier) {
await MainDB.instance.deleteWalletBlockchainData(old.walletId);
}
//
// Set other data values
//
final Map<String, dynamic> otherData = {};
final List<String>? tokenContractAddresses = walletBox.get(
"ethTokenContracts",
) as List<String>?;
if (tokenContractAddresses?.isNotEmpty == true) {
otherData[WalletInfoKeys.tokenContractAddresses] = tokenContractAddresses;
for (final address in tokenContractAddresses!) {
final contract = await MainDB.instance.isar.ethContracts
.where()
.addressEqualTo(address)
.findFirst();
if (contract != null) {
tokenInfo.add(
TokenWalletInfo(
walletId: old.walletId,
tokenAddress: address,
tokenFractionDigits: contract.decimals,
),
);
}
}
}
// epiccash specifics
if (old.coinIdentifier == Epiccash(CryptoCurrencyNetwork.main)) {
final epicWalletInfo = ExtraEpiccashWalletInfo.fromMap({
"receivingIndex": walletBox.get("receivingIndex") as int? ?? 0,
"changeIndex": walletBox.get("changeIndex") as int? ?? 0,
"slatesToAddresses": walletBox.get("slate_to_address") as Map? ?? {},
"slatesToCommits": walletBox.get("slatesToCommits") as Map? ?? {},
"lastScannedBlock": walletBox.get("lastScannedBlock") as int? ?? 0,
"restoreHeight": walletBox.get("restoreHeight") as int? ?? 0,
"creationHeight": walletBox.get("creationHeight") as int? ?? 0,
});
otherData[WalletInfoKeys.epiccashData] = jsonEncode(
epicWalletInfo.toMap(),
);
} else if (old.coinIdentifier ==
Firo(CryptoCurrencyNetwork.main).identifier ||
old.coinIdentifier == Firo(CryptoCurrencyNetwork.test).identifier) {
otherData[WalletInfoKeys.lelantusCoinIsarRescanRequired] = walletBox
.get(WalletInfoKeys.lelantusCoinIsarRescanRequired) as bool? ??
true;
}
//
// Clear out any keys with null values as they are not needed
//
otherData.removeWhere((key, value) => value == null);
final infoMeta = WalletInfoMeta(
walletId: old.walletId,
isMnemonicVerified: allWalletsBox
.get("${old.walletId}_mnemonicHasBeenVerified") as bool? ??
false,
);
final info = WalletInfo(
coinName: old.coinIdentifier,
walletId: old.walletId,
name: old.name,
mainAddressType: AppConfig.getCryptoCurrencyFor(old.coinIdentifier)!
.defaultAddressType,
favouriteOrderIndex: favourites.indexOf(old.walletId),
cachedChainHeight: walletBox.get(
DBKeys.storedChainHeight,
) as int? ??
0,
cachedBalanceString: walletBox.get(
DBKeys.cachedBalance,
) as String?,
cachedBalanceSecondaryString: walletBox.get(
DBKeys.cachedBalanceSecondary,
) as String?,
otherDataJsonString: jsonEncode(otherData),
);
newInfo.add((info, infoMeta));
}
if (migratedNotes.isNotEmpty) {
await MainDB.instance.isar.writeTxn(() async {
await MainDB.instance.isar.transactionNotes.putAll(migratedNotes);
});
}
if (newInfo.isNotEmpty) {
await MainDB.instance.isar.writeTxn(() async {
await MainDB.instance.isar.walletInfo
.putAll(newInfo.map((e) => e.$1).toList());
await MainDB.instance.isar.walletInfoMeta
.putAll(newInfo.map((e) => e.$2).toList());
if (tokenInfo.isNotEmpty) {
await MainDB.instance.isar.tokenWalletInfo.putAll(tokenInfo);
}
});
}
await _cleanupOnSuccess(
walletIds: newInfo.map((e) => e.$1.walletId).toList(),
);
}
Future<void> _cleanupOnSuccess({required List<String> walletIds}) async {
await DB.instance.hive.deleteBoxFromDisk(DB.boxNameFavoriteWallets);
await DB.instance.hive.deleteBoxFromDisk(DB.boxNameAllWalletsData);
for (final walletId in walletIds) {
await DB.instance.hive.deleteBoxFromDisk(walletId);
}
}