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 @@ -24,6 +24,8 @@ CommSecureStore secureStore; const std::string secureStoreAccountDataKey = "cryptoAccountDataKey"; const std::string publicCryptoAccountID = "publicCryptoAccountID"; + const std::string keyserverHostedContentID = "keyserverHostedContentID"; + const std::string initialEncryptedMessageContent = "{\"type\": \"init\"}"; std::unique_ptr cryptoModule; DraftStore draftStore; ThreadStore threadStore; @@ -73,6 +75,12 @@ jsi::String prekey, jsi::String prekeySignature, jsi::String oneTimeKeys) override; + virtual jsi::Value initializeContentSession( + jsi::Runtime &rt, + jsi::String identityKeys, + jsi::String prekey, + jsi::String prekeySignature, + jsi::String oneTimeKeys) override; virtual jsi::Value isNotificationsSessionInitialized(jsi::Runtime &rt) override; virtual void terminate(jsi::Runtime &rt) override; 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 @@ -650,6 +650,63 @@ }); } +jsi::Value CommCoreModule::initializeContentSession( + jsi::Runtime &rt, + jsi::String identityKeys, + jsi::String prekey, + jsi::String prekeySignature, + jsi::String oneTimeKeys) { + auto identityKeysCpp{identityKeys.utf8(rt)}; + auto prekeyCpp{prekey.utf8(rt)}; + auto prekeySignatureCpp{prekeySignature.utf8(rt)}; + auto oneTimeKeysCpp{oneTimeKeys.utf8(rt)}; + return createPromiseAsJSIValue( + rt, [=](jsi::Runtime &innerRt, std::shared_ptr promise) { + taskType job = [=, &innerRt]() { + std::string error; + crypto::EncryptedData result; + folly::Optional storedSecretKey = + this->secureStore.get(this->secureStoreAccountDataKey); + if (!storedSecretKey.hasValue()) { + error = "storedSecretKey is not available"; + } else if (this->cryptoModule == nullptr) { + error = "user has not been initialized"; + } else { + try { + this->cryptoModule->initializeOutboundForSendingSession( + this->keyserverHostedContentID, + std::vector( + identityKeysCpp.begin(), identityKeysCpp.end()), + std::vector(prekeyCpp.begin(), prekeyCpp.end()), + std::vector( + prekeySignatureCpp.begin(), prekeySignatureCpp.end()), + std::vector( + oneTimeKeysCpp.begin(), oneTimeKeysCpp.end())); + result = this->cryptoModule->encrypt( + this->keyserverHostedContentID, + this->initialEncryptedMessageContent); + crypto::Persist newPersist = + this->cryptoModule->storeAsB64(storedSecretKey.value()); + DatabaseManager::getQueryExecutor().storeOlmPersistData( + newPersist); + } catch (const std::exception &e) { + error = e.what(); + } + } + this->jsInvoker_->invokeAsync([=, &innerRt]() { + if (error.size()) { + promise->reject(error); + return; + } + promise->resolve(jsi::String::createFromUtf8( + innerRt, + std::string{result.message.begin(), result.message.end()})); + }); + }; + this->cryptoThread->scheduleTask(job); + }); +} + jsi::Value CommCoreModule::isNotificationsSessionInitialized(jsi::Runtime &rt) { return createPromiseAsJSIValue( rt, [=](jsi::Runtime &innerRt, std::shared_ptr promise) { 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_initializeNotificationsSession(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { return static_cast(&turboModule)->initializeNotificationsSession(rt, args[0].asString(rt), args[1].asString(rt), args[2].asString(rt), args[3].asString(rt)); } +static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_initializeContentSession(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + return static_cast(&turboModule)->initializeContentSession(rt, args[0].asString(rt), args[1].asString(rt), args[2].asString(rt), args[3].asString(rt)); +} static jsi::Value __hostFunction_CommCoreModuleSchemaCxxSpecJSI_isNotificationsSessionInitialized(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { return static_cast(&turboModule)->isNotificationsSessionInitialized(rt); } @@ -154,6 +157,7 @@ methodMap_["getNotificationsOneTimeKeys"] = MethodMetadata {1, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_getNotificationsOneTimeKeys}; methodMap_["generateAndGetPrekeys"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_generateAndGetPrekeys}; methodMap_["initializeNotificationsSession"] = MethodMetadata {4, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_initializeNotificationsSession}; + methodMap_["initializeContentSession"] = MethodMetadata {4, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_initializeContentSession}; methodMap_["isNotificationsSessionInitialized"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_isNotificationsSessionInitialized}; methodMap_["getCodeVersion"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_getCodeVersion}; methodMap_["terminate"] = MethodMetadata {0, __hostFunction_CommCoreModuleSchemaCxxSpecJSI_terminate}; 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 getNotificationsOneTimeKeys(jsi::Runtime &rt, double oneTimeKeysAmount) = 0; virtual jsi::Value generateAndGetPrekeys(jsi::Runtime &rt) = 0; virtual jsi::Value initializeNotificationsSession(jsi::Runtime &rt, jsi::String identityKeys, jsi::String prekey, jsi::String prekeySignature, jsi::String oneTimeKeys) = 0; + virtual jsi::Value initializeContentSession(jsi::Runtime &rt, jsi::String identityKeys, jsi::String prekey, jsi::String prekeySignature, jsi::String oneTimeKeys) = 0; virtual jsi::Value isNotificationsSessionInitialized(jsi::Runtime &rt) = 0; virtual double getCodeVersion(jsi::Runtime &rt) = 0; virtual void terminate(jsi::Runtime &rt) = 0; @@ -239,6 +240,14 @@ return bridging::callFromJs( rt, &T::initializeNotificationsSession, jsInvoker_, instance_, std::move(identityKeys), std::move(prekey), std::move(prekeySignature), std::move(oneTimeKeys)); } + jsi::Value initializeContentSession(jsi::Runtime &rt, jsi::String identityKeys, jsi::String prekey, jsi::String prekeySignature, jsi::String oneTimeKeys) override { + static_assert( + bridging::getParameterCount(&T::initializeContentSession) == 5, + "Expected initializeContentSession(...) to have 5 parameters"); + + return bridging::callFromJs( + rt, &T::initializeContentSession, jsInvoker_, instance_, std::move(identityKeys), std::move(prekey), std::move(prekeySignature), std::move(oneTimeKeys)); + } jsi::Value isNotificationsSessionInitialized(jsi::Runtime &rt) override { static_assert( bridging::getParameterCount(&T::isNotificationsSessionInitialized) == 1, diff --git a/native/schema/CommCoreModuleSchema.js b/native/schema/CommCoreModuleSchema.js --- a/native/schema/CommCoreModuleSchema.js +++ b/native/schema/CommCoreModuleSchema.js @@ -84,6 +84,12 @@ prekeySignature: string, oneTimeKeys: string, ) => Promise; + +initializeContentSession: ( + identityKeys: string, + prekey: string, + prekeySignature: string, + oneTimeKeys: string, + ) => Promise; +isNotificationsSessionInitialized: () => Promise; +getCodeVersion: () => number; +terminate: () => void;