diff --git a/lib/utilities/db_version_migration.dart b/lib/db/db_version_migration.dart similarity index 97% rename from lib/utilities/db_version_migration.dart rename to lib/db/db_version_migration.dart index f54c2b85a..bf26148a8 100644 --- a/lib/utilities/db_version_migration.dart +++ b/lib/db/db_version_migration.dart @@ -12,6 +12,7 @@ import 'package:hive/hive.dart'; import 'package:isar/isar.dart'; import 'package:stackwallet/db/hive/db.dart'; import 'package:stackwallet/db/isar/main_db.dart'; +import 'package:stackwallet/db/migrate_wallets_to_isar.dart'; import 'package:stackwallet/electrumx_rpc/electrumx.dart'; import 'package:stackwallet/models/contact.dart'; import 'package:stackwallet/models/exchange/change_now/exchange_transaction.dart'; @@ -353,74 +354,23 @@ class DbVersionMigrator with WalletDB { // try to continue migrating return await migrate(11, secureStore: secureStore); + case 11: + // migrate + await _v11(secureStore); + + // update version + await DB.instance.put( + boxName: DB.boxNameDBInfo, key: "hive_data_version", value: 12); + + // try to continue migrating + return await migrate(12, secureStore: secureStore); + default: // finally return return; } } - Future _v10(SecureStorageInterface secureStore) async { - await Hive.openBox(DB.boxNameAllWalletsData); - await Hive.openBox(DB.boxNamePrefs); - final walletsService = WalletsService(secureStorageInterface: secureStore); - final prefs = Prefs.instance; - final walletInfoList = await walletsService.walletNames; - await prefs.init(); - await MainDB.instance.initMainDB(); - - for (final walletId in walletInfoList.keys) { - final info = walletInfoList[walletId]!; - assert(info.walletId == walletId); - - if (info.coin == Coin.firo && - MainDB.instance.isar.lelantusCoins - .where() - .walletIdEqualTo(walletId) - .countSync() == - 0) { - final walletBox = await Hive.openBox(walletId); - - final hiveLCoins = DB.instance.get( - boxName: walletId, - key: "_lelantus_coins", - ) as List? ?? - []; - - final jindexes = (DB.instance - .get(boxName: walletId, key: "jindex") as List? ?? - []) - .cast(); - - final List coins = []; - for (final e in hiveLCoins) { - final map = e as Map; - final lcoin = map.values.first as LelantusCoin; - - final isJMint = jindexes.contains(lcoin.index); - - final coin = isar_models.LelantusCoin( - walletId: walletId, - txid: lcoin.txId, - value: lcoin.value.toString(), - mintIndex: lcoin.index, - anonymitySetId: lcoin.anonymitySetId, - isUsed: lcoin.isUsed, - isJMint: isJMint, - otherData: null, - ); - - coins.add(coin); - } - - if (coins.isNotEmpty) { - await MainDB.instance.isar.writeTxn(() async { - await MainDB.instance.isar.lelantusCoins.putAll(coins); - }); - } - } - } - } - Future _v4(SecureStorageInterface secureStore) async { await Hive.openBox(DB.boxNameAllWalletsData); await Hive.openBox(DB.boxNamePrefs); @@ -619,4 +569,70 @@ class DbVersionMigrator with WalletDB { await addressBookBox.deleteFromDisk(); } + + Future _v10(SecureStorageInterface secureStore) async { + await Hive.openBox(DB.boxNameAllWalletsData); + await Hive.openBox(DB.boxNamePrefs); + final walletsService = WalletsService(secureStorageInterface: secureStore); + final prefs = Prefs.instance; + final walletInfoList = await walletsService.walletNames; + await prefs.init(); + await MainDB.instance.initMainDB(); + + for (final walletId in walletInfoList.keys) { + final info = walletInfoList[walletId]!; + assert(info.walletId == walletId); + + if (info.coin == Coin.firo && + MainDB.instance.isar.lelantusCoins + .where() + .walletIdEqualTo(walletId) + .countSync() == + 0) { + final walletBox = await Hive.openBox(walletId); + + final hiveLCoins = DB.instance.get( + boxName: walletId, + key: "_lelantus_coins", + ) as List? ?? + []; + + final jindexes = (DB.instance + .get(boxName: walletId, key: "jindex") as List? ?? + []) + .cast(); + + final List coins = []; + for (final e in hiveLCoins) { + final map = e as Map; + final lcoin = map.values.first as LelantusCoin; + + final isJMint = jindexes.contains(lcoin.index); + + final coin = isar_models.LelantusCoin( + walletId: walletId, + txid: lcoin.txId, + value: lcoin.value.toString(), + mintIndex: lcoin.index, + anonymitySetId: lcoin.anonymitySetId, + isUsed: lcoin.isUsed, + isJMint: isJMint, + otherData: null, + ); + + coins.add(coin); + } + + if (coins.isNotEmpty) { + await MainDB.instance.isar.writeTxn(() async { + await MainDB.instance.isar.lelantusCoins.putAll(coins); + }); + } + } + } + } + + Future _v11(SecureStorageInterface secureStore) async { + await migrateWalletsToIsar(secureStore: secureStore); + } } diff --git a/lib/wallets/migration/migrate_wallets.dart b/lib/db/migrate_wallets_to_isar.dart similarity index 96% rename from lib/wallets/migration/migrate_wallets.dart rename to lib/db/migrate_wallets_to_isar.dart index 7e9ca5274..406d655ff 100644 --- a/lib/wallets/migration/migrate_wallets.dart +++ b/lib/db/migrate_wallets_to_isar.dart @@ -2,13 +2,14 @@ import 'dart:convert'; import 'package:hive_flutter/hive_flutter.dart'; import 'package:stackwallet/db/hive/db.dart'; +import 'package:stackwallet/db/isar/main_db.dart'; import 'package:stackwallet/models/isar/models/blockchain_data/address.dart'; 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'; import 'package:stackwallet/wallets/wallet/supporting/epiccash_wallet_info_extension.dart'; -void migrateWallets({ +Future migrateWalletsToIsar({ required SecureStorageInterface secureStore, }) async { final allWalletsBox = await Hive.openBox(DB.boxNameAllWalletsData); @@ -105,6 +106,10 @@ void migrateWallets({ newInfo.add(info); } + + await MainDB.instance.isar.writeTxn(() async { + await MainDB.instance.isar.walletInfo.putAll(newInfo); + }); } void _cleanupOnSuccess({required List walletIds}) async { diff --git a/lib/pages_desktop_specific/password/desktop_login_view.dart b/lib/pages_desktop_specific/password/desktop_login_view.dart index 471da2763..cdd6c8c63 100644 --- a/lib/pages_desktop_specific/password/desktop_login_view.dart +++ b/lib/pages_desktop_specific/password/desktop_login_view.dart @@ -14,6 +14,7 @@ import 'dart:io'; import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_svg/svg.dart'; +import 'package:stackwallet/db/db_version_migration.dart'; import 'package:stackwallet/db/hive/db.dart'; import 'package:stackwallet/notifications/show_flush_bar.dart'; import 'package:stackwallet/pages_desktop_specific/desktop_home_view.dart'; @@ -24,7 +25,6 @@ import 'package:stackwallet/themes/stack_colors.dart'; import 'package:stackwallet/themes/theme_providers.dart'; import 'package:stackwallet/utilities/assets.dart'; import 'package:stackwallet/utilities/constants.dart'; -import 'package:stackwallet/utilities/db_version_migration.dart'; import 'package:stackwallet/utilities/flutter_secure_storage_interface.dart'; import 'package:stackwallet/utilities/logger.dart'; import 'package:stackwallet/utilities/text_styles.dart';