SPB Git

Repository Public

VerifyPhraseView.swift 5.8 KB 134 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : QYVORA7// File             : App/Sources/Security/VerifyPhraseView.swift8// Module           : Security9// 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 QyvoraCore43import QyvoraWallet44import QyvoraUI4546/// Verifies the written copy of the recovery phrase: the user types every word back and the vault compares them with47/// the stored phrase (`verifyPhraseBackup`). The app never reads the phrase to do this, which is why the check takes48/// the whole phrase rather than a few words.49struct VerifyPhraseView: View {50    enum Outcome: Equatable { case matches, differs }5152    @Environment(WalletStore.self) private var store53    @State private var words: [String] = []54    @State private var wordCount: Int?55    @State private var loadError: String?56    @State private var outcome: Outcome?57    @State private var error: String?58    @State private var busy = false5960    static let wordSet = Set(BIP39.wordList)6162    var complete: Bool { !words.isEmpty && words.allSatisfy { Self.wordSet.contains(SeedPhraseInput.normalizeWord($0)) } }6364    var body: some View {65        SecurityScroll {66            if outcome == .matches {67                ResultView(status: .sent, title: "Your written copy is correct",68                           message: "Every word matches the phrase in the vault. This is recorded for the Quantum Shield.")69                if store.snapshot?.profile.usesPassphrase == true {70                    WarningBanner(.warning, message: "This wallet also uses a BIP-39 passphrase, which this check does not cover. Make sure you can reproduce it exactly.")71                }72            } else if let loadError {73                UnavailableDataView(reason: loadError, title: "Cannot start the check") { Task { await prepare() } }74            } else if wordCount == nil {75                LoadingRow("Reading the phrase length")76            } else {77                Explanation("Type the \(words.count) words from your written copy, in order. Do not reveal the phrase on screen to copy it: the point is to test the paper.")78                SeedPhraseEntry(words: $words, isValidWord: { Self.wordSet.contains($0) },79                                suggestions: { prefix in Array(BIP39.wordList.lazy.filter { $0.hasPrefix(prefix) }.prefix(4)) })80                if outcome == .differs {81                    WarningBanner(.critical, title: "The words do not match",82                                  message: "At least one word or its position differs from the phrase in the vault. Check your written copy; reveal the phrase to correct it if needed.")83                }84                SecInlineError(error)85                LoadingButton("Verify", loadingTitle: "Comparing", systemImage: "checkmark.circle", state: busy ? .loading : .idle) { verify() }86                    .disabled(!complete)87            }88        }89        .navigationTitle("Verify written copy")90        .navigationBarTitleDisplayMode(.inline)91        .privacySensitive()92        .task { await prepare() }93        .onDisappear { words = words.map { _ in "" } }94        .onChange(of: words) { if outcome == .differs { outcome = nil } }95        .sensoryFeedback(trigger: outcome) { _, new in96            switch new {97            case .matches: return .success98            case .differs: return .error99            case nil: return nil100            }101        }102    }103104    /// The number of words is public information of the recovery sheet (no private material is requested).105    func prepare() async {106        guard wordCount == nil else { return }107        do {108            let count = try await store.run { try await $0.recoverySheet(ethereumAddressCount: 1).mnemonicWordCount }109            wordCount = count110            words = SeedPhraseEntry.emptyWords(count)111            loadError = nil112        } catch {113            loadError = SecurityError.text(error)114        }115    }116117    func verify() {118        guard complete, !busy else { return }119        busy = true120        error = nil121        let typed = words.map(SeedPhraseInput.normalizeWord)122        Task {123            do {124                let matches = try await store.run { try await $0.verifyPhraseBackup(words: typed) }125                outcome = matches ? .matches : .differs126                if matches { words = words.map { _ in "" } }127            } catch {128                self.error = SecurityError.text(error)129            }130            busy = false131        }132    }133}134
Files main
Menu

Theme