1
1
package org.xmtp.android.library
2
2
3
3
import android.util.Log
4
+ import com.google.protobuf.kotlin.toByteStringUtf8
4
5
import io.grpc.StatusException
5
6
import kotlinx.coroutines.CancellationException
6
7
import kotlinx.coroutines.channels.awaitClose
@@ -15,6 +16,7 @@ import org.xmtp.android.library.GRPCApiClient.Companion.makeSubscribeRequest
15
16
import org.xmtp.android.library.messages.DecryptedMessage
16
17
import org.xmtp.android.library.messages.Envelope
17
18
import org.xmtp.android.library.messages.EnvelopeBuilder
19
+ import org.xmtp.android.library.messages.HmacKeyValue
18
20
import org.xmtp.android.library.messages.InvitationV1
19
21
import org.xmtp.android.library.messages.MessageV1Builder
20
22
import org.xmtp.android.library.messages.Pagination
@@ -289,6 +291,49 @@ data class Conversations(
289
291
return conversation
290
292
}
291
293
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
+
292
337
private fun listIntroductionPeers (pagination : Pagination ? = null): Map <String , Date > {
293
338
val envelopes =
294
339
runBlocking {
0 commit comments