0

基于官方 TRON 文档,他们有基于 TRONWEB 的 API 来使用 Javascript。 https://developers.tron.network/reference#address

但是我正在开发一个后端 PHP 应用程序,并希望将 TRON 地址存储为 Base58 而不是十六进制格式(API 输出中默认为 HEX) 那么有没有办法将地址从 HEX 转换为 PHP 中的 Base58?

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

4

2 回答 2

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

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

于 2021-02-23T19:39:17.620 回答
0

是的,可以在 php.ini 中进行转换。

首先,iexbase/tron-api通过composer安装包。

我的做法是,我有一个名为的类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

我真的希望这对你有帮助。

于 2020-09-29T15:10:15.290 回答