-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclient.go
132 lines (108 loc) · 2.9 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package rpc
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"time"
"github.com/SkycoinProject/dmsg"
dmsghttp "github.com/SkycoinProject/dmsg-http"
"github.com/SkycoinProject/dmsg/cipher"
"github.com/SkycoinProject/dmsg/disc"
"github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/btcutil"
)
const (
dialTimeout = 60 * time.Second
httpClientTimeout = 120 * time.Second
tlsHandshakeTimeout = 60 * time.Second
// ContentTypeJSON json content type header
ContentTypeJSON = "application/json"
// ContentTypeForm form data content type header
ContentTypeForm = "application/x-www-form-urlencoded"
)
type Client struct {
httpClient *http.Client
addr string
}
// rawResponse is a partially-unmarshaled JSON-RPC response. For this
// to be valid (according to JSON-RPC 1.0 spec), ID may not be nil.
type rawResponse struct {
Result json.RawMessage `json:"result"`
Error *btcjson.RPCError `json:"error"`
}
func NewClient(addr string) *Client {
discovery := disc.NewHTTP("http://dmsg.discovery.skywire.cc")
cPK, cSK := cipher.GenerateKeyPair()
dmsgClient := dmsg.NewClient(cPK, cSK, discovery, dmsg.DefaultConfig())
go dmsgClient.Serve()
time.Sleep(time.Second) // wait for dmsg client to be ready
dmsgTransport := dmsghttp.Transport{
DmsgClient: dmsgClient,
}
httpClient := &http.Client{
Transport: dmsgTransport,
Timeout: httpClientTimeout,
}
addr = strings.TrimRight(addr, "/")
addr += "/"
return NewClientWithHTTPClient(addr, httpClient)
}
func NewClientWithHTTPClient(addr string, httpClient *http.Client) *Client {
return &Client{
httpClient: httpClient,
addr: addr,
}
}
func (c *Client) GetBalance(account string) (*btcutil.Amount, error) {
cmd := btcjson.NewGetBalanceCmd(&account, nil)
resp, err := c.sendCmd(cmd)
if err != nil {
return nil, err
}
fmt.Println(resp)
// Unmarshal result as a floating point number.
var balance float64
err = json.Unmarshal(resp.Result, &balance)
if err != nil {
return nil, err
}
amount, err := btcutil.NewAmount(balance)
if err != nil {
return nil, err
}
return &amount, nil
}
func (c *Client) sendCmd(cmd interface{}) (*rawResponse, error) {
// Marshal the command.
marshalledJSON, err := btcjson.MarshalCmd(1, cmd)
if err != nil {
return nil, err
}
bodyReader := bytes.NewReader(marshalledJSON)
httpReq, err := http.NewRequest("POST", c.addr, bodyReader)
if err != nil {
return nil, err
}
httpReq.Close = true
httpReq.Header.Set("Content-Type", "application/json")
httpReq.SetBasicAuth("user", "password")
httpResponse, err := c.httpClient.Do(httpReq)
if err != nil {
return nil, err
}
// Read the raw bytes and close the response.
respBytes, err := ioutil.ReadAll(httpResponse.Body)
_ = httpResponse.Body.Close()
if err != nil {
return nil, err
}
var resp rawResponse
err = json.Unmarshal(respBytes, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}