PHP将TRON十六进制地址转换为Base58

时间:2020-09-17 08:51:30

标签: php hex tron base58

基于TRON官方文档,它们具有基于TRONWEB的API以与Javascript一起使用。 https://developers.tron.network/reference#address

但是我正在开发一个后端PHP应用程序,并且想将TRON地址存储为Base58而不是十六进制FORMAT(API输出中的默认值为HEX) 那么有什么方法可以在PHP中将地址从HEX转换为Base58?

(已研究并且找不到任何可能的答案)

2 个答案:

答案 0 :(得分:1)

只需使用以下功能:

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;
}

如果您需要更多功能,请查看this

答案 1 :(得分:0)

是的,可以在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

我真的希望这对您有所帮助。