mirror of
https://github.com/cypherstack/stack_wallet.git
synced 2025-01-29 21:55:58 +00:00
hive data migrate to v1 for lelantus coins update
This commit is contained in:
parent
497e8791b0
commit
8fc2e5ee1b
4 changed files with 163 additions and 95 deletions
|
@ -27,6 +27,7 @@ class DB {
|
|||
static const String boxNamePrefs = "prefs";
|
||||
static const String boxNameWalletsToDeleteOnStart = "walletsToDeleteOnStart";
|
||||
static const String boxNamePriceCache = "priceAPIPrice24hCache";
|
||||
static const String boxNameDBInfo = "dbInfo";
|
||||
|
||||
String boxNameTxCache({required Coin coin}) => "${coin.name}_txCache";
|
||||
String boxNameSetCache({required Coin coin}) =>
|
||||
|
@ -50,6 +51,7 @@ class DB {
|
|||
late final Box<xmr.WalletInfo> _walletInfoSource;
|
||||
late final Box<dynamic> _boxPrefs;
|
||||
late final Box<TradeWalletLookup> _boxTradeLookup;
|
||||
late final Box<dynamic> _boxDBInfo;
|
||||
|
||||
final Map<String, Box<dynamic>> _walletBoxes = {};
|
||||
|
||||
|
@ -80,13 +82,40 @@ class DB {
|
|||
// open hive boxes
|
||||
Future<void> init() async {
|
||||
if (!_initialized) {
|
||||
if (Hive.isBoxOpen(boxNameDBInfo)) {
|
||||
_boxDBInfo = Hive.box<dynamic>(boxNameDBInfo);
|
||||
} else {
|
||||
_boxDBInfo = await Hive.openBox<dynamic>(boxNameDBInfo);
|
||||
}
|
||||
await Hive.openBox<String>(boxNameWalletsToDeleteOnStart);
|
||||
_boxPrefs = await Hive.openBox<dynamic>(boxNamePrefs);
|
||||
|
||||
if (Hive.isBoxOpen(boxNamePrefs)) {
|
||||
_boxPrefs = Hive.box<dynamic>(boxNamePrefs);
|
||||
} else {
|
||||
_boxPrefs = await Hive.openBox<dynamic>(boxNamePrefs);
|
||||
}
|
||||
|
||||
_boxAddressBook = await Hive.openBox<dynamic>(boxNameAddressBook);
|
||||
_boxDebugInfo = await Hive.openBox<String>(boxNameDebugInfo);
|
||||
_boxNodeModels = await Hive.openBox<NodeModel>(boxNameNodeModels);
|
||||
_boxPrimaryNodes = await Hive.openBox<NodeModel>(boxNamePrimaryNodes);
|
||||
_boxAllWalletsData = await Hive.openBox<dynamic>(boxNameAllWalletsData);
|
||||
|
||||
if (Hive.isBoxOpen(boxNameNodeModels)) {
|
||||
_boxNodeModels = Hive.box<NodeModel>(boxNameNodeModels);
|
||||
} else {
|
||||
_boxNodeModels = await Hive.openBox<NodeModel>(boxNameNodeModels);
|
||||
}
|
||||
|
||||
if (Hive.isBoxOpen(boxNamePrimaryNodes)) {
|
||||
_boxPrimaryNodes = Hive.box<NodeModel>(boxNamePrimaryNodes);
|
||||
} else {
|
||||
_boxPrimaryNodes = await Hive.openBox<NodeModel>(boxNamePrimaryNodes);
|
||||
}
|
||||
|
||||
if (Hive.isBoxOpen(boxNameAllWalletsData)) {
|
||||
_boxAllWalletsData = Hive.box<dynamic>(boxNameAllWalletsData);
|
||||
} else {
|
||||
_boxAllWalletsData = await Hive.openBox<dynamic>(boxNameAllWalletsData);
|
||||
}
|
||||
|
||||
_boxNotifications =
|
||||
await Hive.openBox<NotificationModel>(boxNameNotifications);
|
||||
_boxWatchedTransactions =
|
||||
|
@ -116,8 +145,13 @@ class DB {
|
|||
name, WalletInfo.fromJson(Map<String, dynamic>.from(dyn as Map))));
|
||||
|
||||
for (final entry in mapped.entries) {
|
||||
_walletBoxes[entry.value.walletId] =
|
||||
await Hive.openBox<dynamic>(entry.value.walletId);
|
||||
if (Hive.isBoxOpen(entry.value.walletId)) {
|
||||
_walletBoxes[entry.value.walletId] =
|
||||
Hive.box<dynamic>(entry.value.walletId);
|
||||
} else {
|
||||
_walletBoxes[entry.value.walletId] =
|
||||
await Hive.openBox<dynamic>(entry.value.walletId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -51,6 +51,7 @@ import 'package:stackwallet/services/trade_service.dart';
|
|||
import 'package:stackwallet/services/wallets.dart';
|
||||
import 'package:stackwallet/utilities/cfcolors.dart';
|
||||
import 'package:stackwallet/utilities/constants.dart';
|
||||
import 'package:stackwallet/utilities/db_version_migration.dart';
|
||||
import 'package:stackwallet/utilities/enums/backup_frequency_type.dart';
|
||||
import 'package:stackwallet/utilities/logger.dart';
|
||||
import 'package:stackwallet/utilities/prefs.dart';
|
||||
|
@ -119,18 +120,16 @@ void main() async {
|
|||
|
||||
Hive.registerAdapter(UnspentCoinsInfoAdapter());
|
||||
|
||||
await Hive.openBox<dynamic>(DB.boxNameDBInfo);
|
||||
int dbVersion = DB.instance.get<dynamic>(
|
||||
boxName: DB.boxNameDBInfo, key: "hive_data_version") as int? ??
|
||||
0;
|
||||
if (dbVersion < Constants.currentHiveDbVersion) {
|
||||
await DbVersionMigrator().migrate(dbVersion);
|
||||
}
|
||||
|
||||
monero.onStartup();
|
||||
|
||||
// final wallets = await Hive.openBox('wallets');
|
||||
// await wallets.put('currentWalletName', "");
|
||||
|
||||
// NOT USED YET
|
||||
// int dbVersion = await wallets.get("db_version");
|
||||
// if (dbVersion == null || dbVersion < Constants.currentDbVersion) {
|
||||
// if (dbVersion == null) dbVersion = 0;
|
||||
// await DbVersionMigrator().migrate(dbVersion);
|
||||
// }
|
||||
|
||||
// SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual,
|
||||
// overlays: [SystemUiOverlay.bottom]);
|
||||
await NotificationApi.init();
|
||||
|
|
|
@ -33,7 +33,7 @@ abstract class Constants {
|
|||
// Enable Logger.print statements
|
||||
static const bool disableLogger = false;
|
||||
|
||||
static const int currentDbVersion = 0;
|
||||
static const int currentHiveDbVersion = 1;
|
||||
|
||||
static List<int> possibleLengthsForCoin(Coin coin) {
|
||||
final List<int> values = [];
|
||||
|
|
|
@ -1,5 +1,16 @@
|
|||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
import 'package:hive/hive.dart';
|
||||
import 'package:stackwallet/electrumx_rpc/electrumx.dart';
|
||||
import 'package:stackwallet/hive/db.dart';
|
||||
import 'package:stackwallet/models/lelantus_coin.dart';
|
||||
import 'package:stackwallet/models/node_model.dart';
|
||||
import 'package:stackwallet/services/node_service.dart';
|
||||
import 'package:stackwallet/services/wallets_service.dart';
|
||||
import 'package:stackwallet/utilities/default_nodes.dart';
|
||||
import 'package:stackwallet/utilities/enums/coin_enum.dart';
|
||||
import 'package:stackwallet/utilities/flutter_secure_storage_interface.dart';
|
||||
import 'package:stackwallet/utilities/logger.dart';
|
||||
import 'package:stackwallet/utilities/prefs.dart';
|
||||
|
||||
class DbVersionMigrator {
|
||||
Future<void> migrate(
|
||||
|
@ -8,83 +19,107 @@ class DbVersionMigrator {
|
|||
FlutterSecureStorage(),
|
||||
),
|
||||
}) async {
|
||||
// final wallets = await Hive.openBox('wallets');
|
||||
// final names = Map<String, String>.from((await wallets.get("names")) ?? {});
|
||||
//
|
||||
// switch (fromVersion) {
|
||||
// case 0:
|
||||
// // migrate each
|
||||
// for (final entry in names.entries) {
|
||||
// final walletId = entry.value;
|
||||
// final walletName = entry.key;
|
||||
//
|
||||
// // move main/test network to walletId based
|
||||
// final network = await wallets.get("${entry.key}_network");
|
||||
// await wallets.put("${walletId}_network", network);
|
||||
// await wallets.delete("${walletName}_network");
|
||||
//
|
||||
// final old = await Hive.openBox(walletName);
|
||||
// final wallet = await Hive.openBox(walletId);
|
||||
//
|
||||
// // notes
|
||||
// final oldNotes = await old.get("notes");
|
||||
// await wallet.put("notes", oldNotes);
|
||||
// await old.delete("notes");
|
||||
//
|
||||
// // address book
|
||||
// final addressBook = await old.get("addressBookEntries");
|
||||
// await wallet.put("addressBookEntries", addressBook);
|
||||
// await old.put("addressBookEntries", null);
|
||||
//
|
||||
// // receiveDerivations
|
||||
// Map<String, dynamic> newReceiveDerivations = {};
|
||||
// final receiveDerivations =
|
||||
// Map<int, dynamic>.from(await old.get("receiveDerivations") ?? {});
|
||||
//
|
||||
// for (int i = 0; i < receiveDerivations.length; i++) {
|
||||
// receiveDerivations[i].remove("fingerprint");
|
||||
// receiveDerivations[i].remove("identifier");
|
||||
// receiveDerivations[i].remove("privateKey");
|
||||
// newReceiveDerivations["$i"] = receiveDerivations[i];
|
||||
// }
|
||||
// final receiveDerivationsString = jsonEncode(newReceiveDerivations);
|
||||
//
|
||||
// await secureStore.write(
|
||||
// key: "${walletId}_receiveDerivations",
|
||||
// value: receiveDerivationsString);
|
||||
// await old.delete("receiveDerivations");
|
||||
//
|
||||
// // changeDerivations
|
||||
// Map<String, dynamic> newChangeDerivations = {};
|
||||
// final changeDerivations =
|
||||
// Map<int, dynamic>.from(await old.get("changeDerivations") ?? {});
|
||||
//
|
||||
// for (int i = 0; i < changeDerivations.length; i++) {
|
||||
// changeDerivations[i].remove("fingerprint");
|
||||
// changeDerivations[i].remove("identifier");
|
||||
// changeDerivations[i].remove("privateKey");
|
||||
// newChangeDerivations["$i"] = changeDerivations[i];
|
||||
// }
|
||||
// final changeDerivationsString = jsonEncode(newChangeDerivations);
|
||||
//
|
||||
// await secureStore.write(
|
||||
// key: "${walletId}_changeDerivations",
|
||||
// value: changeDerivationsString);
|
||||
// await old.delete("changeDerivations");
|
||||
// }
|
||||
//
|
||||
// // finally update version
|
||||
// await wallets.put("db_version", 1);
|
||||
//
|
||||
// return;
|
||||
// // not needed yet
|
||||
// // return migrate(1);
|
||||
//
|
||||
// // case 1:
|
||||
// // return migrate(2);
|
||||
//
|
||||
// default:
|
||||
// return;
|
||||
// }
|
||||
switch (fromVersion) {
|
||||
case 0:
|
||||
await Hive.openBox<dynamic>(DB.boxNameAllWalletsData);
|
||||
await Hive.openBox<dynamic>(DB.boxNamePrefs);
|
||||
final walletsService = WalletsService();
|
||||
final nodeService = NodeService();
|
||||
final prefs = Prefs.instance;
|
||||
final walletInfoList = await walletsService.walletNames;
|
||||
await prefs.init();
|
||||
|
||||
ElectrumX? client;
|
||||
int? latestSetId;
|
||||
|
||||
// only instantiate client if there are firo wallets
|
||||
if (walletInfoList.values.any((element) => element.coin == Coin.firo)) {
|
||||
await Hive.openBox<NodeModel>(DB.boxNameNodeModels);
|
||||
await Hive.openBox<NodeModel>(DB.boxNamePrimaryNodes);
|
||||
final node = nodeService.getPrimaryNodeFor(coin: Coin.firo) ??
|
||||
DefaultNodes.firo;
|
||||
List<ElectrumXNode> failovers = nodeService
|
||||
.failoverNodesFor(coin: Coin.firo)
|
||||
.map(
|
||||
(e) => ElectrumXNode(
|
||||
address: e.host,
|
||||
port: e.port,
|
||||
name: e.name,
|
||||
id: e.id,
|
||||
useSSL: e.useSSL,
|
||||
),
|
||||
)
|
||||
.toList();
|
||||
|
||||
client = ElectrumX.from(
|
||||
node: ElectrumXNode(
|
||||
address: node.host,
|
||||
port: node.port,
|
||||
name: node.name,
|
||||
id: node.id,
|
||||
useSSL: node.useSSL),
|
||||
prefs: prefs,
|
||||
failovers: failovers,
|
||||
);
|
||||
|
||||
try {
|
||||
latestSetId = await client.getLatestCoinId();
|
||||
} catch (e) {
|
||||
// default to 2 for now
|
||||
latestSetId = 2;
|
||||
Logging.instance.log(
|
||||
"Failed to fetch latest coin id during firo db migrate: $e \nUsing a default value of 2",
|
||||
level: LogLevel.Warning);
|
||||
}
|
||||
}
|
||||
|
||||
for (final walletInfo in walletInfoList.values) {
|
||||
// migrate each firo wallet's lelantus coins
|
||||
if (walletInfo.coin == Coin.firo) {
|
||||
await Hive.openBox<dynamic>(walletInfo.walletId);
|
||||
final _lelantusCoins = DB.instance.get<dynamic>(
|
||||
boxName: walletInfo.walletId, key: '_lelantus_coins') as List?;
|
||||
final List<Map<dynamic, LelantusCoin>> lelantusCoins = [];
|
||||
for (var lCoin in _lelantusCoins ?? []) {
|
||||
lelantusCoins
|
||||
.add({lCoin.keys.first: lCoin.values.first as LelantusCoin});
|
||||
}
|
||||
|
||||
List<Map<dynamic, LelantusCoin>> coins = [];
|
||||
for (final element in lelantusCoins) {
|
||||
LelantusCoin coin = element.values.first;
|
||||
int anonSetId = coin.anonymitySetId;
|
||||
if (coin.anonymitySetId == 1 &&
|
||||
(coin.publicCoin == '' ||
|
||||
coin.publicCoin == "jmintData.publicCoin")) {
|
||||
anonSetId = latestSetId!;
|
||||
}
|
||||
coins.add({
|
||||
element.keys.first: LelantusCoin(coin.index, coin.value,
|
||||
coin.publicCoin, coin.txId, anonSetId, coin.isUsed)
|
||||
});
|
||||
}
|
||||
Logger.print("newcoins $coins", normalLength: false);
|
||||
await DB.instance.put<dynamic>(
|
||||
boxName: walletInfo.walletId,
|
||||
key: '_lelantus_coins',
|
||||
value: coins);
|
||||
}
|
||||
}
|
||||
|
||||
// finally update version
|
||||
await DB.instance.put<dynamic>(
|
||||
boxName: DB.boxNameDBInfo, key: "hive_data_version", value: 1);
|
||||
|
||||
return;
|
||||
// not needed yet
|
||||
// return migrate(1);
|
||||
|
||||
// case 1:
|
||||
// return migrate(2);
|
||||
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue