mirror of
https://github.com/oxen-io/session-android.git
synced 2024-11-25 11:05:25 +00:00
feat: adding in new contacts functionality and wrappers, sorting out multiple files to organize all the wrapper functions
This commit is contained in:
parent
43ee5f929d
commit
68b62c3e80
@ -1 +1 @@
|
|||||||
Subproject commit b47ce1b3be84cac7cf051ef851c3c3de58381b26
|
Subproject commit 94b14f212a14fd846f1737cd6180fff4f06d42f5
|
@ -32,6 +32,12 @@ class InstrumentedTests {
|
|||||||
assertEquals("network.loki.messenger.libsession_util.test", appContext.packageName)
|
assertEquals("network.loki.messenger.libsession_util.test", appContext.packageName)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun jni_contacts() {
|
||||||
|
val userProfile = UserProfile.newInstance(keyPair.secretKey)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun jni_accessible() {
|
fun jni_accessible() {
|
||||||
val userProfile = UserProfile.newInstance(keyPair.secretKey)
|
val userProfile = UserProfile.newInstance(keyPair.secretKey)
|
||||||
@ -173,8 +179,8 @@ class InstrumentedTests {
|
|||||||
|
|
||||||
assertEquals(3, userConfigMerge.seqNo)
|
assertEquals(3, userConfigMerge.seqNo)
|
||||||
|
|
||||||
assertEquals("Raz", newConf.getName())
|
assertEquals("Nibbler", newConf.getName())
|
||||||
assertEquals("Raz", userProfile.getName())
|
assertEquals("Nibbler", userProfile.getName())
|
||||||
|
|
||||||
userProfile.free()
|
userProfile.free()
|
||||||
newConf.free()
|
newConf.free()
|
||||||
|
@ -22,14 +22,18 @@ set(CMAKE_CXX_EXTENSIONS OFF)
|
|||||||
set(STATIC_BUNDLE ON)
|
set(STATIC_BUNDLE ON)
|
||||||
add_subdirectory(../../../libsession-util libsession)
|
add_subdirectory(../../../libsession-util libsession)
|
||||||
|
|
||||||
|
set(SOURCES
|
||||||
|
user_profile.cpp
|
||||||
|
config_base.cpp
|
||||||
|
contacts.cpp
|
||||||
|
util.cpp)
|
||||||
|
|
||||||
add_library( # Sets the name of the library.
|
add_library( # Sets the name of the library.
|
||||||
session_util
|
session_util
|
||||||
|
|
||||||
# Sets the library as a shared library.
|
# Sets the library as a shared library.
|
||||||
SHARED
|
SHARED
|
||||||
|
|
||||||
# Provides a relative path to your source file(s).
|
# Provides a relative path to your source file(s).
|
||||||
session_util.cpp)
|
${SOURCES})
|
||||||
|
|
||||||
# Searches for a specified prebuilt library and stores the path as a
|
# Searches for a specified prebuilt library and stores the path as a
|
||||||
# variable. Because CMake includes system libraries in the search path by
|
# variable. Because CMake includes system libraries in the search path by
|
||||||
|
80
libsession-util/src/main/cpp/config_base.cpp
Normal file
80
libsession-util/src/main/cpp/config_base.cpp
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
#include "config_base.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
JNIEXPORT jboolean JNICALL
|
||||||
|
Java_network_loki_messenger_libsession_1util_ConfigBase_dirty(JNIEnv *env, jobject thiz) {
|
||||||
|
auto* configBase = ptrToConfigBase(env, thiz);
|
||||||
|
return configBase->is_dirty();
|
||||||
|
}
|
||||||
|
|
||||||
|
JNIEXPORT jboolean JNICALL
|
||||||
|
Java_network_loki_messenger_libsession_1util_ConfigBase_needsPush(JNIEnv *env, jobject thiz) {
|
||||||
|
auto config = ptrToConfigBase(env, thiz);
|
||||||
|
return config->needs_push();
|
||||||
|
}
|
||||||
|
|
||||||
|
JNIEXPORT jboolean JNICALL
|
||||||
|
Java_network_loki_messenger_libsession_1util_ConfigBase_needsDump(JNIEnv *env, jobject thiz) {
|
||||||
|
auto config = ptrToConfigBase(env, thiz);
|
||||||
|
return config->needs_dump();
|
||||||
|
}
|
||||||
|
|
||||||
|
JNIEXPORT jobject JNICALL
|
||||||
|
Java_network_loki_messenger_libsession_1util_ConfigBase_push(JNIEnv *env, jobject thiz) {
|
||||||
|
auto config = ptrToConfigBase(env, thiz);
|
||||||
|
auto pair = config->push();
|
||||||
|
session::ustring to_push_str = pair.first;
|
||||||
|
jbyteArray returnByteArray = util::bytes_from_ustring(env, to_push_str);
|
||||||
|
jlong seqNo = pair.second;
|
||||||
|
jclass returnObjectClass = env->FindClass("network/loki/messenger/libsession_util/util/ConfigWithSeqNo");
|
||||||
|
jmethodID methodId = env->GetMethodID(returnObjectClass, "<init>", "([BJ)V");
|
||||||
|
jobject returnObject = env->NewObject(returnObjectClass, methodId, returnByteArray, seqNo);
|
||||||
|
return returnObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
JNIEXPORT jbyteArray JNICALL
|
||||||
|
Java_network_loki_messenger_libsession_1util_ConfigBase_dump(JNIEnv *env, jobject thiz) {
|
||||||
|
auto config = ptrToConfigBase(env, thiz);
|
||||||
|
auto dumped = config->dump();
|
||||||
|
jbyteArray bytes = util::bytes_from_ustring(env, dumped);
|
||||||
|
return bytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
JNIEXPORT jstring JNICALL
|
||||||
|
Java_network_loki_messenger_libsession_1util_ConfigBase_encryptionDomain(JNIEnv *env,
|
||||||
|
jobject thiz) {
|
||||||
|
auto conf = ptrToConfigBase(env, thiz);
|
||||||
|
return env->NewStringUTF(conf->encryption_domain());
|
||||||
|
}
|
||||||
|
|
||||||
|
JNIEXPORT void JNICALL
|
||||||
|
Java_network_loki_messenger_libsession_1util_ConfigBase_confirmPushed(JNIEnv *env, jobject thiz,
|
||||||
|
jlong seq_no) {
|
||||||
|
auto conf = ptrToConfigBase(env, thiz);
|
||||||
|
conf->confirm_pushed(seq_no);
|
||||||
|
}
|
||||||
|
|
||||||
|
JNIEXPORT jint JNICALL
|
||||||
|
Java_network_loki_messenger_libsession_1util_ConfigBase_merge___3_3B(JNIEnv *env, jobject thiz,
|
||||||
|
jobjectArray to_merge) {
|
||||||
|
auto conf = ptrToConfigBase(env, thiz);
|
||||||
|
size_t number = env->GetArrayLength(to_merge);
|
||||||
|
std::vector<session::ustring> configs = {};
|
||||||
|
for (int i = 0; i < number; i++) {
|
||||||
|
auto jArr = (jbyteArray) env->GetObjectArrayElement(to_merge, i);
|
||||||
|
auto bytes = util::ustring_from_bytes(env, jArr);
|
||||||
|
configs.push_back(bytes);
|
||||||
|
}
|
||||||
|
return conf->merge(configs);
|
||||||
|
}
|
||||||
|
|
||||||
|
JNIEXPORT jint JNICALL
|
||||||
|
Java_network_loki_messenger_libsession_1util_ConfigBase_merge___3B(JNIEnv *env, jobject thiz,
|
||||||
|
jbyteArray to_merge) {
|
||||||
|
auto conf = ptrToConfigBase(env, thiz);
|
||||||
|
std::vector<session::ustring> configs = {util::ustring_from_bytes(env, to_merge)};
|
||||||
|
return conf->merge(configs);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
14
libsession-util/src/main/cpp/config_base.h
Normal file
14
libsession-util/src/main/cpp/config_base.h
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
#ifndef SESSION_ANDROID_CONFIG_BASE_H
|
||||||
|
#define SESSION_ANDROID_CONFIG_BASE_H
|
||||||
|
|
||||||
|
#include "session/config/base.hpp"
|
||||||
|
#include <jni.h>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
inline session::config::ConfigBase* ptrToConfigBase(JNIEnv *env, jobject obj) {
|
||||||
|
jclass baseClass = env->FindClass("network/loki/messenger/libsession_util/ConfigBase");
|
||||||
|
jfieldID pointerField = env->GetFieldID(baseClass, "pointer", "J");
|
||||||
|
return (session::config::ConfigBase*) env->GetLongField(obj, pointerField);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
39
libsession-util/src/main/cpp/contacts.cpp
Normal file
39
libsession-util/src/main/cpp/contacts.cpp
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
#include "contacts.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
|
extern "C"
|
||||||
|
JNIEXPORT jobject JNICALL
|
||||||
|
Java_network_loki_messenger_libsession_1util_Contacts_get(JNIEnv *env, jobject thiz,
|
||||||
|
jstring session_id) {
|
||||||
|
auto contacts = ptrToContacts(env, thiz);
|
||||||
|
auto contact = contacts->get(env->GetStringUTFChars(session_id, nullptr));
|
||||||
|
if (!contact) return nullptr;
|
||||||
|
jobject j_contact = serialize_contact(env, contact.value());
|
||||||
|
return j_contact;
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C"
|
||||||
|
JNIEXPORT jobject JNICALL
|
||||||
|
Java_network_loki_messenger_libsession_1util_Contacts_getOrCreate(JNIEnv *env, jobject thiz,
|
||||||
|
jstring session_id) {
|
||||||
|
auto contacts = ptrToContacts(env, thiz);
|
||||||
|
auto contact = contacts->get_or_create(env->GetStringUTFChars(session_id, nullptr));
|
||||||
|
return serialize_contact(env, contact);
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C"
|
||||||
|
JNIEXPORT void JNICALL
|
||||||
|
Java_network_loki_messenger_libsession_1util_Contacts_set(JNIEnv *env, jobject thiz,
|
||||||
|
jobject contact) {
|
||||||
|
auto contacts = ptrToContacts(env, thiz);
|
||||||
|
auto contact_info = deserialize_contact(env, contact);
|
||||||
|
contacts->set(contact_info);
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C"
|
||||||
|
JNIEXPORT jboolean JNICALL
|
||||||
|
Java_network_loki_messenger_libsession_1util_Contacts_erase(JNIEnv *env, jobject thiz,
|
||||||
|
jstring session_id) {
|
||||||
|
auto contacts = ptrToContacts(env, thiz);
|
||||||
|
return contacts->erase(env->GetStringUTFChars(session_id, nullptr));
|
||||||
|
}
|
32
libsession-util/src/main/cpp/contacts.h
Normal file
32
libsession-util/src/main/cpp/contacts.h
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
#ifndef SESSION_ANDROID_CONTACTS_H
|
||||||
|
#define SESSION_ANDROID_CONTACTS_H
|
||||||
|
|
||||||
|
#include <jni.h>
|
||||||
|
#include "session/config/contacts.hpp"
|
||||||
|
|
||||||
|
inline session::config::Contacts *ptrToContacts(JNIEnv *env, jobject obj) {
|
||||||
|
jclass contactsClass = env->FindClass("network/loki/messenger/libsession_util/Contacts");
|
||||||
|
jfieldID pointerField = env->GetFieldID(contactsClass, "pointer", "J");
|
||||||
|
return (session::config::Contacts *) env->GetLongField(obj, pointerField);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline jobject serialize_contact(JNIEnv *env, session::config::contact_info info) {
|
||||||
|
jclass contactClass = env->FindClass("network/loki/messenger/libsession_util/util/Contact");
|
||||||
|
jmethodID constructor = env->GetMethodID(contactClass, "<init>",
|
||||||
|
"(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;ZZZLnetwork/loki/messenger/libsession_util/util/UserPic;)V");
|
||||||
|
// val id: String,
|
||||||
|
// val name: String,
|
||||||
|
// val nickname: String,
|
||||||
|
// val approved: Boolean,
|
||||||
|
// val approvedMe: Boolean,
|
||||||
|
// val blocked: Boolean,
|
||||||
|
// val profilePicture: UserPic
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline session::config::contact_info deserialize_contact(JNIEnv *env, jobject info) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endif //SESSION_ANDROID_CONTACTS_H
|
@ -1,217 +0,0 @@
|
|||||||
#include <jni.h>
|
|
||||||
#include <string>
|
|
||||||
#include <sodium/crypto_sign_ed25519.h>
|
|
||||||
#include "session/config/user_profile.hpp"
|
|
||||||
|
|
||||||
session::config::ConfigBase* ptrToConfigBase(JNIEnv *env, jobject obj) {
|
|
||||||
jclass baseClass = env->FindClass("network/loki/messenger/libsession_util/ConfigBase");
|
|
||||||
jfieldID pointerField = env->GetFieldID(baseClass, "pointer", "J");
|
|
||||||
return (session::config::ConfigBase*) env->GetLongField(obj, pointerField);
|
|
||||||
}
|
|
||||||
|
|
||||||
session::config::UserProfile* ptrToProfile(JNIEnv* env, jobject obj) {
|
|
||||||
jclass configClass = env->FindClass("network/loki/messenger/libsession_util/UserProfile");
|
|
||||||
jfieldID pointerField = env->GetFieldID(configClass, "pointer", "J");
|
|
||||||
return (session::config::UserProfile*) env->GetLongField(obj, pointerField);
|
|
||||||
}
|
|
||||||
|
|
||||||
jbyteArray bytes_from_ustring(JNIEnv* env, session::ustring_view from_str) {
|
|
||||||
size_t length = from_str.length();
|
|
||||||
jsize jlength = (jsize)length;
|
|
||||||
jbyteArray new_array = env->NewByteArray(jlength);
|
|
||||||
env->SetByteArrayRegion(new_array, 0, jlength, (jbyte*)from_str.data());
|
|
||||||
return new_array;
|
|
||||||
}
|
|
||||||
|
|
||||||
session::ustring ustring_from_bytes(JNIEnv* env, jbyteArray byteArray) {
|
|
||||||
size_t len = env->GetArrayLength(byteArray);
|
|
||||||
jbyte* bytes = env->GetByteArrayElements(byteArray, nullptr);
|
|
||||||
session::ustring newSt((u_char*)bytes, len);
|
|
||||||
env->ReleaseByteArrayElements(byteArray, bytes, 0);
|
|
||||||
return newSt;
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "C"
|
|
||||||
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) {
|
|
||||||
auto secret_key = ustring_from_bytes(env, ed25519_secret_key);
|
|
||||||
auto initial = ustring_from_bytes(env, initial_dump);
|
|
||||||
auto* profile = new session::config::UserProfile(secret_key, std::optional(initial));
|
|
||||||
|
|
||||||
jclass userClass = env->FindClass("network/loki/messenger/libsession_util/UserProfile");
|
|
||||||
jmethodID constructor = env->GetMethodID(userClass, "<init>", "(J)V");
|
|
||||||
jobject newConfig = env->NewObject(userClass, constructor, reinterpret_cast<jlong>(profile));
|
|
||||||
|
|
||||||
return newConfig;
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "C" JNIEXPORT jobject JNICALL
|
|
||||||
Java_network_loki_messenger_libsession_1util_UserProfile_00024Companion_newInstance___3B(
|
|
||||||
JNIEnv* env,
|
|
||||||
jobject,
|
|
||||||
jbyteArray secretKey) {
|
|
||||||
|
|
||||||
auto* profile = new session::config::UserProfile(ustring_from_bytes(env, secretKey), std::nullopt);
|
|
||||||
|
|
||||||
jclass userClass = env->FindClass("network/loki/messenger/libsession_util/UserProfile");
|
|
||||||
jmethodID constructor = env->GetMethodID(userClass, "<init>", "(J)V");
|
|
||||||
jobject newConfig = env->NewObject(userClass, constructor, reinterpret_cast<jlong>(profile));
|
|
||||||
|
|
||||||
return newConfig;
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "C" JNIEXPORT void JNICALL
|
|
||||||
Java_network_loki_messenger_libsession_1util_UserProfile_setName(
|
|
||||||
JNIEnv* env,
|
|
||||||
jobject thiz,
|
|
||||||
jstring newName) {
|
|
||||||
auto profile = ptrToProfile(env, thiz);
|
|
||||||
profile->set_name(env->GetStringUTFChars(newName, nullptr));
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "C"
|
|
||||||
JNIEXPORT jstring JNICALL
|
|
||||||
Java_network_loki_messenger_libsession_1util_UserProfile_getName(JNIEnv *env, jobject thiz) {
|
|
||||||
auto profile = ptrToProfile(env, thiz);
|
|
||||||
auto name = profile->get_name();
|
|
||||||
if (name == std::nullopt) return nullptr;
|
|
||||||
jstring returnString = env->NewStringUTF(name->data());
|
|
||||||
return returnString;
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "C"
|
|
||||||
JNIEXPORT void JNICALL
|
|
||||||
Java_network_loki_messenger_libsession_1util_UserProfile_free(JNIEnv *env, jobject thiz) {
|
|
||||||
auto profile = ptrToProfile(env, thiz);
|
|
||||||
delete profile;
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "C"
|
|
||||||
JNIEXPORT jboolean JNICALL
|
|
||||||
Java_network_loki_messenger_libsession_1util_ConfigBase_dirty(JNIEnv *env, jobject thiz) {
|
|
||||||
auto* configBase = ptrToConfigBase(env, thiz);
|
|
||||||
return configBase->is_dirty();
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "C"
|
|
||||||
JNIEXPORT jboolean JNICALL
|
|
||||||
Java_network_loki_messenger_libsession_1util_ConfigBase_needsPush(JNIEnv *env, jobject thiz) {
|
|
||||||
auto config = ptrToConfigBase(env, thiz);
|
|
||||||
return config->needs_push();
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "C"
|
|
||||||
JNIEXPORT jboolean JNICALL
|
|
||||||
Java_network_loki_messenger_libsession_1util_ConfigBase_needsDump(JNIEnv *env, jobject thiz) {
|
|
||||||
auto config = ptrToConfigBase(env, thiz);
|
|
||||||
return config->needs_dump();
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "C"
|
|
||||||
JNIEXPORT jobject JNICALL
|
|
||||||
Java_network_loki_messenger_libsession_1util_ConfigBase_push(JNIEnv *env, jobject thiz) {
|
|
||||||
auto config = ptrToConfigBase(env, thiz);
|
|
||||||
auto pair = config->push();
|
|
||||||
session::ustring to_push_str = pair.first;
|
|
||||||
jbyteArray returnByteArray = bytes_from_ustring(env, to_push_str);
|
|
||||||
jlong seqNo = pair.second;
|
|
||||||
jclass returnObjectClass = env->FindClass("network/loki/messenger/libsession_util/util/ConfigWithSeqNo");
|
|
||||||
jmethodID methodId = env->GetMethodID(returnObjectClass, "<init>", "([BJ)V");
|
|
||||||
jobject returnObject = env->NewObject(returnObjectClass, methodId, returnByteArray, seqNo);
|
|
||||||
return returnObject;
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "C"
|
|
||||||
JNIEXPORT jobject JNICALL
|
|
||||||
Java_network_loki_messenger_libsession_1util_UserProfile_getPic(JNIEnv *env, jobject thiz) {
|
|
||||||
auto profile = ptrToProfile(env, thiz);
|
|
||||||
auto pic = profile->get_profile_pic();
|
|
||||||
if (pic == std::nullopt) return nullptr;
|
|
||||||
// return nullptr if either parameter is null as per profile class
|
|
||||||
jclass returnObjectClass = env->FindClass("network/loki/messenger/libsession_util/util/UserPic");
|
|
||||||
jmethodID constructor = env->GetMethodID(returnObjectClass, "<init>", "(Ljava/lang/String;[B)V");
|
|
||||||
jstring url = env->NewStringUTF(pic->url.data());
|
|
||||||
jbyteArray byteArray = bytes_from_ustring(env, pic->key);
|
|
||||||
jobject returnObject = env->NewObject(returnObjectClass, constructor, url, byteArray);
|
|
||||||
return returnObject;
|
|
||||||
}
|
|
||||||
extern "C"
|
|
||||||
JNIEXPORT void JNICALL
|
|
||||||
Java_network_loki_messenger_libsession_1util_UserProfile_setPic(JNIEnv *env, jobject thiz,
|
|
||||||
jobject user_pic) {
|
|
||||||
auto profile = ptrToProfile(env, thiz);
|
|
||||||
jclass userPicClass = env->FindClass("network/loki/messenger/libsession_util/util/UserPic");
|
|
||||||
jfieldID picField = env->GetFieldID(userPicClass, "url", "Ljava/lang/String;");
|
|
||||||
jfieldID keyField = env->GetFieldID(userPicClass, "key", "[B");
|
|
||||||
auto pic = (jstring)env->GetObjectField(user_pic, picField);
|
|
||||||
auto key = (jbyteArray)env->GetObjectField(user_pic, keyField);
|
|
||||||
|
|
||||||
const char* pic_chars = env->GetStringUTFChars(pic, nullptr);
|
|
||||||
auto key_str = ustring_from_bytes(env, key);
|
|
||||||
auto* pic_string = new std::string(pic_chars);
|
|
||||||
|
|
||||||
profile->set_profile_pic(*pic_string, key_str);
|
|
||||||
}
|
|
||||||
extern "C"
|
|
||||||
JNIEXPORT jbyteArray JNICALL
|
|
||||||
Java_network_loki_messenger_libsession_1util_ConfigBase_dump(JNIEnv *env, jobject thiz) {
|
|
||||||
auto config = ptrToConfigBase(env, thiz);
|
|
||||||
auto dumped = config->dump();
|
|
||||||
jbyteArray bytes = bytes_from_ustring(env, dumped);
|
|
||||||
return bytes;
|
|
||||||
}
|
|
||||||
extern "C"
|
|
||||||
JNIEXPORT jobject JNICALL
|
|
||||||
Java_network_loki_messenger_libsession_1util_util_Sodium_ed25519KeyPair(JNIEnv *env, jobject thiz, jbyteArray seed) {
|
|
||||||
std::array<unsigned char, 32> ed_pk;
|
|
||||||
std::array<unsigned char, 64> ed_sk;
|
|
||||||
auto seed_bytes = ustring_from_bytes(env, seed);
|
|
||||||
crypto_sign_ed25519_seed_keypair(ed_pk.data(), ed_sk.data(), seed_bytes.data());
|
|
||||||
|
|
||||||
jclass kp_class = env->FindClass("network/loki/messenger/libsession_util/util/KeyPair");
|
|
||||||
jmethodID kp_constructor = env->GetMethodID(kp_class, "<init>", "([B[B)V");
|
|
||||||
|
|
||||||
jbyteArray pk_jarray = bytes_from_ustring(env, session::ustring_view {ed_pk.data(), ed_pk.size()});
|
|
||||||
jbyteArray sk_jarray = bytes_from_ustring(env, session::ustring_view {ed_sk.data(), ed_sk.size()});
|
|
||||||
|
|
||||||
jobject return_obj = env->NewObject(kp_class, kp_constructor, pk_jarray, sk_jarray);
|
|
||||||
return return_obj;
|
|
||||||
}
|
|
||||||
extern "C"
|
|
||||||
JNIEXPORT jstring JNICALL
|
|
||||||
Java_network_loki_messenger_libsession_1util_ConfigBase_encryptionDomain(JNIEnv *env,
|
|
||||||
jobject thiz) {
|
|
||||||
auto conf = ptrToConfigBase(env, thiz);
|
|
||||||
return env->NewStringUTF(conf->encryption_domain());
|
|
||||||
}
|
|
||||||
extern "C"
|
|
||||||
JNIEXPORT void JNICALL
|
|
||||||
Java_network_loki_messenger_libsession_1util_ConfigBase_confirmPushed(JNIEnv *env, jobject thiz,
|
|
||||||
jlong seq_no) {
|
|
||||||
auto conf = ptrToConfigBase(env, thiz);
|
|
||||||
conf->confirm_pushed(seq_no);
|
|
||||||
}
|
|
||||||
extern "C"
|
|
||||||
JNIEXPORT jint JNICALL
|
|
||||||
Java_network_loki_messenger_libsession_1util_ConfigBase_merge___3_3B(JNIEnv *env, jobject thiz,
|
|
||||||
jobjectArray to_merge) {
|
|
||||||
auto conf = ptrToConfigBase(env, thiz);
|
|
||||||
size_t number = env->GetArrayLength(to_merge);
|
|
||||||
std::vector<session::ustring> configs = {};
|
|
||||||
for (int i = 0; i < number; i++) {
|
|
||||||
auto jArr = (jbyteArray) env->GetObjectArrayElement(to_merge, i);
|
|
||||||
auto bytes = ustring_from_bytes(env, jArr);
|
|
||||||
configs.push_back(bytes);
|
|
||||||
}
|
|
||||||
return conf->merge(configs);
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "C"
|
|
||||||
JNIEXPORT jint JNICALL
|
|
||||||
Java_network_loki_messenger_libsession_1util_ConfigBase_merge___3B(JNIEnv *env, jobject thiz,
|
|
||||||
jbyteArray to_merge) {
|
|
||||||
auto conf = ptrToConfigBase(env, thiz);
|
|
||||||
std::vector<session::ustring> configs = {ustring_from_bytes(env, to_merge)};
|
|
||||||
return conf->merge(configs);
|
|
||||||
}
|
|
78
libsession-util/src/main/cpp/user_profile.cpp
Normal file
78
libsession-util/src/main/cpp/user_profile.cpp
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
#include "user_profile.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
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) {
|
||||||
|
auto secret_key = util::ustring_from_bytes(env, ed25519_secret_key);
|
||||||
|
auto initial = util::ustring_from_bytes(env, initial_dump);
|
||||||
|
auto* profile = new session::config::UserProfile(secret_key, std::optional(initial));
|
||||||
|
|
||||||
|
jclass userClass = env->FindClass("network/loki/messenger/libsession_util/UserProfile");
|
||||||
|
jmethodID constructor = env->GetMethodID(userClass, "<init>", "(J)V");
|
||||||
|
jobject newConfig = env->NewObject(userClass, constructor, reinterpret_cast<jlong>(profile));
|
||||||
|
|
||||||
|
return newConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
JNIEXPORT jobject JNICALL
|
||||||
|
Java_network_loki_messenger_libsession_1util_UserProfile_00024Companion_newInstance___3B(
|
||||||
|
JNIEnv* env,
|
||||||
|
jobject,
|
||||||
|
jbyteArray secretKey) {
|
||||||
|
|
||||||
|
auto* profile = new session::config::UserProfile(util::ustring_from_bytes(env, secretKey), std::nullopt);
|
||||||
|
|
||||||
|
jclass userClass = env->FindClass("network/loki/messenger/libsession_util/UserProfile");
|
||||||
|
jmethodID constructor = env->GetMethodID(userClass, "<init>", "(J)V");
|
||||||
|
jobject newConfig = env->NewObject(userClass, constructor, reinterpret_cast<jlong>(profile));
|
||||||
|
|
||||||
|
return newConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
JNIEXPORT void JNICALL
|
||||||
|
Java_network_loki_messenger_libsession_1util_UserProfile_setName(
|
||||||
|
JNIEnv* env,
|
||||||
|
jobject thiz,
|
||||||
|
jstring newName) {
|
||||||
|
auto profile = ptrToProfile(env, thiz);
|
||||||
|
profile->set_name(env->GetStringUTFChars(newName, nullptr));
|
||||||
|
}
|
||||||
|
|
||||||
|
JNIEXPORT jstring JNICALL
|
||||||
|
Java_network_loki_messenger_libsession_1util_UserProfile_getName(JNIEnv *env, jobject thiz) {
|
||||||
|
auto profile = ptrToProfile(env, thiz);
|
||||||
|
auto name = profile->get_name();
|
||||||
|
if (name == std::nullopt) return nullptr;
|
||||||
|
jstring returnString = env->NewStringUTF(name->data());
|
||||||
|
return returnString;
|
||||||
|
}
|
||||||
|
|
||||||
|
JNIEXPORT void JNICALL
|
||||||
|
Java_network_loki_messenger_libsession_1util_UserProfile_free(JNIEnv *env, jobject thiz) {
|
||||||
|
auto profile = ptrToProfile(env, thiz);
|
||||||
|
delete profile;
|
||||||
|
}
|
||||||
|
|
||||||
|
JNIEXPORT jobject JNICALL
|
||||||
|
Java_network_loki_messenger_libsession_1util_UserProfile_getPic(JNIEnv *env, jobject thiz) {
|
||||||
|
auto profile = ptrToProfile(env, thiz);
|
||||||
|
auto pic = profile->get_profile_pic();
|
||||||
|
|
||||||
|
jobject returnObject = util::serialize_user_pic(env, pic);
|
||||||
|
|
||||||
|
return returnObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
JNIEXPORT void JNICALL
|
||||||
|
Java_network_loki_messenger_libsession_1util_UserProfile_setPic(JNIEnv *env, jobject thiz,
|
||||||
|
jobject user_pic) {
|
||||||
|
auto profile = ptrToProfile(env, thiz);
|
||||||
|
auto pic = util::deserialize_user_pic(env, user_pic);
|
||||||
|
auto url = env->GetStringUTFChars(pic.first, nullptr);
|
||||||
|
auto key = util::ustring_from_bytes(env, pic.second);
|
||||||
|
profile->set_profile_pic(url, key);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
14
libsession-util/src/main/cpp/user_profile.h
Normal file
14
libsession-util/src/main/cpp/user_profile.h
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
#ifndef SESSION_ANDROID_USER_PROFILE_H
|
||||||
|
#define SESSION_ANDROID_USER_PROFILE_H
|
||||||
|
|
||||||
|
#include "session/config/user_profile.hpp"
|
||||||
|
#include <jni.h>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
inline session::config::UserProfile* ptrToProfile(JNIEnv* env, jobject obj) {
|
||||||
|
jclass configClass = env->FindClass("network/loki/messenger/libsession_util/UserProfile");
|
||||||
|
jfieldID pointerField = env->GetFieldID(configClass, "pointer", "J");
|
||||||
|
return (session::config::UserProfile*) env->GetLongField(obj, pointerField);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
60
libsession-util/src/main/cpp/util.cpp
Normal file
60
libsession-util/src/main/cpp/util.cpp
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
#include "util.h"
|
||||||
|
#include <string>
|
||||||
|
#include <sodium/crypto_sign.h>
|
||||||
|
|
||||||
|
namespace util {
|
||||||
|
jbyteArray bytes_from_ustring(JNIEnv* env, session::ustring_view from_str) {
|
||||||
|
size_t length = from_str.length();
|
||||||
|
auto jlength = (jsize)length;
|
||||||
|
jbyteArray new_array = env->NewByteArray(jlength);
|
||||||
|
env->SetByteArrayRegion(new_array, 0, jlength, (jbyte*)from_str.data());
|
||||||
|
return new_array;
|
||||||
|
}
|
||||||
|
|
||||||
|
session::ustring ustring_from_bytes(JNIEnv* env, jbyteArray byteArray) {
|
||||||
|
size_t len = env->GetArrayLength(byteArray);
|
||||||
|
auto bytes = env->GetByteArrayElements(byteArray, nullptr);
|
||||||
|
|
||||||
|
session::ustring st{reinterpret_cast<const unsigned char *>(bytes), len};
|
||||||
|
env->ReleaseByteArrayElements(byteArray, bytes, 0);
|
||||||
|
return st;
|
||||||
|
}
|
||||||
|
|
||||||
|
jobject serialize_user_pic(JNIEnv *env, std::optional<session::config::profile_pic> pic) {
|
||||||
|
if (!pic) return nullptr;
|
||||||
|
|
||||||
|
jclass returnObjectClass = env->FindClass("network/loki/messenger/libsession_util/util/UserPic");
|
||||||
|
jmethodID constructor = env->GetMethodID(returnObjectClass, "<init>", "(Ljava/lang/String;[B)V");
|
||||||
|
jstring url = env->NewStringUTF(pic->url.data());
|
||||||
|
jbyteArray byteArray = util::bytes_from_ustring(env, pic->key);
|
||||||
|
return env->NewObject(returnObjectClass, constructor, url, byteArray);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::pair<jstring, jbyteArray> deserialize_user_pic(JNIEnv *env, jobject user_pic) {
|
||||||
|
jclass userPicClass = env->FindClass("network/loki/messenger/libsession_util/util/UserPic");
|
||||||
|
jfieldID picField = env->GetFieldID(userPicClass, "url", "Ljava/lang/String;");
|
||||||
|
jfieldID keyField = env->GetFieldID(userPicClass, "key", "[B");
|
||||||
|
auto pic = (jstring)env->GetObjectField(user_pic, picField);
|
||||||
|
auto key = (jbyteArray)env->GetObjectField(user_pic, keyField);
|
||||||
|
return {pic, key};
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C"
|
||||||
|
JNIEXPORT jobject JNICALL
|
||||||
|
Java_network_loki_messenger_libsession_1util_util_Sodium_ed25519KeyPair(JNIEnv *env, jobject thiz, jbyteArray seed) {
|
||||||
|
std::array<unsigned char, 32> ed_pk;
|
||||||
|
std::array<unsigned char, 64> ed_sk;
|
||||||
|
auto seed_bytes = util::ustring_from_bytes(env, seed);
|
||||||
|
crypto_sign_ed25519_seed_keypair(ed_pk.data(), ed_sk.data(), seed_bytes.data());
|
||||||
|
|
||||||
|
jclass kp_class = env->FindClass("network/loki/messenger/libsession_util/util/KeyPair");
|
||||||
|
jmethodID kp_constructor = env->GetMethodID(kp_class, "<init>", "([B[B)V");
|
||||||
|
|
||||||
|
jbyteArray pk_jarray = util::bytes_from_ustring(env, session::ustring_view {ed_pk.data(), ed_pk.size()});
|
||||||
|
jbyteArray sk_jarray = util::bytes_from_ustring(env, session::ustring_view {ed_sk.data(), ed_sk.size()});
|
||||||
|
|
||||||
|
jobject return_obj = env->NewObject(kp_class, kp_constructor, pk_jarray, sk_jarray);
|
||||||
|
return return_obj;
|
||||||
|
}
|
17
libsession-util/src/main/cpp/util.h
Normal file
17
libsession-util/src/main/cpp/util.h
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#ifndef SESSION_ANDROID_UTIL_H
|
||||||
|
#define SESSION_ANDROID_UTIL_H
|
||||||
|
|
||||||
|
#include <jni.h>
|
||||||
|
#include <array>
|
||||||
|
#include <optional>
|
||||||
|
#include "session/types.hpp"
|
||||||
|
#include "session/config/profile_pic.hpp"
|
||||||
|
|
||||||
|
namespace util {
|
||||||
|
jbyteArray bytes_from_ustring(JNIEnv* env, session::ustring_view from_str);
|
||||||
|
session::ustring ustring_from_bytes(JNIEnv* env, jbyteArray byteArray);
|
||||||
|
jobject serialize_user_pic(JNIEnv *env, std::optional<session::config::profile_pic> pic);
|
||||||
|
std::pair<jstring, jbyteArray> deserialize_user_pic(JNIEnv *env, jobject user_pic);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
@ -1,6 +1,7 @@
|
|||||||
package network.loki.messenger.libsession_util
|
package network.loki.messenger.libsession_util
|
||||||
|
|
||||||
import network.loki.messenger.libsession_util.util.ConfigWithSeqNo
|
import network.loki.messenger.libsession_util.util.ConfigWithSeqNo
|
||||||
|
import network.loki.messenger.libsession_util.util.Contact
|
||||||
import network.loki.messenger.libsession_util.util.UserPic
|
import network.loki.messenger.libsession_util.util.UserPic
|
||||||
|
|
||||||
|
|
||||||
@ -10,6 +11,7 @@ sealed class ConfigBase(protected val /* yucky */ pointer: Long) {
|
|||||||
System.loadLibrary("session_util")
|
System.loadLibrary("session_util")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
external fun dirty(): Boolean
|
external fun dirty(): Boolean
|
||||||
external fun needsPush(): Boolean
|
external fun needsPush(): Boolean
|
||||||
external fun needsDump(): Boolean
|
external fun needsDump(): Boolean
|
||||||
@ -18,18 +20,28 @@ sealed class ConfigBase(protected val /* yucky */ pointer: Long) {
|
|||||||
external fun encryptionDomain(): String
|
external fun encryptionDomain(): String
|
||||||
external fun confirmPushed(seqNo: Long)
|
external fun confirmPushed(seqNo: Long)
|
||||||
external fun merge(toMerge: Array<ByteArray>): Int
|
external fun merge(toMerge: Array<ByteArray>): Int
|
||||||
|
|
||||||
// Singular merge
|
// Singular merge
|
||||||
external fun merge(toMerge: ByteArray): Int
|
external fun merge(toMerge: ByteArray): Int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class Contacts(pointer: Long) : ConfigBase(pointer) {
|
||||||
|
external fun get(sessionId: String): Contact?
|
||||||
|
external fun getOrCreate(sessionId: String): Contact
|
||||||
|
external fun set(contact: Contact)
|
||||||
|
external fun erase(sessionId: String): Boolean
|
||||||
|
}
|
||||||
|
|
||||||
class UserProfile(pointer: Long) : ConfigBase(pointer) {
|
class UserProfile(pointer: Long) : ConfigBase(pointer) {
|
||||||
companion object {
|
companion object {
|
||||||
init {
|
init {
|
||||||
System.loadLibrary("session_util")
|
System.loadLibrary("session_util")
|
||||||
}
|
}
|
||||||
|
|
||||||
external fun newInstance(ed25519SecretKey: ByteArray): UserProfile
|
external fun newInstance(ed25519SecretKey: ByteArray): UserProfile
|
||||||
external fun newInstance(ed25519SecretKey: ByteArray, initialDump: ByteArray): UserProfile
|
external fun newInstance(ed25519SecretKey: ByteArray, initialDump: ByteArray): UserProfile
|
||||||
}
|
}
|
||||||
|
|
||||||
external fun setName(newName: String)
|
external fun setName(newName: String)
|
||||||
external fun getName(): String?
|
external fun getName(): String?
|
||||||
external fun free()
|
external fun free()
|
||||||
|
@ -0,0 +1,11 @@
|
|||||||
|
package network.loki.messenger.libsession_util.util
|
||||||
|
|
||||||
|
data class Contact(
|
||||||
|
val id: String,
|
||||||
|
val name: String,
|
||||||
|
val nickname: String,
|
||||||
|
val approved: Boolean,
|
||||||
|
val approvedMe: Boolean,
|
||||||
|
val blocked: Boolean,
|
||||||
|
val profilePicture: UserPic
|
||||||
|
)
|
Loading…
Reference in New Issue
Block a user