From ca7d48205009c2cfd622976b24c26089c72a07bf Mon Sep 17 00:00:00 2001 From: 0x330a <92654767+0x330a@users.noreply.github.com> Date: Wed, 14 Dec 2022 11:37:43 +1100 Subject: [PATCH] feat: adding contacts logic, fixing up linter issues, starting contacts tests with native functions --- .../libsession_util/InstrumentedTests.kt | 3 +- libsession-util/src/main/cpp/config_base.cpp | 4 ++- libsession-util/src/main/cpp/contacts.cpp | 34 ++++++++++++++++++- libsession-util/src/main/cpp/user_profile.cpp | 3 ++ libsession-util/src/main/cpp/util.cpp | 16 +++++++-- .../loki/messenger/libsession_util/Config.kt | 8 +++++ .../messenger/libsession_util/util/Sodium.kt | 1 + 7 files changed, 64 insertions(+), 5 deletions(-) diff --git a/libsession-util/src/androidTest/java/network/loki/messenger/libsession_util/InstrumentedTests.kt b/libsession-util/src/androidTest/java/network/loki/messenger/libsession_util/InstrumentedTests.kt index 5c3bb44445..ba14f6bc8a 100644 --- a/libsession-util/src/androidTest/java/network/loki/messenger/libsession_util/InstrumentedTests.kt +++ b/libsession-util/src/androidTest/java/network/loki/messenger/libsession_util/InstrumentedTests.kt @@ -34,7 +34,8 @@ class InstrumentedTests { @Test fun jni_contacts() { - val userProfile = UserProfile.newInstance(keyPair.secretKey) + val contacts = Contacts.newInstance(keyPair.secretKey) + val definitelyRealId = Hex.fromStringCondensed("050000000000000000000000000000000000000000000000000000000000000000") } diff --git a/libsession-util/src/main/cpp/config_base.cpp b/libsession-util/src/main/cpp/config_base.cpp index 0c79b68c60..5d9a25b8ee 100644 --- a/libsession-util/src/main/cpp/config_base.cpp +++ b/libsession-util/src/main/cpp/config_base.cpp @@ -55,6 +55,8 @@ Java_network_loki_messenger_libsession_1util_ConfigBase_confirmPushed(JNIEnv *en conf->confirm_pushed(seq_no); } +#pragma clang diagnostic push +#pragma ide diagnostic ignored "bugprone-reserved-identifier" JNIEXPORT jint JNICALL Java_network_loki_messenger_libsession_1util_ConfigBase_merge___3_3B(JNIEnv *env, jobject thiz, jobjectArray to_merge) { @@ -76,5 +78,5 @@ Java_network_loki_messenger_libsession_1util_ConfigBase_merge___3B(JNIEnv *env, std::vector configs = {util::ustring_from_bytes(env, to_merge)}; return conf->merge(configs); } - +#pragma clang diagnostic pop } \ No newline at end of file diff --git a/libsession-util/src/main/cpp/contacts.cpp b/libsession-util/src/main/cpp/contacts.cpp index fec6e3115b..1113e83062 100644 --- a/libsession-util/src/main/cpp/contacts.cpp +++ b/libsession-util/src/main/cpp/contacts.cpp @@ -36,4 +36,36 @@ Java_network_loki_messenger_libsession_1util_Contacts_erase(JNIEnv *env, jobject jstring session_id) { auto contacts = ptrToContacts(env, thiz); return contacts->erase(env->GetStringUTFChars(session_id, nullptr)); -} \ No newline at end of file +} +extern "C" +#pragma clang diagnostic push +#pragma ide diagnostic ignored "bugprone-reserved-identifier" +JNIEXPORT jobject JNICALL +Java_network_loki_messenger_libsession_1util_Contacts_00024Companion_newInstance___3B(JNIEnv *env, + jobject thiz, + jbyteArray ed25519_secret_key) { + auto secret_key = util::ustring_from_bytes(env, ed25519_secret_key); + auto* contacts = new session::config::Contacts(secret_key, std::nullopt); + + jclass contactsClass = env->FindClass("network/loki/messenger/libsession_util/Contacts"); + jmethodID constructor = env->GetMethodID(contactsClass, "", "(J)V"); + jobject newConfig = env->NewObject(contactsClass, constructor, reinterpret_cast(contacts)); + + return newConfig; +} +extern "C" +JNIEXPORT jobject JNICALL +Java_network_loki_messenger_libsession_1util_Contacts_00024Companion_newInstance___3B_3B( + JNIEnv *env, jobject thiz, jbyteArray ed25519_secret_key, jbyteArray initial_dump) { + auto secret_key = util::ustring_from_bytes(env, ed25519_secret_key); + auto initial = util::ustring_from_bytes(env, initial_dump); + + auto* contacts = new session::config::Contacts(secret_key, initial); + + jclass contactsClass = env->FindClass("network/loki/messenger/libsession_util/Contacts"); + jmethodID constructor = env->GetMethodID(contactsClass, "", "(J)V"); + jobject newConfig = env->NewObject(contactsClass, constructor, reinterpret_cast(contacts)); + + return newConfig; +} +#pragma clang diagnostic pop \ No newline at end of file diff --git a/libsession-util/src/main/cpp/user_profile.cpp b/libsession-util/src/main/cpp/user_profile.cpp index 20c8e5182d..9c1d25f719 100644 --- a/libsession-util/src/main/cpp/user_profile.cpp +++ b/libsession-util/src/main/cpp/user_profile.cpp @@ -2,6 +2,8 @@ #include "util.h" extern "C" { +#pragma clang diagnostic push +#pragma ide diagnostic ignored "bugprone-reserved-identifier" JNIEXPORT jobject JNICALL Java_network_loki_messenger_libsession_1util_UserProfile_00024Companion_newInstance___3B_3B( JNIEnv *env, jobject thiz, jbyteArray ed25519_secret_key, jbyteArray initial_dump) { @@ -30,6 +32,7 @@ Java_network_loki_messenger_libsession_1util_UserProfile_00024Companion_newInsta return newConfig; } +#pragma clang diagnostic pop JNIEXPORT void JNICALL Java_network_loki_messenger_libsession_1util_UserProfile_setName( diff --git a/libsession-util/src/main/cpp/util.cpp b/libsession-util/src/main/cpp/util.cpp index d4cfee8cc4..cc15969c7f 100644 --- a/libsession-util/src/main/cpp/util.cpp +++ b/libsession-util/src/main/cpp/util.cpp @@ -44,8 +44,8 @@ namespace util { extern "C" JNIEXPORT jobject JNICALL Java_network_loki_messenger_libsession_1util_util_Sodium_ed25519KeyPair(JNIEnv *env, jobject thiz, jbyteArray seed) { - std::array ed_pk; - std::array ed_sk; + std::array ed_pk; // NOLINT(cppcoreguidelines-pro-type-member-init) + std::array ed_sk; // NOLINT(cppcoreguidelines-pro-type-member-init) auto seed_bytes = util::ustring_from_bytes(env, seed); crypto_sign_ed25519_seed_keypair(ed_pk.data(), ed_sk.data(), seed_bytes.data()); @@ -57,4 +57,16 @@ Java_network_loki_messenger_libsession_1util_util_Sodium_ed25519KeyPair(JNIEnv * jobject return_obj = env->NewObject(kp_class, kp_constructor, pk_jarray, sk_jarray); return return_obj; +} +extern "C" +JNIEXPORT jbyteArray JNICALL +Java_network_loki_messenger_libsession_1util_util_Sodium_ed25519PkToCurve25519(JNIEnv *env, + jobject thiz, + jbyteArray pk) { + auto ed_pk = util::ustring_from_bytes(env, pk); + std::array curve_pk; // NOLINT(cppcoreguidelines-pro-type-member-init) + crypto_sign_ed25519_pk_to_curve25519(curve_pk.data(), ed_pk.data()); + + jbyteArray curve_pk_jarray = util::bytes_from_ustring(env, session::ustring_view {curve_pk.data(), curve_pk.size()}); + return curve_pk_jarray; } \ No newline at end of file diff --git a/libsession-util/src/main/java/network/loki/messenger/libsession_util/Config.kt b/libsession-util/src/main/java/network/loki/messenger/libsession_util/Config.kt index cf674f0a00..6b4ee80dc1 100644 --- a/libsession-util/src/main/java/network/loki/messenger/libsession_util/Config.kt +++ b/libsession-util/src/main/java/network/loki/messenger/libsession_util/Config.kt @@ -26,6 +26,14 @@ sealed class ConfigBase(protected val /* yucky */ pointer: Long) { } class Contacts(pointer: Long) : ConfigBase(pointer) { + companion object { + init { + System.loadLibrary("session_util") + } + external fun newInstance(ed25519SecretKey: ByteArray): Contacts + external fun newInstance(ed25519SecretKey: ByteArray, initialDump: ByteArray): Contacts + } + external fun get(sessionId: String): Contact? external fun getOrCreate(sessionId: String): Contact external fun set(contact: Contact) diff --git a/libsession-util/src/main/java/network/loki/messenger/libsession_util/util/Sodium.kt b/libsession-util/src/main/java/network/loki/messenger/libsession_util/util/Sodium.kt index 131c8c7084..6168bd2165 100644 --- a/libsession-util/src/main/java/network/loki/messenger/libsession_util/util/Sodium.kt +++ b/libsession-util/src/main/java/network/loki/messenger/libsession_util/util/Sodium.kt @@ -5,4 +5,5 @@ object Sodium { System.loadLibrary("session_util") } external fun ed25519KeyPair(seed: ByteArray): KeyPair + external fun ed25519PkToCurve25519(pk: ByteArray): ByteArray } \ No newline at end of file