Pass custom chain id to WalletConnect

This commit is contained in:
Ivan Grachyov 2021-08-06 18:58:32 +03:00
parent f4c45ca5c3
commit ce010da765
5 changed files with 19 additions and 18 deletions

View File

@ -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()
}

View File

@ -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

View File

@ -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! {

View File

@ -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)

View File

@ -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()
}