diff --git a/native/cpp/CommonCpp/Tools/OlmUtils.h b/native/cpp/CommonCpp/Tools/OlmUtils.h --- a/native/cpp/CommonCpp/Tools/OlmUtils.h +++ b/native/cpp/CommonCpp/Tools/OlmUtils.h @@ -12,4 +12,12 @@ */ std::string parseOLMPrekey(std::string prekeyBlob); +/** + * Extracts the ed25519 signing public key from an identity keys JSON blob + * @param identityKeysBlob JSON string with identity keys + * @return The ed25519 public key string + * @throws std::runtime_error if parsing fails + */ +std::string getSigningPublicKey(const std::string &identityKeysBlob); + } // namespace comm diff --git a/native/cpp/CommonCpp/Tools/OlmUtils.cpp b/native/cpp/CommonCpp/Tools/OlmUtils.cpp --- a/native/cpp/CommonCpp/Tools/OlmUtils.cpp +++ b/native/cpp/CommonCpp/Tools/OlmUtils.cpp @@ -33,4 +33,25 @@ return parsedPrekey["curve25519"].values().begin()->asString(); } +std::string getSigningPublicKey(const std::string &identityKeysBlob) { + folly::dynamic parsedKeys; + try { + parsedKeys = folly::parseJson(identityKeysBlob); + } catch (const folly::json::parse_error &e) { + std::string errorMessage{ + "parsing identity keys failed with: " + std::string(e.what())}; + Logger::log(errorMessage); + throw std::runtime_error(errorMessage); + } + + if (!parsedKeys.count("ed25519") || !parsedKeys["ed25519"].isString()) { + std::string errorMessage{ + "parsing identity keys failed: ed25519 key missing or malformed"}; + Logger::log(errorMessage); + throw std::runtime_error(errorMessage); + } + + return parsedKeys["ed25519"].asString(); +} + } // namespace comm