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