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 @@ -89,6 +89,8 @@ jsi::Runtime &rt, jsi::String password, jsi::String backupID) override; + virtual jsi::Value + generateRandomString(jsi::Runtime &rt, double size) 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 @@ -845,4 +845,33 @@ }); } +jsi::Value CommCoreModule::generateRandomString(jsi::Runtime &rt, double size) { + return createPromiseAsJSIValue( + rt, + [this, &size](jsi::Runtime &innerRt, std::shared_ptr promise) { + taskType job = [this, &innerRt, &size, promise]() { + std::string error; + std::string randomString; + try { + randomString = crypto::Tools::generateRandomString(size); + } catch (const std::exception &e) { + error = "Failed to generate random string for size " + + std::to_string(size) + ": " + e.what(); + } + + this->jsInvoker_->invokeAsync( + [&innerRt, error, randomString, promise]() { + if (error.size()) { + promise->reject(error); + } else { + jsi::String jsiRandomString = + jsi::String::createFromUtf8(innerRt, randomString); + promise->resolve(std::move(jsiRandomString)); + } + }); + }; + 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 @@ -110,6 +110,9 @@ 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)); } +static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_generateRandomString(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + return static_cast(&turboModule)->generateRandomString(rt, args[0].asNumber()); +} CommCoreModuleSchemaCxxSpecJSI::CommCoreModuleSchemaCxxSpecJSI(std::shared_ptr jsInvoker) : TurboModule("CommTurboModule", jsInvoker) { @@ -144,6 +147,7 @@ methodMap_["checkIfDatabaseNeedsDeletion"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_checkIfDatabaseNeedsDeletion}; methodMap_["reportDBOperationsFailure"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_reportDBOperationsFailure}; methodMap_["computeBackupKey"] = MethodMetadata {2, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_computeBackupKey}; + methodMap_["generateRandomString"] = MethodMetadata {1, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_generateRandomString}; } 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 @@ -51,6 +51,7 @@ 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; + virtual jsi::Value generateRandomString(jsi::Runtime &rt, double size) = 0; }; @@ -320,6 +321,14 @@ return bridging::callFromJs( rt, &T::computeBackupKey, jsInvoker_, instance_, std::move(password), std::move(backupID)); } + jsi::Value generateRandomString(jsi::Runtime &rt, double size) override { + static_assert( + bridging::getParameterCount(&T::generateRandomString) == 2, + "Expected generateRandomString(...) to have 2 parameters"); + + return bridging::callFromJs( + rt, &T::generateRandomString, jsInvoker_, instance_, std::move(size)); + } 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 @@ -80,6 +80,7 @@ +checkIfDatabaseNeedsDeletion: () => boolean; +reportDBOperationsFailure: () => void; +computeBackupKey: (password: string, backupID: string) => Promise; + +generateRandomString: (size: number) => Promise; } export interface CoreModuleSpec extends Spec {