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

148
// Parse a comma separated API string to individual api's
149
func ParseApiString(apistr string, codec codec.Codec, xeth *xeth.XEth, eth *eth.Ethereum) ([]shared.EthereumApi, error) {
150 151 152 153 154
	if len(strings.TrimSpace(apistr)) == 0 {
		return nil, fmt.Errorf("Empty apistr provided")
	}

	names := strings.Split(apistr, ",")
155
	apis := make([]shared.EthereumApi, len(names))
156 157 158

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

	return apis, nil
}
Bas van Kervel's avatar
Bas van Kervel committed
186 187 188

func Javascript(name string) string {
	switch strings.ToLower(strings.TrimSpace(name)) {
189
	case shared.AdminApiName:
Bas van Kervel's avatar
Bas van Kervel committed
190
		return Admin_JS
191
	case shared.DebugApiName:
Bas van Kervel's avatar
Bas van Kervel committed
192
		return Debug_JS
193
	case shared.DbApiName:
Bas van Kervel's avatar
Bas van Kervel committed
194
		return Db_JS
195 196
	case shared.EthApiName:
		return Eth_JS
197
	case shared.MinerApiName:
Bas van Kervel's avatar
Bas van Kervel committed
198
		return Miner_JS
199
	case shared.NetApiName:
Bas van Kervel's avatar
Bas van Kervel committed
200
		return Net_JS
201
	case shared.ShhApiName:
Bas van Kervel's avatar
Bas van Kervel committed
202
		return Shh_JS
203
	case shared.TxPoolApiName:
Bas van Kervel's avatar
Bas van Kervel committed
204
		return TxPool_JS
205
	case shared.PersonalApiName:
Bas van Kervel's avatar
Bas van Kervel committed
206
		return Personal_JS
Bas van Kervel's avatar
Bas van Kervel committed
207 208 209 210
	}

	return ""
}