Skip to content

Commit

Permalink
Fix handling icloud oneWalletBackups for v1 and v2
Browse files Browse the repository at this point in the history
- add fileBackedUp field for accounts
  • Loading branch information
ant013 committed Oct 9, 2023
1 parent 6429ea2 commit 194623e
Show file tree
Hide file tree
Showing 15 changed files with 147 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class WalletBackup: Codable {
let id: String
let type: AccountType.Abstract
let isManualBackedUp: Bool
let isFileBackedUp: Bool
let version: Int
let timestamp: TimeInterval?
let enabledWallets: [EnabledWallet]
Expand All @@ -15,16 +16,18 @@ class WalletBackup: Codable {
case id
case type
case isManualBackedUp = "manual_backup"
case isFileBackedUp = "file_backup"
case version
case timestamp
}

init(crypto: BackupCrypto, enabledWallets: [EnabledWallet], id: String, type: AccountType.Abstract, isManualBackedUp: Bool, version: Int, timestamp: TimeInterval) {
init(crypto: BackupCrypto, enabledWallets: [EnabledWallet], id: String, type: AccountType.Abstract, isManualBackedUp: Bool, isFileBackedUp: Bool, version: Int, timestamp: TimeInterval) {
self.crypto = crypto
self.enabledWallets = enabledWallets
self.id = id
self.type = type
self.isManualBackedUp = isManualBackedUp
self.isFileBackedUp = isFileBackedUp
self.version = version
self.timestamp = timestamp
}
Expand All @@ -37,6 +40,8 @@ class WalletBackup: Codable {
type = try container.decode(AccountType.Abstract.self, forKey: .type)
let isManualBackedUp = try? container.decode(Bool.self, forKey: .isManualBackedUp)
self.isManualBackedUp = isManualBackedUp ?? false
let isFileBackedUp = try? container.decode(Bool.self, forKey: .isFileBackedUp)
self.isFileBackedUp = isFileBackedUp ?? false
version = try container.decode(Int.self, forKey: .version)
timestamp = try? container.decode(TimeInterval.self, forKey: .timestamp)
}
Expand All @@ -48,6 +53,7 @@ class WalletBackup: Codable {
try container.encode(id, forKey: .id)
try container.encode(type, forKey: .type)
try container.encode(isManualBackedUp, forKey: .isManualBackedUp)
try container.encode(isFileBackedUp, forKey: .isFileBackedUp)
try container.encode(version, forKey: .version)
try container.encode(timestamp, forKey: .timestamp)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@ extension AccountFactory {
return "Watch Wallet \(order)"
}

func account(type: AccountType, origin: AccountOrigin, backedUp: Bool, name: String) -> Account {
func account(type: AccountType, origin: AccountOrigin, backedUp: Bool, fileBackedUp: Bool, name: String) -> Account {
Account(
id: UUID().uuidString,
level: accountManager.currentLevel,
name: name,
type: type,
origin: origin,
backedUp: backedUp
backedUp: backedUp,
fileBackedUp: fileBackedUp
)
}

Expand All @@ -54,7 +55,8 @@ extension AccountFactory {
name: name,
type: type,
origin: .restored,
backedUp: true
backedUp: true,
fileBackedUp: false
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@ class CloudBackupManager {

do {
forceDownloadContainerFiles(url: url)
let oneWalletItems: [String: WalletBackup] = try Self.downloadItems(url: url, fileStorage: fileStorage, logger: logger)

var oneWalletItems: [String: WalletBackup] = try Self.downloadItems(url: url, fileStorage: fileStorage, logger: logger)
let oneWalletItemsV2: [String: RestoreCloudModule.RestoredBackup] = try Self.downloadItems(url: url, fileStorage: fileStorage, logger: logger)
let mapped = oneWalletItemsV2.reduce(into: [:]) { $0[$1.value.name] = $1.value.walletBackup }

oneWalletItems.merge(mapped) { backup, backup2 in backup2 }
let fullBackupItems: [String: FullBackup] = try Self.downloadItems(url: url, fileStorage: fileStorage, logger: logger)

state = .success
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ class AccountStorage {
name: record.name,
type: type,
origin: origin,
backedUp: record.backedUp
backedUp: record.backedUp,
fileBackedUp: record.fileBackedUp
)
}

Expand Down Expand Up @@ -132,6 +133,7 @@ class AccountStorage {
type: typeName.rawValue,
origin: account.origin.rawValue,
backedUp: account.backedUp,
fileBackedUp: account.fileBackedUp,
wordsKey: wordsKey,
saltKey: saltKey,
dataKey: dataKey,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -784,9 +784,10 @@ class StorageMigrator {
}
}

migrator.registerMigration("Add level to AccountRecord") { db in
migrator.registerMigration("Add level and fileBackedUp to AccountRecord") { db in
try db.alter(table: AccountRecord.databaseTableName) { t in
t.add(column: AccountRecord.Columns.level.name, .integer).defaults(to: 0)
t.add(column: AccountRecord.Columns.fileBackedUp.name, .boolean).defaults(to: false)
}
}

Expand Down
4 changes: 3 additions & 1 deletion UnstoppableWallet/UnstoppableWallet/Models/Account.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ class Account: Identifiable {
let type: AccountType
let origin: AccountOrigin
var backedUp: Bool
var fileBackedUp: Bool

init(id: String, level: Int, name: String, type: AccountType, origin: AccountOrigin, backedUp: Bool) {
init(id: String, level: Int, name: String, type: AccountType, origin: AccountOrigin, backedUp: Bool, fileBackedUp: Bool) {
self.id = id
self.level = level
self.name = name
self.type = type
self.origin = origin
self.backedUp = backedUp
self.fileBackedUp = fileBackedUp
}

var watchAccount: Bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@ class AccountRecord: Record {
let type: String
let origin: String
let backedUp: Bool
let fileBackedUp: Bool
var wordsKey: String?
var saltKey: String?
var dataKey: String?
var bip39Compliant: Bool?

init(id: String, level: Int, name: String, type: String, origin: String, backedUp: Bool, wordsKey: String?, saltKey: String?, dataKey: String?, bip39Compliant: Bool?) {
init(id: String, level: Int, name: String, type: String, origin: String, backedUp: Bool, fileBackedUp: Bool, wordsKey: String?, saltKey: String?, dataKey: String?, bip39Compliant: Bool?) {
self.id = id
self.level = level
self.name = name
self.type = type
self.origin = origin
self.backedUp = backedUp
self.fileBackedUp = fileBackedUp
self.wordsKey = wordsKey
self.saltKey = saltKey
self.dataKey = dataKey
Expand All @@ -32,7 +34,7 @@ class AccountRecord: Record {
}

enum Columns: String, ColumnExpression {
case id, level, name, type, origin, backedUp, wordsKey, saltKey, dataKey, bip39Compliant
case id, level, name, type, origin, backedUp, fileBackedUp, wordsKey, saltKey, dataKey, bip39Compliant
}

required init(row: Row) {
Expand All @@ -42,6 +44,7 @@ class AccountRecord: Record {
type = row[Columns.type]
origin = row[Columns.origin]
backedUp = row[Columns.backedUp]
fileBackedUp = row[Columns.fileBackedUp]
wordsKey = row[Columns.wordsKey]
saltKey = row[Columns.saltKey]
dataKey = row[Columns.dataKey]
Expand All @@ -57,6 +60,7 @@ class AccountRecord: Record {
container[Columns.type] = type
container[Columns.origin] = origin
container[Columns.backedUp] = backedUp
container[Columns.fileBackedUp] = fileBackedUp
container[Columns.wordsKey] = wordsKey
container[Columns.saltKey] = saltKey
container[Columns.dataKey] = dataKey
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ extension AppBackupProvider {
accountType: account.type,
wallets: wallets,
isManualBackedUp: account.backedUp,
isFileBackedUp: account.fileBackedUp,
name: account.name,
passphrase: passphrase
)
Expand Down Expand Up @@ -171,11 +172,18 @@ extension AppBackupProvider {
type: accountType,
origin: .restored,
backedUp: backup.walletBackup.isManualBackedUp,
fileBackedUp: backup.walletBackup.isFileBackedUp,
name: backup.name
)
accountManager.save(account: account)
default:
let account = accountFactory.account(type: accountType, origin: .restored, backedUp: backup.walletBackup.isManualBackedUp, name: backup.name)
let account = accountFactory.account(
type: accountType,
origin: .restored,
backedUp: backup.walletBackup.isManualBackedUp,
fileBackedUp: backup.walletBackup.isFileBackedUp,
name: backup.name
)
accountManager.save(account: account)

let wallets = backup.walletBackup.enabledWallets.map {
Expand Down Expand Up @@ -262,7 +270,7 @@ extension AppBackupProvider {
}

extension AppBackupProvider {
static func walletBackup(accountType: AccountType, wallets: [WalletBackup.EnabledWallet], isManualBackedUp: Bool, name: String, passphrase: String) throws -> RestoreCloudModule.RestoredBackup {
static func walletBackup(accountType: AccountType, wallets: [WalletBackup.EnabledWallet], isManualBackedUp: Bool, isFileBackedUp: Bool, name: String, passphrase: String) throws -> RestoreCloudModule.RestoredBackup {
let message = accountType.uniqueId(hashed: false)
let crypto = try BackupCrypto.instance(data: message, passphrase: passphrase)

Expand All @@ -272,6 +280,7 @@ extension AppBackupProvider {
id: accountType.uniqueId().hs.hex,
type: AccountType.Abstract(accountType),
isManualBackedUp: isManualBackedUp,
isFileBackedUp: isFileBackedUp,
version: Self.version,
timestamp: Date().timeIntervalSince1970.rounded()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class RestoreBinanceService {
private func createAccount() {
let type: AccountType = .cex(cexAccount: .binance(apiKey: apiKey, secret: secretKey))
let name = accountFactory.nextAccountName(cex: .binance)
let account = accountFactory.account(type: type, origin: .restored, backedUp: true, name: name)
let account = accountFactory.account(type: type, origin: .restored, backedUp: true, fileBackedUp: false, name: name)

accountManager.save(account: account)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ extension CreateAccountService {
type: accountType,
origin: .created,
backedUp: false,
fileBackedUp: false,
name: trimmedName.isEmpty ? defaultAccountName : trimmedName
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ struct RestoreCloudModule {
let name: String
let accountType: AccountType
let isManualBackedUp: Bool
let isFileBackedUp: Bool
let showSelectCoins: Bool
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ class RestoreCloudPassphraseService {
}

private func createAccount(accountType: AccountType) {
let account = accountFactory.account(type: accountType, origin: .restored, backedUp: restoredBackup.walletBackup.isManualBackedUp, name: restoredBackup.name)
let account = accountFactory.account(
type: accountType,
origin: .restored,
backedUp: restoredBackup.walletBackup.isManualBackedUp,
fileBackedUp: restoredBackup.walletBackup.isFileBackedUp,
name: restoredBackup.name
)
accountManager.save(account: account)

let wallets = restoredBackup.walletBackup.enabledWallets.map {
Expand Down Expand Up @@ -67,6 +73,7 @@ extension RestoreCloudPassphraseService {
name: restoredBackup.name,
accountType: accountType,
isManualBackedUp: restoredBackup.walletBackup.isManualBackedUp,
isFileBackedUp: restoredBackup.walletBackup.isFileBackedUp,
showSelectCoins: restoredBackup.walletBackup.enabledWallets.isEmpty
))
}
Expand Down
Loading

0 comments on commit 194623e

Please sign in to comment.