Skip to content

Commit 08a913a

Browse files
committed
WIP
1 parent f41e13e commit 08a913a

File tree

3 files changed

+68
-6
lines changed

3 files changed

+68
-6
lines changed

Bitkit/AppScene.swift

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -410,31 +410,73 @@ struct AppScene: View {
410410
return String(data: data, encoding: .utf8)
411411
}()
412412

413+
let migrations = MigrationsService.shared
414+
415+
// If migration was already completed, only use VSS backups (RN backups are outdated)
416+
if migrations.isMigrationCompleted {
417+
Logger.info("Migration already completed, using VSS backup only (RN backups are outdated)", context: "AppScene")
418+
await BackupService.shared.performFullRestoreFromLatestBackup()
419+
return
420+
}
421+
413422
// Check for RN backup and get its timestamp
414-
let hasRNBackup = await MigrationsService.shared.hasRNRemoteBackup(mnemonic: mnemonic, passphrase: passphrase)
423+
let hasRNBackup = await migrations.hasRNRemoteBackup(mnemonic: mnemonic, passphrase: passphrase)
415424
let rnTimestamp: UInt64? = await hasRNBackup ? (try? RNBackupClient.shared.getLatestBackupTimestamp()) : nil
416425

417426
// Get VSS backup timestamp
418427
let vssTimestamp = await BackupService.shared.getLatestBackupTime()
419428

420429
// Determine which backup is more recent
421430
let shouldRestoreRN: Bool = {
422-
guard hasRNBackup else { return false }
423-
guard let vss = vssTimestamp, vss > 0 else { return true } // No VSS, use RN
424-
guard let rn = rnTimestamp else { return false } // No RN timestamp, use VSS
425-
return rn >= vss // RN is same or newer
431+
guard hasRNBackup else {
432+
Logger.debug("No RN backup found, using VSS", context: "AppScene")
433+
return false
434+
}
435+
436+
// If VSS timestamp is unavailable, prefer VSS anyway (it might exist but timestamp fetch failed)
437+
// Only use RN if we're certain VSS doesn't exist
438+
guard let vss = vssTimestamp, vss > 0 else {
439+
Logger.warn(
440+
"VSS backup timestamp unavailable (nil or 0). Attempting VSS restore first. If it fails, will fall back to RN backup.",
441+
context: "AppScene"
442+
)
443+
// Try VSS first, it will handle errors internally
444+
return false
445+
}
446+
447+
guard let rn = rnTimestamp else {
448+
Logger.debug("No RN timestamp available, using VSS", context: "AppScene")
449+
return false
450+
}
451+
452+
let useRN = rn >= vss
453+
if useRN {
454+
Logger.info("RN backup is newer or equal (RN: \(rn), VSS: \(vss)), using RN backup", context: "AppScene")
455+
} else {
456+
Logger.info("VSS backup is newer (RN: \(rn), VSS: \(vss)), using VSS backup", context: "AppScene")
457+
}
458+
return useRN
426459
}()
427460

428461
if shouldRestoreRN {
429462
do {
430-
try await MigrationsService.shared.restoreFromRNRemoteBackup(mnemonic: mnemonic, passphrase: passphrase)
463+
try await migrations.restoreFromRNRemoteBackup(mnemonic: mnemonic, passphrase: passphrase)
431464
} catch {
432465
Logger.error("RN remote backup restore failed: \(error)", context: "AppScene")
433466
// Fall back to VSS
467+
Logger.info("Falling back to VSS backup after RN restore failure", context: "AppScene")
434468
await BackupService.shared.performFullRestoreFromLatestBackup()
435469
}
436470
} else {
471+
// Always try VSS first
437472
await BackupService.shared.performFullRestoreFromLatestBackup()
473+
474+
// If VSS restore didn't work and RN backup exists, try RN as fallback
475+
// Note: We can't easily detect if VSS restore failed since it doesn't throw,
476+
// but this is a reasonable fallback strategy
477+
if hasRNBackup {
478+
Logger.debug("VSS restore attempted. If it failed, user may need to manually restore from RN backup.", context: "AppScene")
479+
}
438480
}
439481
}
440482

Bitkit/Services/MigrationsService.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,14 @@ extension MigrationsService {
416416
UserDefaults.standard.bool(forKey: Self.rnMigrationCheckedKey)
417417
}
418418

419+
var isMigrationCompleted: Bool {
420+
UserDefaults.standard.bool(forKey: Self.rnMigrationCompletedKey)
421+
}
422+
423+
func markMigrationCompleted() {
424+
UserDefaults.standard.set(true, forKey: Self.rnMigrationCompletedKey)
425+
}
426+
419427
func hasRNWalletData() -> Bool {
420428
do {
421429
let mnemonic = try loadStringFromRNKeychain(key: .mnemonic(walletName: rnWalletName))

Bitkit/Utilities/AppReset.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,25 @@ enum AppReset {
3232
// Delete installation marker so next install can detect orphaned keychain
3333
try? InstallationMarker.delete()
3434

35+
// Preserve migration completion status before wiping UserDefaults
36+
// If user migrated from RN before, they should only use VSS backups after restore
37+
let wasMigrationCompleted = MigrationsService.shared.isMigrationCompleted
38+
3539
// Wipe user defaults
3640
if let bundleID = Bundle.main.bundleIdentifier {
3741
UserDefaults.standard.removePersistentDomain(forName: bundleID)
3842
}
3943

44+
// Restore migration flags after wipe
4045
// Prevent RN migration from triggering after wipe
4146
MigrationsService.shared.markMigrationChecked()
47+
48+
// If migration was completed before wipe, preserve that status
49+
// This ensures restore logic will only use VSS backups (not outdated RN backups)
50+
if wasMigrationCompleted {
51+
MigrationsService.shared.markMigrationCompleted()
52+
Logger.info("Preserved migration completion status after wipe", context: "AppReset")
53+
}
4254

4355
// Wipe logs
4456
if Env.network == .regtest {

0 commit comments

Comments
 (0)