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 @@ -3,7 +3,6 @@ import * as React from 'react'; import { isLoggedIn } from 'lib/selectors/user-selectors.js'; -import { accountHasPassword } from 'lib/shared/account-utils.js'; import { latestBackupInfoResponseValidator, type LatestBackupInfo, @@ -15,7 +14,7 @@ import { useSelector } from '../redux/redux-utils.js'; type ClientBackup = { - +uploadBackupProtocol: () => Promise, + +createFullBackup: () => Promise, +createUserKeysBackup: () => Promise, +retrieveLatestBackupInfo: () => Promise, }; @@ -28,29 +27,14 @@ const loggedIn = useSelector(isLoggedIn); const getBackupSecret = useGetBackupSecretForLoggedInUser(); - const uploadBackupProtocol = React.useCallback(async () => { + const createFullBackup = React.useCallback(async () => { if (!loggedIn || !currentUserID) { throw new Error('Attempt to upload backup for not logged in user.'); } - console.info('Start uploading backup...'); - - if (accountHasPassword(currentUserInfo)) { - const backupSecret = await getBackupSecret(); - await commCoreModule.createNewBackup(backupSecret); - } else { - const siweBackupSecrets = await commCoreModule.getSIWEBackupSecrets(); - if (!siweBackupSecrets) { - throw new Error('SIWE backup message and its signature are missing'); - } - await commCoreModule.createNewSIWEBackup( - siweBackupSecrets.signature, - siweBackupSecrets.message, - ); - } - - console.info('Backup uploaded.'); - }, [loggedIn, currentUserID, currentUserInfo, getBackupSecret]); + const backupSecret = await getBackupSecret(); + await commCoreModule.createFullBackup(backupSecret); + }, [loggedIn, currentUserID, getBackupSecret]); const createUserKeysBackup = React.useCallback(async () => { if (!loggedIn || !currentUserID) { @@ -78,11 +62,11 @@ return React.useMemo( () => ({ - uploadBackupProtocol, + createFullBackup, createUserKeysBackup, retrieveLatestBackupInfo, }), - [retrieveLatestBackupInfo, uploadBackupProtocol, createUserKeysBackup], + [retrieveLatestBackupInfo, createFullBackup, createUserKeysBackup], ); } 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 @@ -215,11 +215,7 @@ virtual void startBackupHandler(jsi::Runtime &rt) override; virtual void stopBackupHandler(jsi::Runtime &rt) override; virtual jsi::Value - createNewBackup(jsi::Runtime &rt, jsi::String backupSecret) override; - virtual jsi::Value createNewSIWEBackup( - jsi::Runtime &rt, - jsi::String backupSecret, - jsi::String siweBackupMsg) override; + createFullBackup(jsi::Runtime &rt, jsi::String backupSecret) override; virtual jsi::Value createUserKeysBackup(jsi::Runtime &rt, jsi::String backupSecret) override; virtual jsi::Value restoreBackup( 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 @@ -2507,15 +2507,46 @@ } } -jsi::Value CommCoreModule::createNewBackupInternal( - jsi::Runtime &rt, - std::string backupSecret, - std::string backupMessage) { +std::string getSIWEBackupMessage() { + std::promise backupSIWEMessagePromise; + std::future backupSIWEMessageFuture = + backupSIWEMessagePromise.get_future(); + GlobalDBSingleton::instance.scheduleOrRunCancellable( + [&backupSIWEMessagePromise]() { + try { + std::string backupSecrets = + DatabaseManager::getQueryExecutor().getMetadata( + "siweBackupSecrets"); + if (!backupSecrets.size()) { + backupSIWEMessagePromise.set_value(""); + } else { + folly::dynamic backupSecretsJSON = folly::parseJson(backupSecrets); + std::string message = backupSecretsJSON["message"].asString(); + backupSIWEMessagePromise.set_value(message); + } + } catch (std::system_error &e) { + backupSIWEMessagePromise.set_exception(std::make_exception_ptr(e)); + } + }); + return backupSIWEMessageFuture.get(); +} + +jsi::Value +CommCoreModule::createFullBackup(jsi::Runtime &rt, jsi::String backupSecret) { + std::string backupSecretStr = backupSecret.utf8(rt); return createPromiseAsJSIValue( rt, [=](jsi::Runtime &innerRt, std::shared_ptr promise) { + std::string backupMessage; + try { + backupMessage = getSIWEBackupMessage(); + } catch (std::system_error &e) { + this->jsInvoker_->invokeAsync( + [=, &innerRt]() { promise->reject(e.what()); }); + return; + } + this->cryptoThread->scheduleTask([=, &innerRt]() { std::string error; - std::string backupID; try { backupID = crypto::Tools::generateRandomURLSafeString(32); @@ -2542,7 +2573,7 @@ {promise, this->jsInvoker_, innerRt}); ::createBackup( rust::string(backupID), - rust::string(backupSecret), + rust::string(backupSecretStr), rust::string(pickleKey), rust::string(pickledAccount), rust::string(backupMessage), @@ -2555,54 +2586,15 @@ }); } -jsi::Value -CommCoreModule::createNewBackup(jsi::Runtime &rt, jsi::String backupSecret) { - std::string backupSecretStr = backupSecret.utf8(rt); - return createNewBackupInternal(rt, backupSecretStr, ""); -} - -jsi::Value CommCoreModule::createNewSIWEBackup( - jsi::Runtime &rt, - jsi::String backupSecret, - jsi::String siweBackupMsg) { - std::string backupSecretStr = backupSecret.utf8(rt); - std::string siweBackupMsgStr = siweBackupMsg.utf8(rt); - return createNewBackupInternal(rt, backupSecretStr, siweBackupMsgStr); -} - jsi::Value CommCoreModule::createUserKeysBackup( jsi::Runtime &rt, jsi::String backupSecret) { std::string backupSecretStr = backupSecret.utf8(rt); return createPromiseAsJSIValue( rt, [=](jsi::Runtime &innerRt, std::shared_ptr promise) { - std::promise backupSIWEMessagePromise; - std::future backupSIWEMessageFuture = - backupSIWEMessagePromise.get_future(); - - GlobalDBSingleton::instance.scheduleOrRunCancellable( - [=, &backupSIWEMessagePromise]() { - try { - std::string backupSecrets = - DatabaseManager::getQueryExecutor().getMetadata( - "siweBackupSecrets"); - if (!backupSecrets.size()) { - backupSIWEMessagePromise.set_value(""); - } else { - folly::dynamic backupSecretsJSON = - folly::parseJson(backupSecrets); - std::string message = backupSecretsJSON["message"].asString(); - backupSIWEMessagePromise.set_value(message); - } - } catch (std::system_error &e) { - backupSIWEMessagePromise.set_exception( - std::make_exception_ptr(e)); - } - }); - std::string backupMessage; try { - backupMessage = backupSIWEMessageFuture.get(); + backupMessage = getSIWEBackupMessage(); } catch (std::system_error &e) { this->jsInvoker_->invokeAsync( [=, &innerRt]() { promise->reject(e.what()); }); 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 @@ -181,15 +181,12 @@ static_cast(&turboModule)->stopBackupHandler(rt); return jsi::Value::undefined(); } -static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_createNewBackup(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { - return static_cast(&turboModule)->createNewBackup(rt, args[0].asString(rt)); -} -static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_createNewSIWEBackup(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { - return static_cast(&turboModule)->createNewSIWEBackup(rt, args[0].asString(rt), args[1].asString(rt)); -} static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_createUserKeysBackup(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { return static_cast(&turboModule)->createUserKeysBackup(rt, args[0].asString(rt)); } +static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_createFullBackup(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + return static_cast(&turboModule)->createFullBackup(rt, args[0].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), args[2].asString(rt)); } @@ -304,9 +301,8 @@ methodMap_["clearCommServicesAccessToken"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_clearCommServicesAccessToken}; methodMap_["startBackupHandler"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_startBackupHandler}; methodMap_["stopBackupHandler"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_stopBackupHandler}; - methodMap_["createNewBackup"] = MethodMetadata {1, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_createNewBackup}; - methodMap_["createNewSIWEBackup"] = MethodMetadata {2, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_createNewSIWEBackup}; methodMap_["createUserKeysBackup"] = MethodMetadata {1, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_createUserKeysBackup}; + methodMap_["createFullBackup"] = MethodMetadata {1, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_createFullBackup}; methodMap_["restoreBackup"] = MethodMetadata {3, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_restoreBackup}; methodMap_["restoreBackupData"] = MethodMetadata {4, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_restoreBackupData}; methodMap_["retrieveBackupKeys"] = MethodMetadata {2, __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 @@ -74,9 +74,8 @@ virtual jsi::Value clearCommServicesAccessToken(jsi::Runtime &rt) = 0; virtual void startBackupHandler(jsi::Runtime &rt) = 0; 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 createUserKeysBackup(jsi::Runtime &rt, jsi::String backupSecret) = 0; + virtual jsi::Value createFullBackup(jsi::Runtime &rt, jsi::String backupSecret) = 0; virtual jsi::Value restoreBackup(jsi::Runtime &rt, jsi::String backupSecret, jsi::String maxVersion, jsi::String backupID) = 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, jsi::String backupID) = 0; @@ -549,22 +548,6 @@ return bridging::callFromJs( rt, &T::stopBackupHandler, jsInvoker_, instance_); } - jsi::Value createNewBackup(jsi::Runtime &rt, jsi::String backupSecret) override { - static_assert( - bridging::getParameterCount(&T::createNewBackup) == 2, - "Expected createNewBackup(...) to have 2 parameters"); - - return bridging::callFromJs( - rt, &T::createNewBackup, jsInvoker_, instance_, std::move(backupSecret)); - } - jsi::Value createNewSIWEBackup(jsi::Runtime &rt, jsi::String backupSecret, jsi::String siweBackupMsg) override { - static_assert( - bridging::getParameterCount(&T::createNewSIWEBackup) == 3, - "Expected createNewSIWEBackup(...) to have 3 parameters"); - - return bridging::callFromJs( - rt, &T::createNewSIWEBackup, jsInvoker_, instance_, std::move(backupSecret), std::move(siweBackupMsg)); - } jsi::Value createUserKeysBackup(jsi::Runtime &rt, jsi::String backupSecret) override { static_assert( bridging::getParameterCount(&T::createUserKeysBackup) == 2, @@ -573,6 +556,14 @@ return bridging::callFromJs( rt, &T::createUserKeysBackup, jsInvoker_, instance_, std::move(backupSecret)); } + jsi::Value createFullBackup(jsi::Runtime &rt, jsi::String backupSecret) override { + static_assert( + bridging::getParameterCount(&T::createFullBackup) == 2, + "Expected createFullBackup(...) to have 2 parameters"); + + return bridging::callFromJs( + rt, &T::createFullBackup, jsInvoker_, instance_, std::move(backupSecret)); + } jsi::Value restoreBackup(jsi::Runtime &rt, jsi::String backupSecret, jsi::String maxVersion, jsi::String backupID) override { static_assert( bridging::getParameterCount(&T::restoreBackup) == 4, diff --git a/native/profile/backup-menu.react.js b/native/profile/backup-menu.react.js --- a/native/profile/backup-menu.react.js +++ b/native/profile/backup-menu.react.js @@ -39,22 +39,19 @@ state => state.localSettings.isBackupEnabled, ); - const { - uploadBackupProtocol, - retrieveLatestBackupInfo, - createUserKeysBackup, - } = useClientBackup(); + const { createFullBackup, retrieveLatestBackupInfo, createUserKeysBackup } = + useClientBackup(); const uploadBackup = React.useCallback(async () => { let message = 'Success'; try { - await uploadBackupProtocol(); + await createFullBackup(); } catch (e) { message = `Backup upload error: ${String(getMessageForException(e))}`; console.error(message); } Alert.alert('Upload protocol result', message); - }, [uploadBackupProtocol]); + }, [createFullBackup]); const uploadUserKeys = React.useCallback(async () => { let message = 'Success'; @@ -173,7 +170,7 @@ iosHighlightUnderlayColor={colors.panelIosHighlightUnderlay} iosActiveOpacity={0.85} > - Test backup upload protocol + Test Full Backup upload diff --git a/native/schema/CommCoreModuleSchema.js b/native/schema/CommCoreModuleSchema.js --- a/native/schema/CommCoreModuleSchema.js +++ b/native/schema/CommCoreModuleSchema.js @@ -156,12 +156,8 @@ +clearCommServicesAccessToken: () => Promise; +startBackupHandler: () => void; +stopBackupHandler: () => void; - +createNewBackup: (backupSecret: string) => Promise; - +createNewSIWEBackup: ( - backupSecret: string, - siweBackupMsg: string, - ) => Promise; +createUserKeysBackup: (backupSecret: string) => Promise; + +createFullBackup: (backupSecret: string) => Promise; +restoreBackup: ( backupSecret: string, maxVersion: string,