PHP Convert TRON Hex address to Base58

3.3k Views Asked by At

Based on the official TRON documentation, they have API based on TRONWEB to work with Javascript. https://developers.tron.network/reference#address

However I'm working on a backend PHP app and would like to store TRON address as Base58 instead of hex FORMAT (Default is HEX in API outputs) So is there any way to convert the address from HEX to Base58 in PHP?

(Have researched and couldn't find any possible answer already)

2

There are 2 best solutions below

0
Mage On

Yes, It's possible to do the conversion in php.

First, install the package iexbase/tron-api via composer.

How I do mine is, I have a class called Address and it looks like:

<?php
namespace App\EventHandlers\Param;

use Web3\Utils;

class Address extends Base
{
    private $hex;
    private $base58;

    public function __construct(string $address)
    {
        $this->tron = $this->getTron();

        if ($address === '0x0000000000000000000000000000000000000000' || $address === 'T9yD14Nj9j7xAB4dbGeiX9h8unkKHxuWwb') {
            $this->hex = null;
            $this->base58 = null;
        } else {
            if (Utils::isHex($address)) {
                if (substr($address, 0, 2) === '0x') {
                    //set prefix
                    $address = '41'.substr($address, 2);
                }

                $this->hex = $address;
                $this->base58 = $this->tron->hexString2Address($address);
            } else {
                $this->base58 = $address;
                $this->hex = $this->tron->address2HexString($address);
            }
        }
    }

    public function getHex()
    {
        return $this->hex;
    }

    public function getBase58()
    {
        return $this->base58;
    }

    public function __toString()
    {
        return json_encode(['hex' => $this->hex, 'base58' => $this->base58]);
    }
}

This Address class extends a Base class which helps with setting up an instance of the Tron API client. The Base class looks like:

<?php

namespace App\EventHandlers\Param;

use IEXBase\TronAPI\Tron;
use IEXBase\TronAPI\Provider\HttpProvider;
use IEXBase\TronAPI\Exception\TronException;

class Base
{
    /**
     * @var Tron
     */
    protected $tron;

    /**
     * @return Tron
     * @throws TronException
     */
    public function getTron():?Tron
    {
        $fullNode = new HttpProvider(config('tron.full_node'));
        $solidityNode = new HttpProvider(config('tron.solidity_node'));
        $eventServer = new HttpProvider(config('tron.event_server'));
        try {
            return new Tron($fullNode, $solidityNode, $eventServer);
        } catch (TronException $exception) {
            return null;
        }
    }
}

So you can then do

$address = new Address($hexOrBase58Address);

then access the address in whatever format:

$address->getHex(); // Gives the hex address

or

$address->getBase58();  //Gives the base58 address

I really hope this helps you.

0
Kourosh On

Just use following functions:

function base58_decode($base58)
{
    $alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
    $base = strlen($alphabet);
    if (is_string($base58) === false) {
        return false;
    }
    if (strlen($base58) === 0) {
        return '';
    }
    $indexes = array_flip(str_split($alphabet));
    $chars = str_split($base58);
    foreach ($chars as $char) {
        if (isset($indexes[$char]) === false) {
            return false;
        }
    }
    $decimal = $indexes[$chars[0]];
    for ($i = 1, $l = count($chars); $i < $l; $i++) {
        $decimal = bcmul($decimal, $base);
        $decimal = bcadd($decimal, $indexes[$chars[$i]]);
    }
    $output = '';
    while ($decimal > 0) {
        $byte = bcmod($decimal, 256);
        $output = pack('C', $byte) . $output;
        $decimal = bcdiv($decimal, 256, 0);
    }
    foreach ($chars as $char) {
        if ($indexes[$char] === 0) {
            $output = "\x00" . $output;
            continue;
        }
        break;
    }
    return $output;
}

function base58check_de($base58add)
{
    $address = base58_decode($base58add);
    $size = strlen($address);
    if ($size != 25) {
        return false;
    }
    $checksum = substr($address, 21);
    $address = substr($address, 0, 21);     
    $hash0 = hash("sha256", $address);
    $hash1 = hash("sha256", hex2bin($hash0));
    $checksum0 = substr($hash1, 0, 8);
    $checksum1 = bin2hex($checksum);
    if (strcmp($checksum0, $checksum1)) {
        return false;
    }
    return $address;
}

If you need more functionality then take a look at this.