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