utils.go 4.67 KB
Newer Older
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 6 7 8
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
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 13 14
// GNU Lesser General Public License for more details.
//
// 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 18 19 20 21 22 23 24 25
package api

import (
	"strings"

	"fmt"

	"github.com/ethereum/go-ethereum/eth"
	"github.com/ethereum/go-ethereum/rpc/codec"
26
	"github.com/ethereum/go-ethereum/rpc/shared"
27 28 29
	"github.com/ethereum/go-ethereum/xeth"
)

30 31 32 33 34
var (
	// Mapping between the different methods each api supports
	AutoCompletion = map[string][]string{
		"admin": []string{
			"addPeer",
35
			"datadir",
zelig's avatar
zelig committed
36
			"enableUserAgent",
37
			"exportChain",
38
			"getContractInfo",
zelig's avatar
zelig committed
39
			"httpGet",
40
			"importChain",
41 42 43 44
			"nodeInfo",
			"peers",
			"register",
			"registerUrl",
zelig's avatar
zelig committed
45 46 47 48
			"saveInfo",
			"setGlobalRegistrar",
			"setHashReg",
			"setUrlHint",
49
			"setSolc",
zelig's avatar
zelig committed
50
			"sleep",
51 52
			"sleepBlocks",
			"startNatSpec",
53
			"startRPC",
54
			"stopNatSpec",
55
			"stopRPC",
56
			"verbosity",
57
		},
Bas van Kervel's avatar
Bas van Kervel committed
58 59 60 61 62 63
		"db": []string{
			"getString",
			"putString",
			"getHex",
			"putHex",
		},
64 65 66
		"debug": []string{
			"dumpBlock",
			"getBlockRlp",
67
			"metrics",
68 69 70 71 72 73 74 75
			"printBlock",
			"processBlock",
			"seedHash",
			"setHead",
		},
		"eth": []string{
			"accounts",
			"blockNumber",
76 77
			"call",
			"contract",
78
			"coinbase",
79 80 81 82 83 84 85 86 87 88 89 90 91
			"compile.lll",
			"compile.serpent",
			"compile.solidity",
			"contract",
			"defaultAccount",
			"defaultBlock",
			"estimateGas",
			"filter",
			"getBalance",
			"getBlock",
			"getBlockTransactionCount",
			"getBlockUncleCount",
			"getCode",
zelig's avatar
zelig committed
92
			"getNatSpec",
93
			"getCompilers",
94 95
			"gasPrice",
			"getStorageAt",
96
			"getTransaction",
97
			"getTransactionCount",
98 99 100
			"getTransactionFromBlock",
			"getTransactionReceipt",
			"getUncle",
101
			"hashrate",
102 103
			"mining",
			"namereg",
104
			"pendingTransactions",
Bas van Kervel's avatar
Bas van Kervel committed
105
			"resend",
106 107 108
			"sendRawTransaction",
			"sendTransaction",
			"sign",
109
			"syncing",
110 111 112 113
		},
		"miner": []string{
			"hashrate",
			"makeDAG",
114
			"setEtherbase",
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
			"setExtra",
			"setGasPrice",
			"startAutoDAG",
			"start",
			"stopAutoDAG",
			"stop",
		},
		"net": []string{
			"peerCount",
			"listening",
		},
		"personal": []string{
			"listAccounts",
			"newAccount",
			"unlockAccount",
		},
		"shh": []string{
			"post",
133
			"newIdentity",
134
			"hasIdentity",
135 136 137
			"newGroup",
			"addToGroup",
			"filter",
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
		},
		"txpool": []string{
			"status",
		},
		"web3": []string{
			"sha3",
			"version",
			"fromWei",
			"toWei",
			"toHex",
			"toAscii",
			"fromAscii",
			"toBigNumber",
			"isAddress",
		},
	}
)

156
// Parse a comma separated API string to individual api's
zelig's avatar
zelig committed
157
func ParseApiString(apistr string, codec codec.Codec, xeth *xeth.XEth, eth *eth.Ethereum) ([]shared.EthereumApi, error) {
158 159 160 161 162
	if len(strings.TrimSpace(apistr)) == 0 {
		return nil, fmt.Errorf("Empty apistr provided")
	}

	names := strings.Split(apistr, ",")
163
	apis := make([]shared.EthereumApi, len(names))
164 165 166

	for i, name := range names {
		switch strings.ToLower(strings.TrimSpace(name)) {
167
		case shared.AdminApiName:
168
			apis[i] = NewAdminApi(xeth, eth, codec)
169
		case shared.DebugApiName:
Bas van Kervel's avatar
Bas van Kervel committed
170
			apis[i] = NewDebugApi(xeth, eth, codec)
171
		case shared.DbApiName:
Bas van Kervel's avatar
Bas van Kervel committed
172
			apis[i] = NewDbApi(xeth, eth, codec)
173
		case shared.EthApiName:
174
			apis[i] = NewEthApi(xeth, eth, codec)
175
		case shared.MinerApiName:
Bas van Kervel's avatar
Bas van Kervel committed
176
			apis[i] = NewMinerApi(eth, codec)
177
		case shared.NetApiName:
Bas van Kervel's avatar
Bas van Kervel committed
178
			apis[i] = NewNetApi(xeth, eth, codec)
179
		case shared.ShhApiName:
Bas van Kervel's avatar
Bas van Kervel committed
180
			apis[i] = NewShhApi(xeth, eth, codec)
181
		case shared.TxPoolApiName:
Bas van Kervel's avatar
Bas van Kervel committed
182
			apis[i] = NewTxPoolApi(xeth, eth, codec)
183
		case shared.PersonalApiName:
Bas van Kervel's avatar
Bas van Kervel committed
184
			apis[i] = NewPersonalApi(xeth, eth, codec)
185
		case shared.Web3ApiName:
Bas van Kervel's avatar
Bas van Kervel committed
186
			apis[i] = NewWeb3Api(xeth, codec)
187 188 189 190 191 192 193
		default:
			return nil, fmt.Errorf("Unknown API '%s'", name)
		}
	}

	return apis, nil
}
Bas van Kervel's avatar
Bas van Kervel committed
194 195 196

func Javascript(name string) string {
	switch strings.ToLower(strings.TrimSpace(name)) {
197
	case shared.AdminApiName:
Bas van Kervel's avatar
Bas van Kervel committed
198
		return Admin_JS
199
	case shared.DebugApiName:
Bas van Kervel's avatar
Bas van Kervel committed
200
		return Debug_JS
201
	case shared.DbApiName:
Bas van Kervel's avatar
Bas van Kervel committed
202
		return Db_JS
203 204
	case shared.EthApiName:
		return Eth_JS
205
	case shared.MinerApiName:
Bas van Kervel's avatar
Bas van Kervel committed
206
		return Miner_JS
207
	case shared.NetApiName:
Bas van Kervel's avatar
Bas van Kervel committed
208
		return Net_JS
209
	case shared.ShhApiName:
Bas van Kervel's avatar
Bas van Kervel committed
210
		return Shh_JS
211
	case shared.TxPoolApiName:
Bas van Kervel's avatar
Bas van Kervel committed
212
		return TxPool_JS
213
	case shared.PersonalApiName:
Bas van Kervel's avatar
Bas van Kervel committed
214
		return Personal_JS
Bas van Kervel's avatar
Bas van Kervel committed
215 216 217 218
	}

	return ""
}