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