From 9c63104bbba0ff9ab111d5e0ee724aaa28499b83 Mon Sep 17 00:00:00 2001 From: Naomi Plasterer Date: Wed, 4 Dec 2024 09:17:28 -0800 Subject: [PATCH 1/3] add a static can message method to the client --- .../java/org/xmtp/android/library/Client.kt | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) 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 e771a6f1..d30f96b6 100644 --- a/library/src/main/java/org/xmtp/android/library/Client.kt +++ b/library/src/main/java/org/xmtp/android/library/Client.kt @@ -76,6 +76,35 @@ class Client() { fun register(codec: ContentCodec<*>) { codecRegistry.register(codec = codec) } + + suspend fun canMessage(accountAddresses: List, appContext: Context, api: ClientOptions.Api): Map { + val accountAddress = "0x0000000000000000000000000000000000000000" + val inboxId = getOrCreateInboxId(api, accountAddress) + val alias = "xmtp-${api.env}-$inboxId" + + val directoryFile = File(appContext.filesDir.absolutePath, "xmtp_db") + directoryFile.mkdir() + val dbPath = directoryFile.absolutePath + "/$alias.db3" + + val ffiClient = createClient( + logger = XMTPLogger(), + host = api.env.getUrl(), + isSecure = api.isSecure, + db = dbPath, + encryptionKey = null, + accountAddress = accountAddress.lowercase(), + inboxId = inboxId, + nonce = 0.toULong(), + legacySignedPrivateKeyProto = null, + historySyncUrl = null + ) + + val result = ffiClient.canMessage(accountAddresses) + ffiClient.releaseDbConnection() + File(dbPath).delete() + + return result + } } constructor( From e580e1ac907cb6dbccefc3016dcc7010f52f3b98 Mon Sep 17 00:00:00 2001 From: Naomi Plasterer Date: Wed, 4 Dec 2024 09:30:34 -0800 Subject: [PATCH 2/3] add static can message --- .../org/xmtp/android/library/ClientTest.kt | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) 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 5bb831ff..6fcfd09e 100644 --- a/library/src/androidTest/java/org/xmtp/android/library/ClientTest.kt +++ b/library/src/androidTest/java/org/xmtp/android/library/ClientTest.kt @@ -72,6 +72,33 @@ class ClientTest { assertEquals(inboxId, client.inboxId) } + @Test + fun testStaticCanMessage() { + val context = InstrumentationRegistry.getInstrumentation().targetContext + val fixtures = fixtures() + val notOnNetwork = PrivateKeyBuilder() + + val canMessageList = runBlocking { + Client.canMessage( + listOf( + fixtures.alix.walletAddress, + notOnNetwork.address, + fixtures.bo.walletAddress + ), context, ClientOptions.Api(XMTPEnvironment.LOCAL, false) + ) + } + + val expectedResults = mapOf( + fixtures.alix.walletAddress.lowercase() to true, + notOnNetwork.address.lowercase() to false, + fixtures.bo.walletAddress.lowercase() to true + ) + + expectedResults.forEach { (address, expected) -> + assertEquals(expected, canMessageList[address.lowercase()]) + } + } + @Test fun testCanDeleteDatabase() { val key = SecureRandom().generateSeed(32) From a9cc862f784b8b85c5423f0300c9d7c5c5f34503 Mon Sep 17 00:00:00 2001 From: Naomi Plasterer Date: Wed, 4 Dec 2024 09:37:27 -0800 Subject: [PATCH 3/3] fix up linter --- .../java/org/xmtp/android/library/ClientTest.kt | 4 +++- .../main/java/org/xmtp/android/library/Client.kt | 16 ++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) 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 6fcfd09e..c30f8cc8 100644 --- a/library/src/androidTest/java/org/xmtp/android/library/ClientTest.kt +++ b/library/src/androidTest/java/org/xmtp/android/library/ClientTest.kt @@ -84,7 +84,9 @@ class ClientTest { fixtures.alix.walletAddress, notOnNetwork.address, fixtures.bo.walletAddress - ), context, ClientOptions.Api(XMTPEnvironment.LOCAL, false) + ), + context, + ClientOptions.Api(XMTPEnvironment.LOCAL, false) ) } 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 d30f96b6..5fba3312 100644 --- a/library/src/main/java/org/xmtp/android/library/Client.kt +++ b/library/src/main/java/org/xmtp/android/library/Client.kt @@ -77,7 +77,11 @@ class Client() { codecRegistry.register(codec = codec) } - suspend fun canMessage(accountAddresses: List, appContext: Context, api: ClientOptions.Api): Map { + suspend fun canMessage( + accountAddresses: List, + appContext: Context, + api: ClientOptions.Api, + ): Map { val accountAddress = "0x0000000000000000000000000000000000000000" val inboxId = getOrCreateInboxId(api, accountAddress) val alias = "xmtp-${api.env}-$inboxId" @@ -130,7 +134,7 @@ class Client() { address: String, clientOptions: ClientOptions, signingKey: SigningKey? = null, - inboxId: String? = null + inboxId: String? = null, ): Client { val accountAddress = address.lowercase() val recoveredInboxId = inboxId ?: getOrCreateInboxId(clientOptions.api, accountAddress) @@ -169,7 +173,7 @@ class Client() { suspend fun build( address: String, options: ClientOptions, - inboxId: String? = null + inboxId: String? = null, ): Client { return try { initializeV3Client(address, options, inboxId = inboxId) @@ -274,7 +278,11 @@ class Client() { } } - fun verifySignatureWithInstallationId(message: String, signature: ByteArray, installationId: String): Boolean { + fun verifySignatureWithInstallationId( + message: String, + signature: ByteArray, + installationId: String, + ): Boolean { return try { ffiClient.verifySignedWithPublicKey(message, signature, installationId.hexToByteArray()) true