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 @@ -79,6 +79,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, @@ -689,11 +701,17 @@ std::string error; std::optional maybePrekeyToUpload; + if (this->cryptoModule == nullptr) { + this->jsInvoker_->invokeAsync([=, &innerRt]() { + promise->reject("user has not been initialized"); + }); + return; + } + 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 +734,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 +784,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 contentPrekeySignature, notifPrekey, notifPrekeySignature; + std::optional contentPrekey; + + if (this->cryptoModule == nullptr) { + this->jsInvoker_->invokeAsync([=, &innerRt]() { + promise->reject("user has not been initialized"); + }); + return; + } + try { + contentPrekey = this->cryptoModule->validatePrekey(); + if (!contentPrekey) { + contentPrekey = this->cryptoModule->getUnpublishedPrekey(); + } + if (!contentPrekey) { + 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.value()); + 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 @@ -75,6 +75,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)); } @@ -180,6 +183,7 @@ methodMap_["getUserPublicKey"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_getUserPublicKey}; methodMap_["getOneTimeKeys"] = MethodMetadata {1, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_getOneTimeKeys}; 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 @@ -40,6 +40,7 @@ virtual jsi::Value getUserPublicKey(jsi::Runtime &rt) = 0; virtual jsi::Value getOneTimeKeys(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; @@ -247,6 +248,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 @@ +getUserPublicKey: () => Promise; +getOneTimeKeys: (oneTimeKeysAmount: number) => Promise; +generateAndGetPrekeys: () => Promise; + +validateAndGetPrekeys: () => Promise; +validateAndUploadPrekeys: ( authUserID: string, authDeviceID: string,