Skip to content

Commit 10b3b5a

Browse files
committed
add method to get the hmac keys
1 parent 846ed28 commit 10b3b5a

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

library/src/main/java/org/xmtp/android/library/Conversations.kt

+45
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.xmtp.android.library
22

33
import android.util.Log
4+
import com.google.protobuf.kotlin.toByteStringUtf8
45
import io.grpc.StatusException
56
import kotlinx.coroutines.CancellationException
67
import kotlinx.coroutines.channels.awaitClose
@@ -15,6 +16,7 @@ import org.xmtp.android.library.GRPCApiClient.Companion.makeSubscribeRequest
1516
import org.xmtp.android.library.messages.DecryptedMessage
1617
import org.xmtp.android.library.messages.Envelope
1718
import org.xmtp.android.library.messages.EnvelopeBuilder
19+
import org.xmtp.android.library.messages.HmacKeyValue
1820
import org.xmtp.android.library.messages.InvitationV1
1921
import org.xmtp.android.library.messages.MessageV1Builder
2022
import org.xmtp.android.library.messages.Pagination
@@ -289,6 +291,49 @@ data class Conversations(
289291
return conversation
290292
}
291293

294+
fun getHmacKeys(
295+
topics: List<String>,
296+
): Map<String, List<HmacKeyValue>> {
297+
val thirtyDayPeriodsSinceEpoch = (Date().time / 1000 / 60 / 60 / 24 / 30).toInt()
298+
299+
val requests = topics.map { topic ->
300+
makeQueryRequest(topic = topic)
301+
}
302+
// The maximum number of requests permitted in a single batch call.
303+
val maxQueryRequestsPerBatch = 50
304+
val hmacKeys = mutableMapOf<String, List<HmacKeyValue>>()
305+
val batches = requests.chunked(maxQueryRequestsPerBatch)
306+
for (batch in batches) {
307+
runBlocking {
308+
client.batchQuery(batch).responsesOrBuilderList.flatMap { res ->
309+
res.envelopesList.mapNotNull { envelope ->
310+
val conversation = conversationsByTopic[envelope.contentTopic]
311+
if (conversation == null || conversation.keyMaterial != null) {
312+
Log.d(TAG, "discarding unknown conversation $envelope")
313+
return@mapNotNull null
314+
}
315+
val values =
316+
(thirtyDayPeriodsSinceEpoch - 1..thirtyDayPeriodsSinceEpoch + 1).map { value ->
317+
val info = "$value-${client.address}"
318+
val hmacKey =
319+
Crypto.calculateMac(
320+
conversation.keyMaterial!!,
321+
info.toByteStringUtf8().toByteArray()
322+
)
323+
HmacKeyValue(
324+
thirtyDayPeriodsSinceEpoch = value,
325+
hmacKey = hmacKey
326+
)
327+
}
328+
hmacKeys[conversation.topic] = values
329+
330+
}
331+
}
332+
}
333+
}
334+
return hmacKeys
335+
}
336+
292337
private fun listIntroductionPeers(pagination: Pagination? = null): Map<String, Date> {
293338
val envelopes =
294339
runBlocking {

library/src/main/java/org/xmtp/android/library/messages/MessageV2.kt

+2
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,5 @@ class MessageV2Builder(val senderHmac: ByteArray? = null, val shouldPush: Boolea
167167
}
168168
}
169169
}
170+
171+
data class HmacKeyValue(val thirtyDayPeriodsSinceEpoch: Int, val hmacKey: ByteArray)

0 commit comments

Comments
 (0)