Skip to content

Commit

Permalink
Add ENS names resolving / lookup (trustwallet#712)
Browse files Browse the repository at this point in the history
* Add ENSClient to resolve and reverse lookup

* Add ENSManager / ENSStore / ENSRecord

* Fix SettingsCoordinatorTests

* rename ENSManager

* use Data.suffix

* add comment for magic number 20

* save both resolver and address

* show reverse name if available

* Fix address label typo

* shorter address format

* shorter address format (6...4)
  • Loading branch information
hewigovens authored and vikmeup committed May 12, 2018
1 parent 2f65b06 commit e7d367e
Show file tree
Hide file tree
Showing 16 changed files with 308 additions and 32 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ Trust.zip
# VIM
*.swp
*.swo
*~

# node
node_modules
Expand Down
2 changes: 1 addition & 1 deletion Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ target 'Trust' do
pod 'Crashlytics', '~> 3.10'
pod 'Firebase/Core'
pod 'Kingfisher', '~> 4.0'
pod 'TrustCore', '~> 0.0.6'
pod 'TrustCore', '~> 0.0.7'
pod 'TrustKeystore', '~> 0.4.0'
pod 'Branch'
pod 'SAMKeychain'
Expand Down
8 changes: 4 additions & 4 deletions Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ PODS:
- StatefulViewController (3.0)
- SwiftLint (0.25.0)
- TrezorCrypto (0.0.5)
- TrustCore (0.0.6):
- TrustCore (0.0.7):
- BigInt
- TrezorCrypto
- TrustKeystore (0.4.0):
Expand Down Expand Up @@ -109,7 +109,7 @@ DEPENDENCIES:
- SeedStackViewController
- StatefulViewController
- SwiftLint
- TrustCore (~> 0.0.6)
- TrustCore (~> 0.0.7)
- TrustKeystore (~> 0.4.0)
- TrustSDK (from `https://github.com/TrustWallet/TrustSDK-iOS`, branch `master`)
- TrustWeb3Provider (from `https://github.com/TrustWallet/trust-web3-provider`, branch `master`)
Expand Down Expand Up @@ -224,12 +224,12 @@ SPEC CHECKSUMS:
StatefulViewController: 4803bf900d44de26074344998e10e041113b5931
SwiftLint: e14651157288e9e01d6e1a71db7014fb5744a8ea
TrezorCrypto: ecef681446bf02af6248d3b5e394026d7e766e56
TrustCore: 231439190d1941dc4e945c51c55c5effa8e7b9b4
TrustCore: 688df9a241008cf9cfa3842ec8ef28777a8b7534
TrustKeystore: 1bd016c67de7314e84b79dba2c22ef64b86bb63e
TrustSDK: fe6aac90d0ae5ca0be5b6ccd09f0008d1e223f1d
TrustWeb3Provider: 50fa391bdf170feb43dd4419992931510a5751d8
URLNavigator: af5582fbbb3586c958be16835d799bfdb23a4793

PODFILE CHECKSUM: 565e25f77aef23eca1fe772ccf54bb23e5ea553e
PODFILE CHECKSUM: 407574053cad2a6d49a5e6356743e399229e35b6

COCOAPODS: 1.5.0
24 changes: 24 additions & 0 deletions Trust.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -423,12 +423,16 @@
BB03A8712042B01300649DA0 /* BalanceStatusTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB03A8702042B01300649DA0 /* BalanceStatusTests.swift */; };
BB1CC266203BA4440042E56C /* SendMaxAmountTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB1CC265203BA4440042E56C /* SendMaxAmountTest.swift */; };
BB1CC27A203D061B0042E56C /* TrustDocumentPickerViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB1CC279203D061B0042E56C /* TrustDocumentPickerViewController.swift */; };
BB3395E9209C4F8100ACED2C /* ENSClient.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB3395E8209C4F8100ACED2C /* ENSClient.swift */; };
BB3864A3208F1C8A00CBF773 /* testIsHexEncoded.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB3864A2208F1C8A00CBF773 /* testIsHexEncoded.swift */; };
BB3864A4208F1C9C00CBF773 /* String.swift in Sources */ = {isa = PBXBuildFile; fileRef = 29A0E1841F706B8C00BAAAED /* String.swift */; };
BB3864A5208F1D2600CBF773 /* Data.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2963B6C01F9AE0E4003063C1 /* Data.swift */; };
BB3B86BD204CE9730061FCBA /* URLNavigatorCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB3B86BC204CE9730061FCBA /* URLNavigatorCoordinator.swift */; };
BB3B86BF204CECBC0061FCBA /* URLNavigable.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB3B86BE204CECBC0061FCBA /* URLNavigable.swift */; };
BB3B86C5204D2EF10061FCBA /* InCoordinator+Navigable.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB3B86C4204D2EF10061FCBA /* InCoordinator+Navigable.swift */; };
BB3E2A0B20A03D22000F8B76 /* ENSStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB3E2A0A20A03D22000F8B76 /* ENSStore.swift */; };
BB3E2A0E20A04F4B000F8B76 /* ENSRecord.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB3E2A0D20A04F4B000F8B76 /* ENSRecord.swift */; };
BB3E2A1020A12592000F8B76 /* ENSManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB3E2A0F20A12592000F8B76 /* ENSManager.swift */; };
BB58B92D206F70E800A70A3C /* HistoryStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB58B92C206F70E800A70A3C /* HistoryStore.swift */; };
BB58B92F206F70FE00A70A3C /* History.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB58B92E206F70FE00A70A3C /* History.swift */; };
BB5D6A9E20232EE8000FC5AB /* CurrencyRate+Fee.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB5D6A9D20232EE8000FC5AB /* CurrencyRate+Fee.swift */; };
Expand Down Expand Up @@ -901,10 +905,14 @@
BB03A8702042B01300649DA0 /* BalanceStatusTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BalanceStatusTests.swift; sourceTree = "<group>"; };
BB1CC265203BA4440042E56C /* SendMaxAmountTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SendMaxAmountTest.swift; sourceTree = "<group>"; };
BB1CC279203D061B0042E56C /* TrustDocumentPickerViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TrustDocumentPickerViewController.swift; sourceTree = "<group>"; };
BB3395E8209C4F8100ACED2C /* ENSClient.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ENSClient.swift; sourceTree = "<group>"; };
BB3864A2208F1C8A00CBF773 /* testIsHexEncoded.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = testIsHexEncoded.swift; sourceTree = "<group>"; };
BB3B86BC204CE9730061FCBA /* URLNavigatorCoordinator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = URLNavigatorCoordinator.swift; sourceTree = "<group>"; };
BB3B86BE204CECBC0061FCBA /* URLNavigable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = URLNavigable.swift; sourceTree = "<group>"; };
BB3B86C4204D2EF10061FCBA /* InCoordinator+Navigable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "InCoordinator+Navigable.swift"; sourceTree = "<group>"; };
BB3E2A0A20A03D22000F8B76 /* ENSStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ENSStore.swift; sourceTree = "<group>"; };
BB3E2A0D20A04F4B000F8B76 /* ENSRecord.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ENSRecord.swift; sourceTree = "<group>"; };
BB3E2A0F20A12592000F8B76 /* ENSManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ENSManager.swift; sourceTree = "<group>"; };
BB58B92C206F70E800A70A3C /* HistoryStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HistoryStore.swift; sourceTree = "<group>"; };
BB58B92E206F70FE00A70A3C /* History.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = History.swift; sourceTree = "<group>"; };
BB5D6A9D20232EE8000FC5AB /* CurrencyRate+Fee.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "CurrencyRate+Fee.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1171,6 +1179,7 @@
2923D9B41FDA4E07000CF3F8 /* PasswordGenerator.swift */,
61C359DF2002AA590097B04D /* TransactionSigning.swift */,
29F1C862200375D2003780D8 /* Wallet.swift */,
BB3E2A0C20A04F2C000F8B76 /* ENS */,
);
path = EtherClient;
sourceTree = "<group>";
Expand Down Expand Up @@ -2433,6 +2442,17 @@
path = Extensions;
sourceTree = "<group>";
};
BB3E2A0C20A04F2C000F8B76 /* ENS */ = {
isa = PBXGroup;
children = (
BB3395E8209C4F8100ACED2C /* ENSClient.swift */,
BB3E2A0A20A03D22000F8B76 /* ENSStore.swift */,
BB3E2A0D20A04F4B000F8B76 /* ENSRecord.swift */,
BB3E2A0F20A12592000F8B76 /* ENSManager.swift */,
);
path = ENS;
sourceTree = "<group>";
};
BB767E2C205E102700263658 /* Utils */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -2874,6 +2894,7 @@
A2E43B4E20802879001FAD3D /* UINavigationController.swift in Sources */,
2963B6B11F9891F5003063C1 /* UIButton.swift in Sources */,
29F114F21FA7966300114A29 /* PrivateKeyRule.swift in Sources */,
BB3E2A0E20A04F4B000F8B76 /* ENSRecord.swift in Sources */,
77E81D962036883B00534C99 /* NotificationsViewModel.swift in Sources */,
7778713E2086F93700CF7303 /* Colors.swift in Sources */,
296106C41F7640C50006164B /* TokenViewCellViewModel.swift in Sources */,
Expand All @@ -2898,6 +2919,7 @@
BB767E2E205E10FD00263658 /* Debounce.swift in Sources */,
77872D232023F43B0032D687 /* TransactionsTracker.swift in Sources */,
776C1147205CF6DF00449539 /* TokenHeaderView.swift in Sources */,
BB3E2A0B20A03D22000F8B76 /* ENSStore.swift in Sources */,
29FA00CE201CA64E002F7DC5 /* DappCommand.swift in Sources */,
29FF12F61F74799D00AFD326 /* NSAttributedString.swift in Sources */,
290B2B651F91A4880053C83E /* TransactionsFooterView.swift in Sources */,
Expand Down Expand Up @@ -3157,6 +3179,8 @@
29BB94951F6FC54C009B09CC /* EthereumUnit.swift in Sources */,
29C70C712016C7780072E454 /* SentTransaction.swift in Sources */,
29E9CFD21FE737FE00017744 /* TrustRealmConfiguration.swift in Sources */,
BB3395E9209C4F8100ACED2C /* ENSClient.swift in Sources */,
BB3E2A1020A12592000F8B76 /* ENSManager.swift in Sources */,
2959961C1FAE3EDF00DB66A8 /* TrustClient.swift in Sources */,
29AD8A0C1F93FBBF008E10E7 /* Subscribable.swift in Sources */,
777871382085D7A600CF7303 /* BranchEvent.swift in Sources */,
Expand Down
7 changes: 5 additions & 2 deletions Trust/Accounts/Coordinators/AccountsCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ class AccountsCoordinator: Coordinator {
let keystore: Keystore
let session: WalletSession
let balanceCoordinator: TokensBalanceService
let ensManager: ENSManager
var coordinators: [Coordinator] = []

lazy var accountsViewController: AccountsViewController = {
let controller = AccountsViewController(keystore: keystore, balanceCoordinator: balanceCoordinator)
let controller = AccountsViewController(keystore: keystore, balanceCoordinator: balanceCoordinator, ensManager: ensManager)
controller.navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(add))
controller.delegate = self
return controller
Expand All @@ -33,13 +34,15 @@ class AccountsCoordinator: Coordinator {
navigationController: NavigationController,
keystore: Keystore,
session: WalletSession,
balanceCoordinator: TokensBalanceService
balanceCoordinator: TokensBalanceService,
ensManager: ENSManager
) {
self.navigationController = navigationController
self.navigationController.modalPresentationStyle = .formSheet
self.keystore = keystore
self.session = session
self.balanceCoordinator = balanceCoordinator
self.ensManager = ensManager
}

func start() {
Expand Down
11 changes: 9 additions & 2 deletions Trust/Accounts/ViewModels/AccountViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,20 @@ struct AccountViewModel {
let current: Wallet?
let walletBalance: Balance?
let server: RPCServer
let ensName: String

init(
server: RPCServer,
wallet: Wallet,
current: Wallet?,
walletBalance: Balance?
walletBalance: Balance?,
ensName: String = ""
) {
self.server = server
self.wallet = wallet
self.current = current
self.walletBalance = walletBalance
self.ensName = ensName
}

var isWatch: Bool {
Expand All @@ -34,7 +37,11 @@ struct AccountViewModel {
}

var title: String {
return wallet.address.description
let address = wallet.address.description
if ensName.isEmpty {
return address
}
return String(format: "%@ (%@...%@)", ensName, String(address.prefix(6)), String(address.suffix(4)))
}

var isActive: Bool {
Expand Down
24 changes: 22 additions & 2 deletions Trust/Accounts/ViewModels/AccountsViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import TrustCore
import UIKit
import PromiseKit

protocol AccountsViewControllerDelegate: class {
func didSelectAccount(account: Wallet, in viewController: AccountsViewController)
Expand All @@ -10,6 +11,7 @@ protocol AccountsViewControllerDelegate: class {
}

class AccountsViewController: UITableViewController {
let ensManager: ENSManager
weak var delegate: AccountsViewControllerDelegate?
var headerTitle: String?
var viewModel: AccountsViewModel {
Expand All @@ -27,16 +29,19 @@ class AccountsViewController: UITableViewController {
}
}
private var balances: [Address: Balance?] = [:]
private var addrNames: [Address: String] = [:]
private let keystore: Keystore
private let balanceCoordinator: TokensBalanceService
private let config = Config()

init(
keystore: Keystore,
balanceCoordinator: TokensBalanceService
balanceCoordinator: TokensBalanceService,
ensManager: ENSManager
) {
self.keystore = keystore
self.balanceCoordinator = balanceCoordinator
self.ensManager = ensManager
super.init(style: .grouped)
fetch()
}
Expand All @@ -52,6 +57,7 @@ class AccountsViewController: UITableViewController {
super.viewWillAppear(animated)
fetch()
refreshWalletBalances()
refreshENSNames()
}

func fetch() {
Expand Down Expand Up @@ -153,10 +159,24 @@ class AccountsViewController: UITableViewController {
}
}

private func refreshENSNames() {
let addresses = wallets.compactMap { $0.address }
let promises = addresses.map { ensManager.lookup(address: $0) }
_ = when(fulfilled: promises).done { [weak self] names in
for (index, name) in names.enumerated() {
self?.addrNames[addresses[index]] = name
}
self?.tableView.reloadData()
}.catch { error in
print(error)
}
}

private func getAccountViewModels(for path: IndexPath) -> AccountViewModel {
let account = self.wallet(for: path)! // Avoid force unwrap
let balance = self.balances[account.address].flatMap { $0 }
let model = AccountViewModel(server: config.server, wallet: account, current: EtherKeystore.current, walletBalance: balance)
let ensName = self.addrNames[account.address] ?? ""
let model = AccountViewModel(server: config.server, wallet: account, current: EtherKeystore.current, walletBalance: balance, ensName: ensName)
return model
}

Expand Down
8 changes: 4 additions & 4 deletions Trust/Accounts/Views/AccountViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ class AccountViewCell: UITableViewCell {
@IBOutlet weak var activeView: UIView!
@IBOutlet weak var glassesImageView: UIImageView!
@IBOutlet weak var walletTypeImageView: UIImageView!
@IBOutlet weak var addressLable: UILabel!
@IBOutlet weak var balanceLable: UILabel!
@IBOutlet weak var balanceLabel: UILabel!
@IBOutlet weak var addressLabel: UILabel!
@IBOutlet weak var identiconImageView: UIImageView!
weak var delegate: AccountViewCellDelegate?
var viewModel: AccountViewModel? {
didSet {
guard let model = viewModel else {
return
}
balanceLable.text = model.balanceText
balanceLabel.text = model.balanceText
glassesImageView.isHidden = !model.isWatch
activeView.isHidden = !model.isActive
addressLable.text = model.title
addressLabel.text = model.title
infoButton.tintColor = Colors.lightBlue
identiconImageView.image = model.identicon
}
Expand Down
Loading

0 comments on commit e7d367e

Please sign in to comment.