Skip to content

Commit

Permalink
- remove sessionDeletePublisher events to clear pair objects
Browse files Browse the repository at this point in the history
- fix issue with deeplinks coming to a closed app
  • Loading branch information
simonmcl committed Dec 16, 2023
1 parent 221f5ed commit 828c800
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 27 deletions.
6 changes: 4 additions & 2 deletions Kukai Mobile/Modules/Side Menu/WalletConnectViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
44 changes: 22 additions & 22 deletions Kukai Mobile/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

Expand Down Expand Up @@ -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)
}


Expand Down Expand Up @@ -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)
}
}
}
33 changes: 30 additions & 3 deletions Kukai Mobile/Services/WalletConnectService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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<Any, Never>()

@Published public var didCleanAfterDelete: Bool = false
@Published public var requestDidComplete: Bool = false

Expand All @@ -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
Expand Down Expand Up @@ -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
}
}
}


Expand Down Expand Up @@ -309,6 +334,7 @@ public class WalletConnectService {
}
}

/*
@MainActor
public static func cleanupSessionlessPairs() async {
var pairsToClean: [Pairing] = []
Expand All @@ -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 {
Expand Down

0 comments on commit 828c800

Please sign in to comment.