123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php declare(strict_types=1);
- namespace IEXBase\TronAPI;
- use IEXBase\TronAPI\Support\{Base58Check, BigInteger, Keccak};
- trait TronAwareTrait
- {
-
- public function fromHex($string)
- {
- if(strlen($string) == 42 && mb_substr($string,0,2) === '41') {
- return $this->hexString2Address($string);
- }
- return $this->hexString2Utf8($string);
- }
-
- public function toHex($str)
- {
- if(mb_strlen($str) == 34 && mb_substr($str, 0, 1) === 'T') {
- return $this->address2HexString($str);
- };
- return $this->stringUtf8toHex($str);
- }
-
- public function address2HexString($sHexAddress)
- {
- if(strlen($sHexAddress) == 42 && mb_strpos($sHexAddress, '41') == 0) {
- return $sHexAddress;
- }
- return Base58Check::decode($sHexAddress,0,3);
- }
-
- public function hexString2Address($sHexString)
- {
- if(!ctype_xdigit($sHexString)) {
- return $sHexString;
- }
- if(strlen($sHexString) < 2 || (strlen($sHexString) & 1) != 0) {
- return '';
- }
- return Base58Check::encode($sHexString,0,false);
- }
-
- public function stringUtf8toHex($sUtf8)
- {
- return bin2hex($sUtf8);
- }
-
- public function hexString2Utf8($sHexString)
- {
- return hex2bin($sHexString);
- }
-
- public function toBigNumber($str) {
- return new BigInteger($str);
- }
-
- public function fromTron($amount): float {
- return (float) bcdiv((string)$amount, (string)1e6, 8);
- }
-
- public function toTron($double): int {
- return (int) bcmul((string)$double, (string)1e6,0);
- }
-
- public function sha3($string, $prefix = true)
- {
- return ($prefix ? '0x' : ''). Keccak::hash($string, 256);
- }
- }
|