From ad8678d216837a75537098c5c745b77510717683 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Wed, 1 Apr 2015 04:12:34 -0700 Subject: [PATCH] rpc: auto generated client methods using rpc-gen --- rpc/client.go | 238 ++++++++++++ rpc/client_methods.go | 822 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 1060 insertions(+) create mode 100644 rpc/client.go create mode 100644 rpc/client_methods.go diff --git a/rpc/client.go b/rpc/client.go new file mode 100644 index 000000000..7b9e55410 --- /dev/null +++ b/rpc/client.go @@ -0,0 +1,238 @@ +package rpc + +import ( + "bytes" + "encoding/hex" + "encoding/json" + "fmt" + "github.com/tendermint/tendermint2/binary" + "io/ioutil" + "net/http" + "net/url" + "reflect" + "strconv" +) + +type Response struct { + Status string + Data interface{} + Error string +} + +//go:generate rpc-gen -interface Client -pkg core -type *ClientHTTP,*ClientJSON -exclude pipe.go -out-pkg rpc + +type ClientJSON struct { + addr string +} + +type ClientHTTP struct { + addr string +} + +func NewClient(addr, typ string) Client { + switch typ { + case "HTTP": + return &ClientHTTP{addr} + case "JSONRPC": + return &ClientJSON{addr} + } + return nil +} + +func argsToJson(args ...interface{}) ([][]string, error) { + l := len(args) + jsons := make([][]string, l) + n, err := new(int64), new(error) + for i, a := range args { + //if its a slice, we serliaze separately and pack into a slice of strings + // otherwise its a slice of length 1 + if v := reflect.ValueOf(a); v.Kind() == reflect.Slice { + ty := v.Type() + rt := ty.Elem() + if rt.Kind() == reflect.Uint8 { + buf := new(bytes.Buffer) + binary.WriteJSON(a, buf, n, err) + if *err != nil { + return nil, *err + } + jsons[i] = []string{string(buf.Bytes())} + } else { + slice := make([]string, v.Len()) + for j := 0; j < v.Len(); j++ { + buf := new(bytes.Buffer) + binary.WriteJSON(v.Index(j).Interface(), buf, n, err) + if *err != nil { + return nil, *err + } + slice[j] = string(buf.Bytes()) + } + jsons[i] = slice + } + } else { + buf := new(bytes.Buffer) + binary.WriteJSON(a, buf, n, err) + if *err != nil { + return nil, *err + } + jsons[i] = []string{string(buf.Bytes())} + } + } + return jsons, nil +} + +func (c *ClientHTTP) RequestResponse(method string, values url.Values) (*Response, error) { + resp, err := http.PostForm(c.addr+method, values) + if err != nil { + return nil, err + } + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + response := new(Response) + fmt.Println(string(body)) + binary.ReadJSON(response, body, &err) + if err != nil { + return nil, err + } + fmt.Println(response.Data) + return response, nil +} + +func (c *ClientJSON) requestResponse(s JSONRPC) ([]byte, error) { + b, err := json.Marshal(s) + if err != nil { + return nil, err + } + buf := bytes.NewBuffer(b) + resp, err := http.Post(c.addr, "text/json", buf) + if err != nil { + return nil, err + } + defer resp.Body.Close() + return ioutil.ReadAll(resp.Body) +} + +/* + What follows is used by `rpc-gen` when `go generate` is called + to populate the rpc client methods +*/ + +// first we define the base interface, which rpc-gen will further populate with generated methods + +/*rpc-gen:define-interface Client +type Client interface { + Address() string // returns the remote address +} +*/ + +// encoding functions + +func binaryWriter(args ...interface{}) ([]interface{}, error) { + list := []interface{}{} + for _, a := range args { + buf, n, err := new(bytes.Buffer), new(int64), new(error) + binary.WriteJSON(a, buf, n, err) + if *err != nil { + return nil, *err + } + list = append(list, buf.Bytes()) + + } + return list, nil +} + +func argsToURLValues(argNames []string, args ...interface{}) (url.Values, error) { + values := make(url.Values) + if len(argNames) == 0 { + return values, nil + } + if len(argNames) != len(args) { + return nil, fmt.Errorf("argNames and args have different lengths: %d, %d", len(argNames), len(args)) + } + slice, err := argsToJson(args...) + if err != nil { + return nil, err + } + for i, name := range argNames { + s := slice[i] + values.Set(name, s[0]) + for i := 1; i < len(s); i++ { + values.Add(name, s[i]) + } + } + return values, nil +} + +// import statements we will need for the templates + +/*rpc-gen:imports: +github.com/tendermint/tendermint2/binary +net/http +io/ioutil +fmt +*/ + +// Template functions to be filled in + +/*rpc-gen:template:*ClientJSON func (c *ClientJSON) {{name}}({{args.def}}) ({{response}}) { + params, err := binaryWriter({{args.ident}}) + if err != nil{ + return nil, err + } + s := JSONRPC{ + JSONRPC: "2.0", + Method: {{lowername}}, + Params: params, + Id: 0, + } + body, err := c.requestResponse(s) + if err != nil{ + return nil, err + } + var response struct { + Result {{response.0}} `json:"result"` + Error string `json:"error"` + Id string `json:"id"` + JSONRPC string `json:"jsonrpc"` + } + binary.ReadJSON(&response, body, &err) + if err != nil { + return nil, err + } + if response.Error != ""{ + return nil, fmt.Errorf(response.Error) + } + return response.Result, nil +}*/ + +/*rpc-gen:template:*ClientHTTP func (c *ClientHTTP) {{name}}({{args.def}}) ({{response}}){ + values, err := argsToURLValues({{args.name}}, {{args.ident}}) + if err != nil{ + return nil, err + } + resp, err := http.PostForm(c.addr+{{lowername}}, values) + if err != nil { + return nil, err + } + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + var response struct { + Result {{response.0}} `json:"result"` + Error string `json:"error"` + Id string `json:"id"` + JSONRPC string `json:"jsonrpc"` + } + binary.ReadJSON(&response, body, &err) + if err != nil { + return nil, err + } + if response.Error != ""{ + return nil, fmt.Errorf(response.Error) + } + return response.Result, nil +}*/ diff --git a/rpc/client_methods.go b/rpc/client_methods.go new file mode 100644 index 000000000..95c0c32ab --- /dev/null +++ b/rpc/client_methods.go @@ -0,0 +1,822 @@ +// File generated by github.com/ebuchman/rpc-gen + +package rpc + +import ( + "fmt" + "github.com/tendermint/tendermint2/account" + "github.com/tendermint/tendermint2/binary" + "github.com/tendermint/tendermint2/rpc/core" + "github.com/tendermint/tendermint2/types" + "io/ioutil" + "net/http" +) + +type Client interface { + Status() (*core.ResponseStatus, error) + DumpStorage(addr []byte) (*core.ResponseDumpStorage, error) + ListAccounts() (*core.ResponseListAccounts, error) + BlockchainInfo(minHeight uint) (*core.ResponseBlockchainInfo, error) + GetBlock(height uint) (*core.ResponseGetBlock, error) + BroadcastTx(tx types.Tx) (*core.ResponseBroadcastTx, error) + NetInfo() (*core.ResponseNetInfo, error) + Call(address []byte) (*core.ResponseCall, error) + SignTx(tx types.Tx, privAccounts []*account.PrivAccount) (*core.ResponseSignTx, error) + ListValidators() (*core.ResponseListValidators, error) + GenPrivAccount() (*core.ResponseGenPrivAccount, error) + GetAccount(address []byte) (*core.ResponseGetAccount, error) + GetStorage(address []byte) (*core.ResponseGetStorage, error) +} + +func (c *ClientHTTP) Status() (*core.ResponseStatus, error) { + values, err := argsToURLValues(nil, nil) + if err != nil { + return nil, err + } + resp, err := http.PostForm(c.addr+"status", values) + if err != nil { + return nil, err + } + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + var response struct { + Result *core.ResponseStatus `json:"result"` + Error string `json:"error"` + Id string `json:"id"` + JSONRPC string `json:"jsonrpc"` + } + binary.ReadJSON(&response, body, &err) + if err != nil { + return nil, err + } + if response.Error != "" { + return nil, fmt.Errorf(response.Error) + } + return response.Result, nil +} + +func (c *ClientHTTP) DumpStorage(addr []byte) (*core.ResponseDumpStorage, error) { + values, err := argsToURLValues([]string{"addr"}, addr) + if err != nil { + return nil, err + } + resp, err := http.PostForm(c.addr+"dump_storage", values) + if err != nil { + return nil, err + } + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + var response struct { + Result *core.ResponseDumpStorage `json:"result"` + Error string `json:"error"` + Id string `json:"id"` + JSONRPC string `json:"jsonrpc"` + } + binary.ReadJSON(&response, body, &err) + if err != nil { + return nil, err + } + if response.Error != "" { + return nil, fmt.Errorf(response.Error) + } + return response.Result, nil +} + +func (c *ClientHTTP) ListAccounts() (*core.ResponseListAccounts, error) { + values, err := argsToURLValues(nil, nil) + if err != nil { + return nil, err + } + resp, err := http.PostForm(c.addr+"list_accounts", values) + if err != nil { + return nil, err + } + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + var response struct { + Result *core.ResponseListAccounts `json:"result"` + Error string `json:"error"` + Id string `json:"id"` + JSONRPC string `json:"jsonrpc"` + } + binary.ReadJSON(&response, body, &err) + if err != nil { + return nil, err + } + if response.Error != "" { + return nil, fmt.Errorf(response.Error) + } + return response.Result, nil +} + +func (c *ClientHTTP) BlockchainInfo(minHeight uint) (*core.ResponseBlockchainInfo, error) { + values, err := argsToURLValues([]string{"minHeight"}, minHeight) + if err != nil { + return nil, err + } + resp, err := http.PostForm(c.addr+"blockchain_info", values) + if err != nil { + return nil, err + } + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + var response struct { + Result *core.ResponseBlockchainInfo `json:"result"` + Error string `json:"error"` + Id string `json:"id"` + JSONRPC string `json:"jsonrpc"` + } + binary.ReadJSON(&response, body, &err) + if err != nil { + return nil, err + } + if response.Error != "" { + return nil, fmt.Errorf(response.Error) + } + return response.Result, nil +} + +func (c *ClientHTTP) GetBlock(height uint) (*core.ResponseGetBlock, error) { + values, err := argsToURLValues([]string{"height"}, height) + if err != nil { + return nil, err + } + resp, err := http.PostForm(c.addr+"get_block", values) + if err != nil { + return nil, err + } + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + var response struct { + Result *core.ResponseGetBlock `json:"result"` + Error string `json:"error"` + Id string `json:"id"` + JSONRPC string `json:"jsonrpc"` + } + binary.ReadJSON(&response, body, &err) + if err != nil { + return nil, err + } + if response.Error != "" { + return nil, fmt.Errorf(response.Error) + } + return response.Result, nil +} + +func (c *ClientHTTP) BroadcastTx(tx types.Tx) (*core.ResponseBroadcastTx, error) { + values, err := argsToURLValues([]string{"tx"}, tx) + if err != nil { + return nil, err + } + resp, err := http.PostForm(c.addr+"broadcast_tx", values) + if err != nil { + return nil, err + } + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + var response struct { + Result *core.ResponseBroadcastTx `json:"result"` + Error string `json:"error"` + Id string `json:"id"` + JSONRPC string `json:"jsonrpc"` + } + binary.ReadJSON(&response, body, &err) + if err != nil { + return nil, err + } + if response.Error != "" { + return nil, fmt.Errorf(response.Error) + } + return response.Result, nil +} + +func (c *ClientHTTP) NetInfo() (*core.ResponseNetInfo, error) { + values, err := argsToURLValues(nil, nil) + if err != nil { + return nil, err + } + resp, err := http.PostForm(c.addr+"net_info", values) + if err != nil { + return nil, err + } + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + var response struct { + Result *core.ResponseNetInfo `json:"result"` + Error string `json:"error"` + Id string `json:"id"` + JSONRPC string `json:"jsonrpc"` + } + binary.ReadJSON(&response, body, &err) + if err != nil { + return nil, err + } + if response.Error != "" { + return nil, fmt.Errorf(response.Error) + } + return response.Result, nil +} + +func (c *ClientHTTP) Call(address []byte) (*core.ResponseCall, error) { + values, err := argsToURLValues([]string{"address"}, address) + if err != nil { + return nil, err + } + resp, err := http.PostForm(c.addr+"call", values) + if err != nil { + return nil, err + } + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + var response struct { + Result *core.ResponseCall `json:"result"` + Error string `json:"error"` + Id string `json:"id"` + JSONRPC string `json:"jsonrpc"` + } + binary.ReadJSON(&response, body, &err) + if err != nil { + return nil, err + } + if response.Error != "" { + return nil, fmt.Errorf(response.Error) + } + return response.Result, nil +} + +func (c *ClientHTTP) SignTx(tx types.Tx, privAccounts []*account.PrivAccount) (*core.ResponseSignTx, error) { + values, err := argsToURLValues([]string{"tx", "privAccounts"}, tx, privAccounts) + if err != nil { + return nil, err + } + resp, err := http.PostForm(c.addr+"sign_tx", values) + if err != nil { + return nil, err + } + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + var response struct { + Result *core.ResponseSignTx `json:"result"` + Error string `json:"error"` + Id string `json:"id"` + JSONRPC string `json:"jsonrpc"` + } + binary.ReadJSON(&response, body, &err) + if err != nil { + return nil, err + } + if response.Error != "" { + return nil, fmt.Errorf(response.Error) + } + return response.Result, nil +} + +func (c *ClientHTTP) ListValidators() (*core.ResponseListValidators, error) { + values, err := argsToURLValues(nil, nil) + if err != nil { + return nil, err + } + resp, err := http.PostForm(c.addr+"list_validators", values) + if err != nil { + return nil, err + } + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + var response struct { + Result *core.ResponseListValidators `json:"result"` + Error string `json:"error"` + Id string `json:"id"` + JSONRPC string `json:"jsonrpc"` + } + binary.ReadJSON(&response, body, &err) + if err != nil { + return nil, err + } + if response.Error != "" { + return nil, fmt.Errorf(response.Error) + } + return response.Result, nil +} + +func (c *ClientHTTP) GenPrivAccount() (*core.ResponseGenPrivAccount, error) { + values, err := argsToURLValues(nil, nil) + if err != nil { + return nil, err + } + resp, err := http.PostForm(c.addr+"gen_priv_account", values) + if err != nil { + return nil, err + } + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + var response struct { + Result *core.ResponseGenPrivAccount `json:"result"` + Error string `json:"error"` + Id string `json:"id"` + JSONRPC string `json:"jsonrpc"` + } + binary.ReadJSON(&response, body, &err) + if err != nil { + return nil, err + } + if response.Error != "" { + return nil, fmt.Errorf(response.Error) + } + return response.Result, nil +} + +func (c *ClientHTTP) GetAccount(address []byte) (*core.ResponseGetAccount, error) { + values, err := argsToURLValues([]string{"address"}, address) + if err != nil { + return nil, err + } + resp, err := http.PostForm(c.addr+"get_account", values) + if err != nil { + return nil, err + } + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + var response struct { + Result *core.ResponseGetAccount `json:"result"` + Error string `json:"error"` + Id string `json:"id"` + JSONRPC string `json:"jsonrpc"` + } + binary.ReadJSON(&response, body, &err) + if err != nil { + return nil, err + } + if response.Error != "" { + return nil, fmt.Errorf(response.Error) + } + return response.Result, nil +} + +func (c *ClientHTTP) GetStorage(address []byte) (*core.ResponseGetStorage, error) { + values, err := argsToURLValues([]string{"address"}, address) + if err != nil { + return nil, err + } + resp, err := http.PostForm(c.addr+"get_storage", values) + if err != nil { + return nil, err + } + defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + var response struct { + Result *core.ResponseGetStorage `json:"result"` + Error string `json:"error"` + Id string `json:"id"` + JSONRPC string `json:"jsonrpc"` + } + binary.ReadJSON(&response, body, &err) + if err != nil { + return nil, err + } + if response.Error != "" { + return nil, fmt.Errorf(response.Error) + } + return response.Result, nil +} + +func (c *ClientJSON) Status() (*core.ResponseStatus, error) { + params, err := binaryWriter(nil) + if err != nil { + return nil, err + } + s := JSONRPC{ + JSONRPC: "2.0", + Method: "status", + Params: params, + Id: 0, + } + body, err := c.requestResponse(s) + if err != nil { + return nil, err + } + var response struct { + Result *core.ResponseStatus `json:"result"` + Error string `json:"error"` + Id string `json:"id"` + JSONRPC string `json:"jsonrpc"` + } + binary.ReadJSON(&response, body, &err) + if err != nil { + return nil, err + } + if response.Error != "" { + return nil, fmt.Errorf(response.Error) + } + return response.Result, nil +} + +func (c *ClientJSON) DumpStorage(addr []byte) (*core.ResponseDumpStorage, error) { + params, err := binaryWriter(addr) + if err != nil { + return nil, err + } + s := JSONRPC{ + JSONRPC: "2.0", + Method: "dump_storage", + Params: params, + Id: 0, + } + body, err := c.requestResponse(s) + if err != nil { + return nil, err + } + var response struct { + Result *core.ResponseDumpStorage `json:"result"` + Error string `json:"error"` + Id string `json:"id"` + JSONRPC string `json:"jsonrpc"` + } + binary.ReadJSON(&response, body, &err) + if err != nil { + return nil, err + } + if response.Error != "" { + return nil, fmt.Errorf(response.Error) + } + return response.Result, nil +} + +func (c *ClientJSON) ListAccounts() (*core.ResponseListAccounts, error) { + params, err := binaryWriter(nil) + if err != nil { + return nil, err + } + s := JSONRPC{ + JSONRPC: "2.0", + Method: "list_accounts", + Params: params, + Id: 0, + } + body, err := c.requestResponse(s) + if err != nil { + return nil, err + } + var response struct { + Result *core.ResponseListAccounts `json:"result"` + Error string `json:"error"` + Id string `json:"id"` + JSONRPC string `json:"jsonrpc"` + } + binary.ReadJSON(&response, body, &err) + if err != nil { + return nil, err + } + if response.Error != "" { + return nil, fmt.Errorf(response.Error) + } + return response.Result, nil +} + +func (c *ClientJSON) BlockchainInfo(minHeight uint) (*core.ResponseBlockchainInfo, error) { + params, err := binaryWriter(minHeight) + if err != nil { + return nil, err + } + s := JSONRPC{ + JSONRPC: "2.0", + Method: "blockchain_info", + Params: params, + Id: 0, + } + body, err := c.requestResponse(s) + if err != nil { + return nil, err + } + var response struct { + Result *core.ResponseBlockchainInfo `json:"result"` + Error string `json:"error"` + Id string `json:"id"` + JSONRPC string `json:"jsonrpc"` + } + binary.ReadJSON(&response, body, &err) + if err != nil { + return nil, err + } + if response.Error != "" { + return nil, fmt.Errorf(response.Error) + } + return response.Result, nil +} + +func (c *ClientJSON) GetBlock(height uint) (*core.ResponseGetBlock, error) { + params, err := binaryWriter(height) + if err != nil { + return nil, err + } + s := JSONRPC{ + JSONRPC: "2.0", + Method: "get_block", + Params: params, + Id: 0, + } + body, err := c.requestResponse(s) + if err != nil { + return nil, err + } + var response struct { + Result *core.ResponseGetBlock `json:"result"` + Error string `json:"error"` + Id string `json:"id"` + JSONRPC string `json:"jsonrpc"` + } + binary.ReadJSON(&response, body, &err) + if err != nil { + return nil, err + } + if response.Error != "" { + return nil, fmt.Errorf(response.Error) + } + return response.Result, nil +} + +func (c *ClientJSON) BroadcastTx(tx types.Tx) (*core.ResponseBroadcastTx, error) { + params, err := binaryWriter(tx) + if err != nil { + return nil, err + } + s := JSONRPC{ + JSONRPC: "2.0", + Method: "broadcast_tx", + Params: params, + Id: 0, + } + body, err := c.requestResponse(s) + if err != nil { + return nil, err + } + var response struct { + Result *core.ResponseBroadcastTx `json:"result"` + Error string `json:"error"` + Id string `json:"id"` + JSONRPC string `json:"jsonrpc"` + } + binary.ReadJSON(&response, body, &err) + if err != nil { + return nil, err + } + if response.Error != "" { + return nil, fmt.Errorf(response.Error) + } + return response.Result, nil +} + +func (c *ClientJSON) NetInfo() (*core.ResponseNetInfo, error) { + params, err := binaryWriter(nil) + if err != nil { + return nil, err + } + s := JSONRPC{ + JSONRPC: "2.0", + Method: "net_info", + Params: params, + Id: 0, + } + body, err := c.requestResponse(s) + if err != nil { + return nil, err + } + var response struct { + Result *core.ResponseNetInfo `json:"result"` + Error string `json:"error"` + Id string `json:"id"` + JSONRPC string `json:"jsonrpc"` + } + binary.ReadJSON(&response, body, &err) + if err != nil { + return nil, err + } + if response.Error != "" { + return nil, fmt.Errorf(response.Error) + } + return response.Result, nil +} + +func (c *ClientJSON) Call(address []byte) (*core.ResponseCall, error) { + params, err := binaryWriter(address) + if err != nil { + return nil, err + } + s := JSONRPC{ + JSONRPC: "2.0", + Method: "call", + Params: params, + Id: 0, + } + body, err := c.requestResponse(s) + if err != nil { + return nil, err + } + var response struct { + Result *core.ResponseCall `json:"result"` + Error string `json:"error"` + Id string `json:"id"` + JSONRPC string `json:"jsonrpc"` + } + binary.ReadJSON(&response, body, &err) + if err != nil { + return nil, err + } + if response.Error != "" { + return nil, fmt.Errorf(response.Error) + } + return response.Result, nil +} + +func (c *ClientJSON) SignTx(tx types.Tx, privAccounts []*account.PrivAccount) (*core.ResponseSignTx, error) { + params, err := binaryWriter(tx, privAccounts) + if err != nil { + return nil, err + } + s := JSONRPC{ + JSONRPC: "2.0", + Method: "sign_tx", + Params: params, + Id: 0, + } + body, err := c.requestResponse(s) + if err != nil { + return nil, err + } + var response struct { + Result *core.ResponseSignTx `json:"result"` + Error string `json:"error"` + Id string `json:"id"` + JSONRPC string `json:"jsonrpc"` + } + binary.ReadJSON(&response, body, &err) + if err != nil { + return nil, err + } + if response.Error != "" { + return nil, fmt.Errorf(response.Error) + } + return response.Result, nil +} + +func (c *ClientJSON) ListValidators() (*core.ResponseListValidators, error) { + params, err := binaryWriter(nil) + if err != nil { + return nil, err + } + s := JSONRPC{ + JSONRPC: "2.0", + Method: "list_validators", + Params: params, + Id: 0, + } + body, err := c.requestResponse(s) + if err != nil { + return nil, err + } + var response struct { + Result *core.ResponseListValidators `json:"result"` + Error string `json:"error"` + Id string `json:"id"` + JSONRPC string `json:"jsonrpc"` + } + binary.ReadJSON(&response, body, &err) + if err != nil { + return nil, err + } + if response.Error != "" { + return nil, fmt.Errorf(response.Error) + } + return response.Result, nil +} + +func (c *ClientJSON) GenPrivAccount() (*core.ResponseGenPrivAccount, error) { + params, err := binaryWriter(nil) + if err != nil { + return nil, err + } + s := JSONRPC{ + JSONRPC: "2.0", + Method: "gen_priv_account", + Params: params, + Id: 0, + } + body, err := c.requestResponse(s) + if err != nil { + return nil, err + } + var response struct { + Result *core.ResponseGenPrivAccount `json:"result"` + Error string `json:"error"` + Id string `json:"id"` + JSONRPC string `json:"jsonrpc"` + } + binary.ReadJSON(&response, body, &err) + if err != nil { + return nil, err + } + if response.Error != "" { + return nil, fmt.Errorf(response.Error) + } + return response.Result, nil +} + +func (c *ClientJSON) GetAccount(address []byte) (*core.ResponseGetAccount, error) { + params, err := binaryWriter(address) + if err != nil { + return nil, err + } + s := JSONRPC{ + JSONRPC: "2.0", + Method: "get_account", + Params: params, + Id: 0, + } + body, err := c.requestResponse(s) + if err != nil { + return nil, err + } + var response struct { + Result *core.ResponseGetAccount `json:"result"` + Error string `json:"error"` + Id string `json:"id"` + JSONRPC string `json:"jsonrpc"` + } + binary.ReadJSON(&response, body, &err) + if err != nil { + return nil, err + } + if response.Error != "" { + return nil, fmt.Errorf(response.Error) + } + return response.Result, nil +} + +func (c *ClientJSON) GetStorage(address []byte) (*core.ResponseGetStorage, error) { + params, err := binaryWriter(address) + if err != nil { + return nil, err + } + s := JSONRPC{ + JSONRPC: "2.0", + Method: "get_storage", + Params: params, + Id: 0, + } + body, err := c.requestResponse(s) + if err != nil { + return nil, err + } + var response struct { + Result *core.ResponseGetStorage `json:"result"` + Error string `json:"error"` + Id string `json:"id"` + JSONRPC string `json:"jsonrpc"` + } + binary.ReadJSON(&response, body, &err) + if err != nil { + return nil, err + } + if response.Error != "" { + return nil, fmt.Errorf(response.Error) + } + return response.Result, nil +}