Repository Public
1// SP Wallet iOS2// File: App/AppEnvironment.swift3// Description: The app's long-lived services: API client, domain APIs of SPWalletCore, the passkey provider, the offline cache.4// No private key exists anywhere in this process; the only secret kept is the session cookie, in the Keychain.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import Foundation9import SPWalletCore10#if DEBUG11import SPWalletTestSupport12#endif1314@MainActor15final class AppEnvironment {16 let configuration: AppConfiguration17 let client: APIClient18 let auth: AuthAPI19 let wallet: WalletAPI20 let transfers: TransfersAPI21 let passkeys: any PasskeyAuthenticator22 let cache: AppCache23 let preferences: Preferences2425 init(configuration: AppConfiguration = .load()) {26 self.configuration = configuration27 let tokens: SessionTokenStoring = configuration.ephemeral28 ? InMemoryTokenStore()29 : KeychainTokenStore(service: configuration.isProduction ? "ai.spboucher.spwallet.ios.session" : "ai.spboucher.spwallet.ios.session.dev")30 client = APIClient(configuration: configuration.api, tokens: tokens)31 auth = AuthAPI(client); wallet = WalletAPI(client); transfers = TransfersAPI(client)32 cache = AppCache(directory: configuration.ephemeral ? nil33 : AppCache.defaultDirectory().appendingPathComponent(configuration.isProduction ? "prod" : "dev", isDirectory: true))34 preferences = Preferences(suite: configuration.ephemeral ? nil : .standard)35 #if DEBUG36 if let dir = configuration.devStackDirectory {37 // throwaway stack: a software passkey stands in for the device's (which cannot be bound to "localhost")38 passkeys = SoftwareAuthenticator(origin: configuration.api.originHeader, file: dir.appendingPathComponent("software-passkey.json"))39 } else {40 passkeys = PlatformPasskeyAuthenticator()41 }42 #else43 passkeys = PlatformPasskeyAuthenticator()44 #endif45 }4647 #if DEBUG48 /** Throwaway stack only: perform the one-time owner setup when the stack is brand new. */49 func prepareDevStackIfNeeded() async {50 guard let dir = configuration.devStackDirectory, let soft = passkeys as? SoftwareAuthenticator,51 let token = try? String(contentsOf: dir.appendingPathComponent("setup-token"), encoding: .utf8) else { return }52 guard let state = try? await auth.state(), state.setupRequired else { return }53 try? await DevStackSetup.ensureOwner(client: client, authenticator: soft, setupToken: token.trimmingCharacters(in: .whitespacesAndNewlines))54 }55 #endif56}5758/** Device-local preferences (never synced, nothing sensitive). */59@MainActor @Observable60final class Preferences {61 @ObservationIgnored private let defaults: UserDefaults?62 /** Ask for Face ID / Touch ID / passcode when the app opens or comes back after a while. */63 var deviceLock: Bool { didSet { defaults?.set(deviceLock, forKey: "deviceLock") } }64 /** Show amounts in widgets (off by default: a widget is visible to anyone looking at the screen). */65 var widgetAmounts: Bool { didSet { defaults?.set(widgetAmounts, forKey: "widgetAmounts") } }66 /** Local notifications for confirmed server events. */67 var notifications: Bool { didSet { defaults?.set(notifications, forKey: "notifications") } }68 var hideSmallBalances: Bool { didSet { defaults?.set(hideSmallBalances, forKey: "hideSmallBalances") } }6970 init(suite: UserDefaults?) {71 defaults = suite72 deviceLock = suite?.object(forKey: "deviceLock") as? Bool ?? true73 widgetAmounts = suite?.bool(forKey: "widgetAmounts") ?? false74 notifications = suite?.bool(forKey: "notifications") ?? false75 hideSmallBalances = suite?.bool(forKey: "hideSmallBalances") ?? false76 }77}78