jeth.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum 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. // go-ethereum 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 go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package rpc
  17. import (
  18. "encoding/json"
  19. "fmt"
  20. "github.com/ethereum/go-ethereum/jsre"
  21. "github.com/ethereum/go-ethereum/rpc/comms"
  22. "github.com/ethereum/go-ethereum/rpc/shared"
  23. "github.com/robertkrimen/otto"
  24. )
  25. type Jeth struct {
  26. ethApi shared.EthereumApi
  27. re *jsre.JSRE
  28. client comms.EthereumClient
  29. }
  30. func NewJeth(ethApi shared.EthereumApi, re *jsre.JSRE, client comms.EthereumClient) *Jeth {
  31. return &Jeth{ethApi, re, client}
  32. }
  33. func (self *Jeth) err(call otto.FunctionCall, code int, msg string, id interface{}) (response otto.Value) {
  34. errObj := fmt.Sprintf("{\"message\": \"%s\", \"code\": %d}", msg, code)
  35. retResponse := fmt.Sprintf("ret_response = JSON.parse('{\"jsonrpc\": \"%s\", \"id\": %v, \"error\": %s}');", shared.JsonRpcVersion, id, errObj)
  36. call.Otto.Run("ret_error = " + errObj)
  37. res, _ := call.Otto.Run(retResponse)
  38. return res
  39. }
  40. func (self *Jeth) Send(call otto.FunctionCall) (response otto.Value) {
  41. reqif, err := call.Argument(0).Export()
  42. if err != nil {
  43. return self.err(call, -32700, err.Error(), nil)
  44. }
  45. jsonreq, err := json.Marshal(reqif)
  46. var reqs []shared.Request
  47. batch := true
  48. err = json.Unmarshal(jsonreq, &reqs)
  49. if err != nil {
  50. reqs = make([]shared.Request, 1)
  51. err = json.Unmarshal(jsonreq, &reqs[0])
  52. batch = false
  53. }
  54. call.Otto.Set("response_len", len(reqs))
  55. call.Otto.Run("var ret_response = new Array(response_len);")
  56. for i, req := range reqs {
  57. var respif interface{}
  58. err := self.client.Send(&req)
  59. if err != nil {
  60. return self.err(call, -32603, err.Error(), req.Id)
  61. }
  62. respif, err = self.client.Recv()
  63. if err != nil {
  64. return self.err(call, -32603, err.Error(), req.Id)
  65. }
  66. call.Otto.Set("ret_jsonrpc", shared.JsonRpcVersion)
  67. call.Otto.Set("ret_id", req.Id)
  68. res, _ := json.Marshal(respif)
  69. call.Otto.Set("ret_result", string(res))
  70. call.Otto.Set("response_idx", i)
  71. response, err = call.Otto.Run(`
  72. ret_response[response_idx] = { jsonrpc: ret_jsonrpc, id: ret_id, result: JSON.parse(ret_result) };
  73. `)
  74. }
  75. if !batch {
  76. call.Otto.Run("ret_response = ret_response[0];")
  77. }
  78. if call.Argument(1).IsObject() {
  79. call.Otto.Set("callback", call.Argument(1))
  80. call.Otto.Run(`
  81. if (Object.prototype.toString.call(callback) == '[object Function]') {
  82. callback(null, ret_response);
  83. }
  84. `)
  85. }
  86. return
  87. }