From 43acd37485fb30e6587f9ff51d16c40549d9df24 Mon Sep 17 00:00:00 2001 From: llbartekll Date: Thu, 29 Feb 2024 10:18:21 +0100 Subject: [PATCH 1/6] savepoint --- Sample/Example.xcodeproj/project.pbxproj | 14 ++++---------- .../xcshareddata/swiftpm/Package.resolved | 8 ++++---- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/Sample/Example.xcodeproj/project.pbxproj b/Sample/Example.xcodeproj/project.pbxproj index f94fcf1..110bb0f 100644 --- a/Sample/Example.xcodeproj/project.pbxproj +++ b/Sample/Example.xcodeproj/project.pbxproj @@ -347,14 +347,12 @@ ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; CODE_SIGN_ENTITLEMENTS = Example/Example.entitlements; CODE_SIGN_IDENTITY = "Apple Development"; - "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Distribution"; - CODE_SIGN_STYLE = Manual; + CODE_SIGN_STYLE = Automatic; CURRENT_PROJECT_VERSION = 16; DEAD_CODE_STRIPPING = YES; DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; DEVELOPMENT_ASSET_PATHS = "\"Example/Preview Content\""; - DEVELOPMENT_TEAM = ""; - "DEVELOPMENT_TEAM[sdk=iphoneos*]" = W5R8AG9K22; + DEVELOPMENT_TEAM = W5R8AG9K22; ENABLE_HARDENED_RUNTIME = YES; ENABLE_PREVIEWS = YES; GENERATE_INFOPLIST_FILE = YES; @@ -381,7 +379,6 @@ PRODUCT_NAME = "$(TARGET_NAME)"; PROJECT_ID = "$(PROJECT_ID)"; PROVISIONING_PROFILE_SPECIFIER = ""; - "PROVISIONING_PROFILE_SPECIFIER[sdk=iphoneos*]" = "match AppStore com.walletconnect.web3modal.sample"; SDKROOT = auto; SUPPORTED_PLATFORMS = "iphoneos iphonesimulator"; SUPPORTS_MACCATALYST = NO; @@ -399,14 +396,12 @@ ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; CODE_SIGN_ENTITLEMENTS = Example/Example.entitlements; CODE_SIGN_IDENTITY = "Apple Development"; - "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Distribution"; - CODE_SIGN_STYLE = Manual; + CODE_SIGN_STYLE = Automatic; CURRENT_PROJECT_VERSION = 16; DEAD_CODE_STRIPPING = YES; DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; DEVELOPMENT_ASSET_PATHS = "\"Example/Preview Content\""; - DEVELOPMENT_TEAM = ""; - "DEVELOPMENT_TEAM[sdk=iphoneos*]" = W5R8AG9K22; + DEVELOPMENT_TEAM = W5R8AG9K22; ENABLE_HARDENED_RUNTIME = YES; ENABLE_PREVIEWS = YES; GENERATE_INFOPLIST_FILE = YES; @@ -433,7 +428,6 @@ PRODUCT_NAME = "$(TARGET_NAME)"; PROJECT_ID = "$(PROJECT_ID)"; PROVISIONING_PROFILE_SPECIFIER = ""; - "PROVISIONING_PROFILE_SPECIFIER[sdk=iphoneos*]" = "match AppStore com.walletconnect.web3modal.sample"; SDKROOT = auto; SUPPORTED_PLATFORMS = "iphoneos iphonesimulator"; SUPPORTS_MACCATALYST = NO; diff --git a/Web3Modal.xcworkspace/xcshareddata/swiftpm/Package.resolved b/Web3Modal.xcworkspace/xcshareddata/swiftpm/Package.resolved index 46f1a68..d307813 100644 --- a/Web3Modal.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/Web3Modal.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -41,8 +41,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/pointfreeco/swift-snapshot-testing", "state" : { - "revision" : "59b663f68e69f27a87b45de48cb63264b8194605", - "version" : "1.15.1" + "revision" : "e7b77228b34057041374ebef00c0fd7739d71a2b", + "version" : "1.15.3" } }, { @@ -50,8 +50,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/apple/swift-syntax.git", "state" : { - "revision" : "6ad4ea24b01559dde0773e3d091f1b9e36175036", - "version" : "509.0.2" + "revision" : "64889f0c732f210a935a0ad7cda38f77f876262d", + "version" : "509.1.1" } }, { From 64e05af5e12f77198cec0ce54c80e4a7b03af817 Mon Sep 17 00:00:00 2001 From: llbartekll Date: Thu, 29 Feb 2024 15:48:31 +0100 Subject: [PATCH 2/6] fix socket connection view --- Package.swift | 2 +- Sample/Example/ContentView.swift | 17 ++++------ Sample/Example/ExampleApp.swift | 32 +++++++++++++++---- .../xcshareddata/swiftpm/Package.resolved | 4 +-- 4 files changed, 35 insertions(+), 20 deletions(-) diff --git a/Package.swift b/Package.swift index cff54ee..a7ead74 100644 --- a/Package.swift +++ b/Package.swift @@ -21,7 +21,7 @@ let package = Package( dependencies: [ .package( url: "https://github.com/WalletConnect/WalletConnectSwiftV2", - from: "1.10.0" + from: "1.13.0" ), .package( url: "https://github.com/WalletConnect/QRCode", diff --git a/Sample/Example/ContentView.swift b/Sample/Example/ContentView.swift index 21480ee..8b5590d 100644 --- a/Sample/Example/ContentView.swift +++ b/Sample/Example/ContentView.swift @@ -3,8 +3,9 @@ import Web3Modal struct ContentView: View { @State var showUIComponents: Bool = false - @State var socketConnected: Bool = false - + @EnvironmentObject var socketConnectionManager: SocketConnectionManager + + var body: some View { NavigationView { VStack { @@ -32,19 +33,15 @@ struct ContentView: View { .overlay( HStack { Circle() - .fill(socketConnected ? Color.Success100 : Color.Error100) + .fill(socketConnectionManager.socketConnected ? Color.Success100 : Color.Error100) .frame(width: 10, height: 10) - - Text("Socket \(socketConnected ? "Connected" : "Disconnected")") + + Text("Socket \(socketConnectionManager.socketConnected ? "Connected" : "Disconnected")") .font(.system(size: 12, weight: .semibold)) - .foregroundColor(socketConnected ? Color.Success100 : Color.Error100) + .foregroundColor(socketConnectionManager.socketConnected ? Color.Success100 : Color.Error100) }, alignment: .top ) - .onReceive(Web3Modal.instance.socketConnectionStatusPublisher, perform: { status in - socketConnected = status == .connected - print("🧦 \(status)") - }) } } diff --git a/Sample/Example/ExampleApp.swift b/Sample/Example/ExampleApp.swift index 9b74624..11af8d1 100644 --- a/Sample/Example/ExampleApp.swift +++ b/Sample/Example/ExampleApp.swift @@ -9,13 +9,20 @@ import Web3Modal import Atlantis #endif + +class SocketConnectionManager: ObservableObject { + @Published var socketConnected: Bool = false +} + @main -struct ExampleApp: App { +class ExampleApp: App { private var disposeBag = Set() + private var socketConnectionManager = SocketConnectionManager() + @State var alertMessage: String = "" - init() { + required init() { #if DEBUG Atlantis.start() #endif @@ -65,19 +72,30 @@ struct ExampleApp: App { print(error) } + setup() + + } + + func setup() { + Web3Modal.instance.socketConnectionStatusPublisher.receive(on: DispatchQueue.main).sink { [unowned self] status in + print("Socket connection status: \(status)") + self.socketConnectionManager.socketConnected = (status == .connected) + + }.store(in: &disposeBag) } var body: some Scene { - WindowGroup { + WindowGroup { [unowned self] in ContentView() + .environmentObject(socketConnectionManager) .onOpenURL { url in Web3Modal.instance.handleDeeplink(url) } .alert( "Response", isPresented: .init( - get: { !alertMessage.isEmpty }, - set: { _ in alertMessage = "" } + get: { !self.alertMessage.isEmpty }, + set: { _ in self.alertMessage = "" } ) ) { Button("Dismiss", role: .cancel) {} @@ -87,9 +105,9 @@ struct ExampleApp: App { .onReceive(Web3Modal.instance.sessionResponsePublisher, perform: { response in switch response.result { case let .response(value): - alertMessage = "Session response: \(value.stringRepresentation)" + self.alertMessage = "Session response: \(value.stringRepresentation)" case let .error(error): - alertMessage = "Session error: \(error)" + self.alertMessage = "Session error: \(error)" } }) } diff --git a/Web3Modal.xcworkspace/xcshareddata/swiftpm/Package.resolved b/Web3Modal.xcworkspace/xcshareddata/swiftpm/Package.resolved index d307813..e510d7a 100644 --- a/Web3Modal.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/Web3Modal.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -77,8 +77,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/WalletConnect/WalletConnectSwiftV2", "state" : { - "revision" : "031c61bfbd4c4eed4a5cb6790152ce736a4f1fba", - "version" : "1.10.0" + "revision" : "4f93c19f02b5b9ae0a984794cae542b9ca1b7471", + "version" : "1.13.0" } } ], From 89aea170be17b72fe4ce76c4fe00a311c9f556cf Mon Sep 17 00:00:00 2001 From: llbartekll Date: Mon, 4 Mar 2024 09:35:49 +0100 Subject: [PATCH 3/6] regenerate pairing uri on connect request --- Sample/Example/ExampleApp.swift | 1 + Sources/Web3Modal/Core/SignInteractor.swift | 3 +-- Sources/Web3Modal/Core/Web3Modal.swift | 1 + Sources/Web3Modal/Core/Web3ModalClient.swift | 9 ++++++++- .../Web3Modal/Screens/ConnectWallet/AllWalletsView.swift | 8 ++++++-- .../Screens/ConnectWallet/ConnectWalletView.swift | 8 ++++++-- 6 files changed, 23 insertions(+), 7 deletions(-) diff --git a/Sample/Example/ExampleApp.swift b/Sample/Example/ExampleApp.swift index 11af8d1..d86e38d 100644 --- a/Sample/Example/ExampleApp.swift +++ b/Sample/Example/ExampleApp.swift @@ -82,6 +82,7 @@ class ExampleApp: App { self.socketConnectionManager.socketConnected = (status == .connected) }.store(in: &disposeBag) + Web3Modal.instance.logger.setLogging(level: .debug) } var body: some Scene { diff --git a/Sources/Web3Modal/Core/SignInteractor.swift b/Sources/Web3Modal/Core/SignInteractor.swift index 71621de..cb23a26 100644 --- a/Sources/Web3Modal/Core/SignInteractor.swift +++ b/Sources/Web3Modal/Core/SignInteractor.swift @@ -16,7 +16,7 @@ class SignInteractor: ObservableObject { self.store = store } - func createPairingAndConnect() async throws { + func createPairingAndConnect() async throws { let uri = try await Web3Modal.instance.connect(topic: nil) DispatchQueue.main.async { @@ -41,6 +41,5 @@ class SignInteractor: ObservableObject { Web3Modal.config.onError(error) } try await Web3Modal.instance.cleanup() - try await createPairingAndConnect() } } diff --git a/Sources/Web3Modal/Core/Web3Modal.swift b/Sources/Web3Modal/Core/Web3Modal.swift index a078b42..83b22b4 100644 --- a/Sources/Web3Modal/Core/Web3Modal.swift +++ b/Sources/Web3Modal/Core/Web3Modal.swift @@ -25,6 +25,7 @@ public class Web3Modal { fatalError("Error - you must call Web3Modal.configure(_:) before accessing the shared instance.") } let client = Web3ModalClient( + logger: ConsoleLogger(prefix: "📜", loggingLevel: .off), signClient: Sign.instance, pairingClient: Pair.instance as! (PairingClientProtocol & PairingInteracting & PairingRegisterer), store: .shared diff --git a/Sources/Web3Modal/Core/Web3ModalClient.swift b/Sources/Web3Modal/Core/Web3ModalClient.swift index 2d73561..20d0818 100644 --- a/Sources/Web3Modal/Core/Web3ModalClient.swift +++ b/Sources/Web3Modal/Core/Web3ModalClient.swift @@ -77,12 +77,15 @@ public class Web3ModalClient { private let signClient: SignClientProtocol private let pairingClient: PairingClientProtocol & PairingInteracting & PairingRegisterer private let store: Store - + public let logger: ConsoleLogging + init( + logger: ConsoleLogging, signClient: SignClientProtocol, pairingClient: PairingClientProtocol & PairingInteracting & PairingRegisterer, store: Store ) { + self.logger = logger self.signClient = signClient self.pairingClient = pairingClient self.store = store @@ -90,6 +93,7 @@ public class Web3ModalClient { /// For creating new pairing public func createPairing() async throws -> WalletConnectURI { + logger.debug("Creating new pairing") do { return try await pairingClient.create() } catch { @@ -106,6 +110,7 @@ public class Web3ModalClient { public func connect( topic: String? ) async throws -> WalletConnectURI? { + logger.debug("Connecting Application") do { if let topic = topic { try pairingClient.validatePairingExistance(topic) @@ -143,6 +148,7 @@ public class Web3ModalClient { sessionProperties: [String: String]? = nil, topic: String ) async throws { + logger.debug("Connecting Application on topic: \(topic)") do { try await signClient.connect( requiredNamespaces: requiredNamespaces, @@ -173,6 +179,7 @@ public class Web3ModalClient { } public func request(_ request: W3MJSONRPC) async throws { + logger.debug("Requesting: \(request.rawValues.method)") switch store.connectedWith { case .wc: guard diff --git a/Sources/Web3Modal/Screens/ConnectWallet/AllWalletsView.swift b/Sources/Web3Modal/Screens/ConnectWallet/AllWalletsView.swift index ff7b09e..c14ebc5 100644 --- a/Sources/Web3Modal/Screens/ConnectWallet/AllWalletsView.swift +++ b/Sources/Web3Modal/Screens/ConnectWallet/AllWalletsView.swift @@ -8,7 +8,8 @@ struct AllWalletsView: View { @EnvironmentObject var router: Router @EnvironmentObject var store: Store @EnvironmentObject var interactor: W3MAPIInteractor - + @EnvironmentObject var signInteractor: SignInteractor + @State var searchTerm: String = "" let searchTermPublisher = PassthroughSubject() @@ -134,7 +135,10 @@ struct AllWalletsView: View { private func gridElement(for wallet: Wallet) -> some View { Button(action: { - router.setRoute(Router.ConnectingSubpage.walletDetail(wallet)) + Task { + try await signInteractor.createPairingAndConnect() + router.setRoute(Router.ConnectingSubpage.walletDetail(wallet)) + } }, label: { Text(wallet.name) }) diff --git a/Sources/Web3Modal/Screens/ConnectWallet/ConnectWalletView.swift b/Sources/Web3Modal/Screens/ConnectWallet/ConnectWalletView.swift index 6d5c15f..1dbea2a 100644 --- a/Sources/Web3Modal/Screens/ConnectWallet/ConnectWalletView.swift +++ b/Sources/Web3Modal/Screens/ConnectWallet/ConnectWalletView.swift @@ -3,7 +3,8 @@ import SwiftUI struct ConnectWalletView: View { @EnvironmentObject var store: Store @EnvironmentObject var router: Router - + @EnvironmentObject var signInteractor: SignInteractor + let displayWCConnection = false var wallets: [Wallet] { @@ -57,7 +58,10 @@ struct ConnectWalletView: View { let tagTitle: String? = isRecent ? "RECENT" : nil Button(action: { - router.setRoute(Router.ConnectingSubpage.walletDetail(wallet)) + Task { + try await signInteractor.createPairingAndConnect() + router.setRoute(Router.ConnectingSubpage.walletDetail(wallet)) + } }, label: { Text(wallet.name) }) From b82ee0816b6e6b84752c23cf373c9a2c71dfb569 Mon Sep 17 00:00:00 2001 From: llbartekll Date: Mon, 4 Mar 2024 09:51:34 +0100 Subject: [PATCH 4/6] add error logs, replace socket --- Sample/Example.xcodeproj/project.pbxproj | 17 ++ Sample/Example/ExampleApp.swift | 2 +- Sample/Example/WCSocketFactory.swift | 192 +----------------- .../ConnectWallet/AllWalletsView.swift | 8 +- .../ConnectWallet/ConnectWalletView.swift | 8 +- Sources/Web3ModalUI/Miscellaneous/Toast.swift | 2 +- .../xcshareddata/swiftpm/Package.resolved | 9 + 7 files changed, 48 insertions(+), 190 deletions(-) diff --git a/Sample/Example.xcodeproj/project.pbxproj b/Sample/Example.xcodeproj/project.pbxproj index 110bb0f..06e7648 100644 --- a/Sample/Example.xcodeproj/project.pbxproj +++ b/Sample/Example.xcodeproj/project.pbxproj @@ -8,6 +8,7 @@ /* Begin PBXBuildFile section */ 23F6FD03919B41DE98CAFCD3 /* Sentry in Frameworks */ = {isa = PBXBuildFile; productRef = BD206AA550964C49AE94A3CA /* Sentry */; }; + 84D9CCC02B95C277001EDEE3 /* Starscream in Frameworks */ = {isa = PBXBuildFile; productRef = 84D9CCBF2B95C277001EDEE3 /* Starscream */; }; CF0BCCE52AB0886400A2866C /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = CF0BCCE42AB0886400A2866C /* ContentView.swift */; }; CF25F3A22B40C7070030B3DC /* Web3Modal in Frameworks */ = {isa = PBXBuildFile; productRef = CF25F3A12B40C7070030B3DC /* Web3Modal */; }; CF25F3A42B40C7070030B3DC /* Web3ModalUI in Frameworks */ = {isa = PBXBuildFile; productRef = CF25F3A32B40C7070030B3DC /* Web3ModalUI */; }; @@ -43,6 +44,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + 84D9CCC02B95C277001EDEE3 /* Starscream in Frameworks */, CF3B9AD02ACDBA3A00984D53 /* Web3Modal in Frameworks */, CFD6A70F2ADE8DE2002B402C /* Atlantis in Frameworks */, CF25F3A42B40C7070030B3DC /* Web3ModalUI in Frameworks */, @@ -130,6 +132,7 @@ BD206AA550964C49AE94A3CA /* Sentry */, CF25F3A12B40C7070030B3DC /* Web3Modal */, CF25F3A32B40C7070030B3DC /* Web3ModalUI */, + 84D9CCBF2B95C277001EDEE3 /* Starscream */, ); productName = Example; productReference = CFD720742A9CC60600636CAF /* Example.app */; @@ -163,6 +166,7 @@ CFD6A70D2ADE8DE2002B402C /* XCRemoteSwiftPackageReference "atlantis" */, CF25F3A02B40C7070030B3DC /* XCLocalSwiftPackageReference ".." */, F4A0329B6CFF49E682D3DFE7 /* XCRemoteSwiftPackageReference "sentry-cocoa" */, + 84D9CCBE2B95C277001EDEE3 /* XCRemoteSwiftPackageReference "Starscream" */, ); productRefGroup = CFD720752A9CC60600636CAF /* Products */; projectDirPath = ""; @@ -468,6 +472,14 @@ /* End XCLocalSwiftPackageReference section */ /* Begin XCRemoteSwiftPackageReference section */ + 84D9CCBE2B95C277001EDEE3 /* XCRemoteSwiftPackageReference "Starscream" */ = { + isa = XCRemoteSwiftPackageReference; + repositoryURL = "https://github.com/daltoniam/Starscream"; + requirement = { + kind = upToNextMajorVersion; + minimumVersion = 3.1.2; + }; + }; CFD6A70D2ADE8DE2002B402C /* XCRemoteSwiftPackageReference "atlantis" */ = { isa = XCRemoteSwiftPackageReference; repositoryURL = "https://github.com/ProxymanApp/atlantis"; @@ -487,6 +499,11 @@ /* End XCRemoteSwiftPackageReference section */ /* Begin XCSwiftPackageProductDependency section */ + 84D9CCBF2B95C277001EDEE3 /* Starscream */ = { + isa = XCSwiftPackageProductDependency; + package = 84D9CCBE2B95C277001EDEE3 /* XCRemoteSwiftPackageReference "Starscream" */; + productName = Starscream; + }; BD206AA550964C49AE94A3CA /* Sentry */ = { isa = XCSwiftPackageProductDependency; package = F4A0329B6CFF49E682D3DFE7 /* XCRemoteSwiftPackageReference "sentry-cocoa" */; diff --git a/Sample/Example/ExampleApp.swift b/Sample/Example/ExampleApp.swift index d86e38d..521ad06 100644 --- a/Sample/Example/ExampleApp.swift +++ b/Sample/Example/ExampleApp.swift @@ -51,7 +51,7 @@ class ExampleApp: App { Networking.configure( groupIdentifier: "group.com.walletconnect.web3modal", projectId: projectId, - socketFactory: WalletConnectSocketClientFactory() + socketFactory: DefaultSocketFactory() ) Web3Modal.configure( diff --git a/Sample/Example/WCSocketFactory.swift b/Sample/Example/WCSocketFactory.swift index 3a21e6b..fc929b2 100644 --- a/Sample/Example/WCSocketFactory.swift +++ b/Sample/Example/WCSocketFactory.swift @@ -1,190 +1,14 @@ import Foundation import WalletConnectRelay +import Starscream -enum WalletConnectSocketClientError: Error { - case errorWithCode(URLSessionWebSocketTask.CloseCode) -} - -public struct WalletConnectSocketClientFactory: WebSocketFactory { - public init() { } - - public func create(with url: URL) -> WebSocketConnecting { - return WalletConnectSocketClient(url: url, logger: ConsoleLogger(loggingLevel: .debug)) - } -} - -public final class WalletConnectSocketClient: NSObject, WebSocketConnecting { - private var socket: URLSessionWebSocketTask? = nil - private var url: URL - private let logger: ConsoleLogging - - private var _request: URLRequest - private var _isConnected = false - - // MARK: - WebSocketConnecting - public var isConnected: Bool { - get { - lock.locked { - _isConnected - } - } - - set { - lock.locked { - _isConnected = newValue - } - } - } - - public var request: URLRequest { - get { - lock.locked { - _request - } - } - - set { - lock.locked { - _request = newValue - if let url = _request.url { - self.url = url - createSocketConnection() - _isConnected = false - } - } - } - } - - public var onConnect: (() -> Void)? - public var onDisconnect: ((Error?) -> Void)? - public var onText: ((String) -> Void)? - - private let lock = UnfairLock() - - init(url: URL, logger: ConsoleLogging) { - self.url = url - self.logger = logger - self._request = URLRequest(url: url) - super.init() - } - - // MARK: - WebSocketConnecting - public func connect() { - lock.locked { - logger.debug("[WebSocketClient]: Connect called 🔗 \(url.host ?? "nil")") - socket?.resume() - } - } - - public func disconnect() { - lock.locked { - logger.debug("[WebSocketClient]: Disconnect called") - socket?.cancel() - _isConnected = false - } - } - - public func write(string: String, completion: (() -> Void)?) { - lock.locked { - let message = URLSessionWebSocketTask.Message.string(string) - socket?.send(message) { _ in - completion?() - } - } - } - - public func reconnect() { - createSocketConnection() - - isConnected = false - connect() - } - - private func createSocketConnection() { - let configuration = URLSessionConfiguration.default - let urlSession = URLSession(configuration: configuration, delegate: self, delegateQueue: OperationQueue()) - let urlRequest = URLRequest(url: url) - socket = urlSession.webSocketTask(with: urlRequest) - } -} - -// MARK: - URLSessionWebSocketDelegate -extension WalletConnectSocketClient: URLSessionWebSocketDelegate { - public func urlSession(_ session: URLSession, webSocketTask: URLSessionWebSocketTask, didOpenWithProtocol protocol: String?) { - isConnected = true - logger.debug("[WebSocketClient]: Connected") - onConnect?() - receiveMessage() - } - - public func urlSession(_ session: URLSession, webSocketTask: URLSessionWebSocketTask, didCloseWith closeCode: URLSessionWebSocketTask.CloseCode, reason: Data?) { - if isConnected { - isConnected = false - logger.debug("[WebSocketClient]: Did close with code: \(closeCode)") - onDisconnect?(WalletConnectSocketClientError.errorWithCode(closeCode)) - } - } - - public func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) { - logger.debug("[WebSocketClient]: Did complete with error: \(error?.localizedDescription ?? "unknown")") - } - - func receiveMessage() { - socket?.receive { [weak self] result in - guard let self else { - return - } - switch result { - case .failure(let error): - self.logger.debug("[WebSocketClient]: Error receiving: \(error)") - let nsError = error as NSError - if nsError.code == 57 && nsError.domain == "NSPOSIXErrorDomain" { - if self.isConnected { - self.isConnected = false - self.reconnect() - } - } - - case .success(let message): - switch message { - case .string(let messageString): - self.logger.debug("[WebSocketClient]: Received message: \(messageString)") - self.onText?(messageString) - - case .data(let data): - self.logger.debug("[WebSocketClient]: Received data: \(data.description)") - self.onText?(data.description) - - default: - self.logger.debug("[WebSocketClient]: Received unknown data") - } - } - if self.isConnected == true { - self.receiveMessage() - } - } - } -} - - -import Foundation -import Combine - -final class UnfairLock { - private var _lock: UnsafeMutablePointer - - init() { - _lock = UnsafeMutablePointer.allocate(capacity: 1) - _lock.initialize(to: os_unfair_lock()) - } - - deinit { - _lock.deallocate() - } +extension WebSocket: WebSocketConnecting { } - func locked(_ f: () throws -> ReturnValue) rethrows -> ReturnValue { - os_unfair_lock_lock(_lock) - defer { os_unfair_lock_unlock(_lock) } - return try f() +struct DefaultSocketFactory: WebSocketFactory { + func create(with url: URL) -> WebSocketConnecting { + let socket = WebSocket(url: url) + let queue = DispatchQueue(label: "com.walletconnect.sdk.sockets", attributes: .concurrent) + socket.callbackQueue = queue + return socket } } diff --git a/Sources/Web3Modal/Screens/ConnectWallet/AllWalletsView.swift b/Sources/Web3Modal/Screens/ConnectWallet/AllWalletsView.swift index c14ebc5..fcba55e 100644 --- a/Sources/Web3Modal/Screens/ConnectWallet/AllWalletsView.swift +++ b/Sources/Web3Modal/Screens/ConnectWallet/AllWalletsView.swift @@ -136,8 +136,12 @@ struct AllWalletsView: View { private func gridElement(for wallet: Wallet) -> some View { Button(action: { Task { - try await signInteractor.createPairingAndConnect() - router.setRoute(Router.ConnectingSubpage.walletDetail(wallet)) + do { + try await signInteractor.createPairingAndConnect() + router.setRoute(Router.ConnectingSubpage.walletDetail(wallet)) + } catch { + store.toast = .init(style: .error, message: error.localizedDescription) + } } }, label: { Text(wallet.name) diff --git a/Sources/Web3Modal/Screens/ConnectWallet/ConnectWalletView.swift b/Sources/Web3Modal/Screens/ConnectWallet/ConnectWalletView.swift index 1dbea2a..2bcdd6b 100644 --- a/Sources/Web3Modal/Screens/ConnectWallet/ConnectWalletView.swift +++ b/Sources/Web3Modal/Screens/ConnectWallet/ConnectWalletView.swift @@ -59,8 +59,12 @@ struct ConnectWalletView: View { Button(action: { Task { - try await signInteractor.createPairingAndConnect() - router.setRoute(Router.ConnectingSubpage.walletDetail(wallet)) + do { + try await signInteractor.createPairingAndConnect() + router.setRoute(Router.ConnectingSubpage.walletDetail(wallet)) + } catch { + store.toast = .init(style: .error, message: error.localizedDescription) + } } }, label: { Text(wallet.name) diff --git a/Sources/Web3ModalUI/Miscellaneous/Toast.swift b/Sources/Web3ModalUI/Miscellaneous/Toast.swift index ac720ef..61237fd 100644 --- a/Sources/Web3ModalUI/Miscellaneous/Toast.swift +++ b/Sources/Web3ModalUI/Miscellaneous/Toast.swift @@ -6,7 +6,7 @@ public struct Toast: Equatable { var duration: Double = 1.5 var width: Double = .infinity - public init(style: ToastStyle, message: String, duration: Double = 3, width: Double = .infinity) { + public init(style: ToastStyle, message: String, duration: Double = 1.5, width: Double = .infinity) { self.style = style self.message = message self.duration = duration diff --git a/Web3Modal.xcworkspace/xcshareddata/swiftpm/Package.resolved b/Web3Modal.xcworkspace/xcshareddata/swiftpm/Package.resolved index e510d7a..5723037 100644 --- a/Web3Modal.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/Web3Modal.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -27,6 +27,15 @@ "version" : "8.18.0" } }, + { + "identity" : "starscream", + "kind" : "remoteSourceControl", + "location" : "https://github.com/daltoniam/Starscream", + "state" : { + "revision" : "a063fda2b8145a231953c20e7a646be254365396", + "version" : "3.1.2" + } + }, { "identity" : "swift-qrcode-generator", "kind" : "remoteSourceControl", From 2dc8d15a668e01f4fbf4ff9e06ac7f7b65df2ad7 Mon Sep 17 00:00:00 2001 From: llbartekll Date: Mon, 4 Mar 2024 10:48:36 +0100 Subject: [PATCH 5/6] fix wallet disconnect issue --- Sources/Web3Modal/Sheets/Web3ModalViewModel.swift | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sources/Web3Modal/Sheets/Web3ModalViewModel.swift b/Sources/Web3Modal/Sheets/Web3ModalViewModel.swift index 967e7ec..fec97e9 100644 --- a/Sources/Web3Modal/Sheets/Web3ModalViewModel.swift +++ b/Sources/Web3Modal/Sheets/Web3ModalViewModel.swift @@ -77,6 +77,7 @@ class Web3ModalViewModel: ObservableObject { if store.session?.topic == topic { store.session = nil + store.account = nil } router.setRoute(Router.ConnectingSubpage.connectWallet) } @@ -89,6 +90,7 @@ class Web3ModalViewModel: ObservableObject { if sessions.isEmpty { DispatchQueue.main.async { store.session = nil + store.account = nil router.setRoute(Router.ConnectingSubpage.connectWallet) } } From 220b62ba4d6e96b4a5463ecf3b0c366acddcc672 Mon Sep 17 00:00:00 2001 From: llbartekll Date: Mon, 4 Mar 2024 13:23:39 +0100 Subject: [PATCH 6/6] fix retry display --- Sources/Web3Modal/Core/SignInteractor.swift | 1 + .../WalletDetail/WalletDetailView.swift | 20 +++++++++---------- .../WalletDetail/WalletDetailViewModel.swift | 11 +++------- Sources/Web3Modal/Store.swift | 3 ++- .../xcshareddata/swiftpm/Package.resolved | 8 ++++---- 5 files changed, 20 insertions(+), 23 deletions(-) diff --git a/Sources/Web3Modal/Core/SignInteractor.swift b/Sources/Web3Modal/Core/SignInteractor.swift index cb23a26..fb12017 100644 --- a/Sources/Web3Modal/Core/SignInteractor.swift +++ b/Sources/Web3Modal/Core/SignInteractor.swift @@ -21,6 +21,7 @@ class SignInteractor: ObservableObject { DispatchQueue.main.async { self.store.uri = uri + self.store.retryShown = false } } diff --git a/Sources/Web3Modal/Screens/ConnectWallet/WalletDetail/WalletDetailView.swift b/Sources/Web3Modal/Screens/ConnectWallet/WalletDetail/WalletDetailView.swift index 3ae905b..8372ddb 100644 --- a/Sources/Web3Modal/Screens/ConnectWallet/WalletDetail/WalletDetailView.swift +++ b/Sources/Web3Modal/Screens/ConnectWallet/WalletDetail/WalletDetailView.swift @@ -108,11 +108,11 @@ struct WalletDetailView: View { .padding(2) .background(Color.Background125) .clipShape(Circle()) - .opacity(viewModel.retryShown ? 1 : 0) + .opacity(store.retryShown ? 1 : 0) .offset(x: 5, y: 5) } - if !viewModel.retryShown { + if !store.retryShown { DrawingProgressView( shape: .roundedRectangleRelative(relativeCornerRadius: Radius.m / 80), color: .Blue100, @@ -129,7 +129,7 @@ struct WalletDetailView: View { viewModel.handle(.didTapOpen) } label: { HStack { - Text(!viewModel.retryShown ? "Open" : "Try again") + Text(!store.retryShown ? "Open" : "Try again") } .font(.small600) .foregroundColor(.Blue100) @@ -138,8 +138,8 @@ struct WalletDetailView: View { W3MButtonStyle( size: .m, variant: .accent, - leftIcon: viewModel.retryShown ? Image.Bold.refresh : nil, - rightIcon: viewModel.retryShown ? nil : Image.Bold.externalLink + leftIcon: store.retryShown ? Image.Bold.refresh : nil, + rightIcon: store.retryShown ? nil : Image.Bold.externalLink ) ) } @@ -150,13 +150,13 @@ struct WalletDetailView: View { .padding(.top, 40) .padding(.bottom, Spacing.xl) - Text(viewModel.retryShown ? "Connection declined" : "Continue in \(viewModel.wallet.name)") + Text(store.retryShown ? "Connection declined" : "Continue in \(viewModel.wallet.name)") .font(.paragraph600) - .foregroundColor(viewModel.retryShown ? .Error100 : .Foreground100) + .foregroundColor(store.retryShown ? .Error100 : .Foreground100) .padding(.bottom, Spacing.xs) Text( - viewModel.retryShown + store.retryShown ? "Connection can be declined if a previous request is still active" : viewModel.preferredPlatform == .browser ? "Open and continue in a new browser tab" : "Accept connection request in the wallet" ) @@ -165,7 +165,7 @@ struct WalletDetailView: View { .multilineTextAlignment(.center) .padding(.bottom, Spacing.l) - if viewModel.retryShown || viewModel.preferredPlatform == .browser { + if store.retryShown || viewModel.preferredPlatform == .browser { retryButton() } } @@ -238,7 +238,7 @@ class MockWalletDetailViewModel: WalletDetailViewModel { store: store ) - self.retryShown = retryShown +// self.retryShown = retryShown } override func startObserving() { diff --git a/Sources/Web3Modal/Screens/ConnectWallet/WalletDetail/WalletDetailViewModel.swift b/Sources/Web3Modal/Screens/ConnectWallet/WalletDetail/WalletDetailViewModel.swift index fcd43ab..99e2843 100644 --- a/Sources/Web3Modal/Screens/ConnectWallet/WalletDetail/WalletDetailViewModel.swift +++ b/Sources/Web3Modal/Screens/ConnectWallet/WalletDetail/WalletDetailViewModel.swift @@ -22,7 +22,6 @@ class WalletDetailViewModel: ObservableObject { let signInteractor: SignInteractor @Published var preferredPlatform: Platform = .mobile - @Published var retryShown = false private var disposeBag = Set() @@ -51,12 +50,8 @@ class WalletDetailViewModel: ObservableObject { func startObserving() { Web3Modal.instance.sessionRejectionPublisher .receive(on: DispatchQueue.main) - .sink { _, _ in - self.retryShown = true - - Task { - try await self.signInteractor.createPairingAndConnect() - } + .sink { [weak self] _, _ in + self?.store.retryShown = true } .store(in: &disposeBag) } @@ -83,7 +78,7 @@ class WalletDetailViewModel: ObservableObject { store.recentWallets.append(wallet) case .didTapOpen: - retryShown = false + store.retryShown = false if wallet.alternativeConnectionMethod == nil { navigateToDeepLink( diff --git a/Sources/Web3Modal/Store.swift b/Sources/Web3Modal/Store.swift index 22e4f7d..5dfbf07 100644 --- a/Sources/Web3Modal/Store.swift +++ b/Sources/Web3Modal/Store.swift @@ -10,7 +10,8 @@ class Store: ObservableObject { static var shared: Store = .init() @Published var isModalShown: Bool = false - + @Published var retryShown = false + @Published var identity: Identity? @Published var balance: Double? diff --git a/Web3Modal.xcworkspace/xcshareddata/swiftpm/Package.resolved b/Web3Modal.xcworkspace/xcshareddata/swiftpm/Package.resolved index 5723037..842d072 100644 --- a/Web3Modal.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/Web3Modal.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -5,8 +5,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/ProxymanApp/atlantis", "state" : { - "revision" : "131d757cf8e6e368ad338728379174f7cfff9326", - "version" : "1.23.0" + "revision" : "5145a7041ec71421d09653db87dcc80c81792004", + "version" : "1.24.0" } }, { @@ -23,8 +23,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/getsentry/sentry-cocoa/", "state" : { - "revision" : "3b9a8e69ca296bd8cd0e317ad7a448e5daf4a342", - "version" : "8.18.0" + "revision" : "38f4f70d07117b9f958a76b1bff278c2f29ffe0e", + "version" : "8.21.0" } }, {