TronAwareTrait.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php declare(strict_types=1);
  2. namespace IEXBase\TronAPI;
  3. use IEXBase\TronAPI\Support\{Base58Check, BigInteger, Keccak};
  4. trait TronAwareTrait
  5. {
  6. /**
  7. * Convert from Hex
  8. *
  9. * @param $string
  10. * @return string
  11. */
  12. public function fromHex($string)
  13. {
  14. if(strlen($string) == 42 && mb_substr($string,0,2) === '41') {
  15. return $this->hexString2Address($string);
  16. }
  17. return $this->hexString2Utf8($string);
  18. }
  19. /**
  20. * Convert to Hex
  21. *
  22. * @param $str
  23. * @return string
  24. */
  25. public function toHex($str)
  26. {
  27. if(mb_strlen($str) == 34 && mb_substr($str, 0, 1) === 'T') {
  28. return $this->address2HexString($str);
  29. };
  30. return $this->stringUtf8toHex($str);
  31. }
  32. /**
  33. * Check the address before converting to Hex
  34. *
  35. * @param $sHexAddress
  36. * @return string
  37. */
  38. public function address2HexString($sHexAddress)
  39. {
  40. if(strlen($sHexAddress) == 42 && mb_strpos($sHexAddress, '41') == 0) {
  41. return $sHexAddress;
  42. }
  43. return Base58Check::decode($sHexAddress,0,3);
  44. }
  45. /**
  46. * Check Hex address before converting to Base58
  47. *
  48. * @param $sHexString
  49. * @return string
  50. */
  51. public function hexString2Address($sHexString)
  52. {
  53. if(!ctype_xdigit($sHexString)) {
  54. return $sHexString;
  55. }
  56. if(strlen($sHexString) < 2 || (strlen($sHexString) & 1) != 0) {
  57. return '';
  58. }
  59. return Base58Check::encode($sHexString,0,false);
  60. }
  61. /**
  62. * Convert string to hex
  63. *
  64. * @param $sUtf8
  65. * @return string
  66. */
  67. public function stringUtf8toHex($sUtf8)
  68. {
  69. return bin2hex($sUtf8);
  70. }
  71. /**
  72. * Convert hex to string
  73. *
  74. * @param $sHexString
  75. * @return string
  76. */
  77. public function hexString2Utf8($sHexString)
  78. {
  79. return hex2bin($sHexString);
  80. }
  81. /**
  82. * Convert to great value
  83. *
  84. * @param $str
  85. * @return BigInteger
  86. */
  87. public function toBigNumber($str) {
  88. return new BigInteger($str);
  89. }
  90. /**
  91. * Convert trx to float
  92. *
  93. * @param $amount
  94. * @return float
  95. */
  96. public function fromTron($amount): float {
  97. return (float) bcdiv((string)$amount, (string)1e6, 8);
  98. }
  99. /**
  100. * Convert float to trx format
  101. *
  102. * @param $double
  103. * @return int
  104. */
  105. public function toTron($double): int {
  106. return (int) bcmul((string)$double, (string)1e6,0);
  107. }
  108. /**
  109. * Convert to SHA3
  110. *
  111. * @param $string
  112. * @param bool $prefix
  113. * @return string
  114. * @throws \Exception
  115. */
  116. public function sha3($string, $prefix = true)
  117. {
  118. return ($prefix ? '0x' : ''). Keccak::hash($string, 256);
  119. }
  120. }