Skip to content

Commit

Permalink
Add import main wallet controller
Browse files Browse the repository at this point in the history
  • Loading branch information
vikmeup committed Jul 16, 2018
1 parent 889bef3 commit 2688aae
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 31 deletions.
4 changes: 2 additions & 2 deletions Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,10 @@ CHECKOUT OPTIONS:
:commit: 80bd79cbeede842949b229f81bc6328f53701c5a
:git: https://github.com/yannickl/QRCodeReaderViewController.git
TrustCore:
:commit: 3c07727a0b7a521d9a0e90fa45cda474beaa212e
:commit: 4caee499a817638ce666b0c4448734cf933912f7
:git: https://github.com/TrustWallet/trust-core
TrustKeystore:
:commit: a0676ab97ad9906b990794219a7ae67170ce03a1
:commit: 76fabc60612316a8a44f99673d8052c188cfd8c8
:git: https://github.com/TrustWallet/trust-keystore
TrustWalletSDK:
:commit: 87242dc2fcc8cb62601e6ee5d1eba5608e0fec94
Expand Down
4 changes: 4 additions & 0 deletions Trust.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@
73ED85A72034BFEF00593BF3 /* UITextFieldAdditions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 73ED85A62034BFEF00593BF3 /* UITextFieldAdditions.swift */; };
73ED85A92034C42D00593BF3 /* StringFormatterTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 73ED85A82034C42D00593BF3 /* StringFormatterTest.swift */; };
73F2FF3C20557C020021976E /* TrustOperation.swift in Sources */ = {isa = PBXBuildFile; fileRef = 73F2FF3B20557C020021976E /* TrustOperation.swift */; };
7712437620FD45440097296E /* ImportMainWalletViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7712437520FD45440097296E /* ImportMainWalletViewController.swift */; };
7716EDC320DFB86100773010 /* VerifyPassphraseViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7716EDC220DFB86100773010 /* VerifyPassphraseViewController.swift */; };
771A8471202F067D00528D28 /* NetworksViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 771A8470202F067D00528D28 /* NetworksViewController.swift */; };
771A847320322F2500528D28 /* BrowserConfigurationViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 771A847220322F2500528D28 /* BrowserConfigurationViewController.swift */; };
Expand Down Expand Up @@ -820,6 +821,7 @@
73ED85A62034BFEF00593BF3 /* UITextFieldAdditions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UITextFieldAdditions.swift; sourceTree = "<group>"; };
73ED85A82034C42D00593BF3 /* StringFormatterTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StringFormatterTest.swift; sourceTree = "<group>"; };
73F2FF3B20557C020021976E /* TrustOperation.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TrustOperation.swift; sourceTree = "<group>"; };
7712437520FD45440097296E /* ImportMainWalletViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImportMainWalletViewController.swift; sourceTree = "<group>"; };
7716EDC220DFB86100773010 /* VerifyPassphraseViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VerifyPassphraseViewController.swift; sourceTree = "<group>"; };
771A8470202F067D00528D28 /* NetworksViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworksViewController.swift; sourceTree = "<group>"; };
771A847220322F2500528D28 /* BrowserConfigurationViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BrowserConfigurationViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1473,6 +1475,7 @@
77EAD88320E1B82300D14AA2 /* WalletInfoViewController.swift */,
29AD3D4620EC443D007893C9 /* WalletCreatedController.swift */,
29AD3D5020F5B9F9007893C9 /* WalletsViewController.swift */,
7712437520FD45440097296E /* ImportMainWalletViewController.swift */,
);
path = ViewControllers;
sourceTree = "<group>";
Expand Down Expand Up @@ -3184,6 +3187,7 @@
73B1A6BC204B4FB6001F546C /* NonFungibleTokenViewModel.swift in Sources */,
296AF9AB1F7380920058AF78 /* GetTransactionCountRequest.swift in Sources */,
73ED85A72034BFEF00593BF3 /* UITextFieldAdditions.swift in Sources */,
7712437620FD45440097296E /* ImportMainWalletViewController.swift in Sources */,
2923D9B51FDA4E07000CF3F8 /* PasswordGenerator.swift in Sources */,
4EEE540F20799701007D300D /* CoinTicker.swift in Sources */,
298542F51FBD8E6A00CB5081 /* ConfigExplorer.swift in Sources */,
Expand Down
6 changes: 4 additions & 2 deletions Trust/EtherClient/EtherKeystore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ class EtherKeystore: Keystore {
return completion(.failure(KeystoreError.invalidMnemonicPhrase))
}
do {
let account = try keyStore.import(mnemonic: string, passphrase: passphrase, encryptPassword: newPassword, derivationPath: Blockchain.ethereum.derivationPath(at: 0))
let account = try keyStore.import(mnemonic: string, passphrase: passphrase, encryptPassword: newPassword, derivationPath: Coin.ethereum.derivationPath(at: 0))
setPassword(newPassword, for: account)
completion(.success(WalletStruct(type: .hd(account))))
} catch {
Expand Down Expand Up @@ -197,7 +197,7 @@ class EtherKeystore: Keystore {
}

func createAccout(password: String) -> Wallet {
let wallet = try! keyStore.createWallet(password: password, derivationPaths: [Blockchain.ethereum.derivationPath(at: 0)])
let wallet = try! keyStore.createWallet(password: password, derivationPaths: [Coin.ethereum.derivationPath(at: 0)])
let _ = setPassword(password, for: wallet)
return wallet
}
Expand Down Expand Up @@ -409,6 +409,8 @@ class EtherKeystore: Keystore {
object.name = name
case .backup(let completedBackup):
object.completedBackup = completedBackup
case .mainWallet(let mainWallet):
object.main = mainWallet
}
}
storage.realm.add(object, update: true)
Expand Down
2 changes: 1 addition & 1 deletion Trust/Settings/Types/Config.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct Config {
static let testNetworkWarningOff = "testNetworkWarningOff"
}

static let dbMigrationSchemaVersion: UInt64 = 57
static let dbMigrationSchemaVersion: UInt64 = 58

static let current: Config = Config()

Expand Down
1 change: 1 addition & 0 deletions Trust/Wallet/Coordinators/WalletCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ final class WalletCoordinator: Coordinator {
keystore.store(object: wallet.info, fields: [
.name(initialName),
.backup(completedBackup),
.mainWallet(true)
])
walletCreated(wallet: wallet)
}
Expand Down
1 change: 1 addition & 0 deletions Trust/Wallet/Types/WalletObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ final class WalletObject: Object {
@objc dynamic var name: String = ""
@objc dynamic var createdAt: Date = Date()
@objc dynamic var completedBackup: Bool = false
@objc dynamic var main: Bool = false

override static func primaryKey() -> String? {
return "id"
Expand Down
118 changes: 118 additions & 0 deletions Trust/Wallet/ViewControllers/ImportMainWalletViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Copyright DApps Platform Inc. All rights reserved.

import Foundation
import UIKit
import Eureka
import QRCodeReaderViewController

protocol ImportMainWalletViewControllerDelegate: class {
func didImportWallet(wallet: WalletInfo, fields: [WalletInfoField], in controller: ImportMainWalletViewController)
}

final class ImportMainWalletViewController: FormViewController {

let keystore: Keystore

struct Values {
static let mnemonic = "mnemonic"
static let password = "password"
}

var mnemonicRow: TextAreaRow? {
return form.rowBy(tag: Values.mnemonic)
}
var passwordRow: TextFloatLabelRow? {
return form.rowBy(tag: Values.password)
}
weak var delegate: ImportMainWalletViewControllerDelegate?

init(
keystore: Keystore
) {
self.keystore = keystore
super.init(nibName: nil, bundle: nil)
}

override func viewDidLoad() {
super.viewDidLoad()

title = NSLocalizedString("ImportMainWallet", value: "Import Main Wallet", comment: "")
navigationItem.rightBarButtonItems = [
UIBarButtonItem(image: R.image.qr_code_icon(), style: .done, target: self, action: #selector(openReader)),
]

form
+++ Section()

// Mnemonic
+++ Section(footer: ImportSelectionType.mnemonic.footerTitle)
<<< AppFormAppearance.textArea(tag: Values.mnemonic) {
$0.placeholder = R.string.localizable.importWalletMnemonicPlaceholder()
$0.textAreaHeight = .fixed(cellHeight: 140)
$0.add(rule: RuleRequired())
$0.cell.textView?.autocapitalizationType = .none
}

+++ Section()
<<< ButtonRow(R.string.localizable.importWalletImportButtonTitle()) {
$0.title = $0.tag
}.onCellSelection { [weak self] _, _ in
self?.importWallet()
}
}

func didImport(account: WalletStruct) {
let walletInfo = WalletInfo(wallet: account)
delegate?.didImportWallet(wallet: walletInfo, fields: [
.mainWallet(true),
], in: self)
}

func importWallet() {
let validatedError = mnemonicRow?.section?.form?.validate()
guard let errors = validatedError, errors.isEmpty else { return }

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)

keystore.importWallet(type: importType) { result in
self.hideLoading(animated: false)
switch result {
case .success(let account):
self.didImport(account: account)
case .failure(let error):
self.displayError(error: error)
}
}
}

@objc func openReader() {
let controller = QRCodeReaderViewController()
controller.delegate = self
present(controller, animated: true, completion: nil)
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

extension ImportMainWalletViewController: QRCodeReaderDelegate {
func readerDidCancel(_ reader: QRCodeReaderViewController!) {
reader.stopScanning()
reader.dismiss(animated: true, completion: nil)
}
func reader(_ reader: QRCodeReaderViewController!, didScanResult result: String!) {
reader.stopScanning()

mnemonicRow?.value = result
mnemonicRow?.reload()

reader.dismiss(animated: true)
}
}
26 changes: 0 additions & 26 deletions Trust/Wallet/ViewControllers/ImportWalletViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ final class ImportWalletViewController: FormViewController {

title = viewModel.title
navigationItem.rightBarButtonItems = [
UIBarButtonItem(image: R.image.import_options(), style: .done, target: self, action: #selector(importOptions)),
UIBarButtonItem(image: R.image.qr_code_icon(), style: .done, target: self, action: #selector(openReader)),
]

Expand Down Expand Up @@ -218,31 +217,6 @@ final class ImportWalletViewController: FormViewController {
delegate?.didImportAccount(account: walletInfo, fields: [], in: self)
}

@objc func importOptions(sender: UIBarButtonItem) {
let alertController = UIAlertController(
title: NSLocalizedString("importWallet.import.alertSheet.title", value: "Import Wallet Options", comment: ""),
message: .none,
preferredStyle: .actionSheet
)
alertController.popoverPresentationController?.barButtonItem = sender
alertController.addAction(UIAlertAction(
title: NSLocalizedString("importWallet.import.alertSheet.option.title", value: "iCloud/Dropbox/Google Drive", comment: ""),
style: .default
) { _ in
self.showDocumentPicker()
})
alertController.addAction(UIAlertAction(title: R.string.localizable.cancel(), style: .cancel) { _ in })
present(alertController, animated: true)
}

func showDocumentPicker() {
let types = ["public.text", "public.content", "public.item", "public.data"]
let controller = TrustDocumentPickerViewController(documentTypes: types, in: .import)
controller.delegate = self
controller.modalPresentationStyle = .formSheet
present(controller, animated: true, completion: nil)
}

@objc func openReader() {
let controller = QRCodeReaderViewController()
controller.delegate = self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ protocol WalletInfoViewControllerDelegate: class {
enum WalletInfoField {
case name(String)
case backup(Bool)
case mainWallet(Bool)
}

final class WalletInfoViewController: FormViewController {
Expand Down

0 comments on commit 2688aae

Please sign in to comment.