-
Notifications
You must be signed in to change notification settings - Fork 0
/
WalletGenerationViewController.swift
260 lines (228 loc) · 9.83 KB
/
WalletGenerationViewController.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//
// WalletGenerationViewController.swift
// Geolocation
//
// Adapted from PeepethClient by Matter, Inc.
// Obtained from: https://github.com/matterinc/PeepethClient
// Obtained on: 01/4/19
//
// Created by Matt Johnson on 1/4/19.
// Copyright © 2018 Matt Johnson. All rights reserved.
//
import UIKit
import Web3swift
import QRCodeReader
class WalletGenerationViewController: UIViewController,
QRCodeReaderViewControllerDelegate {
@IBOutlet weak var passwordsDontMatch: UILabel!
@IBOutlet weak var createButton: UIButton!
@IBOutlet var textFields: [UITextField]!
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var repeatPasswordTextField: UITextField!
@IBOutlet weak var enterPrivateKeyTextField: UITextField!
@IBOutlet weak var privateKeyHeight: NSLayoutConstraint!
@IBOutlet weak var qrCodeButton: UIButton!
@IBOutlet weak var enterPrivateKeyLabel: NSLayoutConstraint!
@IBOutlet weak var enterKeyLabel: UILabel!
@IBOutlet weak var qrLabel: UILabel!
@IBOutlet weak var orQrLabel: UILabel!
@IBOutlet weak var qrImageHeigh: NSLayoutConstraint!
@IBOutlet weak var qrLabelHeight: NSLayoutConstraint!
@IBOutlet weak var titleLabel: UILabel!
var creationType: CreationType = .createWallet
let keysService: KeysService = KeysService()
let localStorage = LocalDatabase()
let alerts = Alerts()
let web3service: Web3swiftService = Web3swiftService()
let walletGenerationController = WalletGenerationController()
override func viewDidLoad() {
super.viewDidLoad()
titleLabel.text = creationType.title()
self.hideKeyboardWhenTappedAround()
createButton.setTitle(creationType.title(), for: .normal)
createButton.isEnabled = false
createButton.alpha = 0.5
passwordsDontMatch.isHidden = true
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.navigationBar.prefersLargeTitles = true
self.navigationController?.setNavigationBarHidden(false, animated: true)
if creationType == .createWallet {
qrLabelHeight.constant = 0
enterPrivateKeyLabel.constant = 0
qrImageHeigh.constant = 0
privateKeyHeight.constant = 0
orQrLabel.alpha = 0
qrLabel.alpha = 0
enterKeyLabel.alpha = 0
qrCodeButton.alpha = 0
qrCodeButton.isUserInteractionEnabled = false
enterPrivateKeyTextField.isUserInteractionEnabled = false
}
}
func createWallet() {
guard passwordTextField.text == repeatPasswordTextField.text else {
passwordsDontMatch.isHidden = false
return
}
passwordsDontMatch.isHidden = true
walletGenerationController.createWallet(with: creationType, password: passwordTextField.text, key: enterPrivateKeyTextField.text) { (error) in
guard error == nil else {
self.alerts.show(error, for: self)
return
}
self.performSegue(withIdentifier: "enterMain", sender: nil)
}
}
@IBAction func createWalletButtonTapped(_ sender: UIButton) {
createWallet()
UIView.animate(withDuration: 0.05) {
sender.transform = CGAffineTransform.identity
}
}
@IBAction func backAction(_ sender: UIButton) {
dismiss(animated: true, completion: nil)
}
@IBAction func buttonTouchedDown(_ sender: UIButton) {
UIView.animate(withDuration: 0.05,
animations: {
sender.transform = CGAffineTransform(scaleX: 0.9, y: 0.9)
},
completion: nil)
}
@IBAction func buttonTouchedDragInside(_ sender: UIButton) {
UIView.animate(withDuration: 0.05,
animations: {
sender.transform = CGAffineTransform(scaleX: 0.9, y: 0.9)
},
completion: nil)
}
@IBAction func buttonTouchedDragOutside(_ sender: UIButton) {
UIView.animate(withDuration: 0.05) {
sender.transform = CGAffineTransform.identity
}
}
@IBAction func touchCancel(_ sender: UIButton) {
UIView.animate(withDuration: 0.05) {
sender.transform = CGAffineTransform.identity
}
}
//QR feature
@IBAction func qrScanTapped(_ sender: UIButton) {
readerVC.delegate = self
readerVC.completionBlock = { (result: QRCodeReaderResult?) in
}
readerVC.modalPresentationStyle = .formSheet
present(readerVC, animated: true, completion: nil)
UIView.animate(withDuration: 0.05) {
sender.transform = CGAffineTransform.identity
}
}
// Scan
lazy var readerVC: QRCodeReaderViewController = {
let builder = QRCodeReaderViewControllerBuilder {
$0.reader = QRCodeReader(metadataObjectTypes: [.qr], captureDevicePosition: .back)
}
return QRCodeReaderViewController(builder: builder)
}()
func reader(_ reader: QRCodeReaderViewController, didScanResult result: QRCodeReaderResult) {
reader.stopScanning()
enterPrivateKeyTextField.text = result.value
if passwordTextField.text == repeatPasswordTextField.text &&
!(passwordTextField.text?.isEmpty ?? true) {
createButton.isEnabled = true
createButton.alpha = 1
}
dismiss(animated: true, completion: nil)
}
func readerDidCancel(_ reader: QRCodeReaderViewController) {
reader.stopScanning()
dismiss(animated: true, completion: nil)
}
}
extension WalletGenerationViewController: UITextFieldDelegate {
func textFieldDidBeginEditing(_ textField: UITextField) {
textField.returnKeyType = createButton.isEnabled ? UIReturnKeyType.done : .next
textField.textColor = UIColor(red:0.79, green:0.62, blue:0.40, alpha:1.0)
if textField == passwordTextField || textField == repeatPasswordTextField {
passwordsDontMatch.isHidden = true
}
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let currentText = (textField.text ?? "") as NSString
let futureString = currentText.replacingCharacters(in: range, with: string) as String
createButton.isEnabled = false
switch textField {
case enterPrivateKeyTextField:
if passwordTextField.text == repeatPasswordTextField.text &&
!(passwordTextField.text?.isEmpty ?? true) &&
!futureString.isEmpty && ((passwordTextField.text?.count)! > 4) {
createButton.isEnabled = true
}
case passwordTextField:
if !futureString.isEmpty &&
futureString == repeatPasswordTextField.text ||
repeatPasswordTextField.text?.isEmpty == true {
passwordsDontMatch.isHidden = true
createButton.isEnabled = (!(enterPrivateKeyTextField.text?.isEmpty ?? true) || creationType == .createWallet)
} else {
passwordsDontMatch.isHidden = false
createButton.isEnabled = false
}
case repeatPasswordTextField:
if !futureString.isEmpty &&
futureString == passwordTextField.text {
passwordsDontMatch.isHidden = true
createButton.isEnabled = (!(enterPrivateKeyTextField.text?.isEmpty ?? true) || creationType == .createWallet)
} else {
passwordsDontMatch.isHidden = false
createButton.isEnabled = false
}
default:
createButton.isEnabled = false
passwordsDontMatch.isHidden = false
}
createButton.alpha = createButton.isEnabled ? 1.0 : 0.5
textField.returnKeyType = createButton.isEnabled ? UIReturnKeyType.done : .next
return true
}
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
textField.textColor = UIColor.darkGray
guard textField == repeatPasswordTextField ||
textField == passwordTextField else {
return true
}
if (!(passwordTextField.text?.isEmpty ?? true) ||
!(repeatPasswordTextField.text?.isEmpty ?? true)) &&
passwordTextField.text != repeatPasswordTextField.text {
passwordsDontMatch.isHidden = false
passwordsDontMatch.text = "Passwords don't match"
repeatPasswordTextField.textColor = UIColor.red
passwordTextField.textColor = UIColor.red
} else if (!(passwordTextField.text?.isEmpty ?? true) ||
!(repeatPasswordTextField.text?.isEmpty ?? true)) &&
((passwordTextField.text?.count)! < 5) {
passwordsDontMatch.isHidden = false
passwordsDontMatch.text = "Password is too short"
repeatPasswordTextField.textColor = UIColor.red
passwordTextField.textColor = UIColor.red
} else {
repeatPasswordTextField.textColor = UIColor.darkGray
passwordTextField.textColor = UIColor.darkGray
}
return true
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField.returnKeyType == .done && createButton.isEnabled && ((passwordTextField.text?.count)! > 4) {
createWallet()
} else if textField.returnKeyType == .next {
let index = textFields.index(of: textField) ?? 0
let nextIndex = (index == textFields.count - 1) ? 0 : index + 1
textFields[nextIndex].becomeFirstResponder()
} else {
view.endEditing(true)
}
return true
}
}