Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#219 auth wrapper #222

Merged
merged 23 commits into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Example/DApp/ Accounts/AccountsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ struct AccountDetails {

final class AccountsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

let client = ClientDelegate.shared.client
let session: Session
var accountsDetails: [AccountDetails] = []
var onDisconnect: (()->())?
Expand Down Expand Up @@ -54,7 +53,7 @@ final class AccountsViewController: UIViewController, UITableViewDataSource, UIT
private func disconnect() {
Task {
do {
try await client.disconnect(topic: session.topic, reason: Reason(code: 0, message: "disconnect"))
try await Auth.instance.disconnect(topic: session.topic, reason: Reason(code: 0, message: "disconnect"))
DispatchQueue.main.async { [weak self] in
self?.onDisconnect?()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import WalletConnectUtils

class AccountRequestViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
private let session: Session
private let client: AuthClient = ClientDelegate.shared.client
private let chainId: String
private let account: String
private let methods = ["eth_sendTransaction", "personal_sign", "eth_signTypedData"]
Expand Down Expand Up @@ -61,7 +60,7 @@ class AccountRequestViewController: UIViewController, UITableViewDelegate, UITab
let request = Request(topic: session.topic, method: method, params: requestParams, chainId: Blockchain(chainId)!)
Task {
do {
try await client.request(params: request)
try await Auth.instance.request(params: request)
DispatchQueue.main.async { [weak self] in
self?.presentConfirmationAlert()
}
Expand Down
49 changes: 0 additions & 49 deletions Example/DApp/ClientDelegate.swift

This file was deleted.

4 changes: 2 additions & 2 deletions Example/DApp/Connect/ConnectViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import WalletConnectAuth

class ConnectViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
let uriString: String
let activePairings: [Pairing] = ClientDelegate.shared.client.getSettledPairings()
let activePairings: [Pairing] = Auth.instance.getSettledPairings()
let segmentedControl = UISegmentedControl(items: ["Pairings", "New Pairing"])

init(uri: String) {
Expand Down Expand Up @@ -99,7 +99,7 @@ class ConnectViewController: UIViewController, UITableViewDataSource, UITableVie
let methods: Set<String> = ["eth_sendTransaction", "personal_sign", "eth_signTypedData"]
let namespaces: [String: ProposalNamespace] = ["eip155": ProposalNamespace(chains: blockchains, methods: methods, events: [], extensions: nil)]
Task {
_ = try await ClientDelegate.shared.client.connect(requiredNamespaces: namespaces, topic: pairingTopic)
_ = try await Auth.instance.connect(requiredNamespaces: namespaces, topic: pairingTopic)
connectWithExampleWallet()
}
}
Expand Down
2 changes: 1 addition & 1 deletion Example/DApp/ResponseViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ResponseViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
let record = ClientDelegate.shared.client.getSessionRequestRecord(id: response.result.id)!
let record = Auth.instance.getSessionRequestRecord(id: response.result.id)!
switch response.result {
case .response(let response):
responseView.nameLabel.text = "Received Response\n\(record.request.method)"
Expand Down
25 changes: 19 additions & 6 deletions Example/DApp/SceneDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@

import UIKit
import WalletConnectAuth
import Combine

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

var window: UIWindow?
private var publishers = [AnyCancellable]()


func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
Expand All @@ -13,13 +15,24 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
ClientDelegate.shared.onSessionDelete = { [unowned self] in
showSelectChainScreen()
}
ClientDelegate.shared.onSessionResponse = { [unowned self] response in
let metadata = AppMetadata(
name: "Swift Dapp",
description: "a description",
url: "wallet.connect",
icons: ["https://gblobscdn.gitbook.com/spaces%2F-LJJeCjcLrr53DcT1Ml7%2Favatar.png?alt=media"])
Auth.configure(Auth.Config(metadata: metadata, projectId: "8ba9ee138960775e5231b70cc5ef1c3a"))
Auth.instance.sessionDeletePublisher
.receive(on: DispatchQueue.main)
.sink { [unowned self] _ in
showSelectChainScreen()
}.store(in: &publishers)

Auth.instance.sessionResponsePublisher.sink { [unowned self] response in
presentResponse(for: response)
}
if let session = ClientDelegate.shared.client.getSettledSessions().first {
}.store(in: &publishers)


if let session = Auth.instance.getSettledSessions().first {
showAccountsScreen(session)
} else {
showSelectChainScreen()
Expand Down
12 changes: 6 additions & 6 deletions Example/DApp/SelectChain/SelectChainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import Foundation
import WalletConnectAuth
import UIKit
import Combine

struct Chain {
let name: String
Expand All @@ -13,23 +14,22 @@ class SelectChainViewController: UIViewController, UITableViewDataSource {
private let selectChainView: SelectChainView = {
SelectChainView()
}()
private var publishers = [AnyCancellable]()

let chains = [Chain(name: "Ethereum", id: "eip155:1"), Chain(name: "Polygon", id: "eip155:137")]
let client = ClientDelegate.shared.client
var onSessionSettled: ((Session)->())?
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Available Chains"
selectChainView.tableView.dataSource = self
selectChainView.connectButton.addTarget(self, action: #selector(connect), for: .touchUpInside)
ClientDelegate.shared.onSessionSettled = { [unowned self] session in
Auth.instance.sessionSettlePublisher.sink {[unowned self] session in
onSessionSettled?(session)
}
}.store(in: &publishers)
}

override func loadView() {
view = selectChainView


}

@objc
Expand All @@ -39,7 +39,7 @@ class SelectChainViewController: UIViewController, UITableViewDataSource {
let blockchains: Set<Blockchain> = [Blockchain("eip155:1")!, Blockchain("eip155:137")!]
let namespaces: [String: ProposalNamespace] = ["eip155": ProposalNamespace(chains: blockchains, methods: methods, events: [], extensions: nil)]
Task {
let uri = try await client.connect(requiredNamespaces: namespaces)
let uri = try await Auth.instance.connect(requiredNamespaces: namespaces)
showConnectScreen(uriString: uri!)
}
}
Expand Down
12 changes: 4 additions & 8 deletions Example/ExampleApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
84CE64392798228D00142511 /* Web3 in Frameworks */ = {isa = PBXBuildFile; productRef = 84CE64382798228D00142511 /* Web3 */; };
84CE643D2798322600142511 /* ConnectViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84CE643C2798322600142511 /* ConnectViewController.swift */; };
84CE6444279AB5AD00142511 /* SelectChainViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84CE6443279AB5AD00142511 /* SelectChainViewController.swift */; };
84CE6446279ABBF300142511 /* ClientDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84CE6445279ABBF300142511 /* ClientDelegate.swift */; };
84CE6448279AE68600142511 /* AccountRequestViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84CE6447279AE68600142511 /* AccountRequestViewController.swift */; };
84CE644B279EA1FA00142511 /* AccountRequestView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84CE644A279EA1FA00142511 /* AccountRequestView.swift */; };
84CE644E279ED2FF00142511 /* SelectChainView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84CE644D279ED2FF00142511 /* SelectChainView.swift */; };
Expand Down Expand Up @@ -113,7 +112,6 @@
84CE642C27981DF000142511 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
84CE643C2798322600142511 /* ConnectViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConnectViewController.swift; sourceTree = "<group>"; };
84CE6443279AB5AD00142511 /* SelectChainViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SelectChainViewController.swift; sourceTree = "<group>"; };
84CE6445279ABBF300142511 /* ClientDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClientDelegate.swift; sourceTree = "<group>"; };
84CE6447279AE68600142511 /* AccountRequestViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountRequestViewController.swift; sourceTree = "<group>"; };
84CE644A279EA1FA00142511 /* AccountRequestView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountRequestView.swift; sourceTree = "<group>"; };
84CE644D279ED2FF00142511 /* SelectChainView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SelectChainView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -276,7 +274,6 @@
84CE642027981DED00142511 /* SceneDelegate.swift */,
84CE645427A29D4C00142511 /* ResponseViewController.swift */,
84CE6449279EA1E600142511 /* AccountRequest */,
84CE6445279ABBF300142511 /* ClientDelegate.swift */,
84CE644C279ED2EC00142511 /* SelectChain */,
84CE6450279ED41D00142511 /* Connect */,
84CE644F279ED3FB00142511 /* Accounts */,
Expand Down Expand Up @@ -540,7 +537,6 @@
files = (
84CE6430279820F600142511 /* AccountsViewController.swift in Sources */,
84CE645527A29D4D00142511 /* ResponseViewController.swift in Sources */,
84CE6446279ABBF300142511 /* ClientDelegate.swift in Sources */,
84CE641F27981DED00142511 /* AppDelegate.swift in Sources */,
84CE6452279ED42B00142511 /* ConnectView.swift in Sources */,
84CE6448279AE68600142511 /* AccountRequestViewController.swift in Sources */,
Expand Down Expand Up @@ -733,7 +729,7 @@
CODE_SIGN_ENTITLEMENTS = Wallet.entitlements;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 9;
CURRENT_PROJECT_VERSION = 11;
DEVELOPMENT_TEAM = W5R8AG9K22;
INFOPLIST_FILE = ExampleApp/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
Expand All @@ -758,7 +754,7 @@
CODE_SIGN_ENTITLEMENTS = Wallet.entitlements;
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 9;
CURRENT_PROJECT_VERSION = 11;
DEVELOPMENT_TEAM = W5R8AG9K22;
INFOPLIST_FILE = ExampleApp/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 15.0;
Expand All @@ -783,7 +779,7 @@
CLANG_CXX_LANGUAGE_STANDARD = "gnu++17";
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 3;
CURRENT_PROJECT_VERSION = 5;
DEVELOPMENT_TEAM = W5R8AG9K22;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = DApp/Info.plist;
Expand Down Expand Up @@ -817,7 +813,7 @@
CLANG_CXX_LANGUAGE_STANDARD = "gnu++17";
CODE_SIGN_IDENTITY = "Apple Development";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 3;
CURRENT_PROJECT_VERSION = 5;
DEVELOPMENT_TEAM = W5R8AG9K22;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = DApp/Info.plist;
Expand Down
11 changes: 10 additions & 1 deletion Example/ExampleApp/SceneDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import UIKit
import WalletConnectAuth

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

var window: UIWindow?

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

let metadata = AppMetadata(
name: "Example Wallet",
description: "wallet description",
url: "example.wallet",
icons: ["https://gblobscdn.gitbook.com/spaces%2F-LJJeCjcLrr53DcT1Ml7%2Favatar.png?alt=media"])
Auth.configure(Auth.Config(metadata: metadata, projectId: "8ba9ee138960775e5231b70cc5ef1c3a"))

guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
window?.rootViewController = UITabBarController.createExampleApp()
Expand All @@ -21,7 +30,7 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
vc.onClientConnected = {
Task {
do {
try await vc.client.pair(uri: wcUri)
try await Auth.instance.pair(uri: wcUri)
} catch {
print(error)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ final class SessionDetailsViewController: UIViewController, UITableViewDelegate,
SessionDetailsView()
}()
private var sessionInfo: SessionInfo
private let client: AuthClient
private let session: Session
init(_ session: Session, _ client: AuthClient) {
let pendingRequests = client.getPendingRequests(topic: session.topic).map{$0.method}
init(_ session: Session) {
let pendingRequests = Auth.instance.getPendingRequests(topic: session.topic).map{$0.method}
let chains = Array(session.namespaces.values.flatMap { n in n.accounts.map{$0.blockchain.absoluteString}})
let methods = Array(session.namespaces.values.first?.methods ?? []) // TODO: Rethink how to show this info on example app
self.sessionInfo = SessionInfo(name: session.peer.name,
Expand All @@ -20,7 +19,6 @@ final class SessionDetailsViewController: UIViewController, UITableViewDelegate,
chains: chains,
methods: methods,
pendingRequests: pendingRequests)
self.client = client
self.session = session
super.init(nibName: nil, bundle: nil)
}
Expand Down Expand Up @@ -51,7 +49,7 @@ final class SessionDetailsViewController: UIViewController, UITableViewDelegate,

@objc
private func ping() {
client.ping(topic: session.topic) { result in
Auth.instance.ping(topic: session.topic) { result in
switch result {
case .success():
print("received ping response")
Expand Down Expand Up @@ -101,7 +99,7 @@ final class SessionDetailsViewController: UIViewController, UITableViewDelegate,

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if indexPath.section == 2 {
let pendingRequests = client.getPendingRequests(topic: session.topic)
let pendingRequests = Auth.instance.getPendingRequests(topic: session.topic)
showSessionRequest(pendingRequests[indexPath.row])
}
}
Expand All @@ -111,18 +109,18 @@ final class SessionDetailsViewController: UIViewController, UITableViewDelegate,
requestVC.onSign = { [unowned self] in
let result = Signer.signEth(request: sessionRequest)
let response = JSONRPCResponse<AnyCodable>(id: sessionRequest.id, result: result)
client.respond(topic: sessionRequest.topic, response: .response(response))
Auth.instance.respond(topic: sessionRequest.topic, response: .response(response))
reloadTable()
}
requestVC.onReject = { [unowned self] in
client.respond(topic: sessionRequest.topic, response: .error(JSONRPCErrorResponse(id: sessionRequest.id, error: JSONRPCErrorResponse.Error(code: 0, message: ""))))
Auth.instance.respond(topic: sessionRequest.topic, response: .error(JSONRPCErrorResponse(id: sessionRequest.id, error: JSONRPCErrorResponse.Error(code: 0, message: ""))))
reloadTable()
}
present(requestVC, animated: true)
}

func reloadTable() {
let pendingRequests = client.getPendingRequests(topic: session.topic).map{$0.method}
let pendingRequests = Auth.instance.getPendingRequests(topic: session.topic).map{$0.method}
let chains = Array(session.namespaces.values.flatMap { n in n.accounts.map{$0.blockchain.absoluteString}})
let methods = Array(session.namespaces.values.first?.methods ?? []) // TODO: Rethink how to show this info on example app
self.sessionInfo = SessionInfo(name: session.peer.name,
Expand Down
Loading