From fdede1c656239543a0b6d8266e6fc67afb678ebb Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Mon, 26 Apr 2021 11:39:23 +1000 Subject: [PATCH] Clean --- .../attachments/DatabaseAttachmentProvider.kt | 6 +- .../securesms/loki/activities/HomeActivity.kt | 4 +- .../securesms/loki/activities/PathActivity.kt | 2 +- .../loki/database/LokiAPIDatabase.kt | 2 +- .../database/MessageDataProvider.kt | 2 + .../messaging}/mentions/MentionsManager.kt | 15 +- .../messaging/open_groups/OpenGroup.kt | 4 - .../sending_receiving/pollers/Poller.kt | 2 +- .../libsession/snode/OnionRequestAPI.kt | 4 +- .../org/session/libsession/snode/SnodeAPI.kt | 2 +- .../libsession/snode/StorageProtocol.kt | 2 + .../messages/SignalServiceDataMessage.java | 3 +- .../messages/SignalServiceReceiptMessage.java | 4 +- .../messages/SignalServiceTypingMessage.java | 4 +- .../session/libsignal/service/loki}/Snode.kt | 4 +- .../service/loki/api/MessageWrapper.kt | 84 --------- .../libsignal/service/loki/api/Snode.kt | 34 ---- .../service/loki/api/opengroups/PublicChat.kt | 37 ---- .../loki/api/opengroups/PublicChatInfo.kt | 7 - .../loki/api/opengroups/PublicChatMessage.kt | 178 ------------------ .../loki/api/utilities/DecryptionUtilities.kt | 19 -- .../loki/api/utilities/EncryptionUtilities.kt | 45 ----- .../loki/database/LokiAPIDatabaseProtocol.kt | 2 +- .../database/LokiThreadDatabaseProtocol.kt | 11 -- .../service/loki/utilities/TTLUtilities.kt | 38 ---- 25 files changed, 31 insertions(+), 484 deletions(-) rename {libsignal/src/main/java/org/session/libsignal/service/loki/utilities => libsession/src/main/java/org/session/libsession/messaging}/mentions/MentionsManager.kt (78%) rename {libsession/src/main/java/org/session/libsession/snode => libsignal/src/main/java/org/session/libsignal/service/loki}/Snode.kt (87%) delete mode 100644 libsignal/src/main/java/org/session/libsignal/service/loki/api/MessageWrapper.kt delete mode 100644 libsignal/src/main/java/org/session/libsignal/service/loki/api/Snode.kt delete mode 100644 libsignal/src/main/java/org/session/libsignal/service/loki/api/opengroups/PublicChat.kt delete mode 100644 libsignal/src/main/java/org/session/libsignal/service/loki/api/opengroups/PublicChatInfo.kt delete mode 100644 libsignal/src/main/java/org/session/libsignal/service/loki/api/opengroups/PublicChatMessage.kt delete mode 100644 libsignal/src/main/java/org/session/libsignal/service/loki/api/utilities/DecryptionUtilities.kt delete mode 100644 libsignal/src/main/java/org/session/libsignal/service/loki/api/utilities/EncryptionUtilities.kt delete mode 100644 libsignal/src/main/java/org/session/libsignal/service/loki/database/LokiThreadDatabaseProtocol.kt delete mode 100644 libsignal/src/main/java/org/session/libsignal/service/loki/utilities/TTLUtilities.kt diff --git a/app/src/main/java/org/thoughtcrime/securesms/attachments/DatabaseAttachmentProvider.kt b/app/src/main/java/org/thoughtcrime/securesms/attachments/DatabaseAttachmentProvider.kt index 963b6c1299..c582f909e1 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/attachments/DatabaseAttachmentProvider.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/attachments/DatabaseAttachmentProvider.kt @@ -5,6 +5,7 @@ import android.text.TextUtils import com.google.protobuf.ByteString import org.greenrobot.eventbus.EventBus import org.session.libsession.database.MessageDataProvider +import org.session.libsession.messaging.open_groups.OpenGroup import org.session.libsession.messaging.sending_receiving.attachments.* import org.session.libsession.messaging.threads.Address import org.session.libsession.messaging.utilities.DotNetAPI @@ -26,7 +27,6 @@ import org.thoughtcrime.securesms.util.MediaUtil import java.io.IOException import java.io.InputStream - class DatabaseAttachmentProvider(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper), MessageDataProvider { override fun getAttachmentStream(attachmentId: Long): SessionServiceAttachmentStream? { @@ -104,6 +104,10 @@ class DatabaseAttachmentProvider(context: Context, helper: SQLCipherOpenHelper) return smsDatabase.isOutgoingMessage(timestamp) || mmsDatabase.isOutgoingMessage(timestamp) } + override fun getOpenGroup(threadID: Long): OpenGroup? { + return null // TODO: Implement + } + override fun updateAttachmentAfterUploadSucceeded(attachmentId: Long, attachmentStream: SignalServiceAttachmentStream, attachmentKey: ByteArray, uploadResult: DotNetAPI.UploadResult) { val database = DatabaseFactory.getAttachmentDatabase(context) val databaseAttachment = getDatabaseAttachment(attachmentId) ?: return diff --git a/app/src/main/java/org/thoughtcrime/securesms/loki/activities/HomeActivity.kt b/app/src/main/java/org/thoughtcrime/securesms/loki/activities/HomeActivity.kt index a8ed5f2c74..b458241798 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/loki/activities/HomeActivity.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/loki/activities/HomeActivity.kt @@ -30,10 +30,10 @@ import org.greenrobot.eventbus.EventBus import org.greenrobot.eventbus.Subscribe import org.greenrobot.eventbus.ThreadMode import org.session.libsession.messaging.jobs.JobQueue +import org.session.libsession.messaging.mentions.MentionsManager import org.session.libsession.messaging.open_groups.OpenGroupAPI import org.session.libsession.messaging.sending_receiving.MessageSender import org.session.libsession.utilities.* -import org.session.libsignal.service.loki.utilities.mentions.MentionsManager import org.session.libsignal.service.loki.utilities.toHexString import org.session.libsignal.utilities.ThreadUtils import org.thoughtcrime.securesms.ApplicationContext @@ -139,7 +139,7 @@ class HomeActivity : PassphraseRequiredActionBarActivity(), val userDB = DatabaseFactory.getLokiUserDatabase(this) val userPublicKey = TextSecurePreferences.getLocalNumber(this) if (userPublicKey != null) { - MentionsManager.configureIfNeeded(userPublicKey, threadDB, userDB) + MentionsManager.configureIfNeeded(userPublicKey, userDB) application.publicChatManager.startPollersIfNeeded() JobQueue.shared.resumePendingJobs() } diff --git a/app/src/main/java/org/thoughtcrime/securesms/loki/activities/PathActivity.kt b/app/src/main/java/org/thoughtcrime/securesms/loki/activities/PathActivity.kt index 482a276328..f4f708aeda 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/loki/activities/PathActivity.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/loki/activities/PathActivity.kt @@ -24,7 +24,7 @@ import org.thoughtcrime.securesms.PassphraseRequiredActionBarActivity import org.thoughtcrime.securesms.loki.utilities.* import org.thoughtcrime.securesms.loki.views.GlowViewUtilities import org.thoughtcrime.securesms.loki.views.PathDotView -import org.session.libsignal.service.loki.api.Snode +import org.session.libsignal.service.loki.Snode class PathActivity : PassphraseRequiredActionBarActivity() { private val broadcastReceivers = mutableListOf() diff --git a/app/src/main/java/org/thoughtcrime/securesms/loki/database/LokiAPIDatabase.kt b/app/src/main/java/org/thoughtcrime/securesms/loki/database/LokiAPIDatabase.kt index a9325a5527..58b9aeeec7 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/loki/database/LokiAPIDatabase.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/loki/database/LokiAPIDatabase.kt @@ -7,7 +7,7 @@ import org.session.libsession.utilities.TextSecurePreferences import org.session.libsignal.libsignal.ecc.DjbECPrivateKey import org.session.libsignal.libsignal.ecc.DjbECPublicKey import org.session.libsignal.libsignal.ecc.ECKeyPair -import org.session.libsignal.service.loki.api.Snode +import org.session.libsignal.service.loki.Snode import org.session.libsignal.service.loki.database.LokiAPIDatabaseProtocol import org.session.libsignal.service.loki.utilities.PublicKeyValidation import org.session.libsignal.service.loki.utilities.removing05PrefixIfNeeded diff --git a/libsession/src/main/java/org/session/libsession/database/MessageDataProvider.kt b/libsession/src/main/java/org/session/libsession/database/MessageDataProvider.kt index f28001cc7c..1584f41de4 100644 --- a/libsession/src/main/java/org/session/libsession/database/MessageDataProvider.kt +++ b/libsession/src/main/java/org/session/libsession/database/MessageDataProvider.kt @@ -1,5 +1,6 @@ package org.session.libsession.database +import org.session.libsession.messaging.open_groups.OpenGroup import org.session.libsession.messaging.sending_receiving.attachments.* import org.session.libsession.messaging.threads.Address import org.session.libsession.messaging.utilities.DotNetAPI @@ -37,4 +38,5 @@ interface MessageDataProvider { fun getAttachmentIDsFor(messageID: Long): List fun getLinkPreviewAttachmentIDFor(messageID: Long): Long? + fun getOpenGroup(threadID: Long): OpenGroup? } \ No newline at end of file diff --git a/libsignal/src/main/java/org/session/libsignal/service/loki/utilities/mentions/MentionsManager.kt b/libsession/src/main/java/org/session/libsession/messaging/mentions/MentionsManager.kt similarity index 78% rename from libsignal/src/main/java/org/session/libsignal/service/loki/utilities/mentions/MentionsManager.kt rename to libsession/src/main/java/org/session/libsession/messaging/mentions/MentionsManager.kt index 2edf6f677c..715204825a 100644 --- a/libsignal/src/main/java/org/session/libsignal/service/loki/utilities/mentions/MentionsManager.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/mentions/MentionsManager.kt @@ -1,19 +1,20 @@ -package org.session.libsignal.service.loki.utilities.mentions +package org.session.libsession.messaging.mentions + +import org.session.libsession.messaging.MessagingModuleConfiguration +import org.session.libsignal.service.loki.utilities.mentions.Mention -import org.session.libsignal.service.loki.database.LokiThreadDatabaseProtocol import org.session.libsignal.service.loki.database.LokiUserDatabaseProtocol -class MentionsManager(private val userPublicKey: String, private val threadDatabase: LokiThreadDatabaseProtocol, - private val userDatabase: LokiUserDatabaseProtocol) { +class MentionsManager(private val userPublicKey: String, private val userDatabase: LokiUserDatabaseProtocol) { var userPublicKeyCache = mutableMapOf>() // Thread ID to set of user hex encoded public keys companion object { public lateinit var shared: MentionsManager - public fun configureIfNeeded(userPublicKey: String, threadDatabase: LokiThreadDatabaseProtocol, userDatabase: LokiUserDatabaseProtocol) { + public fun configureIfNeeded(userPublicKey: String, userDatabase: LokiUserDatabaseProtocol) { if (::shared.isInitialized) { return; } - shared = MentionsManager(userPublicKey, threadDatabase, userDatabase) + shared = MentionsManager(userPublicKey, userDatabase) } } @@ -30,7 +31,7 @@ class MentionsManager(private val userPublicKey: String, private val threadDatab // Prepare val cache = userPublicKeyCache[threadID] ?: return listOf() // Gather candidates - val publicChat = threadDatabase.getPublicChat(threadID) + val publicChat = MessagingModuleConfiguration.shared.messageDataProvider.getOpenGroup(threadID) var candidates: List = cache.mapNotNull { publicKey -> val displayName: String? if (publicChat != null) { diff --git a/libsession/src/main/java/org/session/libsession/messaging/open_groups/OpenGroup.kt b/libsession/src/main/java/org/session/libsession/messaging/open_groups/OpenGroup.kt index cf89c9009e..a39b22ad35 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/open_groups/OpenGroup.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/open_groups/OpenGroup.kt @@ -1,6 +1,5 @@ package org.session.libsession.messaging.open_groups -import org.session.libsignal.service.loki.api.opengroups.PublicChat import org.session.libsignal.utilities.JsonUtil data class OpenGroup( @@ -14,9 +13,6 @@ data class OpenGroup( companion object { - @JvmStatic fun from(publicChat: PublicChat): OpenGroup = - OpenGroup(publicChat.channel, publicChat.server, publicChat.displayName, publicChat.isDeletable) - @JvmStatic fun getId(channel: Long, server: String): String { return "$server.$channel" } diff --git a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/pollers/Poller.kt b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/pollers/Poller.kt index d8187bad03..cbbb1b831c 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/pollers/Poller.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/sending_receiving/pollers/Poller.kt @@ -8,7 +8,7 @@ import org.session.libsession.messaging.jobs.MessageReceiveJob import org.session.libsession.messaging.utilities.MessageWrapper import org.session.libsession.snode.SnodeAPI import org.session.libsession.snode.SnodeModule -import org.session.libsignal.service.loki.api.Snode +import org.session.libsignal.service.loki.Snode import org.session.libsignal.utilities.Base64 import org.session.libsignal.utilities.logging.Log import java.security.SecureRandom diff --git a/libsession/src/main/java/org/session/libsession/snode/OnionRequestAPI.kt b/libsession/src/main/java/org/session/libsession/snode/OnionRequestAPI.kt index 4447c494e5..6a9bd70fdf 100644 --- a/libsession/src/main/java/org/session/libsession/snode/OnionRequestAPI.kt +++ b/libsession/src/main/java/org/session/libsession/snode/OnionRequestAPI.kt @@ -11,7 +11,7 @@ import org.session.libsession.utilities.AESGCM import org.session.libsignal.utilities.logging.Log import org.session.libsignal.utilities.Base64 import org.session.libsignal.utilities.* -import org.session.libsignal.service.loki.api.Snode +import org.session.libsignal.service.loki.Snode import org.session.libsignal.service.loki.api.utilities.* import org.session.libsession.utilities.AESGCM.EncryptionResult import org.session.libsignal.utilities.ThreadUtils @@ -79,7 +79,7 @@ object OnionRequestAPI { ) internal sealed class Destination { - class Snode(val snode: org.session.libsignal.service.loki.api.Snode) : Destination() + class Snode(val snode: org.session.libsignal.service.loki.Snode) : Destination() class Server(val host: String, val target: String, val x25519PublicKey: String) : Destination() } diff --git a/libsession/src/main/java/org/session/libsession/snode/SnodeAPI.kt b/libsession/src/main/java/org/session/libsession/snode/SnodeAPI.kt index 1d90331e9a..5a0baa90b5 100644 --- a/libsession/src/main/java/org/session/libsession/snode/SnodeAPI.kt +++ b/libsession/src/main/java/org/session/libsession/snode/SnodeAPI.kt @@ -7,7 +7,7 @@ import nl.komponents.kovenant.* import nl.komponents.kovenant.functional.bind import nl.komponents.kovenant.functional.map import org.session.libsession.snode.utilities.getRandomElement -import org.session.libsignal.service.loki.api.Snode +import org.session.libsignal.service.loki.Snode import org.session.libsignal.service.loki.api.utilities.HTTP import org.session.libsignal.service.loki.database.LokiAPIDatabaseProtocol import org.session.libsignal.service.loki.utilities.Broadcaster diff --git a/libsession/src/main/java/org/session/libsession/snode/StorageProtocol.kt b/libsession/src/main/java/org/session/libsession/snode/StorageProtocol.kt index 85ab507114..7e0d103974 100644 --- a/libsession/src/main/java/org/session/libsession/snode/StorageProtocol.kt +++ b/libsession/src/main/java/org/session/libsession/snode/StorageProtocol.kt @@ -1,5 +1,7 @@ package org.session.libsession.snode +import org.session.libsignal.service.loki.Snode + interface SnodeStorageProtocol { fun getSnodePool(): Set diff --git a/libsignal/src/main/java/org/session/libsignal/service/api/messages/SignalServiceDataMessage.java b/libsignal/src/main/java/org/session/libsignal/service/api/messages/SignalServiceDataMessage.java index cbc8c8fafe..f65568bc42 100644 --- a/libsignal/src/main/java/org/session/libsignal/service/api/messages/SignalServiceDataMessage.java +++ b/libsignal/src/main/java/org/session/libsignal/service/api/messages/SignalServiceDataMessage.java @@ -10,7 +10,6 @@ import org.session.libsignal.libsignal.util.guava.Optional; import org.session.libsignal.service.api.messages.shared.SharedContact; import org.session.libsignal.service.api.push.SignalServiceAddress; import org.session.libsignal.service.internal.push.SignalServiceProtos.DataMessage.ClosedGroupControlMessage; -import org.session.libsignal.service.loki.utilities.TTLUtilities; import java.util.LinkedList; import java.util.List; @@ -247,7 +246,7 @@ public class SignalServiceDataMessage { } public int getTTL() { - return TTLUtilities.getTTL(TTLUtilities.MessageType.Regular); + return 0; } public static class Builder { diff --git a/libsignal/src/main/java/org/session/libsignal/service/api/messages/SignalServiceReceiptMessage.java b/libsignal/src/main/java/org/session/libsignal/service/api/messages/SignalServiceReceiptMessage.java index 8e48450011..83d891eb78 100644 --- a/libsignal/src/main/java/org/session/libsignal/service/api/messages/SignalServiceReceiptMessage.java +++ b/libsignal/src/main/java/org/session/libsignal/service/api/messages/SignalServiceReceiptMessage.java @@ -1,8 +1,6 @@ package org.session.libsignal.service.api.messages; -import org.session.libsignal.service.loki.utilities.TTLUtilities; - import java.util.List; public class SignalServiceReceiptMessage { @@ -41,5 +39,5 @@ public class SignalServiceReceiptMessage { return type == Type.READ; } - public int getTTL() { return TTLUtilities.getTTL(TTLUtilities.MessageType.Receipt); } + public int getTTL() { return 0; } } diff --git a/libsignal/src/main/java/org/session/libsignal/service/api/messages/SignalServiceTypingMessage.java b/libsignal/src/main/java/org/session/libsignal/service/api/messages/SignalServiceTypingMessage.java index d3fb3a2fc0..81a4879ffc 100644 --- a/libsignal/src/main/java/org/session/libsignal/service/api/messages/SignalServiceTypingMessage.java +++ b/libsignal/src/main/java/org/session/libsignal/service/api/messages/SignalServiceTypingMessage.java @@ -1,7 +1,5 @@ package org.session.libsignal.service.api.messages; -import org.session.libsignal.service.loki.utilities.TTLUtilities; - public class SignalServiceTypingMessage { public enum Action { @@ -32,5 +30,5 @@ public class SignalServiceTypingMessage { return action == Action.STOPPED; } - public int getTTL() { return TTLUtilities.getTTL(TTLUtilities.MessageType.TypingIndicator); } + public int getTTL() { return 0; } } diff --git a/libsession/src/main/java/org/session/libsession/snode/Snode.kt b/libsignal/src/main/java/org/session/libsignal/service/loki/Snode.kt similarity index 87% rename from libsession/src/main/java/org/session/libsession/snode/Snode.kt rename to libsignal/src/main/java/org/session/libsignal/service/loki/Snode.kt index 2bb2429eca..8c4f22854f 100644 --- a/libsession/src/main/java/org/session/libsession/snode/Snode.kt +++ b/libsignal/src/main/java/org/session/libsignal/service/loki/Snode.kt @@ -1,9 +1,9 @@ -package org.session.libsession.snode +package org.session.libsignal.service.loki class Snode(val address: String, val port: Int, val publicKeySet: KeySet?) { val ip: String get() = address.removePrefix("https://") - internal enum class Method(val rawValue: String) { + public enum class Method(val rawValue: String) { GetSwarm("get_snodes_for_pubkey"), GetMessages("retrieve"), SendMessage("store") diff --git a/libsignal/src/main/java/org/session/libsignal/service/loki/api/MessageWrapper.kt b/libsignal/src/main/java/org/session/libsignal/service/loki/api/MessageWrapper.kt deleted file mode 100644 index 2cb8dd0440..0000000000 --- a/libsignal/src/main/java/org/session/libsignal/service/loki/api/MessageWrapper.kt +++ /dev/null @@ -1,84 +0,0 @@ -package org.session.libsignal.service.loki.api - -import com.google.protobuf.ByteString -import org.session.libsignal.utilities.logging.Log -import org.session.libsignal.service.internal.push.SignalServiceProtos.Envelope -import org.session.libsignal.utilities.Base64 -import org.session.libsignal.service.internal.websocket.WebSocketProtos.WebSocketMessage -import org.session.libsignal.service.internal.websocket.WebSocketProtos.WebSocketRequestMessage -import java.security.SecureRandom - -object MessageWrapper { - - // region Types - sealed class Error(val description: String) : Exception() { - object FailedToWrapData : Error("Failed to wrap data.") - object FailedToWrapMessageInEnvelope : Error("Failed to wrap message in envelope.") - object FailedToWrapEnvelopeInWebSocketMessage : Error("Failed to wrap envelope in web socket message.") - object FailedToUnwrapData : Error("Failed to unwrap data.") - } - // endregion - - // region Wrapping - /** - * Wraps `message` in a `SignalServiceProtos.Envelope` and then a `WebSocketProtos.WebSocketMessage` to match the desktop application. - */ - fun wrap(message: SignalMessageInfo): ByteArray { - try { - val envelope = createEnvelope(message) - val webSocketMessage = createWebSocketMessage(envelope) - return webSocketMessage.toByteArray() - } catch (e: Exception) { - throw if (e is Error) { e } else { Error.FailedToWrapData } - } - } - - private fun createEnvelope(message: SignalMessageInfo): Envelope { - try { - val builder = Envelope.newBuilder() - builder.type = message.type - builder.timestamp = message.timestamp - builder.source = message.senderPublicKey - builder.sourceDevice = message.senderDeviceID - builder.content = ByteString.copyFrom(Base64.decode(message.content)) - return builder.build() - } catch (e: Exception) { - Log.d("Loki", "Failed to wrap message in envelope: ${e.message}.") - throw Error.FailedToWrapMessageInEnvelope - } - } - - private fun createWebSocketMessage(envelope: Envelope): WebSocketMessage { - try { - val requestBuilder = WebSocketRequestMessage.newBuilder() - requestBuilder.verb = "PUT" - requestBuilder.path = "/api/v1/message" - requestBuilder.id = SecureRandom.getInstance("SHA1PRNG").nextLong() - requestBuilder.body = envelope.toByteString() - val messageBuilder = WebSocketMessage.newBuilder() - messageBuilder.request = requestBuilder.build() - messageBuilder.type = WebSocketMessage.Type.REQUEST - return messageBuilder.build() - } catch (e: Exception) { - Log.d("Loki", "Failed to wrap envelope in web socket message: ${e.message}.") - throw Error.FailedToWrapEnvelopeInWebSocketMessage - } - } - // endregion - - // region Unwrapping - /** - * `data` shouldn't be base 64 encoded. - */ - fun unwrap(data: ByteArray): Envelope { - try { - val webSocketMessage = WebSocketMessage.parseFrom(data) - val envelopeAsData = webSocketMessage.request.body - return Envelope.parseFrom(envelopeAsData) - } catch (e: Exception) { - Log.d("Loki", "Failed to unwrap data: ${e.message}.") - throw Error.FailedToUnwrapData - } - } - // endregion -} diff --git a/libsignal/src/main/java/org/session/libsignal/service/loki/api/Snode.kt b/libsignal/src/main/java/org/session/libsignal/service/loki/api/Snode.kt deleted file mode 100644 index 72c0eb1d4d..0000000000 --- a/libsignal/src/main/java/org/session/libsignal/service/loki/api/Snode.kt +++ /dev/null @@ -1,34 +0,0 @@ -package org.session.libsignal.service.loki.api - -public class Snode(val address: String, val port: Int, val publicKeySet: KeySet?) { - - val ip: String get() = address.removePrefix("https://") - - enum class Method(val rawValue: String) { - /** - * Only supported by snode targets. - */ - GetSwarm("get_snodes_for_pubkey"), - /** - * Only supported by snode targets. - */ - GetMessages("retrieve"), - SendMessage("store") - } - - data class KeySet(val ed25519Key: String, val x25519Key: String) - - override fun equals(other: Any?): Boolean { - return if (other is Snode) { - address == other.address && port == other.port - } else { - false - } - } - - override fun hashCode(): Int { - return address.hashCode() xor port.hashCode() - } - - override fun toString(): String { return "$address:$port" } -} diff --git a/libsignal/src/main/java/org/session/libsignal/service/loki/api/opengroups/PublicChat.kt b/libsignal/src/main/java/org/session/libsignal/service/loki/api/opengroups/PublicChat.kt deleted file mode 100644 index 1cf9ada3cc..0000000000 --- a/libsignal/src/main/java/org/session/libsignal/service/loki/api/opengroups/PublicChat.kt +++ /dev/null @@ -1,37 +0,0 @@ -package org.session.libsignal.service.loki.api.opengroups - -import org.session.libsignal.utilities.JsonUtil - -public data class PublicChat( - public val channel: Long, - private val serverURL: String, - public val displayName: String, - public val isDeletable: Boolean -) { - public val server get() = serverURL.toLowerCase() - public val id get() = getId(channel, server) - - companion object { - - @JvmStatic fun getId(channel: Long, server: String): String { - return "$server.$channel" - } - - @JvmStatic fun fromJSON(jsonAsString: String): PublicChat? { - try { - val json = JsonUtil.fromJson(jsonAsString) - val channel = json.get("channel").asLong() - val server = json.get("server").asText().toLowerCase() - val displayName = json.get("displayName").asText() - val isDeletable = json.get("isDeletable").asBoolean() - return PublicChat(channel, server, displayName, isDeletable) - } catch (e: Exception) { - return null - } - } - } - - public fun toJSON(): Map { - return mapOf( "channel" to channel, "server" to server, "displayName" to displayName, "isDeletable" to isDeletable ) - } -} diff --git a/libsignal/src/main/java/org/session/libsignal/service/loki/api/opengroups/PublicChatInfo.kt b/libsignal/src/main/java/org/session/libsignal/service/loki/api/opengroups/PublicChatInfo.kt deleted file mode 100644 index 78395634fd..0000000000 --- a/libsignal/src/main/java/org/session/libsignal/service/loki/api/opengroups/PublicChatInfo.kt +++ /dev/null @@ -1,7 +0,0 @@ -package org.session.libsignal.service.loki.api.opengroups - -public data class PublicChatInfo ( - public val displayName: String, - public val profilePictureURL: String, - public val memberCount: Int -) diff --git a/libsignal/src/main/java/org/session/libsignal/service/loki/api/opengroups/PublicChatMessage.kt b/libsignal/src/main/java/org/session/libsignal/service/loki/api/opengroups/PublicChatMessage.kt deleted file mode 100644 index 4fdc75a69a..0000000000 --- a/libsignal/src/main/java/org/session/libsignal/service/loki/api/opengroups/PublicChatMessage.kt +++ /dev/null @@ -1,178 +0,0 @@ -package org.session.libsignal.service.loki.api.opengroups - -import org.whispersystems.curve25519.Curve25519 -import org.session.libsignal.utilities.logging.Log -import org.session.libsignal.utilities.Hex -import org.session.libsignal.service.loki.utilities.removing05PrefixIfNeeded - -public data class PublicChatMessage( - public val serverID: Long?, - public val senderPublicKey: String, - public val displayName: String, - public val body: String, - public val timestamp: Long, - public val type: String, - public val quote: Quote?, - public val attachments: List, - public val profilePicture: ProfilePicture?, - public val signature: Signature?, - public val serverTimestamp: Long -) { - - // region Settings - companion object { - private val curve = Curve25519.getInstance(Curve25519.BEST) - private val signatureVersion: Long = 1 - private val attachmentType = "net.app.core.oembed" - } - // endregion - - // region Types - public data class ProfilePicture( - public val profileKey: ByteArray, - public val url: String - ) - - public data class Quote( - public val quotedMessageTimestamp: Long, - public val quoteePublicKey: String, - public val quotedMessageBody: String, - public val quotedMessageServerID: Long? = null - ) - - public data class Signature( - public val data: ByteArray, - public val version: Long - ) - - public data class Attachment( - public val kind: Kind, - public val server: String, - public val serverID: Long, - public val contentType: String, - public val size: Int, - public val fileName: String, - public val flags: Int, - public val width: Int, - public val height: Int, - public val caption: String?, - public val url: String, - /** - Guaranteed to be non-`nil` if `kind` is `LinkPreview`. - */ - public val linkPreviewURL: String?, - /** - Guaranteed to be non-`nil` if `kind` is `LinkPreview`. - */ - public val linkPreviewTitle: String? - ) { - public val dotNetAPIType = when { - contentType.startsWith("image") -> "photo" - contentType.startsWith("video") -> "video" - contentType.startsWith("audio") -> "audio" - else -> "other" - } - - public enum class Kind(val rawValue: String) { - Attachment("attachment"), LinkPreview("preview") - } - } - // endregion - - // region Initialization - constructor(hexEncodedPublicKey: String, displayName: String, body: String, timestamp: Long, type: String, quote: Quote?, attachments: List) - : this(null, hexEncodedPublicKey, displayName, body, timestamp, type, quote, attachments, null, null, 0) - // endregion - - // region Crypto - internal fun sign(privateKey: ByteArray): PublicChatMessage? { - val data = getValidationData(signatureVersion) - if (data == null) { - Log.d("Loki", "Failed to sign public chat message.") - return null - } - try { - val signatureData = curve.calculateSignature(privateKey, data) - val signature = Signature(signatureData, signatureVersion) - return copy(signature = signature) - } catch (e: Exception) { - Log.d("Loki", "Failed to sign public chat message due to error: ${e.message}.") - return null - } - } - - internal fun hasValidSignature(): Boolean { - if (signature == null) { return false } - val data = getValidationData(signature.version) ?: return false - val publicKey = Hex.fromStringCondensed(senderPublicKey.removing05PrefixIfNeeded()) - try { - return curve.verifySignature(publicKey, data, signature.data) - } catch (e: Exception) { - Log.d("Loki", "Failed to verify public chat message due to error: ${e.message}.") - return false - } - } - // endregion - - // region Parsing - internal fun toJSON(): Map { - val value = mutableMapOf( "timestamp" to timestamp ) - if (quote != null) { - value["quote"] = mapOf( "id" to quote.quotedMessageTimestamp, "author" to quote.quoteePublicKey, "text" to quote.quotedMessageBody ) - } - if (signature != null) { - value["sig"] = Hex.toStringCondensed(signature.data) - value["sigver"] = signature.version - } - val annotation = mapOf( "type" to type, "value" to value ) - val annotations = mutableListOf( annotation ) - attachments.forEach { attachment -> - val attachmentValue = mutableMapOf( - // Fields required by the .NET API - "version" to 1, - "type" to attachment.dotNetAPIType, - // Custom fields - "lokiType" to attachment.kind.rawValue, - "server" to attachment.server, - "id" to attachment.serverID, - "contentType" to attachment.contentType, - "size" to attachment.size, - "fileName" to attachment.fileName, - "flags" to attachment.flags, - "width" to attachment.width, - "height" to attachment.height, - "url" to attachment.url - ) - if (attachment.caption != null) { attachmentValue["caption"] = attachment.caption } - if (attachment.linkPreviewURL != null) { attachmentValue["linkPreviewUrl"] = attachment.linkPreviewURL } - if (attachment.linkPreviewTitle != null) { attachmentValue["linkPreviewTitle"] = attachment.linkPreviewTitle } - val attachmentAnnotation = mapOf( "type" to attachmentType, "value" to attachmentValue ) - annotations.add(attachmentAnnotation) - } - val result = mutableMapOf( "text" to body, "annotations" to annotations ) - if (quote?.quotedMessageServerID != null) { - result["reply_to"] = quote.quotedMessageServerID - } - return result - } - // endregion - - // region Convenience - private fun getValidationData(signatureVersion: Long): ByteArray? { - var string = "${body.trim()}$timestamp" - if (quote != null) { - string += "${quote.quotedMessageTimestamp}${quote.quoteePublicKey}${quote.quotedMessageBody.trim()}" - if (quote.quotedMessageServerID != null) { - string += "${quote.quotedMessageServerID}" - } - } - string += attachments.sortedBy { it.serverID }.map { it.serverID }.joinToString("") - string += "$signatureVersion" - try { - return string.toByteArray(Charsets.UTF_8) - } catch (exception: Exception) { - return null - } - } - // endregion -} diff --git a/libsignal/src/main/java/org/session/libsignal/service/loki/api/utilities/DecryptionUtilities.kt b/libsignal/src/main/java/org/session/libsignal/service/loki/api/utilities/DecryptionUtilities.kt deleted file mode 100644 index 017a554b9d..0000000000 --- a/libsignal/src/main/java/org/session/libsignal/service/loki/api/utilities/DecryptionUtilities.kt +++ /dev/null @@ -1,19 +0,0 @@ -package org.session.libsignal.service.loki.api.utilities - -import javax.crypto.Cipher -import javax.crypto.spec.GCMParameterSpec -import javax.crypto.spec.SecretKeySpec - -internal object DecryptionUtilities { - - /** - * Sync. Don't call from the main thread. - */ - internal fun decryptUsingAESGCM(ivAndCiphertext: ByteArray, symmetricKey: ByteArray): ByteArray { - val iv = ivAndCiphertext.sliceArray(0 until EncryptionUtilities.ivSize) - val ciphertext = ivAndCiphertext.sliceArray(EncryptionUtilities.ivSize until ivAndCiphertext.count()) - val cipher = Cipher.getInstance("AES/GCM/NoPadding") - cipher.init(Cipher.DECRYPT_MODE, SecretKeySpec(symmetricKey, "AES"), GCMParameterSpec(EncryptionUtilities.gcmTagSize, iv)) - return cipher.doFinal(ciphertext) - } -} diff --git a/libsignal/src/main/java/org/session/libsignal/service/loki/api/utilities/EncryptionUtilities.kt b/libsignal/src/main/java/org/session/libsignal/service/loki/api/utilities/EncryptionUtilities.kt deleted file mode 100644 index 3ffb480244..0000000000 --- a/libsignal/src/main/java/org/session/libsignal/service/loki/api/utilities/EncryptionUtilities.kt +++ /dev/null @@ -1,45 +0,0 @@ -package org.session.libsignal.service.loki.api.utilities - -import org.whispersystems.curve25519.Curve25519 -import org.session.libsignal.libsignal.util.ByteUtil -import org.session.libsignal.utilities.Hex -import org.session.libsignal.service.internal.util.Util -import javax.crypto.Cipher -import javax.crypto.Mac -import javax.crypto.spec.GCMParameterSpec -import javax.crypto.spec.SecretKeySpec - -internal data class EncryptionResult( - internal val ciphertext: ByteArray, - internal val symmetricKey: ByteArray, - internal val ephemeralPublicKey: ByteArray -) - -internal object EncryptionUtilities { - internal val gcmTagSize = 128 - internal val ivSize = 12 - - /** - * Sync. Don't call from the main thread. - */ - internal fun encryptUsingAESGCM(plaintext: ByteArray, symmetricKey: ByteArray): ByteArray { - val iv = Util.getSecretBytes(ivSize) - val cipher = Cipher.getInstance("AES/GCM/NoPadding") - cipher.init(Cipher.ENCRYPT_MODE, SecretKeySpec(symmetricKey, "AES"), GCMParameterSpec(gcmTagSize, iv)) - return ByteUtil.combine(iv, cipher.doFinal(plaintext)) - } - - /** - * Sync. Don't call from the main thread. - */ - internal fun encryptForX25519PublicKey(plaintext: ByteArray, hexEncodedX25519PublicKey: String): EncryptionResult { - val x25519PublicKey = Hex.fromStringCondensed(hexEncodedX25519PublicKey) - val ephemeralKeyPair = Curve25519.getInstance(Curve25519.BEST).generateKeyPair() - val ephemeralSharedSecret = Curve25519.getInstance(Curve25519.BEST).calculateAgreement(x25519PublicKey, ephemeralKeyPair.privateKey) - val mac = Mac.getInstance("HmacSHA256") - mac.init(SecretKeySpec("LOKI".toByteArray(), "HmacSHA256")) - val symmetricKey = mac.doFinal(ephemeralSharedSecret) - val ciphertext = encryptUsingAESGCM(plaintext, symmetricKey) - return EncryptionResult(ciphertext, symmetricKey, ephemeralKeyPair.publicKey) - } -} diff --git a/libsignal/src/main/java/org/session/libsignal/service/loki/database/LokiAPIDatabaseProtocol.kt b/libsignal/src/main/java/org/session/libsignal/service/loki/database/LokiAPIDatabaseProtocol.kt index 0d24f218ba..d564706ee8 100644 --- a/libsignal/src/main/java/org/session/libsignal/service/loki/database/LokiAPIDatabaseProtocol.kt +++ b/libsignal/src/main/java/org/session/libsignal/service/loki/database/LokiAPIDatabaseProtocol.kt @@ -1,7 +1,7 @@ package org.session.libsignal.service.loki.database import org.session.libsignal.libsignal.ecc.ECKeyPair -import org.session.libsignal.service.loki.api.Snode +import org.session.libsignal.service.loki.Snode import java.util.* interface LokiAPIDatabaseProtocol { diff --git a/libsignal/src/main/java/org/session/libsignal/service/loki/database/LokiThreadDatabaseProtocol.kt b/libsignal/src/main/java/org/session/libsignal/service/loki/database/LokiThreadDatabaseProtocol.kt deleted file mode 100644 index 5eb69da655..0000000000 --- a/libsignal/src/main/java/org/session/libsignal/service/loki/database/LokiThreadDatabaseProtocol.kt +++ /dev/null @@ -1,11 +0,0 @@ -package org.session.libsignal.service.loki.database - -import org.session.libsignal.service.loki.api.opengroups.PublicChat - -interface LokiThreadDatabaseProtocol { - - fun getThreadID(publicKey: String): Long - fun getPublicChat(threadID: Long): PublicChat? - fun setPublicChat(publicChat: PublicChat, threadID: Long) - fun removePublicChat(threadID: Long) -} diff --git a/libsignal/src/main/java/org/session/libsignal/service/loki/utilities/TTLUtilities.kt b/libsignal/src/main/java/org/session/libsignal/service/loki/utilities/TTLUtilities.kt deleted file mode 100644 index c229a97423..0000000000 --- a/libsignal/src/main/java/org/session/libsignal/service/loki/utilities/TTLUtilities.kt +++ /dev/null @@ -1,38 +0,0 @@ -package org.session.libsignal.service.loki.utilities - -public object TTLUtilities { - - /** - * If a message type specifies an invalid TTL, this will be used. - */ - public val fallbackMessageTTL = 2 * 24 * 60 * 60 * 1000 - - public enum class MessageType { - // Unimportant control messages - Address, Call, TypingIndicator, Verified, - // Somewhat important control messages - DeviceLink, - // Important control messages - ClosedGroupUpdate, Ephemeral, SessionRequest, Receipt, Sync, DeviceUnlinkingRequest, - // Visible messages - Regular - } - - @JvmStatic - public fun getTTL(messageType: MessageType): Int { - val minuteInMs = 60 * 1000 - val hourInMs = 60 * minuteInMs - val dayInMs = 24 * hourInMs - return when (messageType) { - // Unimportant control messages - MessageType.Address, MessageType.Call, MessageType.TypingIndicator, MessageType.Verified -> 20 * 1000 - // Somewhat important control messages - MessageType.DeviceLink -> 1 * hourInMs - // Important control messages - MessageType.ClosedGroupUpdate, MessageType.Ephemeral, MessageType.SessionRequest, MessageType.Receipt, - MessageType.Sync, MessageType.DeviceUnlinkingRequest -> 2 * dayInMs - 1 * hourInMs - // Visible messages - MessageType.Regular -> 2 * dayInMs - } - } -}