SPB Git

Repository Public

SendCommon.swift 9.6 KB 210 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : QYVORA7// File             : App/Sources/Wallet/SendCommon.swift8// Module           : Wallet9// 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/// Frame of a send sheet: header with an optional back action, scrolling content, pinned actions.46struct SendScaffold<Content: View, Footer: View>: View {47    let title: String48    let subtitle: String?49    let onBack: (() -> Void)?50    let onClose: () -> Void51    @ViewBuilder let content: Content52    @ViewBuilder let footer: Footer5354    var body: some View {55        VStack(spacing: 0) {56            ScrollView {57                VStack(alignment: .leading, spacing: QSpace.l) {58                    if let onBack {59                        Button(action: onBack) { Label("Back", systemImage: "chevron.left") }60                            .font(QFont.callout.weight(.medium))61                            .foregroundStyle(QColor.violet)62                            .frame(minHeight: QMetric.minTap)63                            .buttonStyle(.plain)64                    }65                    SheetHeader(title, subtitle: subtitle, onClose: onClose)66                    content67                }68                .padding(.horizontal, QMetric.screenPadding)69                .padding(.top, onBack == nil ? QSpace.xl : QSpace.s)70                .padding(.bottom, QSpace.xl)71                .frame(maxWidth: 560, alignment: .leading)72                .frame(maxWidth: .infinity)73            }74            .scrollDismissesKeyboard(.interactively)75            .debugScroll()76            if Footer.self != EmptyView.self {77                QSeparator()78                VStack(spacing: QSpace.m) { footer }79                    .padding(.horizontal, QMetric.screenPadding)80                    .padding(.vertical, QSpace.m)81                    .frame(maxWidth: 560)82                    .frame(maxWidth: .infinity)83                    .background(QColor.background)84            }85        }86        .qScreen()87        .presentationDragIndicator(.visible)88        .interactiveDismissDisabled()89    }90}9192/// An unsigned transaction shown as QR frames for the signing device, with the text form to share.93struct ColdExportView: View {94    let export: QRExport95    /// What the payload is ("PSBT", "unsigned transaction").96    let payloadName: String97    /// A deadline the signing device must meet (TRON: thirty minutes; Solana: about a minute), when there is one.98    var urgency: String?99100    var body: some View {101        VStack(alignment: .leading, spacing: QSpace.l) {102            if let urgency { WarningBanner(.warning, title: "Limited validity", message: urgency) }103            QCard(padding: 20) {104                VStack(spacing: QSpace.m) {105                    if export.frames.count > 1 {106                        AnimatedQRCodeView(frames: export.frames, label: "Animated QR code of the \(payloadName)").frame(maxWidth: 280)107                    } else {108                        QRCodeView(payload: export.frames.first ?? export.text, label: "QR code of the \(payloadName)").frame(maxWidth: 280)109                    }110                    Text(export.frames.count > 1 ? "\(export.frames.count) frames, shown in a loop" : "One code")111                        .font(QFont.caption).foregroundStyle(QColor.textSecondary)112                }113                .frame(maxWidth: .infinity)114            }115            Text("On the device that holds the keys, open QYVORA in cold mode and scan this code. It decodes the transaction itself and shows the recipient, the amount and the fee: compare them with this screen before signing there.")116                .font(QFont.callout).foregroundStyle(QColor.textSecondary).fixedSize(horizontal: false, vertical: true)117            ShareLink(item: export.text) { Label("Share the \(payloadName) as text", systemImage: "square.and.arrow.up").frame(maxWidth: .infinity) }118                .buttonStyle(.qSecondary)119            Text("The \(payloadName) contains no key. It reveals the addresses and amounts of this transaction to whoever receives it.")120                .font(QFont.caption).foregroundStyle(QColor.textSecondary).fixedSize(horizontal: false, vertical: true)121        }122    }123}124125/// Sheet that scans (or pastes) the signed transaction brought back from the signing device.126struct SignedImportSheet: View {127    let title: String128    let onPayload: ([String]) -> Void129130    @Environment(\.dismiss) private var dismiss131    @State private var collector = FrameCollector()132133    var body: some View {134        VStack(spacing: QSpace.l) {135            SheetHeader(title, subtitle: "Scan the code shown by the signing device, or paste its text.") { dismiss() }136            QRScannerView(collect: { collector.collect($0) }, onComplete: {137                let payload = collector.payload138                dismiss()139                onPayload(payload)140            })141        }142        .padding(QMetric.screenPadding)143        .qScreen()144        .presentedPrivacyCover()145    }146}147148/// Broadcasts a transaction signed on a cold device, opened from the dashboard ("More") or from a scan. The payload149/// is checked by the service (`broadcastSigned`: signature, account, chain, limits) before anything is sent.150struct SendSignedImportFlow: View {151    /// Frames already scanned (from the Scan action); nil starts with the scanner.152    var initialPayload: [String]?153154    @Environment(WalletStore.self) private var wallet155    @Environment(\.dismiss) private var dismiss156    @State private var collector = FrameCollector()157    @State private var payload: [String]?158    @State private var busy = false159    @State private var error: String?160    @State private var result: SendResult?161162    var body: some View {163        Group {164            if let result {165                SendResultView(result: result, amountLine: nil, onDone: { dismiss() }, onSendAnother: nil)166            } else if let payload = payload ?? initialPayload {167                SendScaffold(title: "Broadcast a signed transaction", subtitle: nil, onBack: initialPayload == nil ? { self.payload = nil; error = nil } : nil,168                             onClose: { dismiss() }) {169                    ReviewSection("What was read") {170                        ReviewRow("Payload", value: payload.count > 1 ? "\(payload.count) QR frames" : "\(payload.first?.count ?? 0) characters")171                    }172                    WarningBanner(.warning, title: "Reviewed on the signing device",173                                  message: "This device cannot show a signed transaction again before sending it. Broadcast only what you reviewed and signed yourself on the cold device. QYVORA checks that the signature is this wallet's and that the network is active; once broadcast, a transaction cannot be undone.")174                    if wallet.snapshot?.unauditedMainnet == true {175                        WarningBanner(.critical, message: "Main networks are on: the per-transaction limits of this unaudited build are applied again before the broadcast.")176                    }177                    InlineError(message: error)178                } footer: {179                    LoadingButton("Broadcast", loadingTitle: "Checking and broadcasting", systemImage: "paperplane", state: busy ? .loading : .idle) { broadcast(payload) }180                        .disabled(busy)181                }182            } else {183                VStack(spacing: QSpace.l) {184                    SheetHeader("Signed transaction", subtitle: "Scan the code shown by the signing device, or paste its text.") { dismiss() }185                    QRScannerView(collect: { collector.collect($0) }, onComplete: { payload = collector.payload })186                }187                .padding(QMetric.screenPadding)188                .qScreen()189            }190        }191        .qSheetChrome(detents: QDetents.full)192        .qHaptic(trigger: error) { $0 == nil ? nil : .failed }193        .qHaptic(.sent, trigger: result?.id)194    }195196    func broadcast(_ frames: [String]) {197        guard !busy else { return }198        busy = true199        error = nil200        Task {201            defer { busy = false }202            do {203                result = try await wallet.run { try await $0.broadcastSigned(frames) }204            } catch {205                self.error = WalletStore.message(for: error)206            }207        }208    }209}210
Files main
Menu

Theme