@@ -3015,8 +3015,11 @@ public class RegistrationCoordinatorImpl: RegistrationCoordinator {
30153015 break
30163016 case . changingNumber:
30173017 // Change number is different; we do a limited number of operations and then finalize.
3018- if let stepGuarantee = performSVRBackupStepsIfNeeded ( accountIdentity: accountIdentity) {
3019- return stepGuarantee
3018+ if let restoreStepGuarantee = performSVRRestoreStepsIfNeeded ( accountIdentity: accountIdentity) {
3019+ return restoreStepGuarantee
3020+ }
3021+ if let backupStepGuarantee = performSVRBackupStepsIfNeeded ( accountIdentity: accountIdentity) {
3022+ return backupStepGuarantee
30203023 }
30213024
30223025 return exportAndWipeState ( accountIdentity: accountIdentity)
@@ -3056,6 +3059,22 @@ public class RegistrationCoordinatorImpl: RegistrationCoordinator {
30563059 }
30573060 }
30583061
3062+ if
3063+ shouldRestoreFromStorageServiceBeforeUpdatingSVR ( ) ,
3064+ let restoredKey = persistedState. recoveredSVRMasterKey
3065+ {
3066+ // Need to preserve the key recovered by registration and use this for storage service restore
3067+ // If already restored due to AEP change, this step will be skipped
3068+ return restoreFromStorageService (
3069+ accountIdentity: accountIdentity,
3070+ masterKeySource: . explicit( restoredKey)
3071+ )
3072+ }
3073+
3074+ if let stepGuarantee = performSVRRestoreStepsIfNeeded ( accountIdentity: accountIdentity) {
3075+ return stepGuarantee
3076+ }
3077+
30593078 if persistedState. accountEntropyPool == nil {
30603079 if inMemoryState. restoreMethod? . backupType != nil {
30613080 // If the user want's to restore from backup, ask for the key
@@ -3072,18 +3091,6 @@ public class RegistrationCoordinatorImpl: RegistrationCoordinator {
30723091 }
30733092 }
30743093
3075- if
3076- shouldRestoreFromStorageServiceBeforeUpdatingSVR ( ) ,
3077- let restoredKey = persistedState. recoveredSVRMasterKey
3078- {
3079- // Need to preserve the key recovered by registration and use this for storage service restore
3080- // If already restored due to AEP change, this step will be skipped
3081- return restoreFromStorageService (
3082- accountIdentity: accountIdentity,
3083- masterKeySource: . explicit( restoredKey)
3084- )
3085- }
3086-
30873094 if let stepGuarantee = performSVRBackupStepsIfNeeded ( accountIdentity: accountIdentity) {
30883095 return stepGuarantee
30893096 }
@@ -3166,7 +3173,7 @@ public class RegistrationCoordinatorImpl: RegistrationCoordinator {
31663173 }
31673174
31683175 // returns nil if no steps needed.
3169- private func performSVRBackupStepsIfNeeded (
3176+ private func showPinEntryIfNeeded (
31703177 accountIdentity: AccountIdentity
31713178 ) -> Guarantee < RegistrationStep > ? {
31723179 Logger . info ( " " )
@@ -3177,41 +3184,70 @@ public class RegistrationCoordinatorImpl: RegistrationCoordinator {
31773184 )
31783185
31793186 if !persistedState. hasSkippedPinEntry {
3180- guard let pin = inMemoryState. pinFromUser ?? inMemoryState. pinFromDisk else {
3181- if isRestoringPinBackup {
3182- return . value( . pinEntry( RegistrationPinState (
3183- operation: . enteringExistingPin(
3184- skippability: . canSkipAndCreateNew,
3185- remainingAttempts: nil
3186- ) ,
3187- error: nil ,
3188- contactSupportMode: self . contactSupportRegistrationPINMode ( ) ,
3189- exitConfiguration: pinCodeEntryExitConfiguration ( )
3190- ) ) )
3191- } else if let blob = inMemoryState. unconfirmedPinBlob {
3192- return . value( . pinEntry( RegistrationPinState (
3193- operation: . confirmingNewPin( blob) ,
3194- error: nil ,
3195- contactSupportMode: self . contactSupportRegistrationPINMode ( ) ,
3196- exitConfiguration: pinCodeEntryExitConfiguration ( )
3197- ) ) )
3198- } else {
3199- return . value( . pinEntry( RegistrationPinState (
3200- operation: . creatingNewPin,
3201- error: nil ,
3202- contactSupportMode: self . contactSupportRegistrationPINMode ( ) ,
3203- exitConfiguration: pinCodeEntryExitConfiguration ( )
3204- ) ) )
3205- }
3187+ if isRestoringPinBackup {
3188+ return . value( . pinEntry( RegistrationPinState (
3189+ operation: . enteringExistingPin(
3190+ skippability: . canSkipAndCreateNew,
3191+ remainingAttempts: nil
3192+ ) ,
3193+ error: nil ,
3194+ contactSupportMode: self . contactSupportRegistrationPINMode ( ) ,
3195+ exitConfiguration: pinCodeEntryExitConfiguration ( )
3196+ ) ) )
3197+ } else if let blob = inMemoryState. unconfirmedPinBlob {
3198+ return . value( . pinEntry( RegistrationPinState (
3199+ operation: . confirmingNewPin( blob) ,
3200+ error: nil ,
3201+ contactSupportMode: self . contactSupportRegistrationPINMode ( ) ,
3202+ exitConfiguration: pinCodeEntryExitConfiguration ( )
3203+ ) ) )
3204+ } else {
3205+ return . value( . pinEntry( RegistrationPinState (
3206+ operation: . creatingNewPin,
3207+ error: nil ,
3208+ contactSupportMode: self . contactSupportRegistrationPINMode ( ) ,
3209+ exitConfiguration: pinCodeEntryExitConfiguration ( )
3210+ ) ) )
32063211 }
3212+ }
3213+ return nil
3214+ }
3215+
3216+ // returns nil if no steps needed.
3217+ private func performSVRRestoreStepsIfNeeded(
3218+ accountIdentity: AccountIdentity
3219+ ) -> Guarantee < RegistrationStep > ? {
3220+ Logger . info ( " " )
3221+ guard let pin = inMemoryState. pinFromUser ?? inMemoryState. pinFromDisk else {
3222+ return showPinEntryIfNeeded ( accountIdentity: accountIdentity)
3223+ }
3224+
3225+ if
3226+ !persistedState. hasSkippedPinEntry,
3227+ accountIdentity. hasPreviouslyUsedSVR,
3228+ !persistedState. hasGivenUpTryingToRestoreWithSVR,
3229+ inMemoryState. shouldRestoreSVRMasterKeyAfterRegistration
3230+ {
3231+ // If we have no SVR data, fetch it.
3232+ return restoreSVRBackupPostRegistration ( pin: pin, accountIdentity: accountIdentity)
3233+ }
3234+ return nil
3235+ }
3236+
3237+ // returns nil if no steps needed.
3238+ private func performSVRBackupStepsIfNeeded(
3239+ accountIdentity: AccountIdentity
3240+ ) -> Guarantee < RegistrationStep > ? {
3241+ Logger . info ( " " )
3242+
3243+ guard let pin = inMemoryState. pinFromUser ?? inMemoryState. pinFromDisk else {
3244+ return showPinEntryIfNeeded ( accountIdentity: accountIdentity)
3245+ }
3246+
3247+ if !persistedState. hasSkippedPinEntry {
32073248 if inMemoryState. shouldBackUpToSVR {
3208- // If we have no SVR data, fetch it.
3209- if isRestoringPinBackup, inMemoryState. shouldRestoreSVRMasterKeyAfterRegistration {
3210- return restoreSVRBackupPostRegistration ( pin: pin, accountIdentity: accountIdentity)
3211- } else {
3212- // If we haven't backed up, do so now.
3213- return backupToSVR ( pin: pin, accountIdentity: accountIdentity)
3214- }
3249+ // If we haven't backed up, do so now.
3250+ return backupToSVR ( pin: pin, accountIdentity: accountIdentity)
32153251 }
32163252
32173253 switch attributes2FAMode ( e164: accountIdentity. e164) {
@@ -3242,7 +3278,7 @@ public class RegistrationCoordinatorImpl: RegistrationCoordinator {
32423278 authMethod = backupAuthMethod
32433279 }
32443280 return deps. svr
3245- . restoreKeysAndBackup (
3281+ . restoreKeys (
32463282 pin: pin,
32473283 authMethod: authMethod
32483284 )
@@ -3253,7 +3289,6 @@ public class RegistrationCoordinatorImpl: RegistrationCoordinator {
32533289 switch result {
32543290 case . success( let masterKey) :
32553291 self . inMemoryState. shouldRestoreSVRMasterKeyAfterRegistration = false
3256- self . inMemoryState. hasBackedUpToSVR = true
32573292 self . db. write { tx in
32583293 self . updatePersistedState ( tx) { $0. recoveredSVRMasterKey = masterKey }
32593294 }
0 commit comments