SPB Git

Repository Public

TransferSendView.swift 8.6 KB 185 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : QYVORA7// File             : App/Sources/Security/TransferSendView.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 QyvoraIdentity43import QyvoraVault44import QyvoraWallet45import QyvoraUI4647/// Old iPhone side of a device-to-device transfer: scan the identity shown by the new iPhone, compare fingerprints,48/// choose a transfer password, authenticate, then show the encrypted package (`createTransferPackage`) as animated49/// QR frames for the new iPhone to scan.50struct TransferSendView: View {51    /// Receiver identity already read elsewhere ("qyvora-id:…"), or nil to scan it here.52    let receiver: String?5354    @Environment(WalletStore.self) private var store55    @State private var frames: [String] = []56    @State private var own: IdentitySummary?57    @State private var ownLoaded = false58    @State private var compared = false59    @State private var password = ""60    @State private var confirmation = ""61    @State private var package: TransferExport?62    @State private var error: String?63    @State private var busy = false64    @State private var scanning = false65    @State private var reauthenticate = false6667    var receiverIdentity: PublicIdentity? {68        PayloadText.text(from: frames).flatMap { try? PublicIdentity(text: $0) }69    }70    var estimate: PasswordQuality.Estimate? { password.isEmpty ? nil : store.service?.passwordQuality(password) }71    var canCreate: Bool { receiverIdentity != nil && compared && !password.isEmpty && password == confirmation }7273    var body: some View {74        SecurityScroll {75            if let package {76                sending(package)77            } else if ownLoaded, own == nil {78                EmptyStateView(systemImage: "person.text.rectangle", title: "An identity is needed",79                               message: "The transfer is signed by this wallet's identity so that the new iPhone can check where it comes from. Create one in Quantum Identity, then come back.")80                NavigationLink(value: SecurityRoute.identity) {81                    Label("Open Quantum Identity", systemImage: "person.text.rectangle")82                }83                .buttonStyle(.qSecondary)84            } else {85                steps86            }87        }88        .navigationTitle("Transfer to a new iPhone")89        .navigationBarTitleDisplayMode(.inline)90        .privacySensitive()91        .task(id: store.snapshot?.vault) {92            if let receiver, frames.isEmpty { frames = [receiver] }93            guard let service = store.service else { return }94            own = try? await service.identity()95            ownLoaded = true96        }97        .sheet(isPresented: $scanning) {98            PayloadScannerSheet(title: "Scan the new iPhone", prompt: "Keep the animated code of the new iPhone in the frame") { scanned in99                frames = scanned100                compared = false101            }102        }103        .requiresReauthentication(isPresented: $reauthenticate, reason: "Transfer this wallet to another iPhone") { create() }104        .onDisappear {105            password = ""106            confirmation = ""107            package = nil108        }109        .sensoryFeedback(.success, trigger: package != nil)110    }111112    @ViewBuilder var steps: some View {113        Explanation("On the new iPhone, open QYVORA without creating a wallet and choose to receive a wallet from another iPhone. It shows an animated code: its temporary receiving identity.")114        SectionHeader("1 · The new iPhone")115        if let identity = receiverIdentity {116            QListSection("Fingerprint of the new iPhone") {117                FingerprintView(text: identity.fingerprintText, copyable: false).padding(.vertical, QSpace.m)118                Toggle(isOn: $compared) {119                    Text("The same fingerprint is shown on the new iPhone").font(QFont.callout).foregroundStyle(QColor.textPrimary)120                        .fixedSize(horizontal: false, vertical: true)121                }122                .tint(QColor.violet)123                .padding(.vertical, QSpace.m)124                .qHaptic(.toggled, trigger: compared)125            }126            Button { scanning = true } label: { Label("Scan again", systemImage: "qrcode.viewfinder") }.buttonStyle(.qSecondary)127        } else {128            if !frames.isEmpty {129                WarningBanner(.warning, message: "What was scanned is not a QYVORA identity.")130            }131            Button { scanning = true } label: { Label("Scan the new iPhone", systemImage: "qrcode.viewfinder") }.buttonStyle(.qPrimary)132        }133        SectionHeader("2 · Transfer password", subtitle: "Protects the backup inside the package. You type it once more on the new iPhone.")134            .padding(.top, 8)135        PasswordField("Transfer password", text: $password, strength: estimate.map(PasswordStrength.init), isNewPassword: true)136        PasswordField("Repeat the transfer password", text: $confirmation, isNewPassword: true)137        if !confirmation.isEmpty, confirmation != password {138            Label("The two entries differ.", systemImage: QTone.warning.systemImage).font(QFont.caption).foregroundStyle(QColor.warning)139        }140        SecInlineError(error)141        LoadingButton("Create the transfer package", loadingTitle: "Encrypting and signing", systemImage: "iphone.and.arrow.forward", state: busy ? .loading : .idle) {142            reauthenticate = true143        }144        .disabled(!canCreate)145        Explanation("The package is encrypted for the new iPhone (ML-KEM-1024 with X25519) and signed by this wallet's identity. It carries the whole vault: the keys of every chain and the identity. This wallet stays on this iPhone until you delete it.")146    }147148    @ViewBuilder func sending(_ package: TransferExport) -> some View {149        SectionHeader("3 · Show this to the new iPhone")150        QRExportCard(frames: package.frames, text: nil, label: "Transfer package")151        if package.frames.isEmpty {152            ShareLink(item: SharedPackage(data: Data(package.package)), preview: SharePreview("QYVORA transfer package")) {153                Label("Send the package as a file", systemImage: "square.and.arrow.up")154            }155            .buttonStyle(.qPrimary)156            Explanation("The package is too large for QR codes. It is already encrypted for the new iPhone; send it by AirDrop and open it there.")157        }158        if let own {159            QListSection("Fingerprint of this wallet's identity", footnote: "The new iPhone shows the sender's fingerprint after it has opened the package. It must be this one.") {160                FingerprintView(text: own.fingerprintText, copyable: false).padding(.vertical, QSpace.m)161            }162        }163        QListSection {164            SecFactRow(label: "Package size", value: "\(package.package.count) bytes")165        }166    }167168    func create() {169        guard canCreate, !busy else { return }170        busy = true171        error = nil172        let target = frames, secret = password173        Task {174            do {175                package = try await store.run { try await $0.createTransferPackage(to: target, backupPassword: secret) }176                password = ""177                confirmation = ""178            } catch {179                self.error = SecurityError.text(error)180            }181            busy = false182        }183    }184}185
Files main
Menu

Theme