Repository Public
1// =============================================================================2// SP BOUCHER | SOFTWARE & RESEARCH3// ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project : QYVORA7// File : Tests/QyvoraTronTests/AddressTests.swift8// Module : Tests9// 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 Testing46import QyvoraCore47import QyvoraEthereum48@testable import QyvoraTron4950@Suite struct TronAddressTests {51 @Test func networks() {52 #expect(TronNetwork.allCases.count == 3)53 #expect(!TronNetwork.mainnet.isTest && TronNetwork.nile.isTest && TronNetwork.shasta.isTest)54 for n in TronNetwork.allCases {55 #expect(n.coinType == 195 && n.symbol == "TRX")56 let id = Hex.decode(n.genesisBlockID)57 #expect(id?.count == 32)58 // a block id starts with the block number: eight zero bytes for the genesis block59 #expect(id?.prefix(8).allSatisfy { $0 == 0 } == true)60 #expect(TronEndpoints.bases(TronEndpoints.default.fullNodes[n.rawValue] ?? []).count >= 1)61 #expect(TronEndpoints.bases(TronEndpoints.default.history[n.rawValue] ?? []).count == 1)62 }63 #expect(Set(TronNetwork.allCases.map(\.genesisBlockID)).count == 3)64 #expect(TronPaths.account().description == "m/44'/195'/0'/0/0")65 #expect(TronPaths.account(index: 7).description == "m/44'/195'/0'/0/7")66 #expect(TronEndpoints.bases(["https://node.example/", "http://example.com", "https://a.example/x?y=1", "http://192.168.1.4:8090"]).map(\.absoluteString)67 == ["https://node.example", "http://192.168.1.4:8090"])68 }6970 @Test func units() {71 #expect(TronUnits.parseTRX("1") == 1_000_000)72 #expect(TronUnits.parseTRX("0.000001") == 1)73 #expect(TronUnits.parseTRX("0.0000001") == nil)74 #expect(TronUnits.parseTRX("-1") == nil)75 #expect(TronUnits.parseTRX("9223372036854.775807") == UInt64(Int64.max))76 #expect(TronUnits.parseTRX("9223372036854.775808") == nil)77 #expect(TronUnits.formatTRX(1_500_000) == "1.5")78 #expect(TronUnits.formatTRX(1) == "0.000001")79 }8081 /// Both text forms of public addresses, against TronWeb (and tronpy).82 @Test func textForms() throws {83 #expect(TronVectors.addresses.count == 5)84 for v in TronVectors.addresses {85 let a = try #require(TronAddress(v.base58))86 #expect(a.hex == v.hex)87 #expect(a.base58 == v.base58)88 #expect(a.description == v.base58)89 #expect(TronAddress(hex: v.hex) == a)90 #expect(TronAddress(hex: v.hex.uppercased()) == a)91 #expect(TronAddress(any: v.hex) == a && TronAddress(any: v.base58) == a)92 #expect(a.bytes.count == 21 && a.bytes[0] == 0x41)93 #expect(TronAddress(bytes: a.bytes) == a)94 #expect(TronAddress(hash: a.hash) == a)95 #expect(TronAddress(evm: a.evm) == a)96 #expect(a.evm.bytes == a.hash)97 #expect(v.base58.utf8.count == 34 && v.base58.hasPrefix("T"))98 }99 // the 20-byte EVM form of the USDT contract, as it appears in ABI data100 #expect(TronAddress("TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t")?.evm == EthereumAddress("0xa614f803b6fd780986a42c78ec9c7f77e6ded13c"))101 #expect(TronAddress.zero.base58 == "T9yD14Nj9j7xAB4dbGeiX9h8unkKHxuWwb")102 #expect(TronAddress.zero.isZero && TronAddress("TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t")?.isZero == false)103 }104105 @Test func strictValidation() {106 let good = "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t"107 #expect(TronAddress(good) != nil)108 // one character changed: checksum109 #expect(TronAddress("TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6u") == nil)110 // white space, truncation, extension, characters outside the alphabet111 #expect(TronAddress(" " + good) == nil)112 #expect(TronAddress(good + " ") == nil)113 #expect(TronAddress(good + "\n") == nil)114 #expect(TronAddress(String(good.dropLast())) == nil)115 #expect(TronAddress(good + "1") == nil)116 #expect(TronAddress("TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj0t") == nil)117 #expect(TronAddress("TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLjIt") == nil)118 #expect(TronAddress("") == nil)119 #expect(TronAddress("ТR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t") == nil) // Cyrillic Т120 // a valid Base58Check payload with another prefix (Bitcoin P2PKH, 34 characters) is not a TRON address121 #expect(TronAddress("1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2") == nil)122 let wrongPrefix = Base58Check.encode([0x42] + Bytes(repeating: 7, count: 20))123 #expect(TronAddress(wrongPrefix) == nil)124 // 20 or 22 bytes behind a valid checksum125 #expect(TronAddress(Base58Check.encode([0x41] + Bytes(repeating: 7, count: 19))) == nil)126 #expect(TronAddress(Base58Check.encode([0x41] + Bytes(repeating: 7, count: 21))) == nil)127 // hexadecimal form: exactly 41 + 40 digits, no 0x128 #expect(TronAddress(hex: "41a614f803b6fd780986a42c78ec9c7f77e6ded13c") != nil)129 #expect(TronAddress(hex: "0x41a614f803b6fd780986a42c78ec9c7f77e6ded13c") == nil)130 #expect(TronAddress(hex: "a614f803b6fd780986a42c78ec9c7f77e6ded13c") == nil)131 #expect(TronAddress(hex: "42a614f803b6fd780986a42c78ec9c7f77e6ded13c") == nil)132 #expect(TronAddress(hex: "41a614f803b6fd780986a42c78ec9c7f77e6ded13") == nil)133 #expect(TronAddress(hex: "41a614f803b6fd780986a42c78ec9c7f77e6ded1zz") == nil)134 #expect(TronAddress(good)?.hex == "41a614f803b6fd780986a42c78ec9c7f77e6ded13c")135 // the Base58 initialiser does not take the hexadecimal form136 #expect(TronAddress("41a614f803b6fd780986a42c78ec9c7f77e6ded13c") == nil)137 #expect(TronAddress(bytes: Bytes(repeating: 0x41, count: 20)) == nil)138 #expect(TronAddress(hash: Bytes(repeating: 1, count: 21)) == nil)139 }140141 /// Private key → address against TronWeb 6.5.1 and tronpy 0.8.0.142 @Test func keyToAddress() throws {143 #expect(TronVectors.keys.count == 12)144 for v in TronVectors.keys {145 let key = try #require(Hex.decode(v.privateKey))146 let compressed = try Secp256k1.publicKey(privateKey: key, compressed: true)147 let uncompressed = try Secp256k1.publicKey(privateKey: key, compressed: false)148 #expect(try TronAddress(publicKey: compressed).base58 == v.address)149 #expect(try TronAddress(publicKey: uncompressed).base58 == v.address)150 #expect(try TronAddress(publicKey: compressed).hex == v.hex)151 }152 #expect(throws: (any Error).self) { try TronAddress(publicKey: Bytes(repeating: 0, count: 33)) }153 }154155 /// The "abandon … about" mnemonic at m/44'/195'/0'/0/{0,1,2} against TronWeb's `fromMnemonic`.156 @Test func mnemonicDerivation() throws {157 let seed = try BIP39.seed(mnemonic: BIP39.words(from: TronVectors.mnemonic))158 let master = try ExtendedKey.master(seed: seed)159 #expect(TronVectors.derivation.count == 3)160 for (i, v) in TronVectors.derivation.enumerated() {161 let path = TronPaths.account(index: UInt32(i))162 #expect(path.description == v.path)163 let key = try master.derive(path)164 #expect(Hex.encode(key.privateKey ?? []) == v.privateKey)165 #expect(Hex.encode(try Secp256k1.convert(publicKey: key.publicKey, compressed: false)) == v.publicKey)166 #expect(try TronAddress(publicKey: key.publicKey).base58 == v.address)167 }168 #expect(TronVectors.derivation[0].address == "TUEZSdKsoDHQMeZwihtdoBiN46zxhGWYdH")169 }170171 @Test func codable() throws {172 let a = try #require(TronAddress("TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t"))173 let data = try JSONEncoder().encode([a])174 #expect(String(decoding: data, as: UTF8.self) == "[\"TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t\"]")175 #expect(try JSONDecoder().decode([TronAddress].self, from: data) == [a])176 #expect(throws: (any Error).self) { try JSONDecoder().decode([TronAddress].self, from: Data("[\"TR7\"]".utf8)) }177 }178}179