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 @@ -1168,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/Cargo.lock b/native/native_rust_library/Cargo.lock --- a/native/native_rust_library/Cargo.lock +++ b/native/native_rust_library/Cargo.lock @@ -217,6 +217,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc" +[[package]] +name = "convert_case" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e" + [[package]] name = "cpufeatures" version = "0.2.5" @@ -348,6 +354,19 @@ "syn", ] +[[package]] +name = "derive_more" +version = "0.99.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fb810d30a7c1953f91334de7244731fc3f3c10d7fe163338a35b9f640960321" +dependencies = [ + "convert_case", + "proc-macro2", + "quote", + "rustc_version", + "syn", +] + [[package]] name = "digest" version = "0.9.0" @@ -768,6 +787,7 @@ "comm-opaque2", "cxx", "cxx-build", + "derive_more", "lazy_static", "opaque-ke 1.2.0", "prost", @@ -1053,6 +1073,15 @@ "winapi", ] +[[package]] +name = "rustc_version" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366" +dependencies = [ + "semver", +] + [[package]] name = "scratch" version = "1.0.2" @@ -1072,6 +1101,12 @@ "zeroize", ] +[[package]] +name = "semver" +version = "1.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bebd363326d05ec3e2f532ab7660680f3b02130d780c299bca73469d521bc0ed" + [[package]] name = "serde" version = "1.0.144" diff --git a/native/native_rust_library/Cargo.toml b/native/native_rust_library/Cargo.toml --- a/native/native_rust_library/Cargo.toml +++ b/native/native_rust_library/Cargo.toml @@ -18,6 +18,7 @@ tracing = "0.1" regex = "1.6" comm-opaque2 = {path = "../../shared/comm-opaque2"} +derive_more = "0.99" [build-dependencies] cxx-build = "1.0" 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 @@ -91,6 +91,9 @@ notif_onetime_keys: Vec, ) -> Result; + #[cxx_name = "identityGenerateNonce"] + fn generate_nonce(promise_id: u32); + // Tunnelbroker Service Client type TunnelbrokerClient; @@ -105,10 +108,40 @@ include!("RustCallback.h"); #[namespace = "comm"] #[cxx_name = "stringCallback"] - fn string_callback(error: String, counter: u32, ret: String); + fn string_callback(error: String, promise_id: u32, ret: String); } } +fn handle_string_result_as_callback( + result: Result, + promise_id: u32, +) where + E: std::fmt::Display, +{ + match result { + Err(e) => string_callback(e.to_string(), promise_id, "".to_string()), + Ok(r) => string_callback("".to_string(), promise_id, r), + } +} + +fn generate_nonce(promise_id: u32) { + RUNTIME.spawn(async move { + let result = fetch_nonce().await; + handle_string_result_as_callback(result, promise_id); + }); +} + +async fn fetch_nonce() -> Result { + let mut identity_client = + IdentityClientServiceClient::connect("http://127.0.0.1:50054").await?; + let nonce = identity_client + .generate_nonce(Empty {}) + .await? + .into_inner() + .nonce; + Ok(nonce) +} + #[derive(Debug)] pub struct IdentityClient { identity_client: IdentityClientServiceClient, @@ -221,3 +254,13 @@ .expect("Failed to create Tokio runtime for the Tunnelbroker client"), }) } + +#[derive( + Debug, derive_more::Display, derive_more::From, derive_more::Error, +)] +pub enum Error { + #[display(...)] + TonicGRPC(Status), + #[display(...)] + TonicTransport(tonic::transport::Error), +} 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(