Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : QYVORA7// File : Sources/QyvoraTron/TronNetwork.swift8// Module : Qyvora Tron9// 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 QyvoraMath43import QyvoraSymmetric44import QyvoraSecp256k145import QyvoraCore46import QyvoraNetworking4748/// The TRON networks the wallet knows. Every network uses the same address format (prefix 0x41), the same coin type49/// (195) and the same transaction encoding: nothing in a signed transaction names the chain, so a transaction signed50/// for one network is only kept off the others by its reference block (TaPoS). The identity of an endpoint is therefore51/// checked with the id of its genesis block before anything is built for it (`TronClient.verifyChain`).52public enum TronNetwork: String, Codable, Sendable, CaseIterable {53 case mainnet54 case nile55 case shasta5657 public var isTest: Bool { self != .mainnet }5859 public var displayName: String {60 switch self {61 case .mainnet: return "TRON"62 case .nile: return "TRON Nile"63 case .shasta: return "TRON Shasta"64 }65 }6667 /// Ticker of the native coin (test networks hand out valueless TRX).68 public var symbol: String { "TRX" }6970 /// BIP-44 coin type (SLIP-44): 195 on every network, as TronLink does.71 public var coinType: UInt32 { 195 }7273 /// Block id of block 0, lower-case hexadecimal: the first eight bytes are the block number, the rest comes from74 /// the block hash. Read from `/wallet/getblockbynum {"num":0}` on 2026-12-05 (mainnet: TronGrid and PublicNode;75 /// Nile: TronGrid and nileex.io; Shasta: TronGrid).76 public var genesisBlockID: String {77 switch self {78 case .mainnet: return "00000000000000001ebf88508a03865c71d452e25f4d51194196a1d22b6653dc"79 case .nile: return "0000000000000000d698d4192c56cb6be724a558448e2684802de4d6cd8690dc"80 case .shasta: return "0000000000000000de1aa88295e1fcf982742f773e0419c5a9c134c994a9059e"81 }82 }83}8485extension TronNetwork {86 /// The account in which the protocol burns fees ("Blackhole" in the genesis configuration of java-tron; read87 /// from `/wallet/getaccount` on TronGrid and PublicNode on 2026-12-05: `account_name` "Blackhole", a negative88 /// balance). Nobody holds its key: TRX or tokens sent there are destroyed. Main network only.89 public var burnAddress: TronAddress? {90 self == .mainnet ? TronAddress("TLsV52sRDL79HXGGm9yzwKibb6BeruhUzy") : nil91 }92}9394/// Amounts of the native coin are counted in sun: 1 TRX = 1 000 000 sun. Never a floating-point number.95public enum TronUnits {96 public static let decimals = 697 public static let sunPerTRX: UInt64 = 1_000_0009899 /// "1.5" → 1 500 000. nil for a malformed text, more than six decimals or a value beyond 2^63 − 1 sun.100 public static func parseTRX(_ text: String) -> UInt64? {101 guard let v = BaseUnits.parse(text, decimals: decimals)?.uint64, v <= UInt64(Int64.max) else { return nil }102 return v103 }104105 /// 1 500 000 → "1.5".106 public static func formatTRX(_ sun: UInt64) -> String { BaseUnits.format(UInt256(sun), decimals: decimals) }107}108109/// Derivation paths of TRON accounts.110public enum TronPaths {111 /// m/44'/195'/0'/0/index — what TronLink, Ledger Live and TronWeb derive (the same path on test networks).112 public static func account(index: UInt32 = 0) -> DerivationPath {113 DerivationPath([.hardened(44), .hardened(195), .hardened(0), .normal(0), .normal(index)])114 }115}116117/// The TRON endpoints the application talks to. Everything is user-editable (a personal node, a paid TronGrid plan).118public struct TronEndpoints: Codable, Equatable, Sendable {119 /// java-tron full-node HTTP API bases (`/wallet/...`, `/walletsolidity/...`) per network (raw value of `TronNetwork`).120 public var fullNodes: [String: [String]]121 /// TronGrid-compatible history index bases (`/v1/accounts/...`) per network. Empty disables history.122 public var history: [String: [String]]123124 /// Keyless public endpoints, each read on 2026-12-05 (genesis block, head block, chain parameters; the history125 /// index answers on the three TronGrid hosts only: PublicNode and nileex.io return 404 on `/v1`).126 public static let `default` = TronEndpoints(127 fullNodes: [128 "mainnet": ["https://api.trongrid.io", "https://tron-rpc.publicnode.com"],129 "nile": ["https://nile.trongrid.io", "https://api.nileex.io"],130 "shasta": ["https://api.shasta.trongrid.io"],131 ],132 history: [133 "mainnet": ["https://api.trongrid.io"],134 "nile": ["https://nile.trongrid.io"],135 "shasta": ["https://api.shasta.trongrid.io"],136 ]137 )138139 public init(fullNodes: [String: [String]], history: [String: [String]]) {140 self.fullNodes = fullNodes141 self.history = history142 }143144 /// The acceptable URLs of a list (https, or http for a local or private host: the rule of `EndpointConfiguration`),145 /// without a trailing slash, a query or a fragment.146 static func bases(_ list: [String]) -> [URL] {147 list.compactMap { text in148 guard EndpointConfiguration.isAcceptable(text), var c = URLComponents(string: text), c.query == nil, c.fragment == nil else { return nil }149 while c.path.hasSuffix("/") { c.path.removeLast() }150 return c.url151 }152 }153}154