github.com/jeffallen/go-ethereum@v1.1.4-0.20150910155051-571d3236c49c/rpc/api/web3.go (about) 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 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 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 // GNU Lesser General Public License for more details. 13 // 14 // 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 package api 18 19 import ( 20 "github.com/ethereum/go-ethereum/common" 21 "github.com/ethereum/go-ethereum/crypto" 22 "github.com/ethereum/go-ethereum/rpc/codec" 23 "github.com/ethereum/go-ethereum/rpc/shared" 24 "github.com/ethereum/go-ethereum/xeth" 25 ) 26 27 const ( 28 Web3ApiVersion = "1.0" 29 ) 30 31 var ( 32 // mapping between methods and handlers 33 Web3Mapping = map[string]web3handler{ 34 "web3_sha3": (*web3Api).Sha3, 35 "web3_clientVersion": (*web3Api).ClientVersion, 36 } 37 ) 38 39 // web3 callback handler 40 type web3handler func(*web3Api, *shared.Request) (interface{}, error) 41 42 // web3 api provider 43 type web3Api struct { 44 xeth *xeth.XEth 45 methods map[string]web3handler 46 codec codec.ApiCoder 47 } 48 49 // create a new web3 api instance 50 func NewWeb3Api(xeth *xeth.XEth, coder codec.Codec) *web3Api { 51 return &web3Api{ 52 xeth: xeth, 53 methods: Web3Mapping, 54 codec: coder.New(nil), 55 } 56 } 57 58 // collection with supported methods 59 func (self *web3Api) Methods() []string { 60 methods := make([]string, len(self.methods)) 61 i := 0 62 for k := range self.methods { 63 methods[i] = k 64 i++ 65 } 66 return methods 67 } 68 69 // Execute given request 70 func (self *web3Api) Execute(req *shared.Request) (interface{}, error) { 71 if callback, ok := self.methods[req.Method]; ok { 72 return callback(self, req) 73 } 74 75 return nil, &shared.NotImplementedError{req.Method} 76 } 77 78 func (self *web3Api) Name() string { 79 return shared.Web3ApiName 80 } 81 82 func (self *web3Api) ApiVersion() string { 83 return Web3ApiVersion 84 } 85 86 // Calculates the sha3 over req.Params.Data 87 func (self *web3Api) Sha3(req *shared.Request) (interface{}, error) { 88 args := new(Sha3Args) 89 if err := self.codec.Decode(req.Params, &args); err != nil { 90 return nil, err 91 } 92 93 return common.ToHex(crypto.Sha3(common.FromHex(args.Data))), nil 94 } 95 96 // returns the xeth client vrsion 97 func (self *web3Api) ClientVersion(req *shared.Request) (interface{}, error) { 98 return self.xeth.ClientVersion(), nil 99 }