From 5f3bd610e1e1cb7b344ea1ce13838c481f1464bc Mon Sep 17 00:00:00 2001 From: Bartosz Rozwarski Date: Wed, 25 May 2022 08:44:04 +0200 Subject: [PATCH] remove getAcknowledgedSessions (#228) * remove getAcknowledgedSessions * rename method --- Example/DApp/SceneDelegate.swift | 2 +- Example/ExampleApp/Wallet/WalletViewController.swift | 6 +++--- Sources/WalletConnectAuth/Auth/Auth.swift | 6 +++--- Sources/WalletConnectAuth/Auth/AuthClient.swift | 6 +++--- .../WalletConnectAuth/Engine/Common/SessionEngine.swift | 9 ++++----- Sources/WalletConnectAuth/Storage/SessionStorage.swift | 5 ----- .../ControllerSessionStateMachineTests.swift | 2 +- .../WalletConnectTests/Mocks/WCSessionStorageMock.swift | 7 ------- .../NonControllerSessionStateMachineTests.swift | 8 ++++---- Tests/WalletConnectTests/SessionEngineTests.swift | 1 - 10 files changed, 19 insertions(+), 33 deletions(-) diff --git a/Example/DApp/SceneDelegate.swift b/Example/DApp/SceneDelegate.swift index 73a85be39..b7c0db4b4 100644 --- a/Example/DApp/SceneDelegate.swift +++ b/Example/DApp/SceneDelegate.swift @@ -32,7 +32,7 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate { }.store(in: &publishers) - if let session = Auth.instance.getSettledSessions().first { + if let session = Auth.instance.getSessions().first { showAccountsScreen(session) } else { showSelectChainScreen() diff --git a/Example/ExampleApp/Wallet/WalletViewController.swift b/Example/ExampleApp/Wallet/WalletViewController.swift index 12ae3a833..5aa4af28e 100644 --- a/Example/ExampleApp/Wallet/WalletViewController.swift +++ b/Example/ExampleApp/Wallet/WalletViewController.swift @@ -28,7 +28,7 @@ final class WalletViewController: UIViewController { walletView.tableView.dataSource = self walletView.tableView.delegate = self - let settledSessions = Auth.instance.getSettledSessions() + let settledSessions = Auth.instance.getSessions() sessionItems = getActiveSessionItem(for: settledSessions) setUpAuthSubscribing() } @@ -129,7 +129,7 @@ extension WalletViewController: UITableViewDataSource, UITableViewDelegate { func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { print("did select row \(indexPath)") let itemTopic = sessionItems[indexPath.row].topic - if let session = Auth.instance.getSettledSessions().first{$0.topic == itemTopic} { + if let session = Auth.instance.getSessions().first{$0.topic == itemTopic} { showSessionDetailsViewController(session) } } @@ -225,7 +225,7 @@ extension WalletViewController { } private func reloadActiveSessions() { - let settledSessions = Auth.instance.getSettledSessions() + let settledSessions = Auth.instance.getSessions() let activeSessions = getActiveSessionItem(for: settledSessions) DispatchQueue.main.async { // FIXME: Delegate being called from background thread self.sessionItems = activeSessions diff --git a/Sources/WalletConnectAuth/Auth/Auth.swift b/Sources/WalletConnectAuth/Auth/Auth.swift index 00faa0fc1..00b341250 100644 --- a/Sources/WalletConnectAuth/Auth/Auth.swift +++ b/Sources/WalletConnectAuth/Auth/Auth.swift @@ -218,9 +218,9 @@ extension Auth { try await client.disconnect(topic: topic, reason: reason) } - /// - Returns: All settled sessions that are active - public func getSettledSessions() -> [Session] { - client.getSettledSessions() + /// - Returns: All sessions + public func getSessions() -> [Session] { + client.getSessions() } /// - Returns: All settled pairings that are active diff --git a/Sources/WalletConnectAuth/Auth/AuthClient.swift b/Sources/WalletConnectAuth/Auth/AuthClient.swift index 4ac751d85..532ab978c 100644 --- a/Sources/WalletConnectAuth/Auth/AuthClient.swift +++ b/Sources/WalletConnectAuth/Auth/AuthClient.swift @@ -251,9 +251,9 @@ public final class AuthClient { try await sessionEngine.delete(topic: topic, reason: reason) } - /// - Returns: All settled sessions that are active - public func getSettledSessions() -> [Session] { - sessionEngine.getAcknowledgedSessions() + /// - Returns: All sessions + public func getSessions() -> [Session] { + sessionEngine.getSessions() } /// - Returns: All settled pairings that are active diff --git a/Sources/WalletConnectAuth/Engine/Common/SessionEngine.swift b/Sources/WalletConnectAuth/Engine/Common/SessionEngine.swift index 5cd7d97a9..d49a6880c 100644 --- a/Sources/WalletConnectAuth/Engine/Common/SessionEngine.swift +++ b/Sources/WalletConnectAuth/Engine/Common/SessionEngine.swift @@ -57,13 +57,12 @@ final class SessionEngine { return sessionStore.hasSession(forTopic: topic) } - func getAcknowledgedSessions() -> [Session] { - sessionStore.getAcknowledgedSessions().map{$0.publicRepresentation()} + func getSessions() -> [Session] { + sessionStore.getAll().map{$0.publicRepresentation()} } func settle(topic: String, proposal: SessionProposal, namespaces: [String: SessionNamespace]) throws { let agreementKeys = try! kms.getAgreementSecret(for: topic)! - let selfParticipant = Participant(publicKey: agreementKeys.publicKey.hexRepresentation, metadata: metadata) let expectedExpiryTimeStamp = Date().addingTimeInterval(TimeInterval(WCSession.defaultTimeToLive)) @@ -83,6 +82,7 @@ final class SessionEngine { Task { try? await networkingInteractor.subscribe(topic: topic) } sessionStore.setSession(session) networkingInteractor.request(.wcSessionSettle(settleParams), onTopic: topic) + onSessionSettle?(session.publicRepresentation()) } func delete(topic: String, reason: Reason) async throws { @@ -137,7 +137,7 @@ final class SessionEngine { } func emit(topic: String, event: SessionType.EventParams.Event, chainId: Blockchain) async throws { - guard let session = sessionStore.getSession(forTopic: topic), session.acknowledged else { + guard let session = sessionStore.getSession(forTopic: topic) else { logger.debug("Could not find session for topic \(topic)") return } @@ -302,7 +302,6 @@ final class SessionEngine { guard var session = sessionStore.getSession(forTopic: topic) else {return} session.acknowledge() sessionStore.setSession(session) - onSessionSettle?(session.publicRepresentation()) case .error(let error): logger.error("Error - session rejected, Reason: \(error)") networkingInteractor.unsubscribe(topic: topic) diff --git a/Sources/WalletConnectAuth/Storage/SessionStorage.swift b/Sources/WalletConnectAuth/Storage/SessionStorage.swift index 8f4243794..9d2548dbd 100644 --- a/Sources/WalletConnectAuth/Storage/SessionStorage.swift +++ b/Sources/WalletConnectAuth/Storage/SessionStorage.swift @@ -5,7 +5,6 @@ protocol WCSessionStorage: AnyObject { func getSession(forTopic topic: String) -> WCSession? func getAll() -> [WCSession] func delete(topic: String) - func getAcknowledgedSessions() -> [WCSession] } final class SessionStorage: WCSessionStorage { @@ -40,8 +39,4 @@ final class SessionStorage: WCSessionStorage { func delete(topic: String) { storage.delete(topic: topic) } - - func getAcknowledgedSessions() -> [WCSession] { - getAll().filter {$0.acknowledged} - } } diff --git a/Tests/WalletConnectTests/ControllerSessionStateMachineTests.swift b/Tests/WalletConnectTests/ControllerSessionStateMachineTests.swift index a7756b17b..5893ae3b4 100644 --- a/Tests/WalletConnectTests/ControllerSessionStateMachineTests.swift +++ b/Tests/WalletConnectTests/ControllerSessionStateMachineTests.swift @@ -76,7 +76,7 @@ class ControllerSessionStateMachineTests: XCTestCase { storageMock.setSession(session) let twoDays = 2*Time.day await XCTAssertNoThrowAsync(try await sut.extend(topic: session.topic, by: Int64(twoDays))) - let extendedSession = storageMock.getAcknowledgedSessions().first{$0.topic == session.topic}! + let extendedSession = storageMock.getAll().first{$0.topic == session.topic}! XCTAssertEqual(extendedSession.expiryDate.timeIntervalSinceReferenceDate, TimeTraveler.dateByAdding(days: 2).timeIntervalSinceReferenceDate, accuracy: 1) } diff --git a/Tests/WalletConnectTests/Mocks/WCSessionStorageMock.swift b/Tests/WalletConnectTests/Mocks/WCSessionStorageMock.swift index 880f06db8..dd14e717e 100644 --- a/Tests/WalletConnectTests/Mocks/WCSessionStorageMock.swift +++ b/Tests/WalletConnectTests/Mocks/WCSessionStorageMock.swift @@ -25,12 +25,5 @@ final class WCSessionStorageMock: WCSessionStorage { func delete(topic: String) { sessions[topic] = nil } - - func getAcknowledgedSessions() -> [WCSession] { - getAll().compactMap { - guard $0.acknowledged else { return nil } - return $0 - } - } } diff --git a/Tests/WalletConnectTests/NonControllerSessionStateMachineTests.swift b/Tests/WalletConnectTests/NonControllerSessionStateMachineTests.swift index 8ec5eb420..8a24210b2 100644 --- a/Tests/WalletConnectTests/NonControllerSessionStateMachineTests.swift +++ b/Tests/WalletConnectTests/NonControllerSessionStateMachineTests.swift @@ -72,7 +72,7 @@ class NonControllerSessionStateMachineTests: XCTestCase { let twoDaysFromNowTimestamp = Int64(TimeTraveler.dateByAdding(days: 2).timeIntervalSince1970) networkingInteractor.wcRequestPublisherSubject.send(WCRequestSubscriptionPayload.stubUpdateExpiry(topic: session.topic, expiry: twoDaysFromNowTimestamp)) - let extendedSession = storageMock.getAcknowledgedSessions().first{$0.topic == session.topic}! + let extendedSession = storageMock.getAll().first{$0.topic == session.topic}! print(extendedSession.expiryDate) XCTAssertEqual(extendedSession.expiryDate.timeIntervalSince1970, TimeTraveler.dateByAdding(days: 2).timeIntervalSince1970, accuracy: 1) @@ -88,7 +88,7 @@ class NonControllerSessionStateMachineTests: XCTestCase { networkingInteractor.wcRequestPublisherSubject.send(WCRequestSubscriptionPayload.stubUpdateExpiry(topic: session.topic, expiry: twoDaysFromNowTimestamp)) - let potentiallyExtendedSession = storageMock.getAcknowledgedSessions().first{$0.topic == session.topic}! + let potentiallyExtendedSession = storageMock.getAll().first{$0.topic == session.topic}! XCTAssertEqual(potentiallyExtendedSession.expiryDate.timeIntervalSinceReferenceDate, tomorrow.timeIntervalSinceReferenceDate, accuracy: 1, "expiry date has been extended for peer non controller request ") } @@ -99,7 +99,7 @@ class NonControllerSessionStateMachineTests: XCTestCase { let tenDaysFromNowTimestamp = Int64(TimeTraveler.dateByAdding(days: 10).timeIntervalSince1970) networkingInteractor.wcRequestPublisherSubject.send(WCRequestSubscriptionPayload.stubUpdateExpiry(topic: session.topic, expiry: tenDaysFromNowTimestamp)) - let potentaillyExtendedSession = storageMock.getAcknowledgedSessions().first{$0.topic == session.topic}! + let potentaillyExtendedSession = storageMock.getAll().first{$0.topic == session.topic}! XCTAssertEqual(potentaillyExtendedSession.expiryDate.timeIntervalSinceReferenceDate, tomorrow.timeIntervalSinceReferenceDate, accuracy: 1, "expiry date has been extended despite ttl to high") } @@ -110,7 +110,7 @@ class NonControllerSessionStateMachineTests: XCTestCase { let oneDayFromNowTimestamp = Int64(TimeTraveler.dateByAdding(days: 10).timeIntervalSince1970) networkingInteractor.wcRequestPublisherSubject.send(WCRequestSubscriptionPayload.stubUpdateExpiry(topic: session.topic, expiry: oneDayFromNowTimestamp)) - let potentaillyExtendedSession = storageMock.getAcknowledgedSessions().first{$0.topic == session.topic}! + let potentaillyExtendedSession = storageMock.getAll().first{$0.topic == session.topic}! XCTAssertEqual(potentaillyExtendedSession.expiryDate.timeIntervalSinceReferenceDate, tomorrow.timeIntervalSinceReferenceDate, accuracy: 1, "expiry date has been extended despite ttl to low") } } diff --git a/Tests/WalletConnectTests/SessionEngineTests.swift b/Tests/WalletConnectTests/SessionEngineTests.swift index 1b9b2cda0..72476f51a 100644 --- a/Tests/WalletConnectTests/SessionEngineTests.swift +++ b/Tests/WalletConnectTests/SessionEngineTests.swift @@ -98,7 +98,6 @@ final class SessionEngineTests: XCTestCase { networkingInteractor.onResponse?(response) XCTAssertTrue(storageMock.getSession(forTopic: session.topic)!.acknowledged, "Responder must acknowledged session") - XCTAssertTrue(didCallBackOnSessionApproved, "Responder's engine must call back with session") } func testHandleSessionSettleError() {