diff --git a/library/src/main/java/org/xmtp/android/library/Contacts.kt b/library/src/main/java/org/xmtp/android/library/Contacts.kt index e3ab956b1..b18fb2f23 100644 --- a/library/src/main/java/org/xmtp/android/library/Contacts.kt +++ b/library/src/main/java/org/xmtp/android/library/Contacts.kt @@ -4,6 +4,7 @@ import kotlinx.coroutines.runBlocking import org.xmtp.android.library.messages.ContactBundle import org.xmtp.android.library.messages.ContactBundleBuilder import org.xmtp.android.library.messages.Topic +import org.xmtp.android.library.messages.walletAddress data class Contacts( var client: Client, @@ -27,8 +28,14 @@ data class Contacts( if (response.envelopesList.isNullOrEmpty()) return null for (envelope in response.envelopesList) { - knownBundles[peerAddress] = ContactBundleBuilder.buildFromEnvelope(envelope) + val contactBundle = ContactBundleBuilder.buildFromEnvelope(envelope) + knownBundles[peerAddress] = contactBundle + val address = contactBundle.walletAddress + if (address == peerAddress) { + return contactBundle + } } - return ContactBundleBuilder.buildFromEnvelope(response.envelopesList.first()) + + return null } } diff --git a/library/src/main/java/org/xmtp/android/library/ConversationV2.kt b/library/src/main/java/org/xmtp/android/library/ConversationV2.kt index 47f883721..3541583dc 100644 --- a/library/src/main/java/org/xmtp/android/library/ConversationV2.kt +++ b/library/src/main/java/org/xmtp/android/library/ConversationV2.kt @@ -81,7 +81,7 @@ data class ConversationV2( } fun decode(message: MessageV2): DecodedMessage = - MessageV2Builder.buildDecode(message, keyMaterial = keyMaterial) + MessageV2Builder.buildDecode(message, keyMaterial = keyMaterial, topic = topic) fun send(content: T, options: SendOptions? = null) { val codec = Client.codecRegistry.find(options?.contentType) diff --git a/library/src/main/java/org/xmtp/android/library/KeyUtil.kt b/library/src/main/java/org/xmtp/android/library/KeyUtil.kt index 878f7020c..94b6faba4 100644 --- a/library/src/main/java/org/xmtp/android/library/KeyUtil.kt +++ b/library/src/main/java/org/xmtp/android/library/KeyUtil.kt @@ -1,25 +1,12 @@ package org.xmtp.android.library -import org.bouncycastle.asn1.sec.SECNamedCurves -import org.bouncycastle.crypto.params.ECDomainParameters -import org.bouncycastle.math.ec.FixedPointCombMultiplier -import org.bouncycastle.util.Arrays +import org.web3j.crypto.Sign import org.web3j.crypto.Sign.SignatureData import java.math.BigInteger object KeyUtil { fun getPublicKey(privateKey: ByteArray): ByteArray { - val params = SECNamedCurves.getByName("secp256k1") - val curve = ECDomainParameters(params.curve, params.g, params.n, params.h) - - var privKey = BigInteger(1, privateKey) - - if (privKey.bitLength() > curve.n.bitLength()) { - privKey = privKey.mod(curve.n) - } - - val point = FixedPointCombMultiplier().multiply(curve.getG(), privKey) - return Arrays.copyOfRange(point.getEncoded(false), 1, 65) + return Sign.publicKeyFromPrivate(BigInteger(1, privateKey)).toByteArray() } fun addUncompressedByte(publicKey: ByteArray): ByteArray { diff --git a/library/src/main/java/org/xmtp/android/library/messages/MessageV2.kt b/library/src/main/java/org/xmtp/android/library/messages/MessageV2.kt index ba19c6d0c..15cf2f826 100644 --- a/library/src/main/java/org/xmtp/android/library/messages/MessageV2.kt +++ b/library/src/main/java/org/xmtp/android/library/messages/MessageV2.kt @@ -25,7 +25,7 @@ class MessageV2Builder { }.build() } - fun buildDecode(message: MessageV2, keyMaterial: ByteArray): DecodedMessage { + fun buildDecode(message: MessageV2, keyMaterial: ByteArray, topic: String): DecodedMessage { val decrypted = Crypto.decrypt(keyMaterial, message.ciphertext, message.headerBytes.toByteArray()) val signed = SignedContent.parseFrom(decrypted) @@ -64,10 +64,13 @@ class MessageV2Builder { }.build() if (key.walletAddress != (PublicKeyBuilder.buildFromSignedPublicKey(signed.sender.preKey).walletAddress)) { - throw throw XMTPException("Invalid signature") + throw XMTPException("Invalid signature") } val encodedMessage = EncodedContent.parseFrom(signed.payload) val header = MessageHeaderV2.parseFrom(message.headerBytes) + if (header.topic != topic) { + throw XMTPException("Topic mismatch") + } return DecodedMessage( encodedContent = encodedMessage, senderAddress = signed.sender.walletAddress, diff --git a/library/src/test/java/org/xmtp/android/library/MessageTest.kt b/library/src/test/java/org/xmtp/android/library/MessageTest.kt index 0510ba5a2..e87d84373 100644 --- a/library/src/test/java/org/xmtp/android/library/MessageTest.kt +++ b/library/src/test/java/org/xmtp/android/library/MessageTest.kt @@ -83,7 +83,8 @@ class MessageTest { ) val decoded = MessageV2Builder.buildDecode( message1, - keyMaterial = invitationv1.aes256GcmHkdfSha256.keyMaterial.toByteArray() + keyMaterial = invitationv1.aes256GcmHkdfSha256.keyMaterial.toByteArray(), + topic = invitationv1.topic ) val result: String? = decoded.content() assertEquals(result, "Yo!")