61 lines
1.7 KiB
Swift
61 lines
1.7 KiB
Swift
//
|
|
// AccountOnboardingAddPrivateKey.swift
|
|
// Wallet
|
|
//
|
|
// Created by Alexandr Serpokrylow on 23.02.2022.
|
|
// Copyright © 2022 AM. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
import EosioSwift
|
|
import WalletKit
|
|
|
|
final class AccountOnboardingAddPrivateKey: CommonViewCustom {
|
|
|
|
enum Constants {
|
|
static let maxPrivateKeyTextCount = 51
|
|
}
|
|
|
|
@IBOutlet weak var textField: CommonTextField!
|
|
@IBOutlet weak var submitButton: CommonButtonAction!
|
|
|
|
var onSubmit: ((WalletKey) -> Void)?
|
|
|
|
override func setup() {
|
|
super.setup()
|
|
textField.delegate = self
|
|
submitButton.isEnabled = false
|
|
}
|
|
|
|
@IBAction func onCheckClicked(_ sender: Any) {
|
|
do {
|
|
let privateKey = try WalletKey.create(private: textField.value)
|
|
onSubmit?(privateKey)
|
|
} catch {
|
|
self.textField.error = L10n.Error.Accounts.keyInvalid
|
|
self.submitButton.isEnabled = false
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - CommonTextFieldDelegate
|
|
|
|
extension AccountOnboardingAddPrivateKey: CommonTextFieldDelegate {
|
|
|
|
func textFieldDidChange(_ textField: CommonTextField) {
|
|
self.submitButton.isEnabled = textField.value.count == Constants.maxPrivateKeyTextCount
|
|
textField.error = nil
|
|
}
|
|
|
|
func textField(_ textField: CommonTextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
|
|
let currentText = textField.value
|
|
|
|
guard let stringRange = Range(range, in: currentText) else { return false }
|
|
|
|
let updatedText = currentText.replacingCharacters(in: stringRange, with: string)
|
|
let shouldChange = updatedText.count <= Constants.maxPrivateKeyTextCount
|
|
|
|
return shouldChange
|
|
}
|
|
}
|