-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
159 lines (150 loc) · 3.96 KB
/
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
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
package main
import (
"context"
"encoding/json"
"log"
"net/http"
"github.com/aws/aws-lambda-go/events"
"github.com/stellar/go/txnbuild"
"github.com/stellar/go/xdr"
)
type request struct {
Data string `json:"data"`
}
var headers = map[string]string{
"Content-Type": "text/plain",
}
const (
ErrorInvalidRequest = "Invalid request"
ErrorInvalidXDR = "Invalid XDR"
ErrorSourceMismatch = "Source account does not match"
ErrorSponsorship = "Error beginning sponsorship"
ErrorSubmitting = "Error submitting transaction"
ErrorMethodNotAllowed = "Method Not Allowed"
ErrorNotFound = "Not Found"
)
type route struct {
Handler func(ctx context.Context, req events.LambdaFunctionURLRequest) events.LambdaFunctionURLResponse
}
var routes = map[string]map[string]route{
"/": {
http.MethodPost: {handlePost},
},
}
func handleRequest(ctx context.Context, req events.LambdaFunctionURLRequest) (events.LambdaFunctionURLResponse, error) {
if methodRoutes, ok := routes[req.RawPath]; ok {
if route, ok := methodRoutes[req.RequestContext.HTTP.Method]; ok {
return route.Handler(ctx, req), nil
}
}
return events.LambdaFunctionURLResponse{
StatusCode: 404,
Body: ErrorNotFound,
Headers: headers,
}, nil
}
func handlePost(ctx context.Context, req events.LambdaFunctionURLRequest) events.LambdaFunctionURLResponse {
var body request
if err := json.Unmarshal([]byte(req.Body), &body); err != nil {
log.Printf("error unmarshalling request: %v", err)
return events.LambdaFunctionURLResponse{
StatusCode: 400,
Body: ErrorInvalidRequest,
Headers: headers,
}
}
xdrs, err := txnbuild.TransactionFromXDR(body.Data)
if err != nil {
log.Printf("error unmarshalling xdr: %v", err)
return events.LambdaFunctionURLResponse{
StatusCode: 400,
Body: ErrorInvalidXDR,
Headers: headers,
}
}
txXDR, ok := xdrs.Transaction()
if !ok {
return events.LambdaFunctionURLResponse{
StatusCode: 400,
Body: ErrorInvalidXDR,
Headers: headers,
}
}
if txXDR.SourceAccount().AccountID != key.Address() {
return events.LambdaFunctionURLResponse{
StatusCode: 400,
Body: ErrorSourceMismatch,
Headers: headers,
}
}
if len(txXDR.Operations()) != 3 {
return events.LambdaFunctionURLResponse{
StatusCode: 400,
Body: ErrorInvalidXDR,
Headers: headers,
}
}
for _, op := range txXDR.Operations() {
xdrOp, err := op.BuildXDR()
if err != nil {
log.Printf("error building xdr: %v", err)
return events.LambdaFunctionURLResponse{
StatusCode: 400,
Body: ErrorInvalidXDR,
Headers: headers,
}
}
switch xdrOp.Body.Type {
case xdr.OperationTypeCreateAccount:
if xdrOp.Body.CreateAccountOp.StartingBalance != 0 {
return events.LambdaFunctionURLResponse{
StatusCode: 400,
Body: ErrorInvalidXDR,
Headers: headers,
}
}
case xdr.OperationTypeBeginSponsoringFutureReserves:
continue
case xdr.OperationTypeEndSponsoringFutureReserves:
continue
default:
return events.LambdaFunctionURLResponse{
StatusCode: 400,
Body: ErrorInvalidXDR,
Headers: headers,
}
}
}
signedXDR, err := txXDR.Sign(networkPassphrase, key)
if err != nil {
log.Printf("error signing xdr: %v", err)
return events.LambdaFunctionURLResponse{
StatusCode: 500,
Body: ErrorSponsorship,
Headers: headers,
}
}
xdrTxBase64, err := signedXDR.Base64()
if err != nil {
log.Printf("error encoding xdr: %v", err)
return events.LambdaFunctionURLResponse{
StatusCode: 500,
Body: ErrorSponsorship,
Headers: headers,
}
}
res, err := sorobanClient.SubmitTransactionXDR(ctx, xdrTxBase64)
if err != nil {
log.Printf("error submitting transaction: %v", err)
return events.LambdaFunctionURLResponse{
StatusCode: 500,
Body: ErrorSubmitting,
Headers: headers,
}
}
return events.LambdaFunctionURLResponse{
StatusCode: 200,
Body: res.Hash,
Headers: headers,
}
}