web3ext.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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. "accounting": AccountingJs,
  20. "admin": AdminJs,
  21. "chequebook": ChequebookJs,
  22. "clique": CliqueJs,
  23. "ethash": EthashJs,
  24. "debug": DebugJs,
  25. "eth": EthJs,
  26. "miner": MinerJs,
  27. "net": NetJs,
  28. "personal": PersonalJs,
  29. "rpc": RpcJs,
  30. "shh": ShhJs,
  31. "swarmfs": SwarmfsJs,
  32. "txpool": TxpoolJs,
  33. }
  34. const ChequebookJs = `
  35. web3._extend({
  36. property: 'chequebook',
  37. methods: [
  38. new web3._extend.Method({
  39. name: 'deposit',
  40. call: 'chequebook_deposit',
  41. params: 1,
  42. inputFormatter: [null]
  43. }),
  44. new web3._extend.Property({
  45. name: 'balance',
  46. getter: 'chequebook_balance',
  47. outputFormatter: web3._extend.utils.toDecimal
  48. }),
  49. new web3._extend.Method({
  50. name: 'cash',
  51. call: 'chequebook_cash',
  52. params: 1,
  53. inputFormatter: [null]
  54. }),
  55. new web3._extend.Method({
  56. name: 'issue',
  57. call: 'chequebook_issue',
  58. params: 2,
  59. inputFormatter: [null, null]
  60. }),
  61. ]
  62. });
  63. `
  64. const CliqueJs = `
  65. web3._extend({
  66. property: 'clique',
  67. methods: [
  68. new web3._extend.Method({
  69. name: 'getSnapshot',
  70. call: 'clique_getSnapshot',
  71. params: 1,
  72. inputFormatter: [null]
  73. }),
  74. new web3._extend.Method({
  75. name: 'getSnapshotAtHash',
  76. call: 'clique_getSnapshotAtHash',
  77. params: 1
  78. }),
  79. new web3._extend.Method({
  80. name: 'getSigners',
  81. call: 'clique_getSigners',
  82. params: 1,
  83. inputFormatter: [null]
  84. }),
  85. new web3._extend.Method({
  86. name: 'getSignersAtHash',
  87. call: 'clique_getSignersAtHash',
  88. params: 1
  89. }),
  90. new web3._extend.Method({
  91. name: 'propose',
  92. call: 'clique_propose',
  93. params: 2
  94. }),
  95. new web3._extend.Method({
  96. name: 'discard',
  97. call: 'clique_discard',
  98. params: 1
  99. }),
  100. ],
  101. properties: [
  102. new web3._extend.Property({
  103. name: 'proposals',
  104. getter: 'clique_proposals'
  105. }),
  106. ]
  107. });
  108. `
  109. const EthashJs = `
  110. web3._extend({
  111. property: 'ethash',
  112. methods: [
  113. new web3._extend.Method({
  114. name: 'getWork',
  115. call: 'ethash_getWork',
  116. params: 0
  117. }),
  118. new web3._extend.Method({
  119. name: 'getHashrate',
  120. call: 'ethash_getHashrate',
  121. params: 0
  122. }),
  123. new web3._extend.Method({
  124. name: 'submitWork',
  125. call: 'ethash_submitWork',
  126. params: 3,
  127. }),
  128. new web3._extend.Method({
  129. name: 'submitHashRate',
  130. call: 'ethash_submitHashRate',
  131. params: 2,
  132. }),
  133. ]
  134. });
  135. `
  136. const AdminJs = `
  137. web3._extend({
  138. property: 'admin',
  139. methods: [
  140. new web3._extend.Method({
  141. name: 'addPeer',
  142. call: 'admin_addPeer',
  143. params: 1
  144. }),
  145. new web3._extend.Method({
  146. name: 'removePeer',
  147. call: 'admin_removePeer',
  148. params: 1
  149. }),
  150. new web3._extend.Method({
  151. name: 'addTrustedPeer',
  152. call: 'admin_addTrustedPeer',
  153. params: 1
  154. }),
  155. new web3._extend.Method({
  156. name: 'removeTrustedPeer',
  157. call: 'admin_removeTrustedPeer',
  158. params: 1
  159. }),
  160. new web3._extend.Method({
  161. name: 'exportChain',
  162. call: 'admin_exportChain',
  163. params: 1,
  164. inputFormatter: [null]
  165. }),
  166. new web3._extend.Method({
  167. name: 'importChain',
  168. call: 'admin_importChain',
  169. params: 1
  170. }),
  171. new web3._extend.Method({
  172. name: 'sleepBlocks',
  173. call: 'admin_sleepBlocks',
  174. params: 2
  175. }),
  176. new web3._extend.Method({
  177. name: 'startRPC',
  178. call: 'admin_startRPC',
  179. params: 4,
  180. inputFormatter: [null, null, null, null]
  181. }),
  182. new web3._extend.Method({
  183. name: 'stopRPC',
  184. call: 'admin_stopRPC'
  185. }),
  186. new web3._extend.Method({
  187. name: 'startWS',
  188. call: 'admin_startWS',
  189. params: 4,
  190. inputFormatter: [null, null, null, null]
  191. }),
  192. new web3._extend.Method({
  193. name: 'stopWS',
  194. call: 'admin_stopWS'
  195. }),
  196. ],
  197. properties: [
  198. new web3._extend.Property({
  199. name: 'nodeInfo',
  200. getter: 'admin_nodeInfo'
  201. }),
  202. new web3._extend.Property({
  203. name: 'peers',
  204. getter: 'admin_peers'
  205. }),
  206. new web3._extend.Property({
  207. name: 'datadir',
  208. getter: 'admin_datadir'
  209. }),
  210. ]
  211. });
  212. `
  213. const DebugJs = `
  214. web3._extend({
  215. property: 'debug',
  216. methods: [
  217. new web3._extend.Method({
  218. name: 'printBlock',
  219. call: 'debug_printBlock',
  220. params: 1
  221. }),
  222. new web3._extend.Method({
  223. name: 'getBlockRlp',
  224. call: 'debug_getBlockRlp',
  225. params: 1
  226. }),
  227. new web3._extend.Method({
  228. name: 'testSignCliqueBlock',
  229. call: 'debug_testSignCliqueBlock',
  230. params: 2,
  231. inputFormatters: [web3._extend.formatters.inputAddressFormatter, null],
  232. }),
  233. new web3._extend.Method({
  234. name: 'setHead',
  235. call: 'debug_setHead',
  236. params: 1
  237. }),
  238. new web3._extend.Method({
  239. name: 'seedHash',
  240. call: 'debug_seedHash',
  241. params: 1
  242. }),
  243. new web3._extend.Method({
  244. name: 'dumpBlock',
  245. call: 'debug_dumpBlock',
  246. params: 1
  247. }),
  248. new web3._extend.Method({
  249. name: 'chaindbProperty',
  250. call: 'debug_chaindbProperty',
  251. params: 1,
  252. outputFormatter: console.log
  253. }),
  254. new web3._extend.Method({
  255. name: 'chaindbCompact',
  256. call: 'debug_chaindbCompact',
  257. }),
  258. new web3._extend.Method({
  259. name: 'verbosity',
  260. call: 'debug_verbosity',
  261. params: 1
  262. }),
  263. new web3._extend.Method({
  264. name: 'vmodule',
  265. call: 'debug_vmodule',
  266. params: 1
  267. }),
  268. new web3._extend.Method({
  269. name: 'backtraceAt',
  270. call: 'debug_backtraceAt',
  271. params: 1,
  272. }),
  273. new web3._extend.Method({
  274. name: 'stacks',
  275. call: 'debug_stacks',
  276. params: 0,
  277. outputFormatter: console.log
  278. }),
  279. new web3._extend.Method({
  280. name: 'freeOSMemory',
  281. call: 'debug_freeOSMemory',
  282. params: 0,
  283. }),
  284. new web3._extend.Method({
  285. name: 'setGCPercent',
  286. call: 'debug_setGCPercent',
  287. params: 1,
  288. }),
  289. new web3._extend.Method({
  290. name: 'memStats',
  291. call: 'debug_memStats',
  292. params: 0,
  293. }),
  294. new web3._extend.Method({
  295. name: 'gcStats',
  296. call: 'debug_gcStats',
  297. params: 0,
  298. }),
  299. new web3._extend.Method({
  300. name: 'cpuProfile',
  301. call: 'debug_cpuProfile',
  302. params: 2
  303. }),
  304. new web3._extend.Method({
  305. name: 'startCPUProfile',
  306. call: 'debug_startCPUProfile',
  307. params: 1
  308. }),
  309. new web3._extend.Method({
  310. name: 'stopCPUProfile',
  311. call: 'debug_stopCPUProfile',
  312. params: 0
  313. }),
  314. new web3._extend.Method({
  315. name: 'goTrace',
  316. call: 'debug_goTrace',
  317. params: 2
  318. }),
  319. new web3._extend.Method({
  320. name: 'startGoTrace',
  321. call: 'debug_startGoTrace',
  322. params: 1
  323. }),
  324. new web3._extend.Method({
  325. name: 'stopGoTrace',
  326. call: 'debug_stopGoTrace',
  327. params: 0
  328. }),
  329. new web3._extend.Method({
  330. name: 'blockProfile',
  331. call: 'debug_blockProfile',
  332. params: 2
  333. }),
  334. new web3._extend.Method({
  335. name: 'setBlockProfileRate',
  336. call: 'debug_setBlockProfileRate',
  337. params: 1
  338. }),
  339. new web3._extend.Method({
  340. name: 'writeBlockProfile',
  341. call: 'debug_writeBlockProfile',
  342. params: 1
  343. }),
  344. new web3._extend.Method({
  345. name: 'mutexProfile',
  346. call: 'debug_mutexProfile',
  347. params: 2
  348. }),
  349. new web3._extend.Method({
  350. name: 'setMutexProfileFraction',
  351. call: 'debug_setMutexProfileFraction',
  352. params: 1
  353. }),
  354. new web3._extend.Method({
  355. name: 'writeMutexProfile',
  356. call: 'debug_writeMutexProfile',
  357. params: 1
  358. }),
  359. new web3._extend.Method({
  360. name: 'writeMemProfile',
  361. call: 'debug_writeMemProfile',
  362. params: 1
  363. }),
  364. new web3._extend.Method({
  365. name: 'traceBlock',
  366. call: 'debug_traceBlock',
  367. params: 2,
  368. inputFormatter: [null, null]
  369. }),
  370. new web3._extend.Method({
  371. name: 'traceBlockFromFile',
  372. call: 'debug_traceBlockFromFile',
  373. params: 2,
  374. inputFormatter: [null, null]
  375. }),
  376. new web3._extend.Method({
  377. name: 'traceBadBlock',
  378. call: 'debug_traceBadBlock',
  379. params: 1,
  380. inputFormatter: [null]
  381. }),
  382. new web3._extend.Method({
  383. name: 'standardTraceBadBlockToFile',
  384. call: 'debug_standardTraceBadBlockToFile',
  385. params: 2,
  386. inputFormatter: [null, null]
  387. }),
  388. new web3._extend.Method({
  389. name: 'standardTraceBlockToFile',
  390. call: 'debug_standardTraceBlockToFile',
  391. params: 2,
  392. inputFormatter: [null, null]
  393. }),
  394. new web3._extend.Method({
  395. name: 'traceBlockByNumber',
  396. call: 'debug_traceBlockByNumber',
  397. params: 2,
  398. inputFormatter: [null, null]
  399. }),
  400. new web3._extend.Method({
  401. name: 'traceBlockByHash',
  402. call: 'debug_traceBlockByHash',
  403. params: 2,
  404. inputFormatter: [null, null]
  405. }),
  406. new web3._extend.Method({
  407. name: 'traceTransaction',
  408. call: 'debug_traceTransaction',
  409. params: 2,
  410. inputFormatter: [null, null]
  411. }),
  412. new web3._extend.Method({
  413. name: 'preimage',
  414. call: 'debug_preimage',
  415. params: 1,
  416. inputFormatter: [null]
  417. }),
  418. new web3._extend.Method({
  419. name: 'getBadBlocks',
  420. call: 'debug_getBadBlocks',
  421. params: 0,
  422. }),
  423. new web3._extend.Method({
  424. name: 'storageRangeAt',
  425. call: 'debug_storageRangeAt',
  426. params: 5,
  427. }),
  428. new web3._extend.Method({
  429. name: 'getModifiedAccountsByNumber',
  430. call: 'debug_getModifiedAccountsByNumber',
  431. params: 2,
  432. inputFormatter: [null, null],
  433. }),
  434. new web3._extend.Method({
  435. name: 'getModifiedAccountsByHash',
  436. call: 'debug_getModifiedAccountsByHash',
  437. params: 2,
  438. inputFormatter:[null, null],
  439. }),
  440. ],
  441. properties: []
  442. });
  443. `
  444. const EthJs = `
  445. web3._extend({
  446. property: 'eth',
  447. methods: [
  448. new web3._extend.Method({
  449. name: 'chainId',
  450. call: 'eth_chainId',
  451. params: 0
  452. }),
  453. new web3._extend.Method({
  454. name: 'sign',
  455. call: 'eth_sign',
  456. params: 2,
  457. inputFormatter: [web3._extend.formatters.inputAddressFormatter, null]
  458. }),
  459. new web3._extend.Method({
  460. name: 'resend',
  461. call: 'eth_resend',
  462. params: 3,
  463. inputFormatter: [web3._extend.formatters.inputTransactionFormatter, web3._extend.utils.fromDecimal, web3._extend.utils.fromDecimal]
  464. }),
  465. new web3._extend.Method({
  466. name: 'signTransaction',
  467. call: 'eth_signTransaction',
  468. params: 1,
  469. inputFormatter: [web3._extend.formatters.inputTransactionFormatter]
  470. }),
  471. new web3._extend.Method({
  472. name: 'submitTransaction',
  473. call: 'eth_submitTransaction',
  474. params: 1,
  475. inputFormatter: [web3._extend.formatters.inputTransactionFormatter]
  476. }),
  477. new web3._extend.Method({
  478. name: 'getRawTransaction',
  479. call: 'eth_getRawTransactionByHash',
  480. params: 1
  481. }),
  482. new web3._extend.Method({
  483. name: 'getRawTransactionFromBlock',
  484. call: function(args) {
  485. return (web3._extend.utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getRawTransactionByBlockHashAndIndex' : 'eth_getRawTransactionByBlockNumberAndIndex';
  486. },
  487. params: 2,
  488. inputFormatter: [web3._extend.formatters.inputBlockNumberFormatter, web3._extend.utils.toHex]
  489. }),
  490. new web3._extend.Method({
  491. name: 'getProof',
  492. call: 'eth_getProof',
  493. params: 3,
  494. inputFormatter: [web3._extend.formatters.inputAddressFormatter, null, web3._extend.formatters.inputBlockNumberFormatter]
  495. }),
  496. ],
  497. properties: [
  498. new web3._extend.Property({
  499. name: 'pendingTransactions',
  500. getter: 'eth_pendingTransactions',
  501. outputFormatter: function(txs) {
  502. var formatted = [];
  503. for (var i = 0; i < txs.length; i++) {
  504. formatted.push(web3._extend.formatters.outputTransactionFormatter(txs[i]));
  505. formatted[i].blockHash = null;
  506. }
  507. return formatted;
  508. }
  509. }),
  510. ]
  511. });
  512. `
  513. const MinerJs = `
  514. web3._extend({
  515. property: 'miner',
  516. methods: [
  517. new web3._extend.Method({
  518. name: 'start',
  519. call: 'miner_start',
  520. params: 1,
  521. inputFormatter: [null]
  522. }),
  523. new web3._extend.Method({
  524. name: 'stop',
  525. call: 'miner_stop'
  526. }),
  527. new web3._extend.Method({
  528. name: 'setEtherbase',
  529. call: 'miner_setEtherbase',
  530. params: 1,
  531. inputFormatter: [web3._extend.formatters.inputAddressFormatter]
  532. }),
  533. new web3._extend.Method({
  534. name: 'setExtra',
  535. call: 'miner_setExtra',
  536. params: 1
  537. }),
  538. new web3._extend.Method({
  539. name: 'setGasPrice',
  540. call: 'miner_setGasPrice',
  541. params: 1,
  542. inputFormatter: [web3._extend.utils.fromDecimal]
  543. }),
  544. new web3._extend.Method({
  545. name: 'setRecommitInterval',
  546. call: 'miner_setRecommitInterval',
  547. params: 1,
  548. }),
  549. new web3._extend.Method({
  550. name: 'getHashrate',
  551. call: 'miner_getHashrate'
  552. }),
  553. ],
  554. properties: []
  555. });
  556. `
  557. const NetJs = `
  558. web3._extend({
  559. property: 'net',
  560. methods: [],
  561. properties: [
  562. new web3._extend.Property({
  563. name: 'version',
  564. getter: 'net_version'
  565. }),
  566. ]
  567. });
  568. `
  569. const PersonalJs = `
  570. web3._extend({
  571. property: 'personal',
  572. methods: [
  573. new web3._extend.Method({
  574. name: 'importRawKey',
  575. call: 'personal_importRawKey',
  576. params: 2
  577. }),
  578. new web3._extend.Method({
  579. name: 'sign',
  580. call: 'personal_sign',
  581. params: 3,
  582. inputFormatter: [null, web3._extend.formatters.inputAddressFormatter, null]
  583. }),
  584. new web3._extend.Method({
  585. name: 'ecRecover',
  586. call: 'personal_ecRecover',
  587. params: 2
  588. }),
  589. new web3._extend.Method({
  590. name: 'openWallet',
  591. call: 'personal_openWallet',
  592. params: 2
  593. }),
  594. new web3._extend.Method({
  595. name: 'deriveAccount',
  596. call: 'personal_deriveAccount',
  597. params: 3
  598. }),
  599. new web3._extend.Method({
  600. name: 'signTransaction',
  601. call: 'personal_signTransaction',
  602. params: 2,
  603. inputFormatter: [web3._extend.formatters.inputTransactionFormatter, null]
  604. }),
  605. new web3._extend.Method({
  606. name: 'unpair',
  607. call: 'personal_unpair',
  608. params: 2
  609. }),
  610. new web3._extend.Method({
  611. name: 'initializeWallet',
  612. call: 'personal_initializeWallet',
  613. params: 1
  614. })
  615. ],
  616. properties: [
  617. new web3._extend.Property({
  618. name: 'listWallets',
  619. getter: 'personal_listWallets'
  620. }),
  621. ]
  622. })
  623. `
  624. const RpcJs = `
  625. web3._extend({
  626. property: 'rpc',
  627. methods: [],
  628. properties: [
  629. new web3._extend.Property({
  630. name: 'modules',
  631. getter: 'rpc_modules'
  632. }),
  633. ]
  634. });
  635. `
  636. const ShhJs = `
  637. web3._extend({
  638. property: 'shh',
  639. methods: [
  640. ],
  641. properties:
  642. [
  643. new web3._extend.Property({
  644. name: 'version',
  645. getter: 'shh_version',
  646. outputFormatter: web3._extend.utils.toDecimal
  647. }),
  648. new web3._extend.Property({
  649. name: 'info',
  650. getter: 'shh_info'
  651. }),
  652. ]
  653. });
  654. `
  655. const SwarmfsJs = `
  656. web3._extend({
  657. property: 'swarmfs',
  658. methods:
  659. [
  660. new web3._extend.Method({
  661. name: 'mount',
  662. call: 'swarmfs_mount',
  663. params: 2
  664. }),
  665. new web3._extend.Method({
  666. name: 'unmount',
  667. call: 'swarmfs_unmount',
  668. params: 1
  669. }),
  670. new web3._extend.Method({
  671. name: 'listmounts',
  672. call: 'swarmfs_listmounts',
  673. params: 0
  674. }),
  675. ]
  676. });
  677. `
  678. const TxpoolJs = `
  679. web3._extend({
  680. property: 'txpool',
  681. methods: [],
  682. properties:
  683. [
  684. new web3._extend.Property({
  685. name: 'content',
  686. getter: 'txpool_content'
  687. }),
  688. new web3._extend.Property({
  689. name: 'inspect',
  690. getter: 'txpool_inspect'
  691. }),
  692. new web3._extend.Property({
  693. name: 'status',
  694. getter: 'txpool_status',
  695. outputFormatter: function(status) {
  696. status.pending = web3._extend.utils.toDecimal(status.pending);
  697. status.queued = web3._extend.utils.toDecimal(status.queued);
  698. return status;
  699. }
  700. }),
  701. ]
  702. });
  703. `
  704. const AccountingJs = `
  705. web3._extend({
  706. property: 'accounting',
  707. methods: [
  708. new web3._extend.Property({
  709. name: 'balance',
  710. getter: 'account_balance'
  711. }),
  712. new web3._extend.Property({
  713. name: 'balanceCredit',
  714. getter: 'account_balanceCredit'
  715. }),
  716. new web3._extend.Property({
  717. name: 'balanceDebit',
  718. getter: 'account_balanceDebit'
  719. }),
  720. new web3._extend.Property({
  721. name: 'bytesCredit',
  722. getter: 'account_bytesCredit'
  723. }),
  724. new web3._extend.Property({
  725. name: 'bytesDebit',
  726. getter: 'account_bytesDebit'
  727. }),
  728. new web3._extend.Property({
  729. name: 'msgCredit',
  730. getter: 'account_msgCredit'
  731. }),
  732. new web3._extend.Property({
  733. name: 'msgDebit',
  734. getter: 'account_msgDebit'
  735. }),
  736. new web3._extend.Property({
  737. name: 'peerDrops',
  738. getter: 'account_peerDrops'
  739. }),
  740. new web3._extend.Property({
  741. name: 'selfDrops',
  742. getter: 'account_selfDrops'
  743. }),
  744. ]
  745. });
  746. `