added console command

This commit is contained in:
Bas van Kervel
2015-06-08 12:43:58 +02:00
committed by Bas van Kervel
parent 2a0d888326
commit a1a475fb92
12 changed files with 4819 additions and 249 deletions

View File

@ -4,14 +4,26 @@ import "github.com/ethereum/go-ethereum/rpc/shared"
const (
// List with all API's which are offered over the IPC interface by default
DefaultIpcApis = "eth"
DefaultIpcApis = "eth,web3"
EthApiName = "eth"
MergedApiName = "merged"
Web3ApiName = "web3"
)
// Ethereum RPC API interface
type EthereumApi interface {
// API identifier
Name() string
// Execute the given request and returns the response or an error
Execute(*shared.Request) (interface{}, error)
// List of supported RCP methods this API provides
Methods() []string
}
// Merge multiple API's to a single API instance
func Merge(apis ...EthereumApi) EthereumApi {
return newMergedApi(apis...)
}

View File

@ -93,6 +93,10 @@ func (self *EthApi) Execute(req *shared.Request) (interface{}, error) {
return nil, shared.NewNotImplementedError(req.Method)
}
func (self *EthApi) Name() string {
return EthApiName
}
func (self *EthApi) Accounts(req *shared.Request) (interface{}, error) {
return self.xeth.Accounts(), nil
}

56
rpc/api/mergedapi.go Normal file
View File

@ -0,0 +1,56 @@
package api
import "github.com/ethereum/go-ethereum/rpc/shared"
// combines multiple API's
type mergedApi struct {
apis []string
methods map[string]EthereumApi
}
// create new merged api instance
func newMergedApi(apis ...EthereumApi) *mergedApi {
mergedApi := new(mergedApi)
mergedApi.apis = make([]string, len(apis))
mergedApi.methods = make(map[string]EthereumApi)
for i, api := range apis {
mergedApi.apis[i] = api.Name()
for _, method := range api.Methods() {
mergedApi.methods[method] = api
}
}
return mergedApi
}
// Supported RPC methods
func (self *mergedApi) Methods() []string {
all := make([]string, len(self.methods))
for method, _ := range self.methods {
all = append(all, method)
}
return all
}
// Call the correct API's Execute method for the given request
func (self *mergedApi) Execute(req *shared.Request) (interface{}, error) {
if res, _ := self.handle(req); res != nil {
return res, nil
}
if api, found := self.methods[req.Method]; found {
return api.Execute(req)
}
return nil, shared.NewNotImplementedError(req.Method)
}
func (self *mergedApi) Name() string {
return MergedApiName
}
func (self *mergedApi) handle(req *shared.Request) (interface{}, error) {
if req.Method == "support_apis" { // provided API's
return self.apis, nil
}
return nil, nil
}

1
rpc/api/mergedapi_js.go Normal file
View File

@ -0,0 +1 @@
package api

View File

@ -8,11 +8,6 @@ import (
"github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/rpc/codec"
"github.com/ethereum/go-ethereum/xeth"
"github.com/ethereum/go-ethereum/rpc/shared"
)
const (
EthApiName = "eth"
)
// Parse a comma separated API string to individual api's
@ -28,6 +23,8 @@ func ParseApiString(apistr string, codec codec.Codec, xeth *xeth.XEth, eth *eth.
switch strings.ToLower(strings.TrimSpace(name)) {
case EthApiName:
apis[i] = NewEthApi(xeth, codec)
case Web3ApiName:
apis[i] = NewWeb3(xeth, codec)
default:
return nil, fmt.Errorf("Unknown API '%s'", name)
}
@ -35,43 +32,3 @@ func ParseApiString(apistr string, codec codec.Codec, xeth *xeth.XEth, eth *eth.
return apis, nil
}
// combines multiple API's
type mergedApi struct {
apis map[string]EthereumApi
}
// create new merged api instance
func newMergedApi(apis ...EthereumApi) *mergedApi {
mergedApi := new(mergedApi)
mergedApi.apis = make(map[string]EthereumApi)
for _, api := range apis {
for _, method := range api.Methods() {
mergedApi.apis[method] = api
}
}
return mergedApi
}
// Supported RPC methods
func (self *mergedApi) Methods() []string {
all := make([]string, len(self.apis))
for method, _ := range self.apis {
all = append(all, method)
}
return all
}
// Call the correct API's Execute method for the given request
func (self *mergedApi) Execute(req *shared.Request) (interface{}, error) {
if api, found := self.apis[req.Method]; found {
return api.Execute(req)
}
return nil, shared.NewNotImplementedError(req.Method)
}
// Merge multiple API's to a single API instance
func Merge(apis ...EthereumApi) EthereumApi {
return newMergedApi(apis...)
}

84
rpc/api/web3.go Normal file
View File

@ -0,0 +1,84 @@
package api
import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/rpc/codec"
"github.com/ethereum/go-ethereum/rpc/shared"
"github.com/ethereum/go-ethereum/xeth"
)
const (
Web3Version = "1.0.0"
)
var (
// mapping between methods and handlers
Web3Mapping = map[string]web3handler{
"web3_sha3": (*web3).Sha3,
"web3_clientVersion": (*web3).ClientVersion,
}
)
// web3 callback handler
type web3handler func(*web3, *shared.Request) (interface{}, error)
// web3 api provider
type web3 struct {
xeth *xeth.XEth
methods map[string]web3handler
codec codec.ApiCoder
}
// create a new web3 api instance
func NewWeb3(xeth *xeth.XEth, coder codec.Codec) *web3 {
return &web3{
xeth: xeth,
methods: Web3Mapping,
codec: coder.New(nil),
}
}
// collection with supported methods
func (self *web3) Methods() []string {
methods := make([]string, len(self.methods))
i := 0
for k := range self.methods {
methods[i] = k
i++
}
return methods
}
// Execute given request
func (self *web3) Execute(req *shared.Request) (interface{}, error) {
if callback, ok := self.methods[req.Method]; ok {
return callback(self, req)
}
return nil, &shared.NotImplementedError{req.Method}
}
func (self *web3) Name() string {
return Web3ApiName
}
// Version of the API this instance provides
func (self *web3) Version() string {
return Web3Version
}
// Calculates the sha3 over req.Params.Data
func (self *web3) Sha3(req *shared.Request) (interface{}, error) {
args := new(Sha3Args)
if err := self.codec.Decode(req.Params, &args); err != nil {
return nil, err
}
return common.ToHex(crypto.Sha3(common.FromHex(args.Data))), nil
}
// returns the xeth client vrsion
func (self *web3) ClientVersion(req *shared.Request) (interface{}, error) {
return self.xeth.ClientVersion(), nil
}

5
rpc/api/web3_args.go Normal file
View File

@ -0,0 +1,5 @@
package api
type Sha3Args struct {
Data string
}