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 @@ -194,6 +194,10 @@ jsi::String backupLogDataKey) override; virtual jsi::Value retrieveBackupKeys(jsi::Runtime &rt, jsi::String backupSecret) override; + virtual jsi::Value setSIWEBackupSecrets( + jsi::Runtime &rt, + jsi::Object siweBackupSecrets) override; + virtual jsi::Value getSIWEBackupSecrets(jsi::Runtime &rt) override; public: CommCoreModule(std::shared_ptr jsInvoker); 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 @@ -1868,4 +1868,81 @@ ::retrieveBackupKeys(rust::string(backupSecretStr), currentID); }); } + +jsi::Value CommCoreModule::setSIWEBackupSecrets( + jsi::Runtime &rt, + jsi::Object siweBackupSecrets) { + std::string message = + siweBackupSecrets.getProperty(rt, "message").asString(rt).utf8(rt); + std::string signature = + siweBackupSecrets.getProperty(rt, "signature").asString(rt).utf8(rt); + folly::dynamic backupSecretsJSON = + folly::dynamic::object("message", message)("signature", signature); + std::string backupSecrets = folly::toJson(backupSecretsJSON); + + return createPromiseAsJSIValue( + rt, [=](jsi::Runtime &innerRt, std::shared_ptr promise) { + taskType job = [this, promise, backupSecrets]() { + std::string error; + try { + DatabaseManager::getQueryExecutor().setMetadata( + "siweBackupSecrets", backupSecrets); + } catch (const std::exception &e) { + error = e.what(); + } + this->jsInvoker_->invokeAsync([error, promise]() { + if (error.size()) { + promise->reject(error); + } else { + promise->resolve(jsi::Value::undefined()); + } + }); + }; + GlobalDBSingleton::instance.scheduleOrRunCancellable( + job, promise, this->jsInvoker_); + }); +} + +jsi::Value CommCoreModule::getSIWEBackupSecrets(jsi::Runtime &rt) { + return createPromiseAsJSIValue( + rt, [this](jsi::Runtime &innerRt, std::shared_ptr promise) { + taskType job = [this, &innerRt, promise]() { + std::string error; + std::string backupSecrets; + try { + backupSecrets = DatabaseManager::getQueryExecutor().getMetadata( + "siweBackupSecrets"); + } catch (const std::exception &e) { + error = e.what(); + } + this->jsInvoker_->invokeAsync( + [&innerRt, error, backupSecrets, promise]() { + if (error.size()) { + promise->reject(error); + } else if (!backupSecrets.size()) { + promise->resolve(jsi::Value::undefined()); + } else { + folly::dynamic backupSecretsJSON = + folly::parseJson(backupSecrets); + std::string message = backupSecretsJSON["message"].asString(); + std::string signature = + backupSecretsJSON["signature"].asString(); + + auto siweBackupSecrets = jsi::Object(innerRt); + siweBackupSecrets.setProperty( + innerRt, + "message", + jsi::String::createFromUtf8(innerRt, message)); + siweBackupSecrets.setProperty( + innerRt, + "signature", + jsi::String::createFromUtf8(innerRt, signature)); + promise->resolve(std::move(siweBackupSecrets)); + } + }); + }; + GlobalDBSingleton::instance.scheduleOrRunCancellable( + job, promise, this->jsInvoker_); + }); +} } // namespace comm 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 @@ -193,6 +193,12 @@ static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_retrieveBackupKeys(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { return static_cast(&turboModule)->retrieveBackupKeys(rt, args[0].asString(rt)); } +static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_setSIWEBackupSecrets(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + return static_cast(&turboModule)->setSIWEBackupSecrets(rt, args[0].asObject(rt)); +} +static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_getSIWEBackupSecrets(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + return static_cast(&turboModule)->getSIWEBackupSecrets(rt); +} CommCoreModuleSchemaCxxSpecJSI::CommCoreModuleSchemaCxxSpecJSI(std::shared_ptr jsInvoker) : TurboModule("CommTurboModule", jsInvoker) { @@ -254,6 +260,8 @@ methodMap_["restoreBackup"] = MethodMetadata {1, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_restoreBackup}; methodMap_["restoreBackupData"] = MethodMetadata {3, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_restoreBackupData}; methodMap_["retrieveBackupKeys"] = MethodMetadata {1, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_retrieveBackupKeys}; + methodMap_["setSIWEBackupSecrets"] = MethodMetadata {1, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_setSIWEBackupSecrets}; + methodMap_["getSIWEBackupSecrets"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_getSIWEBackupSecrets}; } 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 @@ -78,6 +78,8 @@ virtual jsi::Value restoreBackup(jsi::Runtime &rt, jsi::String backupSecret) = 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 setSIWEBackupSecrets(jsi::Runtime &rt, jsi::Object siweBackupSecrets) = 0; + virtual jsi::Value getSIWEBackupSecrets(jsi::Runtime &rt) = 0; }; @@ -563,6 +565,22 @@ return bridging::callFromJs( rt, &T::retrieveBackupKeys, jsInvoker_, instance_, std::move(backupSecret)); } + jsi::Value setSIWEBackupSecrets(jsi::Runtime &rt, jsi::Object siweBackupSecrets) override { + static_assert( + bridging::getParameterCount(&T::setSIWEBackupSecrets) == 2, + "Expected setSIWEBackupSecrets(...) to have 2 parameters"); + + return bridging::callFromJs( + rt, &T::setSIWEBackupSecrets, jsInvoker_, instance_, std::move(siweBackupSecrets)); + } + jsi::Value getSIWEBackupSecrets(jsi::Runtime &rt) override { + static_assert( + bridging::getParameterCount(&T::getSIWEBackupSecrets) == 1, + "Expected getSIWEBackupSecrets(...) to have 1 parameters"); + + return bridging::callFromJs( + rt, &T::getSIWEBackupSecrets, jsInvoker_, instance_); + } private: T *instance_; diff --git a/native/schema/CommCoreModuleSchema.js b/native/schema/CommCoreModuleSchema.js --- a/native/schema/CommCoreModuleSchema.js +++ b/native/schema/CommCoreModuleSchema.js @@ -24,6 +24,7 @@ } from 'lib/types/crypto-types.js'; import type { ClientDBDraftStoreOperation } from 'lib/types/draft-types.js'; import type { ClientDBMessageInfo } from 'lib/types/message-types.js'; +import type { SIWEBackupSecrets } from 'lib/types/siwe-types.js'; import type { ClientDBStore } from 'lib/types/store-ops-types'; import type { ClientDBThreadInfo } from 'lib/types/thread-types.js'; @@ -156,6 +157,8 @@ backupLogDataKey: string, ) => Promise; +retrieveBackupKeys: (backupSecret: string) => Promise; + +setSIWEBackupSecrets: (siweBackupSecrets: Object) => Promise; + +getSIWEBackupSecrets: () => Promise; } export interface CoreModuleSpec extends Spec { @@ -171,6 +174,10 @@ sessionVersion: number, overwrite: boolean, ) => Promise; + +setSIWEBackupSecrets: ( + siweBackupSecrets: SIWEBackupSecrets, + ) => Promise; + +getSIWEBackupSecrets: () => Promise; } export default (TurboModuleRegistry.getEnforcing(