forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
friendbot_handler.go
82 lines (68 loc) · 1.98 KB
/
friendbot_handler.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
package internal
import (
"net/http"
"net/url"
"github.com/stellar/go/clients/horizon"
"github.com/stellar/go/strkey"
"github.com/stellar/go/support/render/hal"
"github.com/stellar/go/support/render/problem"
)
// FriendbotHandler causes an account at `Address` to be created.
type FriendbotHandler struct {
Friendbot *Bot
}
// Handle is a method that implements http.HandlerFunc
func (handler *FriendbotHandler) Handle(w http.ResponseWriter, r *http.Request) {
result, err := handler.doHandle(r)
if err != nil {
problem.Render(r.Context(), w, err)
return
}
hal.Render(w, *result)
}
// doHandle is just a convenience method that returns the object to be rendered
func (handler *FriendbotHandler) doHandle(r *http.Request) (*horizon.TransactionSuccess, error) {
err := handler.checkEnabled()
if err != nil {
return nil, err
}
err = r.ParseForm()
if err != nil {
return nil, err
}
address, err := handler.loadAddress(r)
if err != nil {
return nil, problem.MakeInvalidFieldProblem("addr", err)
}
return handler.loadResult(address)
}
func (handler *FriendbotHandler) checkEnabled() error {
if handler.Friendbot != nil {
return nil
}
return &problem.P{
Type: "friendbot_disabled",
Title: "Friendbot is disabled",
Status: http.StatusForbidden,
Detail: "Friendbot is disabled on this network. Contact the server administrator if you believe this to be in error.",
}
}
func (handler *FriendbotHandler) loadAddress(r *http.Request) (string, error) {
address := r.Form.Get("addr")
unescaped, err := url.QueryUnescape(address)
if err != nil {
return unescaped, err
}
_, err = strkey.Decode(strkey.VersionByteAccountID, unescaped)
return unescaped, err
}
func (handler *FriendbotHandler) loadResult(address string) (*horizon.TransactionSuccess, error) {
result, err := handler.Friendbot.Pay(address)
switch e := err.(type) {
case horizon.Error:
return result, e.Problem.ToProblem()
case *horizon.Error:
return result, e.Problem.ToProblem()
}
return result, err
}