From 828c80072a8a044e6b25c853a11506dd312ff6b6 Mon Sep 17 00:00:00 2001 From: Simon McLoughlin Date: Sat, 16 Dec 2023 20:58:16 +0000 Subject: [PATCH] - remove sessionDeletePublisher events to clear pair objects - fix issue with deeplinks coming to a closed app --- .../Side Menu/WalletConnectViewModel.swift | 6 ++- Kukai Mobile/SceneDelegate.swift | 44 +++++++++---------- .../Services/WalletConnectService.swift | 33 ++++++++++++-- 3 files changed, 56 insertions(+), 27 deletions(-) diff --git a/Kukai Mobile/Modules/Side Menu/WalletConnectViewModel.swift b/Kukai Mobile/Modules/Side Menu/WalletConnectViewModel.swift index ac4b6c44..2fec91d9 100644 --- a/Kukai Mobile/Modules/Side Menu/WalletConnectViewModel.swift +++ b/Kukai Mobile/Modules/Side Menu/WalletConnectViewModel.swift @@ -68,11 +68,13 @@ class WalletConnectViewModel: ViewModel, UITableViewDiffableDataSourceHandler { // Get data pairs = Pair.instance.getPairings().compactMap({ pair -> PairObj? in - if pair.peer == nil { + let sessions = Sign.instance.getSessions().filter({ $0.pairingTopic == pair.topic }) + + if pair.peer == nil || sessions.count == 0 { return nil } else { - let firstSession = Sign.instance.getSessions().filter({ $0.pairingTopic == pair.topic }).first + let firstSession = sessions.first let firstAccount = firstSession?.accounts.first let address = firstAccount?.address let network = firstAccount?.blockchain.reference == "ghostnet" ? "Ghostnet" : "Mainnet" diff --git a/Kukai Mobile/SceneDelegate.swift b/Kukai Mobile/SceneDelegate.swift index ddbfc4d5..e33f5651 100644 --- a/Kukai Mobile/SceneDelegate.swift +++ b/Kukai Mobile/SceneDelegate.swift @@ -28,9 +28,10 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate { window.overrideUserInterfaceStyle = ThemeManager.shared.currentInterfaceStyle() } - if let userActivity = connectionOptions.userActivities.first { - Logger.app.info("Handling user activity") - handle(userActivity: userActivity) + if let url = connectionOptions.urlContexts.first?.url { + handleDeeplink(url: url) + } else { + Logger.app.info("Launching without URL") } } @@ -70,17 +71,7 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate { return } - if url.absoluteString.prefix(10) == "kukai://wc" { - var wc2URI = String(url.absoluteString.dropFirst(15)) // just strip off "kukai://wc?uri=" - wc2URI = wc2URI.removingPercentEncoding ?? "" - - if let uri = WalletConnectURI(string: String(wc2URI)) { - WalletConnectService.shared.pairClient(uri: uri) - return - } - } - - CustomAuth.handle(url: url) + handleDeeplink(url: url) } @@ -110,15 +101,24 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate { } } - private func handle(userActivity: NSUserActivity) { - guard let url = userActivity.webpageURL, userActivity.activityType == NSUserActivityTypeBrowsingWeb else { return } + private func handleDeeplink(url: URL) { + Logger.app.info("Attempting to handle deeplink \(url.absoluteString)") - Logger.app.info("Attempting to handle Wallet Connect pairing") - let wcUri = url.absoluteString.deletingPrefix("https://walletconnect.com/wc?uri=") - guard let uri = WalletConnectURI(string: wcUri) else { return } - - Task(priority: .high) { - try? await Pair.instance.pair(uri: uri) + if url.absoluteString.prefix(10) == "kukai://wc" { + var wc2URI = String(url.absoluteString.dropFirst(15)) // just strip off "kukai://wc?uri=" + wc2URI = wc2URI.removingPercentEncoding ?? "" + + if let uri = WalletConnectURI(string: String(wc2URI)) { + + if WalletConnectService.shared.hasBeenSetup { + WalletConnectService.shared.pairClient(uri: uri) + + } else { + WalletConnectService.shared.deepLinkPairingToConnect = uri + } + } + } else { + CustomAuth.handle(url: url) } } } diff --git a/Kukai Mobile/Services/WalletConnectService.swift b/Kukai Mobile/Services/WalletConnectService.swift index f09a8090..87809103 100644 --- a/Kukai Mobile/Services/WalletConnectService.swift +++ b/Kukai Mobile/Services/WalletConnectService.swift @@ -54,6 +54,8 @@ public class WalletConnectService { private var temporaryBag = [AnyCancellable]() public static let shared = WalletConnectService() + public var deepLinkPairingToConnect: WalletConnectURI? = nil + public var hasBeenSetup = false public weak var delegate: WalletConnectServiceDelegate? = nil private static let projectId = "97f804b46f0db632c52af0556586a5f3" @@ -63,6 +65,8 @@ public class WalletConnectService { icons: ["https://wallet.kukai.app/assets/img/header-logo.svg"], redirect: AppMetadata.Redirect(native: "kukai://", universal: nil)) + //private var incomingRequestPublisher = PassthroughSubject() + @Published public var didCleanAfterDelete: Bool = false @Published public var requestDidComplete: Bool = false @@ -85,6 +89,18 @@ public class WalletConnectService { // Callbacks + /* + incomingRequestPublisher + .buffer(size: 10, prefetch: .byRequest, whenFull: .dropNewest) + .flatMap(maxPublishers: .max(1)) { [weak self] sessionRequest in + + } + .sink(receiveValue: { success in + Logger.app.info("WC request completed with success: \(success)") + }) + .store(in: &bag) + */ + Sign.instance.sessionRequestPublisher .buffer(size: 10, prefetch: .byRequest, whenFull: .dropNewest) .flatMap(maxPublishers: .max(1)) { [weak self] sessionRequest in @@ -119,11 +135,20 @@ public class WalletConnectService { .receive(on: DispatchQueue.main) .sink { [weak self] data in Logger.app.info("WC sessionDeletePublisher \(data.0)") - Task { [weak self] in - await WalletConnectService.cleanupSessionlessPairs() + //Task { [weak self] in + //await WalletConnectService.cleanupSessionlessPairs() self?.didCleanAfterDelete = true - } + //} }.store(in: &bag) + + hasBeenSetup = true + + if let uri = deepLinkPairingToConnect { + Task { + await pairClient(uri: uri) + deepLinkPairingToConnect = nil + } + } } @@ -309,6 +334,7 @@ public class WalletConnectService { } } + /* @MainActor public static func cleanupSessionlessPairs() async { var pairsToClean: [Pairing] = [] @@ -323,6 +349,7 @@ public class WalletConnectService { try? await Pair.instance.disconnect(topic: pair.topic) }) } + */ @MainActor public static func reject(proposalId: String, reason: RejectionReason) throws {