From 1448cfb096b7d4f0e2cc241b15c308774237bc26 Mon Sep 17 00:00:00 2001 From: Naomi Plasterer Date: Wed, 21 Aug 2024 15:43:32 -0600 Subject: [PATCH] write a test for it --- Sources/XMTPiOS/Client.swift | 17 ++++++------- Sources/XMTPiOS/Mls/InboxState.swift | 2 +- Tests/XMTPTests/ClientTests.swift | 37 ++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/Sources/XMTPiOS/Client.swift b/Sources/XMTPiOS/Client.swift index 9742c341..ad8a8a91 100644 --- a/Sources/XMTPiOS/Client.swift +++ b/Sources/XMTPiOS/Client.swift @@ -609,14 +609,13 @@ public final class Client { throw ClientError.noV3Client("Error: No V3 client initialized") } - if let signatureRequest = try await client.revokeAllOtherInstallations() { - do { - let signedData = try await signingKey.sign(message: signatureRequest.signatureText()) - try await signatureRequest.addEcdsaSignature(signatureBytes: signedData.rawData) - try await client.registerIdentity(signatureRequest: signatureRequest) - } catch { - throw ClientError.creationError("Failed to sign the message: \(error.localizedDescription)") - } + let signatureRequest = try await client.revokeAllOtherInstallations() + do { + let signedData = try await signingKey.sign(message: signatureRequest.signatureText()) + signatureRequest. + try await signatureRequest.addEcdsaSignature(signatureBytes: signedData.rawData) + } catch { + throw ClientError.creationError("Failed to sign the message: \(error.localizedDescription)") } } @@ -624,6 +623,6 @@ public final class Client { guard let client = v3Client else { throw ClientError.noV3Client("Error: No V3 client initialized") } - return InboxState(ffiInboxState: client.inboxState(refreshFromNetwork: refreshFromNetwork)) + return InboxState(ffiInboxState: try await client.inboxState(refreshFromNetwork: refreshFromNetwork)) } } diff --git a/Sources/XMTPiOS/Mls/InboxState.swift b/Sources/XMTPiOS/Mls/InboxState.swift index cb1a5f7b..f604f15c 100644 --- a/Sources/XMTPiOS/Mls/InboxState.swift +++ b/Sources/XMTPiOS/Mls/InboxState.swift @@ -24,7 +24,7 @@ public struct InboxState { } public var installationIds: [String] { - ffiInboxState.installationIds.map { id in id.toHex} + ffiInboxState.installationIds.map { $0.toHex } } public var recoveryAddress: String { diff --git a/Tests/XMTPTests/ClientTests.swift b/Tests/XMTPTests/ClientTests.swift index 6ecdf6bd..27c0381a 100644 --- a/Tests/XMTPTests/ClientTests.swift +++ b/Tests/XMTPTests/ClientTests.swift @@ -455,4 +455,41 @@ class ClientTests: XCTestCase { XCTAssertEqual(inboxId, alixClient.inboxID) } + + func testRevokesAllOtherInstallations() async throws { + let key = try Crypto.secureRandomBytes(count: 32) + let alix = try PrivateKey.generate() + let options = ClientOptions.init( + api: .init(env: .local, isSecure: false), + enableV3: true, + encryptionKey: key + ) + + let alixClient = try await Client.create( + account: alix, + options: options + ) + try alixClient.dropLocalDatabaseConnection() + try alixClient.deleteLocalDatabase() + + let alixClient2 = try await Client.create( + account: alix, + options: options + ) + try alixClient2.dropLocalDatabaseConnection() + try alixClient2.deleteLocalDatabase() + + let alixClient3 = try await Client.create( + account: alix, + options: options + ) + + let state = try await alixClient3.inboxState(refreshFromNetwork: true) + XCTAssertEqual(state.installationIds.count, 3) + + try await alixClient3.revokeAllOtherInstallations(signingKey: alix) + + let newState = try await alixClient3.inboxState(refreshFromNetwork: true) + XCTAssertEqual(newState.installationIds.count, 1) + } }