Skip to content

Commit

Permalink
Fix some unexpected behaviour when restore files from Android
Browse files Browse the repository at this point in the history
  • Loading branch information
ant013 committed Oct 12, 2023
1 parent 607aa1f commit bc4948b
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 30 deletions.
17 changes: 16 additions & 1 deletion UnstoppableWallet/UnstoppableWallet/Core/Crypto/FullBackup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<RestoreCloudModule.RestoredBackup>].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)
Expand All @@ -43,3 +48,13 @@ extension FullBackup: Codable {
try? container.encode(timestamp, forKey: .timestamp)
}
}

struct FailableDecodable<Base : Decodable> : Decodable {

let base: Base?

init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
base = try? container.decode(Base.self)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 4 additions & 8 deletions UnstoppableWallet/UnstoppableWallet/Models/AccountType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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] {
Expand Down Expand Up @@ -201,7 +202,6 @@ extension BackupAppViewModel {
}

func validatePasswords() {
var buttonDisabled = false
clearCautions()

do {
Expand Down Expand Up @@ -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: ()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ...";

Expand Down

0 comments on commit bc4948b

Please sign in to comment.