mirror of
https://github.com/pezkuwichain/pezkuwi-wallet-android.git
synced 2026-07-22 13:45:48 +00:00
Add Tron (TRC20/TRX) send/transfer support
Reuses the existing generic fee-sufficiency validation (sufficientTransferableBalanceToPayOriginFee) and send flow unchanged. Transaction construction goes through TronGrid's own createtransaction/triggersmartcontract endpoints (verified live against Shasta testnet) rather than a hand-rolled protobuf implementation; signing reuses the existing secp256k1 path (SignerPayloadRaw.skipMessageHashing) already used for Ethereum, no new crypto library. Also fixes a Phase 1 gap: Chain.isValidAddress() had no isTronBased branch, so every Tron send would have failed address validation. No Energy staking/delegation/rental UI - only Tron's default protocol behavior (burn TRX to cover Bandwidth/Energy shortfall).
This commit is contained in:
@@ -2,6 +2,7 @@ package io.novafoundation.nova.common.utils
|
||||
|
||||
import io.novasama.substrate_sdk_android.extensions.asEthereumPublicKey
|
||||
import io.novasama.substrate_sdk_android.extensions.toAccountId
|
||||
import io.novasama.substrate_sdk_android.extensions.toHexString
|
||||
import io.novasama.substrate_sdk_android.runtime.AccountId
|
||||
import java.math.BigInteger
|
||||
|
||||
@@ -113,3 +114,21 @@ fun String.tronAddressToAccountId(): AccountId {
|
||||
fun String.isValidTronAddress(): Boolean = runCatching { tronAddressToAccountId() }.isSuccess
|
||||
|
||||
fun emptyTronAccountId() = ByteArray(20) { 1 }
|
||||
|
||||
/**
|
||||
* Hex form of a Tron address (`0x41` prefix byte ++ accountId, hex-encoded, no `0x` prefix), e.g.
|
||||
* `41a614f803b6fd780986a42c78ec9c7f77e6ded13c`. This is the format TronGrid's `/wallet/*` transaction
|
||||
* construction/broadcast endpoints expect when called with `"visible": false` (as opposed to the human-facing
|
||||
* Base58Check form used by the `/v1/accounts/{address}` balance endpoint and by [toTronAddress]).
|
||||
*/
|
||||
fun AccountId.toTronHexAddress(): String {
|
||||
require(size == 20) { "Tron account id must be 20 bytes, got $size" }
|
||||
|
||||
return byteArrayOf(TRON_ADDRESS_PREFIX_BYTE).toHexString(withPrefix = false) + toHexString(withPrefix = false)
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a human-facing Base58Check Tron address (e.g. a TRC20 `contractAddress` from chain config) directly
|
||||
* into the hex form described in [toTronHexAddress].
|
||||
*/
|
||||
fun String.tronAddressToHexAddress(): String = tronAddressToAccountId().toTronHexAddress()
|
||||
|
||||
@@ -44,6 +44,18 @@ class TronAddressTest {
|
||||
assertTrue(decodedBack.contentEquals(accountId))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `toTronHexAddress should produce the known hex form`() {
|
||||
val accountId = knownTronAddressHex.fromHex().copyOfRange(1, 21)
|
||||
|
||||
assertEquals(knownTronAddressHex, accountId.toTronHexAddress())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `tronAddressToHexAddress should produce the known hex form directly from a Base58 address`() {
|
||||
assertEquals(knownTronAddressHex, knownTronAddress.tronAddressToHexAddress())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `isValidTronAddress should accept known good address`() {
|
||||
assertTrue(knownTronAddress.isValidTronAddress())
|
||||
|
||||
Reference in New Issue
Block a user