Skip to content

Commit 0e1e0bc

Browse files
author
Ezequiel Leanes
authored
fix: senderHmac parameter in MessageV2Builder (#199)
* fix: senderHmac parameter in MessageV2Builder * add senderHmacGenerated to tamperedMessage in ConversationTest
1 parent 3ce9a4c commit 0e1e0bc

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

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

+12-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package org.xmtp.android.library
33
import androidx.test.ext.junit.runners.AndroidJUnit4
44
import app.cash.turbine.test
55
import com.google.protobuf.kotlin.toByteString
6+
import com.google.protobuf.kotlin.toByteStringUtf8
67
import kotlinx.coroutines.ExperimentalCoroutinesApi
78
import org.junit.Assert
89
import org.junit.Assert.assertEquals
@@ -228,12 +229,21 @@ class ConversationTest {
228229
signedBytes,
229230
additionalData = headerBytes,
230231
)
232+
val thirtyDayPeriodsSinceEpoch =
233+
(Date().time / 1000 / 60 / 60 / 24 / 30).toInt()
234+
val info = "$thirtyDayPeriodsSinceEpoch-${aliceClient.address}"
235+
val infoEncoded = info.toByteStringUtf8().toByteArray()
236+
val senderHmacGenerated =
237+
Crypto.calculateMac(
238+
Crypto.deriveKey(aliceConversation.keyMaterial!!, ByteArray(0), infoEncoded),
239+
headerBytes
240+
)
231241
val tamperedMessage =
232242
MessageV2Builder.buildFromCipherText(
233243
headerBytes = headerBytes,
234244
ciphertext = ciphertext,
235-
senderHmac = null,
236-
shouldPush = true,
245+
senderHmac = senderHmacGenerated,
246+
shouldPush = codec.shouldPush("this is a fake"),
237247
)
238248
val tamperedEnvelope = EnvelopeBuilder.buildFromString(
239249
topic = aliceConversation.topic,

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@ class MessageV2Builder(val senderHmac: ByteArray? = null, val shouldPush: Boolea
2525
fun buildFromCipherText(
2626
headerBytes: ByteArray,
2727
ciphertext: CipherText?,
28-
senderHmac: ByteArray?,
28+
senderHmac: ByteArray,
2929
shouldPush: Boolean,
3030
): MessageV2Builder {
3131
val messageBuilder = MessageV2Builder(senderHmac = senderHmac, shouldPush = shouldPush)
3232
messageBuilder.messageV2 = MessageV2.newBuilder().also {
3333
it.headerBytes = headerBytes.toByteString()
3434
it.ciphertext = ciphertext
3535
it.shouldPush = shouldPush
36+
it.senderHmac = senderHmac.toByteString()
3637
}.build()
3738
return messageBuilder
3839
}

0 commit comments

Comments
 (0)