From 5a7e3ee133b37879fc08a9708b907ebc000feebb Mon Sep 17 00:00:00 2001 From: Alexander Lisovyk Date: Tue, 28 Mar 2023 21:30:34 +0400 Subject: [PATCH 1/9] Regenerate socket url on error disconnection --- Sources/WalletConnectRelay/Dispatching.swift | 89 ++++++++++++++------ Sources/WalletConnectRelay/RelayClient.swift | 22 ++--- 2 files changed, 71 insertions(+), 40 deletions(-) diff --git a/Sources/WalletConnectRelay/Dispatching.swift b/Sources/WalletConnectRelay/Dispatching.swift index 73fa3c5b2..3488c56bf 100644 --- a/Sources/WalletConnectRelay/Dispatching.swift +++ b/Sources/WalletConnectRelay/Dispatching.swift @@ -13,10 +13,16 @@ protocol Dispatching { final class Dispatcher: NSObject, Dispatching { var onMessage: ((String) -> Void)? - var socket: WebSocketConnecting - var socketConnectionHandler: SocketConnectionHandler - + var socket: WebSocketConnecting? + var socketConnectionHandler: SocketConnectionHandler? + + private let relayHost: String + private let projectId: String + private let relayUrlFactory: RelayUrlFactory + private let socketFactory: WebSocketFactory + private let socketConnectionType: SocketConnectionType private let logger: ConsoleLogging + private let defaultTimeout: Int = 5 private let socketConnectionStatusPublisherSubject = CurrentValueSubject(.disconnected) @@ -27,29 +33,41 @@ final class Dispatcher: NSObject, Dispatching { private let concurrentQueue = DispatchQueue(label: "com.walletconnect.sdk.dispatcher", attributes: .concurrent) - init(socket: WebSocketConnecting, - socketConnectionHandler: SocketConnectionHandler, - logger: ConsoleLogging) { - self.socket = socket + init( + relayHost: String, + projectId: String, + clientIdStorage: ClientIdStorage, + socketFactory: WebSocketFactory, + socketConnectionType: SocketConnectionType, + logger: ConsoleLogging + ) { + self.relayHost = relayHost + self.projectId = projectId + let socketAuthenticator = SocketAuthenticator( + clientIdStorage: clientIdStorage, + relayHost: relayHost + ) + self.relayUrlFactory = RelayUrlFactory(socketAuthenticator: socketAuthenticator) + self.socketFactory = socketFactory + self.socketConnectionType = socketConnectionType self.logger = logger - self.socketConnectionHandler = socketConnectionHandler + super.init() - setUpWebSocketSession() - setUpSocketConnectionObserving() + setUpSocketConnection() } func send(_ string: String, completion: @escaping (Error?) -> Void) { - if socket.isConnected { - self.socket.write(string: string) { - completion(nil) - } - } else { + guard let socket, socket.isConnected else { completion(NetworkError.webSocketNotConnected) + return + } + socket.write(string: string) { + completion(nil) } } func protectedSend(_ string: String, completion: @escaping (Error?) -> Void) { - guard !socket.isConnected else { + guard let socket, !socket.isConnected else { return send(string, completion: completion) } @@ -84,28 +102,51 @@ final class Dispatcher: NSObject, Dispatching { } func connect() throws { - try socketConnectionHandler.handleConnect() + try socketConnectionHandler?.handleConnect() } func disconnect(closeCode: URLSessionWebSocketTask.CloseCode) throws { - try socketConnectionHandler.handleDisconnect(closeCode: closeCode) + try socketConnectionHandler?.handleDisconnect(closeCode: closeCode) + } + + private func setUpSocketConnection() { + setUpSocket() + setUpWebSocketSession() + setUpSocketConnectionObserving() + } + + private func setUpSocket() { + let socket = socketFactory.create(with: relayUrlFactory.create( + host: relayHost, + projectId: projectId + )) + socket.request.addValue(EnvironmentInfo.userAgent, forHTTPHeaderField: "User-Agent") + self.socket = socket + + switch socketConnectionType { + case .automatic: socketConnectionHandler = AutomaticSocketConnectionHandler(socket: socket) + case .manual: socketConnectionHandler = ManualSocketConnectionHandler(socket: socket) + } } private func setUpWebSocketSession() { - socket.onText = { [unowned self] in + socket?.onText = { [unowned self] in self.onMessage?($0) } } private func setUpSocketConnectionObserving() { - socket.onConnect = { [unowned self] in + socket?.onConnect = { [unowned self] in self.socketConnectionStatusPublisherSubject.send(.connected) } - socket.onDisconnect = { [unowned self] _ in + socket?.onDisconnect = { [unowned self] error in self.socketConnectionStatusPublisherSubject.send(.disconnected) - - Task(priority: .high) { - await self.socketConnectionHandler.handleDisconnection() + if error != nil { + setUpSocketConnection() + } else { + Task(priority: .high) { + await self.socketConnectionHandler?.handleDisconnection() + } } } } diff --git a/Sources/WalletConnectRelay/RelayClient.swift b/Sources/WalletConnectRelay/RelayClient.swift index b762450e4..6c53600c5 100644 --- a/Sources/WalletConnectRelay/RelayClient.swift +++ b/Sources/WalletConnectRelay/RelayClient.swift @@ -80,24 +80,14 @@ public final class RelayClient { logger: ConsoleLogging = ConsoleLogger(loggingLevel: .off) ) { let clientIdStorage = ClientIdStorage(keychain: keychainStorage) - let socketAuthenticator = SocketAuthenticator( + let dispatcher = Dispatcher( + relayHost: relayHost, + projectId: projectId, clientIdStorage: clientIdStorage, - relayHost: relayHost + socketFactory: socketFactory, + socketConnectionType: socketConnectionType, + logger: logger ) - let relayUrlFactory = RelayUrlFactory(socketAuthenticator: socketAuthenticator) - let socket = socketFactory.create(with: relayUrlFactory.create( - host: relayHost, - projectId: projectId - )) - socket.request.addValue(EnvironmentInfo.userAgent, forHTTPHeaderField: "User-Agent") - let socketConnectionHandler: SocketConnectionHandler - switch socketConnectionType { - case .automatic: - socketConnectionHandler = AutomaticSocketConnectionHandler(socket: socket) - case .manual: - socketConnectionHandler = ManualSocketConnectionHandler(socket: socket) - } - let dispatcher = Dispatcher(socket: socket, socketConnectionHandler: socketConnectionHandler, logger: logger) self.init(dispatcher: dispatcher, logger: logger, keyValueStorage: keyValueStorage, clientIdStorage: clientIdStorage) } From ba775edd3522341a2bd37af982d903c9f560cbd3 Mon Sep 17 00:00:00 2001 From: Alexander Lisovyk Date: Tue, 28 Mar 2023 22:05:37 +0400 Subject: [PATCH 2/9] Update tests --- Tests/RelayerTests/DispatcherTests.swift | 34 +++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/Tests/RelayerTests/DispatcherTests.swift b/Tests/RelayerTests/DispatcherTests.swift index 9d32b380d..55cdc6b57 100644 --- a/Tests/RelayerTests/DispatcherTests.swift +++ b/Tests/RelayerTests/DispatcherTests.swift @@ -5,6 +5,15 @@ import Combine import TestingUtils import Combine +private class KeychainStorageMock: KeychainStorageProtocol { + func add(_ item: T, forKey key: String) throws where T : WalletConnectKMS.GenericPasswordConvertible {} + func read(key: String) throws -> T where T : WalletConnectKMS.GenericPasswordConvertible { + return try T(rawRepresentation: Data()) + } + func delete(key: String) throws {} + func deleteAll() throws {} +} + class WebSocketMock: WebSocketConnecting { var request: URLRequest = URLRequest(url: URL(string: "wss://relay.walletconnect.com")!) @@ -29,15 +38,38 @@ class WebSocketMock: WebSocketConnecting { } } +class WebSocketFactoryMock: WebSocketFactory { + private let webSocket: WebSocketMock + + init(webSocket: WebSocketMock) { + self.webSocket = webSocket + } + + func create(with url: URL) -> WebSocketConnecting { + return webSocket + } +} + final class DispatcherTests: XCTestCase { var publishers = Set() var sut: Dispatcher! var webSocket: WebSocketMock! + var webSocketFactory: WebSocketFactoryMock! var networkMonitor: NetworkMonitoringMock! + override func setUp() { webSocket = WebSocketMock() + webSocketFactory = WebSocketFactoryMock(webSocket: webSocket) networkMonitor = NetworkMonitoringMock() - sut = Dispatcher(socket: webSocket, socketConnectionHandler: ManualSocketConnectionHandler(socket: webSocket), logger: ConsoleLoggerMock()) + let keychainStorageMock = KeychainStorageMock() + sut = Dispatcher( + relayHost: "", + projectId: "", + clientIdStorage: ClientIdStorage(keychain: keychainStorageMock), + socketFactory: webSocketFactory, + socketConnectionType: .manual, + logger: ConsoleLoggerMock() + ) } func testSendWhileConnected() { From 88ae363e79c7d4113bb3605ca34eba78b473d946 Mon Sep 17 00:00:00 2001 From: Alexander Lisovyk Date: Tue, 28 Mar 2023 22:24:39 +0400 Subject: [PATCH 3/9] Update relay end to end tests --- .../RelayClientEndToEndTests.swift | 32 +++++++++++++++++-- Tests/RelayerTests/DispatcherTests.swift | 11 +++---- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/Example/RelayIntegrationTests/RelayClientEndToEndTests.swift b/Example/RelayIntegrationTests/RelayClientEndToEndTests.swift index 4f4917dea..e8ca2bb31 100644 --- a/Example/RelayIntegrationTests/RelayClientEndToEndTests.swift +++ b/Example/RelayIntegrationTests/RelayClientEndToEndTests.swift @@ -5,6 +5,27 @@ import WalletConnectUtils import Starscream @testable import WalletConnectRelay +private class RelayKeychainStorageMock: KeychainStorageProtocol { + func add(_ item: T, forKey key: String) throws where T : WalletConnectKMS.GenericPasswordConvertible {} + func read(key: String) throws -> T where T : WalletConnectKMS.GenericPasswordConvertible { + return try T(rawRepresentation: Data()) + } + func delete(key: String) throws {} + func deleteAll() throws {} +} + +class WebSocketFactoryMock: WebSocketFactory { + private let webSocket: WebSocket + + init(webSocket: WebSocket) { + self.webSocket = webSocket + } + + func create(with url: URL) -> WebSocketConnecting { + return webSocket + } +} + final class RelayClientEndToEndTests: XCTestCase { private var publishers = Set() @@ -17,9 +38,16 @@ final class RelayClientEndToEndTests: XCTestCase { ) let urlFactory = RelayUrlFactory(socketAuthenticator: socketAuthenticator) let socket = WebSocket(url: urlFactory.create(host: InputConfig.relayHost, projectId: InputConfig.projectId)) - + let webSocketFactory = WebSocketFactoryMock(webSocket: socket) let logger = ConsoleLogger() - let dispatcher = Dispatcher(socket: socket, socketConnectionHandler: ManualSocketConnectionHandler(socket: socket), logger: logger) + let dispatcher = Dispatcher( + relayHost: "relay.walletconnect.com", + projectId: "1012db890cf3cfb0c1cdc929add657ba", + clientIdStorage: ClientIdStorage(keychain: RelayKeychainStorageMock()), + socketFactory: webSocketFactory, + socketConnectionType: .manual, + logger: logger + ) return RelayClient(dispatcher: dispatcher, logger: logger, keyValueStorage: RuntimeKeyValueStorage(), clientIdStorage: clientIdStorage) } diff --git a/Tests/RelayerTests/DispatcherTests.swift b/Tests/RelayerTests/DispatcherTests.swift index 55cdc6b57..029be1424 100644 --- a/Tests/RelayerTests/DispatcherTests.swift +++ b/Tests/RelayerTests/DispatcherTests.swift @@ -5,7 +5,7 @@ import Combine import TestingUtils import Combine -private class KeychainStorageMock: KeychainStorageProtocol { +private class DispatcherKeychainStorageMock: KeychainStorageProtocol { func add(_ item: T, forKey key: String) throws where T : WalletConnectKMS.GenericPasswordConvertible {} func read(key: String) throws -> T where T : WalletConnectKMS.GenericPasswordConvertible { return try T(rawRepresentation: Data()) @@ -54,17 +54,16 @@ final class DispatcherTests: XCTestCase { var publishers = Set() var sut: Dispatcher! var webSocket: WebSocketMock! - var webSocketFactory: WebSocketFactoryMock! var networkMonitor: NetworkMonitoringMock! override func setUp() { webSocket = WebSocketMock() - webSocketFactory = WebSocketFactoryMock(webSocket: webSocket) + let webSocketFactory = WebSocketFactoryMock(webSocket: webSocket) networkMonitor = NetworkMonitoringMock() - let keychainStorageMock = KeychainStorageMock() + let keychainStorageMock = DispatcherKeychainStorageMock() sut = Dispatcher( - relayHost: "", - projectId: "", + relayHost: "relay.walletconnect.com", + projectId: "1012db890cf3cfb0c1cdc929add657ba", clientIdStorage: ClientIdStorage(keychain: keychainStorageMock), socketFactory: webSocketFactory, socketConnectionType: .manual, From 4a482949bcce97d9fe039140ade6d73600426b70 Mon Sep 17 00:00:00 2001 From: Alexander Lisovyk Date: Tue, 28 Mar 2023 22:25:55 +0400 Subject: [PATCH 4/9] Update relay and project id tests --- Example/RelayIntegrationTests/RelayClientEndToEndTests.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Example/RelayIntegrationTests/RelayClientEndToEndTests.swift b/Example/RelayIntegrationTests/RelayClientEndToEndTests.swift index e8ca2bb31..6278535f7 100644 --- a/Example/RelayIntegrationTests/RelayClientEndToEndTests.swift +++ b/Example/RelayIntegrationTests/RelayClientEndToEndTests.swift @@ -41,8 +41,8 @@ final class RelayClientEndToEndTests: XCTestCase { let webSocketFactory = WebSocketFactoryMock(webSocket: socket) let logger = ConsoleLogger() let dispatcher = Dispatcher( - relayHost: "relay.walletconnect.com", - projectId: "1012db890cf3cfb0c1cdc929add657ba", + relayHost: InputConfig.relayHost, + projectId: InputConfig.projectId, clientIdStorage: ClientIdStorage(keychain: RelayKeychainStorageMock()), socketFactory: webSocketFactory, socketConnectionType: .manual, From 446813be56a90bef5f255cc999aaa3edd93283e0 Mon Sep 17 00:00:00 2001 From: Alexander Lisovyk Date: Fri, 31 Mar 2023 23:37:41 +0200 Subject: [PATCH 5/9] Update socket url on error reconnection --- Sources/WalletConnectRelay/Dispatching.swift | 46 ++++++++++---------- 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/Sources/WalletConnectRelay/Dispatching.swift b/Sources/WalletConnectRelay/Dispatching.swift index 3488c56bf..9bb8d78c8 100644 --- a/Sources/WalletConnectRelay/Dispatching.swift +++ b/Sources/WalletConnectRelay/Dispatching.swift @@ -19,7 +19,6 @@ final class Dispatcher: NSObject, Dispatching { private let relayHost: String private let projectId: String private let relayUrlFactory: RelayUrlFactory - private let socketFactory: WebSocketFactory private let socketConnectionType: SocketConnectionType private let logger: ConsoleLogging @@ -48,12 +47,24 @@ final class Dispatcher: NSObject, Dispatching { relayHost: relayHost ) self.relayUrlFactory = RelayUrlFactory(socketAuthenticator: socketAuthenticator) - self.socketFactory = socketFactory self.socketConnectionType = socketConnectionType self.logger = logger - + + let socket = socketFactory.create(with: relayUrlFactory.create( + host: relayHost, + projectId: projectId + )) + socket.request.addValue(EnvironmentInfo.userAgent, forHTTPHeaderField: "User-Agent") + self.socket = socket + + switch socketConnectionType { + case .automatic: socketConnectionHandler = AutomaticSocketConnectionHandler(socket: socket) + case .manual: socketConnectionHandler = ManualSocketConnectionHandler(socket: socket) + } + super.init() - setUpSocketConnection() + setUpWebSocketSession() + setUpSocketConnectionObserving() } func send(_ string: String, completion: @escaping (Error?) -> Void) { @@ -110,24 +121,9 @@ final class Dispatcher: NSObject, Dispatching { } private func setUpSocketConnection() { - setUpSocket() setUpWebSocketSession() setUpSocketConnectionObserving() } - - private func setUpSocket() { - let socket = socketFactory.create(with: relayUrlFactory.create( - host: relayHost, - projectId: projectId - )) - socket.request.addValue(EnvironmentInfo.userAgent, forHTTPHeaderField: "User-Agent") - self.socket = socket - - switch socketConnectionType { - case .automatic: socketConnectionHandler = AutomaticSocketConnectionHandler(socket: socket) - case .manual: socketConnectionHandler = ManualSocketConnectionHandler(socket: socket) - } - } private func setUpWebSocketSession() { socket?.onText = { [unowned self] in @@ -142,11 +138,13 @@ final class Dispatcher: NSObject, Dispatching { socket?.onDisconnect = { [unowned self] error in self.socketConnectionStatusPublisherSubject.send(.disconnected) if error != nil { - setUpSocketConnection() - } else { - Task(priority: .high) { - await self.socketConnectionHandler?.handleDisconnection() - } + self.socket?.request.url = relayUrlFactory.create( + host: relayHost, + projectId: projectId + ) + } + Task(priority: .high) { + await self.socketConnectionHandler?.handleDisconnection() } } } From 9f49fada6ff8d28399151246361d7b4c7a655772 Mon Sep 17 00:00:00 2001 From: Alexander Lisovyk Date: Mon, 3 Apr 2023 10:45:35 +0200 Subject: [PATCH 6/9] Add parameters to RelayUrlFactory --- .../RelayClientEndToEndTests.swift | 8 +++-- Sources/WalletConnectRelay/Dispatching.swift | 31 +++---------------- Sources/WalletConnectRelay/RelayClient.swift | 11 +++++-- .../WalletConnectRelay/RelayURLFactory.swift | 14 +++++++-- 4 files changed, 30 insertions(+), 34 deletions(-) diff --git a/Example/RelayIntegrationTests/RelayClientEndToEndTests.swift b/Example/RelayIntegrationTests/RelayClientEndToEndTests.swift index 6278535f7..175dc13f3 100644 --- a/Example/RelayIntegrationTests/RelayClientEndToEndTests.swift +++ b/Example/RelayIntegrationTests/RelayClientEndToEndTests.swift @@ -36,8 +36,12 @@ final class RelayClientEndToEndTests: XCTestCase { clientIdStorage: clientIdStorage, relayHost: InputConfig.relayHost ) - let urlFactory = RelayUrlFactory(socketAuthenticator: socketAuthenticator) - let socket = WebSocket(url: urlFactory.create(host: InputConfig.relayHost, projectId: InputConfig.projectId)) + let urlFactory = RelayUrlFactory( + relayHost: InputConfig.relayHost, + projectId: InputConfig.projectId, + socketAuthenticator: socketAuthenticator + ) + let socket = WebSocket(url: urlFactory.create()) let webSocketFactory = WebSocketFactoryMock(webSocket: socket) let logger = ConsoleLogger() let dispatcher = Dispatcher( diff --git a/Sources/WalletConnectRelay/Dispatching.swift b/Sources/WalletConnectRelay/Dispatching.swift index 9bb8d78c8..f4a93a6e8 100644 --- a/Sources/WalletConnectRelay/Dispatching.swift +++ b/Sources/WalletConnectRelay/Dispatching.swift @@ -16,10 +16,7 @@ final class Dispatcher: NSObject, Dispatching { var socket: WebSocketConnecting? var socketConnectionHandler: SocketConnectionHandler? - private let relayHost: String - private let projectId: String private let relayUrlFactory: RelayUrlFactory - private let socketConnectionType: SocketConnectionType private let logger: ConsoleLogging private let defaultTimeout: Int = 5 @@ -33,27 +30,15 @@ final class Dispatcher: NSObject, Dispatching { private let concurrentQueue = DispatchQueue(label: "com.walletconnect.sdk.dispatcher", attributes: .concurrent) init( - relayHost: String, - projectId: String, - clientIdStorage: ClientIdStorage, socketFactory: WebSocketFactory, + relayUrlFactory: RelayUrlFactory, socketConnectionType: SocketConnectionType, logger: ConsoleLogging ) { - self.relayHost = relayHost - self.projectId = projectId - let socketAuthenticator = SocketAuthenticator( - clientIdStorage: clientIdStorage, - relayHost: relayHost - ) - self.relayUrlFactory = RelayUrlFactory(socketAuthenticator: socketAuthenticator) - self.socketConnectionType = socketConnectionType + self.relayUrlFactory = relayUrlFactory self.logger = logger - let socket = socketFactory.create(with: relayUrlFactory.create( - host: relayHost, - projectId: projectId - )) + let socket = socketFactory.create(with: relayUrlFactory.create()) socket.request.addValue(EnvironmentInfo.userAgent, forHTTPHeaderField: "User-Agent") self.socket = socket @@ -119,11 +104,6 @@ final class Dispatcher: NSObject, Dispatching { func disconnect(closeCode: URLSessionWebSocketTask.CloseCode) throws { try socketConnectionHandler?.handleDisconnect(closeCode: closeCode) } - - private func setUpSocketConnection() { - setUpWebSocketSession() - setUpSocketConnectionObserving() - } private func setUpWebSocketSession() { socket?.onText = { [unowned self] in @@ -138,10 +118,7 @@ final class Dispatcher: NSObject, Dispatching { socket?.onDisconnect = { [unowned self] error in self.socketConnectionStatusPublisherSubject.send(.disconnected) if error != nil { - self.socket?.request.url = relayUrlFactory.create( - host: relayHost, - projectId: projectId - ) + self.socket?.request.url = relayUrlFactory.create() } Task(priority: .high) { await self.socketConnectionHandler?.handleDisconnection() diff --git a/Sources/WalletConnectRelay/RelayClient.swift b/Sources/WalletConnectRelay/RelayClient.swift index 6c53600c5..59846a852 100644 --- a/Sources/WalletConnectRelay/RelayClient.swift +++ b/Sources/WalletConnectRelay/RelayClient.swift @@ -80,11 +80,18 @@ public final class RelayClient { logger: ConsoleLogging = ConsoleLogger(loggingLevel: .off) ) { let clientIdStorage = ClientIdStorage(keychain: keychainStorage) - let dispatcher = Dispatcher( + let socketAuthenticator = SocketAuthenticator( + clientIdStorage: clientIdStorage, + relayHost: relayHost + ) + let relayUrlFactory = RelayUrlFactory( relayHost: relayHost, projectId: projectId, - clientIdStorage: clientIdStorage, + socketAuthenticator: socketAuthenticator + ) + let dispatcher = Dispatcher( socketFactory: socketFactory, + relayUrlFactory: relayUrlFactory, socketConnectionType: socketConnectionType, logger: logger ) diff --git a/Sources/WalletConnectRelay/RelayURLFactory.swift b/Sources/WalletConnectRelay/RelayURLFactory.swift index 4ee15f387..d3b9d155c 100644 --- a/Sources/WalletConnectRelay/RelayURLFactory.swift +++ b/Sources/WalletConnectRelay/RelayURLFactory.swift @@ -1,16 +1,24 @@ import Foundation struct RelayUrlFactory { + private let relayHost: String + private let projectId: String private let socketAuthenticator: SocketAuthenticating - init(socketAuthenticator: SocketAuthenticating) { + init( + relayHost: String, + projectId: String, + socketAuthenticator: SocketAuthenticating + ) { + self.relayHost = relayHost + self.projectId = projectId self.socketAuthenticator = socketAuthenticator } - func create(host: String, projectId: String) -> URL { + func create() -> URL { var components = URLComponents() components.scheme = "wss" - components.host = host + components.host = relayHost components.queryItems = [ URLQueryItem(name: "projectId", value: projectId) ] From e72dd20c9ea716ee4bc2b00d77faa97be3b47d15 Mon Sep 17 00:00:00 2001 From: Alexander Lisovyk Date: Mon, 3 Apr 2023 10:50:44 +0200 Subject: [PATCH 7/9] Update tests --- Example/RelayIntegrationTests/RelayClientEndToEndTests.swift | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Example/RelayIntegrationTests/RelayClientEndToEndTests.swift b/Example/RelayIntegrationTests/RelayClientEndToEndTests.swift index 175dc13f3..4c2331656 100644 --- a/Example/RelayIntegrationTests/RelayClientEndToEndTests.swift +++ b/Example/RelayIntegrationTests/RelayClientEndToEndTests.swift @@ -45,10 +45,8 @@ final class RelayClientEndToEndTests: XCTestCase { let webSocketFactory = WebSocketFactoryMock(webSocket: socket) let logger = ConsoleLogger() let dispatcher = Dispatcher( - relayHost: InputConfig.relayHost, - projectId: InputConfig.projectId, - clientIdStorage: ClientIdStorage(keychain: RelayKeychainStorageMock()), socketFactory: webSocketFactory, + relayUrlFactory: urlFactory, socketConnectionType: .manual, logger: logger ) From d2a109e5f2da2965e068311aefc60afd044fb60c Mon Sep 17 00:00:00 2001 From: Alexander Lisovyk Date: Mon, 3 Apr 2023 11:04:10 +0200 Subject: [PATCH 8/9] Update dispatcher tests --- Tests/RelayerTests/DispatcherTests.swift | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Tests/RelayerTests/DispatcherTests.swift b/Tests/RelayerTests/DispatcherTests.swift index 029be1424..170bbcb37 100644 --- a/Tests/RelayerTests/DispatcherTests.swift +++ b/Tests/RelayerTests/DispatcherTests.swift @@ -61,11 +61,19 @@ final class DispatcherTests: XCTestCase { let webSocketFactory = WebSocketFactoryMock(webSocket: webSocket) networkMonitor = NetworkMonitoringMock() let keychainStorageMock = DispatcherKeychainStorageMock() - sut = Dispatcher( + let clientIdStorage = ClientIdStorage(keychain: keychainStorageMock) + let socketAuthenticator = SocketAuthenticator( + clientIdStorage: clientIdStorage, + relayHost: "relay.walletconnect.com" + ) + let relayUrlFactory = RelayUrlFactory( relayHost: "relay.walletconnect.com", projectId: "1012db890cf3cfb0c1cdc929add657ba", - clientIdStorage: ClientIdStorage(keychain: keychainStorageMock), + socketAuthenticator: socketAuthenticator + ) + sut = Dispatcher( socketFactory: webSocketFactory, + relayUrlFactory: relayUrlFactory, socketConnectionType: .manual, logger: ConsoleLoggerMock() ) From c437975609862f96fad2ac10941d7585ab5b5bf6 Mon Sep 17 00:00:00 2001 From: Alexander Lisovyk Date: Thu, 6 Apr 2023 11:59:38 +0200 Subject: [PATCH 9/9] Remove optional --- Sources/WalletConnectRelay/Dispatching.swift | 22 ++++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/Sources/WalletConnectRelay/Dispatching.swift b/Sources/WalletConnectRelay/Dispatching.swift index f4a93a6e8..71d08f576 100644 --- a/Sources/WalletConnectRelay/Dispatching.swift +++ b/Sources/WalletConnectRelay/Dispatching.swift @@ -13,8 +13,8 @@ protocol Dispatching { final class Dispatcher: NSObject, Dispatching { var onMessage: ((String) -> Void)? - var socket: WebSocketConnecting? - var socketConnectionHandler: SocketConnectionHandler? + var socket: WebSocketConnecting + var socketConnectionHandler: SocketConnectionHandler private let relayUrlFactory: RelayUrlFactory private let logger: ConsoleLogging @@ -53,7 +53,7 @@ final class Dispatcher: NSObject, Dispatching { } func send(_ string: String, completion: @escaping (Error?) -> Void) { - guard let socket, socket.isConnected else { + guard socket.isConnected else { completion(NetworkError.webSocketNotConnected) return } @@ -63,7 +63,7 @@ final class Dispatcher: NSObject, Dispatching { } func protectedSend(_ string: String, completion: @escaping (Error?) -> Void) { - guard let socket, !socket.isConnected else { + guard !socket.isConnected else { return send(string, completion: completion) } @@ -98,30 +98,30 @@ final class Dispatcher: NSObject, Dispatching { } func connect() throws { - try socketConnectionHandler?.handleConnect() + try socketConnectionHandler.handleConnect() } func disconnect(closeCode: URLSessionWebSocketTask.CloseCode) throws { - try socketConnectionHandler?.handleDisconnect(closeCode: closeCode) + try socketConnectionHandler.handleDisconnect(closeCode: closeCode) } private func setUpWebSocketSession() { - socket?.onText = { [unowned self] in + socket.onText = { [unowned self] in self.onMessage?($0) } } private func setUpSocketConnectionObserving() { - socket?.onConnect = { [unowned self] in + socket.onConnect = { [unowned self] in self.socketConnectionStatusPublisherSubject.send(.connected) } - socket?.onDisconnect = { [unowned self] error in + socket.onDisconnect = { [unowned self] error in self.socketConnectionStatusPublisherSubject.send(.disconnected) if error != nil { - self.socket?.request.url = relayUrlFactory.create() + self.socket.request.url = relayUrlFactory.create() } Task(priority: .high) { - await self.socketConnectionHandler?.handleDisconnection() + await self.socketConnectionHandler.handleDisconnection() } } }