This repository has been archived by the owner on Oct 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
116 lines (96 loc) · 2.79 KB
/
main.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
package main
import (
"encoding/json"
"flag"
"log"
"net/http"
"os"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
hub "github.com/sentinel-official/hub/types"
"github.com/sentinel-official/faucet/types"
"github.com/sentinel-official/faucet/utils"
)
var (
chainID string
rpcAddress string
from string
password string
coins string
secret = os.Getenv("RECAPTCHA_SECRET")
_cli *types.CLI
)
func init() {
flag.StringVar(&chainID, "chain-id", "sentinel-turing-2", "chain id")
flag.StringVar(&rpcAddress, "rpc-address", "127.0.0.1:26657", "rpc server address")
flag.StringVar(&from, "from", "faucet", "from account name")
flag.StringVar(&password, "password", "", "from account password")
flag.StringVar(&coins, "coins", "100000000tsent", "coins to transfer")
flag.Parse()
}
type transferRequest struct {
Address string `json:"address"`
ReCaptchaResponse string `json:"re_captcha_response"`
}
func transferHandler(w http.ResponseWriter, r *http.Request) {
var body transferRequest
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
utils.WriteErrorToResponse(w, 400, &types.Error{
Message: "failed to unmarshal the body",
Info: err.Error(),
})
return
}
if body.Address == "" {
utils.WriteErrorToResponse(w, 400, &types.Error{
Message: "address field is empty",
})
return
}
if body.ReCaptchaResponse == "" {
utils.WriteErrorToResponse(w, 400, &types.Error{
Message: "re_captcha_response field is empty",
})
return
}
if err := utils.ReCaptchaVerify(secret, body.ReCaptchaResponse, ""); err != nil {
utils.WriteErrorToResponse(w, 400, &types.Error{
Message: "failed to verify",
Info: err.Error(),
})
return
}
txRes, err := _cli.Transfer(body.Address, coins)
if err != nil {
utils.WriteErrorToResponse(w, 500, &types.Error{
Message: "failed to transfer the coins",
Info: err.Error(),
})
return
}
w.Header().Add("Content-Type", "application/json")
utils.WriteResultToResponse(w, 200, txRes)
}
func main() {
var err error
cfg := sdk.GetConfig()
cfg.SetBech32PrefixForAccount(hub.Bech32PrefixAccAddr, hub.Bech32PrefixAccPub)
cfg.SetBech32PrefixForValidator(hub.Bech32PrefixValAddr, hub.Bech32PrefixValPub)
cfg.SetBech32PrefixForConsensusNode(hub.Bech32PrefixConsAddr, hub.Bech32PrefixConsPub)
cfg.Seal()
_cli, err = types.NewCLI(chainID, rpcAddress, from, password)
if err != nil {
panic(err)
}
router := mux.NewRouter()
router.Name("Transfer").
Methods("POST").
Path("/transfer").
HandlerFunc(transferHandler)
cors := handlers.CORS(
handlers.AllowedHeaders([]string{"Content-Type"}),
handlers.AllowedMethods([]string{"POST", "OPTIONS"}),
handlers.AllowedOrigins([]string{"*"}))
log.Fatal(http.ListenAndServe(":8000", cors(router)))
}