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 @@ -7,6 +7,7 @@ #include "InternalModules/RustPromiseManager.h" #include "Logger.h" #include "NativeModuleUtils.h" +#include "OlmUtils.h" #include "TerminateApp.h" #include @@ -762,33 +763,6 @@ return jsiOneTimeKeys; } -std::string parseOLMPrekey(std::string prekeyBlob) { - folly::dynamic parsedPrekey; - try { - parsedPrekey = folly::parseJson(prekeyBlob); - } catch (const folly::json::parse_error &e) { - std::string errorMessage{ - "parsing prekey failed with: " + std::string(e.what())}; - Logger::log(errorMessage); - throw std::runtime_error(errorMessage); - } - - folly::dynamic innerObject = parsedPrekey["curve25519"]; - if (!innerObject.isObject()) { - std::string errorMessage{"parsing prekey failed: inner object malformed"}; - Logger::log(errorMessage); - throw std::runtime_error(errorMessage); - } - - if (innerObject.values().begin() == innerObject.values().end()) { - std::string errorMessage{"parsing prekey failed: prekey missing"}; - Logger::log(errorMessage); - throw std::runtime_error(errorMessage); - } - - return parsedPrekey["curve25519"].values().begin()->asString(); -} - jsi::Object parseOneTimeKeysResult( jsi::Runtime &rt, std::string contentOneTimeKeysBlob, diff --git a/native/cpp/CommonCpp/Tools/CMakeLists.txt b/native/cpp/CommonCpp/Tools/CMakeLists.txt --- a/native/cpp/CommonCpp/Tools/CMakeLists.txt +++ b/native/cpp/CommonCpp/Tools/CMakeLists.txt @@ -7,12 +7,14 @@ "Base64.h" "WorkerThread.h" "StaffUtils.h" + "OlmUtils.h" ) set(TOOLS_SRCS "Base64.cpp" "WorkerThread.cpp" "StaffUtils.cpp" + "OlmUtils.cpp" ) add_library(comm-tools diff --git a/native/cpp/CommonCpp/Tools/OlmUtils.h b/native/cpp/CommonCpp/Tools/OlmUtils.h new file mode 100644 --- /dev/null +++ b/native/cpp/CommonCpp/Tools/OlmUtils.h @@ -0,0 +1,15 @@ +#pragma once + +#include + +namespace comm { + +/** + * Parses a OLM prekey string from JSON format + * @param prekeyBlob JSON string with prekey + * @return The prekey string extracted from the JSON + * @throws std::runtime_error if parsing fails + */ +std::string parseOLMPrekey(std::string prekeyBlob); + +} // namespace comm diff --git a/native/cpp/CommonCpp/Tools/OlmUtils.cpp b/native/cpp/CommonCpp/Tools/OlmUtils.cpp new file mode 100644 --- /dev/null +++ b/native/cpp/CommonCpp/Tools/OlmUtils.cpp @@ -0,0 +1,36 @@ +#include "OlmUtils.h" +#include "Logger.h" + +#include +#include + +namespace comm { + +std::string parseOLMPrekey(std::string prekeyBlob) { + folly::dynamic parsedPrekey; + try { + parsedPrekey = folly::parseJson(prekeyBlob); + } catch (const folly::json::parse_error &e) { + std::string errorMessage{ + "parsing prekey failed with: " + std::string(e.what())}; + Logger::log(errorMessage); + throw std::runtime_error(errorMessage); + } + + folly::dynamic innerObject = parsedPrekey["curve25519"]; + if (!innerObject.isObject()) { + std::string errorMessage{"parsing prekey failed: inner object malformed"}; + Logger::log(errorMessage); + throw std::runtime_error(errorMessage); + } + + if (innerObject.values().begin() == innerObject.values().end()) { + std::string errorMessage{"parsing prekey failed: prekey missing"}; + Logger::log(errorMessage); + throw std::runtime_error(errorMessage); + } + + return parsedPrekey["curve25519"].values().begin()->asString(); +} + +} // namespace comm diff --git a/native/ios/Comm.xcodeproj/project.pbxproj b/native/ios/Comm.xcodeproj/project.pbxproj --- a/native/ios/Comm.xcodeproj/project.pbxproj +++ b/native/ios/Comm.xcodeproj/project.pbxproj @@ -44,6 +44,8 @@ 7FBB2A7629E94539002C6493 /* utilsJSI-generated.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7FBB2A7529E94539002C6493 /* utilsJSI-generated.cpp */; }; 7FBB2A7829E945C2002C6493 /* CommUtilsModule.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7FBB2A7329E944FD002C6493 /* CommUtilsModule.cpp */; }; 7FBB2A7B29EEA2A4002C6493 /* Base64.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7FBB2A7A29EEA2A4002C6493 /* Base64.cpp */; }; + 7FDFC0FE2DC0FEBD00B1D87F /* OlmUtils.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7FDFC0FD2DC0FEBD00B1D87F /* OlmUtils.cpp */; }; + 7FDFC0FF2DC0FEBD00B1D87F /* OlmUtils.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7FDFC0FD2DC0FEBD00B1D87F /* OlmUtils.cpp */; }; 7FE4D9F5291DFE9300667BF6 /* commJSI-generated.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7FE4D9F4291DFE9300667BF6 /* commJSI-generated.cpp */; }; 816D2D5A2C480E60001C0B67 /* MessageSearchStore.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 816D2D582C480E60001C0B67 /* MessageSearchStore.cpp */; }; 8B38121629CE5742000C52E9 /* RustPromiseManager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8B38121529CE5742000C52E9 /* RustPromiseManager.cpp */; }; @@ -230,6 +232,8 @@ 7FBB2A7A29EEA2A4002C6493 /* Base64.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = Base64.cpp; sourceTree = ""; }; 7FCEA2DC2444010B004017B1 /* Comm-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Comm-Bridging-Header.h"; sourceTree = ""; }; 7FCFD8BD1E81B8DF00629B0E /* Comm.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; name = Comm.entitlements; path = Comm/Comm.entitlements; sourceTree = ""; }; + 7FDFC0FC2DC0FEBD00B1D87F /* OlmUtils.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OlmUtils.h; sourceTree = ""; }; + 7FDFC0FD2DC0FEBD00B1D87F /* OlmUtils.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = OlmUtils.cpp; sourceTree = ""; }; 7FE4D9F3291DFE9300667BF6 /* commJSI.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = commJSI.h; sourceTree = ""; }; 7FE4D9F4291DFE9300667BF6 /* commJSI-generated.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = "commJSI-generated.cpp"; sourceTree = ""; }; 816D2D582C480E60001C0B67 /* MessageSearchStore.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = MessageSearchStore.cpp; path = PersistentStorageUtilities/DataStores/MessageSearchStore.cpp; sourceTree = ""; }; @@ -492,6 +496,8 @@ 71BE84382636A944002849D2 /* Tools */ = { isa = PBXGroup; children = ( + 7FDFC0FC2DC0FEBD00B1D87F /* OlmUtils.h */, + 7FDFC0FD2DC0FEBD00B1D87F /* OlmUtils.cpp */, CBB0DF5E2B767FDF008E22FF /* CommMMKV.h */, DFD5E7802B05264F00C32B6A /* AESCrypto.h */, CBCA09052A8E0E6B00F75B3E /* StaffUtils.cpp */, @@ -1232,6 +1238,7 @@ CB74AB202B2B0C0A00CBB494 /* RustCSAMetadataEmitter.cpp in Sources */, CB38B48428771CAF00171182 /* EncryptedFileUtils.mm in Sources */, CBFE58292885852B003B94C9 /* ThreadOperations.cpp in Sources */, + 7FDFC0FF2DC0FEBD00B1D87F /* OlmUtils.cpp in Sources */, CB74AB1C2B2AFF6E00CBB494 /* CommServicesAuthMetadataEmitter.mm in Sources */, 8E3994552B039A7C00D5E950 /* UserStore.cpp in Sources */, CBFBEEBA2B4ED90600729F1D /* RustBackupExecutor.cpp in Sources */, @@ -1289,6 +1296,7 @@ files = ( CB99DB4E2C45329500B8055E /* NotificationsInboundKeysProvider.mm in Sources */, CBCF98502BA49A0500DBC3D9 /* CommIOSServicesClient.mm in Sources */, + 7FDFC0FE2DC0FEBD00B1D87F /* OlmUtils.cpp in Sources */, CBCA09072A8E0E7D00F75B3E /* StaffUtils.cpp in Sources */, CB3C0A3B2A125C8F009BD4DA /* NotificationsCryptoModule.cpp in Sources */, CB90951F29534B32002F2A7F /* CommSecureStore.mm in Sources */,