From ce010da7652d54acf30ee88e7593e46d74a49766 Mon Sep 17 00:00:00 2001 From: Ivan Grachyov Date: Fri, 6 Aug 2021 18:58:32 +0300 Subject: [PATCH] Pass custom chain id to WalletConnect --- Encrypted Ink/Agent.swift | 12 ++++++------ .../Screens/AccountsListViewController.swift | 5 +++-- Encrypted Ink/Screens/ImportViewController.swift | 2 +- Encrypted Ink/Services/SessionStorage.swift | 5 +++-- Encrypted Ink/WalletConnect.swift | 13 ++++++------- 5 files changed, 19 insertions(+), 18 deletions(-) diff --git a/Encrypted Ink/Agent.swift b/Encrypted Ink/Agent.swift index 2d939b9b..7a749e46 100644 --- a/Encrypted Ink/Agent.swift +++ b/Encrypted Ink/Agent.swift @@ -119,7 +119,7 @@ class Agent: NSObject { showInitialScreen(wcSession: session) } - func getWalletSelectionCompletionIfShouldSelect() -> ((InkWallet) -> Void)? { + func getWalletSelectionCompletionIfShouldSelect() -> ((Int, InkWallet) -> Void)? { let session = getSessionFromPasteboard() return onSelectedWallet(session: session) } @@ -222,10 +222,10 @@ class Agent: NSObject { } } - private func onSelectedWallet(session: WCSession?) -> ((InkWallet) -> Void)? { + private func onSelectedWallet(session: WCSession?) -> ((Int, InkWallet) -> Void)? { guard let session = session else { return nil } - return { [weak self] wallet in - self?.connectWallet(session: session, wallet: wallet) + return { [weak self] chainId, wallet in + self?.connectWallet(session: session, chainId: chainId, wallet: wallet) } } @@ -284,12 +284,12 @@ class Agent: NSObject { } } - private func connectWallet(session: WCSession, wallet: InkWallet) { + private func connectWallet(session: WCSession, chainId: Int, wallet: InkWallet) { let windowController = Window.showNew() let window = windowController.window windowController.contentViewController = WaitingViewController.withReason("Connecting") - WalletConnect.shared.connect(session: session, walletId: wallet.id) { [weak window] _ in + WalletConnect.shared.connect(session: session, chainId: chainId, walletId: wallet.id) { [weak window] _ in if window?.isVisible == true { Window.closeAllAndActivateBrowser() } diff --git a/Encrypted Ink/Screens/AccountsListViewController.swift b/Encrypted Ink/Screens/AccountsListViewController.swift index cad09c2b..e72c2acd 100644 --- a/Encrypted Ink/Screens/AccountsListViewController.swift +++ b/Encrypted Ink/Screens/AccountsListViewController.swift @@ -8,7 +8,8 @@ class AccountsListViewController: NSViewController { private let walletsManager = WalletsManager.shared private var cellModels = [CellModel]() - var onSelectedWallet: ((InkWallet) -> Void)? + private let chainId = 1 + var onSelectedWallet: ((Int, InkWallet) -> Void)? var newWalletId: String? enum CellModel { @@ -258,7 +259,7 @@ extension AccountsListViewController: NSTableViewDelegate { case .wallet: let wallet = wallets[row] if let onSelectedWallet = onSelectedWallet { - onSelectedWallet(wallet) + onSelectedWallet(chainId, wallet) } else { Timer.scheduledTimer(withTimeInterval: 0.01, repeats: false) { [weak self] _ in var point = NSEvent.mouseLocation diff --git a/Encrypted Ink/Screens/ImportViewController.swift b/Encrypted Ink/Screens/ImportViewController.swift index b9b45f97..03edca7c 100644 --- a/Encrypted Ink/Screens/ImportViewController.swift +++ b/Encrypted Ink/Screens/ImportViewController.swift @@ -5,7 +5,7 @@ import Cocoa class ImportViewController: NSViewController { private let walletsManager = WalletsManager.shared - var onSelectedWallet: ((InkWallet) -> Void)? + var onSelectedWallet: ((Int, InkWallet) -> Void)? private var inputValidationResult = WalletsManager.InputValidationResult.invalid @IBOutlet weak var textField: NSTextField! { diff --git a/Encrypted Ink/Services/SessionStorage.swift b/Encrypted Ink/Services/SessionStorage.swift index e65f548b..59dd2b74 100644 --- a/Encrypted Ink/Services/SessionStorage.swift +++ b/Encrypted Ink/Services/SessionStorage.swift @@ -7,6 +7,7 @@ class SessionStorage { struct Item: Codable { let session: WCSession + let chainId: Int? let walletId: String let clientId: String let sessionDetails: WCSessionRequestParam @@ -51,8 +52,8 @@ class SessionStorage { } } - func add(interactor: WCInteractor, walletId: String, sessionDetails: WCSessionRequestParam) { - let item = Item(session: interactor.session, walletId: walletId, clientId: interactor.clientId, sessionDetails: sessionDetails) + func add(interactor: WCInteractor, chainId: Int, walletId: String, sessionDetails: WCSessionRequestParam) { + let item = Item(session: interactor.session, chainId: chainId, walletId: walletId, clientId: interactor.clientId, sessionDetails: sessionDetails) WCSessionStore.store(interactor.session, peerId: sessionDetails.peerId, peerMeta: sessionDetails.peerMeta) Defaults.storedSessions[interactor.clientId] = item didInteractWith(clientId: interactor.clientId) diff --git a/Encrypted Ink/WalletConnect.swift b/Encrypted Ink/WalletConnect.swift index 449c3064..7515c4d5 100644 --- a/Encrypted Ink/WalletConnect.swift +++ b/Encrypted Ink/WalletConnect.swift @@ -28,10 +28,10 @@ class WalletConnect { return WCSession.from(string: link) } - func connect(session: WCSession, walletId: String, uuid: UUID = UUID(), completion: @escaping ((Bool) -> Void)) { + func connect(session: WCSession, chainId: Int, walletId: String, uuid: UUID = UUID(), completion: @escaping ((Bool) -> Void)) { let clientMeta = WCPeerMeta(name: "Encrypted Ink", url: "https://encrypted.ink", description: "Ethereum agent for macOS", icons: ["https://encrypted.ink/icon.png"]) let interactor = WCInteractor(session: session, meta: clientMeta, uuid: uuid) - configure(interactor: interactor, walletId: walletId) + configure(interactor: interactor, chainId: chainId, walletId: walletId) interactor.connect().done { connected in completion(connected) @@ -49,7 +49,7 @@ class WalletConnect { for item in items { guard let uuid = UUID(uuidString: item.clientId) else { continue } - connect(session: item.session, walletId: item.walletId, uuid: uuid) { _ in } + connect(session: item.session, chainId: item.chainId ?? 1, walletId: item.walletId, uuid: uuid) { _ in } peers[item.clientId] = item.sessionDetails.peerMeta } } @@ -87,17 +87,16 @@ class WalletConnect { return peers[id] } - private func configure(interactor: WCInteractor, walletId: String) { + private func configure(interactor: WCInteractor, chainId: Int, walletId: String) { guard let address = walletsManager.getWallet(id: walletId)?.ethereumAddress else { return } let accounts = [address] - let chainId = 1 - + interactor.onError = { _ in } interactor.onSessionRequest = { [weak self, weak interactor] (id, peerParam) in guard let interactor = interactor else { return } self?.peers[interactor.clientId] = peerParam.peerMeta - self?.sessionStorage.add(interactor: interactor, walletId: walletId, sessionDetails: peerParam) + self?.sessionStorage.add(interactor: interactor, chainId: chainId, walletId: walletId, sessionDetails: peerParam) interactor.approveSession(accounts: accounts, chainId: chainId).cauterize() }