/
request_handler_authorize.go
88 lines (74 loc) · 2.18 KB
/
request_handler_authorize.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
package handlers
import (
"encoding/json"
"net/http"
log "github.com/sirupsen/logrus"
hc "github.com/stellar/go/clients/horizonclient"
"github.com/stellar/go/keypair"
"github.com/stellar/go/services/internal/bridge-compliance-shared/http/helpers"
"github.com/stellar/go/services/internal/bridge-compliance-shared/protocols/bridge"
"github.com/stellar/go/txnbuild"
)
// Authorize implements /authorize endpoint
func (rh *RequestHandler) Authorize(w http.ResponseWriter, r *http.Request) {
request := &bridge.AuthorizeRequest{}
err := helpers.FromRequest(r, request)
if err != nil {
log.Error(err.Error())
helpers.Write(w, helpers.InvalidParameterError)
return
}
err = helpers.Validate(request)
if err != nil {
switch err := err.(type) {
case *helpers.ErrorResponse:
helpers.Write(w, err)
default:
log.Error(err)
helpers.Write(w, helpers.InternalServerError)
}
return
}
kp, err := keypair.Parse(rh.Config.Accounts.AuthorizingSeed)
if err != nil {
log.WithFields(log.Fields{"err": err}).Error("Error parsing authorizing seed")
helpers.Write(w, helpers.InternalServerError)
}
sourceAccount := kp.Address()
allowTrustOp := bridge.AllowTrustOperationBody{
Source: &sourceAccount,
Authorize: true,
Trustor: request.AccountID,
AssetCode: request.AssetCode,
}
operationBuilder := allowTrustOp.Build()
submitResponse, err := rh.TransactionSubmitter.SubmitTransaction(
nil,
rh.Config.Accounts.AuthorizingSeed,
[]txnbuild.Operation{operationBuilder},
nil,
)
jsonEncoder := json.NewEncoder(w)
if err != nil {
herr, isHorizonError := err.(*hc.Error)
if !isHorizonError {
log.WithFields(log.Fields{"err": err}).Error("Error submitting transaction")
helpers.Write(w, helpers.InternalServerError)
return
}
w.WriteHeader(herr.Problem.Status)
err = jsonEncoder.Encode(herr.Problem)
if err != nil {
log.WithFields(log.Fields{"err": err}).Error("Error encoding response")
helpers.Write(w, helpers.InternalServerError)
return
}
return
}
err = jsonEncoder.Encode(submitResponse)
if err != nil {
log.WithFields(log.Fields{"err": err}).Error("Error encoding response")
helpers.Write(w, helpers.InternalServerError)
return
}
}