Ethereum PHP
PHP interface to Ethereum JSON-RPC API.
EthereumStatic.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Ethereum;
4 
6 
10 abstract class EthereumStatic
11 {
29  public static function getMethodSignature($input)
30  {
31 
32  if (self::isValidFunction($input)) {
33  // The signature is 4bytes of the methods keccac hash. E.g: "0x00000000".
34  return self::ensureHexPrefix(substr(self::sha3($input), 0, 10));
35  }
36  else {
37  throw new \InvalidArgumentException("No valid (solidity) signature string provided.");
38  }
39  }
40 
61  public static function phpKeccak256($string)
62  {
63  return self::ensureHexPrefix(Keccak::hash($string, 256));
64  }
65 
80  public static function sha3($string)
81  {
82  return self::phpKeccak256($string);
83  }
84 
105  public static function isValidFunction($input)
106  {
107  // Check for function and Params.
108  // See: https://regex101.com/r/437FZz/4
109  $regex = '/^[a-zA-Z]+[a-zA-Z0-9]*[\(]{1}(([\w\d\[\]*){1}(\,[\w\d\[\]]*[\w\d\[\]]*)*)[\)]{1}$/';
110  if (is_string($input) && preg_match($regex, $input) === 1) {
111  return true;
112  }
113 
114  return false;
115  }
116 
124  public static function getDefinition()
125  {
126  $schema_path = __DIR__ . '/../resources/ethjs-schema.json';
127  return json_decode(file_get_contents($schema_path), true);
128  }
129 
154  public static function isValidHexQuantity($str)
155  {
156 
157  // Always ensure 0x prefix.
158  if (!EthereumStatic::hasHexPrefix($str)) {
159  return false;
160  }
161 
162  // Should always have at least one digit - zero is "0x0".
163  if (strlen($str) < 3) {
164  return false;
165  }
166 
167  return ctype_xdigit(self::removeHexPrefix($str));
168  }
169 
182  public static function isValidHexData($str)
183  {
184 
185  // Always ensure 0x prefix.
186  if (!EthereumStatic::hasHexPrefix($str)) {
187  return false;
188  }
189  // Should always have at least one digit - zero is "0x0".
190  if (strlen($str) <= 3) {
191  return false;
192  }
193 
194  // Ensure two hex digits per byte.
195  if ((strlen($str) % 2 != 0)) {
196  return false;
197  }
198 
199  return ctype_xdigit(self::removeHexPrefix($str));
200  }
201 
216  public static function isValidAddress($address, $throw = false)
217  {
218 
219  if (!self::hasHexPrefix($address)) {
220  return false;
221  }
222  // Address should be 20bytes=40 HEX-chars + prefix.
223  if (strlen($address) !== 42) {
224  return false;
225  }
226  $return = ctype_xdigit(self::removeHexPrefix($address));
227  return $return;
228  }
229 
239  public static function hasHexPrefix($str)
240  {
241  return substr($str, 0, 2) === '0x';
242  }
243 
253  public static function removeHexPrefix($str)
254  {
255  if (!self::hasHexPrefix($str)) {
256  return $str;
257  }
258 
259  return substr($str, 2);
260  }
261 
271  public static function ensureHexPrefix($str)
272  {
273  if (self::hasHexPrefix($str)) {
274  return $str;
275  }
276  return '0x' . $str;
277  }
278 
291  public static function convertCurrency(float $amount, string $from = 'wei', string $to = 'ether') {
292 
293  // relative to Ether
294  $convertTabe = [
295  'wei' => 1000000000000000000,
296  // Kwei, Ada, Femtoether
297  'kwei' => 1000000000000000,
298  // Mwei, Babbage, Picoether
299  'mwei' => 1000000000000,
300  // Gwei, Shannon, Nanoether, Nano
301  'gwei' => 1000000000,
302  // Szabo, Microether,Micro
303  'gwei' => 1000000,
304  // Finney, Milliether,Milli
305  'methere' => 1000,
306  'ether' => 1,
307  // Kether, Grand,Einstein
308  'kether' => 0.001,
309  // Kether, Grand,Einstein
310  'mether' => 0.000001,
311  'gether' => 0.000000001,
312  'thether' => 0.000000000001,
313  ];
314  if (!isset($convertTabe[$from])) {
315  throw new \Exception('Inknown currency to convert from "' . $from . '"');
316  }
317  if (!isset($convertTabe[$to])) {
318  throw new \Exception('Inknown currency to convert to "' . $to . '"');
319  }
320  return $convertTabe[$to] * $amount / $convertTabe[$from];
321  }
322 }
static phpKeccak256($string)
static isValidAddress($address, $throw=false)
static getMethodSignature($input)
static isValidFunction($input)
static convertCurrency(float $amount, string $from='wei', string $to='ether')
static isValidHexQuantity($str)
Definition: Abi.php:3