Load most recent accounts on launch

pull/2/head
Michael Scoff 7 years ago
parent e65e319090
commit 9167b9b388
  1. 5
      Trust/Coordinator/AppCoordinator.swift
  2. 14
      Trust/EtherClient/EtherKeystore.swift
  3. 1
      Trust/EtherClient/Keystore.swift
  4. 17
      TrustTests/Factories/FakeKeystore.swift

@ -23,7 +23,7 @@ class AppCoordinator: NSObject {
return AccountsCoordinator(navigationController: self.rootNavigationController)
}()
private let keystore: Keystore
private var keystore: Keystore
init(
window: UIWindow,
@ -41,13 +41,14 @@ class AppCoordinator: NSObject {
rootNavigationController.viewControllers = [welcomeViewController]
if keystore.hasAccounts {
showTransactions(for: keystore.accounts.first!)
showTransactions(for: keystore.recentlyUsedAccount ?? keystore.accounts.first!)
}
}
func showTransactions(for account: Account) {
let controller = makeTransactionsController(with: account)
rootNavigationController.viewControllers = [controller]
keystore.recentlyUsedAccount = account
}
func showCreateWallet() {

@ -7,6 +7,10 @@ import KeychainSwift
class EtherKeystore: Keystore {
struct Keys {
static let recentlyUsedAddress: String = "recentlyUsedAddress"
}
private let keychain = KeychainSwift(keyPrefix: "trustwallet")
let datadir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
lazy var ks: GethKeyStore = {
@ -21,6 +25,16 @@ class EtherKeystore: Keystore {
return !accounts.isEmpty
}
var recentlyUsedAccount: Account? {
set {
keychain.set(newValue?.address.address ?? "", forKey: Keys.recentlyUsedAddress)
}
get {
let address = keychain.get(Keys.recentlyUsedAddress)
return accounts.filter { $0.address.address == address }.first
}
}
func createAccout(password: String) -> Account {
let account = try! ks.newAccount(password)
return .from(account: account)

@ -5,4 +5,5 @@ import Foundation
protocol Keystore {
var hasAccounts: Bool { get }
var accounts: [Account] { get }
var recentlyUsedAccount: Account? { get set }
}

@ -7,15 +7,26 @@ struct FakeKeystore: Keystore {
var hasAccounts: Bool {
return accounts.count > 0
}
var accounts: [Account] = []
var accounts: [Account]
var recentlyUsedAccount: Account?
init(
accounts: [Account] = [],
recentlyUsedAccount: Account? = .none
) {
self.accounts = accounts
self.recentlyUsedAccount = recentlyUsedAccount
}
}
extension FakeKeystore {
static func make(
accounts: [Account] = []
accounts: [Account] = [],
recentlyUsedAccount: Account? = .none
) -> FakeKeystore {
return FakeKeystore(
accounts: accounts
accounts: accounts,
recentlyUsedAccount: recentlyUsedAccount
)
}
}

Loading…
Cancel
Save