mirror of https://github.com/ethereum/go-ethereum
parent
2a0d888326
commit
a1a475fb92
File diff suppressed because it is too large
Load Diff
@ -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 |
||||
} |
@ -0,0 +1 @@ |
||||
package api |
@ -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 |
||||
} |
@ -0,0 +1,5 @@ |
||||
package api |
||||
|
||||
type Sha3Args struct { |
||||
Data string |
||||
} |
Loading…
Reference in new issue