-
Notifications
You must be signed in to change notification settings - Fork 2
/
eth.go
424 lines (362 loc) · 13.3 KB
/
eth.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
package routes
import (
"bytes"
"encoding/gob"
"encoding/json"
"fmt"
"github.com/davecgh/go-spew/spew"
"github.com/deso-smart/deso-core/v2/lib"
"github.com/golang/glog"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
"io"
"io/ioutil"
"math/big"
"net/http"
"strings"
"time"
)
func (fes *APIServer) IsConfiguredForETH() bool {
return fes.Config.BuyDESOETHAddress != "" && fes.Config.InfuraProjectID != ""
}
type ETHTx struct {
Nonce string `json:"nonce"`
Value string `json:"value"`
ChainId string `json:"chainId"`
To string `json:"to"`
R string `json:"r"`
S string `json:"s"`
}
type SubmitETHTxRequest struct {
PublicKeyBase58Check string
Tx ETHTx
TxBytes string
ToSign []string
SignedHashes []string
}
type SubmitETHTxResponse struct {
DESOTxHash string
}
// ETHTxLog is used by admins to reprocess stuck transactions
type ETHTxLog struct {
PublicKey []byte
DESOTxHash string
}
// We assume that there are valid signatures if the transaction mines.
func (fes *APIServer) validateETHDepositAddress(depositAddress string) error {
// Verify the deposit address is correct
configDepositAddress := strings.ToLower(fes.Config.BuyDESOETHAddress)
txDepositAddress := strings.ToLower(depositAddress)
if configDepositAddress != txDepositAddress {
return errors.Errorf("Invalid deposit address: %s != %s", txDepositAddress, configDepositAddress)
}
return nil
}
func (fes *APIServer) SubmitETHTx(ww http.ResponseWriter, req *http.Request) {
decoder := json.NewDecoder(io.LimitReader(req.Body, MaxRequestBodySizeBytes))
requestData := SubmitETHTxRequest{}
if err := decoder.Decode(&requestData); err != nil {
_AddBadRequestError(ww, fmt.Sprintf("SubmitETHTx: Problem parsing request body: %v", err))
return
}
if !fes.IsConfiguredForETH() {
_AddBadRequestError(ww, "SubmitETHTx: Not configured for ETH")
return
}
if err := fes.validateETHDepositAddress(requestData.Tx.To); err != nil {
_AddBadRequestError(ww, fmt.Sprintf("SubmitETHTx: Failed to validate transaction: %v", err))
return
}
nanosPurchased, err := fes.CalculateNanosPurchasedFromWei(requestData.Tx.Value)
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf("SubmitETHTx: Unable to calculate nanos purchasd from eth tx: %v", err))
return
}
var balanceInsufficient bool
balanceInsufficient, err = fes.ExceedsDeSoBalance(nanosPurchased, fes.Config.BuyDESOSeed)
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf("SubmitETHTx: Error checking if send deso balance is sufficient: %v", err))
return
}
if balanceInsufficient {
_AddBadRequestError(ww, fmt.Sprintf("SubmitETHTx: SendDeSo wallet balance is below nanos purchased"))
return
}
// Parse the public key
pkBytes, _, err := lib.Base58CheckDecode(requestData.PublicKeyBase58Check)
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf("SubmitETHTx: Invalid public key: %v", err))
return
}
// Submit the transaction
params := []interface{}{fmt.Sprintf("0x%v", requestData.SignedHashes[0])}
response, err := fes.ExecuteETHRPCRequest("eth_sendRawTransaction", params)
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf("SubmitETHTx: Error sending raw transaction: %v", err))
return
}
// Record the valid transaction in global state
ethTxLog := ÐTxLog{
PublicKey: pkBytes,
}
// Convert the result from interface to string.
hash := response.Result.(string)
globalStateKey := GlobalStateKeyETHPurchases(hash)
globalStateVal := bytes.NewBuffer([]byte{})
if err = gob.NewEncoder(globalStateVal).Encode(ethTxLog); err != nil {
_AddBadRequestError(ww, fmt.Sprintf("SubmitETHTx: Failed to encode ETH transaction: %v", err))
return
}
if err = fes.GlobalState.Put(globalStateKey, globalStateVal.Bytes()); err != nil {
_AddBadRequestError(ww, fmt.Sprintf("SubmitETHTx: Error processing Put: %v", err))
return
}
// Wait up to 10 minutes
// TODO: Long running requests are bad. Replace this with polling (or websockets etc)
var ethTx *InfuraTx
for i := 0; i < 60; i++ {
// Check if the transaction was mined every 10 seconds
time.Sleep(10 * time.Second)
ethTx, err = fes.GetETHTransactionByHash(hash)
if err != nil {
glog.Errorf("GetETHTransactionByHash: %v", err)
continue
}
if ethTx == nil {
// Sometimes these requests can fail. Ignore the failure and keep polling
continue
}
// A block height means the transaction mined
if ethTx.BlockNumber != nil {
break
}
}
// The transaction has mined or we've waited for 10 minutes so we finish by validating the transaction and paying the user.
desoTxHash, err := fes.finishETHTx(ethTx, ethTxLog)
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf("SubmitETHTx: Failed: %v", err))
return
}
res := SubmitETHTxResponse{
DESOTxHash: desoTxHash.String(),
}
if err = json.NewEncoder(ww).Encode(res); err != nil {
_AddBadRequestError(ww, fmt.Sprintf("SubmitETHTx: Problem encoding response: %v", err))
return
}
}
// 1. Validate the transaction mined and sends money to the correct address
// 2. Calculate the nanos to send
// 3. Send the nanos
// 4. Record the successful send
func (fes *APIServer) finishETHTx(ethTx *InfuraTx, ethTxLog *ETHTxLog) (desoTxHash *lib.BlockHash, _err error) {
if ethTx == nil {
return nil, errors.New("ETHTx provided is nil")
}
glog.Info("finishETHTx - ETH tx provided: ", spew.Sdump(ethTx))
if ethTx.BlockNumber == nil {
return nil, errors.New(fmt.Sprintf("Transaction failed to mine: %v", ethTx.Hash))
}
if err := fes.validateETHDepositAddress(*ethTx.To); err != nil {
return nil, errors.New(fmt.Sprintf("Error validating Infura ETH Tx: %v", err))
}
nanosPurchased, err := fes.CalculateNanosPurchasedFromWei(ethTx.Value)
if err != nil {
return nil, errors.New(fmt.Sprintf("finishETHTx: Error calculating NanosPurchasedFromWei: %v", err))
}
var balanceInsufficient bool
balanceInsufficient, err = fes.ExceedsDeSoBalance(nanosPurchased, fes.Config.BuyDESOSeed)
if err != nil {
return nil, errors.New(fmt.Sprintf("finishETHTx: Error checking if send deso balance is sufficient: %v", err))
}
if balanceInsufficient {
return nil, errors.New("finishETHTx: SendDeSo wallet balance is below nanos purchased")
}
// Send the DESO and get the hash of that transaction
desoTxHash, err = fes.SendSeedDeSo(ethTxLog.PublicKey, nanosPurchased, true)
if err != nil {
return nil, errors.New(fmt.Sprintf("Error sending DESO: %v", err))
}
// Record successful transaction in global state
ethTxLog.DESOTxHash = desoTxHash.String()
globalStateKey := GlobalStateKeyETHPurchases(ethTx.Hash)
globalStateVal := bytes.NewBuffer([]byte{})
// Save the ethTxLog in global state
if err = gob.NewEncoder(globalStateVal).Encode(ethTxLog); err != nil {
return nil, errors.New(fmt.Sprintf("Failed to encode ETH transaction: %v", err))
}
if err = fes.GlobalState.Put(globalStateKey, globalStateVal.Bytes()); err != nil {
return nil, errors.New(fmt.Sprintf("Error processing Put: %v", err))
}
return desoTxHash, nil
}
func (fes *APIServer) CalculateNanosPurchasedFromWei(value string) (_nanosPurchased uint64, _err error) {
// Calculate nanos purchased
// Strip the 0x prefix from the value attribute and parse hex string to uint64
hexValueString := strings.Replace(value, "0x", "", -1)
weiSentBigint, success := big.NewInt(0).SetString(hexValueString, 16)
if !success {
return 0, errors.New(fmt.Sprintf("Failed to convert wei hex to uint64"))
}
// Use big number math to convert wei to eth and then compute DESO nanos purchased.
totalWei := big.NewFloat(0).SetInt(weiSentBigint)
totalEth := big.NewFloat(0).Quo(totalWei, big.NewFloat(1e18))
return fes.GetNanosFromETH(totalEth, fes.BuyDESOFeeBasisPoints), nil
}
type AdminProcessETHTxRequest struct {
ETHTxHash string
}
type AdminProcessETHTxResponse struct {
DESOTxHash string
}
func (fes *APIServer) AdminProcessETHTx(ww http.ResponseWriter, req *http.Request) {
decoder := json.NewDecoder(io.LimitReader(req.Body, MaxRequestBodySizeBytes))
requestData := AdminProcessETHTxRequest{}
if err := decoder.Decode(&requestData); err != nil {
_AddBadRequestError(ww, fmt.Sprintf("AdminProcessETHTx: Problem parsing request body: %v", err))
return
}
if !fes.IsConfiguredForETH() {
_AddBadRequestError(ww, "AdminProcessETHTx: Not configured for ETH")
return
}
// Fetch the log data from global state
globalStateKey := GlobalStateKeyETHPurchases(requestData.ETHTxHash)
globalStateLog, err := fes.GlobalState.Get(globalStateKey)
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf("AdminProcessETHTx: Error processing Get: %v", err))
return
}
ethTxLog := ÐTxLog{}
err = gob.NewDecoder(bytes.NewReader(globalStateLog)).Decode(ethTxLog)
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf("AdminProcessETHTx: Error decoding global state data: %v", err))
return
}
// Transaction must be unsuccessful
if len(ethTxLog.DESOTxHash) > 0 {
_AddBadRequestError(ww, fmt.Sprintf("AdminProcessETHTx: DESO was sent: %s", ethTxLog.DESOTxHash))
return
}
// Fetch the transaction
ethTx, err := fes.GetETHTransactionByHash(requestData.ETHTxHash)
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf("AdminProcessETHTx: Failed to get transaction: %v", err))
return
}
desoTxHash, err := fes.finishETHTx(ethTx, ethTxLog)
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf("AdminProcessETHTx: Failed: %v", err))
return
}
res := AdminProcessETHTxResponse{
DESOTxHash: desoTxHash.String(),
}
if err = json.NewEncoder(ww).Encode(res); err != nil {
_AddBadRequestError(ww, fmt.Sprintf("AdminProcessETHTx: Problem encoding response: %v", err))
return
}
}
// JSON RPC with Infura
type InfuraRequest struct {
JSONRPC string `json:"jsonrpc"`
Method string `json:"method"`
Params []interface{} `json:"params"`
Id uint64 `json:"id"`
}
type InfuraResponse struct {
Id uint64 `json:"id"`
JSONRPC string `json:"jsonrpc"`
Result interface{} `json:"result"`
Error struct {
Code float64 `json:"code"`
Message string `json:"message"`
} `json:"error"`
}
type InfuraTx struct {
BlockHash *string `json:"blockHash"`
BlockNumber *string `json:"blockNumber"`
From string `json:"from"`
Gas string `json:"gas"`
GasPrice string `json:"gasPrice"`
Hash string `json:"hash"`
Input string `json:"input"`
Nonce string `json:"nonce"`
To *string `json:"to"`
TransactionIndex *string `json:"transactionIndex"`
Value string `json:"value"`
V string `json:"v"`
R string `json:"r"`
S string `json:"s"`
}
type QueryETHRPCRequest struct {
Method string
Params []interface{}
}
// QueryETHRPC is an endpoint used to execute queries through Infura
func (fes *APIServer) QueryETHRPC(ww http.ResponseWriter, req *http.Request) {
decoder := json.NewDecoder(io.LimitReader(req.Body, MaxRequestBodySizeBytes))
requestData := QueryETHRPCRequest{}
if err := decoder.Decode(&requestData); err != nil {
_AddBadRequestError(ww, fmt.Sprintf("QueryETHRPC: Problem parsing request body: %v", err))
return
}
res, err := fes.ExecuteETHRPCRequest(requestData.Method, requestData.Params)
if err != nil {
_AddBadRequestError(ww, fmt.Sprintf("QueryETHRPC: Error executing request: %v", err))
return
}
if err = json.NewEncoder(ww).Encode(res); err != nil {
_AddBadRequestError(ww, fmt.Sprintf("QueryETHRPC: Problem encoding response: %v", err))
return
}
}
// ExecuteETHRPCRequest makes a request to Infura to fetch information about the Ethereum blockchain
func (fes *APIServer) ExecuteETHRPCRequest(method string, params []interface{}) (response *InfuraResponse, _err error) {
projectId := fes.Config.InfuraProjectID
URL := fmt.Sprintf("https://mainnet.infura.io/v3/%v", projectId)
if fes.Params.NetworkType == lib.NetworkType_TESTNET {
URL = fmt.Sprintf("https://ropsten.infura.io/v3/%v", projectId)
}
jsonData, err := json.Marshal(InfuraRequest{
JSONRPC: "2.0",
Method: method,
Params: params,
Id: 1,
})
if err != nil {
return nil, err
}
req, _ := http.NewRequest("POST", URL, bytes.NewBuffer(jsonData))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("ExecuteETHRPCRequest: Problem with HTTP request %s: %v", URL, err)
}
defer resp.Body.Close()
// Decode the response into the appropriate struct.
body, _ := ioutil.ReadAll(resp.Body)
var responseData *InfuraResponse
decoder := json.NewDecoder(bytes.NewReader(body))
if err = decoder.Decode(&responseData); err != nil {
return nil, fmt.Errorf("BlockCypherCreateETHTx: Problem decoding response JSON: %v, response: %v, error: %v", responseData, resp, err)
}
if len(responseData.Error.Message) > 0 {
return nil, fmt.Errorf("ExecuteETHRPCRequest: RPC Error: %v", responseData.Error.Message)
}
return responseData, nil
}
// GetETHTransactionByHash is a helper function to fetch transaction details and parse it into an InfuraTx struct
func (fes *APIServer) GetETHTransactionByHash(hash string) (_tx *InfuraTx, _err error) {
params := []interface{}{hash}
txRes, err := fes.ExecuteETHRPCRequest("eth_getTransactionByHash", params)
if err != nil {
return nil, err
}
var response *InfuraTx
if err = mapstructure.Decode(txRes.Result, &response); err != nil {
return nil, err
}
return response, nil
}