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 @@ -31,6 +31,8 @@ ReportStore reportStore; UserStore userStore; + void persistCryptoModule(); + 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,32 @@ TerminateApp::terminate(); } +void CommCoreModule::persistCryptoModule() { + folly::Optional storedSecretKey = + CommSecureStore::get(this->secureStoreAccountDataKey); + if (!storedSecretKey.hasValue()) { + storedSecretKey = crypto::Tools::generateRandomString(64); + CommSecureStore::set( + this->secureStoreAccountDataKey, storedSecretKey.value()); + } + + crypto::Persist newPersist = + this->cryptoModule->storeAsB64(storedSecretKey.value()); + + std::promise persistencePromise; + std::future persistenceFuture = persistencePromise.get_future(); + GlobalDBSingleton::instance.scheduleOrRunCancellable( + [=, &persistencePromise]() { + try { + DatabaseManager::getQueryExecutor().storeOlmPersistData(newPersist); + persistencePromise.set_value(); + } catch (std::system_error &e) { + persistencePromise.set_exception(std::make_exception_ptr(e)); + } + }); + persistenceFuture.get(); +} + jsi::Value CommCoreModule::initializeCryptoAccount(jsi::Runtime &rt) { folly::Optional storedSecretKey = CommSecureStore::get(this->secureStoreAccountDataKey);