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

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 152 153 154 155
	if len(strings.TrimSpace(apistr)) == 0 {
		return nil, fmt.Errorf("Empty apistr provided")
	}

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

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

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

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

	return ""
}