diff --git a/native/backup/use-client-backup.js b/native/backup/use-client-backup.js --- a/native/backup/use-client-backup.js +++ b/native/backup/use-client-backup.js @@ -66,28 +66,6 @@ console.info('Backup uploaded.'); }, [currentUserID, loggedIn, currentUserInfo]); - const restorePasswordUserBackupProtocol = React.useCallback(async () => { - if (!loggedIn || !currentUserID) { - throw new Error('Attempt to restore backup for not logged in user.'); - } - - if (!accountHasPassword(currentUserInfo)) { - throw new Error( - 'Attempt to restore from password for non-password user.', - ); - } - - console.info('Start restoring backup...'); - - const backupSecret = await getBackupSecret(); - await commCoreModule.restoreBackup( - backupSecret, - persistConfig.version.toString(), - ); - - console.info('Backup restored.'); - }, [currentUserID, loggedIn, currentUserInfo]); - const retrieveLatestSIWEBackupData = React.useCallback(async () => { if (!loggedIn || !currentUserID) { throw new Error('Attempt to restore backup for not logged in user.'); @@ -123,6 +101,27 @@ ); }, [currentUserID, currentUserInfo, loggedIn]); + const restorePasswordUserBackupProtocol = React.useCallback(async () => { + if (!accountHasPassword(currentUserInfo)) { + throw new Error( + 'Attempt to restore from password for non-password user.', + ); + } + + const [latestBackupInfo, backupSecret] = await Promise.all([ + retrieveLatestBackupInfo(), + getBackupSecret(), + ]); + + console.info('Start restoring backup...'); + await commCoreModule.restoreBackup( + backupSecret, + persistConfig.version.toString(), + latestBackupInfo.backupID, + ); + console.info('Backup restored.'); + }, [currentUserInfo, retrieveLatestBackupInfo]); + return React.useMemo( () => ({ uploadBackupProtocol, diff --git a/native/cpp/CommonCpp/NativeModules/CommCoreModule.h b/native/cpp/CommonCpp/NativeModules/CommCoreModule.h --- a/native/cpp/CommonCpp/NativeModules/CommCoreModule.h +++ b/native/cpp/CommonCpp/NativeModules/CommCoreModule.h @@ -228,7 +228,8 @@ virtual jsi::Value restoreBackup( jsi::Runtime &rt, jsi::String backupSecret, - jsi::String maxVersion) override; + jsi::String maxVersion, + jsi::String backupID) override; virtual jsi::Value restoreSIWEBackup( jsi::Runtime &rt, jsi::String backupSecret, diff --git a/native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp b/native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp --- a/native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp +++ b/native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp @@ -2574,10 +2574,13 @@ jsi::Value CommCoreModule::restoreBackup( jsi::Runtime &rt, jsi::String backupSecret, - jsi::String maxVersion) { + jsi::String maxVersion, + jsi::String backupID) { std::string backupSecretStr = backupSecret.utf8(rt); std::string maxVersionStr = maxVersion.utf8(rt); - return restoreBackupInternal(rt, backupSecretStr, "", maxVersionStr); + std::string backupIDStr = backupID.utf8(rt); + + return restoreBackupInternal(rt, backupSecretStr, backupIDStr, maxVersionStr); } jsi::Value CommCoreModule::restoreSIWEBackup( diff --git a/native/cpp/CommonCpp/_generated/commJSI-generated.cpp b/native/cpp/CommonCpp/_generated/commJSI-generated.cpp --- a/native/cpp/CommonCpp/_generated/commJSI-generated.cpp +++ b/native/cpp/CommonCpp/_generated/commJSI-generated.cpp @@ -188,7 +188,7 @@ return static_cast(&turboModule)->createNewSIWEBackup(rt, args[0].asString(rt), args[1].asString(rt)); } static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_restoreBackup(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { - return static_cast(&turboModule)->restoreBackup(rt, args[0].asString(rt), args[1].asString(rt)); + return static_cast(&turboModule)->restoreBackup(rt, args[0].asString(rt), args[1].asString(rt), args[2].asString(rt)); } static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_restoreSIWEBackup(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { return static_cast(&turboModule)->restoreSIWEBackup(rt, args[0].asString(rt), args[1].asString(rt), args[2].asString(rt)); @@ -306,7 +306,7 @@ methodMap_["stopBackupHandler"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_stopBackupHandler}; methodMap_["createNewBackup"] = MethodMetadata {1, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_createNewBackup}; methodMap_["createNewSIWEBackup"] = MethodMetadata {2, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_createNewSIWEBackup}; - methodMap_["restoreBackup"] = MethodMetadata {2, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_restoreBackup}; + methodMap_["restoreBackup"] = MethodMetadata {3, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_restoreBackup}; methodMap_["restoreSIWEBackup"] = MethodMetadata {3, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_restoreSIWEBackup}; methodMap_["restoreBackupData"] = MethodMetadata {4, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_restoreBackupData}; methodMap_["retrieveBackupKeys"] = MethodMetadata {1, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_retrieveBackupKeys}; diff --git a/native/cpp/CommonCpp/_generated/commJSI.h b/native/cpp/CommonCpp/_generated/commJSI.h --- a/native/cpp/CommonCpp/_generated/commJSI.h +++ b/native/cpp/CommonCpp/_generated/commJSI.h @@ -76,7 +76,7 @@ virtual void stopBackupHandler(jsi::Runtime &rt) = 0; virtual jsi::Value createNewBackup(jsi::Runtime &rt, jsi::String backupSecret) = 0; virtual jsi::Value createNewSIWEBackup(jsi::Runtime &rt, jsi::String backupSecret, jsi::String siweBackupMsg) = 0; - virtual jsi::Value restoreBackup(jsi::Runtime &rt, jsi::String backupSecret, jsi::String maxVersion) = 0; + virtual jsi::Value restoreBackup(jsi::Runtime &rt, jsi::String backupSecret, jsi::String maxVersion, jsi::String backupID) = 0; virtual jsi::Value restoreSIWEBackup(jsi::Runtime &rt, jsi::String backupSecret, jsi::String backupID, jsi::String maxVersion) = 0; virtual jsi::Value restoreBackupData(jsi::Runtime &rt, jsi::String backupID, jsi::String backupDataKey, jsi::String backupLogDataKey, jsi::String maxVersion) = 0; virtual jsi::Value retrieveBackupKeys(jsi::Runtime &rt, jsi::String backupSecret) = 0; @@ -565,13 +565,13 @@ return bridging::callFromJs( rt, &T::createNewSIWEBackup, jsInvoker_, instance_, std::move(backupSecret), std::move(siweBackupMsg)); } - jsi::Value restoreBackup(jsi::Runtime &rt, jsi::String backupSecret, jsi::String maxVersion) override { + jsi::Value restoreBackup(jsi::Runtime &rt, jsi::String backupSecret, jsi::String maxVersion, jsi::String backupID) override { static_assert( - bridging::getParameterCount(&T::restoreBackup) == 3, - "Expected restoreBackup(...) to have 3 parameters"); + bridging::getParameterCount(&T::restoreBackup) == 4, + "Expected restoreBackup(...) to have 4 parameters"); return bridging::callFromJs( - rt, &T::restoreBackup, jsInvoker_, instance_, std::move(backupSecret), std::move(maxVersion)); + rt, &T::restoreBackup, jsInvoker_, instance_, std::move(backupSecret), std::move(maxVersion), std::move(backupID)); } jsi::Value restoreSIWEBackup(jsi::Runtime &rt, jsi::String backupSecret, jsi::String backupID, jsi::String maxVersion) override { static_assert( diff --git a/native/native_rust_library/src/backup.rs b/native/native_rust_library/src/backup.rs --- a/native/native_rust_library/src/backup.rs +++ b/native/native_rust_library/src/backup.rs @@ -83,7 +83,6 @@ promise_id: u32, ) { RUNTIME.spawn(async move { - let backup_id = Option::from(backup_id).filter(|it| !it.is_empty()); let result = download_backup(backup_secret, backup_id) .await .map_err(|err| err.to_string()); @@ -364,16 +363,8 @@ async fn download_backup( backup_secret: String, - backup_id: Option, + backup_id: String, ) -> Result> { - let backup_id = match backup_id { - Some(backup_id) => backup_id, - None => { - let latest_backup_id_response = download_latest_backup_id().await?; - latest_backup_id_response.backup_id - } - }; - let backup_keys = download_backup_keys(backup_id, backup_secret).await?; download_backup_data(backup_keys).await } diff --git a/native/schema/CommCoreModuleSchema.js b/native/schema/CommCoreModuleSchema.js --- a/native/schema/CommCoreModuleSchema.js +++ b/native/schema/CommCoreModuleSchema.js @@ -161,7 +161,11 @@ backupSecret: string, siweBackupMsg: string, ) => Promise; - +restoreBackup: (backupSecret: string, maxVersion: string) => Promise; + +restoreBackup: ( + backupSecret: string, + maxVersion: string, + backupID: string, + ) => Promise; +restoreSIWEBackup: ( backupSecret: string, backupID: string,