-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.go
More file actions
401 lines (343 loc) · 9.35 KB
/
main.go
File metadata and controls
401 lines (343 loc) · 9.35 KB
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package tronhttpClient
import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
httpClient "github.com/stdevHsequeda/TRONHttpClient/client"
"net/http"
)
const testNet = "https://api.shasta.trongrid.io"
const mainNet = "https://api.trongrid.io"
type Client struct {
client *httpClient.Client
network string
}
// NewClient returns a new instance of Client
func NewClient(network string) *Client {
httpClient.MaxRetry = 5
return &Client{client: httpClient.NewClient(), network: network}
}
// CreateTx Create a TRX transfer transaction.
// If toAddr does not exist, then create the account on the blockchain.
func (c *Client) CreateTx(toAddr, ownerAddr string, amount int) (*Transaction, error) {
encodeData, err := json.Marshal(
map[string]interface{}{
"to_address": toAddr,
"owner_address": ownerAddr,
"amount": amount,
})
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", testNet+"/wallet/createtransaction",
bytes.NewBuffer(encodeData))
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
resp, err := c.client.CallRetryable(req)
if err != nil {
return nil, err
}
var tx Transaction
err = json.NewDecoder(resp).Decode(&tx)
if err != nil {
return nil, err
}
return &tx, err
}
// GetTxSign Sign the transaction, the api has the risk of leaking the private key,
// please make sure to call the api in a secure environment
func (c *Client) GetTxSign(tx *Transaction, privKey string) (*Transaction, error) {
encodeData, err := json.Marshal(
struct {
Transaction *Transaction `json:"transaction"`
PrivateKey string `json:"privateKey"`
}{
Transaction: tx,
PrivateKey: privKey,
})
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", testNet+"/wallet/gettransactionsign",
bytes.NewBuffer(encodeData))
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
resp, err := c.client.CallRetryable(req)
if err != nil {
return nil, err
}
err = json.NewDecoder(resp).Decode(tx)
if err != nil {
return nil, err
}
return tx, err
}
// BroadcastTx Broadcast the signed transaction
func (c *Client) BroadcastTx(tx *Transaction) (*Transaction, error) {
encodeData, err := json.Marshal(tx)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", testNet+"/wallet/broadcasttransaction",
bytes.NewBuffer(encodeData))
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
resp, err := c.client.CallRetryable(req)
if err != nil {
return nil, err
}
err = json.NewDecoder(resp).Decode(tx)
if err != nil {
return nil, err
}
return tx, nil
}
// GenerateAddress Generates a random private key and address pair. Returns a private key,
// the corresponding address in hex, and base58.
func (c *Client) GenerateAddress() (*Address, error) {
req, err := http.NewRequest("GET", testNet+"/wallet/generateaddress",
nil)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
resp, err := c.client.CallRetryable(req)
if err != nil {
return nil, err
}
var addr Address
err = json.NewDecoder(resp).Decode(&addr)
if err != nil {
return nil, err
}
return &addr, nil
}
// CreateAddress Create address from a specified password string (NOT PRIVATE KEY)
func (c *Client) CreateAddress(password string) (*AddressWithoutPrivKey, error) {
encodeData, err := json.Marshal(map[string]string{
"value": hex.EncodeToString([]byte(password)),
})
if err != nil {
return nil, err
}
req, err := http.NewRequest("GET", testNet+"/wallet/createaddress", bytes.NewBuffer(encodeData))
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
resp, err := c.client.CallRetryable(req)
if err != nil {
return nil, err
}
var addr AddressWithoutPrivKey
err = json.NewDecoder(resp).Decode(&addr)
if err != nil {
return nil, err
}
return &addr, nil
}
// ValidateAddress Validates address, returns either true or false.
func (c *Client) ValidateAddress(address string) (bool, error) {
encodeData, err := json.Marshal(map[string]string{
"address": string(address),
})
if err != nil {
return false, err
}
req, err := http.NewRequest("GET", testNet+"/wallet/validateaddress", bytes.NewBuffer(encodeData))
if err != nil {
return false, err
}
req.Header.Add("Content-Type", "application/json")
resp, err := c.client.CallRetryable(req)
if err != nil {
return false, err
}
var addr struct {
ok bool `json:"result"`
}
err = json.NewDecoder(resp).Decode(&addr)
if err != nil {
return false, err
}
return addr.ok, nil
}
// BroadcastHex Broadcast the protobuf encoded transaction hex string after sign
func (c *Client) BroadcastHex(txHex string) (*Transaction, error) {
encodeData, err := json.Marshal(
map[string]string{
"transaction": txHex,
})
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", testNet+"/wallet/broadcasthex",
bytes.NewBuffer(encodeData))
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
resp, err := c.client.CallRetryable(req)
if err != nil {
return nil, err
}
var tx Transaction
err = json.NewDecoder(resp).Decode(&tx)
if err != nil {
return nil, err
}
return &tx, nil
}
// EasyTransfer Easily transfer from an address using the password string.
// Only works with accounts created from createAddress,integrated getransactionsign and broadcasttransaction.
func (c *Client) EasyTransfer(password, toAddress string, amount int) (*Transaction, error) {
encodeData, err := json.Marshal(
map[string]interface{}{
"passPhrase": hex.EncodeToString([]byte(password)),
"toAddress": toAddress,
"amount": amount,
})
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", testNet+"/wallet/easytransfer",
bytes.NewBuffer(encodeData))
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
resp, err := c.client.CallRetryable(req)
if err != nil {
return nil, err
}
var result struct {
Result struct {
Ok bool `json:"result"`
Code string `json:"code"`
Message string `json:"message"`
}
Transaction Transaction `json:"transaction"`
}
err = json.NewDecoder(resp).Decode(&result)
if err != nil {
return nil, err
}
if result.Result.Ok {
return &result.Transaction, nil
} else {
b, err := hex.DecodeString(result.Result.Message)
if err != nil {
return nil, err
}
return nil, fmt.Errorf("%s: %s", result.Result.Code, string(b))
}
}
// EasyTransferByPrivate Easily transfer from an address using the private key.
func (c *Client) EasyTransferByPrivate(privateKey, toAddress string, amount int) (*Transaction, error) {
encodeData, err := json.Marshal(
map[string]interface{}{
"privateKey": privateKey,
"toAddress": toAddress,
"amount": amount,
})
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", testNet+"/wallet/easytransferbyprivate",
bytes.NewBuffer(encodeData))
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
resp, err := c.client.CallRetryable(req)
if err != nil {
return nil, err
}
var result struct {
Result struct {
Ok bool `json:"result"`
Code string `json:"code"`
Message string `json:"message"`
}
Transaction Transaction `json:"transaction"`
}
err = json.NewDecoder(resp).Decode(&result)
if err != nil {
return nil, err
}
if result.Result.Ok {
return &result.Transaction, nil
} else {
b, err := hex.DecodeString(result.Result.Message)
if err != nil {
return nil, err
}
return nil, fmt.Errorf("%s: %s", result.Result.Code, string(b))
}
}
// CreateAccount Create an account. Uses an already activated account to create a new account
// Note: The expiration time of the http api creation transaction is 1 minute,
// so to complete the on-chain, you need to complete gettransactionsign and
// broadcasttransaction within 1 minute after the creation.
func (c *Client) CreateAccount(ownerAddr, accountAddr string, visible bool, permissionID int) (*Transaction, error) {
encodeData, err := json.Marshal(
map[string]interface{}{
"owner_address": ownerAddr,
"account_address": accountAddr,
"visible": visible,
"permission_id": visible,
})
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", testNet+"/wallet/createaccount",
bytes.NewBuffer(encodeData))
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
resp, err := c.client.CallRetryable(req)
if err != nil {
return nil, err
}
var tx Transaction
err = json.NewDecoder(resp).Decode(&tx)
if err != nil {
return nil, err
}
return &tx, err
}
// GetAccount Query information about an account,Including balances, freezes, votes and time, etc
func (c *Client) GetAccount(address string, visible bool) (*Account, error) {
encodeData, err := json.Marshal(
map[string]interface{}{
"address": address,
"visible": visible,
})
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", testNet+"/wallet/getaccount",
bytes.NewBuffer(encodeData))
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
resp, err := c.client.CallRetryable(req)
if err != nil {
return nil, err
}
var account Account
err = json.NewDecoder(resp).Decode(&account)
if err != nil {
return nil, err
}
return &account, err
}