tokenary/Encrypted Ink/Agent.swift

252 lines
9.2 KiB
Swift
Raw Normal View History

2021-06-12 19:16:23 +03:00
// Copyright © 2021 Encrypted Ink. All rights reserved.
import Cocoa
2021-06-13 13:13:47 +03:00
import WalletConnect
2021-06-13 15:30:19 +03:00
import LocalAuthentication
2021-06-12 19:16:23 +03:00
class Agent: NSObject {
2021-06-13 05:45:54 +03:00
2021-06-13 06:30:20 +03:00
static let shared = Agent()
2021-06-13 13:53:38 +03:00
private lazy var statusImage = NSImage(named: "Status")
2021-06-12 19:16:23 +03:00
private override init() { super.init() }
2021-06-13 13:13:47 +03:00
private var statusBarItem: NSStatusItem!
2021-06-19 00:38:05 +03:00
private var hasPassword = Keychain.password != nil
private var didEnterPasswordOnStart = false
var statusBarButtonIsBlocked = false
2021-06-13 06:30:20 +03:00
2021-06-12 19:16:23 +03:00
func start() {
checkPasteboardAndOpen()
2021-06-28 16:20:08 +03:00
setupStatusBarItem()
2021-06-13 05:45:54 +03:00
}
func reopen() {
checkPasteboardAndOpen()
2021-06-13 05:45:54 +03:00
}
2021-06-24 23:35:57 +03:00
func showInitialScreen(wcSession: WCSession?) {
2021-06-19 00:38:05 +03:00
guard hasPassword else {
let welcomeViewController = WelcomeViewController.new { [weak self] createdPassword in
guard createdPassword else { return }
self?.didEnterPasswordOnStart = true
2021-06-19 00:38:05 +03:00
self?.hasPassword = true
2021-06-24 23:35:57 +03:00
self?.showInitialScreen(wcSession: wcSession)
2021-06-19 00:38:05 +03:00
}
let windowController = Window.showNew()
2021-06-19 00:38:05 +03:00
windowController.contentViewController = welcomeViewController
return
}
guard didEnterPasswordOnStart else {
askAuthentication(on: nil, onStart: true, reason: "Start") { [weak self] success in
if success {
self?.didEnterPasswordOnStart = true
2021-06-24 23:35:57 +03:00
self?.showInitialScreen(wcSession: wcSession)
}
}
return
}
let windowController = Window.showNew()
2021-06-17 23:11:03 +03:00
let completion = onSelectedAccount(session: wcSession)
2021-06-13 05:45:54 +03:00
let accounts = AccountsService.getAccounts()
if !accounts.isEmpty {
2021-06-13 06:30:20 +03:00
let accountsList = AccountsListViewController.with(preloadedAccounts: accounts)
2021-06-17 23:11:03 +03:00
accountsList.onSelectedAccount = completion
2021-06-13 06:30:20 +03:00
windowController.contentViewController = accountsList
2021-06-13 05:45:54 +03:00
} else {
2021-06-13 06:30:20 +03:00
let importViewController = instantiate(ImportViewController.self)
2021-06-17 23:11:03 +03:00
importViewController.onSelectedAccount = completion
2021-06-13 06:30:20 +03:00
windowController.contentViewController = importViewController
2021-06-13 05:45:54 +03:00
}
2021-06-12 19:16:23 +03:00
}
func showApprove(transaction: Transaction, completion: @escaping (Transaction?) -> Void) {
let windowController = Window.showNew()
let approveViewController = ApproveTransactionViewController.with(transaction: transaction) { [weak self] transaction in
if transaction != nil {
self?.askAuthentication(on: windowController.window, onStart: false, reason: Strings.sendTransaction) { success in
completion(success ? transaction : nil)
Window.closeAllAndActivateBrowser()
}
} else {
Window.closeAllAndActivateBrowser()
completion(nil)
}
}
windowController.contentViewController = approveViewController
}
func showApprove(title: String, meta: String, completion: @escaping (Bool) -> Void) {
2021-06-13 09:32:07 +03:00
let windowController = Window.showNew()
2021-06-13 15:30:19 +03:00
let approveViewController = ApproveViewController.with(title: title, meta: meta) { [weak self] result in
if result {
self?.askAuthentication(on: windowController.window, onStart: false, reason: title) { success in
completion(success)
Window.closeAllAndActivateBrowser()
}
2021-06-13 15:30:19 +03:00
} else {
Window.closeAllAndActivateBrowser()
2021-06-13 15:30:19 +03:00
completion(result)
}
2021-06-13 09:32:07 +03:00
}
windowController.contentViewController = approveViewController
}
func showErrorMessage(_ message: String) {
let windowController = Window.showNew()
windowController.contentViewController = ErrorViewController.withMessage(message)
}
func processInputLink(_ link: String) {
let session = sessionWithLink(link)
2021-06-24 23:35:57 +03:00
showInitialScreen(wcSession: session)
}
2021-06-17 23:56:42 +03:00
func getAccountSelectionCompletionIfShouldSelect() -> ((Account) -> Void)? {
let session = getSessionFromPasteboard()
return onSelectedAccount(session: session)
}
lazy private var statusBarMenu: NSMenu = {
let menu = NSMenu(title: "Encrypted Ink")
let showItem = NSMenuItem(title: "Show Encrypted Ink", action: #selector(didSelectShowMenuItem), keyEquivalent: "")
let quitItem = NSMenuItem(title: "Quit", action: #selector(didSelectQuitMenuItem), keyEquivalent: "")
showItem.target = self
quitItem.target = self
menu.delegate = self
menu.addItem(showItem)
menu.addItem(quitItem)
return menu
}()
2021-06-23 22:21:23 +03:00
func warnBeforeQuitting(updateStatusBarAfterwards: Bool = false) {
Window.activateWindow(nil)
let alert = Alert()
alert.messageText = "Quit Encrypted Ink?"
2021-06-19 19:39:43 +03:00
alert.informativeText = "You won't be able to sign requests."
alert.alertStyle = .warning
alert.addButton(withTitle: "OK")
alert.addButton(withTitle: "Cancel")
if alert.runModal() == .alertFirstButtonReturn {
NSApp.terminate(nil)
}
2021-06-23 22:21:23 +03:00
if updateStatusBarAfterwards {
setupStatusBarItem()
}
}
@objc private func didSelectShowMenuItem() {
checkPasteboardAndOpen()
}
2021-06-23 21:28:52 +03:00
@objc private func didSelectQuitMenuItem() {
warnBeforeQuitting()
}
func setupStatusBarItem() {
let statusBar = NSStatusBar.system
statusBarItem = statusBar.statusItem(withLength: NSStatusItem.squareLength)
statusBarItem.button?.image = statusImage
statusBarItem.button?.target = self
statusBarItem.button?.action = #selector(statusBarButtonClicked(sender:))
statusBarItem.button?.sendAction(on: [.leftMouseUp, .rightMouseUp])
}
@objc private func statusBarButtonClicked(sender: NSStatusBarButton) {
guard !statusBarButtonIsBlocked, let event = NSApp.currentEvent, event.type == .rightMouseUp || event.type == .leftMouseUp else { return }
if let session = getSessionFromPasteboard() {
showInitialScreen(wcSession: session)
} else {
statusBarItem.menu = statusBarMenu
statusBarItem.button?.performClick(nil)
}
}
2021-06-17 23:11:03 +03:00
private func onSelectedAccount(session: WCSession?) -> ((Account) -> Void)? {
guard let session = session else { return nil }
return { [weak self] account in
self?.connectWallet(session: session, account: account)
}
}
private func getSessionFromPasteboard() -> WCSession? {
2021-06-13 13:13:47 +03:00
let pasteboard = NSPasteboard.general
let link = pasteboard.string(forType: .string) ?? ""
let session = sessionWithLink(link)
if session != nil {
pasteboard.clearContents()
}
2021-06-17 23:11:03 +03:00
return session
}
private func checkPasteboardAndOpen() {
2021-06-17 23:11:03 +03:00
let session = getSessionFromPasteboard()
2021-06-24 23:35:57 +03:00
showInitialScreen(wcSession: session)
}
private func sessionWithLink(_ link: String) -> WCSession? {
return WalletConnect.shared.sessionWithLink(link)
}
func askAuthentication(on: NSWindow?, getBackTo: NSViewController? = nil, onStart: Bool, reason: String, completion: @escaping (Bool) -> Void) {
2021-06-13 15:30:19 +03:00
let context = LAContext()
var error: NSError?
let policy = LAPolicy.deviceOwnerAuthenticationWithBiometrics
let canDoLocalAuthentication = context.canEvaluatePolicy(policy, error: &error)
func showPasswordScreen() {
let window = on ?? Window.showNew().window
let passwordViewController = PasswordViewController.with(mode: .enter, reason: reason) { [weak window] success in
if let getBackTo = getBackTo {
window?.contentViewController = getBackTo
} else {
Window.closeAll()
}
2021-06-13 15:30:19 +03:00
completion(success)
}
window?.contentViewController = passwordViewController
}
if canDoLocalAuthentication {
context.localizedCancelTitle = "Cancel"
context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason ) { [weak self] success, _ in
DispatchQueue.main.async {
if !success, onStart, self?.didEnterPasswordOnStart == false {
showPasswordScreen()
}
completion(success)
}
}
} else {
showPasswordScreen()
2021-06-13 15:30:19 +03:00
}
}
2021-06-13 13:13:47 +03:00
private func connectWallet(session: WCSession, account: Account) {
WalletConnect.shared.connect(session: session, address: account.address) { [weak self] connected in
if connected {
2021-06-19 19:38:51 +03:00
Window.closeAllAndActivateBrowser()
} else {
self?.showErrorMessage("Failed to connect")
}
2021-06-13 06:30:20 +03:00
}
2021-06-13 07:01:01 +03:00
let windowController = Window.showNew()
windowController.contentViewController = WaitingViewController.withReason("Connecting")
2021-06-13 06:30:20 +03:00
}
2021-06-12 19:16:23 +03:00
}
extension Agent: NSMenuDelegate {
func menuDidClose(_ menu: NSMenu) {
statusBarItem.menu = nil
}
}