4

I have an issue while deploying contract in TRON network, where I am required to specify address in format that starts with 4.. or when I receive transactions history (here the api respond with 4.. addresses as well).

Therefore I have a question:

How to convert TRON address started with TLAXtqju7GKyqoP... to 419b6e043089843624c36f1e3b1e8572d189cbe170 and vice versa?

newbie
  • 71
  • 1
  • 1
  • 4

4 Answers4

5

How to convert TRON address started with TLAXtqju7GKyqoP... to 419b6e043089843624c36f1e3b1e8572d189cbe170 and vice versa?

const TronWeb = require('tronweb');
const tronWeb = new TronWeb(
  'http://127.0.0.1:9090',
  'http://127.0.0.1:9090',
  'http://127.0.0.1:9090',
  'd6fbbf6eecffdb32172e391363a401f89617acb9dd01897b9fa180830a8a46b2',
);

Once you have the tronWeb object, then you can convert the addresses vice-versa by using tronWeb's address utility

For Example:
const addressInHexFormat = '414450cf8c8b6a8229b7f628e36b3a658e84441b6f';
const addressInBase58 = tronWeb.address.fromHex(addressInHexFormat);
> addressInBase58 = 'TGCRkw1Vq759FBCrwxkZGgqZbRX1WkBHSu'
const addressInHex = tronWeb.address.toHex(addressInBase58);
> addressInHex = '414450cf8c8b6a8229b7f628e36b3a658e84441b6f'

Note

The above tronWeb object is created by using Tron's Quickstart Docker container. In this way the addresses can be converted vice-versa.

balajipachai
  • 231
  • 2
  • 6
1

You just should decode your base58Address from Base58. In result you will obtain addresschecksum, so you should remove last 4 bytes from result and obtain desired address.

address = 41||sha3[12,32): 415a523b449890854c8fc460ab602df9f31fe4293f 
sha256_0 = sha256(address): 06672d677b33045c16d53dbfb1abda1902125cb3a7519dc2a6c202e3d38d3322 
sha256_1 = sha256(sha256_0): 9b07d5619882ac91dbe59910499b6948eb3019fafc4f5d05d9ed589bb932a1b4 
checkSum = sha256_1[0, 4): 9b07d561 
addchecksum = address || checkSum: 415a523b449890854c8fc460ab602df9f31fe4293f9b07d561 
base58Address = Base58(addchecksum): TJCnKsPa7y5okkXvQAidZBzqx3QyQ6sxMW
1

The address format is well explained in the relevant TRON documentation.

In Java code (based on wallet-cli):

public String tronHex(String base58) {
    byte[] decoded = decode58(base58);
    String hexString = decoded == null ? "" : org.spongycastle.util.encoders.Hex.toHexString(decoded);
    return hexString;
}


private byte[] decode58(String input) {
    byte[] decodeCheck = Base58.decode(input);
    if (decodeCheck.length <= 4) {
        return null;
    }
    byte[] decodeData = new byte[decodeCheck.length - 4];
    System.arraycopy(decodeCheck, 0, decodeData, 0, decodeData.length);
    byte[] hash0 = Sha256Hash.hash(decodeData);
    byte[] hash1 = Sha256Hash.hash(hash0);
    if (hash1[0] == decodeCheck[decodeData.length] &&
            hash1[1] == decodeCheck[decodeData.length + 1] &&
            hash1[2] == decodeCheck[decodeData.length + 2] &&
            hash1[3] == decodeCheck[decodeData.length + 3]) {
        return decodeData;
    }
    return null;
}

And the other way around:

public String hexStringTobBase58(String hexString) {
    hexString = adjustHex(hexString);
    byte[] decodedHex = hexString == null? new byte[0] : org.spongycastle.util.encoders.Hex.decode(hexString);
    String base58 = encode58(decodedHex);
    return base58;
}

private String adjustHex(String hexString) {
    if (hexString.startsWith("0x")) {
        hexString = "41" + hexString.substring(2);
    }
    if (hexString.length() % 2 == 1) {
        hexString = "0" + hexString;
    }
    return hexString;
}


private String encode58(byte[] input) {
    byte[] hash0 = Sha256Hash.hash(input);
    byte[] hash1 = Sha256Hash.hash(hash0);
    byte[] inputCheck = new byte[input.length + 4];
    System.arraycopy(input, 0, inputCheck, 0, input.length);
    System.arraycopy(hash1, 0, inputCheck, input.length, 4);
    return Base58.encode(inputCheck);
}

Find class Base58 here, class Sha256Hash here and the required dependency to Spongy Castle here.

forhas
  • 11,551
  • 21
  • 77
  • 111
0

C# example:

        public static string GetHex(this String str)
        {
            var sb = new StringBuilder();

            var bytes = Base58.Bitcoin.Decode(str); // nuget https://www.nuget.org/packages/SimpleBase/

            for (int i = 0; i < bytes.Length - 4; i++)
            {
                var t = bytes[i];
                sb.Append(t.ToString("X2"));
            }

            return sb.ToString();
        }

DarDev
  • 153
  • 1
  • 1
  • 9