diff --git a/Sources/XMTP/Client.swift b/Sources/XMTP/Client.swift index 2ebffcf1..2461a8a0 100644 --- a/Sources/XMTP/Client.swift +++ b/Sources/XMTP/Client.swift @@ -192,6 +192,18 @@ public final class Client: Sendable { public func canMessage(_ peerAddress: String) async throws -> Bool { return try await query(topic: .contact(peerAddress)).envelopes.count > 0 } + + public static func canMessage(_ peerAddress: String, options: ClientOptions? = nil) async throws -> Bool { + let options = options ?? ClientOptions() + + let client = try await XMTPRust.create_client(GRPCApiClient.envToUrl(env: options.api.env), options.api.env != .local) + let apiClient = try GRPCApiClient( + environment: options.api.env, + secure: options.api.isSecure, + rustClient: client + ) + return try await apiClient.query(topic: .contact(peerAddress)).envelopes.count > 0 + } public func importConversation(from conversationData: Data) throws -> Conversation? { let jsonDecoder = JSONDecoder() diff --git a/Tests/XMTPTests/ClientTests.swift b/Tests/XMTPTests/ClientTests.swift index 164043ae..4207564f 100644 --- a/Tests/XMTPTests/ClientTests.swift +++ b/Tests/XMTPTests/ClientTests.swift @@ -30,6 +30,18 @@ class ClientTests: XCTestCase { XCTAssertFalse(cannotMessage) } + func testStaticCanMessage() async throws { + try TestConfig.skip(because: "run manually against dev") + let fixtures = await fixtures() + let notOnNetwork = try PrivateKey.generate() + let opts = ClientOptions(api: .init(env: .local, isSecure: false)) + + let canMessage = try await Client.canMessage(fixtures.bobClient.address, options: opts) + let cannotMessage = try await Client.canMessage(notOnNetwork.address, options: opts) + XCTAssertTrue(canMessage) + XCTAssertFalse(cannotMessage) + } + func testHasPrivateKeyBundleV1() async throws { let fakeWallet = try PrivateKey.generate() let client = try await Client.create(account: fakeWallet, apiClient: FakeApiClient())