|
1 | 1 | #include <session/session_protocol.hpp> |
2 | | - |
| 2 | +#include <oxenc/base64.h> |
| 3 | +#include <nlohmann/json.hpp> |
3 | 4 | #include <jni.h> |
| 5 | + |
4 | 6 | #include "util.h" |
| 7 | +#include "jni_utils.h" |
5 | 8 |
|
6 | 9 |
|
7 | 10 | extern "C" |
@@ -35,14 +38,44 @@ extern "C" |
35 | 38 | JNIEXPORT jstring JNICALL |
36 | 39 | Java_network_loki_messenger_libsession_1util_pro_ProProof_00024Companion_nativeSerialize( |
37 | 40 | JNIEnv *env, jobject thiz, jlong native_value) { |
38 | | - // TODO: implement nativeSerialize() |
| 41 | + const auto& proof = |
| 42 | + *reinterpret_cast<session::ProProof*>(native_value); |
| 43 | + nlohmann::json j; |
| 44 | + j["version"] = proof.version; |
| 45 | + j["gen_index_hash"] = oxenc::to_base64(proof.gen_index_hash); |
| 46 | + j["rotating_pubkey"] = oxenc::to_base64(proof.rotating_pubkey); |
| 47 | + j["expiry_unix_ts_ms"] = proof.expiry_unix_ts.time_since_epoch().count(); |
| 48 | + j["sig"] = oxenc::to_base64(proof.sig); |
| 49 | + |
| 50 | + return util::jstringFromOptional(env, j.dump()); |
| 51 | +} |
| 52 | + |
| 53 | +template<size_t N> |
| 54 | +void from_json(const nlohmann::json& j, std::array<uint8_t, N>& arr) { |
| 55 | + auto b64_str = j.get<std::string_view>(); |
| 56 | + auto bytes = oxenc::from_base64(b64_str); |
| 57 | + if (bytes.size() != N) { |
| 58 | + throw std::invalid_argument{"Invalid array size in from_json"}; |
| 59 | + } |
| 60 | + std::copy(bytes.begin(), bytes.end(), arr.begin()); |
39 | 61 | } |
40 | 62 |
|
41 | 63 | extern "C" |
42 | 64 | JNIEXPORT jlong JNICALL |
43 | 65 | Java_network_loki_messenger_libsession_1util_pro_ProProof_00024Companion_nativeDeserialize( |
44 | 66 | JNIEnv *env, jobject thiz, jstring data) { |
45 | | - // TODO: implement nativeDeserialize() |
| 67 | + return jni_utils::run_catching_cxx_exception_or_throws<jlong>(env, [=]() { |
| 68 | + auto j = nlohmann::json::parse(jni_utils::JavaStringRef(env, data).view()); |
| 69 | + |
| 70 | + return reinterpret_cast<jlong>(new session::ProProof { |
| 71 | + .version = j.at("version").get<uint8_t>(), |
| 72 | + .gen_index_hash = j.at("gen_index_hash").get<session::array_uc32>(), |
| 73 | + .rotating_pubkey = j.at("rotating_pubkey").get<session::array_uc32>(), |
| 74 | + .expiry_unix_ts = std::chrono::sys_time<std::chrono::milliseconds>{ |
| 75 | + std::chrono::milliseconds{static_cast<int64_t>(j.at("expiry_unix_ts_ms").get<uint64_t>())}}, |
| 76 | + .sig = j.at("sig").get<session::array_uc64>(), |
| 77 | + }); |
| 78 | + }); |
46 | 79 | } |
47 | 80 |
|
48 | 81 | extern "C" |
|
0 commit comments