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 @@ -4,9 +4,11 @@ #include "../Tools/CommSecureStore.h" #include "../Tools/WorkerThread.h" #include "../_generated/commJSI.h" +#include "JSIRust.h" #include #include #include +#include namespace comm { @@ -64,6 +66,18 @@ virtual bool checkIfDatabaseNeedsDeletion(jsi::Runtime &rt) override; virtual void reportDBOperationsFailure(jsi::Runtime &rt) override; virtual jsi::Value generateNonce(jsi::Runtime &rt) override; + virtual jsi::Value registerUser( + jsi::Runtime &rt, + jsi::String username, + jsi::String password, + jsi::String keyPayload, + jsi::String keyPayloadSignature, + jsi::String contentPrekey, + jsi::String contentPrekeySignature, + jsi::String notifPrekey, + jsi::String notifPrekeySignature, + jsi::Array contentOneTimeKeys, + jsi::Array notifOneTimeKeys) 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 @@ -1249,4 +1249,52 @@ }); } +jsi::Value CommCoreModule::registerUser( + jsi::Runtime &rt, + jsi::String username, + jsi::String password, + jsi::String keyPayload, + jsi::String keyPayloadSignature, + jsi::String contentPrekey, + jsi::String contentPrekeySignature, + jsi::String notifPrekey, + jsi::String notifPrekeySignature, + jsi::Array contentOneTimeKeys, + jsi::Array notifOneTimeKeys) { + return createPromiseAsJSIValue( + rt, + [this, + &username, + &password, + &keyPayload, + &keyPayloadSignature, + &contentPrekey, + &contentPrekeySignature, + ¬ifPrekey, + ¬ifPrekeySignature, + &contentOneTimeKeys, + ¬ifOneTimeKeys]( + jsi::Runtime &innerRt, std::shared_ptr promise) { + std::string error; + try { + auto currentID = RustPromiseManager::instance.addPromise( + promise, this->jsInvoker_, innerRt); + identityRegisterUser( + jsiStringToRustString(username, innerRt), + jsiStringToRustString(password, innerRt), + jsiStringToRustString(keyPayload, innerRt), + jsiStringToRustString(keyPayloadSignature, innerRt), + jsiStringToRustString(contentPrekey, innerRt), + jsiStringToRustString(contentPrekeySignature, innerRt), + jsiStringToRustString(notifPrekey, innerRt), + jsiStringToRustString(notifPrekeySignature, innerRt), + jsiStringArrayToRustVec(contentOneTimeKeys, innerRt), + jsiStringArrayToRustVec(notifOneTimeKeys, innerRt), + currentID); + } catch (const std::exception &e) { + error = e.what(); + }; + }); +} + } // namespace comm diff --git a/native/cpp/CommonCpp/NativeModules/JSIRust.h b/native/cpp/CommonCpp/NativeModules/JSIRust.h new file mode 100644 --- /dev/null +++ b/native/cpp/CommonCpp/NativeModules/JSIRust.h @@ -0,0 +1,12 @@ +#pragma once + +#include +#include + +rust::String jsiStringToRustString( + const facebook::jsi::String &jsi_string, + facebook::jsi::Runtime &runtime); + +rust::Vec jsiStringArrayToRustVec( + const facebook::jsi::Array &jsi_array, + facebook::jsi::Runtime &runtime); diff --git a/native/cpp/CommonCpp/NativeModules/JSIRust.cpp b/native/cpp/CommonCpp/NativeModules/JSIRust.cpp new file mode 100644 --- /dev/null +++ b/native/cpp/CommonCpp/NativeModules/JSIRust.cpp @@ -0,0 +1,25 @@ +#include "JSIRust.h" +#include + +rust::String jsiStringToRustString( + const facebook::jsi::String &jsi_string, + facebook::jsi::Runtime &runtime) { + std::string std_string = jsi_string.utf8(runtime); + return rust::String(std_string); +} + +rust::Vec jsiStringArrayToRustVec( + const facebook::jsi::Array &jsi_array, + facebook::jsi::Runtime &runtime) { + rust::Vec rust_vec; + rust_vec.reserve(jsi_array.size(runtime)); + + for (size_t i = 0; i < jsi_array.size(runtime); ++i) { + facebook::jsi::String jsi_string = + jsi_array.getValueAtIndex(runtime, i).getString(runtime); + rust::String rust_string = jsiStringToRustString(jsi_string, runtime); + rust_vec.push_back(rust_string); + } + + return rust_vec; +} 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 @@ -97,6 +97,9 @@ static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_generateNonce(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { return static_cast(&turboModule)->generateNonce(rt); } +static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_registerUser(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + return static_cast(&turboModule)->registerUser(rt, args[0].asString(rt), args[1].asString(rt), args[2].asString(rt), args[3].asString(rt), args[4].asString(rt), args[5].asString(rt), args[6].asString(rt), args[7].asString(rt), args[8].asObject(rt).asArray(rt), args[9].asObject(rt).asArray(rt)); +} CommCoreModuleSchemaCxxSpecJSI::CommCoreModuleSchemaCxxSpecJSI(std::shared_ptr jsInvoker) : TurboModule("CommTurboModule", jsInvoker) { @@ -127,6 +130,7 @@ methodMap_["checkIfDatabaseNeedsDeletion"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_checkIfDatabaseNeedsDeletion}; methodMap_["reportDBOperationsFailure"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_reportDBOperationsFailure}; methodMap_["generateNonce"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_generateNonce}; + methodMap_["registerUser"] = MethodMetadata {10, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_registerUser}; } 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 @@ -47,6 +47,7 @@ virtual bool checkIfDatabaseNeedsDeletion(jsi::Runtime &rt) = 0; virtual void reportDBOperationsFailure(jsi::Runtime &rt) = 0; virtual jsi::Value generateNonce(jsi::Runtime &rt) = 0; + virtual jsi::Value registerUser(jsi::Runtime &rt, jsi::String username, jsi::String password, jsi::String keyPayload, jsi::String keyPayloadSignature, jsi::String contentPrekey, jsi::String contentPrekeySignature, jsi::String notifPrekey, jsi::String notifPrekeySignature, jsi::Array contentOneTimeKeys, jsi::Array notifOneTimeKeys) = 0; }; @@ -284,6 +285,14 @@ return bridging::callFromJs( rt, &T::generateNonce, jsInvoker_, instance_); } + jsi::Value registerUser(jsi::Runtime &rt, jsi::String username, jsi::String password, jsi::String keyPayload, jsi::String keyPayloadSignature, jsi::String contentPrekey, jsi::String contentPrekeySignature, jsi::String notifPrekey, jsi::String notifPrekeySignature, jsi::Array contentOneTimeKeys, jsi::Array notifOneTimeKeys) override { + static_assert( + bridging::getParameterCount(&T::registerUser) == 11, + "Expected registerUser(...) to have 11 parameters"); + + return bridging::callFromJs( + rt, &T::registerUser, jsInvoker_, instance_, std::move(username), std::move(password), std::move(keyPayload), std::move(keyPayloadSignature), std::move(contentPrekey), std::move(contentPrekeySignature), std::move(notifPrekey), std::move(notifPrekeySignature), std::move(contentOneTimeKeys), std::move(notifOneTimeKeys)); + } private: T *instance_; diff --git a/native/ios/Comm.xcodeproj/project.pbxproj b/native/ios/Comm.xcodeproj/project.pbxproj --- a/native/ios/Comm.xcodeproj/project.pbxproj +++ b/native/ios/Comm.xcodeproj/project.pbxproj @@ -45,6 +45,7 @@ 8B652FA6295EAA5B009F8163 /* RustCallback.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8B652FA5295EAA5B009F8163 /* RustCallback.cpp */; }; 8B99BAAC28D50F3000EB5ADB /* libnative_rust_library.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 8B99BAAB28D50F3000EB5ADB /* libnative_rust_library.a */; }; 8B99BAAE28D511FF00EB5ADB /* lib.rs.cc in Sources */ = {isa = PBXBuildFile; fileRef = 8B99BAAD28D511FF00EB5ADB /* lib.rs.cc */; }; + 8BC9568529FC49B00060AE4A /* JSIRust.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8BC9568429FC49B00060AE4A /* JSIRust.cpp */; }; 8E43C32C291E5B4A009378F5 /* TerminateApp.mm in Sources */ = {isa = PBXBuildFile; fileRef = 8E43C32B291E5B4A009378F5 /* TerminateApp.mm */; }; 8E86A6D329537EBB000BBE7D /* DatabaseManager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8E86A6D229537EBB000BBE7D /* DatabaseManager.cpp */; }; B71AFF1F265EDD8600B22352 /* IBMPlexSans-Medium.ttf in Resources */ = {isa = PBXBuildFile; fileRef = B71AFF1E265EDD8600B22352 /* IBMPlexSans-Medium.ttf */; }; @@ -184,6 +185,8 @@ 8B99B59928D50D4900EB5ADB /* cxx.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = cxx.h; sourceTree = ""; }; 8B99BAAB28D50F3000EB5ADB /* libnative_rust_library.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libnative_rust_library.a; path = ../native_rust_library/target/universal/release/libnative_rust_library.a; sourceTree = ""; }; 8B99BAAD28D511FF00EB5ADB /* lib.rs.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = lib.rs.cc; sourceTree = ""; }; + 8BC9568329FC49920060AE4A /* JSIRust.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSIRust.h; sourceTree = ""; }; + 8BC9568429FC49B00060AE4A /* JSIRust.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSIRust.cpp; sourceTree = ""; }; 8E43C32B291E5B4A009378F5 /* TerminateApp.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = TerminateApp.mm; path = Comm/TerminateApp.mm; sourceTree = ""; }; 8E43C32E291E5B9D009378F5 /* TerminateApp.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TerminateApp.h; path = ../cpp/CommonCpp/Tools/TerminateApp.h; sourceTree = ""; }; 8E86A6D229537EBB000BBE7D /* DatabaseManager.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = DatabaseManager.cpp; sourceTree = ""; }; @@ -377,6 +380,8 @@ CBED0E2C284E086100CD3863 /* PersistentStorageUtilities */, 726E5D722731A4240032361D /* InternalModules */, 71BE843C2636A944002849D2 /* CommCoreModule.cpp */, + 8BC9568429FC49B00060AE4A /* JSIRust.cpp */, + 8BC9568329FC49920060AE4A /* JSIRust.h */, 71BE843E2636A944002849D2 /* CommCoreModule.h */, 7FBB2A7329E944FD002C6493 /* CommUtilsModule.cpp */, 7FBB2A7429E9450E002C6493 /* CommUtilsModule.h */, @@ -979,6 +984,7 @@ 7F0C6E31291C4468002AA2D9 /* ExpoModulesProvider.swift in Sources */, 75291F0428F9A0D400F4C80E /* DeviceID.cpp in Sources */, 8E43C32C291E5B4A009378F5 /* TerminateApp.mm in Sources */, + 8BC9568529FC49B00060AE4A /* JSIRust.cpp in Sources */, CB38B48628771CDD00171182 /* TemporaryMessageStorage.mm in Sources */, CB38B48428771CAF00171182 /* EncryptedFileUtils.mm in Sources */, CBFE58292885852B003B94C9 /* ThreadOperations.cpp in Sources */, 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 @@ -49,7 +49,7 @@ #[cxx_name = "identityInitializeClient"] fn initialize_identity_client(addr: String) -> Box; - #[cxx_name = "registerUser"] + #[cxx_name = "identityRegisterUser"] fn register_user( username: String, password: String, diff --git a/native/schema/CommCoreModuleSchema.js b/native/schema/CommCoreModuleSchema.js --- a/native/schema/CommCoreModuleSchema.js +++ b/native/schema/CommCoreModuleSchema.js @@ -67,6 +67,18 @@ +checkIfDatabaseNeedsDeletion: () => boolean; +reportDBOperationsFailure: () => void; +generateNonce: () => Promise; + +registerUser: ( + username: string, + password: string, + keyPayload: string, + keyPayloadSignature: string, + contentPrekey: string, + contentPrekeySignature: string, + notifPrekey: string, + notifPrekeySignature: string, + contentOneTimeKeys: $ReadOnlyArray, + notifOneTimeKeys: $ReadOnlyArray, + ) => Promise; } export default (TurboModuleRegistry.getEnforcing(