SPB Git

Repository Public

WalletService+HiddenTokens.swift 5.6 KB 123 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : QYVORA7// File             : Sources/QyvoraWallet/WalletService+HiddenTokens.swift8// Module           : Qyvora 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 Foundation42import QyvoraCore43import QyvoraEthereum44import QyvoraSolana45import QyvoraTron4647// The tokens the user has hidden, as a list the interface can show: a hidden token is neither read nor reported48// (`ChainData.tokens`, `detectedTokens`), so without this list nothing could offer to show it again.4950/// One hidden token of a chain.51public struct HiddenToken: Equatable, Sendable, Identifiable {52    public let chain: ChainID53    /// Contract or mint address, in the chain's own form.54    public let address: String55    /// The token as the wallet knows it (built-in or custom). nil for a token that was hidden from the "detected" list:56    /// the wallet kept its address only, and nothing is invented in place of its name.57    public let token: ChainToken?58    public var id: String { "\(chain.rawValue):\(address)" }5960    public init(chain: ChainID, address: String, token: ChainToken?) {61        self.chain = chain62        self.address = address63        self.token = token64    }65}6667extension WalletService {68    /// The key the profile stores for a hidden token of a chain: the prefix that names the chain there.69    private static func hiddenPrefix(_ kind: ChainKind) -> String? {70        switch kind {71        case .utxo: return nil72        // an EVM token is filed under the network's own name ("sepolia:0x…"), the others under the chain identifier73        case .evm(let network): return network.rawValue + ":"74        case .solana, .tron: return ChainRegistry.id(kind).rawValue + ":"75        }76    }7778    /// The hidden tokens of a chain, known ones first (in the order of the wallet's list), then bare addresses. No79    /// network, no secret. Empty on a chain without tokens or for an identifier QYVORA does not know.80    public func hiddenTokens(chain id: ChainID) -> [HiddenToken] {81        guard let kind = ChainRegistry.kind(id), let prefix = Self.hiddenPrefix(kind) else { return [] }82        let hidden = profile.hiddenTokens.filter { $0.hasPrefix(prefix) }83        guard !hidden.isEmpty else { return [] }84        let known: [(String, ChainToken)]85        switch kind {86        case .utxo: known = []87        case .evm(let network):88            var seen = Set<EthereumAddress>()89            known = (BuiltInTokens.tokens(for: network) + profile.customTokens.filter { $0.network == network })90                .filter { seen.insert($0.address).inserted }.map { ($0.id, Self.chainToken($0)) }91        case .solana(let network): known = knownSolanaTokens(network).map { ($0.id, $0) }92        case .tron(let network): known = knownTronTokens(network).map { ($0.id, $0) }93        }94        var out = [HiddenToken](), listed = Set<String>()95        for (key, token) in known where hidden.contains(key) && listed.insert(key).inserted {96            out.append(HiddenToken(chain: id, address: token.address, token: token))97        }98        for key in hidden where !listed.contains(key) {99            out.append(HiddenToken(chain: id, address: String(key.dropFirst(prefix.count)), token: nil))100        }101        return out102    }103104    /// Shows a hidden token again. A token of the wallet's list returns to it at once (its balance is read at the next105    /// synchronization); one that had been hidden from the detected list is reported again by the next106    /// synchronization that finds it. No network.107    public func showHiddenToken(_ hidden: HiddenToken) async throws(WalletError) {108        try await run {109            guard let kind = ChainRegistry.kind(hidden.chain), let prefix = Self.hiddenPrefix(kind) else { throw WalletError.unknownChain(hidden.chain.rawValue) }110            let key = prefix + hidden.address111            guard profile.hiddenTokens.contains(key) else { return }112            profile.hiddenTokens.removeAll { $0 == key }113            try saveProfile()114            switch kind {115            case .utxo: break116            case .evm(let network): alignEthereumSnapshotWithTokenList(network)117            case .solana(let network): alignSolanaHoldings(network)118            case .tron(let network): alignTronHoldings(network)119            }120        }121    }122}123
Files main
Menu

Theme