1#include "mtxclient/crypto/types.hpp"
2
3#include <nlohmann/json.hpp>
4
5namespace mtx {
6namespace crypto {
7
8void
9to_json(nlohmann::json &obj, const ExportedSession &s)
10{
11 obj["sender_claimed_keys"] = s.sender_claimed_keys;
12 obj["forwarding_curve25519_key_chain"] = s.forwarding_curve25519_key_chain;
13
14 obj["algorithm"] = s.algorithm;
15 obj["room_id"] = s.room_id;
16 obj["sender_key"] = s.sender_key;
17 obj["session_id"] = s.session_id;
18 obj["session_key"] = s.session_key;
19}
20
21void
22from_json(const nlohmann::json &obj, ExportedSession &s)
23{
24 s.room_id = obj.at(key: "room_id").get<std::string>();
25 s.sender_key = obj.at(key: "sender_key").get<std::string>();
26 s.session_id = obj.at(key: "session_id").get<std::string>();
27 s.session_key = obj.at(key: "session_key").get<std::string>();
28
29 using ClaimedKeys = std::map<std::string, std::string>;
30 using KeyChain = std::vector<std::string>;
31
32 if (obj.find(key: "sender_claimed_keys") != obj.end())
33 s.sender_claimed_keys = obj.at(key: "sender_claimed_keys").get<ClaimedKeys>();
34
35 if (obj.find(key: "forwarding_curve25519_key_chain") != obj.end())
36 s.forwarding_curve25519_key_chain =
37 obj.at(key: "forwarding_curve25519_key_chain").get<KeyChain>();
38}
39
40void
41to_json(nlohmann::json &obj, const ExportedSessionKeys &keys)
42{
43 obj = keys.sessions;
44}
45
46void
47from_json(const nlohmann::json &obj, ExportedSessionKeys &keys)
48{
49 try {
50 keys.sessions = obj.get<std::vector<ExportedSession>>();
51 // might be the old format.
52 } catch (const nlohmann::json::exception &) {
53 keys.sessions = obj.at(key: "sessions").get<std::vector<ExportedSession>>();
54 }
55}
56
57void
58to_json(nlohmann::json &obj, const IdentityKeys &keys)
59{
60 obj[ED25519] = keys.ed25519;
61 obj[CURVE25519] = keys.curve25519;
62}
63
64void
65from_json(const nlohmann::json &obj, IdentityKeys &keys)
66{
67 keys.ed25519 = obj.at(key: ED25519).get<std::string>();
68 keys.curve25519 = obj.at(key: CURVE25519).get<std::string>();
69}
70
71void
72to_json(nlohmann::json &obj, const OneTimeKeys &keys)
73{
74 obj[CURVE25519] = keys.curve25519;
75}
76
77void
78from_json(const nlohmann::json &obj, OneTimeKeys &keys)
79{
80 keys.curve25519 = obj.at(key: CURVE25519).get<std::map<std::string, std::string>>();
81}
82
83} // namespace crypto
84} // namespace mtx
85