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