-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathfaucet.go
603 lines (485 loc) · 14.5 KB
/
faucet.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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
package main
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"regexp"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/dpapathanasiou/go-recaptcha"
"github.com/rs/cors"
"github.com/syndtr/goleveldb/leveldb"
"github.com/tendermint/tmlibs/bech32"
"github.com/tomasen/realip"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/go-bip39"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/cosmos/cosmos-sdk/x/auth/signing"
"github.com/terra-money/core/v2/app"
"github.com/terra-money/core/v2/app/params"
"github.com/PagerDuty/go-pagerduty"
//"github.com/tendermint/tendermint/crypto"
"github.com/cosmos/cosmos-sdk/crypto/hd"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
//"github.com/tendermint/tendermint/crypto/secp256k1"
"github.com/cosmos/cosmos-sdk/client"
txsigning "github.com/cosmos/cosmos-sdk/types/tx/signing"
)
var mnemonic string
var recaptchaKey string
var port string
var lcdURL string
var chainID string
var privKey cryptotypes.PrivKey
// var privKey crypto.PrivKey
var address string
var sequence uint64
var accountNumber uint64
var cdc *params.EncodingConfig
var mtx sync.Mutex
var isClassic bool
type PagerdutyConfig struct {
token string
user string
serviceID string
}
var pagerdutyConfig PagerdutyConfig
const ( // new core hasn't these yet.
MicroUnit = int64(1e6)
fullFundraiserPath = "m/44'/330'/0'/0/0"
accountAddresPrefix = "terra"
accountPubKeyPrefix = "terrapub"
validatorAddressPrefix = "terravaloper"
validatorPubKeyPrefix = "terravaloperpub"
consNodeAddressPrefix = "terravalcons"
consNodePubKeyPrefix = "terravalconspub"
)
var amountTable = map[string]int64{
//Increase the amount of the coin to be sent from 5 to 20
app.BondDenom: 20 * MicroUnit,
}
const (
requestLimitSecs = 30
mnemonicVar = "MNEMONIC"
recaptchaKeyVar = "RECAPTCHA_KEY"
portVar = "PORT"
lcdUrlVar = "LCD_URL"
chainIDVar = "CHAIN_ID"
pagerdutyTokenVar = "PAGERDUTY_TOKEN"
pagerdutyUserVar = "PAGERDUTY_USER"
pagerdutyServiceIDVar = "PAGERDUTY_SERVICE_ID"
)
// Claim wraps a faucet claim
type Claim struct {
Address string `json:"address"`
Response string `json:"response"`
Denom string `json:"denom"`
}
// Coin is the same as sdk.Coin
type Coin struct {
Denom string `json:"denom"`
Amount int64 `json:"amount"`
}
func newCodec() *params.EncodingConfig {
ec := app.MakeEncodingConfig()
config := sdk.GetConfig()
config.SetCoinType(app.CoinType)
config.SetFullFundraiserPath(fullFundraiserPath)
config.SetBech32PrefixForAccount(accountAddresPrefix, accountPubKeyPrefix)
config.SetBech32PrefixForValidator(validatorAddressPrefix, validatorPubKeyPrefix)
config.SetBech32PrefixForConsensusNode(consNodeAddressPrefix, consNodePubKeyPrefix)
config.Seal()
return &ec
}
func loadAccountInfo() {
// Query current faucet sequence
url := fmt.Sprintf("%v/cosmos/auth/v1beta1/accounts/%v", lcdURL, address)
response, err := http.Get(url)
if err != nil {
fmt.Println(err.Error())
return
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Println(err.Error())
return
}
bodyStr := string(body)
var seq uint64
if strings.Contains(bodyStr, `"sequence"`) {
seq, _ = strconv.ParseUint(parseRegexp(`"sequence": ?"?(\d+)"?`, bodyStr), 10, 64)
} else {
seq = 0
}
sequence = atomic.LoadUint64(&seq)
if strings.Contains(bodyStr, `"account_number"`) {
accountNumber, _ = strconv.ParseUint(parseRegexp(`"account_number": ?"?(\d+)"?`, bodyStr), 10, 64)
} else {
accountNumber = 0
}
fmt.Printf("loadAccountInfo: address %v account# %v sequence %v\n", address, accountNumber, sequence)
}
type CoreCoin struct {
Denom string `json:"denom"`
Amount string `json:"amount"`
}
type BalanceResponse struct {
Balance CoreCoin `json:"balance"`
}
func getBalance(address string) (amount int64) {
url := fmt.Sprintf("%v/cosmos/bank/v1beta1/balances/%v/by_denom?denom=uluna", lcdURL, address)
response, err := http.Get(url)
if err != nil {
panic(err)
}
var res BalanceResponse
decoder := json.NewDecoder(response.Body)
decoderErr := decoder.Decode(&res)
if decoderErr != nil {
panic(decoderErr)
}
i, _ := strconv.ParseInt(res.Balance.Amount, 10, 64)
return i
}
func parseRegexp(regexpStr string, target string) (data string) {
// Capture seqeunce string from json
r := regexp.MustCompile(regexpStr)
groups := r.FindStringSubmatch(string(target))
if len(groups) != 2 {
panic(fmt.Errorf("regex did not match %s, %s", regexpStr, target))
}
// Convert sequence string to int64
data = groups[1]
return
}
// RequestLog stores the Log of a Request
type RequestLog struct {
Coins []Coin `json:"coin"`
Requested time.Time `json:"updated"`
}
func (requestLog *RequestLog) dripCoin(denom string) error {
amount := amountTable[denom]
// try to update coin
for idx, coin := range requestLog.Coins {
if coin.Denom == denom {
if (requestLog.Coins[idx].Amount + amount) > amountTable[denom]*5 {
return fmt.Errorf("amount limit exceeded: account is not eligible for additional faucet drops as it has already received the maximum monthly amount of %v", amountTable[denom]*5)
}
requestLog.Coins[idx].Amount += amount
return nil
}
}
// first drip for denom
requestLog.Coins = append(requestLog.Coins, Coin{Denom: denom, Amount: amount})
return nil
}
func checkAndUpdateLimit(db *leveldb.DB, account []byte, denom string) error {
address, _ := bech32.ConvertAndEncode("terra", account)
if getBalance(address) >= amountTable[denom]*5 {
return fmt.Errorf("amount limit exceeded: account %v has a balance exceeding the maximum allowed amount of %v and cannot receive additional faucet drops", address, amountTable[denom]*5)
}
var requestLog RequestLog
logBytes, _ := db.Get(account, nil)
now := time.Now()
if logBytes != nil {
jsonErr := json.Unmarshal(logBytes, &requestLog)
if jsonErr != nil {
return jsonErr
}
// check interval limt
intervalSecs := now.Sub(requestLog.Requested).Seconds()
if intervalSecs < requestLimitSecs {
return errors.New("please wait a while for another tap")
}
// reset log if month was changed
if requestLog.Requested.Month() != now.Month() {
requestLog.Coins = []Coin{}
}
// check amount limit
dripErr := requestLog.dripCoin(denom)
if dripErr != nil {
return dripErr
}
}
// update requested time
requestLog.Requested = now
logBytes, _ = json.Marshal(requestLog)
updateErr := db.Put(account, logBytes, nil)
if updateErr != nil {
return updateErr
}
return nil
}
func drip(encodedAddress string, denom string, amount int64, isDetectMismatch bool) string {
builder := app.MakeEncodingConfig().TxConfig.NewTxBuilder()
builder.SetFeeAmount(sdk.NewCoins(sdk.NewCoin(app.BondDenom, sdk.NewInt(1_000_000))))
builder.SetGasLimit(150_000)
builder.SetMemo("faucet")
builder.SetTimeoutHeight(0)
coins := sdk.NewCoins(sdk.NewCoin(denom, sdk.NewInt(amount)))
from, err := sdk.AccAddressFromBech32(address)
if err != nil {
panic("invalid from address")
}
to, err := sdk.AccAddressFromBech32(encodedAddress)
if err != nil {
panic("invalid to address")
}
sendMsg := banktypes.NewMsgSend(from, to, coins)
builder.SetMsgs(sendMsg)
return signAndBroadcast(builder, isDetectMismatch)
}
func createGetCoinsHandler(db *leveldb.DB) http.HandlerFunc {
return func(w http.ResponseWriter, request *http.Request) {
defer func() {
if err := recover(); err != nil {
http.Error(w, err.(error).Error(), 400)
}
}()
var claim Claim
// decode JSON response from front end
decoder := json.NewDecoder(request.Body)
decoderErr := decoder.Decode(&claim)
if decoderErr != nil {
panic(decoderErr)
}
amount, ok := amountTable[claim.Denom]
if !ok {
panic(fmt.Errorf("invalid denom; %v", claim.Denom))
}
// make sure address is bech32
readableAddress, decodedAddress, decodeErr := bech32.DecodeAndConvert(claim.Address)
if decodeErr != nil {
panic(decodeErr)
}
// re-encode the address in bech32
encodedAddress, encodeErr := bech32.ConvertAndEncode(readableAddress, decodedAddress)
if encodeErr != nil {
panic(encodeErr)
}
// make sure captcha is valid
clientIP := realip.FromRequest(request)
captchaResponse := claim.Response
captchaPassed, captchaErr := recaptcha.Confirm(clientIP, captchaResponse)
if captchaErr != nil {
panic(captchaErr)
}
// Limiting request speed
limitErr := checkAndUpdateLimit(db, decodedAddress, claim.Denom)
if limitErr != nil {
panic(limitErr)
}
// send the coins!
if captchaPassed {
mtx.Lock()
defer mtx.Unlock()
fmt.Println(time.Now().UTC().Format(time.RFC3339), "req", clientIP, encodedAddress, amount, claim.Denom)
body := drip(encodedAddress, claim.Denom, amount, true)
// Sequence mismatch if the body length is zero
if len(body) == 0 {
// Reload for self healing and re-drip
loadAccountInfo()
body = drip(encodedAddress, claim.Denom, amount, true)
// Another try without loading....
if len(body) == 0 {
atomic.AddUint64(&sequence, 1)
body = drip(encodedAddress, claim.Denom, amount, false)
}
}
if len(body) != 0 {
atomic.AddUint64(&sequence, 1)
}
fmt.Printf("%v seq %v %v\n", time.Now().UTC().Format(time.RFC3339), sequence, body)
// Create an incident for broadcast error
if !strings.Contains(body, "\"code\": 0") {
createIncident(body)
}
w.Header().Set("Content-Type", "application/json")
fmt.Fprintf(w, `{"amount": %v, "response": %v}`, amount, body)
} else {
err := errors.New("captcha failed, please refresh page and try again")
panic(err)
}
}
}
// BroadcastReq defines a tx broadcasting request.
type BroadcastReq struct {
Tx string `json:"tx_bytes"`
Mode string `json:"mode"`
}
func signAndBroadcast(txBuilder client.TxBuilder, isDetectMismatch bool) string {
// load account info to update sequence number
loadAccountInfo()
var broadcastReq BroadcastReq
pubKey := privKey.PubKey()
// no need to sort amount because there's only one amount.
// create signature v2
signMode := cdc.TxConfig.SignModeHandler().DefaultMode()
signerData := signing.SignerData{
ChainID: chainID,
AccountNumber: accountNumber,
Sequence: sequence,
}
sigData := txsigning.SingleSignatureData{
SignMode: signMode,
Signature: nil,
}
sigv2 := txsigning.SignatureV2{
PubKey: pubKey,
Data: &sigData,
Sequence: sequence,
}
txBuilder.SetSignatures(sigv2)
bytesToSign, err := cdc.TxConfig.SignModeHandler().GetSignBytes(signMode, signerData, txBuilder.GetTx())
if err != nil {
panic(err)
}
sig, err := privKey.Sign(bytesToSign)
if err != nil {
panic(err)
}
sigData = txsigning.SingleSignatureData{
SignMode: signMode,
Signature: sig,
}
sigv2 = txsigning.SignatureV2{
PubKey: pubKey,
Data: &sigData,
Sequence: sequence,
}
txBuilder.SetSignatures(sigv2)
// encode signed tx
bz, err := cdc.TxConfig.TxEncoder()(txBuilder.GetTx())
if err != nil {
panic(err)
}
// prepare to broacast
broadcastReq.Tx = base64.StdEncoding.EncodeToString(bz)
broadcastReq.Mode = "BROADCAST_MODE_SYNC"
txBz, err := json.Marshal(broadcastReq)
if err != nil {
panic(err)
}
// broadcast
url := fmt.Sprintf("%v/cosmos/tx/v1beta1/txs", lcdURL)
response, err := http.Post(url, "application/json", bytes.NewReader(txBz))
if err != nil {
panic(err)
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
panic(err)
}
stringBody := string(body)
if response.StatusCode != 200 {
err := fmt.Errorf("status: %v, message: %v", response.Status, stringBody)
panic(err)
}
code, err := strconv.ParseUint(parseRegexp(`"code": ?"?(\d+)"?`, stringBody), 10, 64)
if err != nil {
panic("failed to parse code from tx response")
}
if code != 0 {
panic(parseRegexp(`"raw_log": ?"?(\d+)"?`, stringBody))
}
if isDetectMismatch && strings.Contains(stringBody, "sequence mismatch") {
return ""
}
return stringBody
}
func createIncident(body string) (*pagerduty.Incident, error) {
client := pagerduty.NewClient(pagerdutyConfig.token)
input := &pagerduty.CreateIncidentOptions{
Title: "Faucet had an error",
Urgency: "low",
Service: &pagerduty.APIReference{
ID: pagerdutyConfig.serviceID,
Type: "service",
},
Body: &pagerduty.APIDetails{
Details: body,
},
}
return client.CreateIncidentWithContext(context.Background(), pagerdutyConfig.user, input)
}
func main() {
mnemonic = os.Getenv(mnemonicVar)
if mnemonic == "" {
panic("MNEMONIC variable is required")
}
if !bip39.IsMnemonicValid(mnemonic) {
panic("invalid mnemonic")
}
recaptchaKey = os.Getenv(recaptchaKeyVar)
if recaptchaKey == "" {
panic("RECAPTCHA_KEY variable is required")
}
port = os.Getenv(portVar)
if port == "" {
port = "3000"
}
lcdURL = os.Getenv(lcdUrlVar)
if lcdURL == "" {
panic("LCD_URL variable is required")
}
chainID = os.Getenv(chainIDVar)
if chainID == "" {
panic("CHAIN_ID variable is required")
}
if strings.HasPrefix(chainID, "bombay") {
isClassic = true
} else {
isClassic = false
}
pagerdutyConfig.token = os.Getenv(pagerdutyTokenVar)
pagerdutyConfig.user = os.Getenv(pagerdutyUserVar)
pagerdutyConfig.serviceID = os.Getenv(pagerdutyServiceIDVar)
if pagerdutyConfig.token == "" || pagerdutyConfig.user == "" || pagerdutyConfig.serviceID == "" {
panic("PAGERDUTY_TOKEN, PAGERDUTY_USER, and PAGERDUTY_SERVICE_ID variables are required")
}
db, err := leveldb.OpenFile("db/ipdb", nil)
if err != nil {
panic(err)
}
defer db.Close()
cdc = newCodec()
derivedPriv, err := hd.Secp256k1.Derive()(mnemonic, "", fullFundraiserPath)
if err != nil {
panic(err)
}
//privKey = *secp256k1.GenPrivKeyFromSecret(derivedPriv)
privKey = hd.Secp256k1.Generate()(derivedPriv)
pubk := privKey.PubKey()
address, err = bech32.ConvertAndEncode("terra", pubk.Address())
if err != nil {
panic(err)
}
// Load account number and sequence
loadAccountInfo()
recaptcha.Init(recaptchaKey)
// Application server.
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
})
mux.HandleFunc("/claim", createGetCoinsHandler(db))
c := cors.New(cors.Options{
AllowedOrigins: []string{"https://faucet.terra.money", "http://localhost", "localhost", "http://localhost:3000", "http://localhost:8080"},
AllowCredentials: true,
})
handler := c.Handler(mux)
if err := http.ListenAndServe(fmt.Sprintf(":%s", port), handler); err != nil {
log.Fatal("failed to start server", err)
}
}