-
Notifications
You must be signed in to change notification settings - Fork 6
/
chain.go
326 lines (226 loc) · 6.73 KB
/
chain.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
/**
* @copyright defined in eosgo-client/LICENSE.txt
* @author Romain Pellerin - romain@slyseed.com
*
* Donation appreciated :)
* EOS wallet: 0x8d39307d9a0687c894058115365f6ad0f03b9ff9
* ETH wallet: 0x317b60152f0a90c10cea52d751ccb4dfad2fe9e7
* BTC wallet: 3HdFx8C3WNA6RyyGYKygECGbLD1BxyeqTN
* BCH wallet: 14e9fnmcHSZxxd3fnrkaG6EYph9JuWcF9T
*/
package rpc
import (
"eosgo-client/model"
"eosgo-client/common"
"eosgo-client/network"
"eosgo-client/errors"
"encoding/json"
"strings"
"strconv"
"time"
"fmt"
)
/**
See if you have EOS source {$EOS_SOURCE}/docs/group__eosiorpc.html#chainrpc for detailed specs of:
or download from https://eosio.github.io/eos/group__eosiorpc.html
- GetInfo
- GetBlock
- GetAccount
- GetCode
- GetTableRows
- AbiJSONToBin
- AbiBinToJSON
- PushTransaction
- //TODO: PushTransactions
- GetRequiredKeys
*/
func ChainGetInfo() (*model.ChainInfo, *errors.AppError){
data, err := network.Get(common.EOS_URL+"/v1/chain/get_info", map[string]string{})
if err != nil {
return nil, err
}
chainInfo := model.ChainInfo{}
errM := json.Unmarshal(data, &chainInfo)
if errM != nil {
return nil, errors.NewAppError(nil, "cannot parse result", -1, nil)
}
return &chainInfo, nil
}
func ChainGetBlock(blockNumOrId string) (*model.Block, *errors.AppError){
data, err := network.Post(common.EOS_URL+"/v1/chain/get_block", map[string]interface{}{"block_num_or_id":blockNumOrId}, nil)
if err != nil {
return nil, err
}
block := model.Block{}
errM := json.Unmarshal(data, &block)
if errM != nil {
return nil, errors.NewAppError(nil, "cannot parse result", -1, nil)
}
return &block, nil
}
func ChainGetAccount(accountName string) (*model.Account, *errors.AppError){
data, err := network.Post(common.EOS_URL+"/v1/chain/get_account", map[string]interface{}{"account_name":accountName}, nil)
if err != nil {
return nil, err
}
account := model.Account{}
errM := json.Unmarshal(data, &account)
if errM != nil {
return nil, errors.NewAppError(nil, "cannot parse result", -1, nil)
}
return &account, nil
}
func ChainGetCode(accountName string) (*model.Code, *errors.AppError){
data, err := network.Post(common.EOS_URL+"/v1/chain/get_code", map[string]interface{}{"account_name":accountName}, nil)
if err != nil {
return nil, err
}
code := model.Code{}
errM := json.Unmarshal(data, &code)
if errM != nil {
return nil, errors.NewAppError(nil, "cannot parse result", -1, nil)
}
return &code, nil
}
func ChainGetTableRows(scope string, code string, table string, toJSON bool, lowerBound int, upperBound int, limit int ) (*model.TableRows, *errors.AppError){
_toJSON := "false"
if toJSON {
_toJSON = "true"
}
params := map[string]interface{}{
"table": table,
"scope":scope,
"code":code,
"json": _toJSON,
"lower_bound": lowerBound,
"upper_bound": upperBound,
"limit": limit,
}
data, err := network.Post(common.EOS_URL+"/v1/chain/get_table_rows", params, nil)
if err != nil {
return nil, err
}
tableRows := model.TableRows{}
errM := json.Unmarshal(data, &tableRows)
if errM != nil {
return nil, errors.NewAppError(nil, "cannot parse result", -1, nil)
}
return &tableRows, nil
}
func ChainAbiJSONToBin(abiJSON *model.AbiJSON) (*model.Abi, *errors.AppError){
bin, err := model.AbiJSONToBytes(abiJSON)
data, err := network.Post(common.EOS_URL+"/v1/chain/abi_json_to_bin", nil, bin)
if err != nil {
return nil, err
}
abiBin := model.AbiBin{}
errM := json.Unmarshal(data, &abiBin)
if errM != nil {
return nil, errors.NewAppError(nil, "cannot parse result", -1, nil)
}
abi := model.Abi {
*abiJSON,
abiBin,
}
return &abi, nil
}
func ChainAbiBinToJSON(abi *model.Abi) (*model.Abi, *errors.AppError){
bin, err := model.AbiToBytes(abi)
data, err := network.Post(common.EOS_URL+"/v1/chain/abi_bin_to_json", nil, bin)
if err != nil {
return nil, err
}
abiJSON := model.AbiJSON{}
errM := json.Unmarshal(data, &abiJSON)
if errM != nil {
return nil, errors.NewAppError(nil, "cannot parse result", -1, nil)
}
abi.AbiJSON = abiJSON
return abi, nil
}
func ChainPushTransaction(trx model.Transaction, pubKeys []string , chainId string) (*model.Transaction, *errors.AppError) {
chainInfo, err := ChainGetInfo()
block, err := ChainGetBlock(strconv.Itoa(chainInfo.LastIrreversibleBlockNum))
trx.RefBlockNum = chainInfo.LastIrreversibleBlockNum
trx.RefBlockPrefix = int64(block.RefBlockPrefix)
actions:= trx.Actions
// calculate expiration date
time := time.Now().UTC().Add(time.Duration(common.Config.TRANSACTION_EXPIRATION_DELAY*1000*1000*1000))
trx.Expiration = time.Format("2006-01-02T15:04:05")
fmt.Println("time:", trx.Expiration)
// calculate HEX data for each action
for i := 0; i<len(trx.Actions); i++ {
abiJSON := model.AbiJSON{
trx.Actions[i].Account,
trx.Actions[i].Name,
trx.Actions[i].Args,
}
data, err := ChainAbiJSONToBin(&abiJSON)
if err != nil {
return nil, err
}
trx.Actions[i].Data = string(data.Binargs)
}
// sign transaction
trxSigned, err := WalletSignTransaction(trx, pubKeys, chainId)
if err != nil {
return nil, err
}
trx.Signatures = trxSigned.Signatures
trx.Actions = actions
// encode trx
raw, err := model.TransactionToJSON(&trx)
if err != nil {
return nil, err
}
// encode signatures
signatures := "["
for i:=0; i<len(trx.Signatures); i++ {
signatures += "\""+trx.Signatures[i]+"\""
if i+1 < len(trx.Signatures) {
signatures += ","
}
}
signatures += "]"
trxData, err := network.PostRawData(common.EOS_URL+"/v1/chain/push_transaction", "{\"signatures\":"+signatures+",\"transaction\":"+raw+",\"compression\":\"none\"}")
if err != nil {
return nil, err
}
var trxDataMap map[string]interface{}
dec := json.NewDecoder(strings.NewReader(string(trxData)))
errD := dec.Decode(&trxDataMap)
if errD != nil {
return nil, errors.NewAppError(nil, "cannot parse transaction data from: "+string(trxData), -1, nil)
}
trx.ID = trxDataMap["transaction_id"].(string)
return &trx, nil
}
func ChainGetRequiredKeys(trx *model.Transaction) ([]string, *errors.AppError){
myPubKeys, err := WalletGetPublicKeys()
if err != nil {
return nil, err
}
// encode signatures
pubKeys := "["
for i:=0; i<len(myPubKeys); i++ {
pubKeys += "\"" + myPubKeys[i] + "\""
if i+1 < len(myPubKeys) {
pubKeys += ","
}
}
pubKeys += "]"
toJson, err := model.TransactionToJSON(trx)
if err != nil {
return nil, err
}
data, err := network.PostRawData(common.EOS_URL+"/v1/chain/get_required_keys", "{\"transaction\":"+toJson+",\"available_keys\":"+pubKeys+"}")
if err != nil {
return nil, err
}
keys := map[string][]string{}
errM := json.Unmarshal(data, &keys)
if errM != nil {
return nil, errors.NewAppError(nil, "cannot parse result", -1, nil)
}
return keys["required_keys"], nil
}