UtilsTest.php 18 KB

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