From bc4948b1774bf8cf74fa6fe2d24a301aa8256ef8 Mon Sep 17 00:00:00 2001 From: ant013 Date: Thu, 12 Oct 2023 12:55:06 +0600 Subject: [PATCH] Fix some unexpected behaviour when restore files from Android --- .../Core/Crypto/FullBackup.swift | 17 ++++++++++- .../Core/Crypto/SettingsBackup.swift | 2 +- .../Core/Storage/LocalStorage.swift | 2 +- .../Models/AccountType.swift | 12 +++----- .../Backup/ICloud/AppBackupProvider.swift | 28 +++++++++++-------- .../RestoreFileConfigurationViewModel.swift | 1 + .../RestoreType/RestoreTypeViewModel.swift | 2 +- .../BackupApp/Backup/BackupAppViewModel.swift | 4 +-- .../BackupDisclaimerView.swift | 4 +-- .../Backup/BackupName/BackupNameView.swift | 4 +-- .../en.lproj/Localizable.strings | 2 +- 11 files changed, 48 insertions(+), 30 deletions(-) diff --git a/UnstoppableWallet/UnstoppableWallet/Core/Crypto/FullBackup.swift b/UnstoppableWallet/UnstoppableWallet/Core/Crypto/FullBackup.swift index 628a5f3d2a..5e23d8b583 100644 --- a/UnstoppableWallet/UnstoppableWallet/Core/Crypto/FullBackup.swift +++ b/UnstoppableWallet/UnstoppableWallet/Core/Crypto/FullBackup.swift @@ -24,7 +24,12 @@ extension FullBackup: Codable { init(from decoder: Decoder) throws { let container = try decoder.container(keyedBy: CodingKeys.self) id = try container.decode(String.self, forKey: .id) - wallets = (try? container.decode([RestoreCloudModule.RestoredBackup].self, forKey: .wallets)) ?? [] + do { + wallets = (try container.decode([FailableDecodable].self, forKey: .wallets)) + .compactMap { $0.base } + } catch { + wallets = [] + } watchlistIds = (try? container.decode([String].self, forKey: .watchlistIds)) ?? [] contacts = try? container.decode(BackupCrypto.self, forKey: .contacts) settings = try container.decode(SettingsBackup.self, forKey: .settings) @@ -43,3 +48,13 @@ extension FullBackup: Codable { try? container.encode(timestamp, forKey: .timestamp) } } + +struct FailableDecodable : Decodable { + + let base: Base? + + init(from decoder: Decoder) throws { + let container = try decoder.singleValueContainer() + base = try? container.decode(Base.self) + } +} diff --git a/UnstoppableWallet/UnstoppableWallet/Core/Crypto/SettingsBackup.swift b/UnstoppableWallet/UnstoppableWallet/Core/Crypto/SettingsBackup.swift index 696c6502ce..67387cf761 100644 --- a/UnstoppableWallet/UnstoppableWallet/Core/Crypto/SettingsBackup.swift +++ b/UnstoppableWallet/UnstoppableWallet/Core/Crypto/SettingsBackup.swift @@ -8,7 +8,7 @@ struct SettingsBackup: Codable { let btcModes: [BtcBlockchainManager.BtcRestoreModeBackup] let lockTimeEnabled: Bool - let remoteContactsSync: Bool + let remoteContactsSync: Bool? let swapProviders: [DefaultProvider] let chartIndicators: ChartIndicatorsRepository.BackupIndicators let indicatorsShown: Bool diff --git a/UnstoppableWallet/UnstoppableWallet/Core/Storage/LocalStorage.swift b/UnstoppableWallet/UnstoppableWallet/Core/Storage/LocalStorage.swift index 3f4aa55bbe..a49356ca46 100644 --- a/UnstoppableWallet/UnstoppableWallet/Core/Storage/LocalStorage.swift +++ b/UnstoppableWallet/UnstoppableWallet/Core/Storage/LocalStorage.swift @@ -103,7 +103,7 @@ extension LocalStorage { extension LocalStorage { func restore(backup: SettingsBackup) { lockTimeEnabled = backup.lockTimeEnabled - remoteContactsSync = backup.remoteContactsSync + remoteContactsSync = backup.remoteContactsSync ?? false indicatorsShown = backup.indicatorsShown backup.swapProviders.forEach { provider in let blockchainType = BlockchainType(uid: provider.blockchainTypeId) diff --git a/UnstoppableWallet/UnstoppableWallet/Models/AccountType.swift b/UnstoppableWallet/UnstoppableWallet/Models/AccountType.swift index 457064d8ff..2ecad54c61 100644 --- a/UnstoppableWallet/UnstoppableWallet/Models/AccountType.swift +++ b/UnstoppableWallet/UnstoppableWallet/Models/AccountType.swift @@ -41,9 +41,9 @@ enum AccountType { case let .evmPrivateKey(data): privateData = data case let .evmAddress(address): - privateData = address.raw + privateData = address.hex.hs.data case let .tronAddress(address): - privateData = address.raw + privateData = address.hex.hs.data case let .hdExtendedKey(key): privateData = key.serialized case let .cex(cexAccount): @@ -259,13 +259,9 @@ extension AccountType { return nil } case .evmAddress: - return AccountType.evmAddress(address: EvmKit.Address(raw: uniqueId)) + return (try? EvmKit.Address(hex: string)).map { AccountType.evmAddress(address: $0) } case .tronAddress: - do { - return try AccountType.tronAddress(address: TronKit.Address(raw: uniqueId)) - } catch { - return nil - } + return (try? TronKit.Address(address: string)).map { AccountType.tronAddress(address: $0) } case .cex: guard let cexAccount = CexAccount.decode(uniqueId: string) else { return nil diff --git a/UnstoppableWallet/UnstoppableWallet/Modules/Backup/ICloud/AppBackupProvider.swift b/UnstoppableWallet/UnstoppableWallet/Modules/Backup/ICloud/AppBackupProvider.swift index 63a8bf4f64..6d732c9bab 100644 --- a/UnstoppableWallet/UnstoppableWallet/Modules/Backup/ICloud/AppBackupProvider.swift +++ b/UnstoppableWallet/UnstoppableWallet/Modules/Backup/ICloud/AppBackupProvider.swift @@ -150,28 +150,32 @@ extension AppBackupProvider { accountManager.save(accounts: updated.map { $0.account }) - updated.forEach { raw in + updated.forEach { (raw: RawWalletBackup) in switch raw.account.type { case .cex: () default: - let wallets = raw.enabledWallets.map { - if !$0.settings.isEmpty { + let wallets = raw.enabledWallets.compactMap { (wallet: WalletBackup.EnabledWallet) -> EnabledWallet? in + guard let tokenQuery = TokenQuery(id: wallet.tokenQueryId), + BlockchainType.supported.contains(tokenQuery.blockchainType) else { + return nil + } + + if !wallet.settings.isEmpty { var restoreSettings = [RestoreSettingType: String]() - $0.settings.forEach { key, value in + wallet.settings.forEach { key, value in if let key = RestoreSettingType(rawValue: key) { restoreSettings[key] = value } } - if let tokenQuery = TokenQuery(id: $0.tokenQueryId) { - restoreSettingsManager.save(settings: restoreSettings, account: raw.account, blockchainType: tokenQuery.blockchainType) - } + restoreSettingsManager.save(settings: restoreSettings, account: raw.account, blockchainType: tokenQuery.blockchainType) } + return EnabledWallet( - tokenQueryId: $0.tokenQueryId, + tokenQueryId: wallet.tokenQueryId, accountId: raw.account.id, - coinName: $0.coinName, - coinCode: $0.coinCode, - tokenDecimals: $0.tokenDecimals + coinName: wallet.coinName, + coinCode: wallet.coinCode, + tokenDecimals: wallet.tokenDecimals ) } walletManager.save(enabledWallets: wallets) @@ -197,9 +201,11 @@ extension AppBackupProvider { if let currency = currencyKit.currencies.first(where: { $0.code == raw.settings.baseCurrency }) { currencyKit.baseCurrency = currency } + themeManager.themeMode = raw.settings.mode launchScreenManager.showMarket = raw.settings.showMarketTab launchScreenManager.launchScreen = raw.settings.launchScreen + balancePrimaryValueManager.balancePrimaryValue = raw.settings.balancePrimaryValue balanceConversionManager.set(tokenQueryId: raw.settings.conversionTokenQueryId) balanceHiddenManager.set(balanceAutoHide: raw.settings.balanceAutoHide) diff --git a/UnstoppableWallet/UnstoppableWallet/Modules/RestoreAccount/RestoreFile/RestoreFileConfiguration/RestoreFileConfigurationViewModel.swift b/UnstoppableWallet/UnstoppableWallet/Modules/RestoreAccount/RestoreFile/RestoreFileConfiguration/RestoreFileConfigurationViewModel.swift index abe5a7b13f..10a56c7e9a 100644 --- a/UnstoppableWallet/UnstoppableWallet/Modules/RestoreAccount/RestoreFile/RestoreFileConfiguration/RestoreFileConfigurationViewModel.swift +++ b/UnstoppableWallet/UnstoppableWallet/Modules/RestoreAccount/RestoreFile/RestoreFileConfiguration/RestoreFileConfigurationViewModel.swift @@ -42,6 +42,7 @@ extension RestoreFileConfigurationViewModel { rawBackup .accounts .filter { !$0.account.watchAccount } + .sorted { wallet, wallet2 in wallet.account.name.lowercased() < wallet2.account.name.lowercased() } .map { item(account: $0.account) } } diff --git a/UnstoppableWallet/UnstoppableWallet/Modules/RestoreAccount/RestoreType/RestoreTypeViewModel.swift b/UnstoppableWallet/UnstoppableWallet/Modules/RestoreAccount/RestoreType/RestoreTypeViewModel.swift index 534b321441..1df2f45337 100644 --- a/UnstoppableWallet/UnstoppableWallet/Modules/RestoreAccount/RestoreType/RestoreTypeViewModel.swift +++ b/UnstoppableWallet/UnstoppableWallet/Modules/RestoreAccount/RestoreType/RestoreTypeViewModel.swift @@ -58,7 +58,7 @@ extension RestoreTypeViewModel { extension RestoreTypeViewModel { var items: [RestoreTypeModule.RestoreType] { switch sourceType { - case .wallet: return [.recoveryOrPrivateKey, .cloudRestore, .fileRestore] + case .wallet: return [.recoveryOrPrivateKey, .cloudRestore, .fileRestore, .cex] case .full: return [.cloudRestore, .fileRestore] } } diff --git a/UnstoppableWallet/UnstoppableWallet/Modules/Settings/BackupApp/Backup/BackupAppViewModel.swift b/UnstoppableWallet/UnstoppableWallet/Modules/Settings/BackupApp/Backup/BackupAppViewModel.swift index 609f85b564..05656a3542 100644 --- a/UnstoppableWallet/UnstoppableWallet/Modules/Settings/BackupApp/Backup/BackupAppViewModel.swift +++ b/UnstoppableWallet/UnstoppableWallet/Modules/Settings/BackupApp/Backup/BackupAppViewModel.swift @@ -119,6 +119,7 @@ extension BackupAppViewModel { accountManager .accounts .filter { $0.watchAccount == watch } + .sorted { account, account2 in account.name.lowercased() < account2.name.lowercased() } } private var accountIds: [String] { @@ -201,7 +202,6 @@ extension BackupAppViewModel { } func validatePasswords() { - var buttonDisabled = false clearCautions() do { @@ -245,7 +245,7 @@ extension BackupAppViewModel { } passwordButtonProcessing = true - let selectedIds = accountIds.filter { (selected[$0] ?? false) } + let selectedIds = accountIds.filter { (selected[$0] ?? false) } + accounts(watch: true).map { $0.id } Task { switch destination { case .none: () diff --git a/UnstoppableWallet/UnstoppableWallet/Modules/Settings/BackupApp/Backup/BackupDisclaimer/BackupDisclaimerView.swift b/UnstoppableWallet/UnstoppableWallet/Modules/Settings/BackupApp/Backup/BackupDisclaimer/BackupDisclaimerView.swift index 29e4c4e26b..2193670173 100644 --- a/UnstoppableWallet/UnstoppableWallet/Modules/Settings/BackupApp/Backup/BackupDisclaimer/BackupDisclaimerView.swift +++ b/UnstoppableWallet/UnstoppableWallet/Modules/Settings/BackupApp/Backup/BackupDisclaimer/BackupDisclaimerView.swift @@ -36,9 +36,9 @@ struct BackupDisclaimerView: View { Button(action: { viewModel.namePushed = true }) { Text("button.next".localized) } - .buttonStyle(PrimaryButtonStyle(style: .yellow)) - .disabled(!isOn) } + .buttonStyle(PrimaryButtonStyle(style: .yellow)) + .disabled(!isOn) } } .navigationBarTitle(backupDisclaimer.title) diff --git a/UnstoppableWallet/UnstoppableWallet/Modules/Settings/BackupApp/Backup/BackupName/BackupNameView.swift b/UnstoppableWallet/UnstoppableWallet/Modules/Settings/BackupApp/Backup/BackupName/BackupNameView.swift index dfeb9b3835..e46de5d6f6 100644 --- a/UnstoppableWallet/UnstoppableWallet/Modules/Settings/BackupApp/Backup/BackupName/BackupNameView.swift +++ b/UnstoppableWallet/UnstoppableWallet/Modules/Settings/BackupApp/Backup/BackupName/BackupNameView.swift @@ -36,9 +36,9 @@ struct BackupNameView: View { }) { Text("button.next".localized) } - .buttonStyle(PrimaryButtonStyle(style: .yellow)) - .disabled(viewModel.nameCautionState != .none) } + .buttonStyle(PrimaryButtonStyle(style: .yellow)) + .disabled(viewModel.nameCautionState != .none) } .navigationTitle("backup_app.backup.name.title".localized) .navigationBarTitleDisplayMode(.inline) diff --git a/UnstoppableWallet/UnstoppableWallet/en.lproj/Localizable.strings b/UnstoppableWallet/UnstoppableWallet/en.lproj/Localizable.strings index 3bb301c758..b9242c198c 100644 --- a/UnstoppableWallet/UnstoppableWallet/en.lproj/Localizable.strings +++ b/UnstoppableWallet/UnstoppableWallet/en.lproj/Localizable.strings @@ -1109,7 +1109,7 @@ Go to Settings - > %@ and allow access to the camera."; "backup_app.backup_list.other.watch_account.title" = "Watch Wallets"; "backup_app.backup_list.other.watchlist.title" = "Watchlist"; "backup_app.backup_list.other.contacts.title" = "Contacts"; -"backup_app.backup_list.other.blockchain_settings.title" = "Blockchain Settings"; +"backup_app.backup_list.other.blockchain_settings.title" = "Custom RPC"; "backup_app.backup_list.other.app_settings.title" = "App Settings"; "backup_app.backup_list.other.app_settings.description" = "Language, Currency, Appearance ...";