web3ext.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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. "debug": Debug_JS,
  23. "eth": Eth_JS,
  24. "miner": Miner_JS,
  25. "net": Net_JS,
  26. "personal": Personal_JS,
  27. "rpc": RPC_JS,
  28. "shh": Shh_JS,
  29. "swarmfs": SWARMFS_JS,
  30. "txpool": TxPool_JS,
  31. }
  32. const Chequebook_JS = `
  33. web3._extend({
  34. property: 'chequebook',
  35. methods:
  36. [
  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. [
  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. [
  103. new web3._extend.Property({
  104. name: 'proposals',
  105. getter: 'clique_proposals'
  106. }),
  107. ]
  108. });
  109. `
  110. const Admin_JS = `
  111. web3._extend({
  112. property: 'admin',
  113. methods:
  114. [
  115. new web3._extend.Method({
  116. name: 'addPeer',
  117. call: 'admin_addPeer',
  118. params: 1
  119. }),
  120. new web3._extend.Method({
  121. name: 'removePeer',
  122. call: 'admin_removePeer',
  123. params: 1
  124. }),
  125. new web3._extend.Method({
  126. name: 'exportChain',
  127. call: 'admin_exportChain',
  128. params: 1,
  129. inputFormatter: [null]
  130. }),
  131. new web3._extend.Method({
  132. name: 'importChain',
  133. call: 'admin_importChain',
  134. params: 1
  135. }),
  136. new web3._extend.Method({
  137. name: 'sleepBlocks',
  138. call: 'admin_sleepBlocks',
  139. params: 2
  140. }),
  141. new web3._extend.Method({
  142. name: 'setSolc',
  143. call: 'admin_setSolc',
  144. params: 1
  145. }),
  146. new web3._extend.Method({
  147. name: 'startRPC',
  148. call: 'admin_startRPC',
  149. params: 4,
  150. inputFormatter: [null, null, null, null]
  151. }),
  152. new web3._extend.Method({
  153. name: 'stopRPC',
  154. call: 'admin_stopRPC'
  155. }),
  156. new web3._extend.Method({
  157. name: 'startWS',
  158. call: 'admin_startWS',
  159. params: 4,
  160. inputFormatter: [null, null, null, null]
  161. }),
  162. new web3._extend.Method({
  163. name: 'stopWS',
  164. call: 'admin_stopWS'
  165. })
  166. ],
  167. properties:
  168. [
  169. new web3._extend.Property({
  170. name: 'nodeInfo',
  171. getter: 'admin_nodeInfo'
  172. }),
  173. new web3._extend.Property({
  174. name: 'peers',
  175. getter: 'admin_peers'
  176. }),
  177. new web3._extend.Property({
  178. name: 'datadir',
  179. getter: 'admin_datadir'
  180. })
  181. ]
  182. });
  183. `
  184. const Debug_JS = `
  185. web3._extend({
  186. property: 'debug',
  187. methods:
  188. [
  189. new web3._extend.Method({
  190. name: 'printBlock',
  191. call: 'debug_printBlock',
  192. params: 1
  193. }),
  194. new web3._extend.Method({
  195. name: 'getBlockRlp',
  196. call: 'debug_getBlockRlp',
  197. params: 1
  198. }),
  199. new web3._extend.Method({
  200. name: 'setHead',
  201. call: 'debug_setHead',
  202. params: 1
  203. }),
  204. new web3._extend.Method({
  205. name: 'traceBlock',
  206. call: 'debug_traceBlock',
  207. params: 1
  208. }),
  209. new web3._extend.Method({
  210. name: 'traceBlockByFile',
  211. call: 'debug_traceBlockByFile',
  212. params: 1
  213. }),
  214. new web3._extend.Method({
  215. name: 'traceBlockByNumber',
  216. call: 'debug_traceBlockByNumber',
  217. params: 1
  218. }),
  219. new web3._extend.Method({
  220. name: 'traceBlockByHash',
  221. call: 'debug_traceBlockByHash',
  222. params: 1
  223. }),
  224. new web3._extend.Method({
  225. name: 'seedHash',
  226. call: 'debug_seedHash',
  227. params: 1
  228. }),
  229. new web3._extend.Method({
  230. name: 'dumpBlock',
  231. call: 'debug_dumpBlock',
  232. params: 1
  233. }),
  234. new web3._extend.Method({
  235. name: 'chaindbProperty',
  236. call: 'debug_chaindbProperty',
  237. params: 1,
  238. outputFormatter: console.log
  239. }),
  240. new web3._extend.Method({
  241. name: 'chaindbCompact',
  242. call: 'debug_chaindbCompact',
  243. }),
  244. new web3._extend.Method({
  245. name: 'metrics',
  246. call: 'debug_metrics',
  247. params: 1
  248. }),
  249. new web3._extend.Method({
  250. name: 'verbosity',
  251. call: 'debug_verbosity',
  252. params: 1
  253. }),
  254. new web3._extend.Method({
  255. name: 'vmodule',
  256. call: 'debug_vmodule',
  257. params: 1
  258. }),
  259. new web3._extend.Method({
  260. name: 'backtraceAt',
  261. call: 'debug_backtraceAt',
  262. params: 1,
  263. }),
  264. new web3._extend.Method({
  265. name: 'stacks',
  266. call: 'debug_stacks',
  267. params: 0,
  268. outputFormatter: console.log
  269. }),
  270. new web3._extend.Method({
  271. name: 'memStats',
  272. call: 'debug_memStats',
  273. params: 0,
  274. }),
  275. new web3._extend.Method({
  276. name: 'gcStats',
  277. call: 'debug_gcStats',
  278. params: 0,
  279. }),
  280. new web3._extend.Method({
  281. name: 'cpuProfile',
  282. call: 'debug_cpuProfile',
  283. params: 2
  284. }),
  285. new web3._extend.Method({
  286. name: 'startCPUProfile',
  287. call: 'debug_startCPUProfile',
  288. params: 1
  289. }),
  290. new web3._extend.Method({
  291. name: 'stopCPUProfile',
  292. call: 'debug_stopCPUProfile',
  293. params: 0
  294. }),
  295. new web3._extend.Method({
  296. name: 'goTrace',
  297. call: 'debug_goTrace',
  298. params: 2
  299. }),
  300. new web3._extend.Method({
  301. name: 'startGoTrace',
  302. call: 'debug_startGoTrace',
  303. params: 1
  304. }),
  305. new web3._extend.Method({
  306. name: 'stopGoTrace',
  307. call: 'debug_stopGoTrace',
  308. params: 0
  309. }),
  310. new web3._extend.Method({
  311. name: 'blockProfile',
  312. call: 'debug_blockProfile',
  313. params: 2
  314. }),
  315. new web3._extend.Method({
  316. name: 'setBlockProfileRate',
  317. call: 'debug_setBlockProfileRate',
  318. params: 1
  319. }),
  320. new web3._extend.Method({
  321. name: 'writeBlockProfile',
  322. call: 'debug_writeBlockProfile',
  323. params: 1
  324. }),
  325. new web3._extend.Method({
  326. name: 'writeMemProfile',
  327. call: 'debug_writeMemProfile',
  328. params: 1
  329. }),
  330. new web3._extend.Method({
  331. name: 'traceTransaction',
  332. call: 'debug_traceTransaction',
  333. params: 2,
  334. inputFormatter: [null, null]
  335. }),
  336. new web3._extend.Method({
  337. name: 'preimage',
  338. call: 'debug_preimage',
  339. params: 1,
  340. inputFormatter: [null]
  341. }),
  342. new web3._extend.Method({
  343. name: 'getBadBlocks',
  344. call: 'debug_getBadBlocks',
  345. params: 0,
  346. }),
  347. ],
  348. properties: []
  349. });
  350. `
  351. const Eth_JS = `
  352. web3._extend({
  353. property: 'eth',
  354. methods:
  355. [
  356. new web3._extend.Method({
  357. name: 'sign',
  358. call: 'eth_sign',
  359. params: 2,
  360. inputFormatter: [web3._extend.formatters.inputAddressFormatter, null]
  361. }),
  362. new web3._extend.Method({
  363. name: 'resend',
  364. call: 'eth_resend',
  365. params: 3,
  366. inputFormatter: [web3._extend.formatters.inputTransactionFormatter, web3._extend.utils.fromDecimal, web3._extend.utils.fromDecimal]
  367. }),
  368. new web3._extend.Method({
  369. name: 'signTransaction',
  370. call: 'eth_signTransaction',
  371. params: 1,
  372. inputFormatter: [web3._extend.formatters.inputTransactionFormatter]
  373. }),
  374. new web3._extend.Method({
  375. name: 'submitTransaction',
  376. call: 'eth_submitTransaction',
  377. params: 1,
  378. inputFormatter: [web3._extend.formatters.inputTransactionFormatter]
  379. }),
  380. new web3._extend.Method({
  381. name: 'getRawTransaction',
  382. call: 'eth_getRawTransactionByHash',
  383. params: 1
  384. }),
  385. new web3._extend.Method({
  386. name: 'getRawTransactionFromBlock',
  387. call: function(args) {
  388. return (web3._extend.utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getRawTransactionByBlockHashAndIndex' : 'eth_getRawTransactionByBlockNumberAndIndex';
  389. },
  390. params: 2,
  391. inputFormatter: [web3._extend.formatters.inputBlockNumberFormatter, web3._extend.utils.toHex]
  392. })
  393. ],
  394. properties:
  395. [
  396. new web3._extend.Property({
  397. name: 'pendingTransactions',
  398. getter: 'eth_pendingTransactions',
  399. outputFormatter: function(txs) {
  400. var formatted = [];
  401. for (var i = 0; i < txs.length; i++) {
  402. formatted.push(web3._extend.formatters.outputTransactionFormatter(txs[i]));
  403. formatted[i].blockHash = null;
  404. }
  405. return formatted;
  406. }
  407. })
  408. ]
  409. });
  410. `
  411. const Miner_JS = `
  412. web3._extend({
  413. property: 'miner',
  414. methods:
  415. [
  416. new web3._extend.Method({
  417. name: 'start',
  418. call: 'miner_start',
  419. params: 1,
  420. inputFormatter: [null]
  421. }),
  422. new web3._extend.Method({
  423. name: 'stop',
  424. call: 'miner_stop'
  425. }),
  426. new web3._extend.Method({
  427. name: 'setEtherbase',
  428. call: 'miner_setEtherbase',
  429. params: 1,
  430. inputFormatter: [web3._extend.formatters.inputAddressFormatter]
  431. }),
  432. new web3._extend.Method({
  433. name: 'setExtra',
  434. call: 'miner_setExtra',
  435. params: 1
  436. }),
  437. new web3._extend.Method({
  438. name: 'setGasPrice',
  439. call: 'miner_setGasPrice',
  440. params: 1,
  441. inputFormatter: [web3._extend.utils.fromDecimal]
  442. }),
  443. new web3._extend.Method({
  444. name: 'getHashrate',
  445. call: 'miner_getHashrate'
  446. })
  447. ],
  448. properties: []
  449. });
  450. `
  451. const Net_JS = `
  452. web3._extend({
  453. property: 'net',
  454. methods: [],
  455. properties:
  456. [
  457. new web3._extend.Property({
  458. name: 'version',
  459. getter: 'net_version'
  460. })
  461. ]
  462. });
  463. `
  464. const Personal_JS = `
  465. web3._extend({
  466. property: 'personal',
  467. methods:
  468. [
  469. new web3._extend.Method({
  470. name: 'importRawKey',
  471. call: 'personal_importRawKey',
  472. params: 2
  473. }),
  474. new web3._extend.Method({
  475. name: 'sign',
  476. call: 'personal_sign',
  477. params: 3,
  478. inputFormatter: [null, web3._extend.formatters.inputAddressFormatter, null]
  479. }),
  480. new web3._extend.Method({
  481. name: 'ecRecover',
  482. call: 'personal_ecRecover',
  483. params: 2
  484. }),
  485. new web3._extend.Method({
  486. name: 'deriveAccount',
  487. call: 'personal_deriveAccount',
  488. params: 3
  489. })
  490. ],
  491. properties:
  492. [
  493. new web3._extend.Property({
  494. name: 'listWallets',
  495. getter: 'personal_listWallets'
  496. })
  497. ]
  498. })
  499. `
  500. const RPC_JS = `
  501. web3._extend({
  502. property: 'rpc',
  503. methods: [],
  504. properties:
  505. [
  506. new web3._extend.Property({
  507. name: 'modules',
  508. getter: 'rpc_modules'
  509. })
  510. ]
  511. });
  512. `
  513. const Shh_JS = `
  514. web3._extend({
  515. property: 'shh',
  516. methods: [],
  517. properties:
  518. [
  519. new web3._extend.Property({
  520. name: 'version',
  521. getter: 'shh_version',
  522. outputFormatter: web3._extend.utils.toDecimal
  523. })
  524. ]
  525. });
  526. `
  527. const SWARMFS_JS = `
  528. web3._extend({
  529. property: 'swarmfs',
  530. methods:
  531. [
  532. new web3._extend.Method({
  533. name: 'mount',
  534. call: 'swarmfs_mount',
  535. params: 2
  536. }),
  537. new web3._extend.Method({
  538. name: 'unmount',
  539. call: 'swarmfs_unmount',
  540. params: 1
  541. }),
  542. new web3._extend.Method({
  543. name: 'listmounts',
  544. call: 'swarmfs_listmounts',
  545. params: 0
  546. })
  547. ]
  548. });
  549. `
  550. const TxPool_JS = `
  551. web3._extend({
  552. property: 'txpool',
  553. methods: [],
  554. properties:
  555. [
  556. new web3._extend.Property({
  557. name: 'content',
  558. getter: 'txpool_content'
  559. }),
  560. new web3._extend.Property({
  561. name: 'inspect',
  562. getter: 'txpool_inspect'
  563. }),
  564. new web3._extend.Property({
  565. name: 'status',
  566. getter: 'txpool_status',
  567. outputFormatter: function(status) {
  568. status.pending = web3._extend.utils.toDecimal(status.pending);
  569. status.queued = web3._extend.utils.toDecimal(status.queued);
  570. return status;
  571. }
  572. })
  573. ]
  574. });
  575. `