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 @@ -63,6 +63,7 @@ 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 generateNonce(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 @@ -4,9 +4,11 @@ #include "DatabaseManager.h" #include "DraftStoreOperations.h" #include "InternalModules/GlobalDBSingleton.h" +#include "InternalModules/RustPromiseManager.h" #include "MessageStoreOperations.h" #include "TerminateApp.h" #include "ThreadStoreOperations.h" +#include "lib.rs.h" #include #include @@ -1166,4 +1168,18 @@ DatabaseManager::reportDBOperationsFailure(); } +jsi::Value CommCoreModule::generateNonce(jsi::Runtime &rt) { + return createPromiseAsJSIValue( + rt, [this](jsi::Runtime &innerRt, std::shared_ptr promise) { + std::string error; + try { + auto currentID = RustPromiseManager::instance.addPromise( + promise, this->jsInvoker_, innerRt); + identityGenerateNonce(currentID); + } catch (const std::exception &e) { + error = e.what(); + }; + }); +} + } // 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 @@ -94,6 +94,9 @@ static_cast(&turboModule)->reportDBOperationsFailure(rt); return jsi::Value::undefined(); } +static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_generateNonce(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + return static_cast(&turboModule)->generateNonce(rt); +} CommCoreModuleSchemaCxxSpecJSI::CommCoreModuleSchemaCxxSpecJSI(std::shared_ptr jsInvoker) : TurboModule("CommTurboModule", jsInvoker) { @@ -123,6 +126,7 @@ methodMap_["clearSensitiveData"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_clearSensitiveData}; methodMap_["checkIfDatabaseNeedsDeletion"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_checkIfDatabaseNeedsDeletion}; methodMap_["reportDBOperationsFailure"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_reportDBOperationsFailure}; + methodMap_["generateNonce"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_generateNonce}; } 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 @@ -46,6 +46,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 generateNonce(jsi::Runtime &rt) = 0; }; @@ -275,6 +276,14 @@ return bridging::callFromJs( rt, &T::reportDBOperationsFailure, jsInvoker_, instance_); } + jsi::Value generateNonce(jsi::Runtime &rt) override { + static_assert( + bridging::getParameterCount(&T::generateNonce) == 1, + "Expected generateNonce(...) to have 1 parameters"); + + return bridging::callFromJs( + rt, &T::generateNonce, jsInvoker_, instance_); + } private: T *instance_; 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 @@ -1,3 +1,5 @@ +use crate::ffi::string_callback; +use crate::identity::Empty; use lazy_static::lazy_static; use std::sync::Arc; use tokio::runtime::{Builder, Runtime}; @@ -89,6 +91,9 @@ notif_onetime_keys: Vec, ) -> Result; + #[cxx_name = "identityGenerateNonce"] + fn generate_nonce(counter: u32); + // Tunnelbroker Service Client type TunnelbrokerClient; @@ -98,6 +103,34 @@ // Crypto Tools fn generate_device_id(device_type: DeviceType) -> Result; } + + unsafe extern "C++" { + include!("RustCallback.h"); + #[namespace = "comm"] + #[cxx_name = "stringCallback"] + fn string_callback(error: String, counter: u32, ret: String); + } +} + +fn generate_nonce(counter: u32) { + RUNTIME.spawn(async move { + let mut identity_client = match IdentityClientServiceClient::connect( + "http://127.0.0.1:50054", + ) + .await + { + Ok(r) => r, + Err(e) => { + string_callback(e.to_string(), counter, "".to_string()); + return; + } + }; + let request = Empty {}; + match identity_client.generate_nonce(request).await { + Err(e) => string_callback(e.to_string(), counter, "".to_string()), + Ok(r) => string_callback("".to_string(), counter, r.into_inner().nonce), + }; + }); } #[derive(Debug)] diff --git a/native/schema/CommCoreModuleSchema.js b/native/schema/CommCoreModuleSchema.js --- a/native/schema/CommCoreModuleSchema.js +++ b/native/schema/CommCoreModuleSchema.js @@ -74,6 +74,7 @@ +clearSensitiveData: () => Promise; +checkIfDatabaseNeedsDeletion: () => boolean; +reportDBOperationsFailure: () => void; + +generateNonce: () => Promise; } export default (TurboModuleRegistry.getEnforcing(