SPB Git

Repository Public

TronAddress.swift 5.3 KB 134 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : QYVORA7// File             : Sources/QyvoraTron/TronAddress.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 QyvoraEthereum4748/// A TRON account or contract address: the 20-byte hash of an Ethereum address behind the prefix byte 0x41.49///50/// Text form: Base58Check of the 21 bytes — always 34 characters starting with "T". Hexadecimal form: "41" followed by51/// 40 digits, as the node API prints it. Inside ABI call data the prefix is dropped and the 20 bytes are used as an52/// EVM address (`evm`).53public struct TronAddress: Equatable, Hashable, Sendable, CustomStringConvertible, Codable {54    public static let prefix: UInt8 = 0x415556    /// The 20-byte account hash (the EVM form).57    public let hash: Bytes5859    /// 0x41 followed by twenty zero bytes ("T9yD14Nj9j7xAB4dbGeiX9h8unkKHxuWwb"): the caller of read-only metadata60    /// calls, never a recipient.61    public static let zero = TronAddress(unchecked: Bytes(repeating: 0, count: 20))6263    init(unchecked: Bytes) { hash = unchecked }6465    /// From the 20-byte account hash.66    public init?(hash: Bytes) {67        guard hash.count == 20 else { return nil }68        self.hash = hash69    }7071    /// From the 21 bytes of the protocol (0x41 ‖ hash).72    public init?(bytes: Bytes) {73        guard bytes.count == 21, bytes[0] == Self.prefix else { return nil }74        hash = Bytes(bytes[1...])75    }7677    /// Parses the Base58Check form, strictly: 34 characters, first character "T", Base58 alphabet only, valid78    /// checksum, 21 bytes with the prefix 0x41. No surrounding white space, no hexadecimal form (see `init(hex:)`).79    public init?(_ text: String) {80        let ascii = Bytes(text.utf8)81        guard ascii.count == 34, ascii[0] == 0x54, let payload = Base58Check.decode(text), let a = TronAddress(bytes: payload) else { return nil }82        self = a83    }8485    /// Parses the hexadecimal form of the node API: exactly "41" and 40 hexadecimal digits (either case), no "0x".86    public init?(hex: String) {87        guard hex.utf8.count == 42, let b = Hex.decode(hex), let a = TronAddress(bytes: b) else { return nil }88        self = a89    }9091    /// Either text form: Base58Check ("T…", 34 characters) or hexadecimal ("41…", 42 digits).92    public init?(any text: String) {93        guard let a = TronAddress(text) ?? TronAddress(hex: text) else { return nil }94        self = a95    }9697    /// Address of a secp256k1 public key (compressed or uncompressed): last 20 bytes of Keccak-256(X ‖ Y).98    public init(publicKey: Bytes) throws {99        hash = try EthereumAddress(publicKey: publicKey).bytes100    }101102    /// From the EVM form, as found in ABI call data and event topics.103    public init(evm: EthereumAddress) { hash = evm.bytes }104105    /// The 21 bytes used in protocol messages: 0x41 ‖ hash.106    public var bytes: Bytes { [Self.prefix] + hash }107108    /// Base58Check form: 34 characters starting with "T".109    public var base58: String { Base58Check.encode(bytes) }110111    /// Hexadecimal form of the node API: "41" and 40 lower-case digits.112    public var hex: String { Hex.encode(bytes) }113114    /// The same 20 bytes as an EVM address, for ABI encoding.115    public var evm: EthereumAddress { EthereumAddress(bytes: hash) ?? .zero }116117    public var isZero: Bool { hash.allSatisfy { $0 == 0 } }118119    public var description: String { base58 }120121    public init(from decoder: Decoder) throws {122        let s = try decoder.singleValueContainer().decode(String.self)123        guard let a = TronAddress(s) else {124            throw DecodingError.dataCorrupted(.init(codingPath: decoder.codingPath, debugDescription: "invalid TRON address"))125        }126        self = a127    }128129    public func encode(to encoder: Encoder) throws {130        var c = encoder.singleValueContainer()131        try c.encode(base58)132    }133}134
Files main
Menu

Theme