193 lines
7.3 KiB
Swift
193 lines
7.3 KiB
Swift
//
|
|
// SetPrivateKeyViewController.swift
|
|
// Malinka
|
|
//
|
|
// Created by Nut.Tech on 29.07.2022.
|
|
//
|
|
|
|
import UIKit
|
|
import WalletKit
|
|
|
|
final class SetPrivateKeyViewController: UIViewController {
|
|
|
|
private enum WalletNavigation {
|
|
case account(WalletKit.Wallet)
|
|
}
|
|
|
|
@IBOutlet weak var warningView: CommonViewWarning!
|
|
@IBOutlet var headerTitleLabel: UILabel!
|
|
@IBOutlet private weak var submitBtn: CommonButtonAction!
|
|
|
|
private let accountService = AccountOnboarding.Service.CreateAccount()
|
|
private let iapService = AccountOnboarding.Service.IAP.shared
|
|
|
|
@IBOutlet var signalView: UIView!
|
|
@IBOutlet var checkboxButton: UIButton!
|
|
@IBOutlet private weak var usernameView: CommonViewCopy!
|
|
@IBOutlet private weak var privateKeyView: CommonViewCopy!
|
|
|
|
var showCreateManual: Bool!
|
|
var walletService: KeysWalletService!
|
|
|
|
// MARK: - Init
|
|
|
|
// MARK: - Lifecycle
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
|
|
title = L10n.Account.Onboarding.freeTitle
|
|
self.checkboxButton.setImage(Asset.checkboxOff.image, for: .normal)
|
|
|
|
self.usernameView.text = self.walletService.walletName
|
|
self.privateKeyView.text = self.walletService.walletPrivateKey
|
|
self.warningView.makeCritical()
|
|
|
|
self.submitBtn.lzTitle = L10n.Account.Onboarding.Username.create
|
|
self.submitBtn.isEnabled = false
|
|
|
|
if let navigationController = self.navigationController {
|
|
|
|
self.navigationItem.leftBarButtonItem = .pop(self)
|
|
|
|
navigationController.navigationBar.setBackgroundImage(UIImage(), for: .default)
|
|
navigationController.navigationBar.shadowImage = UIImage()
|
|
navigationController.navigationBar.isTranslucent = true
|
|
}
|
|
}
|
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
super.viewWillAppear(animated)
|
|
|
|
if Accounts().current != nil,
|
|
self.navigationController == nil {
|
|
self.dismiss(animated: false)
|
|
}
|
|
}
|
|
|
|
@IBAction private func onSubmit(_ : AnyObject?) {
|
|
|
|
if checkboxButton.image(for: .normal) == Asset.checkboxOff.image {
|
|
UIView.animate(withDuration: 0.3, animations: {
|
|
self.signalView.alpha = 1
|
|
})
|
|
DispatchQueue.main.async {
|
|
UIView.animate(withDuration: 0.3, animations: {
|
|
self.signalView.alpha = 0
|
|
})
|
|
}
|
|
return
|
|
}
|
|
|
|
Loader.show(in: self)
|
|
|
|
let service = self.walletService.createService()
|
|
Task {
|
|
do {
|
|
let wallet = try await service.create()
|
|
|
|
self.showErrorPopup(title: "account.create.success.title".localized,
|
|
description: "account.create.success.description".localized,
|
|
button: "account.create.success.button".localized,
|
|
navigate: .account(wallet))
|
|
|
|
} catch WalletError.dailyLimit {
|
|
|
|
self.showErrorPopup(title: "account.create.failed.daily.title".localized,
|
|
description: "account.create.failed.daily.description".localized,
|
|
button: "account.create.failed.daily.button".localized)
|
|
} catch WalletError.deviceLimit {
|
|
self.showErrorPopup(title: "account.create.failed.device.title".localized,
|
|
description: "account.create.failed.device.description".localized,
|
|
button: "account.create.failed.device.button".localized)
|
|
} catch {
|
|
self.showErrorPopup(title: "account.create.failed.general.title".localized,
|
|
description: "account.create.failed.general.description".localized,
|
|
button: "account.create.failed.general.button".localized)
|
|
}
|
|
}
|
|
}
|
|
|
|
@IBAction func onGenerateKeyClicked(_ sender: Any) {
|
|
self.walletService.generateKey()
|
|
privateKeyView.text = self.walletService.walletPrivateKey
|
|
}
|
|
|
|
@IBAction func onUseOwnKeyClicked(_ sender: Any) {
|
|
let view = AccountOnboardingAddPrivateKey()
|
|
view.onSubmit = { key in
|
|
|
|
self.walletService.update(key: key)
|
|
self.privateKeyView.text = self.walletService.walletPrivateKey
|
|
|
|
Popup.hide()
|
|
|
|
Popup.show(title: L10n.Account.Onboarding.Keys.Use.Own.added, views: [
|
|
.field(title: L10n.Account.Onboarding.Username.accountNameTitle, text: self.walletService.walletName),
|
|
.field(title: L10n.Account.Onboarding.Keys.Use.Own.privateKeyAdded, text: key.privateKey)
|
|
], submit: L10n.Common.Button.continue, in: self)
|
|
}
|
|
|
|
Popup.show(content: CommonViewControllerViewPopUp(
|
|
title: L10n.Account.Onboarding.Enter.Private.key,
|
|
description: L10n.Account.Onboarding.Keys.Use.Own.description,
|
|
view: view
|
|
))
|
|
}
|
|
|
|
@IBAction func onCheckbox(_ sender: Any) {
|
|
self.signalView.alpha = 0
|
|
if self.checkboxButton.image(for: .normal) == Asset.checkboxOff.image {
|
|
self.checkboxButton.setImage(Asset.checkboxOn.image, for: .normal)
|
|
self.submitBtn.isEnabled = true
|
|
} else {
|
|
self.checkboxButton.setImage(Asset.checkboxOff.image, for: .normal)
|
|
self.submitBtn.isEnabled = false
|
|
}
|
|
}
|
|
|
|
@IBAction func haveWalletPrssed(_: AnyObject?) {
|
|
let ctrl = UINavigationController(rootViewController: StoryboardScene.Account.manual.instantiate())
|
|
self.navigationController?.pushViewController(ctrl, animated: true)
|
|
}
|
|
|
|
private func showErrorPopup(title: String, description: String, button: String, navigate: WalletNavigation? = nil) {
|
|
let views: [UIView] = [
|
|
.field(text: title, textStyle: "regular_16"),
|
|
.field(text: description)
|
|
]
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
Loader.hide(in: self)
|
|
|
|
Popup.show(title: title,
|
|
views: views,
|
|
submit: button,
|
|
submitStyle: .primary, in: self) { ctrl in
|
|
ctrl.dismiss(animated: true, completion: nil)
|
|
|
|
if case let .account(wallet) = navigate {
|
|
AccountViewAuthorize.showGetPassword(in: self) { password in
|
|
print(">>>>> \(password)")
|
|
}
|
|
/**
|
|
AccountViewAuthorize.showGetPassword(in: self) {
|
|
Accounts().addAccounts(
|
|
usernames: [self.selectedUsername],
|
|
privateKey: privateKey.base58, password: $0, keyType: .owner
|
|
) { error in
|
|
guard let _ = error else { return }
|
|
self.showErrorPopup()
|
|
}
|
|
self.dismiss(animated: true)
|
|
}
|
|
*/
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
}
|