SPB Git

Repository Public

ARCHITECTURE.md 5.1 KB

# Architecture

SP Vault is one iOS app target with no dependency outside Apple's SDK: Swift 6 (strict concurrency), SwiftUI, a little UIKit, AVFoundation, Vision, VisionKit, Core Image, Metal (SwiftUI shaders), Core Motion, Core Haptics, CryptoKit, Security, LocalAuthentication, UserNotifications. Minimum iOS 26, iPhone, portrait. The project file is generated from project.yml with XcodeGen and committed.

# Layers

text
App/            SPVaultApp, AppEnvironment (object graph), RootView (state → screen), AppRouter, AppSettings
Core/
  Cryptography/ SealedBlob (SPV1), VaultCrypto, KDFParameters, PassphrasePolicy
  Biometrics/   KeyProtector protocol + Keychain / simulator / scripted implementations
  Storage/      Models, VaultRepository (encrypted files), CardMigrator, SearchIndex
  Security/     Vault (the security engine), SensitiveDataPolicy
  Privacy/      PrivacyShield (curtain window, capture and screenshot detection)
Engines/
  SmartScan/    detection, quality gates, perspective correction, OCR, parser, security-code redaction
  ImageProcessing/ CardImageCodec (re-encode, thumbnails, dominant colour) and scan image pipeline
  VirtualCard/  VirtualCardView, FlippableCardView, RawCardFace, DemoCardFactory (Debug / simulator)
  Backup/       BackupArchive (.spvault)
DesignSystem/   tokens, components, Metal shaders, haptics, motion
Features/       Onboarding, Lock, Shell, Wallet, Scanner, Cards, Search, Expirations, SecurityCenter, Settings

Dependencies point downwards: features use engines and core; core knows nothing about SwiftUI views (except Vault, which decodes images with UIKit).

# The security engine

Vault (@MainActor @Observable) is the single owner of secrets and the only path to card data.

text
            bootstrap()                unlock() ── Face ID ── Keychain ── key
 loading ───────────────► noVault ──createVault()──┐
    │                                              ▼
    └────────► locked ◄──────── lock() ──────── unlocked
                  │                                 ▲
                  └── key gone ──► keyInvalidated ──┘ recover(passphrase)
  • unlock() reads the master key through a KeyProtector; the manifest must authenticate with it before the state changes.
  • While unlocked, Vault holds the key, the decrypted Card records, the SearchIndex and an image cache. lock() drops them all.
  • Mutations (add, update, delete, setImage, move, categories, backup, restore) go through VaultRepository, a stateless Sendable value that takes the key as an argument and writes only sealed blobs. Heavy work (image codec, PBKDF2, full loads) runs in detached tasks.
  • reauthenticate() reads the Keychain item again and compares keys; it gates number reveal, export, recovery changes and erasure.

RootView switches on vault.state. The unlocked shell (MainShellView, its AppRouter, every sheet and cover) exists only in the .unlocked branch, so locking removes it from the hierarchy; there is no route that could show content afterwards, and there would be nothing decrypted to show.

# Lock and privacy

  • Scene phase .background → vault.lock().
  • UIApplication.willResignActive → PrivacyShield raises an opaque window at .alert + 1, above sheets and covers, before the system takes the switcher snapshot.
  • isSceneCaptured (recording or mirroring) keeps the curtain up.
  • userDidTakeScreenshotNotification → in-app banner and a generic local notification.

# Scanning pipeline (SmartScan)

Camera frame → rectangle detection → quality gates (sharpness, glare, light, stability) → high-resolution still → perspective correction and crop → optional restrained enhancement → on-device OCR → CardTextParser proposals → for bank-card backs, security-code detection and irreversible redaction with manual validation → ScanDraft (bitmaps in memory) → CardEditorView → CardImageCodec → Vault.add. Nothing in this chain touches the disk or Photos before encryption.

# Data flow of one card

text
ScanDraft (CGImage ×2, proposals)
   └─ editor → Card record
        └─ CardImageCodec: HEIC/JPEG re-encode (metadata dropped), thumbnail
             └─ VaultRepository.insert: new card key, 6 sealed blobs in a staging directory, atomic rename
                  └─ manifest (order) and search index re-sealed

# Testing hooks

Debug builds accept launch arguments (-uiTesting, -seedDemo, -screen, -biometry, -layout, -privacyMode, -keepVault) that select a temporary vault, the scripted key protector and fictitious demo cards. They are parsed inside #if DEBUG; the Release binary contains neither the parser nor the test protectors nor the demo cards.

# Build

scripts/build.sh debug | release | unit | uitest | test, scripts/shot.sh <name> [launch args] (simulator screenshot of a screen), scripts/make_icon.py, scripts/apple_dev_setup.applescript + scripts/portal/*.js (App ID registration through the signed-in Safari session).

Files main
Menu

Theme