Explicitly connect accounts on button click

This commit is contained in:
Ivan Grachev 2022-08-17 16:18:48 +03:00
parent eeed9acd55
commit 3ad698c842
5 changed files with 34 additions and 32 deletions

View File

@ -5,5 +5,5 @@ import WalletCore
struct AccountSelectionConfiguration {
let peer: PeerMeta?
let completion: ((EthereumChain?, String?, Account?) -> Void)
let completion: ((EthereumChain?, SpecificWalletAccount?) -> Void)
}

View File

@ -14,7 +14,7 @@ enum DappRequestAction {
struct SelectAccountAction {
let provider: Web3Provider
let completion: (EthereumChain?, String?, Account?) -> Void
let completion: (EthereumChain?, SpecificWalletAccount?) -> Void
}
struct SignMessageAction {

View File

@ -32,8 +32,9 @@ struct DappRequestProcessor {
ExtensionBridge.respond(response: ResponseToExtension(for: request))
return .justShowApp
case .switchAccount:
let action = SelectAccountAction(provider: .unknown) { chain, _, account in
if let chain = chain, let account = account {
let action = SelectAccountAction(provider: .unknown) { chain, specificWalletAccount in
if let chain = chain, let specificWalletAccount = specificWalletAccount {
let account = specificWalletAccount.account
switch account.coin {
case .ethereum:
let responseBody = ResponseToExtension.Ethereum(results: [account.address], chainId: chain.hexStringId, rpcURL: chain.nodeURLString)
@ -63,9 +64,9 @@ struct DappRequestProcessor {
switch body.method {
case .signIn:
let action = SelectAccountAction(provider: .near) { _, _, account in
if let account = account, account.coin == .near {
let responseBody = ResponseToExtension.Near(account: account.address)
let action = SelectAccountAction(provider: .near) { _, specificWalletAccount in
if let specificWalletAccount = specificWalletAccount, specificWalletAccount.account.coin == .near {
let responseBody = ResponseToExtension.Near(account: specificWalletAccount.account.address)
respond(to: request, body: .near(responseBody), completion: completion)
} else {
respond(to: request, error: Strings.canceled, completion: completion)
@ -114,9 +115,9 @@ struct DappRequestProcessor {
switch body.method {
case .connect:
let action = SelectAccountAction(provider: .solana) { _, _, account in
if let account = account, account.coin == .solana {
let responseBody = ResponseToExtension.Solana(publicKey: account.address)
let action = SelectAccountAction(provider: .solana) { _, specificWalletAccount in
if let specificWalletAccount = specificWalletAccount, specificWalletAccount.account.coin == .solana {
let responseBody = ResponseToExtension.Solana(publicKey: specificWalletAccount.account.address)
respond(to: request, body: .solana(responseBody), completion: completion)
} else {
respond(to: request, error: Strings.canceled, completion: completion)
@ -194,9 +195,9 @@ struct DappRequestProcessor {
switch ethereumRequest.method {
case .requestAccounts:
let action = SelectAccountAction(provider: .ethereum) { chain, _, account in
if let chain = chain, let account = account, account.coin == .ethereum {
let responseBody = ResponseToExtension.Ethereum(results: [account.address], chainId: chain.hexStringId, rpcURL: chain.nodeURLString)
let action = SelectAccountAction(provider: .ethereum) { chain, specificWalletAccount in
if let chain = chain, let specificWalletAccount = specificWalletAccount, specificWalletAccount.account.coin == .ethereum {
let responseBody = ResponseToExtension.Ethereum(results: [specificWalletAccount.account.address], chainId: chain.hexStringId, rpcURL: chain.nodeURLString)
respond(to: request, body: .ethereum(responseBody), completion: completion)
} else {
respond(to: request, error: Strings.canceled, completion: completion)

View File

@ -116,7 +116,7 @@ class Agent: NSObject {
windowController.contentViewController = approveViewController
}
func getWalletSelectionCompletionIfShouldSelect() -> ((EthereumChain?, String?, Account?) -> Void)? {
func getWalletSelectionCompletionIfShouldSelect() -> ((EthereumChain?, SpecificWalletAccount?) -> Void)? {
let session = getSessionFromPasteboard()
return onSelectedWallet(session: session)
}
@ -215,14 +215,14 @@ class Agent: NSObject {
}
}
private func onSelectedWallet(session: WCSession?) -> ((EthereumChain?, String?, Account?) -> Void)? {
private func onSelectedWallet(session: WCSession?) -> ((EthereumChain?, SpecificWalletAccount?) -> Void)? {
guard let session = session else { return nil }
return { [weak self] chain, walletId, account in
guard let chain = chain, let walletId = walletId, account?.coin == .ethereum else {
return { [weak self] chain, specificWalletAccount in
guard let chain = chain, let specificWalletAccount = specificWalletAccount, specificWalletAccount.account.coin == .ethereum else {
Window.closeAllAndActivateBrowser(specific: nil)
return
}
self?.connectWallet(session: session, chainId: chain.id, walletId: walletId)
self?.connectWallet(session: session, chainId: chain.id, walletId: specificWalletAccount.walletId)
}
}

View File

@ -9,7 +9,7 @@ class AccountsListViewController: NSViewController {
private let walletsManager = WalletsManager.shared
private var cellModels = [CellModel]()
private var selectedAccounts = Set([Account]()) // TODO: place inside account selection configuration
private var selectedAccounts = Set([SpecificWalletAccount]()) // TODO: place inside account selection configuration
private var chain = EthereumChain.ethereum
private var didCallCompletion = false
@ -101,10 +101,10 @@ class AccountsListViewController: NSViewController {
Alert.showSafariPrompt()
}
private func callCompletion(walletId: String?, account: Account?) {
private func callCompletion(specificWalletAccount: SpecificWalletAccount?) {
if !didCallCompletion {
didCallCompletion = true
accountSelectionConfiguration?.completion(chain, walletId, account)
accountSelectionConfiguration?.completion(chain, specificWalletAccount)
}
}
@ -197,17 +197,15 @@ class AccountsListViewController: NSViewController {
}
@IBAction func didClickSecondaryButton(_ sender: Any) {
callCompletion(walletId: nil, account: nil)
callCompletion(specificWalletAccount: nil)
// TODO: distinguish cancel and disconnect
// when it was dapp's request, we should deliver response anyway
// when it was extension action, no need to deliver response to inpage
}
@IBAction func didClickPrimaryButton(_ sender: Any) {
// TODO: call completion
// callCompletion(walletId: wallet, account: account)
// woah. what do i do about the fact we do not have a wallet here?
// maybe wallet is not necessary at all and i should remove it from here?
callCompletion(specificWalletAccount: selectedAccounts.first)
// TODO: respond with all selected accounts, to all providers
}
@objc private func didSelectChain(_ sender: AnyObject) {
@ -554,12 +552,13 @@ extension AccountsListViewController: NSTableViewDelegate {
}
if accountSelectionConfiguration != nil {
let wasSelected = selectedAccounts.contains(account)
let specificWalletAccount = SpecificWalletAccount(walletId: wallet.id, account: account)
let wasSelected = selectedAccounts.contains(specificWalletAccount)
(tableView.rowView(atRow: row, makeIfNecessary: false) as? AccountCellView)?.setSelected(!wasSelected)
if wasSelected {
selectedAccounts.remove(account)
selectedAccounts.remove(specificWalletAccount)
} else {
selectedAccounts.insert(account)
selectedAccounts.insert(specificWalletAccount)
}
// TODO: select only one account for each network
return false
@ -580,13 +579,15 @@ extension AccountsListViewController: NSTableViewDataSource {
let wallet = wallets[walletIndex]
let rowView = tableView.makeViewOfType(AccountCellView.self, owner: self)
let account = wallet.accounts[0]
rowView.setup(account: account, isSelected: selectedAccounts.contains(account))
let specificWalletAccount = SpecificWalletAccount(walletId: wallet.id, account: account)
rowView.setup(account: account, isSelected: selectedAccounts.contains(specificWalletAccount))
return rowView
case let .mnemonicAccount(walletIndex: walletIndex, accountIndex: accountIndex):
let wallet = wallets[walletIndex]
let rowView = tableView.makeViewOfType(AccountCellView.self, owner: self)
let account = wallet.accounts[accountIndex]
rowView.setup(account: account, isSelected: selectedAccounts.contains(account))
let specificWalletAccount = SpecificWalletAccount(walletId: wallet.id, account: account)
rowView.setup(account: account, isSelected: selectedAccounts.contains(specificWalletAccount))
return rowView
case .mnemonicWalletHeader:
let rowView = tableView.makeViewOfType(AccountsHeaderRowView.self, owner: self)
@ -636,7 +637,7 @@ extension AccountsListViewController: NSMenuDelegate {
extension AccountsListViewController: NSWindowDelegate {
func windowWillClose(_ notification: Notification) {
callCompletion(walletId: nil, account: nil)
callCompletion(specificWalletAccount: nil)
// TODO: distinguish cancel and disconnect
// when it was dapp's request, we should deliver response anyway
// when it was extension action, no need to deliver response to inpage