github.com/jeffallen/go-ethereum@v1.1.4-0.20150910155051-571d3236c49c/rpc/api/utils.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 api
    18  
    19  import (
    20  	"strings"
    21  
    22  	"fmt"
    23  
    24  	"github.com/ethereum/go-ethereum/eth"
    25  	"github.com/ethereum/go-ethereum/rpc/codec"
    26  	"github.com/ethereum/go-ethereum/rpc/shared"
    27  	"github.com/ethereum/go-ethereum/xeth"
    28  )
    29  
    30  var (
    31  	// Mapping between the different methods each api supports
    32  	AutoCompletion = map[string][]string{
    33  		"admin": []string{
    34  			"addPeer",
    35  			"chainSyncStatus",
    36  			"datadir",
    37  			"exportChain",
    38  			"getContractInfo",
    39  			"importChain",
    40  			"nodeInfo",
    41  			"peers",
    42  			"register",
    43  			"registerUrl",
    44  			"setSolc",
    45  			"sleepBlocks",
    46  			"startNatSpec",
    47  			"startRPC",
    48  			"stopNatSpec",
    49  			"stopRPC",
    50  			"verbosity",
    51  		},
    52  		"db": []string{
    53  			"getString",
    54  			"putString",
    55  			"getHex",
    56  			"putHex",
    57  		},
    58  		"debug": []string{
    59  			"dumpBlock",
    60  			"getBlockRlp",
    61  			"metrics",
    62  			"printBlock",
    63  			"processBlock",
    64  			"seedHash",
    65  			"setHead",
    66  		},
    67  		"eth": []string{
    68  			"accounts",
    69  			"blockNumber",
    70  			"call",
    71  			"contract",
    72  			"coinbase",
    73  			"compile.lll",
    74  			"compile.serpent",
    75  			"compile.solidity",
    76  			"contract",
    77  			"defaultAccount",
    78  			"defaultBlock",
    79  			"estimateGas",
    80  			"filter",
    81  			"getBalance",
    82  			"getBlock",
    83  			"getBlockTransactionCount",
    84  			"getBlockUncleCount",
    85  			"getCode",
    86  			"getCompilers",
    87  			"gasPrice",
    88  			"getStorageAt",
    89  			"getTransaction",
    90  			"getTransactionCount",
    91  			"getTransactionFromBlock",
    92  			"getTransactionReceipt",
    93  			"getUncle",
    94  			"hashrate",
    95  			"mining",
    96  			"namereg",
    97  			"pendingTransactions",
    98  			"resend",
    99  			"sendRawTransaction",
   100  			"sendTransaction",
   101  			"sign",
   102  		},
   103  		"miner": []string{
   104  			"hashrate",
   105  			"makeDAG",
   106  			"setEtherbase",
   107  			"setExtra",
   108  			"setGasPrice",
   109  			"startAutoDAG",
   110  			"start",
   111  			"stopAutoDAG",
   112  			"stop",
   113  		},
   114  		"net": []string{
   115  			"peerCount",
   116  			"listening",
   117  		},
   118  		"personal": []string{
   119  			"listAccounts",
   120  			"newAccount",
   121  			"deleteAccount",
   122  			"unlockAccount",
   123  		},
   124  		"shh": []string{
   125  			"post",
   126  			"newIdentify",
   127  			"hasIdentity",
   128  			"newGroup",
   129  			"addToGroup",
   130  			"filter",
   131  		},
   132  		"txpool": []string{
   133  			"status",
   134  		},
   135  		"web3": []string{
   136  			"sha3",
   137  			"version",
   138  			"fromWei",
   139  			"toWei",
   140  			"toHex",
   141  			"toAscii",
   142  			"fromAscii",
   143  			"toBigNumber",
   144  			"isAddress",
   145  		},
   146  	}
   147  )
   148  
   149  // Parse a comma separated API string to individual api's
   150  func ParseApiString(apistr string, codec codec.Codec, xeth *xeth.XEth, eth *eth.Ethereum) ([]shared.EthereumApi, error) {
   151  	if len(strings.TrimSpace(apistr)) == 0 {
   152  		return nil, fmt.Errorf("Empty apistr provided")
   153  	}
   154  
   155  	names := strings.Split(apistr, ",")
   156  	apis := make([]shared.EthereumApi, len(names))
   157  
   158  	for i, name := range names {
   159  		switch strings.ToLower(strings.TrimSpace(name)) {
   160  		case shared.AdminApiName:
   161  			apis[i] = NewAdminApi(xeth, eth, codec)
   162  		case shared.DebugApiName:
   163  			apis[i] = NewDebugApi(xeth, eth, codec)
   164  		case shared.DbApiName:
   165  			apis[i] = NewDbApi(xeth, eth, codec)
   166  		case shared.EthApiName:
   167  			apis[i] = NewEthApi(xeth, eth, codec)
   168  		case shared.MinerApiName:
   169  			apis[i] = NewMinerApi(eth, codec)
   170  		case shared.NetApiName:
   171  			apis[i] = NewNetApi(xeth, eth, codec)
   172  		case shared.ShhApiName:
   173  			apis[i] = NewShhApi(xeth, eth, codec)
   174  		case shared.TxPoolApiName:
   175  			apis[i] = NewTxPoolApi(xeth, eth, codec)
   176  		case shared.PersonalApiName:
   177  			apis[i] = NewPersonalApi(xeth, eth, codec)
   178  		case shared.Web3ApiName:
   179  			apis[i] = NewWeb3Api(xeth, codec)
   180  		default:
   181  			return nil, fmt.Errorf("Unknown API '%s'", name)
   182  		}
   183  	}
   184  
   185  	return apis, nil
   186  }
   187  
   188  func Javascript(name string) string {
   189  	switch strings.ToLower(strings.TrimSpace(name)) {
   190  	case shared.AdminApiName:
   191  		return Admin_JS
   192  	case shared.DebugApiName:
   193  		return Debug_JS
   194  	case shared.DbApiName:
   195  		return Db_JS
   196  	case shared.EthApiName:
   197  		return Eth_JS
   198  	case shared.MinerApiName:
   199  		return Miner_JS
   200  	case shared.NetApiName:
   201  		return Net_JS
   202  	case shared.ShhApiName:
   203  		return Shh_JS
   204  	case shared.TxPoolApiName:
   205  		return TxPool_JS
   206  	case shared.PersonalApiName:
   207  		return Personal_JS
   208  	}
   209  
   210  	return ""
   211  }