diff --git a/lib/core/backup_service.dart b/lib/core/backup_service.dart index 850e9560d..9b2b2c7d4 100644 --- a/lib/core/backup_service.dart +++ b/lib/core/backup_service.dart @@ -160,7 +160,7 @@ class BackupService { } final data = - json.decode(preferencesFile.readAsStringSync()) as Map; + json.decode(preferencesFile.readAsStringSync()) as Map; String currentWalletName = data[PreferencesKey.currentWalletName] as String; int currentWalletType = data[PreferencesKey.currentWalletType] as int; @@ -173,43 +173,87 @@ class BackupService { currentWalletType = serializeToInt(_correctWallets.first.type); } + final currentNodeId = data[PreferencesKey.currentNodeIdKey] as int?; + final currentBalanceDisplayMode = data[PreferencesKey.currentBalanceDisplayModeKey] as int?; + final currentFiatCurrency = data[PreferencesKey.currentFiatCurrencyKey] as String?; + final shouldSaveRecipientAddress = data[PreferencesKey.shouldSaveRecipientAddressKey] as bool?; + final currentTransactionPriorityKeyLegacy = data[PreferencesKey.currentTransactionPriorityKeyLegacy] as int?; + final allowBiometricalAuthentication = data[PreferencesKey.allowBiometricalAuthenticationKey] as bool?; + final currentBitcoinElectrumSererId = data[PreferencesKey.currentBitcoinElectrumSererIdKey] as int?; + final currentLanguageCode = data[PreferencesKey.currentLanguageCode] as String?; + final displayActionListMode = data[PreferencesKey.displayActionListModeKey] as int?; + final currentPinLength = data[PreferencesKey.currentPinLength] as int?; + final currentTheme = data[PreferencesKey.currentTheme] as int?; + final currentDefaultSettingsMigrationVersion = data[PreferencesKey.currentDefaultSettingsMigrationVersion] as int?; + final moneroTransactionPriority = data[PreferencesKey.moneroTransactionPriority] as int?; + final bitcoinTransactionPriority = data[PreferencesKey.bitcoinTransactionPriority] as int?; + await _sharedPreferences.setString(PreferencesKey.currentWalletName, currentWalletName); - await _sharedPreferences.setInt(PreferencesKey.currentNodeIdKey, - data[PreferencesKey.currentNodeIdKey] as int); - await _sharedPreferences.setInt(PreferencesKey.currentBalanceDisplayModeKey, - data[PreferencesKey.currentBalanceDisplayModeKey] as int); + + if (currentNodeId != null) + await _sharedPreferences.setInt(PreferencesKey.currentNodeIdKey, + currentNodeId); + + if (currentBalanceDisplayMode != null) + await _sharedPreferences.setInt(PreferencesKey.currentBalanceDisplayModeKey, + currentBalanceDisplayMode); + await _sharedPreferences.setInt(PreferencesKey.currentWalletType, currentWalletType); - await _sharedPreferences.setString(PreferencesKey.currentFiatCurrencyKey, - data[PreferencesKey.currentFiatCurrencyKey] as String); - await _sharedPreferences.setBool( + + if (currentFiatCurrency != null) + await _sharedPreferences.setString(PreferencesKey.currentFiatCurrencyKey, + currentFiatCurrency); + + if (shouldSaveRecipientAddress != null) + await _sharedPreferences.setBool( PreferencesKey.shouldSaveRecipientAddressKey, - data[PreferencesKey.shouldSaveRecipientAddressKey] as bool); - await _sharedPreferences.setInt( + shouldSaveRecipientAddress); + + if (currentTransactionPriorityKeyLegacy != null) + await _sharedPreferences.setInt( PreferencesKey.currentTransactionPriorityKeyLegacy, - data[PreferencesKey.currentTransactionPriorityKeyLegacy] as int); - await _sharedPreferences.setBool( + currentTransactionPriorityKeyLegacy); + + if (allowBiometricalAuthentication != null) + await _sharedPreferences.setBool( PreferencesKey.allowBiometricalAuthenticationKey, - data[PreferencesKey.allowBiometricalAuthenticationKey] as bool); - await _sharedPreferences.setInt( + allowBiometricalAuthentication); + + if (currentBitcoinElectrumSererId != null) + await _sharedPreferences.setInt( PreferencesKey.currentBitcoinElectrumSererIdKey, - data[PreferencesKey.currentBitcoinElectrumSererIdKey] as int); - await _sharedPreferences.setString(PreferencesKey.currentLanguageCode, - data[PreferencesKey.currentLanguageCode] as String); - await _sharedPreferences.setInt(PreferencesKey.displayActionListModeKey, - data[PreferencesKey.displayActionListModeKey] as int); - await _sharedPreferences.setInt(PreferencesKey.currentPinLength, - data[PreferencesKey.currentPinLength] as int); - await _sharedPreferences.setInt( - PreferencesKey.currentTheme, data[PreferencesKey.currentTheme] as int); - await _sharedPreferences.setInt( + currentBitcoinElectrumSererId); + + if (currentLanguageCode != null) + await _sharedPreferences.setString(PreferencesKey.currentLanguageCode, + currentLanguageCode); + + if (displayActionListMode != null) + await _sharedPreferences.setInt(PreferencesKey.displayActionListModeKey, + displayActionListMode); + + if (currentPinLength != null) + await _sharedPreferences.setInt(PreferencesKey.currentPinLength, + currentPinLength); + + if (currentTheme != null) + await _sharedPreferences.setInt( + PreferencesKey.currentTheme, currentTheme); + + if (currentDefaultSettingsMigrationVersion != null) + await _sharedPreferences.setInt( PreferencesKey.currentDefaultSettingsMigrationVersion, - data[PreferencesKey.currentDefaultSettingsMigrationVersion] as int); - await _sharedPreferences.setInt(PreferencesKey.moneroTransactionPriority, - data[PreferencesKey.moneroTransactionPriority] as int); - await _sharedPreferences.setInt(PreferencesKey.bitcoinTransactionPriority, - data[PreferencesKey.bitcoinTransactionPriority] as int); + currentDefaultSettingsMigrationVersion); + + if (moneroTransactionPriority != null) + await _sharedPreferences.setInt(PreferencesKey.moneroTransactionPriority, + moneroTransactionPriority); + + if (bitcoinTransactionPriority != null) + await _sharedPreferences.setInt(PreferencesKey.bitcoinTransactionPriority, + bitcoinTransactionPriority); await preferencesFile.delete(); } @@ -283,7 +327,7 @@ class BackupService { Future _exportPreferencesJSON() async { // FIX-ME: Force unwrap - final preferences = { + final preferences = { PreferencesKey.currentWalletName: _sharedPreferences.getString(PreferencesKey.currentWalletName)!, PreferencesKey.currentNodeIdKey: @@ -340,18 +384,15 @@ class BackupService { } Future _decrypt( - Uint8List data, String secretKeySource, String nonceBase64) async { - throw Exception('Unimplemented'); - //final secretKeyHash = await sha256.hash(utf8.encode(secretKeySource)); - //final secretKey = SecretKey(secretKeyHash.bytes); - //final nonce = Nonce(base64.decode(nonceBase64)); - //return await _cipher.decrypt(data, secretKey: secretKey, nonce: nonce); - - // final secretKeyHash = await Cryptography.instance.sha256().hash(utf8.encode(secretKeySource)); - // final secretKey = SecretKey(secretKeyHash.bytes); - // final nonce = base64.decode(nonceBase64).toList(); - // final box = SecretBox(data.toList(), nonce: nonce, mac: Mac); - // final plainData = await _cipher.decrypt(box, secretKey: secretKey); - // return Uint8List.fromList(plainData); + Uint8List data, String secretKeySource, String nonceBase64, {int macLength = 16}) async { + final secretKeyHash = await Cryptography.instance.sha256().hash(utf8.encode(secretKeySource)); + final secretKey = SecretKey(secretKeyHash.bytes); + final nonce = base64.decode(nonceBase64).toList(); + final box = SecretBox( + Uint8List.sublistView(data, 0, data.lengthInBytes - macLength).toList(), + nonce: nonce, + mac: Mac(Uint8List.sublistView(data, data.lengthInBytes - macLength))); + final plainData = await _cipher.decrypt(box, secretKey: secretKey); + return Uint8List.fromList(plainData); } }