11import 'package:hive/hive.dart' ;
22import 'package:stack_wallet_backup/secure_storage.dart' ;
3- import 'package:stackduo/db/hive/db.dart' ;
43import 'package:stackduo/utilities/logger.dart' ;
54
5+ const String kBoxNameDesktopData = "desktopData" ;
66const String _kKeyBlobKey = "swbKeyBlobKeyStringID" ;
77const String _kKeyBlobVersionKey = "swbKeyBlobVersionKeyStringID" ;
88
@@ -52,14 +52,8 @@ class DPS {
5252 kLatestBlobVersion,
5353 );
5454
55- final box = await Hive .openBox <String >(DB .boxNameDesktopData);
56- await DB .instance.put <String >(
57- boxName: DB .boxNameDesktopData,
58- key: _kKeyBlobKey,
59- value: await _handler! .getKeyBlob (),
60- );
55+ await _put (key: _kKeyBlobKey, value: await _handler! .getKeyBlob ());
6156 await _updateStoredKeyBlobVersion (kLatestBlobVersion);
62- await box.close ();
6357 } catch (e, s) {
6458 Logging .instance.log (
6559 "${_getMessageFromException (e )}\n $s " ,
@@ -75,19 +69,13 @@ class DPS {
7569 "DPS: attempted to re initialize with existing passphrase" );
7670 }
7771
78- final box = await Hive .openBox <String >(DB .boxNameDesktopData);
79- final keyBlob = DB .instance.get <String >(
80- boxName: DB .boxNameDesktopData,
81- key: _kKeyBlobKey,
82- );
83- await box.close ();
84-
85- if (keyBlob == null ) {
86- throw Exception (
87- "DPS: failed to find keyBlob while attempting to initialize with existing passphrase" );
88- }
89-
9072 try {
73+ final keyBlob = await _get (key: _kKeyBlobKey);
74+
75+ if (keyBlob == null ) {
76+ throw Exception (
77+ "DPS: failed to find keyBlob while attempting to initialize with existing passphrase" );
78+ }
9179 final blobVersion = await _getStoredKeyBlobVersion ();
9280 _handler = await StorageCryptoHandler .fromExisting (
9381 passphrase,
@@ -97,14 +85,8 @@ class DPS {
9785 if (blobVersion < kLatestBlobVersion) {
9886 // update blob
9987 await _handler! .resetPassphrase (passphrase, kLatestBlobVersion);
100- final box = await Hive .openBox <String >(DB .boxNameDesktopData);
101- await DB .instance.put <String >(
102- boxName: DB .boxNameDesktopData,
103- key: _kKeyBlobKey,
104- value: await _handler! .getKeyBlob (),
105- );
88+ await _put (key: _kKeyBlobKey, value: await _handler! .getKeyBlob ());
10689 await _updateStoredKeyBlobVersion (kLatestBlobVersion);
107- await box.close ();
10890 }
10991 } catch (e, s) {
11092 Logging .instance.log (
@@ -116,19 +98,13 @@ class DPS {
11698 }
11799
118100 Future <bool > verifyPassphrase (String passphrase) async {
119- final box = await Hive .openBox <String >(DB .boxNameDesktopData);
120- final keyBlob = DB .instance.get <String >(
121- boxName: DB .boxNameDesktopData,
122- key: _kKeyBlobKey,
123- );
124- await box.close ();
125-
126- if (keyBlob == null ) {
127- // no passphrase key blob found so any passphrase is technically bad
128- return false ;
129- }
130-
131101 try {
102+ final keyBlob = await _get (key: _kKeyBlobKey);
103+
104+ if (keyBlob == null ) {
105+ // no passphrase key blob found so any passphrase is technically bad
106+ return false ;
107+ }
132108 final blobVersion = await _getStoredKeyBlobVersion ();
133109 await StorageCryptoHandler .fromExisting (passphrase, keyBlob, blobVersion);
134110 // existing passphrase matches key blob
@@ -147,35 +123,25 @@ class DPS {
147123 String passphraseOld,
148124 String passphraseNew,
149125 ) async {
150- final box = await Hive .openBox <String >(DB .boxNameDesktopData);
151- final keyBlob = DB .instance.get <String >(
152- boxName: DB .boxNameDesktopData,
153- key: _kKeyBlobKey,
154- );
155- await box.close ();
156-
157- if (keyBlob == null ) {
158- // no passphrase key blob found so any passphrase is technically bad
159- return false ;
160- }
126+ try {
127+ final keyBlob = await _get (key: _kKeyBlobKey);
161128
162- if (! (await verifyPassphrase (passphraseOld))) {
163- return false ;
164- }
129+ if (keyBlob == null ) {
130+ // no passphrase key blob found so any passphrase is technically bad
131+ return false ;
132+ }
165133
166- final blobVersion = await _getStoredKeyBlobVersion ();
134+ if (! (await verifyPassphrase (passphraseOld))) {
135+ return false ;
136+ }
167137
168- try {
138+ final blobVersion = await _getStoredKeyBlobVersion ();
169139 await _handler! .resetPassphrase (passphraseNew, blobVersion);
170-
171- final box = await Hive .openBox <String >(DB .boxNameDesktopData);
172- await DB .instance.put <String >(
173- boxName: DB .boxNameDesktopData,
140+ await _put (
174141 key: _kKeyBlobKey,
175142 value: await _handler! .getKeyBlob (),
176143 );
177144 await _updateStoredKeyBlobVersion (blobVersion);
178- await box.close ();
179145
180146 // successfully updated passphrase
181147 return true ;
@@ -189,28 +155,48 @@ class DPS {
189155 }
190156
191157 Future <bool > hasPassword () async {
192- final keyBlob = DB .instance.get <String >(
193- boxName: DB .boxNameDesktopData,
194- key: _kKeyBlobKey,
195- );
158+ final keyBlob = await _get (key: _kKeyBlobKey);
196159 return keyBlob != null ;
197160 }
198161
199162 Future <int > _getStoredKeyBlobVersion () async {
200- final box = await Hive .openBox <String >(DB .boxNameDesktopData);
201- final keyBlobVersionString = DB .instance.get <String >(
202- boxName: DB .boxNameDesktopData,
203- key: _kKeyBlobVersionKey,
204- );
205- await box.close ();
163+ final keyBlobVersionString = await _get (key: _kKeyBlobVersionKey);
206164 return int .tryParse (keyBlobVersionString ?? "1" ) ?? 1 ;
207165 }
208166
209167 Future <void > _updateStoredKeyBlobVersion (int version) async {
210- await DB .instance.put <String >(
211- boxName: DB .boxNameDesktopData,
212- key: _kKeyBlobVersionKey,
213- value: version.toString (),
214- );
168+ await _put (key: _kKeyBlobVersionKey, value: version.toString ());
169+ }
170+
171+ Future <void > _put ({required String key, required String value}) async {
172+ Box <String >? box;
173+ try {
174+ box = await Hive .openBox <String >(kBoxNameDesktopData);
175+ await box.put (key, value);
176+ } catch (e, s) {
177+ Logging .instance.log (
178+ "DPS failed put($key ): $e \n $s " ,
179+ level: LogLevel .Fatal ,
180+ );
181+ } finally {
182+ await box? .close ();
183+ }
184+ }
185+
186+ Future <String ?> _get ({required String key}) async {
187+ String ? value;
188+ Box <String >? box;
189+ try {
190+ box = await Hive .openBox <String >(kBoxNameDesktopData);
191+ value = box.get (key);
192+ } catch (e, s) {
193+ Logging .instance.log (
194+ "DPS failed get($key ): $e \n $s " ,
195+ level: LogLevel .Fatal ,
196+ );
197+ } finally {
198+ await box? .close ();
199+ }
200+ return value;
215201 }
216202}
0 commit comments