SPB Git

Repository Public

WalletNetworksSheet.swift 9.0 KB 188 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : QYVORA7// File             : App/Sources/Wallet/WalletNetworksSheet.swift8// Module           : Wallet9// Repository       : https://git.spboucher.ai/qyvora10// Project Website  : Not specified11// Created          : 2026-12-0512// 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/// Enables and disables chains from the dashboard: one switch per chain, grouped by family. Each switch is one47/// service call (`setChainEnabled`); the network mode, the choice of test network and the endpoints live in the48/// Settings area, reached through `WalletHooks.openNetworkSettings`.49struct WalletNetworksSheet: View {50    @Environment(WalletStore.self) private var wallet51    @Environment(\.walletHooks) private var hooks52    @Environment(\.dismiss) private var dismiss53    @State private var error: String?54    @State private var busy: ChainID?5556    var body: some View {57        QSheetScaffold("Networks", subtitle: subtitle, onClose: { dismiss() }) {58            if let snapshot = wallet.snapshot {59                modeCard(snapshot)60                InlineError(message: error)61                ForEach(ChainRegistry.groups) { group in62                    QListSection(group.title, footnote: group.accountNote) {63                        ForEach(group.chains) { main in row(main, snapshot) }64                    }65                }66                WalletNoteLine(text: "One recovery phrase covers every network: enabling one creates nothing and moves nothing, it only decides what this wallet reads and shows.")67            }68        } footer: {69            if let open = hooks.openNetworkSettings {70                Button {71                    dismiss()72                    open()73                } label: {74                    Label("All network settings", systemImage: "gearshape").frame(maxWidth: .infinity)75                }76                .buttonStyle(.qSecondary)77                .accessibilityHint("Network mode, test networks and endpoints, in Settings")78            }79            Button("Done") { dismiss() }.buttonStyle(.qPrimary)80        }81        .qHaptic(.toggled, trigger: wallet.snapshot?.profile.enabledChains)82        .qHaptic(.failed, trigger: error)83    }8485    var subtitle: String {86        guard let snapshot = wallet.snapshot else { return "" }87        let count = snapshot.profile.enabledChains.count88        return "\(count) of \(ChainRegistry.mainChains.count) enabled"89    }9091    func modeCard(_ snapshot: WalletSnapshot) -> some View {92        let isMain = snapshot.networkMode == .main && snapshot.isMainnet93        return WarningBanner(isMain ? .warning : .info, title: isMain ? "Main networks · real funds" : "Test networks",94                             message: isMain95                                ? "Each enabled chain uses its main network. This build is not audited: per-transaction limits apply."96                                : "Each enabled chain uses its test network; test coins have no monetary value."97                                  + (snapshot.mainnetAvailable ? " Main networks are switched on in Settings." : " Main networks are not available in this build."))98    }99100    /// The network a chain uses (or would use) in the current mode.101    func network(of main: ChainDescriptor, _ snapshot: WalletSnapshot) -> ChainDescriptor {102        if let active = snapshot.chains.first(where: { $0.chain.mainCounterpart == main.id }) { return active.chain }103        guard snapshot.networkMode == .test || !snapshot.mainnetEnabled else { return main }104        let chosen = snapshot.profile.testNetworks[main.id.rawValue] ?? ChainRegistry.defaultTestNetwork(of: main.id)105        return chosen.flatMap(ChainRegistry.descriptor) ?? main106    }107108    func lockedReason(_ main: ChainDescriptor, _ snapshot: WalletSnapshot, isOn: Bool) -> String? {109        if isOn, snapshot.profile.enabledChains.count == 1 { return "At least one network stays enabled." }110        guard snapshot.isWatchOnly, !isOn else { return nil }111        switch main.family {112        case .evm where snapshot.profile.ethereumAddress == nil: return "This watch-only wallet has no EVM address."113        case .solana where snapshot.profile.solanaAddress == nil: return "This watch-only wallet has no Solana address."114        case .tron where snapshot.profile.tronAddress == nil: return "This watch-only wallet has no TRON address."115        default: return nil116        }117    }118119    func row(_ main: ChainDescriptor, _ snapshot: WalletSnapshot) -> some View {120        let isOn = snapshot.profile.enabledChains.contains(main.id)121        let shown = network(of: main, snapshot)122        let note = shown.nativeName == shown.nativeSymbol ? shown.nativeSymbol : "\(shown.nativeName) · \(shown.nativeSymbol)"123        return ChainPickerRow(network: shown.qNetwork, note: note, lockedReason: busy == main.id ? "Saving" : lockedReason(main, snapshot, isOn: isOn),124                              isOn: Binding(get: { isOn }, set: { set(main.id, $0) }))125    }126127    func set(_ chain: ChainID, _ enabled: Bool) {128        guard busy == nil else { return }129        busy = chain130        error = nil131        Task {132            defer { busy = nil }133            do {134                try await wallet.run { try await $0.setChainEnabled(chain, enabled: enabled) }135                await wallet.refresh()136            } catch {137                self.error = WalletStore.message(for: error)138            }139        }140    }141}142143/// The "More" action of the dashboard: what does not deserve a permanent control.144struct WalletMoreSheet: View {145    let open: (WalletSheet?) -> Void146    @Environment(WalletStore.self) private var wallet147    @Environment(\.walletHooks) private var hooks148    @HideAmounts private var hideAmounts149150    var body: some View {151        QSheetScaffold("More", onClose: { open(nil) }) {152            QListSection {153                WalletActionRow(title: "Refresh every network", subtitle: wallet.snapshot?.profile.coldMode == true ? "Cold mode is on: nothing is fetched" : "Balances, history and prices",154                                systemImage: "arrow.triangle.2.circlepath") {155                    open(nil)156                    Task { await wallet.synchronize() }157                }158                WalletActionRow(title: hideAmounts ? "Show amounts" : "Hide amounts", subtitle: "On every wallet screen", systemImage: hideAmounts ? "eye" : "eye.slash") {159                    hideAmounts.toggle()160                    open(nil)161                }162                WalletActionRow(title: "Manage networks", subtitle: "Enable or disable chains", systemImage: "slider.horizontal.3") { open(.networks) }163            }164            QListSection("Cold signing", footnote: "A transaction prepared here and signed on a device that stays offline comes back as a QR code or as text.") {165                WalletActionRow(title: "Broadcast a signed transaction", subtitle: "Scan or paste what the signing device shows", systemImage: "qrcode.viewfinder") {166                    open(.importSigned)167                }168            }169            if hooks.openNetworkSettings != nil || hooks.openColdMode != nil {170                QListSection("Settings") {171                    if let settings = hooks.openNetworkSettings {172                        WalletActionRow(title: "Network settings", subtitle: "Mode, test networks, endpoints", systemImage: "gearshape") {173                            open(nil)174                            settings()175                        }176                    }177                    if let cold = hooks.openColdMode {178                        WalletActionRow(title: "Cold mode", subtitle: "Keep this device offline and sign by QR code", systemImage: "snowflake") {179                            open(nil)180                            cold()181                        }182                    }183                }184            }185        }186    }187}188
Files main
Menu

Theme