Skip to content

Commit

Permalink
Initialize WalletConnectURI from deeplinks
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-lsvk committed Sep 26, 2023
1 parent b6ba4b9 commit ffe2ea4
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
3 changes: 1 addition & 2 deletions Example/WalletApp/ApplicationLayer/Application.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Foundation
import WalletConnectChat

final class Application {
var uri: String?
var uri: WalletConnectURI?
var requestSent = false

lazy var pushRegisterer = PushRegisterer()
Expand All @@ -11,4 +11,3 @@ final class Application {
lazy var messageSigner = MessageSignerFactory(signerFactory: DefaultSignerFactory()).create()
lazy var configurationService = ConfigurationService()
}

8 changes: 4 additions & 4 deletions Example/WalletApp/ApplicationLayer/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final class SceneDelegate: UIResponder, UIWindowSceneDelegate {
window = UIWindow(windowScene: windowScene)
window?.makeKeyAndVisible()

app.uri = connectionOptions.urlContexts.first?.url.absoluteString.replacingOccurrences(of: "walletapp://wc?uri=", with: "")
app.uri = WalletConnectURI(connectionOptions: connectionOptions)
app.requestSent = (connectionOptions.urlContexts.first?.url.absoluteString.replacingOccurrences(of: "walletapp://wc?", with: "") == "requestSent")

configurators.configure()
Expand All @@ -37,11 +37,11 @@ final class SceneDelegate: UIResponder, UIWindowSceneDelegate {
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
guard let context = URLContexts.first else { return }

let queryParams = context.url.queryParameters
let uri = WalletConnectURI(urlContext: context)

if let uri = queryParams["uri"] as? String {
if let uri {
Task {
try await Pair.instance.pair(uri: WalletConnectURI(string: uri)!)
try await Pair.instance.pair(uri: uri)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,10 @@ extension WalletPresenter {
}

private func pairFromDapp() {
guard let uri = app.uri,
let walletConnectUri = WalletConnectURI(string: uri)
else {
guard let uri = app.uri else {
return
}
pair(uri: walletConnectUri)
pair(uri: uri)
}

private func removePairingIndicator() {
Expand Down
23 changes: 22 additions & 1 deletion Sources/WalletConnectUtils/WalletConnectURI.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Foundation
import UIKit

This comment has been minimized.

Copy link
@livid

livid Oct 3, 2023

This breaks building on macOS.

Screenshot 2023-10-02 at 11 46 50 PM

public struct WalletConnectURI: Equatable {

Expand Down Expand Up @@ -44,6 +44,27 @@ public struct WalletConnectURI: Equatable {
self.symKey = symKey
self.relay = RelayProtocolOptions(protocol: relayProtocol, data: relayData)
}

public init?(deeplinkUri: URL) {
if let deeplinkUri = deeplinkUri.query?.replacingOccurrences(of: "uri=", with: "") {
self.init(string: deeplinkUri)
}
return nil
}

public init?(connectionOptions: UIScene.ConnectionOptions) {
if let uri = connectionOptions.urlContexts.first?.url.query?.replacingOccurrences(of: "uri=", with: "") {
self.init(string: uri)
}
return nil
}

public init?(urlContext: UIOpenURLContext) {
if let uri = urlContext.url.query?.replacingOccurrences(of: "uri=", with: "") {
self.init(string: uri)
}
return nil
}

private var relayQuery: String {
var query = "relay-protocol=\(relay.protocol)"
Expand Down

0 comments on commit ffe2ea4

Please sign in to comment.