web3ext.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. // package web3ext contains geth specific web3.js extensions.
  17. package web3ext
  18. var Modules = map[string]string{
  19. "admin": Admin_JS,
  20. "chequebook": Chequebook_JS,
  21. "clique": Clique_JS,
  22. "ethash": Ethash_JS,
  23. "debug": Debug_JS,
  24. "eth": Eth_JS,
  25. "miner": Miner_JS,
  26. "net": Net_JS,
  27. "personal": Personal_JS,
  28. "rpc": RPC_JS,
  29. "shh": Shh_JS,
  30. "swarmfs": SWARMFS_JS,
  31. "txpool": TxPool_JS,
  32. }
  33. const Chequebook_JS = `
  34. web3._extend({
  35. property: 'chequebook',
  36. methods: [
  37. new web3._extend.Method({
  38. name: 'deposit',
  39. call: 'chequebook_deposit',
  40. params: 1,
  41. inputFormatter: [null]
  42. }),
  43. new web3._extend.Property({
  44. name: 'balance',
  45. getter: 'chequebook_balance',
  46. outputFormatter: web3._extend.utils.toDecimal
  47. }),
  48. new web3._extend.Method({
  49. name: 'cash',
  50. call: 'chequebook_cash',
  51. params: 1,
  52. inputFormatter: [null]
  53. }),
  54. new web3._extend.Method({
  55. name: 'issue',
  56. call: 'chequebook_issue',
  57. params: 2,
  58. inputFormatter: [null, null]
  59. }),
  60. ]
  61. });
  62. `
  63. const Clique_JS = `
  64. web3._extend({
  65. property: 'clique',
  66. methods: [
  67. new web3._extend.Method({
  68. name: 'getSnapshot',
  69. call: 'clique_getSnapshot',
  70. params: 1,
  71. inputFormatter: [null]
  72. }),
  73. new web3._extend.Method({
  74. name: 'getSnapshotAtHash',
  75. call: 'clique_getSnapshotAtHash',
  76. params: 1
  77. }),
  78. new web3._extend.Method({
  79. name: 'getSigners',
  80. call: 'clique_getSigners',
  81. params: 1,
  82. inputFormatter: [null]
  83. }),
  84. new web3._extend.Method({
  85. name: 'getSignersAtHash',
  86. call: 'clique_getSignersAtHash',
  87. params: 1
  88. }),
  89. new web3._extend.Method({
  90. name: 'propose',
  91. call: 'clique_propose',
  92. params: 2
  93. }),
  94. new web3._extend.Method({
  95. name: 'discard',
  96. call: 'clique_discard',
  97. params: 1
  98. }),
  99. ],
  100. properties: [
  101. new web3._extend.Property({
  102. name: 'proposals',
  103. getter: 'clique_proposals'
  104. }),
  105. ]
  106. });
  107. `
  108. const Ethash_JS = `
  109. web3._extend({
  110. property: 'ethash',
  111. methods: [
  112. new web3._extend.Method({
  113. name: 'getWork',
  114. call: 'ethash_getWork',
  115. params: 0
  116. }),
  117. new web3._extend.Method({
  118. name: 'getHashrate',
  119. call: 'ethash_getHashrate',
  120. params: 0
  121. }),
  122. new web3._extend.Method({
  123. name: 'submitWork',
  124. call: 'ethash_submitWork',
  125. params: 3,
  126. }),
  127. new web3._extend.Method({
  128. name: 'submitHashRate',
  129. call: 'ethash_submitHashRate',
  130. params: 2,
  131. }),
  132. ]
  133. });
  134. `
  135. const Admin_JS = `
  136. web3._extend({
  137. property: 'admin',
  138. methods: [
  139. new web3._extend.Method({
  140. name: 'addPeer',
  141. call: 'admin_addPeer',
  142. params: 1
  143. }),
  144. new web3._extend.Method({
  145. name: 'removePeer',
  146. call: 'admin_removePeer',
  147. params: 1
  148. }),
  149. new web3._extend.Method({
  150. name: 'addTrustedPeer',
  151. call: 'admin_addTrustedPeer',
  152. params: 1
  153. }),
  154. new web3._extend.Method({
  155. name: 'removeTrustedPeer',
  156. call: 'admin_removeTrustedPeer',
  157. params: 1
  158. }),
  159. new web3._extend.Method({
  160. name: 'exportChain',
  161. call: 'admin_exportChain',
  162. params: 1,
  163. inputFormatter: [null]
  164. }),
  165. new web3._extend.Method({
  166. name: 'importChain',
  167. call: 'admin_importChain',
  168. params: 1
  169. }),
  170. new web3._extend.Method({
  171. name: 'sleepBlocks',
  172. call: 'admin_sleepBlocks',
  173. params: 2
  174. }),
  175. new web3._extend.Method({
  176. name: 'startRPC',
  177. call: 'admin_startRPC',
  178. params: 4,
  179. inputFormatter: [null, null, null, null]
  180. }),
  181. new web3._extend.Method({
  182. name: 'stopRPC',
  183. call: 'admin_stopRPC'
  184. }),
  185. new web3._extend.Method({
  186. name: 'startWS',
  187. call: 'admin_startWS',
  188. params: 4,
  189. inputFormatter: [null, null, null, null]
  190. }),
  191. new web3._extend.Method({
  192. name: 'stopWS',
  193. call: 'admin_stopWS'
  194. }),
  195. ],
  196. properties: [
  197. new web3._extend.Property({
  198. name: 'nodeInfo',
  199. getter: 'admin_nodeInfo'
  200. }),
  201. new web3._extend.Property({
  202. name: 'peers',
  203. getter: 'admin_peers'
  204. }),
  205. new web3._extend.Property({
  206. name: 'datadir',
  207. getter: 'admin_datadir'
  208. }),
  209. ]
  210. });
  211. `
  212. const Debug_JS = `
  213. web3._extend({
  214. property: 'debug',
  215. methods: [
  216. new web3._extend.Method({
  217. name: 'printBlock',
  218. call: 'debug_printBlock',
  219. params: 1
  220. }),
  221. new web3._extend.Method({
  222. name: 'getBlockRlp',
  223. call: 'debug_getBlockRlp',
  224. params: 1
  225. }),
  226. new web3._extend.Method({
  227. name: 'setHead',
  228. call: 'debug_setHead',
  229. params: 1
  230. }),
  231. new web3._extend.Method({
  232. name: 'seedHash',
  233. call: 'debug_seedHash',
  234. params: 1
  235. }),
  236. new web3._extend.Method({
  237. name: 'dumpBlock',
  238. call: 'debug_dumpBlock',
  239. params: 1
  240. }),
  241. new web3._extend.Method({
  242. name: 'chaindbProperty',
  243. call: 'debug_chaindbProperty',
  244. params: 1,
  245. outputFormatter: console.log
  246. }),
  247. new web3._extend.Method({
  248. name: 'chaindbCompact',
  249. call: 'debug_chaindbCompact',
  250. }),
  251. new web3._extend.Method({
  252. name: 'metrics',
  253. call: 'debug_metrics',
  254. params: 1
  255. }),
  256. new web3._extend.Method({
  257. name: 'verbosity',
  258. call: 'debug_verbosity',
  259. params: 1
  260. }),
  261. new web3._extend.Method({
  262. name: 'vmodule',
  263. call: 'debug_vmodule',
  264. params: 1
  265. }),
  266. new web3._extend.Method({
  267. name: 'backtraceAt',
  268. call: 'debug_backtraceAt',
  269. params: 1,
  270. }),
  271. new web3._extend.Method({
  272. name: 'stacks',
  273. call: 'debug_stacks',
  274. params: 0,
  275. outputFormatter: console.log
  276. }),
  277. new web3._extend.Method({
  278. name: 'freeOSMemory',
  279. call: 'debug_freeOSMemory',
  280. params: 0,
  281. }),
  282. new web3._extend.Method({
  283. name: 'setGCPercent',
  284. call: 'debug_setGCPercent',
  285. params: 1,
  286. }),
  287. new web3._extend.Method({
  288. name: 'memStats',
  289. call: 'debug_memStats',
  290. params: 0,
  291. }),
  292. new web3._extend.Method({
  293. name: 'gcStats',
  294. call: 'debug_gcStats',
  295. params: 0,
  296. }),
  297. new web3._extend.Method({
  298. name: 'cpuProfile',
  299. call: 'debug_cpuProfile',
  300. params: 2
  301. }),
  302. new web3._extend.Method({
  303. name: 'startCPUProfile',
  304. call: 'debug_startCPUProfile',
  305. params: 1
  306. }),
  307. new web3._extend.Method({
  308. name: 'stopCPUProfile',
  309. call: 'debug_stopCPUProfile',
  310. params: 0
  311. }),
  312. new web3._extend.Method({
  313. name: 'goTrace',
  314. call: 'debug_goTrace',
  315. params: 2
  316. }),
  317. new web3._extend.Method({
  318. name: 'startGoTrace',
  319. call: 'debug_startGoTrace',
  320. params: 1
  321. }),
  322. new web3._extend.Method({
  323. name: 'stopGoTrace',
  324. call: 'debug_stopGoTrace',
  325. params: 0
  326. }),
  327. new web3._extend.Method({
  328. name: 'blockProfile',
  329. call: 'debug_blockProfile',
  330. params: 2
  331. }),
  332. new web3._extend.Method({
  333. name: 'setBlockProfileRate',
  334. call: 'debug_setBlockProfileRate',
  335. params: 1
  336. }),
  337. new web3._extend.Method({
  338. name: 'writeBlockProfile',
  339. call: 'debug_writeBlockProfile',
  340. params: 1
  341. }),
  342. new web3._extend.Method({
  343. name: 'mutexProfile',
  344. call: 'debug_mutexProfile',
  345. params: 2
  346. }),
  347. new web3._extend.Method({
  348. name: 'setMutexProfileFraction',
  349. call: 'debug_setMutexProfileFraction',
  350. params: 1
  351. }),
  352. new web3._extend.Method({
  353. name: 'writeMutexProfile',
  354. call: 'debug_writeMutexProfile',
  355. params: 1
  356. }),
  357. new web3._extend.Method({
  358. name: 'writeMemProfile',
  359. call: 'debug_writeMemProfile',
  360. params: 1
  361. }),
  362. new web3._extend.Method({
  363. name: 'traceBlock',
  364. call: 'debug_traceBlock',
  365. params: 2,
  366. inputFormatter: [null, null]
  367. }),
  368. new web3._extend.Method({
  369. name: 'traceBlockFromFile',
  370. call: 'debug_traceBlockFromFile',
  371. params: 2,
  372. inputFormatter: [null, null]
  373. }),
  374. new web3._extend.Method({
  375. name: 'traceBadBlock',
  376. call: 'debug_traceBadBlock',
  377. params: 1,
  378. inputFormatter: [null]
  379. }),
  380. new web3._extend.Method({
  381. name: 'traceBlockByNumber',
  382. call: 'debug_traceBlockByNumber',
  383. params: 2,
  384. inputFormatter: [null, null]
  385. }),
  386. new web3._extend.Method({
  387. name: 'traceBlockByHash',
  388. call: 'debug_traceBlockByHash',
  389. params: 2,
  390. inputFormatter: [null, null]
  391. }),
  392. new web3._extend.Method({
  393. name: 'traceTransaction',
  394. call: 'debug_traceTransaction',
  395. params: 2,
  396. inputFormatter: [null, null]
  397. }),
  398. new web3._extend.Method({
  399. name: 'preimage',
  400. call: 'debug_preimage',
  401. params: 1,
  402. inputFormatter: [null]
  403. }),
  404. new web3._extend.Method({
  405. name: 'getBadBlocks',
  406. call: 'debug_getBadBlocks',
  407. params: 0,
  408. }),
  409. new web3._extend.Method({
  410. name: 'storageRangeAt',
  411. call: 'debug_storageRangeAt',
  412. params: 5,
  413. }),
  414. new web3._extend.Method({
  415. name: 'getModifiedAccountsByNumber',
  416. call: 'debug_getModifiedAccountsByNumber',
  417. params: 2,
  418. inputFormatter: [null, null],
  419. }),
  420. new web3._extend.Method({
  421. name: 'getModifiedAccountsByHash',
  422. call: 'debug_getModifiedAccountsByHash',
  423. params: 2,
  424. inputFormatter:[null, null],
  425. }),
  426. ],
  427. properties: []
  428. });
  429. `
  430. const Eth_JS = `
  431. web3._extend({
  432. property: 'eth',
  433. methods: [
  434. new web3._extend.Method({
  435. name: 'chainId',
  436. call: 'eth_chainId',
  437. params: 0
  438. }),
  439. new web3._extend.Method({
  440. name: 'sign',
  441. call: 'eth_sign',
  442. params: 2,
  443. inputFormatter: [web3._extend.formatters.inputAddressFormatter, null]
  444. }),
  445. new web3._extend.Method({
  446. name: 'resend',
  447. call: 'eth_resend',
  448. params: 3,
  449. inputFormatter: [web3._extend.formatters.inputTransactionFormatter, web3._extend.utils.fromDecimal, web3._extend.utils.fromDecimal]
  450. }),
  451. new web3._extend.Method({
  452. name: 'signTransaction',
  453. call: 'eth_signTransaction',
  454. params: 1,
  455. inputFormatter: [web3._extend.formatters.inputTransactionFormatter]
  456. }),
  457. new web3._extend.Method({
  458. name: 'submitTransaction',
  459. call: 'eth_submitTransaction',
  460. params: 1,
  461. inputFormatter: [web3._extend.formatters.inputTransactionFormatter]
  462. }),
  463. new web3._extend.Method({
  464. name: 'getRawTransaction',
  465. call: 'eth_getRawTransactionByHash',
  466. params: 1
  467. }),
  468. new web3._extend.Method({
  469. name: 'getRawTransactionFromBlock',
  470. call: function(args) {
  471. return (web3._extend.utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getRawTransactionByBlockHashAndIndex' : 'eth_getRawTransactionByBlockNumberAndIndex';
  472. },
  473. params: 2,
  474. inputFormatter: [web3._extend.formatters.inputBlockNumberFormatter, web3._extend.utils.toHex]
  475. }),
  476. new web3._extend.Method({
  477. name: 'getProof',
  478. call: 'eth_getProof',
  479. params: 3,
  480. inputFormatter: [web3._extend.formatters.inputAddressFormatter, null, web3._extend.formatters.inputBlockNumberFormatter]
  481. }),
  482. ],
  483. properties: [
  484. new web3._extend.Property({
  485. name: 'pendingTransactions',
  486. getter: 'eth_pendingTransactions',
  487. outputFormatter: function(txs) {
  488. var formatted = [];
  489. for (var i = 0; i < txs.length; i++) {
  490. formatted.push(web3._extend.formatters.outputTransactionFormatter(txs[i]));
  491. formatted[i].blockHash = null;
  492. }
  493. return formatted;
  494. }
  495. }),
  496. ]
  497. });
  498. `
  499. const Miner_JS = `
  500. web3._extend({
  501. property: 'miner',
  502. methods: [
  503. new web3._extend.Method({
  504. name: 'start',
  505. call: 'miner_start',
  506. params: 1,
  507. inputFormatter: [null]
  508. }),
  509. new web3._extend.Method({
  510. name: 'stop',
  511. call: 'miner_stop'
  512. }),
  513. new web3._extend.Method({
  514. name: 'setEtherbase',
  515. call: 'miner_setEtherbase',
  516. params: 1,
  517. inputFormatter: [web3._extend.formatters.inputAddressFormatter]
  518. }),
  519. new web3._extend.Method({
  520. name: 'setExtra',
  521. call: 'miner_setExtra',
  522. params: 1
  523. }),
  524. new web3._extend.Method({
  525. name: 'setGasPrice',
  526. call: 'miner_setGasPrice',
  527. params: 1,
  528. inputFormatter: [web3._extend.utils.fromDecimal]
  529. }),
  530. new web3._extend.Method({
  531. name: 'setRecommitInterval',
  532. call: 'miner_setRecommitInterval',
  533. params: 1,
  534. }),
  535. new web3._extend.Method({
  536. name: 'getHashrate',
  537. call: 'miner_getHashrate'
  538. }),
  539. ],
  540. properties: []
  541. });
  542. `
  543. const Net_JS = `
  544. web3._extend({
  545. property: 'net',
  546. methods: [],
  547. properties: [
  548. new web3._extend.Property({
  549. name: 'version',
  550. getter: 'net_version'
  551. }),
  552. ]
  553. });
  554. `
  555. const Personal_JS = `
  556. web3._extend({
  557. property: 'personal',
  558. methods: [
  559. new web3._extend.Method({
  560. name: 'importRawKey',
  561. call: 'personal_importRawKey',
  562. params: 2
  563. }),
  564. new web3._extend.Method({
  565. name: 'sign',
  566. call: 'personal_sign',
  567. params: 3,
  568. inputFormatter: [null, web3._extend.formatters.inputAddressFormatter, null]
  569. }),
  570. new web3._extend.Method({
  571. name: 'ecRecover',
  572. call: 'personal_ecRecover',
  573. params: 2
  574. }),
  575. new web3._extend.Method({
  576. name: 'openWallet',
  577. call: 'personal_openWallet',
  578. params: 2
  579. }),
  580. new web3._extend.Method({
  581. name: 'deriveAccount',
  582. call: 'personal_deriveAccount',
  583. params: 3
  584. }),
  585. new web3._extend.Method({
  586. name: 'signTransaction',
  587. call: 'personal_signTransaction',
  588. params: 2,
  589. inputFormatter: [web3._extend.formatters.inputTransactionFormatter, null]
  590. }),
  591. ],
  592. properties: [
  593. new web3._extend.Property({
  594. name: 'listWallets',
  595. getter: 'personal_listWallets'
  596. }),
  597. ]
  598. })
  599. `
  600. const RPC_JS = `
  601. web3._extend({
  602. property: 'rpc',
  603. methods: [],
  604. properties: [
  605. new web3._extend.Property({
  606. name: 'modules',
  607. getter: 'rpc_modules'
  608. }),
  609. ]
  610. });
  611. `
  612. const Shh_JS = `
  613. web3._extend({
  614. property: 'shh',
  615. methods: [
  616. ],
  617. properties:
  618. [
  619. new web3._extend.Property({
  620. name: 'version',
  621. getter: 'shh_version',
  622. outputFormatter: web3._extend.utils.toDecimal
  623. }),
  624. new web3._extend.Property({
  625. name: 'info',
  626. getter: 'shh_info'
  627. }),
  628. ]
  629. });
  630. `
  631. const SWARMFS_JS = `
  632. web3._extend({
  633. property: 'swarmfs',
  634. methods:
  635. [
  636. new web3._extend.Method({
  637. name: 'mount',
  638. call: 'swarmfs_mount',
  639. params: 2
  640. }),
  641. new web3._extend.Method({
  642. name: 'unmount',
  643. call: 'swarmfs_unmount',
  644. params: 1
  645. }),
  646. new web3._extend.Method({
  647. name: 'listmounts',
  648. call: 'swarmfs_listmounts',
  649. params: 0
  650. }),
  651. ]
  652. });
  653. `
  654. const TxPool_JS = `
  655. web3._extend({
  656. property: 'txpool',
  657. methods: [],
  658. properties:
  659. [
  660. new web3._extend.Property({
  661. name: 'content',
  662. getter: 'txpool_content'
  663. }),
  664. new web3._extend.Property({
  665. name: 'inspect',
  666. getter: 'txpool_inspect'
  667. }),
  668. new web3._extend.Property({
  669. name: 'status',
  670. getter: 'txpool_status',
  671. outputFormatter: function(status) {
  672. status.pending = web3._extend.utils.toDecimal(status.pending);
  673. status.queued = web3._extend.utils.toDecimal(status.queued);
  674. return status;
  675. }
  676. }),
  677. ]
  678. });
  679. `