是的,可以在php中进行转换。
首先,通过composer安装包iexbase/tron-api。
我的做法是,我有一个名为 Address 的类,它看起来像:
<?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]);
}
}
这个 Address 类扩展了一个 Base 类,它有助于设置 Tron API 客户端的实例。 Base 类看起来像:
<?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;
}
}
}
所以你可以这样做
$address = new Address($hexOrBase58Address);
然后以任何格式访问地址:
$address->getHex(); // Gives the hex address
或
$address->getBase58(); //Gives the base58 address
我真的希望这对你有帮助。