Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : QYVORA7// File : App/Sources/Security/PINSetupSheet.swift8// Module : Security9// Repository : https://git.spboucher.ai/qyvora10// Project Website : Not specified11// Created : 2026-12-0412// Last Modified : 2026-12-0513//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 SwiftUI42import QyvoraVault43import QyvoraWallet44import QyvoraUI4546/// Sets (or replaces) the PIN: six digits typed twice, and the optional policy that erases the vault from this47/// device after a number of consecutive wrong PINs. Presented after a fresh authentication.48struct PINSetupSheet: View {49 enum Step { case first, repeated, policy, done }5051 @Environment(WalletStore.self) private var store52 @Environment(\.dismiss) private var dismiss53 @State private var step: Step = .first54 @State private var pin = ""55 @State private var first = ""56 @State private var wipeAfter: Int?57 @State private var errorCount = 058 @State private var message: String?59 @State private var error: String?60 @State private var busy = false6162 static let wipeChoices: [Int?] = [nil, 8, 10, 15]6364 var body: some View {65 ScrollView {66 VStack(alignment: .leading, spacing: 18) {67 SheetHeader("PIN", subtitle: "A PIN of \(ReauthenticationSheet.pinLength) digits opens the vault on this device only. It never replaces the master password.") { dismiss() }68 switch step {69 case .first:70 PINPad(pin: $pin, length: ReauthenticationSheet.pinLength, title: "Choose a PIN", message: message, errorCount: errorCount) { entered in71 first = entered72 pin = ""73 message = nil74 step = .repeated75 }76 .frame(maxWidth: .infinity)77 case .repeated:78 PINPad(pin: $pin, length: ReauthenticationSheet.pinLength, title: "Repeat the PIN", message: message, errorCount: errorCount) { entered in79 if entered == first {80 pin = ""81 step = .policy82 } else {83 pin = ""84 first = ""85 errorCount += 186 message = "The two entries differ. Start again."87 step = .first88 }89 }90 .frame(maxWidth: .infinity)91 case .policy:92 policy93 case .done:94 WarningBanner(.positive, title: "PIN enabled", message: "Wrong PINs are delayed with escalating waiting times: none for the first three, then from one minute to a day.")95 Button("Done") { dismiss() }.buttonStyle(.qPrimary)96 }97 SecInlineError(error)98 }99 .padding(20)100 .frame(maxWidth: 520, alignment: .leading)101 .frame(maxWidth: .infinity)102 }103 .qScreen()104 .privacySensitive()105 .interactiveDismissDisabled(busy)106 .sensoryFeedback(.success, trigger: step == .done)107 .onDisappear {108 pin = ""109 first = ""110 }111 }112113 @ViewBuilder var policy: some View {114 QListSection("After repeated wrong PINs", separatorInset: QMetric.padding) {115 ForEach(Self.wipeChoices, id: \.self) { choice in116 Button { wipeAfter = choice } label: {117 HStack(spacing: QSpace.m) {118 Image(systemName: wipeAfter == choice ? "largecircle.fill.circle" : "circle")119 .font(.title3)120 .foregroundStyle(wipeAfter == choice ? QColor.violet : QColor.strokeStrong)121 .accessibilityHidden(true)122 Text(choice.map { "Erase the vault after \($0) wrong PINs" } ?? "Never erase (delays only)")123 .font(QFont.body).foregroundStyle(QColor.textPrimary)124 .fixedSize(horizontal: false, vertical: true)125 Spacer(minLength: 0)126 }127 .padding(.vertical, QSpace.m)128 .frame(minHeight: QMetric.rowMinHeight)129 }130 .buttonStyle(.qRow)131 .accessibilityAddTraits(wipeAfter == choice ? [.isSelected, .isButton] : [.isButton])132 }133 }134 .qHaptic(.selection, trigger: wipeAfter)135 if wipeAfter != nil {136 WarningBanner(.warning, title: "Erasing is irreversible",137 message: "After that many consecutive wrong PINs the vault is erased from this device. The funds remain reachable only with the recovery phrase. Check that your backup of the phrase is verified first.")138 }139 LoadingButton("Enable PIN", loadingTitle: "Deriving the PIN key", systemImage: "circle.grid.3x3", state: busy ? .loading : .idle) { enable() }140 }141142 func enable() {143 guard !busy else { return }144 if let rejection = WalletService.pinRejection(first) {145 error = rejection146 return147 }148 busy = true149 error = nil150 let value = first, wipe = wipeAfter151 Task {152 do {153 try await store.run { try await $0.enablePIN(value, wipeAfterFailedAttempts: wipe) }154 first = ""155 step = .done156 } catch {157 self.error = SecurityError.text(error)158 }159 busy = false160 }161 }162}163