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 @@ -1,6 +1,7 @@ #include "CommCoreModule.h" #include "../CryptoTools/DeviceID.h" #include "../Notifications/BackgroundDataStorage/NotificationsCryptoModule.h" +#include "../Tools/CommSecureStore.h" #include "DatabaseManager.h" #include "DraftStoreOperations.h" #include "InternalModules/GlobalDBSingleton.h" @@ -9,6 +10,7 @@ #include "ThreadStoreOperations.h" #include +#include #include #include #include @@ -820,24 +822,17 @@ } jsi::Value CommCoreModule::getUserPublicKey(jsi::Runtime &rt) { + CommSecureStore secureStore{}; + folly::Optional picklingKey = secureStore.get( + NotificationsCryptoModule::secureStoreNotificationsAccountDataKey); return createPromiseAsJSIValue( rt, [=](jsi::Runtime &innerRt, std::shared_ptr promise) { taskType job = [=, &innerRt]() { std::string error; - std::string primaryKeysResult; - std::string notificationsKeysResult; - if (this->cryptoModule == nullptr) { - error = "user has not been initialized"; - } else { - primaryKeysResult = this->cryptoModule->getIdentityKeys(); - } - try { - if (!error.size()) { - notificationsKeysResult = - NotificationsCryptoModule::getNotificationsIdentityKeys(); - } - } catch (const std::exception &e) { - error = e.what(); + if (!picklingKey.hasValue()) { + error = + "Attempt to retrieve notifications crypto account before it " + "was correctly initialized."; } std::string notificationsCurve25519Cpp, notificationsEd25519Cpp, @@ -845,40 +840,64 @@ primaryEd25519Cpp; if (!error.size()) { - folly::dynamic parsedPrimary; + std::string primaryKeysResult; + std::string notificationsKeysResult; + + if (this->cryptoModule == nullptr) { + error = "user has not been initialized"; + } else { + primaryKeysResult = this->cryptoModule->getIdentityKeys(); + } try { - parsedPrimary = folly::parseJson(primaryKeysResult); - } catch (const folly::json::parse_error &e) { - error = - "parsing identity keys failed with: " + std::string(e.what()); + std::string unwrappedPicklingKey = picklingKey.value(); + if (!error.size()) { + notificationsKeysResult = + NotificationsCryptoModule::getNotificationsIdentityKeys( + unwrappedPicklingKey); + } + } catch (const std::exception &e) { + error = e.what(); } - if (!error.size()) { - primaryCurve25519Cpp = parsedPrimary["curve25519"].asString(); - primaryEd25519Cpp = parsedPrimary["ed25519"].asString(); - folly::dynamic parsedNotifications; + if (!error.size()) { + folly::dynamic parsedPrimary; try { - parsedNotifications = folly::parseJson(notificationsKeysResult); + parsedPrimary = folly::parseJson(primaryKeysResult); } catch (const folly::json::parse_error &e) { - error = "parsing notifications keys failed with: " + + error = "parsing identity keys failed with: " + std::string(e.what()); } if (!error.size()) { - notificationsCurve25519Cpp = - parsedNotifications["curve25519"].asString(); - notificationsEd25519Cpp = - parsedNotifications["ed25519"].asString(); - - folly::dynamic blobPayloadJSON = folly::dynamic::object( - "primaryIdentityPublicKeys", - folly::dynamic::object("ed25519", primaryEd25519Cpp)( - "curve25519", primaryCurve25519Cpp))( - "notificationIdentityPublicKeys", - folly::dynamic::object("ed25519", notificationsEd25519Cpp)( - "curve25519", notificationsCurve25519Cpp)); - - blobPayloadCpp = folly::toJson(blobPayloadJSON); - signatureCpp = this->cryptoModule->signMessage(blobPayloadCpp); + primaryCurve25519Cpp = parsedPrimary["curve25519"].asString(); + primaryEd25519Cpp = parsedPrimary["ed25519"].asString(); + + folly::dynamic parsedNotifications; + try { + parsedNotifications = + folly::parseJson(notificationsKeysResult); + } catch (const folly::json::parse_error &e) { + error = "parsing notifications keys failed with: " + + std::string(e.what()); + } + if (!error.size()) { + notificationsCurve25519Cpp = + parsedNotifications["curve25519"].asString(); + notificationsEd25519Cpp = + parsedNotifications["ed25519"].asString(); + + folly::dynamic blobPayloadJSON = folly::dynamic::object( + "primaryIdentityPublicKeys", + folly::dynamic::object("ed25519", primaryEd25519Cpp)( + "curve25519", primaryCurve25519Cpp))( + "notificationIdentityPublicKeys", + folly::dynamic::object( + "ed25519", notificationsEd25519Cpp)( + "curve25519", notificationsCurve25519Cpp)); + + blobPayloadCpp = folly::toJson(blobPayloadJSON); + signatureCpp = + this->cryptoModule->signMessage(blobPayloadCpp); + } } } } diff --git a/native/cpp/CommonCpp/Notifications/BackgroundDataStorage/NotificationsCryptoModule.h b/native/cpp/CommonCpp/Notifications/BackgroundDataStorage/NotificationsCryptoModule.h --- a/native/cpp/CommonCpp/Notifications/BackgroundDataStorage/NotificationsCryptoModule.h +++ b/native/cpp/CommonCpp/Notifications/BackgroundDataStorage/NotificationsCryptoModule.h @@ -6,7 +6,6 @@ namespace comm { class NotificationsCryptoModule { - const static std::string secureStoreNotificationsAccountDataKey; const static std::string notificationsCryptoAccountID; static void serializeAndFlushCryptoModule( @@ -19,9 +18,12 @@ const std::string &picklingKey); public: + const static std::string secureStoreNotificationsAccountDataKey; + static void initializeNotificationsCryptoAccount(const std::string &callingProcessName); static void clearSensitiveData(); - static std::string getNotificationsIdentityKeys(); + static std::string + getNotificationsIdentityKeys(const std::string &picklingKey); }; } // namespace comm diff --git a/native/cpp/CommonCpp/Notifications/BackgroundDataStorage/NotificationsCryptoModule.cpp b/native/cpp/CommonCpp/Notifications/BackgroundDataStorage/NotificationsCryptoModule.cpp --- a/native/cpp/CommonCpp/Notifications/BackgroundDataStorage/NotificationsCryptoModule.cpp +++ b/native/cpp/CommonCpp/Notifications/BackgroundDataStorage/NotificationsCryptoModule.cpp @@ -5,6 +5,7 @@ #include "../../Tools/PlatformSpecificTools.h" #include +#include #include #include #include @@ -155,21 +156,12 @@ callingProcessName); } -std::string NotificationsCryptoModule::getNotificationsIdentityKeys() { - CommSecureStore secureStore{}; - folly::Optional picklingKey = secureStore.get( - NotificationsCryptoModule::secureStoreNotificationsAccountDataKey); - if (!picklingKey.hasValue()) { - throw std::runtime_error( - "Attempt to retrieve notifications crypto account before it was " - "correctly initialized."); - } - +std::string NotificationsCryptoModule::getNotificationsIdentityKeys( + const std::string &picklingKey) { const std::string path = PlatformSpecificTools::getNotificationsCryptoAccountPath(); crypto::CryptoModule cryptoModule = - NotificationsCryptoModule::deserializeCryptoModule( - path, picklingKey.value()); + NotificationsCryptoModule::deserializeCryptoModule(path, picklingKey); return cryptoModule.getIdentityKeys(); }