github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/internal/web3ext/web3ext.go (about) 1 // Copyright 2015 The Spectrum Authors 2 // This file is part of the Spectrum library. 3 // 4 // The Spectrum 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 Spectrum 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 Spectrum 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 "tribe": Tribe_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 Tribe_JS = ` 114 web3._extend({ 115 property: 'tribe', 116 methods: [ 117 new web3._extend.Method({ 118 name: 'getSigners', 119 call: 'tribe_getSigners', 120 params: 1, 121 inputFormatter: [null] 122 }), 123 new web3._extend.Method({ 124 name: 'getStatus', 125 call: 'tribe_getStatus', 126 params: 1, 127 inputFormatter: [null] 128 }), 129 new web3._extend.Method({ 130 name: 'getVolunteers', 131 call: 'tribe_getVolunteers', 132 params: 1, 133 inputFormatter: [null] 134 }), 135 new web3._extend.Method({ 136 name: 'getSinners', 137 call: 'tribe_getSinners', 138 params: 1, 139 inputFormatter: [null] 140 }), 141 new web3._extend.Method({ 142 name: 'getHistory', 143 call: 'tribe_getHistory', 144 params: 2, 145 inputFormatter: [null,null] 146 }), 147 new web3._extend.Method({ 148 name: 'getMiner', 149 call: 'tribe_getMiner', 150 params: 1, 151 inputFormatter: [null] 152 }), 153 new web3._extend.Method({ 154 name: 'bindInfo', 155 call: 'tribe_bindInfo', 156 params: 2, 157 inputFormatter: [null,null] 158 }), 159 new web3._extend.Method({ 160 name: 'bind', 161 call: 'tribe_bind', 162 params: 2, 163 inputFormatter: [null,null] 164 }), 165 new web3._extend.Method({ 166 name: 'unbind', 167 call: 'tribe_unbind', 168 params: 2, 169 inputFormatter: [null,null] 170 }), 171 new web3._extend.Method({ 172 name: 'bindSign', 173 call: 'tribe_bindSign', 174 params: 1, 175 inputFormatter: [null] 176 }), 177 new web3._extend.Method({ 178 name: 'pocDeposit', 179 call: 'tribe_pocDeposit', 180 params: 2, 181 inputFormatter: [null,null] 182 }), 183 new web3._extend.Method({ 184 name: 'pocStart', 185 call: 'tribe_pocStart', 186 params: 2, 187 inputFormatter: [null,null] 188 }), 189 new web3._extend.Method({ 190 name: 'pocStop', 191 call: 'tribe_pocStop', 192 params: 2, 193 inputFormatter: [null,null] 194 }), 195 new web3._extend.Method({ 196 name: 'pocWithdraw', 197 call: 'tribe_pocWithdraw', 198 params: 2, 199 inputFormatter: [null,null] 200 }), 201 new web3._extend.Method({ 202 name: 'pocWithdrawSurplus', 203 call: 'tribe_pocWithdrawSurplus', 204 params: 2, 205 inputFormatter: [null,null] 206 }), 207 new web3._extend.Method({ 208 name: 'pocGetStatus', 209 call: 'tribe_pocGetStatus', 210 params: 1, 211 inputFormatter: [null] 212 }), 213 ], 214 }); 215 ` 216 217 const Admin_JS = ` 218 web3._extend({ 219 property: 'admin', 220 methods: [ 221 new web3._extend.Method({ 222 name: 'addPeer', 223 call: 'admin_addPeer', 224 params: 1 225 }), 226 new web3._extend.Method({ 227 name: 'removePeer', 228 call: 'admin_removePeer', 229 params: 1 230 }), 231 new web3._extend.Method({ 232 name: 'exportChain', 233 call: 'admin_exportChain', 234 params: 1, 235 inputFormatter: [null] 236 }), 237 new web3._extend.Method({ 238 name: 'importChain', 239 call: 'admin_importChain', 240 params: 1 241 }), 242 new web3._extend.Method({ 243 name: 'sleepBlocks', 244 call: 'admin_sleepBlocks', 245 params: 2 246 }), 247 new web3._extend.Method({ 248 name: 'startRPC', 249 call: 'admin_startRPC', 250 params: 4, 251 inputFormatter: [null, null, null, null] 252 }), 253 new web3._extend.Method({ 254 name: 'stopRPC', 255 call: 'admin_stopRPC' 256 }), 257 new web3._extend.Method({ 258 name: 'startWS', 259 call: 'admin_startWS', 260 params: 4, 261 inputFormatter: [null, null, null, null] 262 }), 263 new web3._extend.Method({ 264 name: 'stopWS', 265 call: 'admin_stopWS' 266 }), 267 ], 268 properties: [ 269 new web3._extend.Property({ 270 name: 'nodeInfo', 271 getter: 'admin_nodeInfo' 272 }), 273 new web3._extend.Property({ 274 name: 'peers', 275 getter: 'admin_peers' 276 }), 277 new web3._extend.Property({ 278 name: 'datadir', 279 getter: 'admin_datadir' 280 }), 281 ] 282 }); 283 ` 284 285 const Debug_JS = ` 286 web3._extend({ 287 property: 'debug', 288 methods: [ 289 new web3._extend.Method({ 290 name: 'printBlock', 291 call: 'debug_printBlock', 292 params: 1 293 }), 294 new web3._extend.Method({ 295 name: 'getBlockRlp', 296 call: 'debug_getBlockRlp', 297 params: 1 298 }), 299 new web3._extend.Method({ 300 name: 'setHead', 301 call: 'debug_setHead', 302 params: 1 303 }), 304 new web3._extend.Method({ 305 name: 'seedHash', 306 call: 'debug_seedHash', 307 params: 1 308 }), 309 new web3._extend.Method({ 310 name: 'dumpBlock', 311 call: 'debug_dumpBlock', 312 params: 1 313 }), 314 new web3._extend.Method({ 315 name: 'chaindbProperty', 316 call: 'debug_chaindbProperty', 317 params: 1, 318 outputFormatter: console.log 319 }), 320 new web3._extend.Method({ 321 name: 'chaindbCompact', 322 call: 'debug_chaindbCompact', 323 }), 324 new web3._extend.Method({ 325 name: 'metrics', 326 call: 'debug_metrics', 327 params: 1 328 }), 329 new web3._extend.Method({ 330 name: 'verbosity', 331 call: 'debug_verbosity', 332 params: 1 333 }), 334 new web3._extend.Method({ 335 name: 'vmodule', 336 call: 'debug_vmodule', 337 params: 1 338 }), 339 new web3._extend.Method({ 340 name: 'backtraceAt', 341 call: 'debug_backtraceAt', 342 params: 1, 343 }), 344 new web3._extend.Method({ 345 name: 'stacks', 346 call: 'debug_stacks', 347 params: 0, 348 outputFormatter: console.log 349 }), 350 new web3._extend.Method({ 351 name: 'freeOSMemory', 352 call: 'debug_freeOSMemory', 353 params: 0, 354 }), 355 new web3._extend.Method({ 356 name: 'setGCPercent', 357 call: 'debug_setGCPercent', 358 params: 1, 359 }), 360 new web3._extend.Method({ 361 name: 'memStats', 362 call: 'debug_memStats', 363 params: 0, 364 }), 365 new web3._extend.Method({ 366 name: 'gcStats', 367 call: 'debug_gcStats', 368 params: 0, 369 }), 370 new web3._extend.Method({ 371 name: 'cpuProfile', 372 call: 'debug_cpuProfile', 373 params: 2 374 }), 375 new web3._extend.Method({ 376 name: 'startCPUProfile', 377 call: 'debug_startCPUProfile', 378 params: 1 379 }), 380 new web3._extend.Method({ 381 name: 'stopCPUProfile', 382 call: 'debug_stopCPUProfile', 383 params: 0 384 }), 385 new web3._extend.Method({ 386 name: 'goTrace', 387 call: 'debug_goTrace', 388 params: 2 389 }), 390 new web3._extend.Method({ 391 name: 'startGoTrace', 392 call: 'debug_startGoTrace', 393 params: 1 394 }), 395 new web3._extend.Method({ 396 name: 'stopGoTrace', 397 call: 'debug_stopGoTrace', 398 params: 0 399 }), 400 new web3._extend.Method({ 401 name: 'blockProfile', 402 call: 'debug_blockProfile', 403 params: 2 404 }), 405 new web3._extend.Method({ 406 name: 'setBlockProfileRate', 407 call: 'debug_setBlockProfileRate', 408 params: 1 409 }), 410 new web3._extend.Method({ 411 name: 'writeBlockProfile', 412 call: 'debug_writeBlockProfile', 413 params: 1 414 }), 415 new web3._extend.Method({ 416 name: 'writeMemProfile', 417 call: 'debug_writeMemProfile', 418 params: 1 419 }), 420 new web3._extend.Method({ 421 name: 'traceBlock', 422 call: 'debug_traceBlock', 423 params: 2, 424 inputFormatter: [null, null] 425 }), 426 new web3._extend.Method({ 427 name: 'traceBlockFromFile', 428 call: 'debug_traceBlockFromFile', 429 params: 2, 430 inputFormatter: [null, null] 431 }), 432 new web3._extend.Method({ 433 name: 'traceBlockByNumber', 434 call: 'debug_traceBlockByNumber', 435 params: 2, 436 inputFormatter: [null, null] 437 }), 438 new web3._extend.Method({ 439 name: 'traceBlockByHash', 440 call: 'debug_traceBlockByHash', 441 params: 2, 442 inputFormatter: [null, null] 443 }), 444 new web3._extend.Method({ 445 name: 'traceTransaction', 446 call: 'debug_traceTransaction', 447 params: 2, 448 inputFormatter: [null, null] 449 }), 450 new web3._extend.Method({ 451 name: 'preimage', 452 call: 'debug_preimage', 453 params: 1, 454 inputFormatter: [null] 455 }), 456 new web3._extend.Method({ 457 name: 'getBadBlocks', 458 call: 'debug_getBadBlocks', 459 params: 0, 460 }), 461 new web3._extend.Method({ 462 name: 'storageRangeAt', 463 call: 'debug_storageRangeAt', 464 params: 5, 465 }), 466 new web3._extend.Method({ 467 name: 'getModifiedAccountsByNumber', 468 call: 'debug_getModifiedAccountsByNumber', 469 params: 2, 470 inputFormatter: [null, null], 471 }), 472 new web3._extend.Method({ 473 name: 'getModifiedAccountsByHash', 474 call: 'debug_getModifiedAccountsByHash', 475 params: 2, 476 inputFormatter:[null, null], 477 }), 478 ], 479 properties: [] 480 }); 481 ` 482 483 const Eth_JS = ` 484 web3._extend({ 485 property: 'eth', 486 methods: [ 487 new web3._extend.Method({ 488 name: 'chainId', 489 call: 'eth_chainId', 490 params: 0 491 }), 492 new web3._extend.Method({ 493 name: 'sign', 494 call: 'eth_sign', 495 params: 2, 496 inputFormatter: [web3._extend.formatters.inputAddressFormatter, null] 497 }), 498 new web3._extend.Method({ 499 name: 'resend', 500 call: 'eth_resend', 501 params: 3, 502 inputFormatter: [web3._extend.formatters.inputTransactionFormatter, web3._extend.utils.fromDecimal, web3._extend.utils.fromDecimal] 503 }), 504 new web3._extend.Method({ 505 name: 'signTransaction', 506 call: 'eth_signTransaction', 507 params: 1, 508 inputFormatter: [web3._extend.formatters.inputTransactionFormatter] 509 }), 510 new web3._extend.Method({ 511 name: 'submitTransaction', 512 call: 'eth_submitTransaction', 513 params: 1, 514 inputFormatter: [web3._extend.formatters.inputTransactionFormatter] 515 }), 516 new web3._extend.Method({ 517 name: 'getRawTransaction', 518 call: 'eth_getRawTransactionByHash', 519 params: 1 520 }), 521 new web3._extend.Method({ 522 name: 'getRawTransactionFromBlock', 523 call: function(args) { 524 return (web3._extend.utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getRawTransactionByBlockHashAndIndex' : 'eth_getRawTransactionByBlockNumberAndIndex'; 525 }, 526 params: 2, 527 inputFormatter: [web3._extend.formatters.inputBlockNumberFormatter, web3._extend.utils.toHex] 528 }), 529 ], 530 properties: [ 531 new web3._extend.Property({ 532 name: 'pendingTransactions', 533 getter: 'eth_pendingTransactions', 534 outputFormatter: function(txs) { 535 var formatted = []; 536 for (var i = 0; i < txs.length; i++) { 537 formatted.push(web3._extend.formatters.outputTransactionFormatter(txs[i])); 538 formatted[i].blockHash = null; 539 } 540 return formatted; 541 } 542 }), 543 ] 544 }); 545 ` 546 547 const Miner_JS = ` 548 web3._extend({ 549 property: 'miner', 550 methods: [ 551 /* new web3._extend.Method({ 552 name: 'start', 553 call: 'miner_start', 554 params: 1, 555 inputFormatter: [null] 556 }), 557 new web3._extend.Method({ 558 name: 'stop', 559 call: 'miner_stop' 560 }), 561 */ 562 /* 563 new web3._extend.Method({ 564 name: 'setEtherbase', 565 call: 'miner_setEtherbase', 566 params: 1, 567 inputFormatter: [web3._extend.formatters.inputAddressFormatter] 568 }), 569 new web3._extend.Method({ 570 name: 'setExtra', 571 call: 'miner_setExtra', 572 params: 1 573 }), 574 new web3._extend.Method({ 575 name: 'getHashrate', 576 call: 'miner_getHashrate' 577 }), 578 */ 579 new web3._extend.Method({ 580 name: 'setGasPrice', 581 call: 'miner_setGasPrice', 582 params: 1, 583 inputFormatter: [web3._extend.utils.fromDecimal] 584 }), 585 586 ], 587 properties: [] 588 }); 589 ` 590 591 const Net_JS = ` 592 web3._extend({ 593 property: 'net', 594 methods: [], 595 properties: [ 596 new web3._extend.Property({ 597 name: 'version', 598 getter: 'net_version' 599 }), 600 ] 601 }); 602 ` 603 604 const Personal_JS = ` 605 web3._extend({ 606 property: 'personal', 607 methods: [ 608 new web3._extend.Method({ 609 name: 'importRawKey', 610 call: 'personal_importRawKey', 611 params: 2 612 }), 613 new web3._extend.Method({ 614 name: 'sign', 615 call: 'personal_sign', 616 params: 3, 617 inputFormatter: [null, web3._extend.formatters.inputAddressFormatter, null] 618 }), 619 new web3._extend.Method({ 620 name: 'ecRecover', 621 call: 'personal_ecRecover', 622 params: 2 623 }), 624 new web3._extend.Method({ 625 name: 'openWallet', 626 call: 'personal_openWallet', 627 params: 2 628 }), 629 new web3._extend.Method({ 630 name: 'deriveAccount', 631 call: 'personal_deriveAccount', 632 params: 3 633 }), 634 ], 635 properties: [ 636 new web3._extend.Property({ 637 name: 'listWallets', 638 getter: 'personal_listWallets' 639 }), 640 ] 641 }) 642 ` 643 644 const RPC_JS = ` 645 web3._extend({ 646 property: 'rpc', 647 methods: [], 648 properties: [ 649 new web3._extend.Property({ 650 name: 'modules', 651 getter: 'rpc_modules' 652 }), 653 ] 654 }); 655 ` 656 657 const Shh_JS = ` 658 web3._extend({ 659 property: 'shh', 660 methods: [ 661 ], 662 properties: 663 [ 664 new web3._extend.Property({ 665 name: 'version', 666 getter: 'shh_version', 667 outputFormatter: web3._extend.utils.toDecimal 668 }), 669 new web3._extend.Property({ 670 name: 'info', 671 getter: 'shh_info' 672 }), 673 ] 674 }); 675 ` 676 677 const SWARMFS_JS = ` 678 web3._extend({ 679 property: 'swarmfs', 680 methods: 681 [ 682 new web3._extend.Method({ 683 name: 'mount', 684 call: 'swarmfs_mount', 685 params: 2 686 }), 687 new web3._extend.Method({ 688 name: 'unmount', 689 call: 'swarmfs_unmount', 690 params: 1 691 }), 692 new web3._extend.Method({ 693 name: 'listmounts', 694 call: 'swarmfs_listmounts', 695 params: 0 696 }), 697 ] 698 }); 699 ` 700 701 const TxPool_JS = ` 702 web3._extend({ 703 property: 'txpool', 704 methods: [], 705 properties: 706 [ 707 new web3._extend.Property({ 708 name: 'content', 709 getter: 'txpool_content' 710 }), 711 new web3._extend.Property({ 712 name: 'inspect', 713 getter: 'txpool_inspect' 714 }), 715 new web3._extend.Property({ 716 name: 'status', 717 getter: 'txpool_status', 718 outputFormatter: function(status) { 719 status.pending = web3._extend.utils.toDecimal(status.pending); 720 status.queued = web3._extend.utils.toDecimal(status.queued); 721 return status; 722 } 723 }), 724 ] 725 }); 726 `