| 1 | import UIKit |
| 2 | import Flutter |
| 3 | |
| 4 | @main |
| 5 | @objc class AppDelegate: FlutterAppDelegate, FlutterImplicitEngineDelegate { |
| 6 | override func application( |
| 7 | _ application: UIApplication, |
| 8 | didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? |
| 9 | ) -> Bool { |
| 10 | if #available(iOS 10.0, *) { |
| 11 | UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate |
| 12 | } |
| 13 | |
| 14 | DispatchQueue.main.async { [weak self] in |
| 15 | self?.makeSecure() |
| 16 | } |
| 17 | |
| 18 | |
| 19 | return super.application(application, didFinishLaunchingWithOptions: launchOptions) |
| 20 | } |
| 21 | |
| 22 | |
| 23 | func didInitializeImplicitFlutterEngine(_ engineBridge: FlutterImplicitEngineBridge) { |
| 24 | GeneratedPluginRegistrant.register(with: engineBridge.pluginRegistry) |
| 25 | |
| 26 | let messenger = engineBridge.applicationRegistrar.messenger() |
| 27 | |
| 28 | let legacyMigrationChannel = FlutterMethodChannel( |
| 29 | name: "com.cakewallet.cakewallet/legacy_wallet_migration", |
| 30 | binaryMessenger: messenger) |
| 31 | legacyMigrationChannel.setMethodCallHandler({ |
| 32 | (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in |
| 33 | |
| 34 | switch call.method { |
| 35 | case "decrypt": |
| 36 | guard let args = call.arguments as? Dictionary<String, Any>, |
| 37 | let data = args["bytes"] as? FlutterStandardTypedData, |
| 38 | let key = args["key"] as? String, |
| 39 | let salt = args["salt"] as? String else { |
| 40 | result(nil) |
| 41 | return |
| 42 | } |
| 43 | |
| 44 | let content = decrypt(data: data.data, key: key, salt: salt) |
| 45 | result(content) |
| 46 | case "read_user_defaults": |
| 47 | guard let args = call.arguments as? Dictionary<String, Any>, |
| 48 | let key = args["key"] as? String, |
| 49 | let type = args["type"] as? String else { |
| 50 | result(nil) |
| 51 | return |
| 52 | } |
| 53 | |
| 54 | var value: Any? |
| 55 | |
| 56 | switch (type) { |
| 57 | case "string": |
| 58 | value = UserDefaults.standard.string(forKey: key) |
| 59 | case "int": |
| 60 | value = UserDefaults.standard.integer(forKey: key) |
| 61 | case "bool": |
| 62 | value = UserDefaults.standard.bool(forKey: key) |
| 63 | default: |
| 64 | break |
| 65 | } |
| 66 | |
| 67 | result(value) |
| 68 | default: |
| 69 | result(FlutterMethodNotImplemented) |
| 70 | } |
| 71 | }) |
| 72 | |
| 73 | let utilsChannel = FlutterMethodChannel( |
| 74 | name: "com.cake_wallet/native_utils", |
| 75 | binaryMessenger: messenger) |
| 76 | utilsChannel.setMethodCallHandler({ [weak self] (call: FlutterMethodCall, result: @escaping FlutterResult) -> Void in |
| 77 | switch call.method { |
| 78 | case "sec_random": |
| 79 | guard let args = call.arguments as? Dictionary<String, Any>, |
| 80 | let count = args["count"] as? Int else { |
| 81 | result(nil) |
| 82 | return |
| 83 | } |
| 84 | |
| 85 | result(secRandom(count: count)) |
| 86 | |
| 87 | case "setIsAppSecure": |
| 88 | guard let args = call.arguments as? Dictionary<String, Bool>, |
| 89 | let isAppSecure = args["isAppSecure"] else { |
| 90 | result(nil) |
| 91 | return |
| 92 | } |
| 93 | |
| 94 | if isAppSecure { |
| 95 | self?.textField.isSecureTextEntry = true |
| 96 | } else { |
| 97 | self?.textField.isSecureTextEntry = false |
| 98 | } |
| 99 | |
| 100 | result(nil) |
| 101 | default: |
| 102 | result(FlutterMethodNotImplemented) |
| 103 | } |
| 104 | }) |
| 105 | } |
| 106 | |
| 107 | private var textField = UITextField() |
| 108 | |
| 109 | private func keyWindow() -> UIWindow? { |
| 110 | UIApplication.shared.connectedScenes |
| 111 | .compactMap { $0 as? UIWindowScene } |
| 112 | .flatMap { $0.windows } |
| 113 | .first { $0.isKeyWindow } |
| 114 | } |
| 115 | |
| 116 | private func makeSecure() { |
| 117 | guard let window = self.window ?? keyWindow() else { return } |
| 118 | |
| 119 | if (!window.subviews.contains(textField)) { |
| 120 | let view = UIView(frame: CGRect(x: 0, y: 0, width: textField.frame.self.width, height: textField.frame.self.height)) |
| 121 | window.addSubview(textField) |
| 122 | window.layer.superlayer?.addSublayer(textField.layer) |
| 123 | |
| 124 | textField.layer.sublayers?.last?.addSublayer(window.layer) |
| 125 | textField.leftView = view |
| 126 | textField.leftViewMode = .always |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | } |