1111import 'dart:isolate' ;
1212
1313import 'package:cw_core/wallet_info.dart' as xmr;
14- import 'package:hive/hive.dart' ;
14+ import 'package:hive/hive.dart' show Box;
15+ import 'package:hive/src/hive_impl.dart' ;
1516import 'package:mutex/mutex.dart' ;
1617
1718import '../../app_config.dart' ;
@@ -24,6 +25,8 @@ import '../../utilities/logger.dart';
2425import '../../wallets/crypto_currency/crypto_currency.dart' ;
2526
2627class DB {
28+ final hive = HiveImpl ();
29+
2730 // legacy (required for migrations)
2831 @Deprecated ("Left over for migration from old versions of Stack Wallet" )
2932 static const String boxNameAddressBook = "addressBook" ;
@@ -104,52 +107,52 @@ class DB {
104107
105108 // open hive boxes
106109 Future <void > init () async {
107- if (Hive .isBoxOpen (boxNameDBInfo)) {
108- _boxDBInfo = Hive .box <dynamic >(boxNameDBInfo);
110+ if (hive .isBoxOpen (boxNameDBInfo)) {
111+ _boxDBInfo = hive .box <dynamic >(boxNameDBInfo);
109112 } else {
110- _boxDBInfo = await Hive .openBox <dynamic >(boxNameDBInfo);
113+ _boxDBInfo = await hive .openBox <dynamic >(boxNameDBInfo);
111114 }
112- await Hive .openBox <String >(boxNameWalletsToDeleteOnStart);
115+ await hive .openBox <String >(boxNameWalletsToDeleteOnStart);
113116
114- if (Hive .isBoxOpen (boxNamePrefs)) {
115- _boxPrefs = Hive .box <dynamic >(boxNamePrefs);
117+ if (hive .isBoxOpen (boxNamePrefs)) {
118+ _boxPrefs = hive .box <dynamic >(boxNamePrefs);
116119 } else {
117- _boxPrefs = await Hive .openBox <dynamic >(boxNamePrefs);
120+ _boxPrefs = await hive .openBox <dynamic >(boxNamePrefs);
118121 }
119122
120- if (Hive .isBoxOpen (boxNameNodeModels)) {
121- _boxNodeModels = Hive .box <NodeModel >(boxNameNodeModels);
123+ if (hive .isBoxOpen (boxNameNodeModels)) {
124+ _boxNodeModels = hive .box <NodeModel >(boxNameNodeModels);
122125 } else {
123- _boxNodeModels = await Hive .openBox <NodeModel >(boxNameNodeModels);
126+ _boxNodeModels = await hive .openBox <NodeModel >(boxNameNodeModels);
124127 }
125128
126- if (Hive .isBoxOpen (boxNamePrimaryNodes)) {
127- _boxPrimaryNodes = Hive .box <NodeModel >(boxNamePrimaryNodes);
129+ if (hive .isBoxOpen (boxNamePrimaryNodes)) {
130+ _boxPrimaryNodes = hive .box <NodeModel >(boxNamePrimaryNodes);
128131 } else {
129- _boxPrimaryNodes = await Hive .openBox <NodeModel >(boxNamePrimaryNodes);
132+ _boxPrimaryNodes = await hive .openBox <NodeModel >(boxNamePrimaryNodes);
130133 }
131134
132- if (Hive .isBoxOpen (boxNameAllWalletsData)) {
133- _boxAllWalletsData = Hive .box <dynamic >(boxNameAllWalletsData);
135+ if (hive .isBoxOpen (boxNameAllWalletsData)) {
136+ _boxAllWalletsData = hive .box <dynamic >(boxNameAllWalletsData);
134137 } else {
135- _boxAllWalletsData = await Hive .openBox <dynamic >(boxNameAllWalletsData);
138+ _boxAllWalletsData = await hive .openBox <dynamic >(boxNameAllWalletsData);
136139 }
137140
138141 _boxNotifications =
139- await Hive .openBox <NotificationModel >(boxNameNotifications);
142+ await hive .openBox <NotificationModel >(boxNameNotifications);
140143 _boxWatchedTransactions =
141- await Hive .openBox <NotificationModel >(boxNameWatchedTransactions);
144+ await hive .openBox <NotificationModel >(boxNameWatchedTransactions);
142145 _boxWatchedTrades =
143- await Hive .openBox <NotificationModel >(boxNameWatchedTrades);
144- _boxTradesV2 = await Hive .openBox <Trade >(boxNameTradesV2);
145- _boxTradeNotes = await Hive .openBox <String >(boxNameTradeNotes);
146- _boxTradeLookup = await Hive .openBox <TradeWalletLookup >(boxNameTradeLookup);
146+ await hive .openBox <NotificationModel >(boxNameWatchedTrades);
147+ _boxTradesV2 = await hive .openBox <Trade >(boxNameTradesV2);
148+ _boxTradeNotes = await hive .openBox <String >(boxNameTradeNotes);
149+ _boxTradeLookup = await hive .openBox <TradeWalletLookup >(boxNameTradeLookup);
147150 _walletInfoSource =
148- await Hive .openBox< xmr.WalletInfo > (xmr.WalletInfo .boxName);
149- _boxFavoriteWallets = await Hive .openBox <String >(boxNameFavoriteWallets);
151+ await hive .openBox< xmr.WalletInfo > (xmr.WalletInfo .boxName);
152+ _boxFavoriteWallets = await hive .openBox <String >(boxNameFavoriteWallets);
150153
151154 await Future .wait ([
152- Hive .openBox <dynamic >(boxNamePriceCache),
155+ hive .openBox <dynamic >(boxNamePriceCache),
153156 _loadWalletBoxes (),
154157 ]);
155158 }
@@ -177,12 +180,12 @@ class DB {
177180 );
178181
179182 for (final entry in mapped.entries) {
180- if (Hive .isBoxOpen (entry.value.walletId)) {
183+ if (hive .isBoxOpen (entry.value.walletId)) {
181184 _walletBoxes[entry.value.walletId] =
182- Hive .box <dynamic >(entry.value.walletId);
185+ hive .box <dynamic >(entry.value.walletId);
183186 } else {
184187 _walletBoxes[entry.value.walletId] =
185- await Hive .openBox <dynamic >(entry.value.walletId);
188+ await hive .openBox <dynamic >(entry.value.walletId);
186189 }
187190 }
188191 }
@@ -192,7 +195,7 @@ class DB {
192195 _txCacheBoxes.remove (currency.identifier);
193196 }
194197 return _txCacheBoxes[currency.identifier] ?? =
195- await Hive .openBox <dynamic >(_boxNameTxCache (currency: currency));
198+ await hive .openBox <dynamic >(_boxNameTxCache (currency: currency));
196199 }
197200
198201 Future <void > closeTxCacheBox ({required CryptoCurrency currency}) async {
@@ -206,7 +209,7 @@ class DB {
206209 _setCacheBoxes.remove (currency.identifier);
207210 }
208211 return _setCacheBoxes[currency.identifier] ?? =
209- await Hive .openBox <dynamic >(_boxNameSetCache (currency: currency));
212+ await hive .openBox <dynamic >(_boxNameSetCache (currency: currency));
210213 }
211214
212215 Future <void > closeAnonymitySetCacheBox ({
@@ -222,7 +225,7 @@ class DB {
222225 _usedSerialsCacheBoxes.remove (currency.identifier);
223226 }
224227 return _usedSerialsCacheBoxes[currency.identifier] ?? =
225- await Hive .openBox <dynamic >(
228+ await hive .openBox <dynamic >(
226229 _boxNameUsedSerialsCache (currency: currency),
227230 );
228231 }
@@ -252,7 +255,7 @@ class DB {
252255 if (_walletBoxes[walletId] != null ) {
253256 throw Exception ("Attempted overwrite of existing wallet box!" );
254257 }
255- _walletBoxes[walletId] = await Hive .openBox <dynamic >(walletId);
258+ _walletBoxes[walletId] = await hive .openBox <dynamic >(walletId);
256259 }
257260
258261 Future <void > removeWalletBox ({required String walletId}) async {
@@ -264,19 +267,19 @@ class DB {
264267 // reads
265268
266269 List <dynamic > keys <T >({required String boxName}) =>
267- Hive .box <T >(boxName).keys.toList (growable: false );
270+ hive .box <T >(boxName).keys.toList (growable: false );
268271
269272 List <T > values <T >({required String boxName}) =>
270- Hive .box <T >(boxName).values.toList (growable: false );
273+ hive .box <T >(boxName).values.toList (growable: false );
271274
272275 T ? get <T >({
273276 required String boxName,
274277 required dynamic key,
275278 }) =>
276- Hive .box <T >(boxName).get (key);
279+ hive .box <T >(boxName).get (key);
277280
278281 bool containsKey <T >({required String boxName, required dynamic key}) =>
279- Hive .box <T >(boxName).containsKey (key);
282+ hive .box <T >(boxName).containsKey (key);
280283
281284 // writes
282285
@@ -286,33 +289,33 @@ class DB {
286289 required T value,
287290 }) async =>
288291 await mutex
289- .protect (() async => await Hive .box <T >(boxName).put (key, value));
292+ .protect (() async => await hive .box <T >(boxName).put (key, value));
290293
291294 Future <void > add <T >({required String boxName, required T value}) async =>
292- await mutex.protect (() async => await Hive .box <T >(boxName).add (value));
295+ await mutex.protect (() async => await hive .box <T >(boxName).add (value));
293296
294297 Future <void > addAll <T >({
295298 required String boxName,
296299 required Iterable <T > values,
297300 }) async =>
298301 await mutex
299- .protect (() async => await Hive .box <T >(boxName).addAll (values));
302+ .protect (() async => await hive .box <T >(boxName).addAll (values));
300303
301304 Future <void > delete <T >({
302305 required dynamic key,
303306 required String boxName,
304307 }) async =>
305- await mutex.protect (() async => await Hive .box <T >(boxName).delete (key));
308+ await mutex.protect (() async => await hive .box <T >(boxName).delete (key));
306309
307310 Future <void > deleteAll <T >({required String boxName}) async {
308311 await mutex.protect (() async {
309- final box = await Hive .openBox <T >(boxName);
312+ final box = await hive .openBox <T >(boxName);
310313 await box.clear ();
311314 });
312315 }
313316
314317 Future <void > deleteBoxFromDisk ({required String boxName}) async =>
315- await mutex.protect (() async => await Hive .deleteBoxFromDisk (boxName));
318+ await mutex.protect (() async => await hive .deleteBoxFromDisk (boxName));
316319
317320 ///////////////////////////////////////////////////////////////////////////
318321 Future <bool > deleteEverything () async {
0 commit comments