SPB Git

Repository Public

ReceiveView.swift 10.3 KB 215 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : QYVORA7// File             : App/Sources/Wallet/ReceiveView.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 QyvoraCore43import QyvoraWallet44import QyvoraUI4546/// Receive sheet: the network first, then its address with the rule that applies to it (`ChainReceive`): a fresh47/// address for every payment on UTXO chains, one fixed address elsewhere. The network is always in view.48struct ReceiveFlow: View {49    @Environment(WalletStore.self) private var wallet50    @Environment(\.dismiss) private var dismiss51    @State private var selected: ChainID?52    @State private var search = ""5354    init(initialChain: ChainID?) {55        _selected = State(initialValue: initialChain)56    }5758    var body: some View {59        Group {60            if let snapshot = wallet.snapshot {61                if let id = selected, let chain = snapshot.activeChain(id) {62                    address(chain, snapshot)63                } else {64                    picker(snapshot)65                }66            }67        }68        .qSheetChrome(detents: QDetents.full)69        .qHaptic(.selection, trigger: selected)70        .qAnimation(value: selected)71        #if DEBUG72        .task(id: DebugNavigator.shared.serial) {73            if let name = DebugLaunch.defaults.string(forKey: "QYChain"), DebugLaunch.screen == "receive" { selected = ChainID(name) }74        }75        #endif76    }7778    // MARK: Picker7980    func picker(_ snapshot: WalletSnapshot) -> some View {81        let chains = snapshot.chains.filter { chain in82            let q = search.trimmingCharacters(in: .whitespaces).lowercased()83            return q.isEmpty || chain.chain.displayName.lowercased().contains(q) || chain.chain.nativeSymbol.lowercased().contains(q)84        }85        return QSheetScaffold("Receive", subtitle: "Choose the network the sender will use", onClose: { dismiss() }) {86            if snapshot.chains.count > 6 { WalletSearchField(text: $search, prompt: "Search a network") }87            if snapshot.isTestMode {88                WarningBanner(.info, title: "Test networks", message: "Coins received here have no monetary value. Never send real funds to a test-network address.")89            }90            QListSection {91                ForEach(chains) { chain in92                    Button { selected = chain.id } label: {93                        HStack(spacing: QSpace.m) {94                            ChainGlyph(chain.network)95                            VStack(alignment: .leading, spacing: 2) {96                                HStack(spacing: 6) {97                                    Text(chain.network.fullName).font(QFont.body).foregroundStyle(QColor.textPrimary).lineLimit(1)98                                    if chain.chain.isTest { QTestTag() }99                                }100                                Text(caption(chain)).font(QFont.caption).foregroundStyle(QColor.textSecondary).lineLimit(2)101                            }102                            Spacer(minLength: QSpace.s)103                            Image(systemName: "chevron.right").font(.footnote.weight(.semibold)).foregroundStyle(QColor.textTertiary).accessibilityHidden(true)104                        }105                        .frame(minHeight: QMetric.rowMinHeight)106                        .contentShape(Rectangle())107                    }108                    .buttonStyle(.qRow)109                    .accessibilityElement(children: .combine)110                    .accessibilityLabel("\(chain.network.spokenName). \(caption(chain))")111                }112            }113            if chains.isEmpty { WalletNoteLine(text: "No enabled network matches.", systemImage: "magnifyingglass") }114        }115    }116117    func caption(_ chain: ChainSnapshot) -> String {118        guard let receive = chain.receive else { return "No account on this network" }119        let coin = chain.chain.capabilities.tokens ? "\(chain.chain.nativeSymbol) and tokens" : chain.chain.nativeSymbol120        return receive.rule == .freshAddressPerPayment ? "\(coin) · a new address for every payment" : "\(coin) · one address"121    }122123    // MARK: Address124125    func address(_ chain: ChainSnapshot, _ snapshot: WalletSnapshot) -> some View {126        QSheetScaffold("Receive \(chain.chain.nativeSymbol)", subtitle: chain.network.fullName, onClose: { dismiss() }) {127            if snapshot.chains.count > 1 {128                Button { selected = nil } label: {129                    Label("Another network", systemImage: "chevron.left").font(QFont.callout.weight(.medium)).frame(minHeight: QMetric.minTap)130                }131                .buttonStyle(.plain)132                .foregroundStyle(QColor.violet)133            }134            if let receive = chain.receive {135                AddressCard(address: receive.address, network: chain.network, caption: receive.derivationPath, hasChecksum: chain.chain.addressHasChecksum)136                    .privacySensitive()137                rule(receive, chain)138                ReviewSection("Before you share it") {139                    ForEach(notes(receive, chain), id: \.self) { note in140                        WalletNoteLine(text: note).padding(.vertical, QSpace.m)141                    }142                }143                if let limits = chain.limits {144                    WalletNoteLine(text: "This build is not audited: keep only small amounts here. " + limits.native, systemImage: "exclamationmark.triangle", tone: QColor.warning)145                }146            } else {147                UnavailableDataView(reason: unavailableReason(chain), title: "No address on \(chain.chain.displayName)", compact: true)148            }149        }150    }151152    /// The state of the address shown, in one line: fresh and checked, or not checked against the network.153    @ViewBuilder154    func rule(_ receive: ChainReceive, _ chain: ChainSnapshot) -> some View {155        switch receive.rule {156        case .freshAddressPerPayment:157            if let data = chain.data.value, !data.isFromPreviousSession {158                WalletNoteLine(text: "Never used as of the last synchronization: no transaction involves this address.", systemImage: "checkmark.circle", tone: QColor.positive)159            } else {160                WarningBanner(.warning, title: "Not checked against the network",161                              message: "This address follows the usage this device last knew. Until \(chain.chain.displayName) is synchronized in this session, QYVORA cannot confirm that it was never used.")162            }163        case .fixedAddress:164            WalletNoteLine(text: "This account has one address: every payment to it is publicly linked to the others.", systemImage: "link")165        }166    }167168    /// The service's sentences for this address, then what the format of the address implies.169    func notes(_ receive: ChainReceive, _ chain: ChainSnapshot) -> [String] {170        var out = ["Send only \(chain.chain.capabilities.tokens ? "\(chain.chain.nativeSymbol) and tokens of" : chain.chain.nativeSymbol + " on") \(chain.chain.displayName) to this address."]171        out += receive.notes172        switch chain.chain.family {173        case .evm:174            out.append("If something is sent to this address on another EVM network, it only becomes visible here once that network is enabled in this wallet.")175        case .solana:176            out.append("A Solana address carries no checksum: a mistyped character is not detected. Ask the sender to compare the highlighted first and last groups.")177        case .tron:178            out.append("Ask the sender to compare the highlighted first and last groups with what their own screen shows.")179        case .utxo:180            break181        }182        return out183    }184185    func unavailableReason(_ chain: ChainSnapshot) -> String {186        if case .unavailable(let reason) = chain.data { return reason }187        return "This wallet has no account on this network."188    }189}190191/// A search field in the style of the design system's inputs.192struct WalletSearchField: View {193    @Binding var text: String194    let prompt: String195196    var body: some View {197        HStack(spacing: QSpace.s) {198            Image(systemName: "magnifyingglass").foregroundStyle(QColor.textTertiary).accessibilityHidden(true)199            TextField(prompt, text: $text)200                .textInputAutocapitalization(.never)201                .autocorrectionDisabled()202                .submitLabel(.search)203            if !text.isEmpty {204                Button { text = "" } label: { Image(systemName: "xmark.circle.fill").foregroundStyle(QColor.textTertiary) }205                    .buttonStyle(.plain)206                    .accessibilityLabel("Clear the search")207            }208        }209        .padding(.horizontal, 14)210        .frame(minHeight: QMetric.minTap + 4)211        .background(QColor.surface, in: RoundedRectangle(cornerRadius: QMetric.radiusSmall, style: .continuous))212        .overlay(RoundedRectangle(cornerRadius: QMetric.radiusSmall, style: .continuous).strokeBorder(QColor.stroke, lineWidth: 1))213    }214}215
Files main
Menu

Theme