2018-03-05 08:53:09 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2019-02-12 19:03:21 +00:00
|
|
|
"sync"
|
2018-03-05 08:53:09 +00:00
|
|
|
"time"
|
2018-12-21 09:32:18 +00:00
|
|
|
|
2019-11-25 17:39:11 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/entities"
|
2019-11-19 17:16:15 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/transaction"
|
2019-08-27 13:29:42 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/crypto/keys"
|
2019-11-19 17:16:15 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
2018-12-21 09:32:18 +00:00
|
|
|
"github.com/pkg/errors"
|
2018-03-05 08:53:09 +00:00
|
|
|
)
|
|
|
|
|
2019-11-22 09:08:33 +00:00
|
|
|
const (
|
2018-03-05 08:53:09 +00:00
|
|
|
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.
|
2019-02-12 19:03:21 +00:00
|
|
|
cliMu *sync.Mutex
|
|
|
|
cli *http.Client
|
|
|
|
endpoint *url.URL
|
|
|
|
ctx context.Context
|
|
|
|
version string
|
|
|
|
wifMu *sync.Mutex
|
2019-08-27 13:29:42 +00:00
|
|
|
wif *keys.WIF
|
2019-02-12 19:03:21 +00:00
|
|
|
balancerMu *sync.Mutex
|
|
|
|
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 {
|
2019-02-12 19:03:21 +00:00
|
|
|
Cert string
|
|
|
|
Key string
|
|
|
|
CACert string
|
|
|
|
DialTimeout time.Duration
|
|
|
|
Client *http.Client
|
2018-03-05 08:53:09 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2019-10-22 14:56:03 +00:00
|
|
|
// NewClient returns a new Client ready to use.
|
2018-03-05 08:53:09 +00:00
|
|
|
func NewClient(ctx context.Context, endpoint string, opts ClientOptions) (*Client, error) {
|
|
|
|
url, err := url.Parse(endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if opts.Version == "" {
|
|
|
|
opts.Version = defaultClientVersion
|
|
|
|
}
|
|
|
|
|
2019-02-12 19:03:21 +00:00
|
|
|
if opts.Client == nil {
|
|
|
|
opts.Client = &http.Client{
|
|
|
|
Transport: &http.Transport{
|
|
|
|
DialContext: (&net.Dialer{
|
|
|
|
Timeout: opts.DialTimeout,
|
|
|
|
}).DialContext,
|
|
|
|
},
|
|
|
|
}
|
2018-03-05 08:53:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(@antdm): Enable SSL.
|
|
|
|
if opts.Cert != "" && opts.Key != "" {
|
|
|
|
}
|
|
|
|
|
2019-02-12 19:03:21 +00:00
|
|
|
if opts.Client.Timeout == 0 {
|
|
|
|
opts.Client.Timeout = defaultRequestTimeout
|
|
|
|
}
|
|
|
|
|
2018-03-05 08:53:09 +00:00
|
|
|
return &Client{
|
2019-02-12 19:03:21 +00:00
|
|
|
ctx: ctx,
|
|
|
|
cli: opts.Client,
|
|
|
|
cliMu: new(sync.Mutex),
|
|
|
|
balancerMu: new(sync.Mutex),
|
|
|
|
wifMu: new(sync.Mutex),
|
|
|
|
endpoint: url,
|
|
|
|
version: opts.Version,
|
2018-03-05 08:53:09 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-09-03 14:51:37 +00:00
|
|
|
// WIF returns WIF structure associated with the client.
|
2019-08-27 13:29:42 +00:00
|
|
|
func (c *Client) WIF() keys.WIF {
|
2019-02-12 19:03:21 +00:00
|
|
|
c.wifMu.Lock()
|
|
|
|
defer c.wifMu.Unlock()
|
2019-08-27 13:29:42 +00:00
|
|
|
return keys.WIF{
|
2019-02-12 19:03:21 +00:00
|
|
|
Version: c.wif.Version,
|
|
|
|
Compressed: c.wif.Compressed,
|
|
|
|
PrivateKey: c.wif.PrivateKey,
|
|
|
|
S: c.wif.S,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2019-02-12 19:03:21 +00:00
|
|
|
c.wifMu.Lock()
|
|
|
|
defer c.wifMu.Unlock()
|
2019-08-27 13:29:42 +00:00
|
|
|
decodedWif, err := keys.WIFDecode(wif, 0x00)
|
2018-12-21 09:32:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Failed to decode WIF; failed to add WIF to client ")
|
|
|
|
}
|
2019-02-12 19:03:21 +00:00
|
|
|
c.wif = decodedWif
|
2018-12-21 09:32:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-03 14:51:37 +00:00
|
|
|
// Balancer is a getter for balance field.
|
2019-02-12 19:03:21 +00:00
|
|
|
func (c *Client) Balancer() BalanceGetter {
|
|
|
|
c.balancerMu.Lock()
|
|
|
|
defer c.balancerMu.Unlock()
|
|
|
|
return c.balancer
|
|
|
|
}
|
|
|
|
|
2019-09-03 14:51:37 +00:00
|
|
|
// SetBalancer is a setter for balance field.
|
2018-12-21 09:32:18 +00:00
|
|
|
func (c *Client) SetBalancer(b BalanceGetter) {
|
2019-02-12 19:03:21 +00:00
|
|
|
c.balancerMu.Lock()
|
|
|
|
defer c.balancerMu.Unlock()
|
|
|
|
|
|
|
|
if b != nil {
|
|
|
|
c.balancer = b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-03 14:51:37 +00:00
|
|
|
// Client is a getter for client field.
|
2019-02-12 19:03:21 +00:00
|
|
|
func (c *Client) Client() *http.Client {
|
|
|
|
c.cliMu.Lock()
|
|
|
|
defer c.cliMu.Unlock()
|
|
|
|
return c.cli
|
|
|
|
}
|
|
|
|
|
2019-09-03 14:51:37 +00:00
|
|
|
// SetClient is a setter for client field.
|
2019-02-12 19:03:21 +00:00
|
|
|
func (c *Client) SetClient(cli *http.Client) {
|
|
|
|
c.cliMu.Lock()
|
|
|
|
defer c.cliMu.Unlock()
|
|
|
|
|
|
|
|
if cli != nil {
|
|
|
|
c.cli = cli
|
|
|
|
}
|
2018-12-21 09:32:18 +00:00
|
|
|
}
|
|
|
|
|
2019-11-19 17:16:15 +00:00
|
|
|
// CalculateInputs creates input transactions for the specified amount of given
|
|
|
|
// asset belonging to specified address. This implementation uses GetUnspents
|
|
|
|
// JSON-RPC call internally, so make sure your RPC server suppors that.
|
|
|
|
func (c *Client) CalculateInputs(address string, asset util.Uint256, cost util.Fixed8) ([]transaction.Input, util.Fixed8, error) {
|
2019-11-25 17:39:11 +00:00
|
|
|
var utxos entities.UnspentBalances
|
2019-11-19 17:16:15 +00:00
|
|
|
|
|
|
|
resp, err := c.GetUnspents(address)
|
|
|
|
if err != nil || resp.Error != nil {
|
|
|
|
if err == nil {
|
|
|
|
err = fmt.Errorf("remote returned %d: %s", resp.Error.Code, resp.Error.Message)
|
|
|
|
}
|
|
|
|
return nil, util.Fixed8(0), errors.Wrapf(err, "cannot get balance for address %v", address)
|
|
|
|
}
|
|
|
|
for _, ubi := range resp.Result.Balance {
|
|
|
|
if asset.Equals(ubi.AssetHash) {
|
|
|
|
utxos = ubi.Unspents
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return unspentsToInputs(utxos, cost)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2019-02-12 19:03:21 +00:00
|
|
|
resp, err := c.Client().Do(req)
|
2018-03-05 08:53:09 +00:00
|
|
|
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
|
|
|
|
}
|