Files
raspberry/iOS/Wallet/Sources/Account/Controller/AccountControllerChangeKey.swift
2022-12-14 23:28:32 +03:00

240 lines
9.5 KiB
Swift

//
// AccountControllerOnboardingChangeKey.swift
// Wallet
//
// Created by Alexandr Serpokrylow on 16.02.2022.
// Copyright © 2022 AM. All rights reserved.
//
import UIKit
import WalletKit
import EosioSwift
import eosswift
final class AccountControllerChangeKey: UIViewController {
@IBOutlet weak var accountsLabel: UILabel!
@IBOutlet weak var accountsTextView: UITextView!
@IBOutlet weak var canChangeLabel: UILabel!
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var confirmButton: CommonButtonAction!
private let service = Account.Service.ChangeKey()
private var accounts = [ChangeKeyModel]()
var privateKey = ""
var wallet: WalletKit.Wallet?
override func viewDidLoad() {
super.viewDidLoad()
self.title = L10n.Account.Onboarding.Changing.Private.key
if let navigationController = self.navigationController {
self.navigationItem.leftBarButtonItem = .back { self.dismiss(animated: true) }
navigationController.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController.navigationBar.shadowImage = UIImage()
navigationController.navigationBar.isTranslucent = true
}
self.configureView()
}
private func configureView() {
guard let wallet else { return }
self.configureChangeLabel(wallet: wallet)
self.tableView.delegate = self
self.tableView.dataSource = self
self.tableView.register(cell: ChangeKeyCell.self)
let records = ChangeKeyModel.create(by: wallet, with: self.privateKey)
self.accounts = records
self.configureAccountView()
self.tableView.reloadData()
}
private func configureAccountView() {
let attributedString = NSMutableAttributedString(string: "")
for (index, account) in self.accounts.enumerated() {
let divider = index != self.accounts.count - 1 ? "\n" : ""
let string = NSMutableAttributedString(string: "\(account.wallet.name)@\(account.keyType.rawValue)\(divider)")
string.setColor(color: Asset.coal.color, forText: account.wallet.name)
string.setColor(color: Asset.pebble.color, forText: "@\(account.keyType.rawValue)")
string.setFont(font: UIFont.font(style: .medium, size: 14), forText: account.wallet.name)
string.setFont(font: UIFont.font(style: .regular, size: 14), forText: "@\(account.keyType.rawValue)")
attributedString.append(string)
}
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 8
attributedString.addAttribute(NSAttributedString.Key.paragraphStyle,
value: paragraphStyle,
range: NSMakeRange(0, attributedString.length))
self.accountsLabel.attributedText = attributedString
self.accountsTextView.attributedText = attributedString
self.accountsLabel.isHidden = accounts.count > 5
self.accountsTextView.isHidden = accounts.count <= 5
}
private func configureChangeLabel(wallet: WalletKit.Wallet) {
let avaialableKeys = wallet.keyType == WalletKeyType.owner
? "\(WalletKeyType.owner.rawValue), \(WalletKeyType.active.rawValue)"
: WalletKeyType.active.rawValue
let attributedString = NSMutableAttributedString(string: "\(L10n.Account.Onboarding.Can.change)\(avaialableKeys)")
attributedString.setFont(font: UIFont.font(style: .regular, size: 14), forText: L10n.Account.Onboarding.Can.change)
attributedString.setFont(font: UIFont.font(style: .medium, size: 14), forText: avaialableKeys)
self.canChangeLabel.attributedText = attributedString
}
private func checkButtonState() {
let keysValid = !self.accounts.filter({$0.isChecked}).contains(where: { $0.hasError })
let hasChecked = self.accounts.contains(where: { $0.isChecked })
self.confirmButton.isEnabled = keysValid && hasChecked
}
func validateKeyPair(for cell: ChangeKeyCell, row: Int, privateKey: String, publicKey: String) {
let privateWallet = privateKey.count == Constants.maxPrivateKeyTextCount
? try? WalletKey.create(private: privateKey)
: nil
let publicWallet = publicKey.count == Constants.maxPublicKeyTextCount
? try? WalletKey.create(public: publicKey)
: nil
self.accounts[row].privateKey = privateKey
self.accounts[row].publicKey = publicKey
if let privateWalletKey = privateWallet,
let publicWalletKey = publicWallet {
let isValidKeyPair = privateWalletKey.publicKey == publicWalletKey.publicKey
if isValidKeyPair {
cell.configureError(for: cell.privateKeyTextField, errorMessage: nil)
cell.configureError(for: cell.publicKeyTextField, errorMessage: nil)
self.accounts[row].hasError = false
self.accounts[row].key = privateWalletKey
} else {
cell.configureError(for: cell.privateKeyTextField, errorMessage: L10n.Error.Accounts.keyPairInvalid)
cell.configureError(for: cell.publicKeyTextField, errorMessage: L10n.Error.Accounts.keyPairInvalid)
self.accounts[row].hasError = true
}
} else {
if !privateWallet.isExist {
cell.configureError(for: cell.privateKeyTextField, errorMessage: L10n.Error.Accounts.keyInvalid)
self.accounts[row].hasError = true
}
if !publicWallet.isExist {
cell.configureError(for: cell.publicKeyTextField, errorMessage: L10n.Error.Accounts.publicKeyInvalid)
self.accounts[row].hasError = true
}
}
self.checkButtonState()
}
@IBAction func onConfirmClicked(_ sender: Any) {
view.endEditing(true)
let keysValid = !accounts.filter {$0.isChecked}.contains(where: { $0.hasError })
if keysValid {
let ctrl = StoryboardScene.Account.confirmChangeKey.instantiate()
ctrl.collection = accounts.filter { $0.isChecked }
ctrl.changeKeyHasBeenProcessed = {
self.dismiss(animated: true)
}
self.navigationController?.pushViewController(ctrl, animated: true)
}
}
}
extension AccountControllerChangeKey: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.accounts.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(ChangeKeyCell.self, indexPath: indexPath)
let account = self.accounts[indexPath.row]
cell.configureView(account: account)
cell.onCheckboxClicked = {
let newState = !self.accounts[indexPath.row].isChecked
self.accounts[indexPath.row].isChecked = newState
cell.selectAccount(isChecked: newState)
self.checkButtonState()
}
cell.onGenerateNewKeysClicked = {
guard let newPrivateKey = try? EOSPrivateKey() else {
return
}
self.accounts[indexPath.row].privateKey = newPrivateKey.base58
self.accounts[indexPath.row].publicKey = newPrivateKey.publicKey.base58
self.accounts[indexPath.row].hasError = false
cell.configureView(account: self.accounts[indexPath.row])
cell.configureError(for: cell.privateKeyTextField, errorMessage: nil)
cell.configureError(for: cell.publicKeyTextField, errorMessage: nil)
self.checkButtonState()
}
cell.onValidatePrivateKey = { privateKey in
let isValidPrivateKey = privateKey.count == Constants.maxPrivateKeyTextCount
? (try? WalletKey.create(private: privateKey)).isExist
: false
self.accounts[indexPath.row].hasError = !isValidPrivateKey
if isValidPrivateKey {
guard let privateKey = try? EOSPrivateKey(base58: privateKey) else {
return
}
let publicKey = privateKey.publicKey.base58
cell.publicKeyTextField.value = publicKey
self.accounts[indexPath.row].privateKey = privateKey.base58
self.accounts[indexPath.row].publicKey = publicKey
cell.configureError(for: cell.privateKeyTextField, errorMessage: nil)
} else {
cell.configureError(for: cell.privateKeyTextField, errorMessage: L10n.Error.Accounts.keyInvalid)
}
self.checkButtonState()
}
cell.onValidateKeyPair = { privateKey, publicKey in
self.validateKeyPair(for: cell, row: indexPath.row, privateKey: privateKey, publicKey: publicKey)
}
return cell
}
}
extension AccountControllerChangeKey {
enum Constants {
static let maxPrivateKeyTextCount = 51
static let maxPublicKeyTextCount = 53
}
}