diff --git a/android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt b/android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt index 1436c7ff..dc080891 100644 --- a/android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt +++ b/android/src/main/java/expo/modules/xmtpreactnativesdk/XMTPModule.kt @@ -296,7 +296,7 @@ class XMTPModule : Module() { withContext(Dispatchers.IO) { logV("connectToApiBackend") val api = apiEnvironments(environment, null) - val xmtpApiClient = Client().connectToApiBackend(api) + val xmtpApiClient = Client.connectToApiBackend(api) apiClient = xmtpApiClient } } @@ -310,7 +310,8 @@ class XMTPModule : Module() { authParams, hasPreAuthenticateToInboxCallback, ) - val randomClient = Client().create(account = privateKey, options = options, apiClient = apiClient) + val randomClient = + Client().create(account = privateKey, options = options, apiClient = apiClient) ContentJson.Companion clients[randomClient.installationId] = randomClient @@ -336,7 +337,8 @@ class XMTPModule : Module() { authParams, hasAuthInboxCallback, ) - val client = Client().create(account = reactSigner, options = options, apiClient = apiClient) + val client = + Client().create(account = reactSigner, options = options, apiClient = apiClient) clients[client.installationId] = client apiClient = client.apiClient ContentJson.Companion @@ -352,7 +354,12 @@ class XMTPModule : Module() { dbEncryptionKey, authParams, ) - val client = Client().build(address = address, options = options, inboxId = inboxId, apiClient = apiClient) + val client = Client().build( + address = address, + options = options, + inboxId = inboxId, + apiClient = apiClient + ) ContentJson.Companion clients[client.installationId] = client apiClient = client.apiClient @@ -464,7 +471,8 @@ class XMTPModule : Module() { Client.canMessage( peerAddresses, context, - apiEnvironments(environment, null) + apiEnvironments(environment, null), + apiClient = apiClient ) } } diff --git a/ios/XMTPModule.swift b/ios/XMTPModule.swift index c0f335cb..05d36c4f 100644 --- a/ios/XMTPModule.swift +++ b/ios/XMTPModule.swift @@ -31,6 +31,7 @@ actor IsolatedManager { public class XMTPModule: Module { var signer: ReactNativeSigner? let clientsManager = ClientsManager() + var apiClient: XmtpApiClient? let subscriptionsManager = IsolatedManager>() private var preAuthenticateToInboxCallbackDeferred: DispatchSemaphore? @@ -219,6 +220,14 @@ public class XMTPModule: Module { try signer?.handleSCW(id: requestID, signature: signature) } + AsyncFunction("connectToApiBackend") { + (environment: String) in + let xmtpApiClient = try await XMTP.Client.connectToApiBackend( + api: createApiClient(env: environment) + ) + apiClient = xmtpApiClient + } + AsyncFunction("createRandom") { ( hasAuthenticateToInboxCallback: Bool?, dbEncryptionKey: [UInt8], @@ -241,8 +250,8 @@ public class XMTPModule: Module { preAuthenticateToInboxCallback: preAuthenticateToInboxCallback ) let client = try await Client.create( - account: privateKey, options: options) - + account: privateKey, options: options, apiClient: apiClient) + apiClient = client.apiClient await clientsManager.updateClient( key: client.installationID, client: client) return try ClientWrapper.encodeToObj(client) @@ -277,9 +286,10 @@ public class XMTPModule: Module { preAuthenticateToInboxCallback: preAuthenticateToInboxCallback ) let client = try await XMTP.Client.create( - account: signer, options: options) + account: signer, options: options, apiClient: apiClient) await self.clientsManager.updateClient( key: client.installationID, client: client) + apiClient = client.apiClient self.signer = nil self.sendEvent("authed", try ClientWrapper.encodeToObj(client)) } @@ -299,9 +309,11 @@ public class XMTPModule: Module { preAuthenticateToInboxCallback: preAuthenticateToInboxCallback ) let client = try await XMTP.Client.build( - address: address, options: options, inboxId: inboxId) + address: address, options: options, inboxId: inboxId, + apiClient: apiClient) await clientsManager.updateClient( key: client.installationID, client: client) + apiClient = client.apiClient return try ClientWrapper.encodeToObj(client) }