aboutsummaryrefslogtreecommitdiffstats
path: root/rpc/api
diff options
context:
space:
mode:
Diffstat (limited to 'rpc/api')
-rw-r--r--rpc/api/api.go14
-rw-r--r--rpc/api/eth.go4
-rw-r--r--rpc/api/mergedapi.go56
-rw-r--r--rpc/api/mergedapi_js.go1
-rw-r--r--rpc/api/utils.go47
-rw-r--r--rpc/api/web3.go84
-rw-r--r--rpc/api/web3_args.go5
7 files changed, 165 insertions, 46 deletions
diff --git a/rpc/api/api.go b/rpc/api/api.go
index 93dc3058c..153c73f48 100644
--- a/rpc/api/api.go
+++ b/rpc/api/api.go
@@ -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...)
+}
diff --git a/rpc/api/eth.go b/rpc/api/eth.go
index fa14aa41e..0a8cecdbc 100644
--- a/rpc/api/eth.go
+++ b/rpc/api/eth.go
@@ -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
}
diff --git a/rpc/api/mergedapi.go b/rpc/api/mergedapi.go
new file mode 100644
index 000000000..7784661d7
--- /dev/null
+++ b/rpc/api/mergedapi.go
@@ -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
+}
diff --git a/rpc/api/mergedapi_js.go b/rpc/api/mergedapi_js.go
new file mode 100644
index 000000000..778f64ec1
--- /dev/null
+++ b/rpc/api/mergedapi_js.go
@@ -0,0 +1 @@
+package api
diff --git a/rpc/api/utils.go b/rpc/api/utils.go
index a62058140..7024365e4 100644
--- a/rpc/api/utils.go
+++ b/rpc/api/utils.go
@@ -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...)
-}
diff --git a/rpc/api/web3.go b/rpc/api/web3.go
new file mode 100644
index 000000000..4c51c4a97
--- /dev/null
+++ b/rpc/api/web3.go
@@ -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
+}
diff --git a/rpc/api/web3_args.go b/rpc/api/web3_args.go
new file mode 100644
index 000000000..5455a6c8e
--- /dev/null
+++ b/rpc/api/web3_args.go
@@ -0,0 +1,5 @@
+package api
+
+type Sha3Args struct {
+ Data string
+}