web3ext.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  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: 'standardTraceBadBlockToFile',
  382. call: 'debug_standardTraceBadBlockToFile',
  383. params: 2,
  384. }),
  385. new web3._extend.Method({
  386. name: 'standardTraceBlockToFile',
  387. call: 'debug_standardTraceBlockToFile',
  388. params: 2,
  389. }),
  390. new web3._extend.Method({
  391. name: 'traceBlockByNumber',
  392. call: 'debug_traceBlockByNumber',
  393. params: 2,
  394. inputFormatter: [null, null]
  395. }),
  396. new web3._extend.Method({
  397. name: 'traceBlockByHash',
  398. call: 'debug_traceBlockByHash',
  399. params: 2,
  400. inputFormatter: [null, null]
  401. }),
  402. new web3._extend.Method({
  403. name: 'traceTransaction',
  404. call: 'debug_traceTransaction',
  405. params: 2,
  406. inputFormatter: [null, null]
  407. }),
  408. new web3._extend.Method({
  409. name: 'preimage',
  410. call: 'debug_preimage',
  411. params: 1,
  412. inputFormatter: [null]
  413. }),
  414. new web3._extend.Method({
  415. name: 'getBadBlocks',
  416. call: 'debug_getBadBlocks',
  417. params: 0,
  418. }),
  419. new web3._extend.Method({
  420. name: 'storageRangeAt',
  421. call: 'debug_storageRangeAt',
  422. params: 5,
  423. }),
  424. new web3._extend.Method({
  425. name: 'getModifiedAccountsByNumber',
  426. call: 'debug_getModifiedAccountsByNumber',
  427. params: 2,
  428. inputFormatter: [null, null],
  429. }),
  430. new web3._extend.Method({
  431. name: 'getModifiedAccountsByHash',
  432. call: 'debug_getModifiedAccountsByHash',
  433. params: 2,
  434. inputFormatter:[null, null],
  435. }),
  436. ],
  437. properties: []
  438. });
  439. `
  440. const Eth_JS = `
  441. web3._extend({
  442. property: 'eth',
  443. methods: [
  444. new web3._extend.Method({
  445. name: 'chainId',
  446. call: 'eth_chainId',
  447. params: 0
  448. }),
  449. new web3._extend.Method({
  450. name: 'sign',
  451. call: 'eth_sign',
  452. params: 2,
  453. inputFormatter: [web3._extend.formatters.inputAddressFormatter, null]
  454. }),
  455. new web3._extend.Method({
  456. name: 'resend',
  457. call: 'eth_resend',
  458. params: 3,
  459. inputFormatter: [web3._extend.formatters.inputTransactionFormatter, web3._extend.utils.fromDecimal, web3._extend.utils.fromDecimal]
  460. }),
  461. new web3._extend.Method({
  462. name: 'signTransaction',
  463. call: 'eth_signTransaction',
  464. params: 1,
  465. inputFormatter: [web3._extend.formatters.inputTransactionFormatter]
  466. }),
  467. new web3._extend.Method({
  468. name: 'submitTransaction',
  469. call: 'eth_submitTransaction',
  470. params: 1,
  471. inputFormatter: [web3._extend.formatters.inputTransactionFormatter]
  472. }),
  473. new web3._extend.Method({
  474. name: 'getRawTransaction',
  475. call: 'eth_getRawTransactionByHash',
  476. params: 1
  477. }),
  478. new web3._extend.Method({
  479. name: 'getRawTransactionFromBlock',
  480. call: function(args) {
  481. return (web3._extend.utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getRawTransactionByBlockHashAndIndex' : 'eth_getRawTransactionByBlockNumberAndIndex';
  482. },
  483. params: 2,
  484. inputFormatter: [web3._extend.formatters.inputBlockNumberFormatter, web3._extend.utils.toHex]
  485. }),
  486. new web3._extend.Method({
  487. name: 'getProof',
  488. call: 'eth_getProof',
  489. params: 3,
  490. inputFormatter: [web3._extend.formatters.inputAddressFormatter, null, web3._extend.formatters.inputBlockNumberFormatter]
  491. }),
  492. ],
  493. properties: [
  494. new web3._extend.Property({
  495. name: 'pendingTransactions',
  496. getter: 'eth_pendingTransactions',
  497. outputFormatter: function(txs) {
  498. var formatted = [];
  499. for (var i = 0; i < txs.length; i++) {
  500. formatted.push(web3._extend.formatters.outputTransactionFormatter(txs[i]));
  501. formatted[i].blockHash = null;
  502. }
  503. return formatted;
  504. }
  505. }),
  506. ]
  507. });
  508. `
  509. const Miner_JS = `
  510. web3._extend({
  511. property: 'miner',
  512. methods: [
  513. new web3._extend.Method({
  514. name: 'start',
  515. call: 'miner_start',
  516. params: 1,
  517. inputFormatter: [null]
  518. }),
  519. new web3._extend.Method({
  520. name: 'stop',
  521. call: 'miner_stop'
  522. }),
  523. new web3._extend.Method({
  524. name: 'setEtherbase',
  525. call: 'miner_setEtherbase',
  526. params: 1,
  527. inputFormatter: [web3._extend.formatters.inputAddressFormatter]
  528. }),
  529. new web3._extend.Method({
  530. name: 'setExtra',
  531. call: 'miner_setExtra',
  532. params: 1
  533. }),
  534. new web3._extend.Method({
  535. name: 'setGasPrice',
  536. call: 'miner_setGasPrice',
  537. params: 1,
  538. inputFormatter: [web3._extend.utils.fromDecimal]
  539. }),
  540. new web3._extend.Method({
  541. name: 'setRecommitInterval',
  542. call: 'miner_setRecommitInterval',
  543. params: 1,
  544. }),
  545. new web3._extend.Method({
  546. name: 'getHashrate',
  547. call: 'miner_getHashrate'
  548. }),
  549. ],
  550. properties: []
  551. });
  552. `
  553. const Net_JS = `
  554. web3._extend({
  555. property: 'net',
  556. methods: [],
  557. properties: [
  558. new web3._extend.Property({
  559. name: 'version',
  560. getter: 'net_version'
  561. }),
  562. ]
  563. });
  564. `
  565. const Personal_JS = `
  566. web3._extend({
  567. property: 'personal',
  568. methods: [
  569. new web3._extend.Method({
  570. name: 'importRawKey',
  571. call: 'personal_importRawKey',
  572. params: 2
  573. }),
  574. new web3._extend.Method({
  575. name: 'sign',
  576. call: 'personal_sign',
  577. params: 3,
  578. inputFormatter: [null, web3._extend.formatters.inputAddressFormatter, null]
  579. }),
  580. new web3._extend.Method({
  581. name: 'ecRecover',
  582. call: 'personal_ecRecover',
  583. params: 2
  584. }),
  585. new web3._extend.Method({
  586. name: 'openWallet',
  587. call: 'personal_openWallet',
  588. params: 2
  589. }),
  590. new web3._extend.Method({
  591. name: 'deriveAccount',
  592. call: 'personal_deriveAccount',
  593. params: 3
  594. }),
  595. new web3._extend.Method({
  596. name: 'signTransaction',
  597. call: 'personal_signTransaction',
  598. params: 2,
  599. inputFormatter: [web3._extend.formatters.inputTransactionFormatter, null]
  600. }),
  601. ],
  602. properties: [
  603. new web3._extend.Property({
  604. name: 'listWallets',
  605. getter: 'personal_listWallets'
  606. }),
  607. ]
  608. })
  609. `
  610. const RPC_JS = `
  611. web3._extend({
  612. property: 'rpc',
  613. methods: [],
  614. properties: [
  615. new web3._extend.Property({
  616. name: 'modules',
  617. getter: 'rpc_modules'
  618. }),
  619. ]
  620. });
  621. `
  622. const Shh_JS = `
  623. web3._extend({
  624. property: 'shh',
  625. methods: [
  626. ],
  627. properties:
  628. [
  629. new web3._extend.Property({
  630. name: 'version',
  631. getter: 'shh_version',
  632. outputFormatter: web3._extend.utils.toDecimal
  633. }),
  634. new web3._extend.Property({
  635. name: 'info',
  636. getter: 'shh_info'
  637. }),
  638. ]
  639. });
  640. `
  641. const SWARMFS_JS = `
  642. web3._extend({
  643. property: 'swarmfs',
  644. methods:
  645. [
  646. new web3._extend.Method({
  647. name: 'mount',
  648. call: 'swarmfs_mount',
  649. params: 2
  650. }),
  651. new web3._extend.Method({
  652. name: 'unmount',
  653. call: 'swarmfs_unmount',
  654. params: 1
  655. }),
  656. new web3._extend.Method({
  657. name: 'listmounts',
  658. call: 'swarmfs_listmounts',
  659. params: 0
  660. }),
  661. ]
  662. });
  663. `
  664. const TxPool_JS = `
  665. web3._extend({
  666. property: 'txpool',
  667. methods: [],
  668. properties:
  669. [
  670. new web3._extend.Property({
  671. name: 'content',
  672. getter: 'txpool_content'
  673. }),
  674. new web3._extend.Property({
  675. name: 'inspect',
  676. getter: 'txpool_inspect'
  677. }),
  678. new web3._extend.Property({
  679. name: 'status',
  680. getter: 'txpool_status',
  681. outputFormatter: function(status) {
  682. status.pending = web3._extend.utils.toDecimal(status.pending);
  683. status.queued = web3._extend.utils.toDecimal(status.queued);
  684. return status;
  685. }
  686. }),
  687. ]
  688. });
  689. `