Ethereum PHP
PHP interface to Ethereum JSON-RPC API.
EthD.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Ethereum\DataType;
4 use Ethereum\Abi;
5 
6 
15 class EthD extends EthDataType
16 {
17 
18  protected $value;
19 
20  protected $abi;
21 
27  private const SCHEMA_MAP = [
28  // Bytes data.
29  'D' => 'EthD',
30  // Bytes data, length 20
31  // 40 hex characters, 160 bits. E.g Ethereum Address.
32  'D20' => 'EthD20',
33  // Bytes data, length 32
34  // 64 hex characters, 256 bits. Eg. TX hash.
35  'D32' => 'EthD32',
36  // Number quantity.
37  'Q' => 'EthQ',
38  // Boolean.
39  'B' => 'EthB',
40  // String data.
41  'S' => 'EthS',
42  // Default block parameter: Address/D20 or tag [latest|earliest|pending].
43  'Q|T' => 'EthBlockParam',
44  // Either an array of DATA or a single bytes DATA with variable length.
45  'Array|DATA' => 'EthBytes',
46  // Derived ABI types
47  'bool' => 'EthB',
48  // WORKAROUND? Some clients may return an Data Array. Works on testrpc.
49  'B|EthSyncing' => 'EthB',
50  // EthSyncing ?
51  // WORKAROUND? Some clients may return an Data Array. Works on testrpc.
52  'DATA|Transaction' => 'Transaction',
53  'FilterChange' => 'FilterChange'
54  ];
55 
56 
62  private const ABI_MAP = [
63  // The following elementary types exist:
64  'uint' => 'EthQ',
65  'int' => 'EthQ',
66  'address' => 'EthD20',
67  // = uint 160?
68  'bool' => 'EthB',
69  // function, an address (20 bytes) followed by a function selector (4 bytes).
70  // Encoded identical to bytes24
71  // 'function' => ''
72 
73  // Fixed signed fixed-point decimal number of M bits, 8 <= M <= 256
74  // @todo fixed-point decimal number not implemented.
75  // 'fixed' => 'EthB',
76  // 'ufixed' => 'EthS',
77 
78  // string: dynamic sized unicode string assumed to be UTF-8 encoded.
79  'string' => 'EthS',
80  // bytes: dynamic sized byte sequence.
81  'bytes' => 'EthBytes',
82 
83  // Small Bytes < 32
84  // Are always padded to 32bytes (64 chars in hex).
85  //
86  // bytes<M>: enc(X) is the sequence of bytes in X padded
87  // with trailing zero-bytes to a length of 32 bytes.
88  //
89  // bytes<M>: binary type of M bytes, 0 < M <= 32
90  'bytes1' => 'EthD32',
91  'bytes2' => 'EthD32',
92  'bytes3' => 'EthD32',
93  'bytes4' => 'EthD32',
94  'bytes5' => 'EthD32',
95  'bytes6' => 'EthD32',
96  'bytes7' => 'EthD32',
97  'bytes8' => 'EthD32',
98  'bytes9' => 'EthD32',
99  'bytes10' => 'EthD32',
100  'bytes11' => 'EthD32',
101  'bytes12' => 'EthD32',
102  'bytes13' => 'EthD32',
103  'bytes14' => 'EthD32',
104  'bytes15' => 'EthD32',
105  'bytes16' => 'EthD32',
106  'bytes17' => 'EthD32',
107  'bytes18' => 'EthD32',
108  'bytes19' => 'EthD32',
109  'bytes20' => 'EthD20',
110  'bytes21' => 'EthD32',
111  'bytes22' => 'EthD32',
112  'bytes23' => 'EthD32',
113  'bytes24' => 'EthD32',
114  'bytes25' => 'EthD32',
115  'bytes26' => 'EthD32',
116  'bytes27' => 'EthD32',
117  'bytes28' => 'EthD32',
118  'bytes29' => 'EthD32',
119  'bytes30' => 'EthD32',
120  'bytes31' => 'EthD32',
121  'bytes32' => 'EthD32',
122  // Simple byte values
123 
124 
125  // @todo Function not implemented.
126  // An address (20 bytes) followed by a function selector (4 bytes).
127  // Encoded identical to bytes24
128  // 'function' => 'EthBytes',
129  ];
130 
131 
143  public function __construct($val, array $params = [])
144  {
145  $this->setValue($val, $params);
146  }
147 
148 
164  public function convertByAbi($abiType)
165  {
166  return Abi::convertByAbi($abiType, $this);
167  }
168 
169 
184  public function validate($val, array $params)
185  {
186  $val = $this->ensureHexPrefix($val);
187 
188  if ($this->validateHexString($val)) {
189 
190  // All Hex strings are lowercase.
191  $val = strtolower($val);
192  if (method_exists($this, 'validateLength')) {
193  $val = call_user_func([$this, 'validateLength'], $val);
194  }
195 
196  return $val;
197 
198  } else {
199  throw new \InvalidArgumentException('Can not decode hex binary: ' . $val);
200  }
201  }
202 
217  public function validateHexString($val)
218  {
219  if ($val === '0x') {
220  $val = '0x0';
221  }
222  if (!ctype_xdigit(substr($val, 2))) {
223  throw new \InvalidArgumentException('A non well formed hex value encountered: ' . $val);
224  }
225  return true;
226  }
227 
228 
236  public static function getPrimitiveTypes()
237  {
238  return ['EthD', 'EthD20', 'EthD32', 'EthQ', 'EthB', 'EthS'];
239  }
240 
247  public static function isPrimitive()
248  {
249  return true;
250  }
251 
261  public static function typeMap(string $type)
262  {
263  $map = self::SCHEMA_MAP;
264  if (isset($map[$type])) {
265  return $map[$type];
266  }
267  else {
268  return null;
269  }
270  }
271 
283  public static function reverseTypeMap($class_name)
284  {
285  $schema_type = array_search($class_name, self::SCHEMA_MAP);
286  if (is_string($schema_type)) {
287  return $schema_type;
288  }
289  else {
290  throw new \Exception('Could not determine data type.');
291  }
292  }
293 
302  public static function getSchemaType()
303  {
304  $class_name = get_called_class();
305  if (substr($class_name, 0, strlen(__NAMESPACE__)) === __NAMESPACE__) {
306  // Cut of namespace and Slash. E.g "Ethereum\".
307  $class_name = substr(get_called_class(), strlen(__NAMESPACE__) + 1);
308  }
309  return self::reverseTypeMap($class_name);
310  }
311 
325  public function convertTo($type)
326  {
327  $class = '\Ethereum\\DataType\\' . $this->typeMap($type);
328  $obj = new $class($this->hexVal());
329  return $obj;
330  }
331 
340  public static function getTypeArray()
341  {
342  return array(
343  'value' => get_class(),
344  );
345  }
346 
353  public function toArray()
354  {
355  return array(
356  'value' => $this->value,
357  );
358  }
359 
360 
368  public function hexVal()
369  {
370  return $this->value;
371  }
372 
376  public function encodedHexVal() {
377  // Default for non Rlp, fixed length types.
378  return $this->value;
379  }
380 
389  public function val()
390  {
391  return substr($this->value, 2);
392  }
393 
394 }
static getTypeArray()
Definition: EthD.php:340
static getSchemaType()
Definition: EthD.php:302
const const __construct($val, array $params=[])
Definition: EthD.php:143
validate($val, array $params)
Definition: EthD.php:184
convertByAbi($abiType)
Definition: EthD.php:164
validateHexString($val)
Definition: EthD.php:217
static reverseTypeMap($class_name)
Definition: EthD.php:283
static isPrimitive()
Definition: EthD.php:247
static typeMap(string $type)
Definition: EthD.php:261
setValue($val, array $params=[])
static convertByAbi(string $abiType, EthD $value)
Definition: Abi.php:206
static getPrimitiveTypes()
Definition: EthD.php:236