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 @@ -82,6 +82,7 @@ jsi::String authUserID, jsi::String authDeviceID, jsi::String authAccessToken) override; + virtual jsi::Value validateAndGetPrekeys(jsi::Runtime &rt) override; virtual jsi::Value initializeNotificationsSession( jsi::Runtime &rt, jsi::String identityKeys, 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 @@ -675,6 +675,18 @@ }); } +std::pair getNotificationsPrekeyAndSignature() { + // TODO: Implement notifs prekey rotation. + // Notifications prekey is not rotated at this moment. It + // is fetched with signature to match identity service API. + std::string notificationsPrekey = + NotificationsCryptoModule::getNotificationsPrekey("Comm"); + std::string notificationsPrekeySignature = + NotificationsCryptoModule::getNotificationsPrekeySignature("Comm"); + + return std::make_pair(notificationsPrekey, notificationsPrekeySignature); +} + jsi::Value CommCoreModule::validateAndUploadPrekeys( jsi::Runtime &rt, jsi::String authUserID, @@ -691,9 +703,8 @@ try { maybePrekeyToUpload = this->cryptoModule->validatePrekey(); - if (maybePrekeyToUpload.has_value()) { - this->persistCryptoModule(); - } else { + this->persistCryptoModule(); + if (!maybePrekeyToUpload.has_value()) { maybePrekeyToUpload = this->cryptoModule->getUnpublishedPrekey(); } } catch (const std::exception &e) { @@ -716,14 +727,10 @@ try { std::string prekeySignature = this->cryptoModule->getPrekeySignature(); - // TODO: Implement notifs prekey rotation. - // Notifications prekey is not rotated at this moment. It - // is fetched with signature to match identity service API. - std::string notificationsPrekey = - NotificationsCryptoModule::getNotificationsPrekey("Comm"); - std::string notificationsPrekeySignature = - NotificationsCryptoModule::getNotificationsPrekeySignature( - "Comm"); + std::string notificationsPrekey, notificationsPrekeySignature; + std::tie(notificationsPrekey, notificationsPrekeySignature) = + getNotificationsPrekeyAndSignature(); + try { std::promise prekeyPromise; std::future prekeyFuture = @@ -770,6 +777,71 @@ }); } +jsi::Value CommCoreModule::validateAndGetPrekeys(jsi::Runtime &rt) { + return createPromiseAsJSIValue( + rt, [=](jsi::Runtime &innerRt, std::shared_ptr promise) { + taskType job = [=, &innerRt]() { + std::string error; + std::string contentPrekey, contentPrekeySignature, notifPrekey, + notifPrekeySignature; + + if (this->cryptoModule == nullptr) { + error = "user has not been initialized"; + } else { + try { + if (auto validatedPrekey = this->cryptoModule->validatePrekey()) { + contentPrekey = validatedPrekey.value(); + } else if ( + auto unpublishedPrekey = + this->cryptoModule->getUnpublishedPrekey()) { + contentPrekey = unpublishedPrekey.value(); + } else { + contentPrekey = this->cryptoModule->getPrekey(); + } + this->persistCryptoModule(); + + contentPrekeySignature = this->cryptoModule->getPrekeySignature(); + + std::tie(notifPrekey, notifPrekeySignature) = + getNotificationsPrekeyAndSignature(); + + } catch (const std::exception &e) { + error = e.what(); + } + } + + this->jsInvoker_->invokeAsync([=, &innerRt]() { + if (error.size()) { + promise->reject(error); + return; + } + auto contentPrekeyJSI = + jsi::String::createFromUtf8(innerRt, contentPrekey); + auto contentPrekeySignatureJSI = + jsi::String::createFromUtf8(innerRt, contentPrekeySignature); + auto notifPrekeyJSI = + jsi::String::createFromUtf8(innerRt, notifPrekey); + auto notifPrekeySignatureJSI = + jsi::String::createFromUtf8(innerRt, notifPrekeySignature); + + auto signedPrekeysJSI = jsi::Object(innerRt); + signedPrekeysJSI.setProperty( + innerRt, "contentPrekey", contentPrekeyJSI); + signedPrekeysJSI.setProperty( + innerRt, "contentPrekeySignature", contentPrekeySignatureJSI); + signedPrekeysJSI.setProperty( + innerRt, "notifPrekey", notifPrekeyJSI); + signedPrekeysJSI.setProperty( + innerRt, "notifPrekeySignature", notifPrekeySignatureJSI); + + promise->resolve(std::move(signedPrekeysJSI)); + }); + }; + + this->cryptoThread->scheduleTask(job); + }); +} + jsi::Value CommCoreModule::initializeNotificationsSession( jsi::Runtime &rt, jsi::String identityKeys, 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 @@ -78,6 +78,9 @@ static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_generateAndGetPrekeys(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { return static_cast(&turboModule)->generateAndGetPrekeys(rt); } +static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_validateAndGetPrekeys(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + return static_cast(&turboModule)->validateAndGetPrekeys(rt); +} static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_validateAndUploadPrekeys(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { return static_cast(&turboModule)->validateAndUploadPrekeys(rt, args[0].asString(rt), args[1].asString(rt), args[2].asString(rt)); } @@ -184,6 +187,7 @@ methodMap_["getPrimaryOneTimeKeys"] = MethodMetadata {1, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_getPrimaryOneTimeKeys}; methodMap_["getNotificationsOneTimeKeys"] = MethodMetadata {1, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_getNotificationsOneTimeKeys}; methodMap_["generateAndGetPrekeys"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_generateAndGetPrekeys}; + methodMap_["validateAndGetPrekeys"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_validateAndGetPrekeys}; methodMap_["validateAndUploadPrekeys"] = MethodMetadata {3, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_validateAndUploadPrekeys}; methodMap_["initializeNotificationsSession"] = MethodMetadata {5, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_initializeNotificationsSession}; methodMap_["isNotificationsSessionInitialized"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_isNotificationsSessionInitialized}; 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 @@ -41,6 +41,7 @@ virtual jsi::Value getPrimaryOneTimeKeys(jsi::Runtime &rt, double oneTimeKeysAmount) = 0; virtual jsi::Value getNotificationsOneTimeKeys(jsi::Runtime &rt, double oneTimeKeysAmount) = 0; virtual jsi::Value generateAndGetPrekeys(jsi::Runtime &rt) = 0; + virtual jsi::Value validateAndGetPrekeys(jsi::Runtime &rt) = 0; virtual jsi::Value validateAndUploadPrekeys(jsi::Runtime &rt, jsi::String authUserID, jsi::String authDeviceID, jsi::String authAccessToken) = 0; virtual jsi::Value initializeNotificationsSession(jsi::Runtime &rt, jsi::String identityKeys, jsi::String prekey, jsi::String prekeySignature, jsi::String oneTimeKeys, jsi::String keyserverID) = 0; virtual jsi::Value isNotificationsSessionInitialized(jsi::Runtime &rt) = 0; @@ -256,6 +257,14 @@ return bridging::callFromJs( rt, &T::generateAndGetPrekeys, jsInvoker_, instance_); } + jsi::Value validateAndGetPrekeys(jsi::Runtime &rt) override { + static_assert( + bridging::getParameterCount(&T::validateAndGetPrekeys) == 1, + "Expected validateAndGetPrekeys(...) to have 1 parameters"); + + return bridging::callFromJs( + rt, &T::validateAndGetPrekeys, jsInvoker_, instance_); + } jsi::Value validateAndUploadPrekeys(jsi::Runtime &rt, jsi::String authUserID, jsi::String authDeviceID, jsi::String authAccessToken) override { static_assert( bridging::getParameterCount(&T::validateAndUploadPrekeys) == 4, diff --git a/native/schema/CommCoreModuleSchema.js b/native/schema/CommCoreModuleSchema.js --- a/native/schema/CommCoreModuleSchema.js +++ b/native/schema/CommCoreModuleSchema.js @@ -86,6 +86,7 @@ oneTimeKeysAmount: number, ) => Promise; +generateAndGetPrekeys: () => Promise; + +validateAndGetPrekeys: () => Promise; +validateAndUploadPrekeys: ( authUserID: string, authDeviceID: string,