diff --git a/Shared/Extension/String.swift b/Shared/Extension/String.swift index defb63e4..b8eaebc7 100644 --- a/Shared/Extension/String.swift +++ b/Shared/Extension/String.swift @@ -21,10 +21,4 @@ extension String { return self + "…" } - var trimmedAddress: String { - guard !isEmpty else { return self } - let without0x = dropFirst(2) - return without0x.prefix(4) + "..." + without0x.suffix(4) - } - } diff --git a/Shared/Models/DappRequestAction.swift b/Shared/Models/DappRequestAction.swift index 7f2d03bd..aafd7ab9 100644 --- a/Shared/Models/DappRequestAction.swift +++ b/Shared/Models/DappRequestAction.swift @@ -19,7 +19,7 @@ struct SelectAccountAction { struct SignMessageAction { let provider: Web3Provider let subject: ApprovalSubject - let address: String + let account: Account let meta: String let peerMeta: PeerMeta let completion: (Bool) -> Void @@ -29,7 +29,7 @@ struct SendTransactionAction { let provider: Web3Provider let transaction: Transaction let chain: EthereumChain - let address: String + let account: Account let peerMeta: PeerMeta let completion: (Transaction?) -> Void } diff --git a/Shared/Services/DappRequestProcessor.swift b/Shared/Services/DappRequestProcessor.swift index 49eff907..181fac0d 100644 --- a/Shared/Services/DappRequestProcessor.swift +++ b/Shared/Services/DappRequestProcessor.swift @@ -50,6 +50,10 @@ struct DappRequestProcessor { private static func process(request: SafariRequest, solanaRequest body: SafariRequest.Solana, completion: @escaping () -> Void) -> DappRequestAction { let peerMeta = PeerMeta(title: request.host, iconURLString: request.favicon) + func getAccount() -> Account? { + return walletsManager.wallets.flatMap { $0.accounts }.first(where: { $0.address == body.publicKey }) + } + func getPrivateKey() -> WalletCore.PrivateKey? { guard let password = Keychain.shared.password else { return nil } for wallet in walletsManager.wallets { @@ -72,12 +76,12 @@ struct DappRequestProcessor { } return .selectAccount(action) case .signAllTransactions: - guard let messages = body.messages else { + guard let messages = body.messages, let account = getAccount() else { respond(to: request, error: Strings.somethingWentWrong, completion: completion) return .none } let displayMessage = messages.joined(separator: "\n\n") - let action = SignMessageAction(provider: request.provider, subject: .approveTransaction, address: body.publicKey, meta: displayMessage, peerMeta: peerMeta) { approved in + let action = SignMessageAction(provider: request.provider, subject: .approveTransaction, account: account, meta: displayMessage, peerMeta: peerMeta) { approved in if approved, let privateKey = getPrivateKey() { var results = [String]() for message in messages { @@ -95,7 +99,7 @@ struct DappRequestProcessor { } return .approveMessage(action) case .signMessage, .signTransaction, .signAndSendTransaction: - guard let message = body.message else { + guard let message = body.message, let account = getAccount() else { respond(to: request, error: Strings.somethingWentWrong, completion: completion) return .none } @@ -109,7 +113,7 @@ struct DappRequestProcessor { displayMessage = message subject = .approveTransaction } - let action = SignMessageAction(provider: request.provider, subject: subject, address: body.publicKey, meta: displayMessage, peerMeta: peerMeta) { approved in + let action = SignMessageAction(provider: request.provider, subject: subject, account: account, meta: displayMessage, peerMeta: peerMeta) { approved in guard approved, let privateKey = getPrivateKey() else { respond(to: request, error: Strings.failedToSign, completion: completion) return @@ -139,6 +143,10 @@ struct DappRequestProcessor { private static func process(request: SafariRequest, ethereumRequest: SafariRequest.Ethereum, completion: @escaping () -> Void) -> DappRequestAction { let peerMeta = PeerMeta(title: request.host, iconURLString: request.favicon) + func getAccount() -> Account? { + return walletsManager.wallets.flatMap { $0.accounts }.first(where: { $0.address.lowercased() == ethereumRequest.address.lowercased() }) + } + switch ethereumRequest.method { case .switchAccount, .requestAccounts: let action = SelectAccountAction(provider: .ethereum) { chain, wallet, account in @@ -153,8 +161,8 @@ struct DappRequestProcessor { case .signTypedMessage: if let raw = ethereumRequest.raw, let wallet = walletsManager.getWallet(address: ethereumRequest.address), - let address = wallet.ethereumAddress { - let action = SignMessageAction(provider: request.provider, subject: .signTypedData, address: address, meta: raw, peerMeta: peerMeta) { approved in + let account = getAccount() { + let action = SignMessageAction(provider: request.provider, subject: .signTypedData, account: account, meta: raw, peerMeta: peerMeta) { approved in if approved { signTypedData(wallet: wallet, raw: raw, request: request, completion: completion) } else { @@ -168,8 +176,8 @@ struct DappRequestProcessor { case .signMessage: if let data = ethereumRequest.message, let wallet = walletsManager.getWallet(address: ethereumRequest.address), - let address = wallet.ethereumAddress { - let action = SignMessageAction(provider: request.provider, subject: .signMessage, address: address, meta: data.hexString, peerMeta: peerMeta) { approved in + let account = getAccount() { + let action = SignMessageAction(provider: request.provider, subject: .signMessage, account: account, meta: data.hexString, peerMeta: peerMeta) { approved in if approved { signMessage(wallet: wallet, data: data, request: request, completion: completion) } else { @@ -183,9 +191,9 @@ struct DappRequestProcessor { case .signPersonalMessage: if let data = ethereumRequest.message, let wallet = walletsManager.getWallet(address: ethereumRequest.address), - let address = wallet.ethereumAddress { + let account = getAccount() { let text = String(data: data, encoding: .utf8) ?? data.hexString - let action = SignMessageAction(provider: request.provider, subject: .signPersonalMessage, address: address, meta: text, peerMeta: peerMeta) { approved in + let action = SignMessageAction(provider: request.provider, subject: .signPersonalMessage, account: account, meta: text, peerMeta: peerMeta) { approved in if approved { signPersonalMessage(wallet: wallet, data: data, request: request, completion: completion) } else { @@ -200,11 +208,11 @@ struct DappRequestProcessor { if let transaction = ethereumRequest.transaction, let chain = ethereumRequest.chain, let wallet = walletsManager.getWallet(address: ethereumRequest.address), - let address = wallet.ethereumAddress { + let account = getAccount() { let action = SendTransactionAction(provider: request.provider, transaction: transaction, chain: chain, - address: address, + account: account, peerMeta: peerMeta) { transaction in if let transaction = transaction { sendTransaction(wallet: wallet, transaction: transaction, chain: chain, request: request, completion: completion) diff --git a/Tokenary iOS/Screens/Accounts/AccountsListViewController.swift b/Tokenary iOS/Screens/Accounts/AccountsListViewController.swift index 6b65ac95..7258769a 100644 --- a/Tokenary iOS/Screens/Accounts/AccountsListViewController.swift +++ b/Tokenary iOS/Screens/Accounts/AccountsListViewController.swift @@ -154,7 +154,7 @@ class AccountsListViewController: UIViewController, DataStateContainer { presentForSafariRequest(selectAccountViewController.inNavigationController, id: request.id) case .approveMessage(let action): let approveViewController = ApproveViewController.with(subject: action.subject, - address: action.address, + account: action.account, meta: action.meta, peerMeta: action.peerMeta, completion: action.completion) @@ -162,7 +162,7 @@ class AccountsListViewController: UIViewController, DataStateContainer { case .approveTransaction(let action): let approveTransactionViewController = ApproveTransactionViewController.with(transaction: action.transaction, chain: action.chain, - address: action.address, + account: action.account, peerMeta: action.peerMeta, completion: action.completion) presentForSafariRequest(approveTransactionViewController.inNavigationController, id: request.id) diff --git a/Tokenary iOS/Screens/Approve/ApproveTransactionViewController.swift b/Tokenary iOS/Screens/Approve/ApproveTransactionViewController.swift index c76a1471..4a4a240e 100644 --- a/Tokenary iOS/Screens/Approve/ApproveTransactionViewController.swift +++ b/Tokenary iOS/Screens/Approve/ApproveTransactionViewController.swift @@ -2,6 +2,7 @@ import UIKit import BlockiesSwift +import WalletCore class ApproveTransactionViewController: UIViewController { @@ -29,7 +30,7 @@ class ApproveTransactionViewController: UIViewController { private var sectionModels = [[CellModel]]() private var didEnableSpeedConfiguration = false - private var address: String! + private var account: Account! private var transaction: Transaction! private var chain: EthereumChain! private var completion: ((Transaction?) -> Void)! @@ -38,12 +39,12 @@ class ApproveTransactionViewController: UIViewController { @IBOutlet weak var okButton: UIButton! @IBOutlet weak var cancelButton: UIButton! - static func with(transaction: Transaction, chain: EthereumChain, address: String, peerMeta: PeerMeta?, completion: @escaping (Transaction?) -> Void) -> ApproveTransactionViewController { + static func with(transaction: Transaction, chain: EthereumChain, account: Account, peerMeta: PeerMeta?, completion: @escaping (Transaction?) -> Void) -> ApproveTransactionViewController { let new = instantiate(ApproveTransactionViewController.self, from: .main) new.transaction = transaction new.chain = chain new.completion = completion - new.address = address + new.account = account new.peerMeta = peerMeta return new } @@ -78,7 +79,7 @@ class ApproveTransactionViewController: UIViewController { private func updateDisplayedTransactionInfo(initially: Bool) { var cellModels: [CellModel] = [ .textWithImage(text: peerMeta?.name ?? Strings.unknownWebsite, imageURL: peerMeta?.iconURLString, image: nil), - .textWithImage(text: address.trimmedAddress, imageURL: nil, image: Blockies(seed: address.lowercased()).createImage()) + .textWithImage(text: account.croppedAddress, imageURL: nil, image: account.image) ] if let value = transaction.valueWithSymbol(chain: chain, ethPrice: priceService.currentPrice, withLabel: true) { diff --git a/Tokenary iOS/Screens/Approve/ApproveViewController.swift b/Tokenary iOS/Screens/Approve/ApproveViewController.swift index 6d981132..ece1e675 100644 --- a/Tokenary iOS/Screens/Approve/ApproveViewController.swift +++ b/Tokenary iOS/Screens/Approve/ApproveViewController.swift @@ -2,6 +2,7 @@ import UIKit import BlockiesSwift +import WalletCore class ApproveViewController: UIViewController { @@ -22,17 +23,17 @@ class ApproveViewController: UIViewController { private var cellModels = [CellModel]() private var approveTitle: String! - private var address: String! + private var account: Account! private var meta: String! private var completion: ((Bool) -> Void)! private var peerMeta: PeerMeta? @IBOutlet weak var okButton: UIButton! - static func with(subject: ApprovalSubject, address: String, meta: String, peerMeta: PeerMeta?, completion: @escaping (Bool) -> Void) -> ApproveViewController { + static func with(subject: ApprovalSubject, account: Account, meta: String, peerMeta: PeerMeta?, completion: @escaping (Bool) -> Void) -> ApproveViewController { let new = instantiate(ApproveViewController.self, from: .main) new.completion = completion - new.address = address + new.account = account new.meta = meta new.approveTitle = subject.title new.peerMeta = peerMeta @@ -46,7 +47,7 @@ class ApproveViewController: UIViewController { navigationItem.largeTitleDisplayMode = .always isModalInPresentation = true cellModels = [.textWithImage(text: peerMeta?.name ?? Strings.unknownWebsite, imageURL: peerMeta?.iconURLString, image: nil), - .textWithImage(text: address.trimmedAddress, imageURL: nil, image: Blockies(seed: address.lowercased()).createImage()), + .textWithImage(text: account.croppedAddress, imageURL: nil, image: account.image), .text(meta)] }