-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathRecoverWalletTableViewController.swift
229 lines (186 loc) · 9.45 KB
/
RecoverWalletTableViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//
// RecoverWalletTableViewController.swift
// Decred Wallet
// Copyright (c) 2018-2019 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
import UIKit
class RecoverWalletTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet var tableView : UITableView!
@IBOutlet weak var wordSelectionDropDownContainer: UIView!
@IBOutlet weak var tableViewFooter: UIStackView!
@IBOutlet weak var tableViewFooterTopSpacingConstraint: NSLayoutConstraint!
@IBOutlet weak var lblEnterAllSeeds: UILabel!
@IBOutlet weak var btnConfirm: UIButton!
var validSeedWords: [String] = []
var userEnteredSeedWords = [String](repeating: "", count: 33)
private var testSeed = "reform aftermath printer warranty gremlin paragraph beehive stethoscope regain disruptive regain Bradbury chisel October trouble forever Algol applicant island infancy physique paragraph woodlark hydraulic snapshot backwater ratchet surrender revenge customer retouch intention minnow"
private var useTestSeed: Bool = false
override func viewDidLoad() {
super.viewDidLoad()
// load seed words
let seedWordsPath = Bundle.main.path(forResource: "wordlist", ofType: "txt")
let seedWords = try? String(contentsOfFile: seedWordsPath ?? "")
validSeedWords = seedWords?.split{$0 == "\n"}.map(String.init) ?? []
registerObserverForKeyboardNotification()
self.hideKeyboardWhenTappedAround()
// long press to proceed with test seed, only on testnet
let isTestnet = Bool(infoForKey(GlobalConstants.Strings.IS_TESTNET)!)!
if isTestnet {
let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(longPressConfirm))
btnConfirm.addGestureRecognizer(longGesture)
}
}
deinit {
unregisterObserverForKeyboardNotification()
}
func registerObserverForKeyboardNotification() {
NotificationCenter.default.addObserver(self, selector: #selector(onKeyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(onKeyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
func unregisterObserverForKeyboardNotification() {
NotificationCenter.default.removeObserver(self, name:NSNotification.Name.UIKeyboardWillShow, object:nil)
NotificationCenter.default.removeObserver(self, name:NSNotification.Name.UIKeyboardWillHide, object:nil)
}
@objc func onKeyboardWillShow(_ notification: Notification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue,
let window = self.view.window?.frame {
// Minusing keyboard height from window height
self.view.frame = CGRect(x: self.view.frame.origin.x,
y: self.view.frame.origin.y,
width: self.view.frame.width,
height: window.origin.y + window.height - keyboardSize.height)
}
}
@objc func onKeyboardWillHide(_ notification: Notification) {
if let window = self.view.window?.frame {
// Resize main view to window height
self.view.frame = CGRect(x: self.view.frame.origin.x,
y: self.view.frame.origin.y,
width: self.view.frame.width,
height: window.origin.y + window.height)
}
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 33
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let seedWordCell = tableView.dequeueReusableCell(withIdentifier: "seedWordCell", for: indexPath) as! RecoveryWalletSeedWordCell
seedWordCell.lbSeedWordNum.text = "Word #\(indexPath.row + 1)"
seedWordCell.seedWordAutoComplete.text = self.userEnteredSeedWords[indexPath.row]
seedWordCell.seedWordAutoComplete.resignFirstResponder()
seedWordCell.setupAutoComplete(for: indexPath.row,
filter: self.validSeedWords,
dropDownListPlaceholder: self.wordSelectionDropDownContainer,
onSeedEntered: self.seedWordEntered)
return seedWordCell
}
func seedWordEntered(for wordIndex: Int, seedWord: String, moveToNextField: Bool) {
self.userEnteredSeedWords[wordIndex] = seedWord
if wordIndex < 32 && moveToNextField {
self.focusSeedWordInput(at: wordIndex + 1)
} else {
self.view.endEditing(true)
}
if self.validateSeed().valid {
self.activateConfirmButton()
} else {
self.deactivateConfirmButton()
}
}
func focusSeedWordInput(at tableRowIndex: Int) {
let tableIndexPath = IndexPath(row: tableRowIndex, section: 0)
let nextSeedWordCell = self.tableView.cellForRow(at: tableIndexPath) as? RecoveryWalletSeedWordCell
nextSeedWordCell?.seedWordAutoComplete.becomeFirstResponder()
self.tableView.scrollToRow(at: tableIndexPath, at: .middle, animated: true)
}
func activateConfirmButton() {
self.btnConfirm.backgroundColor = UIColor.DecredColors.Green
self.lblEnterAllSeeds.isHidden = true
// increase top spacing since warning label is now hidden so as to position button in center
self.tableViewFooterTopSpacingConstraint.constant = 30
UIView.animate(withDuration: 0.5) {
self.tableViewFooter.layoutIfNeeded()
}
}
func deactivateConfirmButton() {
self.btnConfirm.backgroundColor = UIColor.LightGray
self.lblEnterAllSeeds.isHidden = false
if self.userEnteredSeedWords.contains("") {
self.lblEnterAllSeeds.text = "Not all seeds are entered. Please, check input fields and enter all seeds."
} else {
self.lblEnterAllSeeds.text = "You entered an incorrect seed. Please check your words."
}
// reduce top spacing so that warning label and confirm button are centered in display
self.tableViewFooterTopSpacingConstraint.constant = 10
UIView.animate(withDuration: 0.5) {
self.tableViewFooter.layoutIfNeeded()
}
}
@IBAction func onConfirm() {
if self.validSeedWords.contains("") || !self.validateSeed().valid {
// all words have not been entered
return
}
self.performSegue(withIdentifier: "confirmSeedSegue", sender: nil)
}
@objc func longPressConfirm() {
if self.useTestSeed {
return
}
self.useTestSeed = true
self.performSegue(withIdentifier: "confirmSeedSegue", sender: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "confirmSeedSegue" {
var confirmSeedVC = segue.destination as? SeedCheckupProtocol
if self.useTestSeed {
confirmSeedVC?.seedToVerify = self.testSeed
} else {
confirmSeedVC?.seedToVerify = self.validateSeed().seed
}
}
}
private func validateSeed() -> (seed: String, valid: Bool) {
let seed = self.userEnteredSeedWords.reduce("", {(word1, word2) in "\(word1!) \(word2)"})
let seedValid = SingleInstance.shared.wallet?.verifySeed(seed)
return (seed, seedValid!)
}
private func showError(_ error: String) {
let alert = UIAlertController(title: "Wallet recovery error", message: error, preferredStyle: .alert)
let okAction = UIAlertAction(title: "Try again", style: .default) { _ in
alert.dismiss(animated: true, completion: nil)
self.clearSeedInputs()
}
alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)
}
func clearSeedInputs() {
self.userEnteredSeedWords = [String](repeating: "", count: 33)
self.tableView.reloadData()
self.tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: true)
}
}
extension RecoverWalletTableViewController: UIScrollViewDelegate {
func scrollViewDidScroll(_ scrollView: UIScrollView) {
self.wordSelectionDropDownContainer.isHidden = true
}
func hideKeyboardWhenTappedAround() {
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
tap.cancelsTouchesInView = false
view.addGestureRecognizer(tap)
}
@objc func handleTap(_ tap: UITapGestureRecognizer) {
var tapPoint = tap.location(in: self.view)
tapPoint = self.wordSelectionDropDownContainer.convert(tapPoint, from: self.view)
if self.wordSelectionDropDownContainer.bounds.contains(tapPoint) {
// ignore taps inside the autoselection dropdown
return
}
view.endEditing(true)
self.wordSelectionDropDownContainer.isHidden = true
}
}