diff --git a/android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt b/android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt index 03d58ff34..5501c7895 100644 --- a/android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt +++ b/android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt @@ -146,6 +146,53 @@ class XMTPModule : Module() { } } + private fun clientOptions( + dbEncryptionKey: List?, + authParams: String, + hasCreateIdentityCallback: Boolean? = null, + hasEnableIdentityCallback: Boolean? = null, + hasPreAuthenticateToInboxCallback: Boolean? = null, + ): ClientOptions { + if (hasCreateIdentityCallback == true) + preCreateIdentityCallbackDeferred = CompletableDeferred() + if (hasEnableIdentityCallback == true) + preEnableIdentityCallbackDeferred = CompletableDeferred() + if (hasPreAuthenticateToInboxCallback == true) + preAuthenticateToInboxCallbackDeferred = CompletableDeferred() + val preCreateIdentityCallback: PreEventCallback? = + preCreateIdentityCallback.takeIf { hasCreateIdentityCallback == true } + val preEnableIdentityCallback: PreEventCallback? = + preEnableIdentityCallback.takeIf { hasEnableIdentityCallback == true } + val preAuthenticateToInboxCallback: PreEventCallback? = + preAuthenticateToInboxCallback.takeIf { hasPreAuthenticateToInboxCallback == true } + + val authOptions = AuthParamsWrapper.authParamsFromJson(authParams) + val context = if (authOptions.enableV3) context else null + val encryptionKeyBytes = + dbEncryptionKey?.foldIndexed(ByteArray(dbEncryptionKey.size)) { i, a, v -> + a.apply { set(i, v.toByte()) } + } + + val historySyncUrl = authOptions.historySyncUrl + ?: when (authOptions.environment) { + "production" -> "https://message-history.production.ephemera.network/" + "local" -> "http://0.0.0.0:5558" + else -> "https://message-history.dev.ephemera.network/" + } + + return ClientOptions( + api = apiEnvironments(authOptions.environment, authOptions.appVersion), + preCreateIdentityCallback = preCreateIdentityCallback, + preEnableIdentityCallback = preEnableIdentityCallback, + preAuthenticateToInboxCallback = preAuthenticateToInboxCallback, + enableV3 = authOptions.enableV3, + appContext = context, + dbEncryptionKey = encryptionKeyBytes, + dbDirectory = authOptions.dbDirectory, + historySyncUrl = historySyncUrl + ) + } + private var clients: MutableMap = mutableMapOf() private var xmtpPush: XMTPPush? = null private var signer: ReactNativeSigner? = null @@ -235,36 +282,12 @@ class XMTPModule : Module() { logV("auth") val reactSigner = ReactNativeSigner(module = this@XMTPModule, address = address) signer = reactSigner - val authOptions = AuthParamsWrapper.authParamsFromJson(authParams) - - if (hasCreateIdentityCallback == true) - preCreateIdentityCallbackDeferred = CompletableDeferred() - if (hasEnableIdentityCallback == true) - preEnableIdentityCallbackDeferred = CompletableDeferred() - if (hasAuthInboxCallback == true) - preAuthenticateToInboxCallbackDeferred = CompletableDeferred() - val preCreateIdentityCallback: PreEventCallback? = - preCreateIdentityCallback.takeIf { hasCreateIdentityCallback == true } - val preEnableIdentityCallback: PreEventCallback? = - preEnableIdentityCallback.takeIf { hasEnableIdentityCallback == true } - val preAuthenticateToInboxCallback: PreEventCallback? = - preAuthenticateToInboxCallback.takeIf { hasAuthInboxCallback == true } - val context = if (authOptions.enableV3) context else null - val encryptionKeyBytes = - dbEncryptionKey?.foldIndexed(ByteArray(dbEncryptionKey.size)) { i, a, v -> - a.apply { set(i, v.toByte()) } - } - - val options = ClientOptions( - api = apiEnvironments(authOptions.environment, authOptions.appVersion), - preCreateIdentityCallback = preCreateIdentityCallback, - preEnableIdentityCallback = preEnableIdentityCallback, - preAuthenticateToInboxCallback = preAuthenticateToInboxCallback, - enableV3 = authOptions.enableV3, - appContext = context, - dbEncryptionKey = encryptionKeyBytes, - dbDirectory = authOptions.dbDirectory, - historySyncUrl = authOptions.historySyncUrl + val options = clientOptions( + dbEncryptionKey, + authParams, + hasCreateIdentityCallback, + hasEnableIdentityCallback, + hasAuthInboxCallback, ) val client = Client().create(account = reactSigner, options = options) clients[client.inboxId] = client @@ -284,38 +307,12 @@ class XMTPModule : Module() { withContext(Dispatchers.IO) { logV("createRandom") val privateKey = PrivateKeyBuilder() - - if (hasCreateIdentityCallback == true) - preCreateIdentityCallbackDeferred = CompletableDeferred() - if (hasEnableIdentityCallback == true) - preEnableIdentityCallbackDeferred = CompletableDeferred() - if (hasPreAuthenticateToInboxCallback == true) - preAuthenticateToInboxCallbackDeferred = CompletableDeferred() - val preCreateIdentityCallback: PreEventCallback? = - preCreateIdentityCallback.takeIf { hasCreateIdentityCallback == true } - val preEnableIdentityCallback: PreEventCallback? = - preEnableIdentityCallback.takeIf { hasEnableIdentityCallback == true } - val preAuthenticateToInboxCallback: PreEventCallback? = - preAuthenticateToInboxCallback.takeIf { hasPreAuthenticateToInboxCallback == true } - - val authOptions = AuthParamsWrapper.authParamsFromJson(authParams) - val context = if (authOptions.enableV3) context else null - val encryptionKeyBytes = - dbEncryptionKey?.foldIndexed(ByteArray(dbEncryptionKey.size)) { i, a, v -> - a.apply { set(i, v.toByte()) } - } - - val options = ClientOptions( - api = apiEnvironments(authOptions.environment, authOptions.appVersion), - preCreateIdentityCallback = preCreateIdentityCallback, - preEnableIdentityCallback = preEnableIdentityCallback, - preAuthenticateToInboxCallback = preAuthenticateToInboxCallback, - enableV3 = authOptions.enableV3, - appContext = context, - dbEncryptionKey = encryptionKeyBytes, - dbDirectory = authOptions.dbDirectory, - historySyncUrl = authOptions.historySyncUrl - + val options = clientOptions( + dbEncryptionKey, + authParams, + hasCreateIdentityCallback, + hasEnableIdentityCallback, + hasPreAuthenticateToInboxCallback, ) val randomClient = Client().create(account = privateKey, options = options) @@ -328,20 +325,10 @@ class XMTPModule : Module() { AsyncFunction("createFromKeyBundle") Coroutine { keyBundle: String, dbEncryptionKey: List?, authParams: String -> withContext(Dispatchers.IO) { logV("createFromKeyBundle") - val authOptions = AuthParamsWrapper.authParamsFromJson(authParams) try { - val context = if (authOptions.enableV3) context else null - val encryptionKeyBytes = - dbEncryptionKey?.foldIndexed(ByteArray(dbEncryptionKey.size)) { i, a, v -> - a.apply { set(i, v.toByte()) } - } - val options = ClientOptions( - api = apiEnvironments(authOptions.environment, authOptions.appVersion), - enableV3 = authOptions.enableV3, - appContext = context, - dbEncryptionKey = encryptionKeyBytes, - dbDirectory = authOptions.dbDirectory, - historySyncUrl = authOptions.historySyncUrl + val options = clientOptions( + dbEncryptionKey, + authParams ) val bundle = PrivateKeyOuterClass.PrivateKeyBundle.parseFrom( @@ -921,7 +908,10 @@ class XMTPModule : Module() { val client = clients[inboxId] ?: throw XMTPException("No client") val createGroupParams = CreateGroupParamsWrapper.createGroupParamsFromJson(groupOptionsJson) - val permissionPolicySet = PermissionPolicySetWrapper.createPermissionPolicySetFromJson(permissionPolicySetJson) + val permissionPolicySet = + PermissionPolicySetWrapper.createPermissionPolicySetFromJson( + permissionPolicySetJson + ) val group = client.conversations.newGroupCustomPermissions( peerAddresses, permissionPolicySet,