204 lines
7.5 KiB
Swift
204 lines
7.5 KiB
Swift
//
|
|
// AccountViewAuthorize.swift
|
|
// Wallet
|
|
//
|
|
// Created by Igor Danich on 20.08.2020.
|
|
// Copyright © 2020 List. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
import Combine
|
|
|
|
final class AccountViewControllerAuthorize: UIViewController {
|
|
|
|
let authView = AccountViewAuthorize()
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
|
|
title = L10n.Account.Authorize.title
|
|
image = Asset.accountPasswordPopup.image
|
|
|
|
view.addSubview(authView)
|
|
authView.translatesAutoresizingMaskIntoConstraints = false
|
|
authView.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor).isActive = true
|
|
authView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
|
|
authView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
|
|
authView.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
|
|
}
|
|
}
|
|
|
|
final class AccountViewAuthorize: CommonViewCustom {
|
|
|
|
typealias Password = String
|
|
typealias Completion = (Password) -> Void
|
|
|
|
@IBOutlet private var descriptionLabel: UILabel!
|
|
@IBOutlet private weak var textField: CommonTextField!
|
|
@IBOutlet private weak var biometricksBtn: UIButton!
|
|
|
|
private var didSubmit: ((String) -> Void)?
|
|
private var isGetPassword = false
|
|
|
|
static func showGetPrivateKey(in viewController: UIViewController,
|
|
description: String = "",
|
|
showPinBackButton: Bool = true,
|
|
_ completion: @escaping Completion) {
|
|
|
|
showGet(in: viewController,
|
|
isPasswordRequired: false,
|
|
description: description,
|
|
showPinBackButton: showPinBackButton,
|
|
completion)
|
|
}
|
|
|
|
static func getPrivateKey() -> String {
|
|
|
|
if UserDefaults.standard.bool(forKey: "isPinPwdMode"),
|
|
let pin = Account.Service.Authorize.shared.password,
|
|
Account.Service.check(password: pin),
|
|
let value = Accounts().current?.privateKey(password: pin) {
|
|
return value
|
|
}
|
|
return ""
|
|
}
|
|
|
|
static func showGetPassword(in viewController: UIViewController,
|
|
viewModel: WalletPinViewModel? = nil,
|
|
description: String = "",
|
|
showPinBackButton: Bool = true,
|
|
_ completion: @escaping Completion) {
|
|
|
|
showGet(in: viewController,
|
|
isPasswordRequired: true,
|
|
description: description,
|
|
showPinBackButton: showPinBackButton,
|
|
viewModel: viewModel,
|
|
completion)
|
|
}
|
|
|
|
private static func showGet(in viewController: UIViewController,
|
|
isPasswordRequired: Bool,
|
|
description: String,
|
|
showPinBackButton: Bool,
|
|
viewModel: WalletPinViewModel? = nil,
|
|
_ completion: @escaping Completion) {
|
|
|
|
if UserDefaults.standard.bool(forKey: "isPinPwdMode") {
|
|
if let pin = Account.Service.Authorize.shared.password,
|
|
Account.Service.check(password: pin),
|
|
!pin.isEmpty,
|
|
pin.count == 4 {
|
|
|
|
completion(isPasswordRequired ? pin : Accounts().current?.privateKey(password: pin) ?? "")
|
|
return
|
|
}
|
|
|
|
let accountControollerPin = WalletPinController.instantiate(viewModel: viewModel)
|
|
let ctrl = UINavigationController(rootViewController: accountControollerPin)
|
|
accountControollerPin.showBackButton = showPinBackButton
|
|
accountControollerPin.viewModel.validatePin = { [weak ctrl] (pin: String) -> Bool in
|
|
if Account.Service.check(password: pin),
|
|
!pin.isEmpty,
|
|
pin.count == 4 {
|
|
|
|
ctrl?.dismiss(animated: true)
|
|
|
|
Account.Service.Authorize.shared.update(password: pin, type: .both)
|
|
|
|
completion(isPasswordRequired ? pin : Accounts().current?.privateKey(password: pin) ?? "")
|
|
|
|
Account.Service.Authorize.shared.clearPinTries()
|
|
return true
|
|
} else {
|
|
Account.Service.Authorize.shared.savePinTry()
|
|
return false
|
|
}
|
|
}
|
|
accountControollerPin.viewModel.submitBiometric = { [weak ctrl] in
|
|
if let pin = Account.Service.passwordViaBiometrics(),
|
|
!pin.isEmpty,
|
|
pin.count == 4 {
|
|
|
|
Account.Service.Authorize.shared.update(password: pin, type: .both)
|
|
|
|
ctrl?.dismiss(animated: true) {
|
|
completion(isPasswordRequired
|
|
? pin
|
|
: Accounts().current?.privateKey(password: pin) ?? "")
|
|
}
|
|
}
|
|
}
|
|
ctrl.modalPresentationStyle = .fullScreen
|
|
viewController.present(ctrl, animated: true)
|
|
} else {
|
|
let ctrl = AccountViewControllerAuthorize()
|
|
ctrl.authView.isGetPassword = isPasswordRequired
|
|
ctrl.authView.descriptionLabel.text = description
|
|
ctrl.authView.descriptionLabel.isHidden = description.isEmpty
|
|
ctrl.authView.didSubmit = { [unowned ctrl] in
|
|
ctrl.dismiss(animated: true)
|
|
completion($0)
|
|
Account.Service.Authorize.shared.update(password: nil, type: .toggle)
|
|
}
|
|
Popup.show(content: ctrl, in: viewController)
|
|
}
|
|
}
|
|
|
|
override func setup() {
|
|
super.setup()
|
|
|
|
textField.lzText = kAuthorizationPassword
|
|
textField.delegate = self
|
|
biometricksBtn.isHidden = !Account.Service.isBiometricksAvailable
|
|
}
|
|
|
|
@IBAction private func onSubmit(_ : AnyObject?) {
|
|
if isGetPassword {
|
|
if let pin = textField.lzText, Account.Service.check(password: pin), !pin.isEmpty {
|
|
self.didSubmit?(pin)
|
|
} else {
|
|
self.textField.error = L10n.Account.Authorize.error
|
|
}
|
|
} else {
|
|
if let pin = textField.lzText,
|
|
let privateKey = Accounts().current?.privateKey(password: pin),
|
|
!privateKey.isEmpty {
|
|
self.didSubmit?(privateKey)
|
|
} else {
|
|
self.textField.error = L10n.Account.Authorize.error
|
|
}
|
|
}
|
|
}
|
|
|
|
@IBAction private func onFaceId(_ : AnyObject?) {
|
|
if isGetPassword {
|
|
if let password = Account.Service.passwordViaBiometrics(),
|
|
!password.isEmpty {
|
|
self.didSubmit?(password)
|
|
} else {
|
|
self.textField.error = L10n.Account.Authorize.error
|
|
}
|
|
} else {
|
|
if let privateKey = Accounts().current?.privateKey(password: nil),
|
|
!privateKey.isEmpty {
|
|
self.didSubmit?(privateKey)
|
|
} else {
|
|
self.textField.error = L10n.Account.Authorize.error
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
extension AccountViewAuthorize: CommonTextFieldDelegate {
|
|
|
|
func textFieldDidSubmit(_ textField: CommonTextField) {
|
|
onSubmit(nil)
|
|
}
|
|
|
|
func textFieldDidChange(_ textField: CommonTextField) {
|
|
textField.error = nil
|
|
}
|
|
}
|