From 71e48b9024e26ef20097cfe566a2c9c44e00659f Mon Sep 17 00:00:00 2001 From: jubb Date: Tue, 23 Feb 2021 17:13:57 +1100 Subject: [PATCH] refactor: move ProfileKeyUtil from app/ to libsession/ module and implement new config message sends --- .../securesms/ApplicationContext.java | 2 +- .../crypto/UnidentifiedAccessUtil.java | 1 + .../securesms/jobs/PushSendJob.java | 2 +- .../loki/activities/SettingsActivity.kt | 2 +- .../loki/utilities/OpenGroupUtilities.kt | 2 +- .../messages/control/ConfigurationMessage.kt | 25 ++++++++++++++++--- .../MessageReceiverHandler.kt | 1 + .../preferences}/ProfileKeyUtil.java | 2 +- 8 files changed, 28 insertions(+), 9 deletions(-) rename {app/src/main/java/org/thoughtcrime/securesms/crypto => libsession/src/main/java/org/session/libsession/utilities/preferences}/ProfileKeyUtil.java (96%) diff --git a/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java b/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java index a5c5697b3d..0c6d15960a 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java +++ b/app/src/main/java/org/thoughtcrime/securesms/ApplicationContext.java @@ -50,7 +50,7 @@ import org.thoughtcrime.securesms.sskenvironment.TypingStatusRepository; import org.thoughtcrime.securesms.components.TypingStatusSender; import org.thoughtcrime.securesms.crypto.IdentityKeyUtil; import org.thoughtcrime.securesms.crypto.MasterSecretUtil; -import org.thoughtcrime.securesms.crypto.ProfileKeyUtil; +import org.session.libsession.utilities.preferences.ProfileKeyUtil; import org.session.libsession.messaging.threads.Address; import org.thoughtcrime.securesms.database.DatabaseFactory; import org.thoughtcrime.securesms.database.GroupDatabase; diff --git a/app/src/main/java/org/thoughtcrime/securesms/crypto/UnidentifiedAccessUtil.java b/app/src/main/java/org/thoughtcrime/securesms/crypto/UnidentifiedAccessUtil.java index ad545f3c94..d4f047085d 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/crypto/UnidentifiedAccessUtil.java +++ b/app/src/main/java/org/thoughtcrime/securesms/crypto/UnidentifiedAccessUtil.java @@ -6,6 +6,7 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.WorkerThread; +import org.session.libsession.utilities.preferences.ProfileKeyUtil; import org.session.libsignal.metadata.SignalProtos; import org.session.libsignal.metadata.certificate.CertificateValidator; import org.session.libsignal.metadata.certificate.InvalidCertificateException; diff --git a/app/src/main/java/org/thoughtcrime/securesms/jobs/PushSendJob.java b/app/src/main/java/org/thoughtcrime/securesms/jobs/PushSendJob.java index d54bdf8d5a..85fbccc143 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/jobs/PushSendJob.java +++ b/app/src/main/java/org/thoughtcrime/securesms/jobs/PushSendJob.java @@ -18,7 +18,7 @@ import org.session.libsession.utilities.Util; import org.greenrobot.eventbus.EventBus; import org.thoughtcrime.securesms.ApplicationContext; import org.thoughtcrime.securesms.contactshare.ContactModelMapper; -import org.thoughtcrime.securesms.crypto.ProfileKeyUtil; +import org.session.libsession.utilities.preferences.ProfileKeyUtil; import org.session.libsession.messaging.threads.Address; import org.thoughtcrime.securesms.database.DatabaseFactory; import org.thoughtcrime.securesms.events.PartProgressEvent; diff --git a/app/src/main/java/org/thoughtcrime/securesms/loki/activities/SettingsActivity.kt b/app/src/main/java/org/thoughtcrime/securesms/loki/activities/SettingsActivity.kt index fc0c2b1235..59864f32b7 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/loki/activities/SettingsActivity.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/loki/activities/SettingsActivity.kt @@ -27,7 +27,7 @@ import nl.komponents.kovenant.ui.alwaysUi import org.thoughtcrime.securesms.ApplicationContext import org.thoughtcrime.securesms.PassphraseRequiredActionBarActivity import org.thoughtcrime.securesms.avatar.AvatarSelection -import org.thoughtcrime.securesms.crypto.ProfileKeyUtil +import org.session.libsession.utilities.preferences.ProfileKeyUtil import org.session.libsession.messaging.threads.Address import org.thoughtcrime.securesms.database.DatabaseFactory import org.thoughtcrime.securesms.loki.dialogs.ChangeUiModeDialog diff --git a/app/src/main/java/org/thoughtcrime/securesms/loki/utilities/OpenGroupUtilities.kt b/app/src/main/java/org/thoughtcrime/securesms/loki/utilities/OpenGroupUtilities.kt index 83d86f9f8b..45c792931a 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/loki/utilities/OpenGroupUtilities.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/loki/utilities/OpenGroupUtilities.kt @@ -4,7 +4,7 @@ import android.content.Context import androidx.annotation.WorkerThread import org.greenrobot.eventbus.EventBus import org.thoughtcrime.securesms.ApplicationContext -import org.thoughtcrime.securesms.crypto.ProfileKeyUtil +import org.session.libsession.utilities.preferences.ProfileKeyUtil import org.thoughtcrime.securesms.database.DatabaseFactory import org.thoughtcrime.securesms.groups.GroupManager import org.session.libsession.utilities.GroupUtil diff --git a/libsession/src/main/java/org/session/libsession/messaging/messages/control/ConfigurationMessage.kt b/libsession/src/main/java/org/session/libsession/messaging/messages/control/ConfigurationMessage.kt index ebf3236cab..6a03f48e5d 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/messages/control/ConfigurationMessage.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/messages/control/ConfigurationMessage.kt @@ -4,6 +4,8 @@ import com.google.protobuf.ByteString import org.session.libsession.messaging.MessagingConfiguration import org.session.libsession.messaging.threads.Address import org.session.libsession.utilities.GroupUtil +import org.session.libsession.utilities.TextSecurePreferences +import org.session.libsession.utilities.preferences.ProfileKeyUtil import org.session.libsignal.libsignal.ecc.DjbECPrivateKey import org.session.libsignal.libsignal.ecc.DjbECPublicKey import org.session.libsignal.libsignal.ecc.ECKeyPair @@ -12,7 +14,7 @@ import org.session.libsignal.service.loki.utilities.removing05PrefixIfNeeded import org.session.libsignal.service.loki.utilities.toHexString import org.session.libsignal.utilities.Hex -class ConfigurationMessage(val closedGroups: List, val openGroups: List): ControlMessage() { +class ConfigurationMessage(val closedGroups: List, val openGroups: List, val displayName: String, val profilePicture: String?, val profileKey: ByteArray): ControlMessage() { class ClosedGroup(val publicKey: String, val name: String, val encryptionKeyPair: ECKeyPair, val members: List, val admins: List) { val isValid: Boolean get() = members.isNotEmpty() && admins.isNotEmpty() @@ -57,7 +59,12 @@ class ConfigurationMessage(val closedGroups: List, val openGroups: fun getCurrent(): ConfigurationMessage { val closedGroups = mutableListOf() val openGroups = mutableListOf() - val storage = MessagingConfiguration.shared.storage + val sharedConfig = MessagingConfiguration.shared + val storage = sharedConfig.storage + val context = sharedConfig.context + val displayName = TextSecurePreferences.getProfileName(context)!! + val profilePicture = TextSecurePreferences.getProfilePictureURL(context) + val profileKey = ProfileKeyUtil.getProfileKey(context) val groups = storage.getAllGroups() for (groupRecord in groups) { if (groupRecord.isClosedGroup) { @@ -74,7 +81,8 @@ class ConfigurationMessage(val closedGroups: List, val openGroups: openGroups.add(openGroup.server) } } - return ConfigurationMessage(closedGroups, openGroups) + + return ConfigurationMessage(closedGroups, openGroups, displayName, profilePicture, profileKey) } fun fromProto(proto: SignalServiceProtos.Content): ConfigurationMessage? { @@ -82,7 +90,10 @@ class ConfigurationMessage(val closedGroups: List, val openGroups: val configurationProto = proto.configurationMessage val closedGroups = configurationProto.closedGroupsList.mapNotNull { ClosedGroup.fromProto(it) } val openGroups = configurationProto.openGroupsList - return ConfigurationMessage(closedGroups, openGroups) + val displayName = configurationProto.displayName + val profilePicture = configurationProto.profilePicture + val profileKey = configurationProto.profileKey + return ConfigurationMessage(closedGroups, openGroups, displayName, profilePicture, profileKey.toByteArray()) } } @@ -90,6 +101,9 @@ class ConfigurationMessage(val closedGroups: List, val openGroups: val configurationProto = SignalServiceProtos.ConfigurationMessage.newBuilder() configurationProto.addAllClosedGroups(closedGroups.mapNotNull { it.toProto() }) configurationProto.addAllOpenGroups(openGroups) + configurationProto.displayName = displayName + configurationProto.profilePicture = profilePicture + configurationProto.profileKey = ByteString.copyFrom(profileKey) val contentProto = SignalServiceProtos.Content.newBuilder() contentProto.configurationMessage = configurationProto.build() return contentProto.build() @@ -100,6 +114,9 @@ class ConfigurationMessage(val closedGroups: List, val openGroups: ConfigurationMessage( closedGroups: ${(closedGroups)} openGroups: ${(openGroups)} + displayName: $displayName + profilePicture: $profilePicture + profileKey: $profileKey ) """.trimIndent() } diff --git a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageReceiverHandler.kt b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageReceiverHandler.kt index b37c2eafe7..530badd329 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageReceiverHandler.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/MessageReceiverHandler.kt @@ -117,6 +117,7 @@ private fun MessageReceiver.handleConfigurationMessage(message: ConfigurationMes if (allOpenGroups.contains(openGroup)) continue storage.addOpenGroup(openGroup, 1) } + // TODO: in future handle the latest in config messages TextSecurePreferences.setConfigurationMessageSynced(context, true) } diff --git a/app/src/main/java/org/thoughtcrime/securesms/crypto/ProfileKeyUtil.java b/libsession/src/main/java/org/session/libsession/utilities/preferences/ProfileKeyUtil.java similarity index 96% rename from app/src/main/java/org/thoughtcrime/securesms/crypto/ProfileKeyUtil.java rename to libsession/src/main/java/org/session/libsession/utilities/preferences/ProfileKeyUtil.java index 6fd6e9a4c1..e5a51fe476 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/crypto/ProfileKeyUtil.java +++ b/libsession/src/main/java/org/session/libsession/utilities/preferences/ProfileKeyUtil.java @@ -1,4 +1,4 @@ -package org.thoughtcrime.securesms.crypto; +package org.session.libsession.utilities.preferences; import android.content.Context;