/* * This file is part of Stack Wallet. * * Copyright (c) 2023 Cypher Stack * All Rights Reserved. * The code is distributed under GPLv3 license, see LICENSE file for details. * Generated by Cypher Stack on 2023-05-26 * */ import 'dart:isolate'; import 'package:cw_core/wallet_info.dart' as xmr; import 'package:hive/hive.dart'; import 'package:mutex/mutex.dart'; import 'package:stackwallet/models/exchange/response_objects/trade.dart'; import 'package:stackwallet/models/node_model.dart'; import 'package:stackwallet/models/notification_model.dart'; import 'package:stackwallet/models/trade_wallet_lookup.dart'; import 'package:stackwallet/services/wallets_service.dart'; import 'package:stackwallet/utilities/enums/coin_enum.dart'; import 'package:stackwallet/utilities/logger.dart'; class DB { // legacy (required for migrations) @Deprecated("Left over for migration from old versions of Stack Wallet") static const String boxNameAddressBook = "addressBook"; static const String boxNameTrades = "exchangeTransactionsBox"; static const String boxNameAllWalletsData = "wallets"; static const String boxNameFavoriteWallets = "favoriteWallets"; // in use // TODO: migrate static const String boxNameNodeModels = "nodeModels"; static const String boxNamePrimaryNodes = "primaryNodes"; static const String boxNameNotifications = "notificationModels"; static const String boxNameWatchedTransactions = "watchedTxNotificationModels"; static const String boxNameWatchedTrades = "watchedTradesNotificationModels"; static const String boxNameTradesV2 = "exchangeTradesBox"; static const String boxNameTradeNotes = "tradeNotesBox"; static const String boxNameTradeLookup = "tradeToTxidLookUpBox"; static const String boxNameWalletsToDeleteOnStart = "walletsToDeleteOnStart"; static const String boxNamePriceCache = "priceAPIPrice24hCache"; // in use (keep for now) static const String boxNameDBInfo = "dbInfo"; static const String boxNamePrefs = "prefs"; static const String boxNameOneTimeDialogsShown = "oneTimeDialogsShown"; String _boxNameTxCache({required Coin coin}) => "${coin.name}_txCache"; // firo only String _boxNameSetCache({required Coin coin}) => "${coin.name}_anonymitySetCache"; String _boxNameUsedSerialsCache({required Coin coin}) => "${coin.name}_usedSerialsCache"; Box? _boxNodeModels; Box? _boxPrimaryNodes; Box? _boxAllWalletsData; Box? _boxNotifications; Box? _boxWatchedTransactions; Box? _boxWatchedTrades; Box? _boxTradesV2; Box? _boxTradeNotes; Box? _boxFavoriteWallets; Box? _walletInfoSource; Box? _boxPrefs; Box? _boxTradeLookup; Box? _boxDBInfo; // Box? _boxDesktopData; final Map> _walletBoxes = {}; final Map> _txCacheBoxes = {}; final Map> _setCacheBoxes = {}; final Map> _usedSerialsCacheBoxes = {}; // exposed for monero Box get moneroWalletInfoBox => _walletInfoSource!; // mutex for stack backup final mutex = Mutex(); DB._(); static final DB _instance = DB._(); static DB get instance { // "This name does not uniquely identify an isolate. Multiple isolates in the same process may have the same debugName." // if (Isolate.current.debugName != "main") { // TODO: make sure this works properly if (Isolate.current.debugName != "main") { throw Exception( "DB.instance should not be accessed outside the main isolate!"); } return _instance; } // open hive boxes Future init() async { if (Hive.isBoxOpen(boxNameDBInfo)) { _boxDBInfo = Hive.box(boxNameDBInfo); } else { _boxDBInfo = await Hive.openBox(boxNameDBInfo); } await Hive.openBox(boxNameWalletsToDeleteOnStart); if (Hive.isBoxOpen(boxNamePrefs)) { _boxPrefs = Hive.box(boxNamePrefs); } else { _boxPrefs = await Hive.openBox(boxNamePrefs); } if (Hive.isBoxOpen(boxNameNodeModels)) { _boxNodeModels = Hive.box(boxNameNodeModels); } else { _boxNodeModels = await Hive.openBox(boxNameNodeModels); } if (Hive.isBoxOpen(boxNamePrimaryNodes)) { _boxPrimaryNodes = Hive.box(boxNamePrimaryNodes); } else { _boxPrimaryNodes = await Hive.openBox(boxNamePrimaryNodes); } if (Hive.isBoxOpen(boxNameAllWalletsData)) { _boxAllWalletsData = Hive.box(boxNameAllWalletsData); } else { _boxAllWalletsData = await Hive.openBox(boxNameAllWalletsData); } _boxNotifications = await Hive.openBox(boxNameNotifications); _boxWatchedTransactions = await Hive.openBox(boxNameWatchedTransactions); _boxWatchedTrades = await Hive.openBox(boxNameWatchedTrades); _boxTradesV2 = await Hive.openBox(boxNameTradesV2); _boxTradeNotes = await Hive.openBox(boxNameTradeNotes); _boxTradeLookup = await Hive.openBox(boxNameTradeLookup); _walletInfoSource = await Hive.openBox(xmr.WalletInfo.boxName); _boxFavoriteWallets = await Hive.openBox(boxNameFavoriteWallets); await Future.wait([ Hive.openBox(boxNamePriceCache), _loadWalletBoxes(), ]); } Future _loadWalletBoxes() async { final names = _boxAllWalletsData!.get("names") as Map? ?? {}; names.removeWhere((name, dyn) { final jsonObject = Map.from(dyn as Map); try { Coin.values.byName(jsonObject["coin"] as String); return false; } catch (e, s) { Logging.instance.log( "Error, ${jsonObject["coin"]} does not exist, $name wallet cannot be loaded", level: LogLevel.Error); return true; } }); final mapped = Map.from(names).map((name, dyn) => MapEntry( name, WalletInfo.fromJson(Map.from(dyn as Map)))); for (final entry in mapped.entries) { if (Hive.isBoxOpen(entry.value.walletId)) { _walletBoxes[entry.value.walletId] = Hive.box(entry.value.walletId); } else { _walletBoxes[entry.value.walletId] = await Hive.openBox(entry.value.walletId); } } } Future> getTxCacheBox({required Coin coin}) async { if (_txCacheBoxes[coin]?.isOpen != true) { _txCacheBoxes.remove(coin); } return _txCacheBoxes[coin] ??= await Hive.openBox(_boxNameTxCache(coin: coin)); } Future closeTxCacheBox({required Coin coin}) async { await _txCacheBoxes[coin]?.close(); } Future> getAnonymitySetCacheBox({required Coin coin}) async { if (_setCacheBoxes[coin]?.isOpen != true) { _setCacheBoxes.remove(coin); } return _setCacheBoxes[coin] ??= await Hive.openBox(_boxNameSetCache(coin: coin)); } Future closeAnonymitySetCacheBox({required Coin coin}) async { await _setCacheBoxes[coin]?.close(); } Future> getUsedSerialsCacheBox({required Coin coin}) async { if (_usedSerialsCacheBoxes[coin]?.isOpen != true) { _usedSerialsCacheBoxes.remove(coin); } return _usedSerialsCacheBoxes[coin] ??= await Hive.openBox(_boxNameUsedSerialsCache(coin: coin)); } Future closeUsedSerialsCacheBox({required Coin coin}) async { await _usedSerialsCacheBoxes[coin]?.close(); } /// Clear all cached transactions for the specified coin Future clearSharedTransactionCache({required Coin coin}) async { await deleteAll(boxName: _boxNameTxCache(coin: coin)); if (coin == Coin.firo) { await deleteAll(boxName: _boxNameSetCache(coin: coin)); await deleteAll(boxName: _boxNameUsedSerialsCache(coin: coin)); } } ///////////////////////////////////////// Future addWalletBox({required String walletId}) async { if (_walletBoxes[walletId] != null) { throw Exception("Attempted overwrite of existing wallet box!"); } _walletBoxes[walletId] = await Hive.openBox(walletId); } Future removeWalletBox({required String walletId}) async { _walletBoxes.remove(walletId); } /////////////////////////////////////////// // reads List keys({required String boxName}) => Hive.box(boxName).keys.toList(growable: false); List values({required String boxName}) => Hive.box(boxName).values.toList(growable: false); T? get({ required String boxName, required dynamic key, }) => Hive.box(boxName).get(key); bool containsKey({required String boxName, required dynamic key}) => Hive.box(boxName).containsKey(key); // writes Future put( {required String boxName, required dynamic key, required T value}) async => await mutex .protect(() async => await Hive.box(boxName).put(key, value)); Future add({required String boxName, required T value}) async => await mutex.protect(() async => await Hive.box(boxName).add(value)); Future addAll( {required String boxName, required Iterable values}) async => await mutex .protect(() async => await Hive.box(boxName).addAll(values)); Future delete( {required dynamic key, required String boxName}) async => await mutex.protect(() async => await Hive.box(boxName).delete(key)); Future deleteAll({required String boxName}) async { await mutex.protect(() async { final box = await Hive.openBox(boxName); await box.clear(); }); } Future deleteBoxFromDisk({required String boxName}) async => await mutex.protect(() async => await Hive.deleteBoxFromDisk(boxName)); /////////////////////////////////////////////////////////////////////////// Future deleteEverything() async { try { await DB.instance.deleteBoxFromDisk(boxName: DB.boxNameAddressBook); await DB.instance.deleteBoxFromDisk(boxName: "debugInfoBox"); await DB.instance.deleteBoxFromDisk(boxName: DB.boxNameNodeModels); await DB.instance.deleteBoxFromDisk(boxName: DB.boxNamePrimaryNodes); await DB.instance.deleteBoxFromDisk(boxName: DB.boxNameAllWalletsData); await DB.instance.deleteBoxFromDisk(boxName: DB.boxNameNotifications); await DB.instance .deleteBoxFromDisk(boxName: DB.boxNameWatchedTransactions); await DB.instance.deleteBoxFromDisk(boxName: DB.boxNameWatchedTrades); await DB.instance.deleteBoxFromDisk(boxName: DB.boxNameTrades); await DB.instance.deleteBoxFromDisk(boxName: DB.boxNameTradesV2); await DB.instance.deleteBoxFromDisk(boxName: DB.boxNameTradeNotes); await DB.instance.deleteBoxFromDisk(boxName: DB.boxNameTradeLookup); await DB.instance.deleteBoxFromDisk(boxName: DB.boxNameFavoriteWallets); await DB.instance.deleteBoxFromDisk(boxName: DB.boxNamePrefs); await DB.instance .deleteBoxFromDisk(boxName: DB.boxNameWalletsToDeleteOnStart); await DB.instance.deleteBoxFromDisk(boxName: DB.boxNamePriceCache); await DB.instance.deleteBoxFromDisk(boxName: DB.boxNameDBInfo); await DB.instance.deleteBoxFromDisk(boxName: "theme"); return true; } catch (e, s) { Logging.instance.log("$e $s", level: LogLevel.Error); return false; } } } abstract class DBKeys { static const String cachedBalance = "cachedBalance"; static const String cachedBalanceSecondary = "cachedBalanceSecondary"; static const String isFavorite = "isFavorite"; static const String id = "id"; static const String storedChainHeight = "storedChainHeight"; static const String ethTokenContracts = "ethTokenContracts"; }