diff --git a/library/src/androidTest/java/org/xmtp/android/library/ClientTest.kt b/library/src/androidTest/java/org/xmtp/android/library/ClientTest.kt index 53b4cfcb0..5bb831ff0 100644 --- a/library/src/androidTest/java/org/xmtp/android/library/ClientTest.kt +++ b/library/src/androidTest/java/org/xmtp/android/library/ClientTest.kt @@ -1,5 +1,6 @@ package org.xmtp.android.library +import android.util.Log import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.platform.app.InstrumentationRegistry import kotlinx.coroutines.runBlocking @@ -12,6 +13,7 @@ import org.xmtp.android.library.messages.PrivateKeyBuilder import org.xmtp.android.library.messages.walletAddress import uniffi.xmtpv3.GenericException import java.security.SecureRandom +import java.util.Date import java.util.concurrent.CompletableFuture import java.util.concurrent.TimeUnit @@ -474,4 +476,62 @@ class ClientTest { } } } + + @Test + fun testCreatesADevClientPerformance() { + val key = SecureRandom().generateSeed(32) + val context = InstrumentationRegistry.getInstrumentation().targetContext + val fakeWallet = PrivateKeyBuilder() + val start = Date() + val client = runBlocking { + Client().create( + account = fakeWallet, + options = ClientOptions( + ClientOptions.Api(XMTPEnvironment.DEV, true), + appContext = context, + dbEncryptionKey = key + ) + ) + } + val end = Date() + val time1 = end.time - start.time + Log.d("PERF", "Created a client in ${time1 / 1000.0}s") + + val start2 = Date() + val buildClient1 = runBlocking { + Client().build( + fakeWallet.address, + options = ClientOptions( + ClientOptions.Api(XMTPEnvironment.DEV, true), + appContext = context, + dbEncryptionKey = key + ) + ) + } + val end2 = Date() + val time2 = end2.time - start2.time + Log.d("PERF", "Built a client in ${time2 / 1000.0}s") + + val start3 = Date() + val buildClient2 = runBlocking { + Client().build( + fakeWallet.address, + options = ClientOptions( + ClientOptions.Api(XMTPEnvironment.DEV, true), + appContext = context, + dbEncryptionKey = key + ), + inboxId = client.inboxId + ) + } + val end3 = Date() + val time3 = end3.time - start3.time + Log.d("PERF", "Built a client with inboxId in ${time3 / 1000.0}s") + + assert(time2 < time1) + assert(time3 < time1) + assert(time3 < time2) + assertEquals(client.inboxId, buildClient1.inboxId) + assertEquals(client.inboxId, buildClient2.inboxId) + } } diff --git a/library/src/main/java/libxmtp-version.txt b/library/src/main/java/libxmtp-version.txt index 5832e3d52..e2ef7042e 100644 --- a/library/src/main/java/libxmtp-version.txt +++ b/library/src/main/java/libxmtp-version.txt @@ -1,3 +1,3 @@ -Version: 964a227c +Version: f6f9af92 Branch: main -Date: 2024-11-26 17:49:43 +0000 +Date: 2024-12-03 20:02:58 +0000 diff --git a/library/src/main/java/org/xmtp/android/library/Client.kt b/library/src/main/java/org/xmtp/android/library/Client.kt index 129b93f61..e771a6f18 100644 --- a/library/src/main/java/org/xmtp/android/library/Client.kt +++ b/library/src/main/java/org/xmtp/android/library/Client.kt @@ -101,13 +101,14 @@ class Client() { address: String, clientOptions: ClientOptions, signingKey: SigningKey? = null, + inboxId: String? = null ): Client { val accountAddress = address.lowercase() - val inboxId = getOrCreateInboxId(clientOptions.api, accountAddress) + val recoveredInboxId = inboxId ?: getOrCreateInboxId(clientOptions.api, accountAddress) val (ffiClient, dbPath) = createFfiClient( accountAddress, - inboxId, + recoveredInboxId, clientOptions, signingKey, clientOptions.appContext, @@ -139,9 +140,10 @@ class Client() { suspend fun build( address: String, options: ClientOptions, + inboxId: String? = null ): Client { return try { - initializeV3Client(address, options) + initializeV3Client(address, options, inboxId = inboxId) } catch (e: Exception) { throw XMTPException("Error creating V3 client: ${e.message}", e) } @@ -188,7 +190,6 @@ class Client() { ?: throw XMTPException("No signer passed but signer was required.") ffiClient.registerIdentity(signatureRequest) } - return Pair(ffiClient, dbPath) } diff --git a/library/src/main/java/org/xmtp/android/library/messages/PrivateKey.kt b/library/src/main/java/org/xmtp/android/library/messages/PrivateKey.kt index 7f251d33b..0f49068a5 100644 --- a/library/src/main/java/org/xmtp/android/library/messages/PrivateKey.kt +++ b/library/src/main/java/org/xmtp/android/library/messages/PrivateKey.kt @@ -71,12 +71,14 @@ class PrivateKeyBuilder : SigningKey { ) val signatureKey = KeyUtil.getSignatureBytes(signatureData) - return SignatureOuterClass.Signature.newBuilder().also { + val sign = SignatureOuterClass.Signature.newBuilder().also { it.ecdsaCompact = it.ecdsaCompact.toBuilder().also { builder -> builder.bytes = signatureKey.take(64).toByteArray().toByteString() builder.recovery = signatureKey[64].toInt() }.build() }.build() + + return sign } override suspend fun sign(message: String): SignatureOuterClass.Signature { diff --git a/library/src/main/jniLibs/arm64-v8a/libuniffi_xmtpv3.so b/library/src/main/jniLibs/arm64-v8a/libuniffi_xmtpv3.so index de7ca74c0..b8ce33d79 100644 Binary files a/library/src/main/jniLibs/arm64-v8a/libuniffi_xmtpv3.so and b/library/src/main/jniLibs/arm64-v8a/libuniffi_xmtpv3.so differ diff --git a/library/src/main/jniLibs/armeabi-v7a/libuniffi_xmtpv3.so b/library/src/main/jniLibs/armeabi-v7a/libuniffi_xmtpv3.so index 1729c3622..d4aca1761 100644 Binary files a/library/src/main/jniLibs/armeabi-v7a/libuniffi_xmtpv3.so and b/library/src/main/jniLibs/armeabi-v7a/libuniffi_xmtpv3.so differ diff --git a/library/src/main/jniLibs/x86/libuniffi_xmtpv3.so b/library/src/main/jniLibs/x86/libuniffi_xmtpv3.so index 01e8af8a2..5b90945eb 100644 Binary files a/library/src/main/jniLibs/x86/libuniffi_xmtpv3.so and b/library/src/main/jniLibs/x86/libuniffi_xmtpv3.so differ diff --git a/library/src/main/jniLibs/x86_64/libuniffi_xmtpv3.so b/library/src/main/jniLibs/x86_64/libuniffi_xmtpv3.so index 0d081178b..d9529ef58 100644 Binary files a/library/src/main/jniLibs/x86_64/libuniffi_xmtpv3.so and b/library/src/main/jniLibs/x86_64/libuniffi_xmtpv3.so differ