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
|
|
|
|
2021-06-19 14:07:57 +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
|
|
|
|
2021-06-19 14:07:57 +03:00
|
|
|
private override init() { super.init() }
|
2021-06-13 13:13:47 +03:00
|
|
|
private var statusBarItem: NSStatusItem!
|
2021-07-17 20:20:51 +03:00
|
|
|
private let accountsService = AccountsService.shared
|
|
|
|
private var hasPassword = Keychain.shared.password != nil
|
2021-06-19 19:22:18 +03:00
|
|
|
private var didEnterPasswordOnStart = false
|
2021-06-23 22:15:56 +03:00
|
|
|
var statusBarButtonIsBlocked = false
|
2021-06-13 06:30:20 +03:00
|
|
|
|
2021-06-12 19:16:23 +03:00
|
|
|
func start() {
|
2021-06-29 21:25:23 +03:00
|
|
|
checkPasteboardAndOpen()
|
2021-06-28 16:20:08 +03:00
|
|
|
setupStatusBarItem()
|
2021-06-13 05:45:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func reopen() {
|
2021-06-29 21:25:23 +03:00
|
|
|
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 }
|
2021-06-19 19:22:18 +03:00
|
|
|
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
|
|
|
}
|
2021-06-25 00:43:21 +03:00
|
|
|
let windowController = Window.showNew()
|
2021-06-19 00:38:05 +03:00
|
|
|
windowController.contentViewController = welcomeViewController
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-19 19:22:18 +03:00
|
|
|
guard didEnterPasswordOnStart else {
|
2021-06-25 00:43:21 +03:00
|
|
|
askAuthentication(on: nil, onStart: true, reason: "Start") { [weak self] success in
|
2021-06-19 19:22:18 +03:00
|
|
|
if success {
|
|
|
|
self?.didEnterPasswordOnStart = true
|
2021-06-24 23:35:57 +03:00
|
|
|
self?.showInitialScreen(wcSession: wcSession)
|
2021-07-10 22:54:55 +03:00
|
|
|
WalletConnect.shared.restartSessions()
|
2021-06-19 19:22:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-25 00:43:21 +03:00
|
|
|
let windowController = Window.showNew()
|
2021-06-17 23:11:03 +03:00
|
|
|
let completion = onSelectedAccount(session: wcSession)
|
2021-07-20 23:19:35 +03:00
|
|
|
let accountsList = instantiate(AccountsListViewController.self)
|
|
|
|
accountsList.onSelectedAccount = completion
|
|
|
|
windowController.contentViewController = accountsList
|
2021-06-12 19:16:23 +03:00
|
|
|
}
|
|
|
|
|
2021-07-02 23:00:00 +03:00
|
|
|
func showApprove(transaction: Transaction, peerMeta: WCPeerMeta?, completion: @escaping (Transaction?) -> Void) {
|
2021-06-28 16:15:53 +03:00
|
|
|
let windowController = Window.showNew()
|
2021-07-02 23:00:00 +03:00
|
|
|
let approveViewController = ApproveTransactionViewController.with(transaction: transaction, peerMeta: peerMeta) { [weak self] transaction in
|
2021-06-28 16:15:53 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-07-02 23:00:00 +03:00
|
|
|
func showApprove(title: String, meta: String, peerMeta: WCPeerMeta?, completion: @escaping (Bool) -> Void) {
|
2021-06-13 09:32:07 +03:00
|
|
|
let windowController = Window.showNew()
|
2021-07-02 23:00:00 +03:00
|
|
|
let approveViewController = ApproveViewController.with(title: title, meta: meta, peerMeta: peerMeta) { [weak self] result in
|
2021-06-13 15:30:19 +03:00
|
|
|
if result {
|
2021-06-25 00:43:21 +03:00
|
|
|
self?.askAuthentication(on: windowController.window, onStart: false, reason: title) { success in
|
2021-06-19 19:22:18 +03:00
|
|
|
completion(success)
|
|
|
|
Window.closeAllAndActivateBrowser()
|
|
|
|
}
|
2021-06-13 15:30:19 +03:00
|
|
|
} else {
|
2021-06-19 19:22:18 +03:00
|
|
|
Window.closeAllAndActivateBrowser()
|
2021-06-13 15:30:19 +03:00
|
|
|
completion(result)
|
|
|
|
}
|
2021-06-13 09:32:07 +03:00
|
|
|
}
|
|
|
|
windowController.contentViewController = approveViewController
|
|
|
|
}
|
|
|
|
|
2021-06-13 07:12:39 +03:00
|
|
|
func showErrorMessage(_ message: String) {
|
|
|
|
let windowController = Window.showNew()
|
|
|
|
windowController.contentViewController = ErrorViewController.withMessage(message)
|
|
|
|
}
|
|
|
|
|
2021-06-15 00:42:21 +03:00
|
|
|
func processInputLink(_ link: String) {
|
2021-06-15 23:52:33 +03:00
|
|
|
let session = sessionWithLink(link)
|
2021-06-24 23:35:57 +03:00
|
|
|
showInitialScreen(wcSession: session)
|
2021-06-15 00:42:21 +03:00
|
|
|
}
|
|
|
|
|
2021-06-17 23:56:42 +03:00
|
|
|
func getAccountSelectionCompletionIfShouldSelect() -> ((Account) -> Void)? {
|
|
|
|
let session = getSessionFromPasteboard()
|
|
|
|
return onSelectedAccount(session: session)
|
|
|
|
}
|
|
|
|
|
2021-06-19 14:07:57 +03:00
|
|
|
lazy private var statusBarMenu: NSMenu = {
|
|
|
|
let menu = NSMenu(title: "Encrypted Ink")
|
2021-06-29 21:21:21 +03:00
|
|
|
|
|
|
|
let showItem = NSMenuItem(title: "Show Encrypted Ink", action: #selector(didSelectShowMenuItem), keyEquivalent: "")
|
2021-07-17 19:05:59 +03:00
|
|
|
let howToItem = NSMenuItem(title: "How to WalletConnect?", action: #selector(showInstructionsAlert), keyEquivalent: "")
|
2021-07-06 21:08:06 +03:00
|
|
|
let mailItem = NSMenuItem(title: "Drop us a line…", action: #selector(didSelectMailMenuItem), keyEquivalent: "")
|
|
|
|
let githubItem = NSMenuItem(title: "View on GitHub…", action: #selector(didSelectGitHubMenuItem), keyEquivalent: "")
|
|
|
|
let quitItem = NSMenuItem(title: "Quit", action: #selector(didSelectQuitMenuItem), keyEquivalent: "q")
|
2021-07-15 01:47:10 +03:00
|
|
|
showItem.attributedTitle = NSAttributedString(string: "👀 Show Encrypted Ink", attributes: [.font: NSFont.systemFont(ofSize: 21, weight: .bold)])
|
2021-06-29 21:21:21 +03:00
|
|
|
|
|
|
|
showItem.target = self
|
2021-07-17 19:05:59 +03:00
|
|
|
howToItem.target = self
|
2021-07-06 19:59:45 +03:00
|
|
|
githubItem.target = self
|
|
|
|
mailItem.target = self
|
2021-06-19 14:07:57 +03:00
|
|
|
quitItem.target = self
|
2021-07-06 19:59:45 +03:00
|
|
|
|
2021-06-19 14:07:57 +03:00
|
|
|
menu.delegate = self
|
2021-06-29 21:21:21 +03:00
|
|
|
menu.addItem(showItem)
|
2021-07-06 19:59:45 +03:00
|
|
|
menu.addItem(NSMenuItem.separator())
|
2021-07-17 19:05:59 +03:00
|
|
|
menu.addItem(howToItem)
|
|
|
|
menu.addItem(NSMenuItem.separator())
|
2021-07-06 19:59:45 +03:00
|
|
|
menu.addItem(githubItem)
|
|
|
|
menu.addItem(mailItem)
|
|
|
|
menu.addItem(NSMenuItem.separator())
|
2021-06-19 14:07:57 +03:00
|
|
|
menu.addItem(quitItem)
|
|
|
|
return menu
|
|
|
|
}()
|
|
|
|
|
2021-06-23 22:21:23 +03:00
|
|
|
func warnBeforeQuitting(updateStatusBarAfterwards: Bool = false) {
|
2021-06-19 15:08:41 +03:00
|
|
|
Window.activateWindow(nil)
|
2021-06-23 22:15:56 +03:00
|
|
|
let alert = Alert()
|
2021-06-19 15:08:41 +03:00
|
|
|
alert.messageText = "Quit Encrypted Ink?"
|
2021-06-19 19:39:43 +03:00
|
|
|
alert.informativeText = "You won't be able to sign requests."
|
2021-06-19 15:08:41 +03:00
|
|
|
alert.alertStyle = .warning
|
|
|
|
alert.addButton(withTitle: "OK")
|
|
|
|
alert.addButton(withTitle: "Cancel")
|
|
|
|
if alert.runModal() == .alertFirstButtonReturn {
|
2021-06-22 20:10:07 +03:00
|
|
|
NSApp.terminate(nil)
|
2021-06-19 15:08:41 +03:00
|
|
|
}
|
2021-06-23 22:21:23 +03:00
|
|
|
if updateStatusBarAfterwards {
|
|
|
|
setupStatusBarItem()
|
|
|
|
}
|
2021-06-19 14:07:57 +03:00
|
|
|
}
|
|
|
|
|
2021-07-06 19:59:45 +03:00
|
|
|
@objc private func didSelectGitHubMenuItem() {
|
|
|
|
if let url = URL(string: "https://github.com/zeriontech/Encrypted-Ink") {
|
|
|
|
NSWorkspace.shared.open(url)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-17 19:05:59 +03:00
|
|
|
@objc private func showInstructionsAlert() {
|
|
|
|
Window.activateWindow(nil)
|
|
|
|
Alert.showWalletConnectInstructions()
|
|
|
|
}
|
|
|
|
|
2021-07-06 19:59:45 +03:00
|
|
|
@objc private func didSelectMailMenuItem() {
|
|
|
|
if let url = URL(string: "mailto:support@encrypted.ink") {
|
|
|
|
NSWorkspace.shared.open(url)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-29 21:21:21 +03:00
|
|
|
@objc private func didSelectShowMenuItem() {
|
2021-06-29 21:25:23 +03:00
|
|
|
checkPasteboardAndOpen()
|
2021-06-29 21:21:21 +03:00
|
|
|
}
|
|
|
|
|
2021-06-23 21:28:52 +03:00
|
|
|
@objc private func didSelectQuitMenuItem() {
|
|
|
|
warnBeforeQuitting()
|
|
|
|
}
|
|
|
|
|
2021-06-19 14:07:57 +03:00
|
|
|
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) {
|
2021-06-29 21:21:21 +03:00
|
|
|
guard !statusBarButtonIsBlocked, let event = NSApp.currentEvent, event.type == .rightMouseUp || event.type == .leftMouseUp else { return }
|
2021-06-29 21:25:23 +03:00
|
|
|
|
|
|
|
if let session = getSessionFromPasteboard() {
|
|
|
|
showInitialScreen(wcSession: session)
|
|
|
|
} else {
|
|
|
|
statusBarItem.menu = statusBarMenu
|
|
|
|
statusBarItem.button?.performClick(nil)
|
|
|
|
}
|
2021-06-19 14:07:57 +03:00
|
|
|
}
|
|
|
|
|
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) ?? ""
|
2021-06-15 23:52:33 +03:00
|
|
|
let session = sessionWithLink(link)
|
|
|
|
if session != nil {
|
|
|
|
pasteboard.clearContents()
|
|
|
|
}
|
2021-06-17 23:11:03 +03:00
|
|
|
return session
|
|
|
|
}
|
|
|
|
|
2021-06-29 21:25:23 +03:00
|
|
|
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)
|
2021-06-15 23:52:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private func sessionWithLink(_ link: String) -> WCSession? {
|
|
|
|
return WalletConnect.shared.sessionWithLink(link)
|
|
|
|
}
|
|
|
|
|
2021-06-25 00:43:21 +03:00
|
|
|
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()
|
2021-06-13 15:39:31 +03:00
|
|
|
var error: NSError?
|
2021-06-19 19:22:18 +03:00
|
|
|
let policy = LAPolicy.deviceOwnerAuthenticationWithBiometrics
|
|
|
|
let canDoLocalAuthentication = context.canEvaluatePolicy(policy, error: &error)
|
2021-06-13 15:39:31 +03:00
|
|
|
|
2021-06-25 00:43:21 +03:00
|
|
|
func showPasswordScreen() {
|
|
|
|
let window = on ?? Window.showNew().window
|
|
|
|
let passwordViewController = PasswordViewController.with(mode: .enter, reason: reason) { [weak window] success in
|
2021-06-19 19:22:18 +03:00
|
|
|
if let getBackTo = getBackTo {
|
2021-06-25 00:43:21 +03:00
|
|
|
window?.contentViewController = getBackTo
|
2021-06-19 19:22:18 +03:00
|
|
|
} else {
|
|
|
|
Window.closeAll()
|
|
|
|
}
|
2021-06-13 15:30:19 +03:00
|
|
|
completion(success)
|
|
|
|
}
|
2021-06-25 00:43:21 +03:00
|
|
|
window?.contentViewController = passwordViewController
|
2021-06-19 19:22:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if canDoLocalAuthentication {
|
|
|
|
context.localizedCancelTitle = "Cancel"
|
2021-06-25 00:43:21 +03:00
|
|
|
context.evaluatePolicy(.deviceOwnerAuthentication, localizedReason: reason ) { [weak self] success, _ in
|
2021-06-19 19:22:18 +03:00
|
|
|
DispatchQueue.main.async {
|
2021-06-25 00:43:21 +03:00
|
|
|
if !success, onStart, self?.didEnterPasswordOnStart == false {
|
|
|
|
showPasswordScreen()
|
2021-06-19 20:15:46 +03:00
|
|
|
}
|
2021-06-19 19:22:18 +03:00
|
|
|
completion(success)
|
|
|
|
}
|
|
|
|
}
|
2021-06-25 00:43:21 +03:00
|
|
|
} 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) {
|
2021-07-09 20:04:26 +03:00
|
|
|
let windowController = Window.showNew()
|
2021-07-09 20:13:59 +03:00
|
|
|
let window = windowController.window
|
2021-07-09 20:04:26 +03:00
|
|
|
windowController.contentViewController = WaitingViewController.withReason("Connecting")
|
|
|
|
|
2021-07-09 20:13:59 +03:00
|
|
|
WalletConnect.shared.connect(session: session, address: account.address) { [weak window] _ in
|
|
|
|
if window?.isVisible == true {
|
2021-06-19 19:38:51 +03:00
|
|
|
Window.closeAllAndActivateBrowser()
|
2021-06-13 07:12:39 +03:00
|
|
|
}
|
2021-06-13 06:30:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-12 19:16:23 +03:00
|
|
|
}
|
2021-06-19 14:07:57 +03:00
|
|
|
|
|
|
|
extension Agent: NSMenuDelegate {
|
|
|
|
|
|
|
|
func menuDidClose(_ menu: NSMenu) {
|
|
|
|
statusBarItem.menu = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|