SPB Git

Repository Public

IdentitySignView.swift 6.6 KB 147 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : QYVORA7// File             : App/Sources/Security/IdentitySignView.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 QyvoraWallet43import QyvoraUI4445/// Signs a text message with the wallet's identity (ML-DSA-87, optionally with a second SLH-DSA signature) after a46/// fresh authentication. The result is binary; it is shown as base64 to copy or share.47struct IdentitySignView: View {48    @Environment(WalletStore.self) private var store49    @State private var message = ""50    @State private var dual = false51    @State private var signature: [UInt8]?52    @State private var signedMessage = ""53    @State private var signer: String?54    @State private var error: String?55    @State private var busy = false56    @State private var reauthenticate = false5758    var body: some View {59        SecurityScroll {60            Explanation("The signature proves that the holder of this identity approved the exact text below. It is not a blockchain signature and moves no funds.")61            VStack(alignment: .leading, spacing: 6) {62                QEyebrow("Message")63                TextEditor(text: $message)64                    .font(QFont.body)65                    .frame(minHeight: 120)66                    .scrollContentBackground(.hidden)67                    .padding(8)68                    .background(QColor.surface, in: RoundedRectangle(cornerRadius: QMetric.radiusSmall, style: .continuous))69                    .overlay(RoundedRectangle(cornerRadius: QMetric.radiusSmall, style: .continuous).strokeBorder(QColor.stroke, lineWidth: 1))70                    .accessibilityLabel("Message to sign")71            }72            QListSection(footnote: "Adds a hash-based signature next to the lattice one, so that the signature holds even if one of the two families is broken. It is about 50 kB and takes noticeably longer to produce.") {73                SecToggleRow(title: "Dual signature", subtitle: "Adds SLH-DSA to ML-DSA-87", systemImage: "signature", isOn: $dual)74            }75            SecInlineError(error)76            LoadingButton("Sign", loadingTitle: dual ? "Signing (SLH-DSA is slow)" : "Signing", systemImage: "signature", state: busy ? .loading : .idle) {77                reauthenticate = true78            }79            .disabled(message.isEmpty)80            if let signature {81                result(signature)82            }83        }84        .navigationTitle("Sign a message")85        .navigationBarTitleDisplayMode(.inline)86        .requiresReauthentication(isPresented: $reauthenticate, reason: "Sign a message with the identity") { sign() }87        .sensoryFeedback(.success, trigger: signature)88        #if DEBUG89        .task {90            if SecurityDebug.action == "go" {91                message = "QYVORA debug message"92                reauthenticate = true93            }94        }95        #endif96        .onChange(of: message) { _, new in97            // A signature shown under an edited message would be misleading.98            if new != signedMessage { signature = nil }99        }100    }101102    @ViewBuilder func result(_ signature: [UInt8]) -> some View {103        let encoded = SecurityFormat.base64(signature)104        QListSection("Signature", footnote: "Sharing sends the message, the signature and the public identity, which is what a verifier needs.",105                     separatorInset: QMetric.padding) {106            SecTextRow(text: "Signed", systemImage: QTone.positive.systemImage, tone: .positive)107            SecFactRow(label: "Size", value: "\(signature.count) bytes")108            SecFactRow(label: "Signature (base64)", value: MiddleTruncation.truncate(encoded, head: 28, tail: 20), mono: true)109                .accessibilityLabel("Signature, base64, \(signature.count) bytes")110            HStack(spacing: QSpace.m) {111                CopyButton(encoded, title: "Copy signature", style: .labelled)112                ShareLink(item: Self.bundle(message: signedMessage, signature: encoded, signer: signer)) {113                    Label("Share", systemImage: "square.and.arrow.up").font(.callout.weight(.medium)).frame(minHeight: 44)114                }115                .tint(QColor.violet)116            }117            .padding(.vertical, QSpace.xs)118        }119        .transition(.opacity)120    }121122    /// Text handed to the share sheet: everything a verifier needs, in labelled blocks.123    static func bundle(message: String, signature: String, signer: String?) -> String {124        var text = "QYVORA signed message\n\nMessage:\n\(message)\n\nSignature (base64):\n\(signature)\n"125        if let signer { text += "\nSigner identity:\n\(signer)\n" }126        return text127    }128129    func sign() {130        guard !busy, !message.isEmpty else { return }131        busy = true132        error = nil133        let text = message, dual = dual134        Task {135            do {136                let bytes = try await store.run { try await $0.signMessage(Array(text.utf8), dual: dual) }137                signer = try? await store.service?.exportPublicIdentity().text138                signedMessage = text139                signature = bytes140            } catch {141                self.error = SecurityError.text(error)142            }143            busy = false144        }145    }146}147
Files main
Menu

Theme