Skip to content

Commit

Permalink
add android functions for decrypting group messages
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Apr 3, 2024
1 parent 2d84f08 commit 57a7103
Showing 1 changed file with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import kotlin.coroutines.Continuation
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import com.facebook.common.util.Hex
import org.xmtp.android.library.messages.Topic

class ReactNativeSigner(var module: XMTPModule, override var address: String) : SigningKey {
private val continuations: MutableMap<String, Continuation<Signature>> = mutableMapOf()
Expand Down Expand Up @@ -812,6 +813,29 @@ class XMTPModule : Module() {
}
}

AsyncFunction("processGroupMessage") Coroutine { clientAddress: String, topic: String, encryptedMessage: String ->
withContext(Dispatchers.IO) {
logV("processGroupMessage")
val client = clients[clientAddress] ?: throw XMTPException("No client")
val group = findGroup(clientAddress, getGroupIdFromTopic(topic))

val message = group?.processMessage(Base64.decode(encryptedMessage, NO_WRAP))
?: throw XMTPException("could not decrypt message for $topic")
DecodedMessageWrapper.encodeMap(message.decrypt())
}
}

AsyncFunction("processWelcomeMessage") Coroutine { clientAddress: String, encryptedMessage: String ->
withContext(Dispatchers.IO) {
logV("isGroupAdmin")
val client = clients[clientAddress] ?: throw XMTPException("No client")

val group =
client.conversations.fromWelcome(Base64.decode(encryptedMessage, NO_WRAP))
GroupWrapper.encode(client, group)
}
}

Function("subscribeToConversations") { clientAddress: String ->
logV("subscribeToConversations")
subscribeToConversations(clientAddress = clientAddress)
Expand Down Expand Up @@ -1016,6 +1040,12 @@ class XMTPModule : Module() {
// Helpers
//

private fun getGroupIdFromTopic(topic: String): String {
val pattern = "/xmtp/mls/1/g-(.*?)/proto".toRegex()
val matchResult = pattern.find(topic)
return matchResult?.groups?.get(1)?.value ?: ""
}

private suspend fun findConversation(
clientAddress: String,
topic: String,
Expand Down

0 comments on commit 57a7103

Please sign in to comment.