Skip to content

Commit b93e7cc

Browse files
SessionHero01SessionHero01
authored andcommitted
Added master key generation and pro proof serialization
1 parent c6926a8 commit b93e7cc

File tree

4 files changed

+62
-3
lines changed

4 files changed

+62
-3
lines changed

library/src/main/cpp/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,6 @@ target_link_libraries( # Specifies the target library.
160160
webpdemux
161161
webpdecoder
162162
yuv
163+
nlohmann_json
164+
oxenc
163165
)

library/src/main/cpp/ed25519.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,19 @@ Java_network_loki_messenger_libsession_1util_ED25519_generate(JNIEnv *env, jobje
4343

4444
return jni_utils::new_key_pair(env, util::bytes_from_span(env, pk), util::bytes_from_span(env, sk));
4545
});
46+
}
47+
48+
extern "C"
49+
JNIEXPORT jbyteArray JNICALL
50+
Java_network_loki_messenger_libsession_1util_ED25519_generateProKeyPair(JNIEnv *env, jobject thiz,
51+
jbyteArray ed25519_seed) {
52+
return jni_utils::run_catching_cxx_exception_or_throws<jbyteArray>(env, [=] {
53+
return util::bytes_from_span(
54+
env,
55+
session::ed25519::ed25519_pro_privkey_for_ed25519_seed(
56+
jni_utils::JavaByteArrayRef(env, ed25519_seed).get()
57+
)
58+
);
59+
60+
});
4661
}

library/src/main/cpp/pro_proof.cpp

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#include <session/session_protocol.hpp>
2-
2+
#include <oxenc/base64.h>
3+
#include <nlohmann/json.hpp>
34
#include <jni.h>
5+
46
#include "util.h"
7+
#include "jni_utils.h"
58

69

710
extern "C"
@@ -35,14 +38,44 @@ extern "C"
3538
JNIEXPORT jstring JNICALL
3639
Java_network_loki_messenger_libsession_1util_pro_ProProof_00024Companion_nativeSerialize(
3740
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());
3961
}
4062

4163
extern "C"
4264
JNIEXPORT jlong JNICALL
4365
Java_network_loki_messenger_libsession_1util_pro_ProProof_00024Companion_nativeDeserialize(
4466
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+
});
4679
}
4780

4881
extern "C"

library/src/main/java/network/loki/messenger/libsession_util/ED25519.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,13 @@ object ED25519 : LibSessionUtilCApi() {
2929
): Boolean
3030

3131
external fun generate(seed: ByteArray?): KeyPair
32+
33+
/**
34+
* Generate the deterministic Master Session Pro key for signing requests to interact with the
35+
* Session Pro features of the protocol.
36+
*
37+
* @param ed25519Seed The seed the user uses to generate their session id
38+
* @return The libsodium-style Master Session Pro Ed25519 secret key, 64 bytes.
39+
*/
40+
external fun generateProPrivateKey(ed25519Seed: ByteArray): ByteArray
3241
}

0 commit comments

Comments
 (0)