diff options
-rw-r--r-- | rpc/http.go | 14 | ||||
-rw-r--r-- | rpc/jeth.go | 43 | ||||
-rw-r--r-- | rpc/messages.go | 12 |
3 files changed, 56 insertions, 13 deletions
diff --git a/rpc/http.go b/rpc/http.go index 8dcd55ad1..b6edb7cd7 100644 --- a/rpc/http.go +++ b/rpc/http.go @@ -26,7 +26,7 @@ func JSONRPC(pipe *xeth.XEth, dataDir string) http.Handler { if req.ContentLength > maxSizeReqLength { jsonerr := &RpcErrorObject{-32700, "Request too large"} - json.Send(w, &RpcErrorResponse{JsonRpc: jsonrpcver, ID: nil, Error: jsonerr}) + json.Send(w, &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: nil, Error: jsonerr}) return } @@ -36,11 +36,11 @@ func JSONRPC(pipe *xeth.XEth, dataDir string) http.Handler { break case *DecodeParamError, *InsufficientParamsError, *ValidationError: jsonerr := &RpcErrorObject{-32602, reqerr.Error()} - json.Send(w, &RpcErrorResponse{JsonRpc: jsonrpcver, ID: nil, Error: jsonerr}) + json.Send(w, &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: nil, Error: jsonerr}) return default: jsonerr := &RpcErrorObject{-32700, "Could not parse request"} - json.Send(w, &RpcErrorResponse{JsonRpc: jsonrpcver, ID: nil, Error: jsonerr}) + json.Send(w, &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: nil, Error: jsonerr}) return } @@ -51,19 +51,19 @@ func JSONRPC(pipe *xeth.XEth, dataDir string) http.Handler { break case *NotImplementedError: jsonerr := &RpcErrorObject{-32601, reserr.Error()} - json.Send(w, &RpcErrorResponse{JsonRpc: jsonrpcver, ID: reqParsed.ID, Error: jsonerr}) + json.Send(w, &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: reqParsed.Id, Error: jsonerr}) return case *DecodeParamError, *InsufficientParamsError, *ValidationError: jsonerr := &RpcErrorObject{-32602, reserr.Error()} - json.Send(w, &RpcErrorResponse{JsonRpc: jsonrpcver, ID: reqParsed.ID, Error: jsonerr}) + json.Send(w, &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: reqParsed.Id, Error: jsonerr}) return default: jsonerr := &RpcErrorObject{-32603, reserr.Error()} - json.Send(w, &RpcErrorResponse{JsonRpc: jsonrpcver, ID: reqParsed.ID, Error: jsonerr}) + json.Send(w, &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: reqParsed.Id, Error: jsonerr}) return } rpchttplogger.DebugDetailf("Generated response: %T %s", response, response) - json.Send(w, &RpcSuccessResponse{JsonRpc: jsonrpcver, ID: reqParsed.ID, Result: response}) + json.Send(w, &RpcSuccessResponse{Jsonrpc: jsonrpcver, Id: reqParsed.Id, Result: response}) }) } diff --git a/rpc/jeth.go b/rpc/jeth.go new file mode 100644 index 000000000..11d4599c9 --- /dev/null +++ b/rpc/jeth.go @@ -0,0 +1,43 @@ +package rpc + +import ( + "encoding/json" + // "fmt" + "github.com/obscuren/otto" +) + +type Jeth struct { + ethApi *EthereumApi + toVal func(interface{}) otto.Value +} + +func NewJeth(ethApi *EthereumApi, toVal func(interface{}) otto.Value) *Jeth { + return &Jeth{ethApi, toVal} +} + +func (self *Jeth) err(code int, msg string, id interface{}) otto.Value { + rpcerr := &RpcErrorObject{code, msg} + rpcresponse := &RpcErrorResponse{Jsonrpc: jsonrpcver, Id: id, Error: rpcerr} + return self.toVal(rpcresponse) +} + +func (self *Jeth) Send(call otto.FunctionCall) (response otto.Value) { + reqif, err := call.Argument(0).Export() + if err != nil { + return self.err(-32700, err.Error(), nil) + } + + jsonreq, err := json.Marshal(reqif) + + var req RpcRequest + err = json.Unmarshal(jsonreq, &req) + + var respif interface{} + err = self.ethApi.GetRequestReply(&req, &respif) + if err != nil { + return self.err(-32603, err.Error(), req.Id) + } + rpcresponse := &RpcSuccessResponse{Jsonrpc: jsonrpcver, Id: req.Id, Result: respif} + response = self.toVal(rpcresponse) + return +} diff --git a/rpc/messages.go b/rpc/messages.go index 781394196..7f5ebab11 100644 --- a/rpc/messages.go +++ b/rpc/messages.go @@ -83,21 +83,21 @@ func NewValidationError(param string, msg string) error { } type RpcRequest struct { - ID interface{} `json:"id"` - JsonRpc string `json:"jsonrpc"` + Id interface{} `json:"id"` + Jsonrpc string `json:"jsonrpc"` Method string `json:"method"` Params json.RawMessage `json:"params"` } type RpcSuccessResponse struct { - ID interface{} `json:"id"` - JsonRpc string `json:"jsonrpc"` + Id interface{} `json:"id"` + Jsonrpc string `json:"jsonrpc"` Result interface{} `json:"result"` } type RpcErrorResponse struct { - ID interface{} `json:"id"` - JsonRpc string `json:"jsonrpc"` + Id interface{} `json:"id"` + Jsonrpc string `json:"jsonrpc"` Error *RpcErrorObject `json:"error"` } |