CW-194 Fixes for import backups v1

M committed Oct 18, 2022 at 11:38 UTC a0294a16b2b72cc50f477415d5ac10cbe53aee10
1 file changed +84 -43
lib/core/backup_service.dart
+84 -43
@@ -160,7 +160,7 @@ class BackupService {
160 }
161
162 final data =
163 - json.decode(preferencesFile.readAsStringSync()) as Map<String, Object>;
163 + json.decode(preferencesFile.readAsStringSync()) as Map<String, dynamic>;
164 String currentWalletName = data[PreferencesKey.currentWalletName] as String;
165 int currentWalletType = data[PreferencesKey.currentWalletType] as int;
166
@@ -173,43 +173,87 @@ class BackupService {
173 currentWalletType = serializeToInt(_correctWallets.first.type);
174 }
175
176 + final currentNodeId = data[PreferencesKey.currentNodeIdKey] as int?;
177 + final currentBalanceDisplayMode = data[PreferencesKey.currentBalanceDisplayModeKey] as int?;
178 + final currentFiatCurrency = data[PreferencesKey.currentFiatCurrencyKey] as String?;
179 + final shouldSaveRecipientAddress = data[PreferencesKey.shouldSaveRecipientAddressKey] as bool?;
180 + final currentTransactionPriorityKeyLegacy = data[PreferencesKey.currentTransactionPriorityKeyLegacy] as int?;
181 + final allowBiometricalAuthentication = data[PreferencesKey.allowBiometricalAuthenticationKey] as bool?;
182 + final currentBitcoinElectrumSererId = data[PreferencesKey.currentBitcoinElectrumSererIdKey] as int?;
183 + final currentLanguageCode = data[PreferencesKey.currentLanguageCode] as String?;
184 + final displayActionListMode = data[PreferencesKey.displayActionListModeKey] as int?;
185 + final currentPinLength = data[PreferencesKey.currentPinLength] as int?;
186 + final currentTheme = data[PreferencesKey.currentTheme] as int?;
187 + final currentDefaultSettingsMigrationVersion = data[PreferencesKey.currentDefaultSettingsMigrationVersion] as int?;
188 + final moneroTransactionPriority = data[PreferencesKey.moneroTransactionPriority] as int?;
189 + final bitcoinTransactionPriority = data[PreferencesKey.bitcoinTransactionPriority] as int?;
190 +
191 await _sharedPreferences.setString(PreferencesKey.currentWalletName,
192 currentWalletName);
178 - await _sharedPreferences.setInt(PreferencesKey.currentNodeIdKey,
179 - data[PreferencesKey.currentNodeIdKey] as int);
180 - await _sharedPreferences.setInt(PreferencesKey.currentBalanceDisplayModeKey,
181 - data[PreferencesKey.currentBalanceDisplayModeKey] as int);
193 +
194 + if (currentNodeId != null)
195 + await _sharedPreferences.setInt(PreferencesKey.currentNodeIdKey,
196 + currentNodeId);
197 +
198 + if (currentBalanceDisplayMode != null)
199 + await _sharedPreferences.setInt(PreferencesKey.currentBalanceDisplayModeKey,
200 + currentBalanceDisplayMode);
201 +
202 await _sharedPreferences.setInt(PreferencesKey.currentWalletType,
203 currentWalletType);
184 - await _sharedPreferences.setString(PreferencesKey.currentFiatCurrencyKey,
185 - data[PreferencesKey.currentFiatCurrencyKey] as String);
186 - await _sharedPreferences.setBool(
204 +
205 + if (currentFiatCurrency != null)
206 + await _sharedPreferences.setString(PreferencesKey.currentFiatCurrencyKey,
207 + currentFiatCurrency);
208 +
209 + if (shouldSaveRecipientAddress != null)
210 + await _sharedPreferences.setBool(
211 PreferencesKey.shouldSaveRecipientAddressKey,
188 - data[PreferencesKey.shouldSaveRecipientAddressKey] as bool);
189 - await _sharedPreferences.setInt(
212 + shouldSaveRecipientAddress);
213 +
214 + if (currentTransactionPriorityKeyLegacy != null)
215 + await _sharedPreferences.setInt(
216 PreferencesKey.currentTransactionPriorityKeyLegacy,
191 - data[PreferencesKey.currentTransactionPriorityKeyLegacy] as int);
192 - await _sharedPreferences.setBool(
217 + currentTransactionPriorityKeyLegacy);
218 +
219 + if (allowBiometricalAuthentication != null)
220 + await _sharedPreferences.setBool(
221 PreferencesKey.allowBiometricalAuthenticationKey,
194 - data[PreferencesKey.allowBiometricalAuthenticationKey] as bool);
195 - await _sharedPreferences.setInt(
222 + allowBiometricalAuthentication);
223 +
224 + if (currentBitcoinElectrumSererId != null)
225 + await _sharedPreferences.setInt(
226 PreferencesKey.currentBitcoinElectrumSererIdKey,
197 - data[PreferencesKey.currentBitcoinElectrumSererIdKey] as int);
198 - await _sharedPreferences.setString(PreferencesKey.currentLanguageCode,
199 - data[PreferencesKey.currentLanguageCode] as String);
200 - await _sharedPreferences.setInt(PreferencesKey.displayActionListModeKey,
201 - data[PreferencesKey.displayActionListModeKey] as int);
202 - await _sharedPreferences.setInt(PreferencesKey.currentPinLength,
203 - data[PreferencesKey.currentPinLength] as int);
204 - await _sharedPreferences.setInt(
205 - PreferencesKey.currentTheme, data[PreferencesKey.currentTheme] as int);
206 - await _sharedPreferences.setInt(
227 + currentBitcoinElectrumSererId);
228 +
229 + if (currentLanguageCode != null)
230 + await _sharedPreferences.setString(PreferencesKey.currentLanguageCode,
231 + currentLanguageCode);
232 +
233 + if (displayActionListMode != null)
234 + await _sharedPreferences.setInt(PreferencesKey.displayActionListModeKey,
235 + displayActionListMode);
236 +
237 + if (currentPinLength != null)
238 + await _sharedPreferences.setInt(PreferencesKey.currentPinLength,
239 + currentPinLength);
240 +
241 + if (currentTheme != null)
242 + await _sharedPreferences.setInt(
243 + PreferencesKey.currentTheme, currentTheme);
244 +
245 + if (currentDefaultSettingsMigrationVersion != null)
246 + await _sharedPreferences.setInt(
247 PreferencesKey.currentDefaultSettingsMigrationVersion,
208 - data[PreferencesKey.currentDefaultSettingsMigrationVersion] as int);
209 - await _sharedPreferences.setInt(PreferencesKey.moneroTransactionPriority,
210 - data[PreferencesKey.moneroTransactionPriority] as int);
211 - await _sharedPreferences.setInt(PreferencesKey.bitcoinTransactionPriority,
212 - data[PreferencesKey.bitcoinTransactionPriority] as int);
248 + currentDefaultSettingsMigrationVersion);
249 +
250 + if (moneroTransactionPriority != null)
251 + await _sharedPreferences.setInt(PreferencesKey.moneroTransactionPriority,
252 + moneroTransactionPriority);
253 +
254 + if (bitcoinTransactionPriority != null)
255 + await _sharedPreferences.setInt(PreferencesKey.bitcoinTransactionPriority,
256 + bitcoinTransactionPriority);
257
258 await preferencesFile.delete();
259 }
@@ -283,7 +327,7 @@ class BackupService {
327
328 Future<String> _exportPreferencesJSON() async {
329 // FIX-ME: Force unwrap
286 - final preferences = <String, Object>{
330 + final preferences = <String, dynamic>{
331 PreferencesKey.currentWalletName:
332 _sharedPreferences.getString(PreferencesKey.currentWalletName)!,
333 PreferencesKey.currentNodeIdKey:
@@ -340,18 +384,15 @@ class BackupService {
384 }
385
386 Future<Uint8List> _decrypt(
343 - Uint8List data, String secretKeySource, String nonceBase64) async {
344 - throw Exception('Unimplemented');
345 - //final secretKeyHash = await sha256.hash(utf8.encode(secretKeySource));
346 - //final secretKey = SecretKey(secretKeyHash.bytes);
347 - //final nonce = Nonce(base64.decode(nonceBase64));
348 - //return await _cipher.decrypt(data, secretKey: secretKey, nonce: nonce);
349 -
350 - // final secretKeyHash = await Cryptography.instance.sha256().hash(utf8.encode(secretKeySource));
351 - // final secretKey = SecretKey(secretKeyHash.bytes);
352 - // final nonce = base64.decode(nonceBase64).toList();
353 - // final box = SecretBox(data.toList(), nonce: nonce, mac: Mac);
354 - // final plainData = await _cipher.decrypt(box, secretKey: secretKey);
355 - // return Uint8List.fromList(plainData);
387 + Uint8List data, String secretKeySource, String nonceBase64, {int macLength = 16}) async {
388 + final secretKeyHash = await Cryptography.instance.sha256().hash(utf8.encode(secretKeySource));
389 + final secretKey = SecretKey(secretKeyHash.bytes);
390 + final nonce = base64.decode(nonceBase64).toList();
391 + final box = SecretBox(
392 + Uint8List.sublistView(data, 0, data.lengthInBytes - macLength).toList(),
393 + nonce: nonce,
394 + mac: Mac(Uint8List.sublistView(data, data.lengthInBytes - macLength)));
395 + final plainData = await _cipher.decrypt(box, secretKey: secretKey);
396 + return Uint8List.fromList(plainData);
397 }
398 }