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 @@ -52,6 +52,10 @@ jsi::Runtime &rt, std::string backupSecret, std::string backupMessage); + jsi::Value restoreBackupInternal( + jsi::Runtime &rt, + std::string backupSecret, + std::string backupID); virtual jsi::Value getDraft(jsi::Runtime &rt, jsi::String key) override; virtual jsi::Value @@ -184,6 +188,10 @@ jsi::String siweBackupMsg) override; virtual jsi::Value restoreBackup(jsi::Runtime &rt, jsi::String backupSecret) override; + virtual jsi::Value restoreSIWEBackup( + jsi::Runtime &rt, + jsi::String backupSecret, + jsi::String backupID) override; virtual jsi::Value restoreBackupData( jsi::Runtime &rt, jsi::String backupID, 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 @@ -2012,17 +2012,34 @@ return createNewBackupInternal(rt, backupSecretStr, siweBackupMsgStr); } -jsi::Value -CommCoreModule::restoreBackup(jsi::Runtime &rt, jsi::String backupSecret) { - std::string backupSecretStr = backupSecret.utf8(rt); +jsi::Value CommCoreModule::restoreBackupInternal( + jsi::Runtime &rt, + std::string backupSecret, + std::string backupID) { return createPromiseAsJSIValue( rt, [=](jsi::Runtime &innerRt, std::shared_ptr promise) { auto currentID = RustPromiseManager::instance.addPromise( {promise, this->jsInvoker_, innerRt}); - ::restoreBackup(rust::string(backupSecretStr), currentID); + ::restoreBackup( + rust::string(backupSecret), rust::string(backupID), currentID); }); } +jsi::Value +CommCoreModule::restoreBackup(jsi::Runtime &rt, jsi::String backupSecret) { + std::string backupSecretStr = backupSecret.utf8(rt); + return restoreBackupInternal(rt, backupSecretStr, ""); +} + +jsi::Value CommCoreModule::restoreSIWEBackup( + jsi::Runtime &rt, + jsi::String backupSecret, + jsi::String backupID) { + std::string backupSecretStr = backupSecret.utf8(rt); + std::string backupIDStr = backupID.utf8(rt); + return restoreBackupInternal(rt, backupSecretStr, backupIDStr); +} + jsi::Value CommCoreModule::restoreBackupData( jsi::Runtime &rt, jsi::String backupID, 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 @@ -166,6 +166,9 @@ 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)); } +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)); +} static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_restoreBackupData(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { return static_cast(&turboModule)->restoreBackupData(rt, args[0].asString(rt), args[1].asString(rt), args[2].asString(rt)); } @@ -239,6 +242,7 @@ methodMap_["createNewBackup"] = MethodMetadata {1, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_createNewBackup}; methodMap_["createNewSIWEBackup"] = MethodMetadata {2, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_createNewSIWEBackup}; methodMap_["restoreBackup"] = MethodMetadata {1, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_restoreBackup}; + methodMap_["restoreSIWEBackup"] = MethodMetadata {2, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_restoreSIWEBackup}; methodMap_["restoreBackupData"] = MethodMetadata {3, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_restoreBackupData}; methodMap_["retrieveBackupKeys"] = MethodMetadata {1, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_retrieveBackupKeys}; methodMap_["retrieveLatestSIWEBackupData"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_retrieveLatestSIWEBackupData}; 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 @@ -69,6 +69,7 @@ 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) = 0; + virtual jsi::Value restoreSIWEBackup(jsi::Runtime &rt, jsi::String backupSecret, jsi::String backupID) = 0; virtual jsi::Value restoreBackupData(jsi::Runtime &rt, jsi::String backupID, jsi::String backupDataKey, jsi::String backupLogDataKey) = 0; virtual jsi::Value retrieveBackupKeys(jsi::Runtime &rt, jsi::String backupSecret) = 0; virtual jsi::Value retrieveLatestSIWEBackupData(jsi::Runtime &rt) = 0; @@ -489,6 +490,14 @@ return bridging::callFromJs( rt, &T::restoreBackup, jsInvoker_, instance_, std::move(backupSecret)); } + jsi::Value restoreSIWEBackup(jsi::Runtime &rt, jsi::String backupSecret, jsi::String backupID) override { + static_assert( + bridging::getParameterCount(&T::restoreSIWEBackup) == 3, + "Expected restoreSIWEBackup(...) to have 3 parameters"); + + return bridging::callFromJs( + rt, &T::restoreSIWEBackup, jsInvoker_, instance_, std::move(backupSecret), std::move(backupID)); + } jsi::Value restoreBackupData(jsi::Runtime &rt, jsi::String backupID, jsi::String backupDataKey, jsi::String backupLogDataKey) override { static_assert( bridging::getParameterCount(&T::restoreBackupData) == 4, 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 @@ -75,9 +75,13 @@ }); } - pub fn restore_backup(backup_secret: String, promise_id: u32) { + pub fn restore_backup( + backup_secret: String, + backup_id: String, + promise_id: u32, + ) { RUNTIME.spawn(async move { - let result = download_backup(backup_secret) + let result = download_backup(backup_secret, backup_id) .await .map_err(|err| err.to_string()); @@ -115,7 +119,7 @@ pub fn retrieve_backup_keys(backup_secret: String, promise_id: u32) { RUNTIME.spawn(async move { - let result = download_backup_keys(backup_secret) + let result = download_backup_keys(backup_secret, "".to_string()) .await .map_err(|err| err.to_string()); @@ -261,8 +265,16 @@ async fn download_backup( backup_secret: String, + backup_id: String, ) -> Result> { - let backup_keys = download_backup_keys(backup_secret).await?; + let backup_id = if !backup_id.is_empty() { + backup_id + } else { + 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 } @@ -291,6 +303,7 @@ } async fn download_backup_keys( + backup_id: String, backup_secret: String, ) -> Result> { let backup_client = BackupClient::new(BACKUP_SOCKET_ADDR)?; @@ -300,19 +313,10 @@ username: user_identity.user_id.clone(), }; - let backup_id_response = backup_client - .download_backup_data(&latest_backup_descriptor, RequestedData::BackupID) - .await?; - - let LatestBackupIDResponse { backup_id, .. } = - serde_json::from_slice(&backup_id_response)?; - - let mut backup_key = compute_backup_key_str(&backup_secret, &backup_id)?; - let mut encrypted_user_keys = backup_client .download_backup_data(&latest_backup_descriptor, RequestedData::UserKeys) .await?; - + let mut backup_key = compute_backup_key_str(&backup_secret, &backup_id)?; let user_keys = UserKeys::from_encrypted(&mut encrypted_user_keys, &mut backup_key)?; @@ -393,14 +397,18 @@ } #[derive(Debug, Serialize)] +#[serde(rename_all = "camelCase")] struct BackupKeysResult { + #[serde(rename = "backupID")] backup_id: String, backup_data_key: String, backup_log_data_key: String, } #[derive(Debug, Serialize)] +#[serde(rename_all = "camelCase")] struct SIWEBackupData { + #[serde(rename = "backupID")] backup_id: String, siwe_backup_msg: String, } diff --git a/native/native_rust_library/src/lib.rs b/native/native_rust_library/src/lib.rs --- a/native/native_rust_library/src/lib.rs +++ b/native/native_rust_library/src/lib.rs @@ -360,7 +360,11 @@ ); #[cxx_name = "restoreBackup"] - fn restore_backup(backup_secret: String, promise_id: u32); + fn restore_backup( + backup_secret: String, + backup_id: String, + promise_id: u32, + ); #[cxx_name = "restoreBackupData"] fn restore_backup_data( diff --git a/native/schema/CommCoreModuleSchema.js b/native/schema/CommCoreModuleSchema.js --- a/native/schema/CommCoreModuleSchema.js +++ b/native/schema/CommCoreModuleSchema.js @@ -129,6 +129,10 @@ siweBackupMsg: string, ) => Promise; +restoreBackup: (backupSecret: string) => Promise; + +restoreSIWEBackup: ( + backupSecret: string, + backupID: string, + ) => Promise; +restoreBackupData: ( backupID: string, backupDataKey: string,