【问题标题】:PHP Convert TRON Hex address to Base58PHP 将 TRON 十六进制地址转换为 Base58
【发布时间】:2021-01-04 03:20:50
【问题描述】:

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

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

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

【问题讨论】:

    标签: php hex tron base58


    【解决方案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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-08-08
        • 1970-01-01
        • 1970-01-01
        • 2017-04-23
        • 1970-01-01
        • 2016-11-04
        • 1970-01-01
        • 2013-11-16
        相关资源
        最近更新 更多