Skip to content

Commit

Permalink
Adding the way for adding should push and hmac
Browse files Browse the repository at this point in the history
  • Loading branch information
giovas17 committed Jan 26, 2024
1 parent 3e94c70 commit 9f12fb1
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 34 deletions.
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ dependencies {
implementation 'org.web3j:crypto:5.0.0'
implementation "net.java.dev.jna:jna:5.13.0@aar"
api 'com.google.protobuf:protobuf-kotlin-lite:3.22.3'
api 'org.xmtp:proto-kotlin:3.31.0'
api 'org.xmtp:proto-kotlin:3.39.0'

testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'app.cash.turbine:turbine:0.12.1'
Expand Down
19 changes: 11 additions & 8 deletions library/src/androidTest/java/org/xmtp/android/library/CodecTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ data class NumberCodec(
authorityId = "example.com",
typeId = "number",
versionMajor = 1,
versionMinor = 1
)
versionMinor = 1,
),
) : ContentCodec<Double> {
override fun encode(content: Double): EncodedContent {
return EncodedContent.newBuilder().also {
it.type = ContentTypeIdBuilder.builderFromAuthorityId(
authorityId = "example.com",
typeId = "number",
versionMajor = 1,
versionMinor = 1
versionMinor = 1,
)
it.content = mapOf(Pair("number", content)).toString().toByteStringUtf8()
}.build()
Expand All @@ -37,10 +37,13 @@ data class NumberCodec(
override fun decode(content: EncodedContent): Double =
content.content.toStringUtf8().filter { it.isDigit() || it == '.' }.toDouble()

override fun shouldPush(): Boolean = false

override fun fallback(content: Double): String? {
return "Error: This app does not support numbers."
}
}

@RunWith(AndroidJUnit4::class)
class CodecTest {

Expand All @@ -53,7 +56,7 @@ class CodecTest {
aliceClient.conversations.newConversation(fixtures.bob.walletAddress)
aliceConversation.send(
content = 3.14,
options = SendOptions(contentType = NumberCodec().contentType)
options = SendOptions(contentType = NumberCodec().contentType),
)
val messages = aliceConversation.messages()
assertEquals(messages.size, 1)
Expand All @@ -75,7 +78,7 @@ class CodecTest {
val source = DecodedComposite(encodedContent = textContent)
aliceConversation.send(
content = source,
options = SendOptions(contentType = CompositeCodec().contentType)
options = SendOptions(contentType = CompositeCodec().contentType),
)
val messages = aliceConversation.messages()
val decoded: DecodedComposite? = messages[0].content()
Expand All @@ -95,12 +98,12 @@ class CodecTest {
val source = DecodedComposite(
parts = listOf(
DecodedComposite(encodedContent = textContent),
DecodedComposite(parts = listOf(DecodedComposite(encodedContent = numberContent)))
)
DecodedComposite(parts = listOf(DecodedComposite(encodedContent = numberContent))),
),
)
aliceConversation.send(
content = source,
options = SendOptions(contentType = CompositeCodec().contentType)
options = SendOptions(contentType = CompositeCodec().contentType),
)
val messages = aliceConversation.messages()
val decoded: DecodedComposite? = messages[0].content()
Expand Down
47 changes: 28 additions & 19 deletions library/src/main/java/org/xmtp/android/library/Crypto.kt
Original file line number Diff line number Diff line change
Expand Up @@ -77,26 +77,35 @@ class Crypto {
null
}
}
}

fun calculateMac(secret: ByteArray, message: ByteArray): ByteArray {
val sha256HMAC: Mac = Mac.getInstance("HmacSHA256")
val secretKey = SecretKeySpec(secret, "HmacSHA256")
sha256HMAC.init(secretKey)
return sha256HMAC.doFinal(message)
}
fun calculateMac(secret: ByteArray, message: ByteArray): ByteArray {
val sha256HMAC: Mac = Mac.getInstance("HmacSHA256")
val secretKey = SecretKeySpec(secret, "HmacSHA256")
sha256HMAC.init(secretKey)
return sha256HMAC.doFinal(message)
}

fun deriveKey(
secret: ByteArray,
salt: ByteArray,
info: ByteArray,
): ByteArray {
val derivationParameters = HKDFParameters(secret, salt, info)
val digest = SHA256Digest()
val hkdfGenerator = HKDFBytesGenerator(digest)
hkdfGenerator.init(derivationParameters)
val hkdf = ByteArray(32)
hkdfGenerator.generateBytes(hkdf, 0, hkdf.size)
return hkdf
fun deriveKey(
secret: ByteArray,
salt: ByteArray,
info: ByteArray,
): ByteArray {
val derivationParameters = HKDFParameters(secret, salt, info)
val digest = SHA256Digest()
val hkdfGenerator = HKDFBytesGenerator(digest)
hkdfGenerator.init(derivationParameters)
val hkdf = ByteArray(32)
hkdfGenerator.generateBytes(hkdf, 0, hkdf.size)
return hkdf
}

fun generateHmacSignature(
secret: ByteArray,
info: ByteArray,
message: ByteArray,
): ByteArray {
val hkdfKey = deriveKey(secret, message, info)
return calculateMac(secret, hkdfKey)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ data class AttachmentCodec(override var contentType: ContentTypeId = ContentType
override fun fallback(content: Attachment): String? {
return "Can’t display \"${content.filename}”. This app doesn’t support attachments."
}

override fun shouldPush(): Boolean = true
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class CompositeCodec : ContentCodec<DecodedComposite> {
return null
}

override fun shouldPush(): Boolean = false

private fun toComposite(decodedComposite: DecodedComposite): Composite {
return Composite.newBuilder().also {
val content = decodedComposite.encodedContent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ interface ContentCodec<T> {
fun encode(content: T): EncodedContent
fun decode(content: EncodedContent): T
fun fallback(content: T): String?
fun shouldPush(): Boolean
}

val id: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ data class ReactionCodec(override var contentType: ContentTypeId = ContentTypeRe
else -> null
}
}

override fun shouldPush(): Boolean = true
}

private class ReactionSerializer : JsonSerializer<Reaction> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ data class ReadReceiptCodec(override var contentType: ContentTypeId = ContentTyp
override fun fallback(content: ReadReceipt): String? {
return null
}

override fun shouldPush(): Boolean = false
}
Original file line number Diff line number Diff line change
Expand Up @@ -171,4 +171,6 @@ data class RemoteAttachmentCodec(override var contentType: ContentTypeId = Conte
override fun fallback(content: RemoteAttachment): String? {
return "Can’t display \"${content.filename}”. This app doesn’t support attachments."
}

override fun shouldPush(): Boolean = true
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ data class ReplyCodec(override var contentType: ContentTypeId = ContentTypeReply
return "Replied with “${content.content}” to an earlier message"
}

override fun shouldPush(): Boolean = true

private fun <Codec : ContentCodec<T>, T> encodeReply(
codec: Codec,
content: Any,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ data class TextCodec(override var contentType: ContentTypeId = ContentTypeText)
override fun fallback(content: String): String? {
return null
}

override fun shouldPush(): Boolean = true
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fun InvitationV1.createRandom(context: Context? = null): InvitationV1 {
return InvitationV1Builder.buildFromTopic(
topic = topic,
context = inviteContext,
aes256GcmHkdfSha256 = aes256GcmHkdfSha256
aes256GcmHkdfSha256 = aes256GcmHkdfSha256,
)
}

Expand All @@ -68,7 +68,7 @@ fun InvitationV1.createDeterministic(
val secret = sender.sharedSecret(
peer = recipient,
myPreKey = sender.preKeysList[0].publicKey,
isRecipient = myAddress < theirAddress
isRecipient = myAddress < theirAddress,
)

val addresses = arrayOf(myAddress, theirAddress)
Expand All @@ -80,12 +80,12 @@ fun InvitationV1.createDeterministic(
addresses.joinToString(separator = ",")
}

val topicId = Crypto().calculateMac(secret = secret, message = msg.toByteArray()).toHex()
val topicId = Crypto.calculateMac(secret = secret, message = msg.toByteArray()).toHex()
val topic = Topic.directMessageV2(topicId)
val keyMaterial = Crypto().deriveKey(
val keyMaterial = Crypto.deriveKey(
secret = secret,
salt = "__XMTP__INVITATION__SALT__XMTP__".toByteArray(),
info = listOf("0").plus(addresses).joinToString(separator = "|").toByteArray()
info = listOf("0").plus(addresses).joinToString(separator = "|").toByteArray(),
)
val aes256GcmHkdfSha256 = Invitation.InvitationV1.Aes256gcmHkdfsha256.newBuilder().apply {
this.keyMaterial = keyMaterial.toByteString()
Expand All @@ -94,7 +94,7 @@ fun InvitationV1.createDeterministic(
return InvitationV1Builder.buildFromTopic(
topic = topic,
context = inviteContext,
aes256GcmHkdfSha256 = aes256GcmHkdfSha256
aes256GcmHkdfSha256 = aes256GcmHkdfSha256,
)
}

Expand Down

0 comments on commit 9f12fb1

Please sign in to comment.