-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrpcClient.go
385 lines (336 loc) · 9.61 KB
/
rpcClient.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/pkg/errors"
"io/ioutil"
"net/http"
"strings"
)
type getUtxoRequest struct {
JsonRpc string `json:"jsonrpc"`
Id int64 `json:"id"`
Method string `json:"method"`
Params sendtxParams `json:"params"`
}
type getUtxoParams struct {
Value int64 `json:"count"`
Fee int64 `json:"skip"`
}
type getUtxoResponse struct {
JsonRpc string `json:"jsonrpc"`
Id int64 `json:"id"`
Result []getUtxoResult `json:"result,omitempty"`
Error ErrorInfo `json:"error,omitempty"`
}
type getUtxoResult struct {
Amount int64 `json:"amount"`
CreateTxId string `json:"createTxId"`
Id string `json:"id"`
Maturity uint64 `json:"maturity"`
Session int64 `json:"session"`
SpentTxId string `json:"spentTxId"`
Status int64 `json:"status"`
Status_string string `json:"status_string"`
Type string `json:"type"`
}
func GetUtxos() (balance int64, err error) {
var reqUtxo getUtxoRequest
reqUtxo.Method = "get_utxo"
reqUtxo.JsonRpc = "2.0"
reqUtxo.Id = 1
client := &http.Client{}
bytesData, _ := json.Marshal(reqUtxo)
req, _ := http.NewRequest("POST", cfg.WalletApiUrl, bytes.NewReader(bytesData))
resp, _ := client.Do(req)
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
var res getUtxoResponse
err = json.Unmarshal(body, &res)
if err != nil {
Warning.Println("Unmarshal Response error:", err)
return 0, err
}
if strings.Contains(string(body), "error") {
err = errors.New(res.Error.Message)
return 0, err
}
var totalAmount int64
for _, item := range res.Result {
if item.Type != "mine" {
continue
}
if item.Status != 2 && item.Status != 1 {
continue
}
amount := item.Amount
totalAmount += amount
}
return totalAmount, nil
}
type getWalletStatusRequest struct {
JsonRpc string `json:"jsonrpc"`
Id int64 `json:"id"`
Method string `json:"method"`
}
type getWalletStatusResponse struct {
JsonRpc string `json:"jsonrpc"`
Id int64 `json:"id"`
Result getWalletStatusResult `json:"result,omitempty"`
Error ErrorInfo `json:"error,omitempty"`
}
type getWalletStatusResult struct {
Current_height int64 `json:"current_height"`
Current_state_hash string `json:"current_state_hash"`
Prev_state_hash string `json:"prev_state_hash"`
Available int64 `json:"available"`
Receiving int64 `json:"receiving"`
Sending int64 `json:"sending"`
Maturing int64 `json:"maturing"`
Locked int64 `json:"locked"`
Difficulty float64 `json:"difficulty"`
}
func getWalletStatus() (balance int64, err error) {
var reqUtxo getUtxoRequest
reqUtxo.Method = "wallet_status"
reqUtxo.JsonRpc = "2.0"
reqUtxo.Id = 1
client := &http.Client{}
bytesData, _ := json.Marshal(reqUtxo)
req, _ := http.NewRequest("POST", cfg.WalletApiUrl, bytes.NewReader(bytesData))
resp, _ := client.Do(req)
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
var res getWalletStatusResponse
err = json.Unmarshal(body, &res)
if err != nil {
Warning.Println("Unmarshal Response error:", err.Error())
return 0, err
}
if strings.Contains(string(body), "error") {
err = errors.New(res.Error.Message)
return 0, err
}
return res.Result.Available, nil
}
type sendTxRequest struct {
JsonRpc string `json:"jsonrpc"`
Id int64 `json:"id"`
Method string `json:"method"`
Params sendtxParams `json:"params"`
}
type sendtxParams struct {
Value int64 `json:"value"`
Fee int64 `json:"fee,omitempty"`
From string `json:"from,omitempty"`
Address string `json:"address"`
Comment string `json:"comment,omitempty"`
}
type sendTxResponse struct {
JsonRpc string `json:"jsonrpc"`
Id int64 `json:"id"`
Result sendtxResult `json:"result,omitempty"`
Error ErrorInfo `json:"error,omitempty"`
}
type sendtxResult struct {
Txid string `json:"txId"`
}
type ErrorInfo struct {
Code int64 `json:"code"`
Data string `json:"data"`
Message string `json:"message"`
}
func sendTx(amount int64, toaddr string) (txId string, err error) {
client := &http.Client{}
var reqSendtx sendTxRequest
reqSendtx.JsonRpc = "2.0"
reqSendtx.Id = 1
reqSendtx.Method = "tx_send"
var reqparams sendtxParams
reqparams.Value = amount
reqparams.Address = toaddr
reqparams.Fee = cfg.TxFee
reqSendtx.Params = reqparams
bytesData, err := json.Marshal(reqSendtx)
if err != nil {
Warning.Println("Marshal reqSendtx error:", err.Error())
}
Info.Println("bytesData:", string(bytesData))
req, err := http.NewRequest("POST", cfg.WalletApiUrl, bytes.NewReader(bytesData))
if err != nil {
Warning.Println("NewRequest error:", err.Error())
}
resp, _ := client.Do(req)
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
var res sendTxResponse
err = json.Unmarshal(body, &res)
if err != nil {
Warning.Println("Unmarshal Response error:", err.Error())
return "", err
}
if res.Error.Message != "" {
err = errors.New(res.Error.Message)
return "", err
}
if res.Result.Txid != "" {
return res.Result.Txid, nil
}
return
}
type validAddrRequest struct {
JsonRpc string `json:"jsonrpc"`
Id int64 `json:"id"`
Method string `json:"method"`
Params validAddrParams `json:"params"`
}
type validAddrParams struct {
Address string `json:"address"`
}
type validAddrResponse struct {
JsonRpc string `json:"jsonrpc"`
Id int64 `json:"id"`
Result validAddrResult `json:"result,omitempty"`
Error ErrorInfo `json:"error,omitempty"`
}
type validAddrResult struct {
IsMine bool `json:"is_mine"`
IsValid bool `json:"is_valid"`
}
func validAddrress(addr string) (isvalid bool, err error) {
client := &http.Client{}
var validaddr validAddrRequest
validaddr.JsonRpc = "2.0"
validaddr.Id = 1
validaddr.Method = "validate_address"
var reqparams validAddrParams
reqparams.Address = addr
validaddr.Params = reqparams
bytesData, _ := json.Marshal(validaddr)
req, _ := http.NewRequest("POST", cfg.WalletApiUrl, bytes.NewReader(bytesData))
resp, _ := client.Do(req)
if resp != nil {
defer resp.Body.Close()
} else {
err = errors.New("Connect wallet-api faild,please check.")
return false, err
}
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
var res validAddrResponse
err = json.Unmarshal(body, &res)
if err != nil {
Warning.Println("Unmarshal Response error:", err)
return false, err
}
if res.Error.Message != "" {
err = errors.New(res.Error.Message)
return false, err
}
return res.Result.IsValid, nil
}
type checkTxRequest struct {
JsonRpc string `json:"jsonrpc"`
Id int64 `json:"id"`
Method string `json:"method"`
Params checkTxParams `json:"params"`
}
type checkTxParams struct {
TxId string `json:"txId"`
}
type checkTxResponse struct {
JsonRpc string `json:"jsonrpc"`
Id int64 `json:"id"`
Result checkTxResult `json:"result,omitempty"`
Error ErrorInfo `json:"error,omitempty"`
}
type checkTxResult struct {
TxId string `json:"txId"`
Comment string `json:"comment"`
Fee int64 `json:"fee"`
Kernel string `json:"kernel"`
Receiver string `json:"receiver"`
Sender string `json:"sender"`
Status int64 `json:"status"`
Status_string string `json:"status_string"`
Failure_reason string `json:"failure_reason"`
Value int64 `json:"value"`
Height int64 `json:"height"`
Confirmations int64 `json:"confirmations"`
Create_time int64 `json:"create_time"`
Income bool `json:"income"`
}
func getTxState(txid string) (state int64, err error) {
client := &http.Client{}
var checkTx checkTxRequest
checkTx.JsonRpc = "2.0"
checkTx.Id = 1
checkTx.Method = "tx_status"
var reqparams checkTxParams
reqparams.TxId = txid
checkTx.Params = reqparams
bytesData, _ := json.Marshal(checkTx)
req, _ := http.NewRequest("POST", cfg.WalletApiUrl, bytes.NewReader(bytesData))
resp, _ := client.Do(req)
if resp != nil {
defer resp.Body.Close()
} else {
err = errors.New("Connect wallet-api faild,please check.")
return -1, err
}
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
var res checkTxResponse
err = json.Unmarshal(body, &res)
if err != nil {
Warning.Println("Unmarshal Response error:", err)
return -1, err
}
if res.Error.Message != "" {
err = errors.New(res.Error.Message)
return -1, err
}
return res.Result.Status, nil
}
type CancelTxResponse struct {
JsonRpc string `json:"jsonrpc"`
Id int64 `json:"id"`
Result bool `json:"result,omitempty"`
Error ErrorInfo `json:"error,omitempty"`
}
func cancelTx(txid string) (success bool, err error) {
client := &http.Client{}
var cancelTx checkTxRequest
cancelTx.JsonRpc = "2.0"
cancelTx.Id = 1
cancelTx.Method = "tx_cancel"
var reqparams checkTxParams
reqparams.TxId = txid
cancelTx.Params = reqparams
bytesData, _ := json.Marshal(cancelTx)
req, _ := http.NewRequest("POST", cfg.WalletApiUrl, bytes.NewReader(bytesData))
resp, _ := client.Do(req)
if resp != nil {
defer resp.Body.Close()
} else {
err = errors.New("Connect wallet-api faild,please check.")
return false, err
}
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
var res CancelTxResponse
err = json.Unmarshal(body, &res)
if err != nil {
Warning.Println("Unmarshal Response error:", err)
return false, err
}
if res.Error.Message != "" {
err = errors.New(res.Error.Message)
return false, err
}
return res.Result, nil
}