-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtss_handler.go
173 lines (154 loc) · 5.09 KB
/
tss_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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package httphandler
import (
"fmt"
"net/http"
"github.com/stellar/go/support/log"
"github.com/stellar/go/support/render/httpjson"
"github.com/stellar/go/txnbuild"
"github.com/stellar/wallet-backend/internal/apptracker"
"github.com/stellar/wallet-backend/internal/serve/httperror"
"github.com/stellar/wallet-backend/internal/tss"
"github.com/stellar/wallet-backend/internal/tss/router"
tssservices "github.com/stellar/wallet-backend/internal/tss/services"
"github.com/stellar/wallet-backend/internal/tss/store"
"github.com/stellar/wallet-backend/internal/tss/utils"
)
type TSSHandler struct {
Router router.Router
Store store.Store
AppTracker apptracker.AppTracker
NetworkPassphrase string
TransactionService tssservices.TransactionService
}
type Transaction struct {
Operations []string `json:"operations" validate:"required"`
TimeBounds int64 `json:"timebounds" validate:"required"`
}
type BuildTransactionsRequest struct {
Transactions []Transaction `json:"transactions" validate:"required,gt=0"`
}
type BuildTransactionsResponse struct {
TransactionXDRs []string `json:"transactionxdrs"`
}
type TransactionSubmissionRequest struct {
WebhookURL string `json:"webhook" validate:"required"`
Transactions []string `json:"transactions" validate:"required,gt=0"`
FeeBump bool `json:"feebump"`
}
type TransactionSubmissionResponse struct {
TransactionHashes []string `json:"transactionhashes"`
}
func (t *TSSHandler) BuildTransactions(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
var reqParams BuildTransactionsRequest
httpErr := DecodeJSONAndValidate(ctx, r, &reqParams, t.AppTracker)
if httpErr != nil {
httpErr.Render(w)
return
}
var transactionXDRs []string
for _, transaction := range reqParams.Transactions {
ops, err := utils.BuildOperations(transaction.Operations)
if err != nil {
httperror.BadRequest("bad operation xdr", nil).Render(w)
return
}
tx, err := t.TransactionService.BuildAndSignTransactionWithChannelAccount(ctx, ops, transaction.TimeBounds)
if err != nil {
httperror.InternalServerError(ctx, "unable to build transaction", err, nil, t.AppTracker).Render(w)
return
}
txXdrStr, err := tx.Base64()
if err != nil {
httperror.InternalServerError(ctx, "unable to base64 transaction", err, nil, t.AppTracker).Render(w)
return
}
transactionXDRs = append(transactionXDRs, txXdrStr)
}
httpjson.Render(w, BuildTransactionsResponse{
TransactionXDRs: transactionXDRs,
}, httpjson.JSON)
}
func (t *TSSHandler) SubmitTransactions(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
var reqParams TransactionSubmissionRequest
httpErr := DecodeJSONAndValidate(ctx, r, &reqParams, t.AppTracker)
if httpErr != nil {
httpErr.Render(w)
return
}
var transactionHashes []string
var payloads []tss.Payload
for _, txXDR := range reqParams.Transactions {
genericTx, err := txnbuild.TransactionFromXDR(txXDR)
if err != nil {
httperror.BadRequest("bad transaction xdr", nil).Render(w)
return
}
tx, txEmpty := genericTx.Transaction()
if !txEmpty {
httperror.BadRequest("bad transaction xdr", nil).Render(w)
return
}
txHash, err := tx.HashHex(t.NetworkPassphrase)
if err != nil {
httperror.InternalServerError(ctx, "unable to hashhex transaction", err, nil, t.AppTracker).Render(w)
return
}
payload := tss.Payload{
TransactionHash: txHash,
TransactionXDR: txXDR,
WebhookURL: reqParams.WebhookURL,
FeeBump: reqParams.FeeBump,
}
payloads = append(payloads, payload)
transactionHashes = append(transactionHashes, txHash)
}
httpjson.Render(w, TransactionSubmissionResponse{
TransactionHashes: transactionHashes,
}, httpjson.JSON)
for _, payload := range payloads {
err := t.Router.Route(payload)
if err != nil {
log.Errorf("unable to route payload: %v", err)
}
}
}
type GetTransactionRequest struct {
TransactionHash string `json:"transactionhash" validate:"required"`
}
type GetTransactionResponse struct {
Hash string `json:"transactionhash"`
XDR string `json:"transactionxdr"`
Status string `json:"status"`
}
func (t *TSSHandler) GetTransaction(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
var reqParams GetTransactionRequest
httpErr := DecodePathAndValidate(ctx, r, &reqParams, t.AppTracker)
if httpErr != nil {
httpErr.Render(w)
return
}
tx, err := t.Store.GetTransaction(ctx, reqParams.TransactionHash)
if err != nil {
httperror.InternalServerError(ctx, "unable to get transaction "+reqParams.TransactionHash, err, nil, t.AppTracker).Render(w)
return
}
if tx == (store.Transaction{}) {
httperror.NotFound.Render(w)
}
tssTry, err := t.Store.GetLatestTry(ctx, tx.Hash)
if err != nil {
httperror.InternalServerError(ctx, "unable to get tx try "+tx.Hash, err, nil, t.AppTracker).Render(w)
return
}
httpjson.Render(w, tss.TSSResponse{
TransactionHash: tx.Hash,
TransactionResultCode: fmt.Sprint(tssTry.Code),
Status: tx.Status,
CreatedAt: tx.CreatedAt.Unix(),
EnvelopeXDR: tssTry.XDR,
ResultXDR: tssTry.ResultXDR,
}, httpjson.JSON)
}