web3.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. package api
  17. import (
  18. "github.com/ethereum/go-ethereum/common"
  19. "github.com/ethereum/go-ethereum/crypto"
  20. "github.com/ethereum/go-ethereum/rpc/codec"
  21. "github.com/ethereum/go-ethereum/rpc/shared"
  22. "github.com/ethereum/go-ethereum/xeth"
  23. )
  24. const (
  25. Web3ApiVersion = "1.0"
  26. )
  27. var (
  28. // mapping between methods and handlers
  29. Web3Mapping = map[string]web3handler{
  30. "web3_sha3": (*web3Api).Sha3,
  31. "web3_clientVersion": (*web3Api).ClientVersion,
  32. }
  33. )
  34. // web3 callback handler
  35. type web3handler func(*web3Api, *shared.Request) (interface{}, error)
  36. // web3 api provider
  37. type web3Api struct {
  38. xeth *xeth.XEth
  39. methods map[string]web3handler
  40. codec codec.ApiCoder
  41. }
  42. // create a new web3 api instance
  43. func NewWeb3Api(xeth *xeth.XEth, coder codec.Codec) *web3Api {
  44. return &web3Api{
  45. xeth: xeth,
  46. methods: Web3Mapping,
  47. codec: coder.New(nil),
  48. }
  49. }
  50. // collection with supported methods
  51. func (self *web3Api) Methods() []string {
  52. methods := make([]string, len(self.methods))
  53. i := 0
  54. for k := range self.methods {
  55. methods[i] = k
  56. i++
  57. }
  58. return methods
  59. }
  60. // Execute given request
  61. func (self *web3Api) Execute(req *shared.Request) (interface{}, error) {
  62. if callback, ok := self.methods[req.Method]; ok {
  63. return callback(self, req)
  64. }
  65. return nil, &shared.NotImplementedError{req.Method}
  66. }
  67. func (self *web3Api) Name() string {
  68. return shared.Web3ApiName
  69. }
  70. func (self *web3Api) ApiVersion() string {
  71. return Web3ApiVersion
  72. }
  73. // Calculates the sha3 over req.Params.Data
  74. func (self *web3Api) Sha3(req *shared.Request) (interface{}, error) {
  75. args := new(Sha3Args)
  76. if err := self.codec.Decode(req.Params, &args); err != nil {
  77. return nil, err
  78. }
  79. return common.ToHex(crypto.Sha3(common.FromHex(args.Data))), nil
  80. }
  81. // returns the xeth client vrsion
  82. func (self *web3Api) ClientVersion(req *shared.Request) (interface{}, error) {
  83. return self.xeth.ClientVersion(), nil
  84. }