UtilsTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. <?php
  2. namespace Test\Unit;
  3. use InvalidArgumentException;
  4. use stdClass;
  5. use Test\TestCase;
  6. use phpseclib\Math\BigInteger as BigNumber;
  7. use Web3\Utils;
  8. use Web3\Contract;
  9. class UtilsTest extends TestCase
  10. {
  11. /**
  12. * testHex
  13. * 'hello world'
  14. * you can check by call pack('H*', $hex)
  15. *
  16. * @var string
  17. */
  18. protected $testHex = '68656c6c6f20776f726c64';
  19. /**
  20. * testJsonMethodString
  21. * from GameToken approve function
  22. *
  23. * @var string
  24. */
  25. protected $testJsonMethodString = '{
  26. "constant": false,
  27. "inputs": [
  28. {
  29. "name": "_spender",
  30. "type": "address"
  31. },
  32. {
  33. "name": "_value",
  34. "type": "uint256"
  35. }
  36. ],
  37. "name": "approve",
  38. "outputs": [
  39. {
  40. "name": "success",
  41. "type": "bool"
  42. }
  43. ],
  44. "payable": false,
  45. "stateMutability": "nonpayable",
  46. "type": "function",
  47. "test": {
  48. "name": "testObject"
  49. }
  50. }';
  51. /**
  52. * testIssue112Json
  53. * see: https://github.com/sc0Vu/web3.php/issues/112
  54. *
  55. * @var string
  56. */
  57. protected $testIssue112Json = '[
  58. {
  59. "constant": true,
  60. "inputs": [],
  61. "name": "name",
  62. "outputs": [
  63. {
  64. "name": "",
  65. "type": "string"
  66. }
  67. ],
  68. "payable": false,
  69. "stateMutability": "view",
  70. "type": "function"
  71. },
  72. {
  73. "constant": true,
  74. "inputs": [],
  75. "name": "decimals",
  76. "outputs": [
  77. {
  78. "name": "",
  79. "type": "uint256"
  80. }
  81. ],
  82. "payable": false,
  83. "stateMutability": "view",
  84. "type": "function"
  85. },
  86. {
  87. "constant": true,
  88. "inputs": [
  89. {
  90. "name": "tokenOwner",
  91. "type": "address"
  92. }
  93. ],
  94. "name": "balanceOf",
  95. "outputs": [
  96. {
  97. "name": "balance",
  98. "type": "uint256"
  99. }
  100. ],
  101. "payable": false,
  102. "stateMutability": "view",
  103. "type": "function"
  104. },
  105. {
  106. "constant": false,
  107. "inputs": [
  108. {
  109. "name": "to",
  110. "type": "address"
  111. },
  112. {
  113. "name": "tokens",
  114. "type": "uint256"
  115. }
  116. ],
  117. "name": "transfer",
  118. "outputs": [
  119. {
  120. "name": "success",
  121. "type": "bool"
  122. }
  123. ],
  124. "payable": false,
  125. "stateMutability": "nonpayable",
  126. "type": "function"
  127. }
  128. ]';
  129. /**
  130. * setUp
  131. *
  132. * @return void
  133. */
  134. public function setUp()
  135. {
  136. parent::setUp();
  137. }
  138. /**
  139. * testToHex
  140. *
  141. * @return void
  142. */
  143. public function testToHex()
  144. {
  145. $this->assertEquals($this->testHex, Utils::toHex('hello world'));
  146. $this->assertEquals('0x' . $this->testHex, Utils::toHex('hello world', true));
  147. $this->assertEquals('0x927c0', Utils::toHex(0x0927c0, true));
  148. $this->assertEquals('0x927c0', Utils::toHex('600000', true));
  149. $this->assertEquals('0x927c0', Utils::toHex(600000, true));
  150. $this->assertEquals('0x927c0', Utils::toHex(new BigNumber(600000), true));
  151. $this->assertEquals('0xea60', Utils::toHex(0x0ea60, true));
  152. $this->assertEquals('0xea60', Utils::toHex('60000', true));
  153. $this->assertEquals('0xea60', Utils::toHex(60000, true));
  154. $this->assertEquals('0xea60', Utils::toHex(new BigNumber(60000), true));
  155. $this->assertEquals('0x', Utils::toHex(0x00, true));
  156. $this->assertEquals('0x', Utils::toHex('0', true));
  157. $this->assertEquals('0x', Utils::toHex(0, true));
  158. $this->assertEquals('0x', Utils::toHex(new BigNumber(0), true));
  159. $this->assertEquals('0x30', Utils::toHex(48, true));
  160. $this->assertEquals('0x30', Utils::toHex('48', true));
  161. $this->assertEquals('30', Utils::toHex(48));
  162. $this->assertEquals('30', Utils::toHex('48'));
  163. $this->assertEquals('0x30', Utils::toHex(new BigNumber(48), true));
  164. $this->assertEquals('0x30', Utils::toHex(new BigNumber('48'), true));
  165. $this->assertEquals('30', Utils::toHex(new BigNumber(48)));
  166. $this->assertEquals('30', Utils::toHex(new BigNumber('48')));
  167. $this->expectException(InvalidArgumentException::class);
  168. $hex = Utils::toHex(new stdClass);
  169. }
  170. /**
  171. * testHexToBin
  172. *
  173. * @return void
  174. */
  175. public function testHexToBin()
  176. {
  177. $str = Utils::hexToBin($this->testHex);
  178. $this->assertEquals($str, 'hello world');
  179. $str = Utils::hexToBin('0x' . $this->testHex);
  180. $this->assertEquals($str, 'hello world');
  181. $str = Utils::hexToBin('0xe4b883e5bda9e7a59ee4bb99e9b1bc');
  182. $this->assertEquals($str, '七彩神仙鱼');
  183. $this->expectException(InvalidArgumentException::class);
  184. $str = Utils::hexToBin(new stdClass);
  185. }
  186. /**
  187. * testIsZeroPrefixed
  188. *
  189. * @return void
  190. */
  191. public function testIsZeroPrefixed()
  192. {
  193. $isPrefixed = Utils::isZeroPrefixed($this->testHex);
  194. $this->assertEquals($isPrefixed, false);
  195. $isPrefixed = Utils::isZeroPrefixed('0x' . $this->testHex);
  196. $this->assertEquals($isPrefixed, true);
  197. $this->expectException(InvalidArgumentException::class);
  198. $isPrefixed = Utils::isZeroPrefixed(new stdClass);
  199. }
  200. /**
  201. * testIsAddress
  202. *
  203. * @return void
  204. */
  205. public function testIsAddress()
  206. {
  207. $isAddress = Utils::isAddress('ca35b7d915458ef540ade6068dfe2f44e8fa733c');
  208. $this->assertEquals($isAddress, true);
  209. $isAddress = Utils::isAddress('0xca35b7d915458ef540ade6068dfe2f44e8fa733c');
  210. $this->assertEquals($isAddress, true);
  211. $isAddress = Utils::isAddress('0Xca35b7d915458ef540ade6068dfe2f44e8fa733c');
  212. $this->assertEquals($isAddress, true);
  213. $isAddress = Utils::isAddress('0XCA35B7D915458EF540ADE6068DFE2F44E8FA733C');
  214. $this->assertEquals($isAddress, true);
  215. $isAddress = Utils::isAddress('0xCA35B7D915458EF540ADE6068DFE2F44E8FA733C');
  216. $this->assertEquals($isAddress, true);
  217. $isAddress = Utils::isAddress('0xCA35B7D915458EF540ADE6068DFE2F44E8FA73cc');
  218. $this->assertEquals($isAddress, false);
  219. $this->expectException(InvalidArgumentException::class);
  220. $isAddress = Utils::isAddress(new stdClass);
  221. }
  222. /**
  223. * testIsAddressChecksum
  224. *
  225. * @return void
  226. */
  227. public function testIsAddressChecksum()
  228. {
  229. $isAddressChecksum = Utils::isAddressChecksum('0x52908400098527886E0F7030069857D2E4169EE7');
  230. $this->assertEquals($isAddressChecksum, true);
  231. $isAddressChecksum = Utils::isAddressChecksum('0x8617E340B3D01FA5F11F306F4090FD50E238070D');
  232. $this->assertEquals($isAddressChecksum, true);
  233. $isAddressChecksum = Utils::isAddressChecksum('0xde709f2102306220921060314715629080e2fb77');
  234. $this->assertEquals($isAddressChecksum, true);
  235. $isAddressChecksum = Utils::isAddressChecksum('0x27b1fdb04752bbc536007a920d24acb045561c26');
  236. $this->assertEquals($isAddressChecksum, true);
  237. $isAddressChecksum = Utils::isAddressChecksum('0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed');
  238. $this->assertEquals($isAddressChecksum, true);
  239. $isAddressChecksum = Utils::isAddressChecksum('0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed');
  240. $this->assertEquals($isAddressChecksum, true);
  241. $isAddressChecksum = Utils::isAddressChecksum('0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359');
  242. $this->assertEquals($isAddressChecksum, true);
  243. $isAddressChecksum = Utils::isAddressChecksum('0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB');
  244. $this->assertEquals($isAddressChecksum, true);
  245. $isAddressChecksum = Utils::isAddressChecksum('0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb');
  246. $this->assertEquals($isAddressChecksum, true);
  247. $isAddressChecksum = Utils::isAddressChecksum('0XD1220A0CF47C7B9BE7A2E6BA89F429762E7B9ADB');
  248. $this->assertEquals($isAddressChecksum, false);
  249. $isAddressChecksum = Utils::isAddressChecksum('0xd1220a0cf47c7b9be7a2e6ba89f429762e7b9adb');
  250. $this->assertEquals($isAddressChecksum, false);
  251. $this->expectException(InvalidArgumentException::class);
  252. $isAddressChecksum = Utils::isAddressChecksum(new stdClass);
  253. }
  254. /**
  255. * testToChecksumAddress
  256. *
  257. * @return void
  258. */
  259. public function testToChecksumAddress()
  260. {
  261. $checksumAddressTest = [
  262. // All caps
  263. '0x52908400098527886E0F7030069857D2E4169EE7',
  264. '0x8617E340B3D01FA5F11F306F4090FD50E238070D',
  265. // All Lower
  266. '0xde709f2102306220921060314715629080e2fb77',
  267. '0x27b1fdb04752bbc536007a920d24acb045561c26',
  268. // Normal
  269. '0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed',
  270. '0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359',
  271. '0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB',
  272. '0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb'
  273. ];
  274. for ($i=0; $i<count($checksumAddressTest); $i++) {
  275. $checksumAddress = Utils::toChecksumAddress(strtolower($checksumAddressTest[$i]));
  276. $this->assertEquals($checksumAddressTest[$i], $checksumAddress);
  277. }
  278. }
  279. /**
  280. * testStripZero
  281. *
  282. * @return void
  283. */
  284. public function testStripZero()
  285. {
  286. $str = Utils::stripZero($this->testHex);
  287. $this->assertEquals($str, $this->testHex);
  288. $str = Utils::stripZero('0x' . $this->testHex);
  289. $this->assertEquals($str, $this->testHex);
  290. }
  291. /**
  292. * testSha3
  293. *
  294. * @return void
  295. */
  296. public function testSha3()
  297. {
  298. $str = Utils::sha3('');
  299. $this->assertNull($str);
  300. $str = Utils::sha3('baz(uint32,bool)');
  301. $this->assertEquals(mb_substr($str, 0, 10), '0xcdcd77c0');
  302. $this->expectException(InvalidArgumentException::class);
  303. $str = Utils::sha3(new stdClass);
  304. }
  305. /**
  306. * testToWei
  307. *
  308. * @return void
  309. */
  310. public function testToWei()
  311. {
  312. $bn = Utils::toWei('0x1', 'wei');
  313. $this->assertEquals('1', $bn->toString());
  314. $bn = Utils::toWei('18', 'wei');
  315. $this->assertEquals('18', $bn->toString());
  316. $bn = Utils::toWei('1', 'ether');
  317. $this->assertEquals('1000000000000000000', $bn->toString());
  318. $bn = Utils::toWei('0x5218', 'wei');
  319. $this->assertEquals('21016', $bn->toString());
  320. $bn = Utils::toWei('0.000012', 'ether');
  321. $this->assertEquals('12000000000000', $bn->toString());
  322. $bn = Utils::toWei('0.1', 'ether');
  323. $this->assertEquals('100000000000000000', $bn->toString());
  324. $bn = Utils::toWei('1.69', 'ether');
  325. $this->assertEquals('1690000000000000000', $bn->toString());
  326. $bn = Utils::toWei('0.01', 'ether');
  327. $this->assertEquals('10000000000000000', $bn->toString());
  328. $bn = Utils::toWei('0.002', 'ether');
  329. $this->assertEquals('2000000000000000', $bn->toString());
  330. $bn = Utils::toWei('-0.1', 'ether');
  331. $this->assertEquals('-100000000000000000', $bn->toString());
  332. $bn = Utils::toWei('-1.69', 'ether');
  333. $this->assertEquals('-1690000000000000000', $bn->toString());
  334. $bn = Utils::toWei('', 'ether');
  335. $this->assertEquals('0', $bn->toString());
  336. try {
  337. $bn = Utils::toWei('0x5218', new stdClass);
  338. } catch (InvalidArgumentException $e) {
  339. $this->assertEquals('toWei unit must be string.', $e->getMessage());
  340. }
  341. try {
  342. $bn = Utils::toWei('0x5218', 'test');
  343. } catch (InvalidArgumentException $e) {
  344. $this->assertEquals('toWei doesn\'t support test unit.', $e->getMessage());
  345. }
  346. try {
  347. // out of limit
  348. $bn = Utils::toWei(-1.6977, 'kwei');
  349. } catch (InvalidArgumentException $e) {
  350. $this->assertEquals('toWei number must be string or bignumber.', $e->getMessage());
  351. }
  352. }
  353. /**
  354. * testToEther
  355. *
  356. * @return void
  357. */
  358. public function testToEther()
  359. {
  360. list($bnq, $bnr) = Utils::toEther('0x1', 'wei');
  361. $this->assertEquals($bnq->toString(), '0');
  362. $this->assertEquals($bnr->toString(), '1');
  363. list($bnq, $bnr) = Utils::toEther('18', 'wei');
  364. $this->assertEquals($bnq->toString(), '0');
  365. $this->assertEquals($bnr->toString(), '18');
  366. list($bnq, $bnr) = Utils::toEther('1', 'kether');
  367. $this->assertEquals($bnq->toString(), '1000');
  368. $this->assertEquals($bnr->toString(), '0');
  369. list($bnq, $bnr) = Utils::toEther('0x5218', 'wei');
  370. $this->assertEquals($bnq->toString(), '0');
  371. $this->assertEquals($bnr->toString(), '21016');
  372. list($bnq, $bnr) = Utils::toEther('0x5218', 'ether');
  373. $this->assertEquals($bnq->toString(), '21016');
  374. $this->assertEquals($bnr->toString(), '0');
  375. }
  376. /**
  377. * testFromWei
  378. *
  379. * @return void
  380. */
  381. public function testFromWei()
  382. {
  383. list($bnq, $bnr) = Utils::fromWei('1000000000000000000', 'ether');
  384. $this->assertEquals($bnq->toString(), '1');
  385. $this->assertEquals($bnr->toString(), '0');
  386. list($bnq, $bnr) = Utils::fromWei('18', 'wei');
  387. $this->assertEquals($bnq->toString(), '18');
  388. $this->assertEquals($bnr->toString(), '0');
  389. list($bnq, $bnr) = Utils::fromWei(1, 'femtoether');
  390. $this->assertEquals($bnq->toString(), '0');
  391. $this->assertEquals($bnr->toString(), '1');
  392. list($bnq, $bnr) = Utils::fromWei(0x11, 'nano');
  393. $this->assertEquals($bnq->toString(), '0');
  394. $this->assertEquals($bnr->toString(), '17');
  395. list($bnq, $bnr) = Utils::fromWei('0x5218', 'kwei');
  396. $this->assertEquals($bnq->toString(), '21');
  397. $this->assertEquals($bnr->toString(), '16');
  398. try {
  399. list($bnq, $bnr) = Utils::fromWei('0x5218', new stdClass);
  400. } catch (InvalidArgumentException $e) {
  401. $this->assertTrue($e !== null);
  402. }
  403. try {
  404. list($bnq, $bnr) = Utils::fromWei('0x5218', 'test');
  405. } catch (InvalidArgumentException $e) {
  406. $this->assertTrue($e !== null);
  407. }
  408. }
  409. /**
  410. * testJsonMethodToString
  411. *
  412. * @return void
  413. */
  414. public function testJsonMethodToString()
  415. {
  416. $json = json_decode($this->testJsonMethodString);
  417. $methodString = Utils::jsonMethodToString($json);
  418. $this->assertEquals($methodString, 'approve(address,uint256)');
  419. $json = json_decode($this->testJsonMethodString, true);
  420. $methodString = Utils::jsonMethodToString($json);
  421. $this->assertEquals($methodString, 'approve(address,uint256)');
  422. $methodString = Utils::jsonMethodToString([
  423. 'name' => 'approve(address,uint256)'
  424. ]);
  425. $this->assertEquals($methodString, 'approve(address,uint256)');
  426. $this->expectException(InvalidArgumentException::class);
  427. $methodString = Utils::jsonMethodToString('test');
  428. }
  429. /**
  430. * testJsonToArray
  431. *
  432. * @return void
  433. */
  434. public function testJsonToArray()
  435. {
  436. $decodedJson = json_decode($this->testJsonMethodString);
  437. $jsonArray = Utils::jsonToArray($decodedJson);
  438. $jsonAssoc = json_decode($this->testJsonMethodString, true);
  439. $jsonArray2 = Utils::jsonToArray($jsonAssoc);
  440. $this->assertEquals($jsonAssoc, $jsonArray);
  441. $this->assertEquals($jsonAssoc, $jsonArray2);
  442. $jsonAssoc = json_decode($this->testIssue112Json, true);
  443. $jsonArray = Utils::jsonToArray($jsonAssoc);
  444. $this->assertEquals($jsonAssoc, $jsonArray);
  445. }
  446. /**
  447. * testIsHex
  448. *
  449. * @return void
  450. */
  451. public function testIsHex()
  452. {
  453. $isHex = Utils::isHex($this->testHex);
  454. $this->assertTrue($isHex);
  455. $isHex = Utils::isHex('0x' . $this->testHex);
  456. $this->assertTrue($isHex);
  457. $isHex = Utils::isHex('hello world');
  458. $this->assertFalse($isHex);
  459. }
  460. /**
  461. * testIsNegative
  462. *
  463. * @return void
  464. */
  465. public function testIsNegative()
  466. {
  467. $isNegative = Utils::isNegative('-1');
  468. $this->assertTrue($isNegative);
  469. $isNegative = Utils::isNegative('1');
  470. $this->assertFalse($isNegative);
  471. }
  472. /**
  473. * testToBn
  474. *
  475. * @return void
  476. */
  477. public function testToBn()
  478. {
  479. $bn = Utils::toBn('');
  480. $this->assertEquals($bn->toString(), '0');
  481. $bn = Utils::toBn(11);
  482. $this->assertEquals($bn->toString(), '11');
  483. $bn = Utils::toBn('0x12');
  484. $this->assertEquals($bn->toString(), '18');
  485. $bn = Utils::toBn('-0x12');
  486. $this->assertEquals($bn->toString(), '-18');
  487. $bn = Utils::toBn(0x12);
  488. $this->assertEquals($bn->toString(), '18');
  489. $bn = Utils::toBn('ae');
  490. $this->assertEquals($bn->toString(), '174');
  491. $bn = Utils::toBn('-ae');
  492. $this->assertEquals($bn->toString(), '-174');
  493. $bn = Utils::toBn('-1');
  494. $this->assertEquals($bn->toString(), '-1');
  495. $bn = Utils::toBn('-0.1');
  496. $this->assertEquals(count($bn), 4);
  497. $this->assertEquals($bn[0]->toString(), '0');
  498. $this->assertEquals($bn[1]->toString(), '1');
  499. $this->assertEquals($bn[2], 1);
  500. $this->assertEquals($bn[3]->toString(), '-1');
  501. $bn = Utils::toBn(-0.1);
  502. $this->assertEquals(count($bn), 4);
  503. $this->assertEquals($bn[0]->toString(), '0');
  504. $this->assertEquals($bn[1]->toString(), '1');
  505. $this->assertEquals($bn[2], 1);
  506. $this->assertEquals($bn[3]->toString(), '-1');
  507. $bn = Utils::toBn('0.1');
  508. $this->assertEquals(count($bn), 4);
  509. $this->assertEquals($bn[0]->toString(), '0');
  510. $this->assertEquals($bn[1]->toString(), '1');
  511. $this->assertEquals($bn[2], 1);
  512. $this->assertEquals($bn[3], false);
  513. $bn = Utils::toBn('-1.69');
  514. $this->assertEquals(count($bn), 4);
  515. $this->assertEquals($bn[0]->toString(), '1');
  516. $this->assertEquals($bn[1]->toString(), '69');
  517. $this->assertEquals($bn[2], 2);
  518. $this->assertEquals($bn[3]->toString(), '-1');
  519. $bn = Utils::toBn(-1.69);
  520. $this->assertEquals($bn[0]->toString(), '1');
  521. $this->assertEquals($bn[1]->toString(), '69');
  522. $this->assertEquals($bn[2], 2);
  523. $this->assertEquals($bn[3]->toString(), '-1');
  524. $bn = Utils::toBn('1.69');
  525. $this->assertEquals(count($bn), 4);
  526. $this->assertEquals($bn[0]->toString(), '1');
  527. $this->assertEquals($bn[1]->toString(), '69');
  528. $this->assertEquals($bn[2], 2);
  529. $this->assertEquals($bn[3], false);
  530. $bn = Utils::toBn(new BigNumber(1));
  531. $this->assertEquals($bn->toString(), '1');
  532. $this->expectException(InvalidArgumentException::class);
  533. $bn = Utils::toBn(new stdClass);
  534. }
  535. }