2018-03-05 08:53:09 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
2018-12-21 09:32:18 +00:00
|
|
|
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/wallet"
|
|
|
|
"github.com/pkg/errors"
|
2018-03-05 08:53:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
defaultDialTimeout = 4 * time.Second
|
|
|
|
defaultRequestTimeout = 4 * time.Second
|
|
|
|
defaultClientVersion = "2.0"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Client represents the middleman for executing JSON RPC calls
|
|
|
|
// to remote NEO RPC nodes.
|
|
|
|
type Client struct {
|
|
|
|
// The underlying http client. It's never a good practice to use
|
|
|
|
// the http.DefaultClient, therefore we will role our own.
|
|
|
|
http.Client
|
|
|
|
endpoint *url.URL
|
|
|
|
ctx context.Context
|
|
|
|
version string
|
2018-12-21 09:32:18 +00:00
|
|
|
Wif *wallet.WIF
|
2019-01-22 12:14:40 +00:00
|
|
|
Balancer BalanceGetter
|
2018-03-05 08:53:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ClientOptions defines options for the RPC client.
|
|
|
|
// All Values are optional. If any duration is not specified
|
|
|
|
// a default of 3 seconds will be used.
|
|
|
|
type ClientOptions struct {
|
|
|
|
Cert string
|
|
|
|
Key string
|
|
|
|
CACert string
|
|
|
|
DialTimeout time.Duration
|
|
|
|
RequestTimeout time.Duration
|
|
|
|
// Version is the version of the client that will be send
|
|
|
|
// along with the request body. If no version is specified
|
|
|
|
// the default version (currently 2.0) will be used.
|
|
|
|
Version string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewClient return a new Client ready to use.
|
|
|
|
func NewClient(ctx context.Context, endpoint string, opts ClientOptions) (*Client, error) {
|
|
|
|
url, err := url.Parse(endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.DialTimeout == 0 {
|
|
|
|
opts.DialTimeout = defaultDialTimeout
|
|
|
|
}
|
|
|
|
if opts.RequestTimeout == 0 {
|
|
|
|
opts.RequestTimeout = defaultRequestTimeout
|
|
|
|
}
|
|
|
|
if opts.Version == "" {
|
|
|
|
opts.Version = defaultClientVersion
|
|
|
|
}
|
|
|
|
|
|
|
|
transport := &http.Transport{
|
2019-02-05 08:29:39 +00:00
|
|
|
DialContext: (&net.Dialer{
|
2018-03-05 08:53:09 +00:00
|
|
|
Timeout: opts.DialTimeout,
|
2019-02-05 08:29:39 +00:00
|
|
|
}).DialContext,
|
2018-03-05 08:53:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(@antdm): Enable SSL.
|
|
|
|
if opts.Cert != "" && opts.Key != "" {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Client{
|
|
|
|
Client: http.Client{
|
|
|
|
Timeout: opts.RequestTimeout,
|
|
|
|
Transport: transport,
|
|
|
|
},
|
|
|
|
endpoint: url,
|
|
|
|
ctx: ctx,
|
|
|
|
version: opts.Version,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-12-21 09:32:18 +00:00
|
|
|
// SetWIF decodes given WIF and adds some wallet
|
|
|
|
// data to client. Useful for RPC calls that require an open wallet.
|
|
|
|
func (c *Client) SetWIF(wif string) error {
|
|
|
|
decodedWif, err := wallet.WIFDecode(wif, 0x00)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Failed to decode WIF; failed to add WIF to client ")
|
|
|
|
}
|
|
|
|
c.Wif = decodedWif
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) SetBalancer(b BalanceGetter) {
|
|
|
|
c.Balancer = b
|
|
|
|
}
|
|
|
|
|
2018-03-05 08:53:09 +00:00
|
|
|
func (c *Client) performRequest(method string, p params, v interface{}) error {
|
2018-11-26 15:56:45 +00:00
|
|
|
var (
|
|
|
|
r = request{
|
|
|
|
JSONRPC: c.version,
|
|
|
|
Method: method,
|
|
|
|
Params: p.values,
|
|
|
|
ID: 1,
|
|
|
|
}
|
|
|
|
buf = new(bytes.Buffer)
|
|
|
|
)
|
2018-03-05 08:53:09 +00:00
|
|
|
|
2018-11-26 15:56:45 +00:00
|
|
|
if err := json.NewEncoder(buf).Encode(r); err != nil {
|
2018-03-05 08:53:09 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-11-26 15:56:45 +00:00
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", c.endpoint.String(), buf)
|
2018-03-05 08:53:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resp, err := c.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
2019-01-25 11:20:35 +00:00
|
|
|
return fmt.Errorf("remote responded with a non 200 response: %d", resp.StatusCode)
|
2018-03-05 08:53:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return json.NewDecoder(resp.Body).Decode(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ping attempts to create a connection to the endpoint.
|
|
|
|
// and returns an error if there is one.
|
|
|
|
func (c *Client) Ping() error {
|
|
|
|
conn, err := net.DialTimeout("tcp", c.endpoint.Host, defaultDialTimeout)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_ = conn.Close()
|
|
|
|
return nil
|
|
|
|
}
|