Skip to content

Commit

Permalink
write a test for creating a v3 client
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Jan 23, 2024
1 parent 750eb84 commit 15db4e8
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ClientTest {
val v1Copy = PrivateKeyBundleV1Builder.fromEncodedData(encodedData)
val client = Client().buildFrom(v1Copy)
assertEquals(
wallet.address,
wallet.getAddress(),
client.address,
)
}
Expand Down Expand Up @@ -78,12 +78,20 @@ class ClientTest {
)
}

@Test
fun testCreatesAV3Client() {
val fakeWallet = PrivateKeyBuilder()
val client = Client().create(account = fakeWallet)
val v3Client = client.libXMTPClient
assertEquals(client.address, v3Client?.accountAddress())
}

@Test
fun testCanMessage() {
val fixtures = fixtures()
val notOnNetwork = PrivateKeyBuilder()
val canMessage = fixtures.aliceClient.canMessage(fixtures.bobClient.address)
val cannotMessage = fixtures.aliceClient.canMessage(notOnNetwork.address)
val cannotMessage = fixtures.aliceClient.canMessage(notOnNetwork.getAddress())
assert(canMessage)
assert(!cannotMessage)
}
Expand All @@ -97,8 +105,8 @@ class ClientTest {
val aliceClient = Client().create(aliceWallet, opts)
aliceClient.ensureUserContactPublished()

val canMessage = Client.canMessage(aliceWallet.address, opts)
val cannotMessage = Client.canMessage(notOnNetwork.address, opts)
val canMessage = Client.canMessage(aliceWallet.getAddress(), opts)
val cannotMessage = Client.canMessage(notOnNetwork.getAddress(), opts)

assert(canMessage)
assert(!cannotMessage)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,39 +169,39 @@ class ConversationTest {
// Overwrite contact as legacy so we can get v1
fixtures.publishLegacyContact(client = bobClient)
fixtures.publishLegacyContact(client = aliceClient)
val bobConversation = bobClient.conversations.newConversation(aliceWallet.address)
val aliceConversation = aliceClient.conversations.newConversation(bobWallet.address)
val bobConversation = bobClient.conversations.newConversation(aliceWallet.getAddress())
val aliceConversation = aliceClient.conversations.newConversation(bobWallet.getAddress())

bobConversation.send(content = "hey alice")
bobConversation.send(content = "hey alice again")
val messages = aliceConversation.messages()
assertEquals(2, messages.size)
assertEquals("hey alice", messages[1].body)
assertEquals(bobWallet.address, messages[1].senderAddress)
assertEquals(bobWallet.getAddress(), messages[1].senderAddress)
}

@Test
fun testCanLoadV2Messages() {
val bobConversation = bobClient.conversations.newConversation(
aliceWallet.address,
aliceWallet.getAddress(),
InvitationV1ContextBuilder.buildFromConversation("hi"),
)

val aliceConversation = aliceClient.conversations.newConversation(
bobWallet.address,
bobWallet.getAddress(),
InvitationV1ContextBuilder.buildFromConversation("hi"),
)
bobConversation.send(content = "hey alice")
val messages = aliceConversation.messages()
assertEquals(1, messages.size)
assertEquals("hey alice", messages[0].body)
assertEquals(bobWallet.address, messages[0].senderAddress)
assertEquals(bobWallet.getAddress(), messages[0].senderAddress)
}

@Test
fun testVerifiesV2MessageSignature() {
val aliceConversation = aliceClient.conversations.newConversation(
bobWallet.address,
bobWallet.getAddress(),
context = InvitationV1ContextBuilder.buildFromConversation(conversationId = "hi"),
)

Expand Down Expand Up @@ -237,7 +237,7 @@ class ConversationTest {
)
aliceClient.publish(envelopes = listOf(tamperedEnvelope))
val bobConversation = bobClient.conversations.newConversation(
aliceWallet.address,
aliceWallet.getAddress(),
InvitationV1ContextBuilder.buildFromConversation("hi"),
)
assertThrows("Invalid signature", XMTPException::class.java) {
Expand All @@ -251,8 +251,8 @@ class ConversationTest {
fun testCanSendGzipCompressedV1Messages() {
fixtures.publishLegacyContact(client = bobClient)
fixtures.publishLegacyContact(client = aliceClient)
val bobConversation = bobClient.conversations.newConversation(aliceWallet.address)
val aliceConversation = aliceClient.conversations.newConversation(bobWallet.address)
val bobConversation = bobClient.conversations.newConversation(aliceWallet.getAddress())
val aliceConversation = aliceClient.conversations.newConversation(bobWallet.getAddress())
bobConversation.send(
text = MutableList(1000) { "A" }.toString(),
sendOptions = SendOptions(compression = EncodedContentCompression.GZIP),
Expand All @@ -266,8 +266,8 @@ class ConversationTest {
fun testCanSendDeflateCompressedV1Messages() {
fixtures.publishLegacyContact(client = bobClient)
fixtures.publishLegacyContact(client = aliceClient)
val bobConversation = bobClient.conversations.newConversation(aliceWallet.address)
val aliceConversation = aliceClient.conversations.newConversation(bobWallet.address)
val bobConversation = bobClient.conversations.newConversation(aliceWallet.getAddress())
val aliceConversation = aliceClient.conversations.newConversation(bobWallet.getAddress())
bobConversation.send(
content = MutableList(1000) { "A" }.toString(),
options = SendOptions(compression = EncodedContentCompression.DEFLATE),
Expand All @@ -280,11 +280,11 @@ class ConversationTest {
@Test
fun testCanSendGzipCompressedV2Messages() {
val bobConversation = bobClient.conversations.newConversation(
aliceWallet.address,
aliceWallet.getAddress(),
InvitationV1ContextBuilder.buildFromConversation(conversationId = "hi"),
)
val aliceConversation = aliceClient.conversations.newConversation(
bobWallet.address,
bobWallet.getAddress(),
InvitationV1ContextBuilder.buildFromConversation(conversationId = "hi"),
)
bobConversation.send(
Expand All @@ -294,17 +294,17 @@ class ConversationTest {
val messages = aliceConversation.messages()
assertEquals(1, messages.size)
assertEquals(MutableList(1000) { "A" }.toString(), messages[0].body)
assertEquals(bobWallet.address, messages[0].senderAddress)
assertEquals(bobWallet.getAddress(), messages[0].senderAddress)
}

@Test
fun testCanSendDeflateCompressedV2Messages() {
val bobConversation = bobClient.conversations.newConversation(
aliceWallet.address,
aliceWallet.getAddress(),
InvitationV1ContextBuilder.buildFromConversation(conversationId = "hi"),
)
val aliceConversation = aliceClient.conversations.newConversation(
bobWallet.address,
bobWallet.getAddress(),
InvitationV1ContextBuilder.buildFromConversation(conversationId = "hi"),
)
bobConversation.send(
Expand All @@ -314,7 +314,7 @@ class ConversationTest {
val messages = aliceConversation.messages()
assertEquals(1, messages.size)
assertEquals(MutableList(1000) { "A" }.toString(), messages[0].body)
assertEquals(bobWallet.address, messages[0].senderAddress)
assertEquals(bobWallet.getAddress(), messages[0].senderAddress)
}

@Test
Expand All @@ -324,8 +324,8 @@ class ConversationTest {
fakeContactClient.publishUserContact()
val fakeWallet = PrivateKeyBuilder()
val client = Client().create(account = fakeWallet)
val contact = client.getUserContact(peerAddress = fakeContactWallet.address)!!
assertEquals(contact.walletAddress, fakeContactWallet.address)
val contact = client.getUserContact(peerAddress = fakeContactWallet.getAddress())!!
assertEquals(contact.walletAddress, fakeContactWallet.getAddress())
val created = Date()
val invitationContext = Invitation.InvitationV1.Context.newBuilder().also {
it.conversationId = "https://example.com/1"
Expand All @@ -338,7 +338,7 @@ class ConversationTest {
val senderBundle = client.privateKeyBundleV1?.toV2()
assertEquals(
senderBundle?.identityKey?.publicKey?.recoverWalletSignerPublicKey()?.walletAddress,
fakeWallet.address,
fakeWallet.getAddress(),
)
val invitation = SealedInvitationBuilder.buildFromV1(
sender = client.privateKeyBundleV1!!.toV2(),
Expand All @@ -347,12 +347,12 @@ class ConversationTest {
invitation = invitationv1,
)
val inviteHeader = invitation.v1.header
assertEquals(inviteHeader.sender.walletAddress, fakeWallet.address)
assertEquals(inviteHeader.recipient.walletAddress, fakeContactWallet.address)
assertEquals(inviteHeader.sender.walletAddress, fakeWallet.getAddress())
assertEquals(inviteHeader.recipient.walletAddress, fakeContactWallet.getAddress())
val header = SealedInvitationHeaderV1.parseFrom(invitation.v1.headerBytes)
val conversation =
ConversationV2.create(client = client, invitation = invitationv1, header = header)
assertEquals(fakeContactWallet.address, conversation.peerAddress)
assertEquals(fakeContactWallet.getAddress(), conversation.peerAddress)

conversation.send(content = "hello world")

Expand Down Expand Up @@ -734,8 +734,8 @@ class ConversationTest {
fun testCanSendEncodedContentV1Message() {
fixtures.publishLegacyContact(client = bobClient)
fixtures.publishLegacyContact(client = aliceClient)
val bobConversation = bobClient.conversations.newConversation(aliceWallet.address)
val aliceConversation = aliceClient.conversations.newConversation(bobWallet.address)
val bobConversation = bobClient.conversations.newConversation(aliceWallet.getAddress())
val aliceConversation = aliceClient.conversations.newConversation(bobWallet.getAddress())
val encodedContent = TextCodec().encode(content = "hi")
bobConversation.send(encodedContent = encodedContent)
val messages = aliceConversation.messages()
Expand All @@ -745,7 +745,7 @@ class ConversationTest {

@Test
fun testCanSendEncodedContentV2Message() {
val bobConversation = bobClient.conversations.newConversation(aliceWallet.address)
val bobConversation = bobClient.conversations.newConversation(aliceWallet.getAddress())
val encodedContent = TextCodec().encode(content = "hi")
bobConversation.send(encodedContent = encodedContent)
val messages = bobConversation.messages()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class ConversationsTest {
message = MessageBuilder.buildFromMessageV1(v1 = message).toByteArray()
)
val conversation = client.conversations.fromIntro(envelope = envelope)
assertEquals(conversation.peerAddress, newWallet.address)
assertEquals(conversation.peerAddress, newWallet.getAddress())
assertEquals(conversation.createdAt.time, created.time)
}

Expand Down Expand Up @@ -73,7 +73,7 @@ class ConversationsTest {
message = sealed.toByteArray()
)
val conversation = client.conversations.fromInvite(envelope = envelope)
assertEquals(conversation.peerAddress, newWallet.address)
assertEquals(conversation.peerAddress, newWallet.getAddress())
assertEquals(conversation.createdAt.time, created.time)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class LocalInstrumentedTest {
ClientOptions(api = ClientOptions.Api(env = XMTPEnvironment.LOCAL, isSecure = false))
val client = Client().create(account = aliceWallet, options = clientOptions)
assertEquals(XMTPEnvironment.LOCAL, client.apiClient.environment)
val contact = client.getUserContact(peerAddress = aliceWallet.address)
val contact = client.getUserContact(peerAddress = aliceWallet.getAddress())
assertEquals(
contact?.v2?.keyBundle?.identityKey?.secp256K1Uncompressed,
client.privateKeyBundleV1?.identityKey?.publicKey?.secp256K1Uncompressed
Expand All @@ -122,7 +122,7 @@ class LocalInstrumentedTest {
// Publish alice's contact
Client().create(account = alice, clientOptions)
val convo = bobClient.conversations.newConversation(
alice.address,
alice.getAddress(),
context = InvitationV1ContextBuilder.buildFromConversation("hi")
)
// Say this message is sent in the past
Expand Down Expand Up @@ -248,7 +248,7 @@ class LocalInstrumentedTest {
val bobClient = Client().create(bob, clientOptions)
// Publish alice's contact
Client().create(account = alice, clientOptions)
val convo = ConversationV1(client = bobClient, peerAddress = alice.address, sentAt = Date())
val convo = ConversationV1(client = bobClient, peerAddress = alice.getAddress(), sentAt = Date())
// Say this message is sent in the past
convo.send(text = "10 seconds ago")
Thread.sleep(10000)
Expand Down Expand Up @@ -362,7 +362,7 @@ class LocalInstrumentedTest {
val aliceClient = Client().create(account = alice, options = clientOptions)
aliceClient.publishUserContact(legacy = true)
bobClient.publishUserContact(legacy = true)
val convo = ConversationV1(client = bobClient, peerAddress = alice.address, sentAt = Date())
val convo = ConversationV1(client = bobClient, peerAddress = alice.getAddress(), sentAt = Date())
convo.streamEphemeral().mapLatest {
assertEquals("hi", it.message.toStringUtf8())
}
Expand All @@ -380,11 +380,11 @@ class LocalInstrumentedTest {
val bobClient = Client().create(bob, clientOptions)
val aliceClient = Client().create(account = alice, options = clientOptions)
val aliceConversation = aliceClient.conversations.newConversation(
bob.address,
bob.getAddress(),
context = InvitationV1ContextBuilder.buildFromConversation("https://example.com/3")
)
val bobConversation = bobClient.conversations.newConversation(
alice.address,
alice.getAddress(),
context = InvitationV1ContextBuilder.buildFromConversation("https://example.com/3")
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,23 @@ class FakeWallet : SigningKey {
}
}

override val address: String
get() = privateKey.walletAddress

override suspend fun sign(data: ByteArray): Signature {
val signature = privateKeyBuilder.sign(data)
return signature
}

override fun sign(text: String): ByteArray {
TODO("Not yet implemented")
}

override suspend fun signLegacy(message: String): Signature {
val signature = privateKeyBuilder.signLegacy(message)
return signature
}

override fun getAddress(): String {
return privateKey.walletAddress
}
}

class FakeStreamHolder {
Expand Down
13 changes: 6 additions & 7 deletions library/src/main/java/org/xmtp/android/library/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.web3j.crypto.Keys
import org.web3j.crypto.Keys.toChecksumAddress
import org.xmtp.android.library.codecs.ContentCodec
import org.xmtp.android.library.codecs.TextCodec
import org.xmtp.android.library.libxmtp.XMTPLogger
import org.xmtp.android.library.messages.ContactBundle
import org.xmtp.android.library.messages.EncryptedPrivateKeyBundle
import org.xmtp.android.library.messages.Envelope
Expand All @@ -34,9 +35,8 @@ import org.xmtp.android.library.messages.walletAddress
import org.xmtp.proto.message.api.v1.MessageApiOuterClass
import org.xmtp.proto.message.api.v1.MessageApiOuterClass.BatchQueryResponse
import org.xmtp.proto.message.api.v1.MessageApiOuterClass.QueryRequest
import uniffi.xmtp_dh.FfiXmtpClient
import uniffi.xmtp_dh.createClient
import uniffi.xmtp_dh.org.xmtp.android.library.libxmtp.XMTPLogger
import uniffi.xmtpv3.FfiXmtpClient
import uniffi.xmtpv3.createClient
import java.nio.charset.StandardCharsets
import java.text.SimpleDateFormat
import java.time.Instant
Expand Down Expand Up @@ -66,7 +66,7 @@ class Client() {
lateinit var apiClient: ApiClient
lateinit var contacts: Contacts
lateinit var conversations: Conversations
lateinit var logger: XMTPLogger
var logger: XMTPLogger = XMTPLogger()
var libXMTPClient: FfiXmtpClient? = null

companion object {
Expand Down Expand Up @@ -149,7 +149,6 @@ class Client() {
this.apiClient = apiClient
this.contacts = Contacts(client = this)
this.conversations = Conversations(client = this, libXMTPConversations = libXmtpClient?.conversations())
this.logger = XMTPLogger()
this.libXMTPClient = libXmtpClient
}

Expand Down Expand Up @@ -192,7 +191,7 @@ class Client() {
db = null,
encryptionKey = null
)
val client = Client(account.address, privateKeyBundleV1, apiClient, libXMTPClient)
val client = Client(account.getAddress(), privateKeyBundleV1, apiClient, libXMTPClient)
client.ensureUserContactPublished()
client
} catch (e: java.lang.Exception) {
Expand Down Expand Up @@ -253,7 +252,7 @@ class Client() {
apiClient: ApiClient,
options: ClientOptions? = null,
): PrivateKeyBundleV1? {
val encryptedBundles = authCheck(apiClient, account.address)
val encryptedBundles = authCheck(apiClient, account.getAddress())
for (encryptedBundle in encryptedBundles) {
try {
val bundle = encryptedBundle.decrypted(account, options?.preEnableIdentityCallback)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import org.xmtp.proto.keystore.api.v1.Keystore.TopicMap.TopicData
import org.xmtp.proto.message.contents.Contact
import org.xmtp.proto.message.contents.Invitation
import org.xmtp.android.library.messages.DecryptedMessage
import uniffi.xmtp_dh.FfiConversations
import uniffi.xmtpv3.FfiConversations
import java.util.Date

data class Conversations(
Expand Down
Loading

0 comments on commit 15db4e8

Please sign in to comment.