Changeset View
Changeset View
Standalone View
Standalone View
native/cpp/CommonCpp/NativeModules/CommCoreModule.cpp
#include "CommCoreModule.h" | #include "CommCoreModule.h" | ||||
#include "../CryptoTools/DeviceID.h" | #include "../CryptoTools/DeviceID.h" | ||||
#include "../Notifications/BackgroundDataStorage/NotificationsCryptoModule.h" | #include "../Notifications/BackgroundDataStorage/NotificationsCryptoModule.h" | ||||
#include "../Tools/CommSecureStore.h" | |||||
#include "DatabaseManager.h" | #include "DatabaseManager.h" | ||||
#include "DraftStoreOperations.h" | #include "DraftStoreOperations.h" | ||||
#include "InternalModules/GlobalDBSingleton.h" | #include "InternalModules/GlobalDBSingleton.h" | ||||
#include "MessageStoreOperations.h" | #include "MessageStoreOperations.h" | ||||
#include "TerminateApp.h" | #include "TerminateApp.h" | ||||
#include "ThreadStoreOperations.h" | #include "ThreadStoreOperations.h" | ||||
#include <ReactCommon/TurboModuleUtils.h> | #include <ReactCommon/TurboModuleUtils.h> | ||||
#include <folly/Optional.h> | |||||
#include <folly/dynamic.h> | #include <folly/dynamic.h> | ||||
#include <folly/json.h> | #include <folly/json.h> | ||||
#include <future> | #include <future> | ||||
namespace comm { | namespace comm { | ||||
using namespace facebook::react; | using namespace facebook::react; | ||||
▲ Show 20 Lines • Show All 795 Lines • ▼ Show 20 Lines | return createPromiseAsJSIValue( | ||||
}); | }); | ||||
}; | }; | ||||
GlobalDBSingleton::instance.scheduleOrRunCancellable( | GlobalDBSingleton::instance.scheduleOrRunCancellable( | ||||
job, promise, this->jsInvoker_); | job, promise, this->jsInvoker_); | ||||
}); | }); | ||||
} | } | ||||
jsi::Value CommCoreModule::getUserPublicKey(jsi::Runtime &rt) { | jsi::Value CommCoreModule::getUserPublicKey(jsi::Runtime &rt) { | ||||
CommSecureStore secureStore{}; | |||||
folly::Optional<std::string> picklingKey = secureStore.get( | |||||
NotificationsCryptoModule::secureStoreNotificationsAccountDataKey); | |||||
return createPromiseAsJSIValue( | return createPromiseAsJSIValue( | ||||
rt, [=](jsi::Runtime &innerRt, std::shared_ptr<Promise> promise) { | rt, [=](jsi::Runtime &innerRt, std::shared_ptr<Promise> promise) { | ||||
taskType job = [=, &innerRt]() { | taskType job = [=, &innerRt]() { | ||||
std::string error; | std::string error; | ||||
if (!picklingKey.hasValue()) { | |||||
error = | |||||
"Attempt to retrieve notifications crypto account before it " | |||||
"was correctly initialized."; | |||||
} | |||||
std::string notificationsCurve25519Cpp, notificationsEd25519Cpp, | |||||
blobPayloadCpp, signatureCpp, primaryCurve25519Cpp, | |||||
primaryEd25519Cpp; | |||||
if (!error.size()) { | |||||
std::string primaryKeysResult; | std::string primaryKeysResult; | ||||
std::string notificationsKeysResult; | std::string notificationsKeysResult; | ||||
if (this->cryptoModule == nullptr) { | if (this->cryptoModule == nullptr) { | ||||
error = "user has not been initialized"; | error = "user has not been initialized"; | ||||
} else { | } else { | ||||
primaryKeysResult = this->cryptoModule->getIdentityKeys(); | primaryKeysResult = this->cryptoModule->getIdentityKeys(); | ||||
} | } | ||||
try { | try { | ||||
std::string unwrappedPicklingKey = picklingKey.value(); | |||||
if (!error.size()) { | if (!error.size()) { | ||||
notificationsKeysResult = | notificationsKeysResult = | ||||
NotificationsCryptoModule::getNotificationsIdentityKeys(); | NotificationsCryptoModule::getNotificationsIdentityKeys( | ||||
unwrappedPicklingKey); | |||||
} | } | ||||
} catch (const std::exception &e) { | } catch (const std::exception &e) { | ||||
error = e.what(); | error = e.what(); | ||||
} | } | ||||
std::string notificationsCurve25519Cpp, notificationsEd25519Cpp, | |||||
blobPayloadCpp, signatureCpp, primaryCurve25519Cpp, | |||||
primaryEd25519Cpp; | |||||
if (!error.size()) { | if (!error.size()) { | ||||
folly::dynamic parsedPrimary; | folly::dynamic parsedPrimary; | ||||
try { | try { | ||||
parsedPrimary = folly::parseJson(primaryKeysResult); | parsedPrimary = folly::parseJson(primaryKeysResult); | ||||
} catch (const folly::json::parse_error &e) { | } catch (const folly::json::parse_error &e) { | ||||
error = | error = "parsing identity keys failed with: " + | ||||
"parsing identity keys failed with: " + std::string(e.what()); | std::string(e.what()); | ||||
} | } | ||||
if (!error.size()) { | if (!error.size()) { | ||||
primaryCurve25519Cpp = parsedPrimary["curve25519"].asString(); | primaryCurve25519Cpp = parsedPrimary["curve25519"].asString(); | ||||
primaryEd25519Cpp = parsedPrimary["ed25519"].asString(); | primaryEd25519Cpp = parsedPrimary["ed25519"].asString(); | ||||
folly::dynamic parsedNotifications; | folly::dynamic parsedNotifications; | ||||
try { | try { | ||||
parsedNotifications = folly::parseJson(notificationsKeysResult); | parsedNotifications = | ||||
folly::parseJson(notificationsKeysResult); | |||||
} catch (const folly::json::parse_error &e) { | } catch (const folly::json::parse_error &e) { | ||||
error = "parsing notifications keys failed with: " + | error = "parsing notifications keys failed with: " + | ||||
std::string(e.what()); | std::string(e.what()); | ||||
} | } | ||||
if (!error.size()) { | if (!error.size()) { | ||||
notificationsCurve25519Cpp = | notificationsCurve25519Cpp = | ||||
parsedNotifications["curve25519"].asString(); | parsedNotifications["curve25519"].asString(); | ||||
notificationsEd25519Cpp = | notificationsEd25519Cpp = | ||||
parsedNotifications["ed25519"].asString(); | parsedNotifications["ed25519"].asString(); | ||||
folly::dynamic blobPayloadJSON = folly::dynamic::object( | folly::dynamic blobPayloadJSON = folly::dynamic::object( | ||||
"primaryIdentityPublicKeys", | "primaryIdentityPublicKeys", | ||||
folly::dynamic::object("ed25519", primaryEd25519Cpp)( | folly::dynamic::object("ed25519", primaryEd25519Cpp)( | ||||
"curve25519", primaryCurve25519Cpp))( | "curve25519", primaryCurve25519Cpp))( | ||||
"notificationIdentityPublicKeys", | "notificationIdentityPublicKeys", | ||||
folly::dynamic::object("ed25519", notificationsEd25519Cpp)( | folly::dynamic::object( | ||||
"ed25519", notificationsEd25519Cpp)( | |||||
"curve25519", notificationsCurve25519Cpp)); | "curve25519", notificationsCurve25519Cpp)); | ||||
blobPayloadCpp = folly::toJson(blobPayloadJSON); | blobPayloadCpp = folly::toJson(blobPayloadJSON); | ||||
signatureCpp = this->cryptoModule->signMessage(blobPayloadCpp); | signatureCpp = | ||||
this->cryptoModule->signMessage(blobPayloadCpp); | |||||
} | |||||
} | } | ||||
} | } | ||||
} | } | ||||
this->jsInvoker_->invokeAsync([=, &innerRt]() { | this->jsInvoker_->invokeAsync([=, &innerRt]() { | ||||
if (error.size()) { | if (error.size()) { | ||||
promise->reject(error); | promise->reject(error); | ||||
return; | return; | ||||
▲ Show 20 Lines • Show All 280 Lines • Show Last 20 Lines |