Skip to content

Commit 3131c8e

Browse files
SessionHero01SessionHero01
andauthored
More integration of pro state (#36)
Co-authored-by: SessionHero01 <SesshioHero01@getsession.org>
1 parent 170a7ec commit 3131c8e

4 files changed

Lines changed: 126 additions & 29 deletions

File tree

library/src/main/cpp/conversation.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,64 @@ JavaLocalRef<jobject> serialize_one_to_one(JNIEnv *env, const session::config::c
4949
one_to_one.pro_expiry_unix_ts).get())};
5050
}
5151

52+
53+
struct WithProProofInfoClassInfo : public JavaClassInfo {
54+
jmethodID proProofInfo_getter;
55+
56+
WithProProofInfoClassInfo(JNIEnv *env, jobject obj)
57+
: JavaClassInfo(env, obj)
58+
, proProofInfo_getter(env->GetMethodID(java_class, "getProProofInfo", "()Lnetwork/loki/messenger/libsession_util/util/Conversation$ProProofInfo;"))
59+
{}
60+
61+
static const WithProProofInfoClassInfo& get(JNIEnv *env, jobject obj) {
62+
static WithProProofInfoClassInfo instance(env, obj);
63+
return instance;
64+
}
65+
};
66+
67+
struct ProProofInfoClassInfo : public JavaClassInfo {
68+
jmethodID genIndexHash_getter;
69+
jmethodID expiryUnixTs_getter;
70+
71+
ProProofInfoClassInfo(JNIEnv *env, jobject obj)
72+
: JavaClassInfo(env, obj)
73+
, genIndexHash_getter(env->GetMethodID(java_class, "getGenIndexHashBytes", "()[B"))
74+
, expiryUnixTs_getter(env->GetMethodID(java_class, "getExpiryUnixTs", "()J"))
75+
{}
76+
77+
static const ProProofInfoClassInfo& get(JNIEnv *env, jobject obj) {
78+
static ProProofInfoClassInfo instance(env, obj);
79+
return instance;
80+
}
81+
82+
static void read_gen_index_hash(std::optional<session::array_uc32> &out, JNIEnv *env, jobject obj) {
83+
if (!obj) {
84+
out = std::nullopt;
85+
return;
86+
}
87+
88+
JavaLocalRef<jbyteArray> hash_bytes(
89+
env,
90+
(jbyteArray) env->CallObjectMethod(
91+
obj,
92+
get(env, obj).genIndexHash_getter));
93+
94+
JavaByteArrayRef bytes_ref(env, hash_bytes.get());
95+
96+
out.emplace();
97+
std::copy_n(bytes_ref.get().begin(),
98+
std::min(env->GetArrayLength(hash_bytes.get()), 32), out->begin());
99+
}
100+
101+
static std::chrono::sys_time<std::chrono::milliseconds> read_pro_expiry(JNIEnv *env, jobject obj) {
102+
if (!obj) return {};
103+
104+
jlong expiry_ts = env->CallLongMethod(obj, get(env, obj).expiryUnixTs_getter);
105+
return std::chrono::sys_time<std::chrono::milliseconds>{std::chrono::milliseconds{expiry_ts}};
106+
}
107+
};
108+
109+
52110
session::config::convo::one_to_one deserialize_one_to_one(JNIEnv *env, jobject info) {
53111
struct ClassInfo : public JavaClassInfo {
54112
jmethodID id_getter;
@@ -73,6 +131,13 @@ session::config::convo::one_to_one deserialize_one_to_one(JNIEnv *env, jobject i
73131

74132
r.last_read = env->CallLongMethod(info, class_info.lastRead_getter);
75133
r.unread = env->CallBooleanMethod(info, class_info.unread_getter);
134+
135+
JavaLocalRef<jobject> pro_proof(env, env->CallObjectMethod(
136+
info, WithProProofInfoClassInfo::get(env, info).proProofInfo_getter));
137+
138+
ProProofInfoClassInfo::read_gen_index_hash(r.pro_gen_index_hash, env, pro_proof.get());
139+
r.pro_expiry_unix_ts = ProProofInfoClassInfo::read_pro_expiry(env, pro_proof.get());
140+
76141
return r;
77142
}
78143

@@ -234,6 +299,13 @@ session::config::convo::blinded_one_to_one deserialize_blinded_one_to_one(JNIEnv
234299
r.last_read = env->CallLongMethod(info, class_info.last_read_getter);
235300
r.unread = env->CallBooleanMethod(info, class_info.unread_getter);
236301

302+
JavaLocalRef<jobject> pro_proof(env, env->CallObjectMethod(
303+
info, WithProProofInfoClassInfo::get(env, info).proProofInfo_getter));
304+
305+
ProProofInfoClassInfo::read_gen_index_hash(r.pro_gen_index_hash, env, pro_proof.get());
306+
r.pro_expiry_unix_ts = ProProofInfoClassInfo::read_pro_expiry(env, pro_proof.get());
307+
308+
237309
return r;
238310
}
239311

library/src/main/cpp/group_keys.cpp

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,13 @@ extern "C"
127127
JNIEXPORT jbyteArray JNICALL
128128
Java_network_loki_messenger_libsession_1util_GroupKeysConfig_rekey(JNIEnv *env, jobject thiz,
129129
jlong info_ptr, jlong members_ptr) {
130-
auto keys = ptrToKeys(env, thiz);
131-
auto info = reinterpret_cast<session::config::groups::Info*>(info_ptr);
132-
auto members = reinterpret_cast<session::config::groups::Members*>(members_ptr);
133-
auto rekey = keys->rekey(*info, *members);
134-
return util::bytes_from_span(env, rekey).release();
130+
return jni_utils::run_catching_cxx_exception_or_throws<jbyteArray>(env, [=] {
131+
auto keys = ptrToKeys(env, thiz);
132+
auto info = reinterpret_cast<session::config::groups::Info*>(info_ptr);
133+
auto members = reinterpret_cast<session::config::groups::Members*>(members_ptr);
134+
auto rekey = keys->rekey(*info, *members);
135+
return util::bytes_from_span(env, rekey).release();
136+
});
135137
}
136138

137139
extern "C"
@@ -188,8 +190,9 @@ Java_network_loki_messenger_libsession_1util_GroupKeysConfig_keys(JNIEnv *env, j
188190
extern "C"
189191
JNIEXPORT jbyteArray JNICALL
190192
Java_network_loki_messenger_libsession_1util_GroupKeysConfig_groupEncKey(JNIEnv *env, jobject thiz) {
191-
auto ptr = ptrToKeys(env, thiz);
192-
return util::bytes_from_span(env, ptr->group_enc_key()).release();
193+
return jni_utils::run_catching_cxx_exception_or_throws<jbyteArray>(env, [=] {
194+
return util::bytes_from_span(env, ptrToKeys(env, thiz)->group_enc_key()).release();
195+
});
193196
}
194197

195198
extern "C"
@@ -206,9 +209,11 @@ Java_network_loki_messenger_libsession_1util_GroupKeysConfig_makeSubAccount(JNIE
206209
jstring session_id,
207210
jboolean can_write,
208211
jboolean can_delete) {
209-
auto ptr = ptrToKeys(env, thiz);
210-
auto new_subaccount_key = ptr->swarm_make_subaccount(jni_utils::JavaStringRef(env, session_id).view(), can_write, can_delete);
211-
return util::bytes_from_vector(env, new_subaccount_key).release();
212+
return jni_utils::run_catching_cxx_exception_or_throws<jbyteArray>(env, [=] {
213+
auto new_subaccount_key = ptrToKeys(env, thiz)->swarm_make_subaccount(
214+
jni_utils::JavaStringRef(env, session_id).view(), can_write, can_delete);
215+
return util::bytes_from_vector(env, new_subaccount_key).release();
216+
});
212217
}
213218

214219
extern "C"
@@ -218,18 +223,21 @@ Java_network_loki_messenger_libsession_1util_GroupKeysConfig_getSubAccountToken(
218223
jstring session_id,
219224
jboolean can_write,
220225
jboolean can_delete) {
221-
auto ptr = ptrToKeys(env, thiz);
222-
auto token = ptr->swarm_subaccount_token(jni_utils::JavaStringRef(env, session_id).view(), can_write, can_delete);
223-
return util::bytes_from_vector(env, token).release();
226+
return jni_utils::run_catching_cxx_exception_or_throws<jbyteArray>(env, [=] {
227+
auto token = ptrToKeys(env, thiz)->swarm_subaccount_token(
228+
jni_utils::JavaStringRef(env, session_id).view(),
229+
can_write, can_delete);
230+
return util::bytes_from_vector(env, token).release();
231+
});
224232
}
225233

226234
static jni_utils::JavaLocalRef<jobject> deserialize_swarm_auth(JNIEnv *env, session::config::groups::Keys::swarm_auth auth) {
227235
static jni_utils::BasicJavaClassInfo class_info(
228236
env, "network/loki/messenger/libsession_util/GroupKeysConfig$SwarmAuth", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
229237

230-
auto sub_account = jni_utils::JavaLocalRef(env, env->NewStringUTF(auth.subaccount.data()));
231-
auto sub_account_sig = jni_utils::JavaLocalRef(env, env->NewStringUTF(auth.subaccount_sig.data()));
232-
auto signature = jni_utils::JavaLocalRef(env, env->NewStringUTF(auth.signature.data()));
238+
jni_utils::JavaLocalRef sub_account(env, env->NewStringUTF(auth.subaccount.data()));
239+
jni_utils::JavaLocalRef sub_account_sig(env, env->NewStringUTF(auth.subaccount_sig.data()));
240+
jni_utils::JavaLocalRef signature(env, env->NewStringUTF(auth.signature.data()));
233241

234242
return {env, env->NewObject(class_info.java_class, class_info.constructor, sub_account.get(), sub_account_sig.get(), signature.get())};
235243
}
@@ -240,26 +248,35 @@ Java_network_loki_messenger_libsession_1util_GroupKeysConfig_subAccountSign(JNIE
240248
jobject thiz,
241249
jbyteArray message,
242250
jbyteArray signing_value) {
243-
auto ptr = ptrToKeys(env, thiz);
244-
auto message_vector = util::vector_from_bytes(env, message);
245-
auto signing_value_vector = util::vector_from_bytes(env, signing_value);
246-
auto swarm_auth = ptr->swarm_subaccount_sign(message_vector, signing_value_vector, false);
247-
return deserialize_swarm_auth(env, swarm_auth).release();
251+
return jni_utils::run_catching_cxx_exception_or_throws<jobject>(env, [=] {
252+
auto ptr = ptrToKeys(env, thiz);
253+
auto message_vector = util::vector_from_bytes(env, message);
254+
auto signing_value_vector = util::vector_from_bytes(env, signing_value);
255+
auto swarm_auth = ptr->swarm_subaccount_sign(message_vector, signing_value_vector, false);
256+
return deserialize_swarm_auth(env, swarm_auth).release();
257+
});
248258
}
249259

250260
extern "C"
251261
JNIEXPORT jbyteArray JNICALL
252262
Java_network_loki_messenger_libsession_1util_GroupKeysConfig_supplementFor(JNIEnv *env,
253263
jobject thiz,
254264
jobjectArray j_user_session_ids) {
255-
auto ptr = ptrToKeys(env, thiz);
256-
std::vector<std::string> user_session_ids;
257-
for (int i = 0, size = env->GetArrayLength(j_user_session_ids); i < size; i++) {
258-
user_session_ids.push_back(jni_utils::JavaStringRef(env, jni_utils::JavaLocalRef(env, (jstring)(env->GetObjectArrayElement(j_user_session_ids, i))).get()).copy());
259-
}
260-
auto supplement = ptr->key_supplement(user_session_ids);
261-
return util::bytes_from_vector(env, supplement).release();
265+
return jni_utils::run_catching_cxx_exception_or_throws<jbyteArray>(env, [=] {
266+
auto ptr = ptrToKeys(env, thiz);
267+
std::vector<std::string> user_session_ids;
268+
for (int i = 0, size = env->GetArrayLength(j_user_session_ids); i < size; i++) {
269+
jni_utils::JavaLocalRef element(
270+
env, (jstring)(env->GetObjectArrayElement(j_user_session_ids, i)));
271+
272+
user_session_ids.emplace_back(jni_utils::JavaStringRef(env, element.get()).view());
273+
}
274+
275+
auto supplement = ptr->key_supplement(user_session_ids);
276+
return util::bytes_from_vector(env, supplement).release();
277+
});
262278
}
279+
263280
extern "C"
264281
JNIEXPORT jint JNICALL
265282
Java_network_loki_messenger_libsession_1util_GroupKeysConfig_currentGeneration(JNIEnv *env,

library/src/main/java/network/loki/messenger/libsession_util/util/Conversation.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ sealed interface Conversation {
2424
genIndexHash = Bytes(genIndexHash),
2525
expiry = Instant.ofEpochMilli(expiryMs)
2626
)
27+
28+
@get:Keep
29+
val genIndexHashBytes: ByteArray
30+
get() = genIndexHash.data
31+
32+
@get:Keep
33+
val expiryUnixTs: Long
34+
get() = expiry.toEpochMilli()
2735
}
2836

2937
/**

0 commit comments

Comments
 (0)