Skip to content

Commit

Permalink
Implement multi wallet import
Browse files Browse the repository at this point in the history
  • Loading branch information
vikmeup committed Jul 24, 2018
1 parent 61e2606 commit 4217639
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ CHECKOUT OPTIONS:
:commit: f987ee12f98c2df9d1538d86448dd8e671669350
:git: https://github.com/TrustWallet/trust-core
TrustKeystore:
:commit: 30feafc123885a580b9f0b73ea8f6b9bcca4693f
:commit: 3f0b6230803a010f9e9a845ae32b1636f1fde842
:git: https://github.com/TrustWallet/trust-keystore
TrustWalletSDK:
:commit: 99eb1456c6ebf1ce6a2dca15731ff99d69f93eb0
Expand Down
3 changes: 3 additions & 0 deletions Trust.xcodeproj/xcshareddata/xcschemes/Trust.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
<Test
Identifier = "SendViewModelTest">
</Test>
<Test
Identifier = "TokensDataStoreTest">
</Test>
<Test
Identifier = "TypedMessageEncodingTests">
</Test>
Expand Down
9 changes: 2 additions & 7 deletions Trust/EtherClient/EtherKeystore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,10 @@ class EtherKeystore: Keystore {
}

func createAccout(password: String) -> Wallet {
let derivationPaths = Config.current.servers.map { $0.derivationPath(at: 0) }
let wallet = try! keyStore.createWallet(
password: password,
derivationPaths: [
Coin.ethereum.derivationPath(at: 0),
Coin.poa.derivationPath(at: 0),
Coin.callisto.derivationPath(at: 0),
Coin.gochain.derivationPath(at: 0),
Coin.ethereumClassic.derivationPath(at: 0),
]
derivationPaths: derivationPaths
)
let _ = setPassword(password, for: wallet)
return wallet
Expand Down
39 changes: 29 additions & 10 deletions Trust/Wallet/ViewControllers/ImportMainWalletViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ final class ImportMainWalletViewController: FormViewController {
static let password = "password"
}

var mnemonicRow: TextAreaRow? {
private var mnemonicRow: TextAreaRow? {
return form.rowBy(tag: Values.mnemonic)
}
var passwordRow: TextFloatLabelRow? {
private var passwordRow: TextFloatLabelRow? {
return form.rowBy(tag: Values.password)
}
weak var delegate: ImportMainWalletViewControllerDelegate?
Expand Down Expand Up @@ -70,25 +70,44 @@ final class ImportMainWalletViewController: FormViewController {
let validatedError = mnemonicRow?.section?.form?.validate()
guard let errors = validatedError, errors.isEmpty else { return }

let password = passwordRow?.value ?? ""
let password = ""//passwordRow?.value ?? ""
let mnemonicInput = mnemonicRow?.value?.trimmed ?? ""
let words = mnemonicInput.components(separatedBy: " ").map { $0.trimmed.lowercased() }

displayLoading(text: R.string.localizable.importWalletImportingIndicatorLabelTitle(), animated: false)

let importType = ImportType.mnemonic(words: words, password: password, derivationPath: Coin.ethereum.derivationPath(at: 0))

keystore.importWallet(type: importType, coin: .ethereum) { result in
self.hideLoading(animated: false)
switch result {
case .success(let account):
self.didImport(account: account)
case .failure(let error):
self.displayError(error: error)
DispatchQueue.global(qos: .userInitiated).async {
self.keystore.importWallet(type: importType, coin: .ethereum) { result in
switch result {
case .success(let account):
self.addWallets(wallet: account)
DispatchQueue.main.async {
self.hideLoading(animated: false)
self.didImport(account: account)
}
case .failure(let error):
DispatchQueue.main.async {
self.hideLoading(animated: false)
self.displayError(error: error)
}
}
}
}
}

@discardableResult
func addWallets(wallet: WalletInfo) -> Bool {
// Create coins based on supported networks
guard let w = wallet.currentWallet else {
return false
}
let derivationPaths = Config.current.servers.map { $0.derivationPath(at: 0) }
let _ = keystore.addAccount(to: w, derivationPaths: derivationPaths)
return true
}

@objc func openReader() {
let controller = QRCodeReaderViewController()
controller.delegate = self
Expand Down
1 change: 1 addition & 0 deletions TrustTests/EtherClient/EtherKeystoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class EtherKeystoreTests: XCTestCase {
let account = keystore.createAccout(password: password)
let retrivedPassword = keystore.getPassword(for: account)

XCTAssertEqual(5, account.accounts.count)
XCTAssertEqual(password, retrivedPassword)
XCTAssertEqual(1, keystore.wallets.count)
}
Expand Down
12 changes: 6 additions & 6 deletions TrustTests/Tokens/TokensDataStoreTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ class TokensDataStoreTest: XCTestCase {

let coinTickers = FakeCoinTickerFactory.make3UniqueCionTickers()

XCTAssertEqual(1000.00, tokensDataStore.getBalance(for: tokenObject, with: coinTickers))
XCTAssertEqual(1000.00, tokensDataStore.getBalance(for: tokenObject))

XCTAssertEqual(0.00, tokensDataStore.getBalance(for: tokenObject, with: [CoinTicker(price: "", contract: .make())]))
XCTAssertEqual(0.00, tokensDataStore.getBalance(for: tokenObject, with: [CoinTicker]()))
XCTAssertEqual(0.00, tokensDataStore.getBalance(for: tokenObject))
XCTAssertEqual(0.00, tokensDataStore.getBalance(for: tokenObject))

tokenObject = TokenObject(
contract: "0x0000000000000000000000000000000000000002",
Expand All @@ -70,7 +70,7 @@ class TokensDataStoreTest: XCTestCase {
value: "20000"
)

XCTAssertEqual(400.00, tokensDataStore.getBalance(for: tokenObject, with: coinTickers))
XCTAssertEqual(400.00, tokensDataStore.getBalance(for: tokenObject))

tokenObject = TokenObject(
contract: "contract that doesn't match any",
Expand All @@ -80,7 +80,7 @@ class TokensDataStoreTest: XCTestCase {
value: "30000"
)

XCTAssertEqual(0.00, tokensDataStore.getBalance(for: tokenObject, with: coinTickers))
XCTAssertEqual(0.00, tokensDataStore.getBalance(for: tokenObject))
}

// This test checks that even the key generation algorithm changes, coinTicker(for:) still can pick up the correct CoinTicker object without needing to delete the old CoinTicker records since they have old key.
Expand All @@ -94,7 +94,7 @@ class TokensDataStoreTest: XCTestCase {
type: .coin,
symbol: "symbol1", decimals: 18, value: "", isCustom: false, isDisabled: false)

let coinTicker = tokensDataStore.coinTicker(for: token)
let coinTicker = tokensDataStore.coinTicker(by: token.address)

XCTAssertEqual("0x0000000000000000000000000000000000000001_tickers-USD", coinTicker?.key)
}
Expand Down

0 comments on commit 4217639

Please sign in to comment.