From 89d4d0ffb15d97ad81410cf39a87f3d581e26ba5 Mon Sep 17 00:00:00 2001 From: Cameron Voell <1103838+cameronvoell@users.noreply.github.com> Date: Wed, 31 Jul 2024 17:01:13 -0700 Subject: [PATCH] adds blocking callback before auth inbox sign action occurs (#282) Co-authored-by: cameronvoell --- .../org/xmtp/android/library/ClientTest.kt | 27 +++++++++++++++++++ .../java/org/xmtp/android/library/Client.kt | 7 +++++ 2 files changed, 34 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 2e6e82f54..b47ccdd99 100644 --- a/library/src/androidTest/java/org/xmtp/android/library/ClientTest.kt +++ b/library/src/androidTest/java/org/xmtp/android/library/ClientTest.kt @@ -327,6 +327,33 @@ class ClientTest { } } + @Test + fun testPreAuthenticateToInboxCallback() { + val fakeWallet = PrivateKeyBuilder() + val expectation = CompletableFuture() + val key = SecureRandom().generateSeed(32) + val context = InstrumentationRegistry.getInstrumentation().targetContext + + val preAuthenticateToInboxCallback: suspend () -> Unit = { + expectation.complete(Unit) + } + + val opts = ClientOptions( + ClientOptions.Api(XMTPEnvironment.LOCAL, false), + preAuthenticateToInboxCallback = preAuthenticateToInboxCallback, + enableV3 = true, + appContext = context, + dbEncryptionKey = key + ) + + try { + runBlocking { Client().create(account = fakeWallet, options = opts) } + expectation.get(5, TimeUnit.SECONDS) + } catch (e: Exception) { + fail("Error: $e") + } + } + @Test fun testCanDropReconnectDatabase() { val key = SecureRandom().generateSeed(32) 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 275581c3e..664c4cdce 100644 --- a/library/src/main/java/org/xmtp/android/library/Client.kt +++ b/library/src/main/java/org/xmtp/android/library/Client.kt @@ -5,6 +5,7 @@ import android.os.Build import android.util.Log import com.google.crypto.tink.subtle.Base64 import com.google.gson.GsonBuilder +import kotlinx.coroutines.runBlocking import org.web3j.crypto.Keys import org.web3j.crypto.Keys.toChecksumAddress import org.xmtp.android.library.codecs.ContentCodec @@ -61,6 +62,7 @@ data class ClientOptions( val api: Api = Api(), val preCreateIdentityCallback: PreEventCallback? = null, val preEnableIdentityCallback: PreEventCallback? = null, + val preAuthenticateToInboxCallback: PreEventCallback? = null, val appContext: Context? = null, val enableV3: Boolean = false, val dbDirectory: String? = null, @@ -349,6 +351,11 @@ class Client() { } if (v3Client != null) { + options.preAuthenticateToInboxCallback?.let { + runBlocking { + it.invoke() + } + } v3Client.signatureRequest()?.let { signatureRequest -> if (account != null) { account.sign(signatureRequest.signatureText())?.let {