forked from trustwallet/trust-wallet-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
132 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
Trust/Wallet/ViewControllers/ImportMainWalletViewController.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters