1
1
package org.xmtp.android.library
2
2
3
3
import android.util.Log
4
+ import com.google.protobuf.kotlin.toByteString
4
5
import com.google.protobuf.kotlin.toByteStringUtf8
5
6
import io.grpc.StatusException
6
7
import kotlinx.coroutines.CancellationException
@@ -16,7 +17,6 @@ import org.xmtp.android.library.GRPCApiClient.Companion.makeSubscribeRequest
16
17
import org.xmtp.android.library.messages.DecryptedMessage
17
18
import org.xmtp.android.library.messages.Envelope
18
19
import org.xmtp.android.library.messages.EnvelopeBuilder
19
- import org.xmtp.android.library.messages.HmacKeyValue
20
20
import org.xmtp.android.library.messages.InvitationV1
21
21
import org.xmtp.android.library.messages.MessageV1Builder
22
22
import org.xmtp.android.library.messages.Pagination
@@ -34,6 +34,9 @@ import org.xmtp.android.library.messages.senderAddress
34
34
import org.xmtp.android.library.messages.sentAt
35
35
import org.xmtp.android.library.messages.toSignedPublicKeyBundle
36
36
import org.xmtp.android.library.messages.walletAddress
37
+ import org.xmtp.proto.keystore.api.v1.Keystore
38
+ import org.xmtp.proto.keystore.api.v1.Keystore.GetConversationHmacKeysResponse.HmacKeyData
39
+ import org.xmtp.proto.keystore.api.v1.Keystore.GetConversationHmacKeysResponse.HmacKeys
37
40
import org.xmtp.proto.keystore.api.v1.Keystore.TopicMap.TopicData
38
41
import org.xmtp.proto.message.contents.Contact
39
42
import org.xmtp.proto.message.contents.Invitation
@@ -45,6 +48,7 @@ import java.util.Date
45
48
import kotlin.time.Duration.Companion.nanoseconds
46
49
import kotlin.time.DurationUnit
47
50
51
+
48
52
data class Conversations (
49
53
var client : Client ,
50
54
var conversationsByTopic : MutableMap <String , Conversation > = mutableMapOf(),
@@ -292,46 +296,40 @@ data class Conversations(
292
296
}
293
297
294
298
fun getHmacKeys (
295
- topics : List < String > ,
296
- ): Map < String , List < HmacKeyValue >> {
299
+ request : Keystore . GetConversationHmacKeysRequest ? = null ,
300
+ ): Keystore . GetConversationHmacKeysResponse {
297
301
val thirtyDayPeriodsSinceEpoch = (Date ().time / 1000 / 60 / 60 / 24 / 30 ).toInt()
302
+ val hmacKeys = Keystore .GetConversationHmacKeysResponse .newBuilder()
303
+
304
+ var topics = conversationsByTopic
298
305
299
- val requests = topics.map { topic ->
300
- makeQueryRequest(topic = topic)
306
+ if (! request?.topicsList.isNullOrEmpty()) {
307
+ topics = topics.filter {
308
+ request!! .topicsList.contains(it.key)
309
+ }.toMutableMap()
301
310
}
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
311
330
- }
331
- }
312
+ topics.forEach {
313
+ val conversation = it.value
314
+
315
+ (thirtyDayPeriodsSinceEpoch - 1 .. thirtyDayPeriodsSinceEpoch + 1 ).map { value ->
316
+ val info = " $value -${client.address} "
317
+ val hmacKey =
318
+ Crypto .calculateMac(
319
+ conversation.keyMaterial!! ,
320
+ info.toByteStringUtf8().toByteArray()
321
+ )
322
+
323
+ hmacKeys.putHmacKeys(
324
+ conversation.topic, HmacKeys .newBuilder().addValues(
325
+ HmacKeyData .newBuilder().setHmacKey(hmacKey.toByteString())
326
+ .setThirtyDayPeriodsSinceEpoch(value).build()
327
+ ).build()
328
+ )
332
329
}
333
330
}
334
- return hmacKeys
331
+
332
+ return hmacKeys.build()
335
333
}
336
334
337
335
private fun listIntroductionPeers (pagination : Pagination ? = null): Map <String , Date > {
0 commit comments