SPB Git

Repository Public

IdentityVerifyView.swift 7.1 KB 156 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : QYVORA7// File             : App/Sources/Security/IdentityVerifyView.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 QyvoraIdentity43import QyvoraWallet44import QyvoraUI4546/// Verifies a message and its signature against a public identity that was pasted or scanned. Pure computation:47/// no vault, no network.48struct IdentityVerifyView: View {49    enum Outcome: Equatable {50        case valid(fingerprint: String)51        case invalid52        case malformed(String)53    }5455    @Environment(WalletStore.self) private var store56    @State private var message = ""57    @State private var signature = ""58    @State private var signer = ""59    @State private var requireDual = false60    @State private var outcome: Outcome?61    @State private var busy = false62    @State private var scanning = false6364    var canVerify: Bool { !message.isEmpty && !signature.isEmpty && !signer.isEmpty }6566    var body: some View {67        SecurityScroll {68            Explanation("Paste the exact message, its signature and the signer's public identity. A valid result means this identity signed this text; whether the identity belongs to the person you think is established by comparing fingerprints.")69            field("Message", text: $message, minHeight: 90, mono: false)70            field("Signature (base64 or hexadecimal)", text: $signature, minHeight: 70, mono: true)71            VStack(alignment: .leading, spacing: 6) {72                field("Signer identity (qyvora-id:…)", text: $signer, minHeight: 70, mono: true)73                Button { scanning = true } label: { Label("Scan the identity", systemImage: "qrcode.viewfinder") }74                    .buttonStyle(.qSecondary)75            }76            QListSection(footnote: "When on, a signature without its SLH-DSA part is rejected.") {77                SecToggleRow(title: "Require the dual signature", systemImage: "checkmark.seal", isOn: $requireDual)78            }79            LoadingButton("Verify", loadingTitle: "Verifying", systemImage: "checkmark.seal", state: busy ? .loading : .idle) { verify() }80                .disabled(!canVerify)81            if let outcome { result(outcome) }82        }83        .navigationTitle("Verify a signature")84        .navigationBarTitleDisplayMode(.inline)85        .sheet(isPresented: $scanning) {86            PayloadScannerSheet(title: "Scan an identity", prompt: "Keep the animated code in the frame") { frames in87                if let text = PayloadText.text(from: frames) { signer = text }88            }89        }90        .onChange(of: message) { outcome = nil }91        .onChange(of: signature) { outcome = nil }92        .onChange(of: signer) { outcome = nil }93        .sensoryFeedback(trigger: outcome) { _, new in94            switch new {95            case .valid: return .success96            case .invalid, .malformed: return .error97            case nil: return nil98            }99        }100    }101102    func field(_ label: String, text: Binding<String>, minHeight: CGFloat, mono: Bool) -> some View {103        VStack(alignment: .leading, spacing: 6) {104            QEyebrow(label)105            TextEditor(text: text)106                .font(mono ? QFont.mono : QFont.body)107                .textInputAutocapitalization(.never)108                .autocorrectionDisabled(mono)109                .frame(minHeight: minHeight)110                .scrollContentBackground(.hidden)111                .padding(8)112                .background(QColor.surface, in: RoundedRectangle(cornerRadius: QMetric.radiusSmall, style: .continuous))113                .overlay(RoundedRectangle(cornerRadius: QMetric.radiusSmall, style: .continuous).strokeBorder(QColor.stroke, lineWidth: 1))114                .accessibilityLabel(label)115        }116    }117118    @ViewBuilder func result(_ outcome: Outcome) -> some View {119        switch outcome {120        case .valid(let fingerprint):121            QListSection("Result", footnote: "Signed by the identity with this fingerprint. Compare it with the one your contact shows.", separatorInset: QMetric.padding) {122                SecTextRow(text: "Valid signature", systemImage: QTone.positive.systemImage, tone: .positive)123                FingerprintView(text: fingerprint).padding(.vertical, QSpace.m)124            }125        case .invalid:126            WarningBanner(.critical, title: "Signature not valid",127                          message: "This signature does not match this message and this identity" + (requireDual ? ", or it lacks the SLH-DSA part." : "."))128        case .malformed(let what):129            WarningBanner(.warning, title: "Cannot verify", message: what)130        }131    }132133    func verify() {134        guard canVerify, !busy, let service = store.service else { return }135        let identityText = signer.trimmingCharacters(in: .whitespacesAndNewlines)136        guard let identity = try? PublicIdentity(text: identityText) else {137            outcome = .malformed("The signer identity is not a valid \u{201C}qyvora-id:\u{201D} text.")138            return139        }140        guard let bytes = SecurityFormat.bytes(fromEncoded: signature) else {141            outcome = .malformed("The signature is neither base64 nor hexadecimal.")142            return143        }144        busy = true145        let text = message, dual = requireDual, fingerprint = identity.fingerprintText146        Task {147            // SLH-DSA verification is slow: keep it off the main actor.148            let valid = await Task.detached(priority: .userInitiated) {149                service.verifyMessage(Array(text.utf8), signature: bytes, signer: identityText, requireDual: dual)150            }.value151            outcome = valid ? .valid(fingerprint: fingerprint) : .invalid152            busy = false153        }154    }155}156
Files main
Menu

Theme