SPB Git

Repository Public

NewPasswordForm.swift 5.7 KB 126 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : QYVORA7// File             : App/Sources/Onboarding/NewPasswordForm.swift8// Module           : Onboarding9// Repository       : https://git.spboucher.ai/qyvora10// Project Website  : Not specified11// Created          : 2026-12-0412// Last Modified    : 2026-12-0413//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/// Entry of a new master password with its confirmation and the strength measured by the wallet service.47/// Rule: the one of the wallet service (`WalletService.passwordRejection`): at least ten characters and a "fair" estimate.48struct NewPasswordForm: View {49    @Binding var password: String50    @Binding var confirmation: String51    @Environment(WalletStore.self) private var wallet5253    var body: some View {54        VStack(alignment: .leading, spacing: 16) {55            PasswordField("Master password", text: $password, strength: strength, isNewPassword: true)56            PasswordField("Repeat the password", text: $confirmation, isNewPassword: true)57            if let problem = Self.problem(password: password, confirmation: confirmation, estimate: estimate), !password.isEmpty {58                InlineError(message: problem)59            }60        }61    }6263    var estimate: PasswordQuality.Estimate? {64        password.isEmpty ? nil : wallet.service?.passwordQuality(password)65    }6667    var strength: PasswordStrength? {68        estimate.map { PasswordStrength(level: $0.level.rawValue, entropyBits: $0.entropyBits, advice: Self.advice($0)) }69    }7071    /// Why the pair cannot be used yet; nil when it can.72    static func problem(password: String, confirmation: String, estimate: PasswordQuality.Estimate?) -> String? {73        guard let estimate else { return "Enter a password." }74        _ = estimate75        if let reason = WalletService.passwordRejection(password) { return reason }76        if confirmation.isEmpty { return "Repeat the password to confirm it." }77        if confirmation != password { return "The two entries differ." }78        return nil79    }8081    static func advice(_ estimate: PasswordQuality.Estimate) -> String? {82        guard let first = estimate.warnings.first else { return nil }83        switch first {84        case .tooShort: return "Too short: length matters more than anything else."85        case .singleCharacterClass: return "Only one kind of character is used."86        case .commonPassword: return "This is a commonly used password."87        case .repeatedCharacters: return "Repeated characters add little."88        case .sequentialCharacters: return "Sequences such as abc or 123 are guessed first."89        case .keyboardPattern: return "Keyboard patterns are guessed first."90        case .yearOrDate: return "Years and dates are guessed first."91        case .mostlyDigits: return "Mostly digits: add words."92        }93    }94}9596/// Optional BIP-39 passphrase behind an "Advanced" disclosure.97struct PassphraseDisclosure: View {98    @Binding var passphrase: String99    let isImport: Bool100    @State private var expanded = false101102    var body: some View {103        DisclosureGroup(isExpanded: $expanded) {104            VStack(alignment: .leading, spacing: 12) {105                WarningBanner(.warning, title: "A passphrase is a second secret",106                              message: isImport107                                ? "Enter it only if the wallet was created with one. A different passphrase opens a different, empty wallet without any error."108                                : "It is not stored in the recovery phrase. Without both the 24 words and the exact passphrase the funds cannot be recovered, and nobody can reset it. Leave empty unless you know why you need it.")109                SecureField("BIP-39 passphrase (optional)", text: $passphrase)110                    .textContentType(.none)111                    .textInputAutocapitalization(.never)112                    .autocorrectionDisabled()113                    .keyboardType(.asciiCapable)114                    .padding(14)115                    .background(QColor.surface, in: RoundedRectangle(cornerRadius: QMetric.radiusSmall))116                    .overlay(RoundedRectangle(cornerRadius: QMetric.radiusSmall).strokeBorder(QColor.stroke))117                    .accessibilityLabel("BIP-39 passphrase, optional")118            }119            .padding(.top, 12)120        } label: {121            Text("Advanced").font(QFont.headline).foregroundStyle(QColor.textPrimary)122        }123        .tint(QColor.textSecondary)124    }125}126
Files main
Menu

Theme