-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhandlers.go
283 lines (232 loc) · 7.68 KB
/
handlers.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
package main
import (
"encoding/json"
"fmt"
"log"
"math/big"
"net/http"
"strconv"
)
func Respond(w http.ResponseWriter, code int, payload interface{}) {
ret := make(map[string]interface{})
if code >= 200 && code < 300 {
ret["result"] = "success"
} else {
ret["result"] = "failure"
}
if payload != nil {
ret["response"] = payload
}
response, _ := json.Marshal(ret)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
w.Write(response)
}
func RespondWithError(w http.ResponseWriter, code int, msg string) {
Respond(w, code, map[string]string{"error": msg})
}
func NotFoundHandler(w http.ResponseWriter, r *http.Request) {
log.Printf("404: %s %s", r.Method, r.URL)
RespondWithError(w, 404, "Not found")
}
func CreateAddressHandler(config *Config, db *DB) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
with_private := r.URL.Query().Get("with_private")
pub, priv, err := CreateAddress()
if err != nil {
RespondWithError(w, 500, fmt.Sprintf("Could not create a new key: %v", err))
return
}
err = db.InsertKey(pub, priv)
if err != nil {
RespondWithError(w, 500, fmt.Sprintf("Could not save newly created key: %v", err))
return
}
log.Printf("Created address: %v", pub)
if with_private == "true" {
Respond(w, 200, map[string]string{"address": pub, "private": priv})
} else {
Respond(w, 200, map[string]string{"address": pub})
}
}
}
func RegisterAddressHandler(config *Config, db *DB) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
log.Printf("RegisterAddressHandler: Could not parse body parameters")
RespondWithError(w, 400, "Could not parse parameters")
return
}
address := r.Form.Get("address")
private := r.Form.Get("private")
if false == IsAddress(address) {
log.Printf("Invalid 'address' field: Not an hex address")
RespondWithError(w, 400, "Invalid 'address' field: Not an hex address")
return
}
if private != "" {
addressVerify, err := PrivateHexToAddress(private)
if err != nil {
log.Printf("Invalid 'private' field: Could not transform to private key")
RespondWithError(w, 400, "Invalid 'private' field: Could not transform to private key")
return
}
if address != addressVerify {
log.Printf("Given 'address' and 'private' key doesn't match.")
RespondWithError(w, 400, "Given 'address' and 'private' key doesn't match.")
return
}
}
// InsertKey will UPSERT.
err = db.InsertKey(address, private)
if err != nil {
RespondWithError(w, 500, fmt.Sprintf("Could not save newly created key: %v", err))
return
}
Respond(w, 200, map[string]string{"message": "Address saved in database"})
}
}
func GetBalanceHandler(config *Config) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
var balance *big.Int
var err error
address := r.URL.Query().Get("address")
contractAddress := r.URL.Query().Get("contract")
if address == "" {
RespondWithError(w, 400, "Missing 'address' field")
return
}
if contractAddress == "" {
// Retrieve ETH balance
balanceFloat, err := GetAddressBalance(config, address)
if err != nil {
RespondWithError(w, 500, fmt.Sprintf("Could not retrieve ethereum balance: %v", err))
return
}
Respond(w, 200, map[string]string{"balance": balanceFloat.Text('f', 10)})
} else {
// Retrieve erc20 balance for given address
balance, err = GetERC20AddressBalance(config, address, contractAddress)
if err != nil {
RespondWithError(w, 500, fmt.Sprintf("Could not retrieve ethereum balance: %v", err))
return
}
Respond(w, 200, map[string]string{"balance": balance.Text(10)})
}
}
}
func SendEthHandler(config *Config) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
log.Printf("SendEthHandler: Could not parse body parameters")
RespondWithError(w, 400, "Could not parse parameters")
return
}
address := r.Form.Get("address")
private := r.Form.Get("private")
amount := r.Form.Get("amount")
if address == "" {
log.Printf("Got Send Ethereum order but 'address' field is missing")
RespondWithError(w, 400, "Missing 'address' field")
return
}
if private == "" {
log.Printf("Got Send Ethereum order but 'private' field is missing")
RespondWithError(w, 400, "Missing 'private' field")
return
}
if amount == "" {
log.Printf("Got Send Ethereum order but 'amount' field is missing")
RespondWithError(w, 400, "Missing 'amount' field")
return
}
f, err := strconv.ParseFloat(amount, 64)
if err != nil {
RespondWithError(w, 400, "Could not convert amount")
return
}
bgAmount := new(big.Float)
bgAmount.SetFloat64(f)
bgEthWei := new(big.Float)
bgEthWei.SetString("1000000000000000000")
bgAmount = bgAmount.Mul(bgAmount, bgEthWei)
bgAmountInt, _ := bgAmount.Int(new(big.Int))
tx, err := SendEthCoin(config, bgAmountInt, private, address)
if err != nil {
RespondWithError(w, 500, fmt.Sprintf("Could not send Ethereum coin: %v", err))
return
}
Respond(w, 200, map[string]string{"txhash": tx})
}
}
func SendERC20Handler(config *Config, db *DB) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
log.Printf("SendERC20Handler: Could not parse body parameters")
RespondWithError(w, 400, "Could not parse parameters")
return
}
address := r.Form.Get("address")
addressFrom := r.Form.Get("address_from")
contract := r.Form.Get("contract")
private := r.Form.Get("private")
amount := r.Form.Get("amount")
if address == "" {
log.Printf("Got Send Ethereum order but 'address' field is missing")
RespondWithError(w, 400, "Missing 'address' field")
return
}
if contract == "" {
log.Printf("Got Send Ethereum order but 'contract' field is missing")
RespondWithError(w, 400, "Missing 'contract' field")
return
}
if private == "" || addressFrom == "" {
log.Printf("Got Send Ethereum order but 'private' and 'address_from' fields are missing")
RespondWithError(w, 400, "'address_from' and'private' fields are both missing. At least one is mandatory ")
return
}
if amount == "" {
log.Printf("Got Send Ethereum order but 'amount' field is missing")
RespondWithError(w, 400, "Missing 'amount' field")
return
}
if private == "" {
private, err := db.GetKey(addressFrom)
if err != nil {
log.Printf("Could not retrieve the address_from private key: %v", err)
RespondWithError(w, 400, fmt.Sprintf("Error while retrieving the private key: %v", err))
return
}
if private == "" {
log.Printf("Private key of given address_from is not known.")
RespondWithError(w, 400, fmt.Sprintf("Unknown private key for %s", addressFrom))
return
}
}
bgAmount := new(big.Int)
bgAmount.UnmarshalText([]byte(amount))
tx, err := SendERC20Token(config, bgAmount, contract, private, address)
if err != nil {
RespondWithError(w, 500, fmt.Sprintf("Could not send ERC20 token: %v", err))
return
}
Respond(w, 200, map[string]string{"txhash": tx})
}
}
func GetNotificationsHandler(config *Config, db *DB) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
remove := r.URL.Query().Get("remove")
// Retrieve next 100 records in database
notifications, err := db.GetNotifications(remove == "true")
if err != nil {
log.Printf("GetNotificationsHandler: %v", err)
RespondWithError(w, 500, "Could retrieve notifications")
return
}
Respond(w, 200, notifications)
}
}