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 @@ -85,6 +85,10 @@ virtual jsi::Value clearSensitiveData(jsi::Runtime &rt) override; virtual bool checkIfDatabaseNeedsDeletion(jsi::Runtime &rt) override; virtual void reportDBOperationsFailure(jsi::Runtime &rt) override; + virtual jsi::Value computeBackupKey( + jsi::Runtime &rt, + jsi::String password, + jsi::String backupID) 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 @@ -12,6 +12,9 @@ #include #include +#include "lib.rs.h" +#include + namespace comm { using namespace facebook::react; @@ -804,4 +807,42 @@ DatabaseManager::reportDBOperationsFailure(); } +jsi::Value CommCoreModule::computeBackupKey( + jsi::Runtime &rt, + jsi::String password, + jsi::String backupID) { + std::string passwordStr = password.utf8(rt); + std::string backupIDStr = backupID.utf8(rt); + return createPromiseAsJSIValue( + rt, [=](jsi::Runtime &innerRt, std::shared_ptr promise) { + taskType job = [=, &innerRt]() { + std::string error; + std::array<::std::uint8_t, 32> backupKey; + try { + backupKey = compute_backup_key(passwordStr, backupIDStr); + } catch (const std::exception &e) { + error = std::string{"Failed to compute backup key: "} + e.what(); + } + + this->jsInvoker_->invokeAsync([=, &innerRt]() { + if (error.size()) { + promise->reject(error); + return; + } + auto size = backupKey.size(); + auto arrayBuffer = + innerRt.global() + .getPropertyAsFunction(innerRt, "ArrayBuffer") + .callAsConstructor(innerRt, {static_cast(size)}) + .asObject(innerRt) + .getArrayBuffer(innerRt); + auto bufferPtr = arrayBuffer.data(innerRt); + memcpy(bufferPtr, backupKey.data(), size); + promise->resolve(std::move(arrayBuffer)); + }); + }; + this->cryptoThread->scheduleTask(job); + }); +} + } // 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 @@ -107,6 +107,9 @@ static_cast(&turboModule)->reportDBOperationsFailure(rt); return jsi::Value::undefined(); } +static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_computeBackupKey(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + return static_cast(&turboModule)->computeBackupKey(rt, args[0].asString(rt), args[1].asString(rt)); +} CommCoreModuleSchemaCxxSpecJSI::CommCoreModuleSchemaCxxSpecJSI(std::shared_ptr jsInvoker) : TurboModule("CommTurboModule", jsInvoker) { @@ -140,6 +143,7 @@ methodMap_["clearSensitiveData"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_clearSensitiveData}; methodMap_["checkIfDatabaseNeedsDeletion"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_checkIfDatabaseNeedsDeletion}; methodMap_["reportDBOperationsFailure"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_reportDBOperationsFailure}; + methodMap_["computeBackupKey"] = MethodMetadata {2, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_computeBackupKey}; } 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 @@ -50,6 +50,7 @@ virtual jsi::Value clearSensitiveData(jsi::Runtime &rt) = 0; virtual bool checkIfDatabaseNeedsDeletion(jsi::Runtime &rt) = 0; virtual void reportDBOperationsFailure(jsi::Runtime &rt) = 0; + virtual jsi::Value computeBackupKey(jsi::Runtime &rt, jsi::String password, jsi::String backupID) = 0; }; @@ -311,6 +312,14 @@ return bridging::callFromJs( rt, &T::reportDBOperationsFailure, jsInvoker_, instance_); } + jsi::Value computeBackupKey(jsi::Runtime &rt, jsi::String password, jsi::String backupID) override { + static_assert( + bridging::getParameterCount(&T::computeBackupKey) == 3, + "Expected computeBackupKey(...) to have 3 parameters"); + + return bridging::callFromJs( + rt, &T::computeBackupKey, jsInvoker_, instance_, std::move(password), std::move(backupID)); + } private: T *instance_; diff --git a/native/native-modules.js b/native/native-modules.js --- a/native/native-modules.js +++ b/native/native-modules.js @@ -1,6 +1,6 @@ // @flow -import type { Spec as CoreModuleSpec } from './schema/CommCoreModuleSchema.js'; +import type { CoreModuleSpec } from './schema/CommCoreModuleSchema.js'; import type { Spec as RustModuleSpec } from './schema/CommRustModuleSchema.js'; import type { UtilsModuleSpec } from './schema/CommUtilsModuleSchema.js'; diff --git a/native/schema/CommCoreModuleSchema.js b/native/schema/CommCoreModuleSchema.js --- a/native/schema/CommCoreModuleSchema.js +++ b/native/schema/CommCoreModuleSchema.js @@ -27,7 +27,7 @@ +signature: string, }; -export interface Spec extends TurboModule { +interface Spec extends TurboModule { +getDraft: (key: string) => Promise; +updateDraft: (key: string, text: string) => Promise; +moveDraft: (oldKey: string, newKey: string) => Promise; @@ -79,6 +79,14 @@ +clearSensitiveData: () => Promise; +checkIfDatabaseNeedsDeletion: () => boolean; +reportDBOperationsFailure: () => void; + +computeBackupKey: (password: string, backupID: string) => Promise; +} + +export interface CoreModuleSpec extends Spec { + +computeBackupKey: ( + password: string, + backupID: string, + ) => Promise; } export default (TurboModuleRegistry.getEnforcing(