| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 | 
							- <?php
 
- namespace App\Services\Web3;
 
- use kornrunner\Keccak;
 
- use stdClass;
 
- class Utils
 
- {
 
-     const SHA3_NULL_HASH = 'c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470';
 
-     public static function remove0x($value)
 
-     {
 
-         if (strtolower(substr($value, 0, 2)) == '0x') {
 
-             return substr($value, 2);
 
-         }
 
-         return $value;
 
-     }
 
-     public static function add0x($value): string
 
-     {
 
-         return '0x' . self::remove0x($value);
 
-     }
 
-     public static function pubKeyToAddress($pubkey)
 
-     {
 
-         return '0x' . substr(Keccak::hash(substr(hex2bin($pubkey), 1), 256), 24);
 
-     }
 
-     /**
 
-      * RLPencode
 
-      */
 
-     public static function rawEncode(array $input): string
 
-     {
 
-         $rlp = new RLP\RLP;
 
-         $data = [];
 
-         foreach ($input as $item) {
 
-             // If the value is invalid: 0, 0x0, list it as an empty string
 
-             $data[] = $item && hexdec(Utils::remove0x($item)) != 0 ? Utils::add0x($item) : '';
 
-         }
 
-         return $rlp->encode($data)->toString('hex');
 
-     }
 
-     /**
 
-      *
 
-      * @param string $str
 
-      * @param int $bit
 
-      * @return string
 
-      */
 
-     public static function fill0($str, $bit = 64)
 
-     {
 
-         $str_len = strlen($str);
 
-         $zero = '';
 
-         for ($i = $str_len; $i < $bit; $i++) {
 
-             $zero .= "0";
 
-         }
 
-         $real_str = $zero . $str;
 
-         return $real_str;
 
-     }
 
-     /**
 
-      * ether to wei
 
-      */
 
-     public static function ethToWei($value, $hex = false)
 
-     {
 
-         $value = bcmul($value, '1000000000000000000');
 
-         if ($hex) {
 
-             return self::decToHex($value, $hex);
 
-         }
 
-         return $value;
 
-     }
 
-     /**
 
-      * wei to ether
 
-      */
 
-     public static function weiToEth($value, $hex = false)
 
-     {
 
-         if (strtolower(substr($value, 0, 2)) == '0x') {
 
-             $value = self::hexToDec(self::remove0x($value));
 
-         }
 
-         $value = bcdiv($value, '1000000000000000000', 18);
 
-         if ($hex) {
 
-             return '0x' . self::decToHex($value);
 
-         }
 
-         return $value;
 
-     }
 
-     /**
 
-      * change to hex(0x)
 
-      * @param string|number $value
 
-      * @param boolean $mark
 
-      * @return string
 
-      */
 
-     public static function decToHex($value, $mark = true)
 
-     {
 
-         $hexvalues = [
 
-             '0', '1', '2', '3', '4', '5', '6', '7',
 
-             '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
 
-         ];
 
-         $hexval = '';
 
-         while ($value != '0') {
 
-             $hexval = $hexvalues[bcmod($value, '16')] . $hexval;
 
-             $value = bcdiv($value, '16', 0);
 
-         }
 
-         return ($mark ? '0x' . $hexval : $hexval);
 
-     }
 
-     /**
 
-      * change to hex(0x)
 
-      * @param string $number hex number
 
-      * @return string
 
-      */
 
-     public static function hexToDec($number)
 
-     {
 
-         // have 0x,remove it
 
-         $number = self::remove0x(strtolower($number));
 
-         $decvalues = [
 
-             '0' => '0', '1' => '1', '2' => '2',
 
-             '3' => '3', '4' => '4', '5' => '5',
 
-             '6' => '6', '7' => '7', '8' => '8',
 
-             '9' => '9', 'a' => '10', 'b' => '11',
 
-             'c' => '12', 'd' => '13', 'e' => '14',
 
-             'f' => '15'];
 
-         $decval = '0';
 
-         $number = strrev($number);
 
-         for ($i = 0; $i < strlen($number); $i++) {
 
-             $decval = bcadd(bcmul(bcpow('16', $i, 0), $decvalues[$number[$i]]), $decval);
 
-         }
 
-         return $decval;
 
-     }
 
-     /**
 
-      * jsonMethodToString
 
-      *
 
-      * @param stdClass|array $json
 
-      * @return string
 
-      */
 
-     public static function jsonMethodToString($json)
 
-     {
 
-         if ($json instanceof stdClass) {
 
-             // one way to change whole json stdClass to array type
 
-             // $jsonString = json_encode($json);
 
-             // if (JSON_ERROR_NONE !== json_last_error()) {
 
-             //     throw new InvalidArgumentException('json_decode error: ' . json_last_error_msg());
 
-             // }
 
-             // $json = json_decode($jsonString, true);
 
-             // another way to change whole json to array type but need the depth
 
-             // $json = self::jsonToArray($json, $depth)
 
-             // another way to change json to array type but not whole json stdClass
 
-             $json = (array)$json;
 
-             $typeName = [];
 
-             foreach ($json['inputs'] as $param) {
 
-                 if (isset($param->type)) {
 
-                     $typeName[] = $param->type;
 
-                 }
 
-             }
 
-             return $json['name'] . '(' . implode(',', $typeName) . ')';
 
-         } elseif (!is_array($json)) {
 
-             throw new \Exception('jsonMethodToString json must be array or stdClass.');
 
-         }
 
-         if (isset($json['name']) && strpos($json['name'], '(') > 0) {
 
-             return $json['name'];
 
-         }
 
-         $typeName = [];
 
-         foreach ($json['inputs'] as $param) {
 
-             if (isset($param['type'])) {
 
-                 $typeName[] = $param['type'];
 
-             }
 
-         }
 
-         return $json['name'] . '(' . implode(',', $typeName) . ')';
 
-     }
 
-     /**
 
-      * sha3
 
-      * keccak256
 
-      *
 
-      * @param string $value
 
-      * @return string
 
-      */
 
-     public static function sha3($value)
 
-     {
 
-         if (!is_string($value)) {
 
-             throw new InvalidArgumentException('The value to sha3 function must be string.');
 
-         }
 
-         if (strpos($value, '0x') === 0) {
 
-             $value = self::hexToBin($value);
 
-         }
 
-         $hash = Keccak::hash($value, 256);
 
-         if ($hash === self::SHA3_NULL_HASH) {
 
-             return null;
 
-         }
 
-         return '0x' . $hash;
 
-     }
 
-     /**
 
-      * hexToBin
 
-      *
 
-      * @param string
 
-      * @return string
 
-      */
 
-     public static function hexToBin($value)
 
-     {
 
-         if (!is_string($value)) {
 
-             throw new \Exception('The value to hexToBin function must be string.');
 
-         }
 
-         if (self::isZeroPrefixed($value)) {
 
-             $count = 1;
 
-             $value = str_replace('0x', '', $value, $count);
 
-         }
 
-         return pack('H*', $value);
 
-     }
 
-     /**
 
-      * isZeroPrefixed
 
-      *
 
-      * @param string
 
-      * @return bool
 
-      */
 
-     public static function isZeroPrefixed($value)
 
-     {
 
-         if (!is_string($value)) {
 
-             throw new \Exception('The value to isZeroPrefixed function must be string.');
 
-         }
 
-         return (strpos($value, '0x') === 0);
 
-     }
 
-     public static function hexToString($value){
 
-         return pack("H*",$value);
 
-     }
 
- }
 
 
  |