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 @@ -32,6 +32,8 @@ ReportStore reportStore; UserStore userStore; + void persistOlmAccount(std::shared_ptr promise); + virtual jsi::Value getDraft(jsi::Runtime &rt, jsi::String key) override; virtual jsi::Value updateDraft(jsi::Runtime &rt, jsi::String key, jsi::String text) 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 @@ -294,6 +294,36 @@ TerminateApp::terminate(); } +void CommCoreModule::persistOlmAccount(std::shared_ptr promise) { + folly::Optional storedSecretKey = + this->secureStore.get(this->secureStoreAccountDataKey); + if (!storedSecretKey.hasValue()) { + storedSecretKey = crypto::Tools::generateRandomString(64); + this->secureStore.set( + this->secureStoreAccountDataKey, storedSecretKey.value()); + } + + crypto::Persist newPersist = + this->cryptoModule->storeAsB64(storedSecretKey.value()); + GlobalDBSingleton::instance.scheduleOrRunCancellable( + [=]() { + std::string error; + try { + DatabaseManager::getQueryExecutor().storeOlmPersistData(newPersist); + } catch (std::system_error &e) { + error = e.what(); + } + this->jsInvoker_->invokeAsync([=]() { + if (error.size()) { + promise->reject(error); + return; + } + }); + }, + promise, + this->jsInvoker_); +} + jsi::Value CommCoreModule::initializeCryptoAccount(jsi::Runtime &rt) { folly::Optional storedSecretKey = this->secureStore.get(this->secureStoreAccountDataKey);