Skip to content

Commit c2f4f5b

Browse files
committed
add the get keys code for hmacs
1 parent b52a9e6 commit c2f4f5b

File tree

4 files changed

+36
-39
lines changed

4 files changed

+36
-39
lines changed

library/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ dependencies {
8686
implementation 'org.web3j:crypto:5.0.0'
8787
implementation "net.java.dev.jna:jna:5.13.0@aar"
8888
api 'com.google.protobuf:protobuf-kotlin-lite:3.22.3'
89-
api 'org.xmtp:proto-kotlin:3.40.1'
89+
api 'org.xmtp:proto-kotlin:3.42.0'
9090

9191
testImplementation 'junit:junit:4.13.2'
9292
androidTestImplementation 'app.cash.turbine:turbine:0.12.1'

library/src/androidTest/java/org/xmtp/android/library/CodecTest.kt

+1
Original file line numberDiff line numberDiff line change
@@ -139,5 +139,6 @@ class CodecTest {
139139

140140
assertEquals(false, message.shouldPush)
141141
assertEquals(true, message.senderHmac?.isNotEmpty())
142+
val keys = aliceClient.conversations.getHmacKeys()
142143
}
143144
}

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

+33-35
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.toByteString
45
import com.google.protobuf.kotlin.toByteStringUtf8
56
import io.grpc.StatusException
67
import kotlinx.coroutines.CancellationException
@@ -16,7 +17,6 @@ import org.xmtp.android.library.GRPCApiClient.Companion.makeSubscribeRequest
1617
import org.xmtp.android.library.messages.DecryptedMessage
1718
import org.xmtp.android.library.messages.Envelope
1819
import org.xmtp.android.library.messages.EnvelopeBuilder
19-
import org.xmtp.android.library.messages.HmacKeyValue
2020
import org.xmtp.android.library.messages.InvitationV1
2121
import org.xmtp.android.library.messages.MessageV1Builder
2222
import org.xmtp.android.library.messages.Pagination
@@ -34,6 +34,9 @@ import org.xmtp.android.library.messages.senderAddress
3434
import org.xmtp.android.library.messages.sentAt
3535
import org.xmtp.android.library.messages.toSignedPublicKeyBundle
3636
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
3740
import org.xmtp.proto.keystore.api.v1.Keystore.TopicMap.TopicData
3841
import org.xmtp.proto.message.contents.Contact
3942
import org.xmtp.proto.message.contents.Invitation
@@ -45,6 +48,7 @@ import java.util.Date
4548
import kotlin.time.Duration.Companion.nanoseconds
4649
import kotlin.time.DurationUnit
4750

51+
4852
data class Conversations(
4953
var client: Client,
5054
var conversationsByTopic: MutableMap<String, Conversation> = mutableMapOf(),
@@ -292,46 +296,40 @@ data class Conversations(
292296
}
293297

294298
fun getHmacKeys(
295-
topics: List<String>,
296-
): Map<String, List<HmacKeyValue>> {
299+
request: Keystore.GetConversationHmacKeysRequest? = null,
300+
): Keystore.GetConversationHmacKeysResponse {
297301
val thirtyDayPeriodsSinceEpoch = (Date().time / 1000 / 60 / 60 / 24 / 30).toInt()
302+
val hmacKeys = Keystore.GetConversationHmacKeysResponse.newBuilder()
303+
304+
var topics = conversationsByTopic
298305

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()
301310
}
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
329311

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+
)
332329
}
333330
}
334-
return hmacKeys
331+
332+
return hmacKeys.build()
335333
}
336334

337335
private fun listIntroductionPeers(pagination: Pagination? = null): Map<String, Date> {

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,4 @@ class MessageV2Builder(val senderHmac: ByteArray? = null, val shouldPush: Boolea
166166
)
167167
}
168168
}
169-
}
170-
171-
data class HmacKeyValue(val thirtyDayPeriodsSinceEpoch: Int, val hmacKey: ByteArray)
169+
}

0 commit comments

Comments
 (0)