Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : SP ID & Card Vault7// File : SPVault/App/AppEnvironment.swift8// Module : SPVault9// Repository : https://git.spboucher.ai/sp-id-card-vault10// Project Website : Not specified11// Created : 2026-12-0212// Last Modified : 2026-12-0213//14// Author : Simon-Pierre Boucher15// Position : Professor of Finance16// Institution : Université du Québec en Outaouais (UQO)17// Organization : SP Boucher18// Website : https://spboucher.ai19// Git : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development : dev@spboucher.ai23// Support : support@spboucher.ai24// Security/Admin : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License : None specified (no license file in this repository)33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import Foundation42import Observation43import UIKit4445/// Launch switches used by tests and screenshot runs. They exist only in Debug46/// builds; a Release binary ignores every argument and always uses the real47/// Keychain protector and the real vault directory.48struct LaunchOptions: Sendable {49 var isUITesting = false50 var seedDemo = false51 var keepVault = false52 /// Screen to open directly after unlock (screenshot runs).53 var screen: String?54 var biometryScript: String?55 var layout: String?56 var privacyMode = false5758 static func current() -> LaunchOptions {59 var options = LaunchOptions()60 #if DEBUG61 let arguments = ProcessInfo.processInfo.arguments62 func value(after flag: String) -> String? {63 guard let index = arguments.firstIndex(of: flag), index + 1 < arguments.count else { return nil }64 return arguments[index + 1]65 }66 options.isUITesting = arguments.contains("-uiTesting")67 options.seedDemo = arguments.contains("-seedDemo")68 options.keepVault = arguments.contains("-keepVault")69 options.screen = value(after: "-screen")70 options.biometryScript = value(after: "-biometry")71 options.layout = value(after: "-layout")72 options.privacyMode = arguments.contains("-privacyMode")73 #endif74 return options75 }76}7778/// Object graph of the app, built once at launch.79@MainActor80@Observable81final class AppEnvironment {82 let vault: Vault83 let settings: AppSettings84 let reminders: ExpiryReminderScheduler85 let privacy: PrivacyShield86 let launch: LaunchOptions8788 init(vault: Vault, settings: AppSettings, launch: LaunchOptions) {89 self.vault = vault90 self.settings = settings91 self.launch = launch92 reminders = ExpiryReminderScheduler(settings: settings)93 privacy = PrivacyShield()94 vault.reminders = reminders95 privacy.onScreenshot = { [reminders] in reminders.notifyScreenshot() }96 }9798 static func make() -> AppEnvironment {99 let launch = LaunchOptions.current()100 #if DEBUG101 if launch.isUITesting {102 let root = URL.temporaryDirectory.appending(path: "SPVault-UITest", directoryHint: .isDirectory)103 if !launch.keepVault { try? FileManager.default.removeItem(at: root) }104 let protector = ScriptedKeyProtector()105 let defaults = UserDefaults(suiteName: "ai.spboucher.idcardvault.uitest")!106 if !launch.keepVault { defaults.removePersistentDomain(forName: "ai.spboucher.idcardvault.uitest") }107 let environment = AppEnvironment(vault: Vault(repository: VaultRepository(root: root), protector: protector),108 settings: AppSettings(defaults: defaults), launch: launch)109 environment.vault.kdfIterations = KDFParameters.minimumAcceptedIterations // tests only: keep UI runs fast110 environment.scriptedProtector = protector111 if let layout = launch.layout.flatMap(WalletLayout.init(rawValue:)) { environment.settings.walletLayout = layout }112 if launch.privacyMode { environment.settings.privacyMode = true }113 return environment114 }115 #endif116 return AppEnvironment(vault: Vault(), settings: AppSettings(), launch: launch)117 }118119 #if DEBUG120 @ObservationIgnored var scriptedProtector: ScriptedKeyProtector?121122 /// Debug only: creates a vault filled with the fictitious demo cards.123 func seedDemoVault() async {124 guard launch.seedDemo, vault.state == .noVault else { return }125 try? await vault.createVault(recoveryPassphrase: "correct horse battery staple 42")126 for demo in DemoCardFactory.all().reversed() {127 // Same rule as the scanner: a bank card's security code is blacked out128 // in the pixels before the photograph is encrypted.129 let back = demo.card.kind.isBanking ? demo.back.map { Redactor.redact($0, rects: [DemoCardFactory.bankBackCVVRect]) } : demo.back130 guard let images = try? CardImageCodec.images(front: demo.front, back: back) else { continue }131 try? await vault.add(demo.card, images: images)132 }133 switch launch.biometryScript {134 case "lock": vault.lock()135 case "denied": vault.lock(); scriptedProtector?.setNextError(.authenticationFailed)136 case "lockedOut": vault.lock(); scriptedProtector?.setState(.lockedOut)137 case "invalidated": vault.lock(); scriptedProtector?.invalidate(); vault.refreshLockState()138 default: break139 }140 }141 #endif142}143