| 590 | } |
| 591 | |
| 592 | jsi::Value CommCoreModule::initializeCryptoAccount(jsi::Runtime &rt) { |
| 593 | folly::Optional<std::string> storedSecretKey = |
| 594 | CommSecureStore::get(this->secureStoreAccountDataKey); |
| 595 | if (!storedSecretKey.hasValue()) { |
| 596 | storedSecretKey = crypto::Tools::generateRandomString(64); |
| 597 | CommSecureStore::set( |
| 598 | this->secureStoreAccountDataKey, storedSecretKey.value()); |
| 599 | } |
| 600 | |
| 601 | return createPromiseAsJSIValue( |
| 602 | rt, [=](jsi::Runtime &innerRt, std::shared_ptr<Promise> promise) { |
| 603 | taskType job = [=]() { |
| 604 | crypto::Persist contentPersist; |
| 605 | crypto::Persist notifsPersist; |
| 606 | try { |
| 607 | std::optional<std::string> contentAccountData = |
| 608 | DatabaseManager::getQueryExecutor().getOlmPersistAccountData( |
| 609 | DatabaseManager::getQueryExecutor().getContentAccountID()); |
| 610 | if (contentAccountData.has_value()) { |
| 611 | contentPersist.account = crypto::OlmBuffer( |
| 612 | contentAccountData->begin(), contentAccountData->end()); |
| 613 | // handle sessions data |
| 614 | std::vector<OlmPersistSession> sessionsData = |
| 615 | DatabaseManager::getQueryExecutor() |
| 616 | .getOlmPersistSessionsData(); |
| 617 | for (OlmPersistSession &sessionsDataItem : sessionsData) { |
| 618 | crypto::OlmBuffer sessionDataBuffer( |
| 619 | sessionsDataItem.session_data.begin(), |
| 620 | sessionsDataItem.session_data.end()); |
| 621 | crypto::SessionPersist sessionPersist{ |
| 622 | sessionDataBuffer, sessionsDataItem.version}; |
| 623 | contentPersist.sessions.insert(std::make_pair( |
| 624 | sessionsDataItem.target_device_id, sessionPersist)); |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | std::optional<std::string> notifsAccountData = |
| 629 | DatabaseManager::getQueryExecutor().getOlmPersistAccountData( |
| 630 | DatabaseManager::getQueryExecutor().getNotifsAccountID()); |
| 631 | |
| 632 | if (notifsAccountData.has_value()) { |
| 633 | notifsPersist.account = crypto::OlmBuffer( |
| 634 | notifsAccountData->begin(), notifsAccountData->end()); |
| 635 | } |
| 636 | |
| 637 | } catch (std::exception &e) { |
| 638 | std::string error = e.what(); |
| 639 | this->jsInvoker_->invokeAsync([=]() { promise->reject(error); }); |
| 640 | return; |
| 641 | } |
| 642 | |
| 643 | taskType cryptoJob = [=]() { |
| 644 | std::string error; |
| 645 | this->contentCryptoModule.reset(new crypto::CryptoModule( |
| 646 | storedSecretKey.value(), contentPersist)); |
| 647 | |
| 648 | std::optional< |
| 649 | std::pair<std::shared_ptr<crypto::CryptoModule>, std::string>> |
nothing calls this directly
no test coverage detected