stack_wallet/lib/db/migrate_wallets_to_isar.dart

160 lines
4.9 KiB
Dart
Raw Normal View History

2023-09-18 21:28:31 +00:00
import 'dart:convert';
import 'package:hive_flutter/hive_flutter.dart';
2023-11-09 15:33:51 +00:00
import 'package:isar/isar.dart';
2023-09-18 21:28:31 +00:00
import 'package:stackwallet/db/hive/db.dart';
2023-11-06 17:01:52 +00:00
import 'package:stackwallet/db/isar/main_db.dart';
2023-11-09 15:33:51 +00:00
import 'package:stackwallet/models/isar/models/transaction_note.dart';
2023-09-18 21:28:31 +00:00
import 'package:stackwallet/utilities/enums/coin_enum.dart';
import 'package:stackwallet/utilities/flutter_secure_storage_interface.dart';
import 'package:stackwallet/wallets/isar/models/wallet_info.dart';
2023-09-18 21:28:31 +00:00
import 'package:stackwallet/wallets/wallet/supporting/epiccash_wallet_info_extension.dart';
2023-11-06 17:01:52 +00:00
Future<void> migrateWalletsToIsar({
2023-09-18 21:28:31 +00:00
required SecureStorageInterface secureStore,
}) async {
final allWalletsBox = await 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<
({
Coin coin,
String name,
String walletId,
})> oldInfo = Map<String, dynamic>.from(names).values.map((e) {
final map = e as Map;
return (
coin: Coin.values.byName(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 Hive.openBox<String>(DB.boxNameFavoriteWallets)).values.toList();
final List<WalletInfo> newInfo = [];
2023-11-09 15:33:51 +00:00
final List<TransactionNote> migratedNotes = [];
2023-09-18 21:28:31 +00:00
//
// Convert each old info into the new Isar WalletInfo
//
for (final old in oldInfo) {
final walletBox = await Hive.openBox<dynamic>(old.walletId);
2023-11-09 15:33:51 +00:00
//
// 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);
}
}
}
}
2023-09-18 21:28:31 +00:00
//
// Set other data values
//
Map<String, dynamic> otherData = {};
otherData[WalletInfoKeys.tokenContractAddresses] = walletBox.get(
DBKeys.ethTokenContracts,
) as List<String>?;
// epiccash specifics
if (old.coin == Coin.epicCash) {
final epicWalletInfo = ExtraEpiccashWalletInfo.fromMap({
"receivingIndex": walletBox.get("receivingIndex") as int? ?? 0,
"changeIndex": walletBox.get("changeIndex") as int? ?? 0,
2023-11-06 17:23:51 +00:00
"slatesToAddresses": walletBox.get("slate_to_address") as Map? ?? {},
2023-09-18 21:28:31 +00:00
"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(),
);
}
//
// Clear out any keys with null values as they are not needed
//
otherData.removeWhere((key, value) => value == null);
final info = WalletInfo(
coinName: old.coin.name,
walletId: old.walletId,
name: old.name,
2023-11-09 22:27:17 +00:00
mainAddressType: old.coin.primaryAddressType,
2023-09-18 21:28:31 +00:00
favouriteOrderIndex: favourites.indexOf(old.walletId),
2023-11-06 17:37:23 +00:00
isMnemonicVerified: allWalletsBox
.get("${old.walletId}_mnemonicHasBeenVerified") as bool? ??
false,
2023-09-18 21:28:31 +00:00
cachedChainHeight: walletBox.get(
DBKeys.storedChainHeight,
) as int? ??
0,
cachedBalanceString: walletBox.get(
DBKeys.cachedBalance,
) as String?,
2023-11-16 23:32:47 +00:00
cachedBalanceSecondaryString: walletBox.get(
DBKeys.cachedBalanceSecondary,
) as String?,
2023-09-18 21:28:31 +00:00
otherDataJsonString: jsonEncode(otherData),
);
newInfo.add(info);
}
2023-11-06 17:01:52 +00:00
2023-11-09 15:33:51 +00:00
if (migratedNotes.isNotEmpty) {
await MainDB.instance.isar.writeTxn(() async {
await MainDB.instance.isar.transactionNotes.putAll(migratedNotes);
});
}
2023-11-06 17:01:52 +00:00
await MainDB.instance.isar.writeTxn(() async {
await MainDB.instance.isar.walletInfo.putAll(newInfo);
});
2023-11-06 17:37:23 +00:00
await _cleanupOnSuccess(walletIds: newInfo.map((e) => e.walletId).toList());
2023-09-18 21:28:31 +00:00
}
2023-11-06 17:37:23 +00:00
Future<void> _cleanupOnSuccess({required List<String> walletIds}) async {
2023-09-18 21:28:31 +00:00
await Hive.deleteBoxFromDisk(DB.boxNameFavoriteWallets);
await Hive.deleteBoxFromDisk(DB.boxNameAllWalletsData);
for (final walletId in walletIds) {
await Hive.deleteBoxFromDisk(walletId);
}
}