github.com/fxsjy/go-ethereum@v1.8.4-0.20180410143526-2e247705cd27/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: 'seedHash',
   201  			call: 'debug_seedHash',
   202  			params: 1
   203  		}),
   204  		new web3._extend.Method({
   205  			name: 'dumpBlock',
   206  			call: 'debug_dumpBlock',
   207  			params: 1
   208  		}),
   209  		new web3._extend.Method({
   210  			name: 'chaindbProperty',
   211  			call: 'debug_chaindbProperty',
   212  			params: 1,
   213  			outputFormatter: console.log
   214  		}),
   215  		new web3._extend.Method({
   216  			name: 'chaindbCompact',
   217  			call: 'debug_chaindbCompact',
   218  		}),
   219  		new web3._extend.Method({
   220  			name: 'metrics',
   221  			call: 'debug_metrics',
   222  			params: 1
   223  		}),
   224  		new web3._extend.Method({
   225  			name: 'verbosity',
   226  			call: 'debug_verbosity',
   227  			params: 1
   228  		}),
   229  		new web3._extend.Method({
   230  			name: 'vmodule',
   231  			call: 'debug_vmodule',
   232  			params: 1
   233  		}),
   234  		new web3._extend.Method({
   235  			name: 'backtraceAt',
   236  			call: 'debug_backtraceAt',
   237  			params: 1,
   238  		}),
   239  		new web3._extend.Method({
   240  			name: 'stacks',
   241  			call: 'debug_stacks',
   242  			params: 0,
   243  			outputFormatter: console.log
   244  		}),
   245  		new web3._extend.Method({
   246  			name: 'freeOSMemory',
   247  			call: 'debug_freeOSMemory',
   248  			params: 0,
   249  		}),
   250  		new web3._extend.Method({
   251  			name: 'setGCPercent',
   252  			call: 'debug_setGCPercent',
   253  			params: 1,
   254  		}),
   255  		new web3._extend.Method({
   256  			name: 'memStats',
   257  			call: 'debug_memStats',
   258  			params: 0,
   259  		}),
   260  		new web3._extend.Method({
   261  			name: 'gcStats',
   262  			call: 'debug_gcStats',
   263  			params: 0,
   264  		}),
   265  		new web3._extend.Method({
   266  			name: 'cpuProfile',
   267  			call: 'debug_cpuProfile',
   268  			params: 2
   269  		}),
   270  		new web3._extend.Method({
   271  			name: 'startCPUProfile',
   272  			call: 'debug_startCPUProfile',
   273  			params: 1
   274  		}),
   275  		new web3._extend.Method({
   276  			name: 'stopCPUProfile',
   277  			call: 'debug_stopCPUProfile',
   278  			params: 0
   279  		}),
   280  		new web3._extend.Method({
   281  			name: 'goTrace',
   282  			call: 'debug_goTrace',
   283  			params: 2
   284  		}),
   285  		new web3._extend.Method({
   286  			name: 'startGoTrace',
   287  			call: 'debug_startGoTrace',
   288  			params: 1
   289  		}),
   290  		new web3._extend.Method({
   291  			name: 'stopGoTrace',
   292  			call: 'debug_stopGoTrace',
   293  			params: 0
   294  		}),
   295  		new web3._extend.Method({
   296  			name: 'blockProfile',
   297  			call: 'debug_blockProfile',
   298  			params: 2
   299  		}),
   300  		new web3._extend.Method({
   301  			name: 'setBlockProfileRate',
   302  			call: 'debug_setBlockProfileRate',
   303  			params: 1
   304  		}),
   305  		new web3._extend.Method({
   306  			name: 'writeBlockProfile',
   307  			call: 'debug_writeBlockProfile',
   308  			params: 1
   309  		}),
   310  		new web3._extend.Method({
   311  			name: 'mutexProfile',
   312  			call: 'debug_mutexProfile',
   313  			params: 2
   314  		}),
   315  		new web3._extend.Method({
   316  			name: 'setMutexProfileRate',
   317  			call: 'debug_setMutexProfileRate',
   318  			params: 1
   319  		}),
   320  		new web3._extend.Method({
   321  			name: 'writeMutexProfile',
   322  			call: 'debug_writeMutexProfile',
   323  			params: 1
   324  		}),
   325  		new web3._extend.Method({
   326  			name: 'writeMemProfile',
   327  			call: 'debug_writeMemProfile',
   328  			params: 1
   329  		}),
   330  		new web3._extend.Method({
   331  			name: 'traceBlock',
   332  			call: 'debug_traceBlock',
   333  			params: 2,
   334  			inputFormatter: [null, null]
   335  		}),
   336  		new web3._extend.Method({
   337  			name: 'traceBlockFromFile',
   338  			call: 'debug_traceBlockFromFile',
   339  			params: 2,
   340  			inputFormatter: [null, null]
   341  		}),
   342  		new web3._extend.Method({
   343  			name: 'traceBlockByNumber',
   344  			call: 'debug_traceBlockByNumber',
   345  			params: 2,
   346  			inputFormatter: [null, null]
   347  		}),
   348  		new web3._extend.Method({
   349  			name: 'traceBlockByHash',
   350  			call: 'debug_traceBlockByHash',
   351  			params: 2,
   352  			inputFormatter: [null, null]
   353  		}),
   354  		new web3._extend.Method({
   355  			name: 'traceTransaction',
   356  			call: 'debug_traceTransaction',
   357  			params: 2,
   358  			inputFormatter: [null, null]
   359  		}),
   360  		new web3._extend.Method({
   361  			name: 'preimage',
   362  			call: 'debug_preimage',
   363  			params: 1,
   364  			inputFormatter: [null]
   365  		}),
   366  		new web3._extend.Method({
   367  			name: 'getBadBlocks',
   368  			call: 'debug_getBadBlocks',
   369  			params: 0,
   370  		}),
   371  		new web3._extend.Method({
   372  			name: 'storageRangeAt',
   373  			call: 'debug_storageRangeAt',
   374  			params: 5,
   375  		}),
   376  		new web3._extend.Method({
   377  			name: 'getModifiedAccountsByNumber',
   378  			call: 'debug_getModifiedAccountsByNumber',
   379  			params: 2,
   380  			inputFormatter: [null, null],
   381  		}),
   382  		new web3._extend.Method({
   383  			name: 'getModifiedAccountsByHash',
   384  			call: 'debug_getModifiedAccountsByHash',
   385  			params: 2,
   386  			inputFormatter:[null, null],
   387  		}),
   388  	],
   389  	properties: []
   390  });
   391  `
   392  
   393  const Eth_JS = `
   394  web3._extend({
   395  	property: 'eth',
   396  	methods: [
   397  		new web3._extend.Method({
   398  			name: 'sign',
   399  			call: 'eth_sign',
   400  			params: 2,
   401  			inputFormatter: [web3._extend.formatters.inputAddressFormatter, null]
   402  		}),
   403  		new web3._extend.Method({
   404  			name: 'resend',
   405  			call: 'eth_resend',
   406  			params: 3,
   407  			inputFormatter: [web3._extend.formatters.inputTransactionFormatter, web3._extend.utils.fromDecimal, web3._extend.utils.fromDecimal]
   408  		}),
   409  		new web3._extend.Method({
   410  			name: 'signTransaction',
   411  			call: 'eth_signTransaction',
   412  			params: 1,
   413  			inputFormatter: [web3._extend.formatters.inputTransactionFormatter]
   414  		}),
   415  		new web3._extend.Method({
   416  			name: 'submitTransaction',
   417  			call: 'eth_submitTransaction',
   418  			params: 1,
   419  			inputFormatter: [web3._extend.formatters.inputTransactionFormatter]
   420  		}),
   421  		new web3._extend.Method({
   422  			name: 'getRawTransaction',
   423  			call: 'eth_getRawTransactionByHash',
   424  			params: 1
   425  		}),
   426  		new web3._extend.Method({
   427  			name: 'getRawTransactionFromBlock',
   428  			call: function(args) {
   429  				return (web3._extend.utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getRawTransactionByBlockHashAndIndex' : 'eth_getRawTransactionByBlockNumberAndIndex';
   430  			},
   431  			params: 2,
   432  			inputFormatter: [web3._extend.formatters.inputBlockNumberFormatter, web3._extend.utils.toHex]
   433  		}),
   434  	],
   435  	properties: [
   436  		new web3._extend.Property({
   437  			name: 'pendingTransactions',
   438  			getter: 'eth_pendingTransactions',
   439  			outputFormatter: function(txs) {
   440  				var formatted = [];
   441  				for (var i = 0; i < txs.length; i++) {
   442  					formatted.push(web3._extend.formatters.outputTransactionFormatter(txs[i]));
   443  					formatted[i].blockHash = null;
   444  				}
   445  				return formatted;
   446  			}
   447  		}),
   448  	]
   449  });
   450  `
   451  
   452  const Miner_JS = `
   453  web3._extend({
   454  	property: 'miner',
   455  	methods: [
   456  		new web3._extend.Method({
   457  			name: 'start',
   458  			call: 'miner_start',
   459  			params: 1,
   460  			inputFormatter: [null]
   461  		}),
   462  		new web3._extend.Method({
   463  			name: 'stop',
   464  			call: 'miner_stop'
   465  		}),
   466  		new web3._extend.Method({
   467  			name: 'setEtherbase',
   468  			call: 'miner_setEtherbase',
   469  			params: 1,
   470  			inputFormatter: [web3._extend.formatters.inputAddressFormatter]
   471  		}),
   472  		new web3._extend.Method({
   473  			name: 'setExtra',
   474  			call: 'miner_setExtra',
   475  			params: 1
   476  		}),
   477  		new web3._extend.Method({
   478  			name: 'setGasPrice',
   479  			call: 'miner_setGasPrice',
   480  			params: 1,
   481  			inputFormatter: [web3._extend.utils.fromDecimal]
   482  		}),
   483  		new web3._extend.Method({
   484  			name: 'getHashrate',
   485  			call: 'miner_getHashrate'
   486  		}),
   487  	],
   488  	properties: []
   489  });
   490  `
   491  
   492  const Net_JS = `
   493  web3._extend({
   494  	property: 'net',
   495  	methods: [],
   496  	properties: [
   497  		new web3._extend.Property({
   498  			name: 'version',
   499  			getter: 'net_version'
   500  		}),
   501  	]
   502  });
   503  `
   504  
   505  const Personal_JS = `
   506  web3._extend({
   507  	property: 'personal',
   508  	methods: [
   509  		new web3._extend.Method({
   510  			name: 'importRawKey',
   511  			call: 'personal_importRawKey',
   512  			params: 2
   513  		}),
   514  		new web3._extend.Method({
   515  			name: 'sign',
   516  			call: 'personal_sign',
   517  			params: 3,
   518  			inputFormatter: [null, web3._extend.formatters.inputAddressFormatter, null]
   519  		}),
   520  		new web3._extend.Method({
   521  			name: 'ecRecover',
   522  			call: 'personal_ecRecover',
   523  			params: 2
   524  		}),
   525  		new web3._extend.Method({
   526  			name: 'openWallet',
   527  			call: 'personal_openWallet',
   528  			params: 2
   529  		}),
   530  		new web3._extend.Method({
   531  			name: 'deriveAccount',
   532  			call: 'personal_deriveAccount',
   533  			params: 3
   534  		}),
   535  		new web3._extend.Method({
   536  			name: 'signTransaction',
   537  			call: 'personal_signTransaction',
   538  			params: 2,
   539  			inputFormatter: [web3._extend.formatters.inputTransactionFormatter, null]
   540  		}),
   541  	],
   542  	properties: [
   543  		new web3._extend.Property({
   544  			name: 'listWallets',
   545  			getter: 'personal_listWallets'
   546  		}),
   547  	]
   548  })
   549  `
   550  
   551  const RPC_JS = `
   552  web3._extend({
   553  	property: 'rpc',
   554  	methods: [],
   555  	properties: [
   556  		new web3._extend.Property({
   557  			name: 'modules',
   558  			getter: 'rpc_modules'
   559  		}),
   560  	]
   561  });
   562  `
   563  
   564  const Shh_JS = `
   565  web3._extend({
   566  	property: 'shh',
   567  	methods: [
   568  	],
   569  	properties:
   570  	[
   571  		new web3._extend.Property({
   572  			name: 'version',
   573  			getter: 'shh_version',
   574  			outputFormatter: web3._extend.utils.toDecimal
   575  		}),
   576  		new web3._extend.Property({
   577  			name: 'info',
   578  			getter: 'shh_info'
   579  		}),
   580  	]
   581  });
   582  `
   583  
   584  const SWARMFS_JS = `
   585  web3._extend({
   586  	property: 'swarmfs',
   587  	methods:
   588  	[
   589  		new web3._extend.Method({
   590  			name: 'mount',
   591  			call: 'swarmfs_mount',
   592  			params: 2
   593  		}),
   594  		new web3._extend.Method({
   595  			name: 'unmount',
   596  			call: 'swarmfs_unmount',
   597  			params: 1
   598  		}),
   599  		new web3._extend.Method({
   600  			name: 'listmounts',
   601  			call: 'swarmfs_listmounts',
   602  			params: 0
   603  		}),
   604  	]
   605  });
   606  `
   607  
   608  const TxPool_JS = `
   609  web3._extend({
   610  	property: 'txpool',
   611  	methods: [],
   612  	properties:
   613  	[
   614  		new web3._extend.Property({
   615  			name: 'content',
   616  			getter: 'txpool_content'
   617  		}),
   618  		new web3._extend.Property({
   619  			name: 'inspect',
   620  			getter: 'txpool_inspect'
   621  		}),
   622  		new web3._extend.Property({
   623  			name: 'status',
   624  			getter: 'txpool_status',
   625  			outputFormatter: function(status) {
   626  				status.pending = web3._extend.utils.toDecimal(status.pending);
   627  				status.queued = web3._extend.utils.toDecimal(status.queued);
   628  				return status;
   629  			}
   630  		}),
   631  	]
   632  });
   633  `