@@ -3,7 +3,6 @@ import 'dart:isolate';
33import 'package:cw_core/wallet_info.dart' as xmr;
44import 'package:hive/hive.dart' ;
55import 'package:mutex/mutex.dart' ;
6- import 'package:stackduo/models/exchange/change_now/exchange_transaction.dart' ;
76import 'package:stackduo/models/exchange/response_objects/trade.dart' ;
87import 'package:stackduo/models/node_model.dart' ;
98import 'package:stackduo/models/notification_model.dart' ;
@@ -13,50 +12,53 @@ import 'package:stackduo/utilities/enums/coin_enum.dart';
1312import 'package:stackduo/utilities/logger.dart' ;
1413
1514class DB {
15+ // legacy (required for migrations)
16+ @Deprecated ("Left over for migration from old versions of Stack Wallet" )
1617 static const String boxNameAddressBook = "addressBook" ;
17- static const String boxNameDebugInfo = "debugInfoBox" ;
18+ static const String boxNameTrades = "exchangeTransactionsBox" ;
19+
20+ // in use
21+ // TODO: migrate
1822 static const String boxNameNodeModels = "nodeModels" ;
1923 static const String boxNamePrimaryNodes = "primaryNodes" ;
2024 static const String boxNameAllWalletsData = "wallets" ;
2125 static const String boxNameNotifications = "notificationModels" ;
2226 static const String boxNameWatchedTransactions =
2327 "watchedTxNotificationModels" ;
2428 static const String boxNameWatchedTrades = "watchedTradesNotificationModels" ;
25- static const String boxNameTrades = "exchangeTransactionsBox" ;
2629 static const String boxNameTradesV2 = "exchangeTradesBox" ;
2730 static const String boxNameTradeNotes = "tradeNotesBox" ;
2831 static const String boxNameTradeLookup = "tradeToTxidLookUpBox" ;
2932 static const String boxNameFavoriteWallets = "favoriteWallets" ;
30- static const String boxNamePrefs = "prefs" ;
3133 static const String boxNameWalletsToDeleteOnStart = "walletsToDeleteOnStart" ;
3234 static const String boxNamePriceCache = "priceAPIPrice24hCache" ;
35+
36+ // in use (keep for now)
3337 static const String boxNameDBInfo = "dbInfo" ;
34- // static const String boxNameTheme = "theme ";
35- static const String boxNameDesktopData = "desktopData" ;
36- static const String boxNameBuys = "buysBox " ;
38+ static const String boxNamePrefs = "prefs " ;
39+
40+ String _boxNameTxCache ({ required Coin coin}) => "${ coin . name }_txCache " ;
3741
38- String boxNameTxCache ({ required Coin coin}) => "${ coin . name }_txCache" ;
39- String boxNameSetCache ({required Coin coin}) =>
42+ // firo only
43+ String _boxNameSetCache ({required Coin coin}) =>
4044 "${coin .name }_anonymitySetCache" ;
41- String boxNameUsedSerialsCache ({required Coin coin}) =>
45+ String _boxNameUsedSerialsCache ({required Coin coin}) =>
4246 "${coin .name }_usedSerialsCache" ;
4347
44- Box <String >? _boxDebugInfo;
4548 Box <NodeModel >? _boxNodeModels;
4649 Box <NodeModel >? _boxPrimaryNodes;
4750 Box <dynamic >? _boxAllWalletsData;
4851 Box <NotificationModel >? _boxNotifications;
4952 Box <NotificationModel >? _boxWatchedTransactions;
5053 Box <NotificationModel >? _boxWatchedTrades;
51- Box <ExchangeTransaction >? _boxTrades;
5254 Box <Trade >? _boxTradesV2;
5355 Box <String >? _boxTradeNotes;
5456 Box <String >? _boxFavoriteWallets;
5557 Box <xmr.WalletInfo >? _walletInfoSource;
5658 Box <dynamic >? _boxPrefs;
5759 Box <TradeWalletLookup >? _boxTradeLookup;
5860 Box <dynamic >? _boxDBInfo;
59- Box <String >? _boxDesktopData;
61+ // Box<String>? _boxDesktopData;
6062
6163 final Map <String , Box <dynamic >> _walletBoxes = {};
6264
@@ -99,8 +101,6 @@ class DB {
99101 _boxPrefs = await Hive .openBox <dynamic >(boxNamePrefs);
100102 }
101103
102- _boxDebugInfo = await Hive .openBox <String >(boxNameDebugInfo);
103-
104104 if (Hive .isBoxOpen (boxNameNodeModels)) {
105105 _boxNodeModels = Hive .box <NodeModel >(boxNameNodeModels);
106106 } else {
@@ -119,19 +119,12 @@ class DB {
119119 _boxAllWalletsData = await Hive .openBox <dynamic >(boxNameAllWalletsData);
120120 }
121121
122- if (Hive .isBoxOpen (boxNameDesktopData)) {
123- _boxDesktopData = Hive .box <String >(boxNameDesktopData);
124- } else {
125- _boxDesktopData = await Hive .openBox <String >(boxNameDesktopData);
126- }
127-
128122 _boxNotifications =
129123 await Hive .openBox <NotificationModel >(boxNameNotifications);
130124 _boxWatchedTransactions =
131125 await Hive .openBox <NotificationModel >(boxNameWatchedTransactions);
132126 _boxWatchedTrades =
133127 await Hive .openBox <NotificationModel >(boxNameWatchedTrades);
134- _boxTrades = await Hive .openBox <ExchangeTransaction >(boxNameTrades);
135128 _boxTradesV2 = await Hive .openBox <Trade >(boxNameTradesV2);
136129 _boxTradeNotes = await Hive .openBox <String >(boxNameTradeNotes);
137130 _boxTradeLookup = await Hive .openBox <TradeWalletLookup >(boxNameTradeLookup);
@@ -142,7 +135,6 @@ class DB {
142135 await Future .wait ([
143136 Hive .openBox <dynamic >(boxNamePriceCache),
144137 _loadWalletBoxes (),
145- _loadSharedCoinCacheBoxes (),
146138 ]);
147139 }
148140
@@ -174,15 +166,40 @@ class DB {
174166 }
175167 }
176168
177- Future <void > _loadSharedCoinCacheBoxes () async {
178- for (final coin in Coin .values) {
179- _txCacheBoxes[coin] =
180- await Hive .openBox <dynamic >(boxNameTxCache (coin: coin));
181- _setCacheBoxes[coin] =
182- await Hive .openBox <dynamic >(boxNameSetCache (coin: coin));
183- _usedSerialsCacheBoxes[coin] =
184- await Hive .openBox <dynamic >(boxNameUsedSerialsCache (coin: coin));
185- }
169+ Future <Box <dynamic >> getTxCacheBox ({required Coin coin}) async {
170+ return _txCacheBoxes[coin] ?? =
171+ await Hive .openBox <dynamic >(_boxNameTxCache (coin: coin));
172+ }
173+
174+ Future <void > closeTxCacheBox ({required Coin coin}) async {
175+ await _txCacheBoxes[coin]? .close ();
176+ }
177+
178+ Future <Box <dynamic >> getAnonymitySetCacheBox ({required Coin coin}) async {
179+ return _setCacheBoxes[coin] ?? =
180+ await Hive .openBox <dynamic >(_boxNameSetCache (coin: coin));
181+ }
182+
183+ Future <void > closeAnonymitySetCacheBox ({required Coin coin}) async {
184+ await _setCacheBoxes[coin]? .close ();
185+ }
186+
187+ Future <Box <dynamic >> getUsedSerialsCacheBox ({required Coin coin}) async {
188+ return _usedSerialsCacheBoxes[coin] ?? =
189+ await Hive .openBox <dynamic >(_boxNameUsedSerialsCache (coin: coin));
190+ }
191+
192+ Future <void > closeUsedSerialsCacheBox ({required Coin coin}) async {
193+ await _usedSerialsCacheBoxes[coin]? .close ();
194+ }
195+
196+ /// Clear all cached transactions for the specified coin
197+ Future <void > clearSharedTransactionCache ({required Coin coin}) async {
198+ await deleteAll <dynamic >(boxName: _boxNameTxCache (coin: coin));
199+ // if (coin == Coin.firo) {
200+ // await deleteAll<dynamic>(boxName: _boxNameSetCache(coin: coin));
201+ // await deleteAll<dynamic>(boxName: _boxNameUsedSerialsCache(coin: coin));
202+ // }
186203 }
187204
188205 /////////////////////////////////////////
@@ -243,6 +260,36 @@ class DB {
243260
244261 Future <void > deleteBoxFromDisk ({required String boxName}) async =>
245262 await mutex.protect (() async => await Hive .deleteBoxFromDisk (boxName));
263+
264+ ///////////////////////////////////////////////////////////////////////////
265+ Future <bool > deleteEverything () async {
266+ try {
267+ await DB .instance.deleteBoxFromDisk (boxName: DB .boxNameAddressBook);
268+ await DB .instance.deleteBoxFromDisk (boxName: "debugInfoBox" );
269+ await DB .instance.deleteBoxFromDisk (boxName: DB .boxNameNodeModels);
270+ await DB .instance.deleteBoxFromDisk (boxName: DB .boxNamePrimaryNodes);
271+ await DB .instance.deleteBoxFromDisk (boxName: DB .boxNameAllWalletsData);
272+ await DB .instance.deleteBoxFromDisk (boxName: DB .boxNameNotifications);
273+ await DB .instance
274+ .deleteBoxFromDisk (boxName: DB .boxNameWatchedTransactions);
275+ await DB .instance.deleteBoxFromDisk (boxName: DB .boxNameWatchedTrades);
276+ await DB .instance.deleteBoxFromDisk (boxName: DB .boxNameTrades);
277+ await DB .instance.deleteBoxFromDisk (boxName: DB .boxNameTradesV2);
278+ await DB .instance.deleteBoxFromDisk (boxName: DB .boxNameTradeNotes);
279+ await DB .instance.deleteBoxFromDisk (boxName: DB .boxNameTradeLookup);
280+ await DB .instance.deleteBoxFromDisk (boxName: DB .boxNameFavoriteWallets);
281+ await DB .instance.deleteBoxFromDisk (boxName: DB .boxNamePrefs);
282+ await DB .instance
283+ .deleteBoxFromDisk (boxName: DB .boxNameWalletsToDeleteOnStart);
284+ await DB .instance.deleteBoxFromDisk (boxName: DB .boxNamePriceCache);
285+ await DB .instance.deleteBoxFromDisk (boxName: DB .boxNameDBInfo);
286+ await DB .instance.deleteBoxFromDisk (boxName: "theme" );
287+ return true ;
288+ } catch (e, s) {
289+ Logging .instance.log ("$e $s " , level: LogLevel .Error );
290+ return false ;
291+ }
292+ }
246293}
247294
248295abstract class DBKeys {
0 commit comments