forked from bolt-observer/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest.go
572 lines (427 loc) · 13.6 KB
/
rest.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
package lightning
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strconv"
"strings"
utils "github.com/bolt-observer/go_common/utils"
"github.com/lightningnetwork/lnd/lnrpc"
)
// GetDoFunc = signature for Do function.
type GetDoFunc func(req *http.Request) (*http.Response, error)
// HTTPAPI struct.
type HTTPAPI struct {
DoFunc GetDoFunc
client *http.Client
}
// Do - invokes HTTP request.
func (h *HTTPAPI) Do(req *http.Request) (*http.Response, error) {
if h.DoFunc != nil {
return h.DoFunc(req)
} else if h.client != nil {
return h.client.Do(req)
}
return nil, fmt.Errorf("no way to fulfill request")
}
// NewHTTPAPI returns a new HTTPAPI.
func NewHTTPAPI() *HTTPAPI {
return &HTTPAPI{client: nil, DoFunc: nil}
}
// SetTransport - sets HTTP transport.
func (h *HTTPAPI) SetTransport(transport *http.Transport) {
h.client = &http.Client{Transport: transport}
}
// HTTPGetInfo - invokes GetInfo method.
func (h *HTTPAPI) HTTPGetInfo(ctx context.Context, req *http.Request) (*GetInfoResponseOverride, error) {
var info GetInfoResponseOverride
req = req.WithContext(ctx)
u, err := url.Parse(fmt.Sprintf("%s/v1/getinfo", req.URL))
if err != nil {
return nil, fmt.Errorf("invalid url %s", err)
}
req.URL = u
err = h.doGetRequest(req, &info)
if err != nil {
return nil, err
}
return &info, nil
}
// HTTPGetChannels - invokes GetChannels method.
func (h *HTTPAPI) HTTPGetChannels(ctx context.Context, req *http.Request) (*Channels, error) {
var channels Channels
req = req.WithContext(ctx)
u, err := url.Parse(fmt.Sprintf("%s/v1/channels", req.URL))
if err != nil {
return nil, fmt.Errorf("invalid url %s", err)
}
req.URL = u
req.Method = http.MethodGet
err = h.doGetRequest(req, &channels)
if err != nil {
return nil, err
}
return &channels, nil
}
// HTTPGetGraph - invokes GetGraph method.
func (h *HTTPAPI) HTTPGetGraph(ctx context.Context, req *http.Request, unannounced bool) (*Graph, error) {
var graph Graph
req = req.WithContext(ctx)
u, err := url.Parse(fmt.Sprintf("%s/v1/graph?include_unannounced=%s", req.URL, strconv.FormatBool(unannounced)))
if err != nil {
return nil, fmt.Errorf("invalid url %s", err)
}
req.URL = u
req.Method = http.MethodGet
err = h.doGetRequest(req, &graph)
if err != nil {
return nil, err
}
return &graph, nil
}
// HTTPGetNodeInfo - invokes GetNodeInfo method.
func (h *HTTPAPI) HTTPGetNodeInfo(ctx context.Context, req *http.Request, pubKey string, channels bool) (*GetNodeInfoOverride, error) {
var nodeinfo GetNodeInfoOverride
req = req.WithContext(ctx)
u, err := url.Parse(fmt.Sprintf("%s/v1/graph/node/%s?include_channels=%s", req.URL, pubKey, strconv.FormatBool(channels)))
if err != nil {
return nil, fmt.Errorf("invalid url %s", err)
}
req.URL = u
req.Method = http.MethodGet
err = h.doGetRequest(req, &nodeinfo)
if err != nil {
return nil, err
}
return &nodeinfo, nil
}
// HTTPGetChanInfo - invokes GetChanInfo method.
func (h *HTTPAPI) HTTPGetChanInfo(ctx context.Context, req *http.Request, chanID uint64) (*GraphEdgeOverride, error) {
var chaninfo GraphEdgeOverride
req = req.WithContext(ctx)
u, err := url.Parse(fmt.Sprintf("%s/v1/graph/edge/%d", req.URL, chanID))
if err != nil {
return nil, fmt.Errorf("invalid url %s", err)
}
req.URL = u
req.Method = http.MethodGet
err = h.doGetRequest(req, &chaninfo)
if err != nil {
return nil, err
}
return &chaninfo, nil
}
// HTTPForwardEvents - invokes ForwardEvents method.
func (h *HTTPAPI) HTTPForwardEvents(ctx context.Context, req *http.Request, input *ForwardingHistoryRequestOverride) (*ForwardingHistoryResponseOverride, error) {
var data ForwardingHistoryResponseOverride
req = req.WithContext(ctx)
req.Method = http.MethodPost
u, err := url.Parse(fmt.Sprintf("%s/v1/switch", req.URL))
if err != nil {
return nil, fmt.Errorf("invalid url %s", err)
}
s, err := json.Marshal(input)
if err != nil {
return nil, err
}
b := bytes.NewBuffer(s)
req.URL = u
req.Body = io.NopCloser(b)
resp, err := h.Do(req)
if err != nil {
return nil, fmt.Errorf("http request failed %s", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("http got error %d", resp.StatusCode)
}
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&data)
if err != nil {
return nil, fmt.Errorf("got error %v", err)
}
return &data, nil
}
// HTTPSubscribeHtlcEvents - invokes SubscribeHtlcEvents method.
func (h *HTTPAPI) HTTPSubscribeHtlcEvents(ctx context.Context, req *http.Request) (<-chan *HtlcEventOverride, error) {
req = req.WithContext(ctx)
u, err := url.Parse(fmt.Sprintf("%s/v2/router/htlcevents", req.URL))
if err != nil {
return nil, fmt.Errorf("invalid url %s", err)
}
req.URL = u
req.Method = http.MethodGet
resp, err := h.Do(req)
if err != nil {
return nil, fmt.Errorf("http request failed %s", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("http got error %d", resp.StatusCode)
}
outchan := make(chan *HtlcEventOverride)
go func() {
var data HtlcEventOverride
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
for {
if ctx.Err() != nil {
return
}
err := decoder.Decode(&data)
if err != nil {
return
}
outchan <- &data
}
}()
return outchan, nil
}
// HTTPListInvoices - invokes ListInvoices method.
func (h *HTTPAPI) HTTPListInvoices(ctx context.Context, req *http.Request, input *ListInvoiceRequestOverride) (*ListInvoiceResponseOverride, error) {
var data ListInvoiceResponseOverride
req = req.WithContext(ctx)
u, err := url.Parse(fmt.Sprintf("%s/v1/invoices?pending_only=%v&index_offset=%s&num_max_invoices=%s&reversed=%v", req.URL, input.PendingOnly, input.IndexOffset,
input.NumMaxInvoices, input.Reversed))
if err != nil {
return nil, fmt.Errorf("invalid url %s", err)
}
req.URL = u
err = h.doGetRequest(req, &data)
if err != nil {
return nil, err
}
return &data, nil
}
// HTTPListPayments - invokes ListPayments method.
func (h *HTTPAPI) HTTPListPayments(ctx context.Context, req *http.Request, input *ListPaymentsRequestOverride) (*ListPaymentsResponseOverride, error) {
var data ListPaymentsResponseOverride
req = req.WithContext(ctx)
u, err := url.Parse(fmt.Sprintf("%s/v1/payments?include_incomplete=%v&index_offset=%s&max_payments=%s&reversed=%v", req.URL, input.IncludeIncomplete, input.IndexOffset,
input.MaxPayments, input.Reversed))
if err != nil {
return nil, fmt.Errorf("invalid url %s", err)
}
req.URL = u
err = h.doGetRequest(req, &data)
if err != nil {
return nil, err
}
return &data, nil
}
// HTTPPeers - invokes peers method.
func (h *HTTPAPI) HTTPPeers(ctx context.Context, req *http.Request, input *ConnectPeerRequestOverride) error {
req = req.WithContext(ctx)
req.Method = http.MethodPost
u, err := url.Parse(fmt.Sprintf("%s/v1/peers", req.URL))
if err != nil {
return fmt.Errorf("invalid url %s", err)
}
s, err := json.Marshal(input)
if err != nil {
return err
}
b := bytes.NewBuffer(s)
req.URL = u
req.Body = io.NopCloser(b)
resp, err := h.Do(req)
if err != nil {
return fmt.Errorf("http request failed %s", err)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
alreadyConnected := strings.Contains(string(body), "already connected to peer")
if resp.StatusCode != http.StatusOK && !alreadyConnected {
return fmt.Errorf("http got error %d %s", resp.StatusCode, body)
}
return nil
}
// GetHTTPRequest - generic method for doing HTTP requests.
func (h *HTTPAPI) GetHTTPRequest(getData GetDataCall) (*http.Request, *http.Transport, error) {
if getData == nil {
return nil, nil, fmt.Errorf("getData is nil")
}
data, err := getData()
if err != nil {
return nil, nil, err
}
myurl := data.Endpoint
if !strings.HasPrefix(data.Endpoint, "http") {
myurl = fmt.Sprintf("https://%s", data.Endpoint)
}
req, err := http.NewRequest(http.MethodGet, myurl, nil)
if err != nil {
return nil, nil, fmt.Errorf("new request %s", err.Error())
}
req.Header.Set("Grpc-Metadata-macaroon", data.MacaroonHex)
certBytes, err := utils.SafeBase64Decode(data.CertificateBase64)
if err != nil {
return nil, nil, fmt.Errorf("base64 decode error %s", err)
}
verification := PublicCAorCert
if data.CertVerificationType != nil {
verification = CertificateVerification(*data.CertVerificationType)
}
tls, err := getTLSConfig(certBytes, data.Endpoint, verification)
if err != nil {
return nil, nil, fmt.Errorf("getTlsConfig failed %v", err)
}
trans := &http.Transport{
TLSClientConfig: tls,
}
return req, trans, nil
}
func (h *HTTPAPI) doGetRequest(req *http.Request, data any) error {
if req != nil {
req.Method = http.MethodGet
}
resp, err := h.Do(req)
if err != nil {
return fmt.Errorf("http request failed %s", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("http got error %d", resp.StatusCode)
}
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&data)
if err != nil {
return fmt.Errorf("got error %v", err)
}
return nil
}
// HTTPNewAddress - invokes NewAddress method.
func (h *HTTPAPI) HTTPNewAddress(ctx context.Context, req *http.Request, input *lnrpc.NewAddressRequest) (*lnrpc.NewAddressResponse, error) {
var resp lnrpc.NewAddressResponse
req = req.WithContext(ctx)
t := uint32(input.Type)
u, err := url.Parse(fmt.Sprintf("%s/v1/newaddress?type=%d", req.URL, t))
if err != nil {
return nil, fmt.Errorf("invalid url %s", err)
}
req.URL = u
err = h.doGetRequest(req, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}
// HTTPBalance - invokes Balance method.
func (h *HTTPAPI) HTTPBalance(ctx context.Context, req *http.Request) (*WalletBalanceResponseOverride, error) {
var resp WalletBalanceResponseOverride
req = req.WithContext(ctx)
u, err := url.Parse(fmt.Sprintf("%s/v1/balance/blockchain", req.URL))
if err != nil {
return nil, fmt.Errorf("invalid url %s", err)
}
req.URL = u
err = h.doGetRequest(req, &resp)
if err != nil {
return nil, err
}
return &resp, nil
}
func doPostRequest[U, V any](ctx context.Context, h *HTTPAPI, req *http.Request, uri string, input *U) (*V, error) {
var reply V
req = req.WithContext(ctx)
req.Method = http.MethodPost
u, err := url.Parse(uri)
if err != nil {
return nil, fmt.Errorf("invalid url %s", err)
}
s, err := json.Marshal(input)
if err != nil {
return nil, err
}
b := bytes.NewBuffer(s)
req.URL = u
req.Body = io.NopCloser(b)
resp, err := h.Do(req)
if err != nil {
return nil, fmt.Errorf("http request failed %s", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("http got error %d", resp.StatusCode)
}
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&reply)
if err != nil {
return nil, fmt.Errorf("got error %v", err)
}
return &reply, nil
}
// HTTPSendCoins - invokes SendCoins method.
func (h *HTTPAPI) HTTPSendCoins(ctx context.Context, req *http.Request, input *SendCoinsRequestOverride) (*lnrpc.SendCoinsResponse, error) {
return doPostRequest[SendCoinsRequestOverride, lnrpc.SendCoinsResponse](ctx, h, req, fmt.Sprintf("%s/v1/transactions", req.URL), input)
}
// HTTPPayInvoice - invokes PayInvoice method.
func (h *HTTPAPI) HTTPPayInvoice(ctx context.Context, req *http.Request, input *SendPaymentRequestOverride) (*PaymentOverride, error) {
return doPostRequest[SendPaymentRequestOverride, PaymentOverride](ctx, h, req, fmt.Sprintf("%s/v2/router/send", req.URL), input)
}
// HTTPTrackPayment - invokes TrackPayment method.
func (h *HTTPAPI) HTTPTrackPayment(ctx context.Context, req *http.Request, input *TrackPaymentRequestOverride) (*PaymentOverride, error) {
var reply PaymentOverride
req = req.WithContext(ctx)
req.Method = http.MethodPost
u, err := url.Parse(fmt.Sprintf("%s/v2/router/track/%s?no_inflight_updates=%v", req.URL, input.PaymentHash, strconv.FormatBool(input.NoInflightUpdates)))
if err != nil {
return nil, fmt.Errorf("invalid url %s", err)
}
req.URL = u
resp, err := h.Do(req)
if err != nil {
return nil, fmt.Errorf("http request failed %s", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("http got error %d", resp.StatusCode)
}
decoder := json.NewDecoder(resp.Body)
err = decoder.Decode(&reply)
if err != nil {
return nil, fmt.Errorf("got error %v", err)
}
return &reply, nil
}
// HTTPAddInvoice - invokes AddInvoice method.
func (h *HTTPAPI) HTTPAddInvoice(ctx context.Context, req *http.Request, input *InvoiceOverride) (*AddInvoiceResponseOverride, error) {
return doPostRequest[InvoiceOverride, AddInvoiceResponseOverride](ctx, h, req, fmt.Sprintf("%s/v1/invoices", req.URL), input)
}
// HTTPLookupInvoice - invokes LookupInvoice method.
func (h *HTTPAPI) HTTPLookupInvoice(ctx context.Context, req *http.Request, paymentHash string) (*InvoiceOverride, error) {
var reply InvoiceOverride
req = req.WithContext(ctx)
u, err := url.Parse(fmt.Sprintf("%s/v1/invoice/%s", req.URL, paymentHash))
if err != nil {
return nil, fmt.Errorf("invalid url %s", err)
}
req.URL = u
err = h.doGetRequest(req, &reply)
if err != nil {
return nil, err
}
return &reply, nil
}
// HTTPClosedChannels - invokes ClosedChannels method.
func (h *HTTPAPI) HTTPClosedChannels(ctx context.Context, req *http.Request) (*ClosedChannelsResponseOverride, error) {
var reply ClosedChannelsResponseOverride
req = req.WithContext(ctx)
u, err := url.Parse(fmt.Sprintf("%s/v1/channels/closed", req.URL))
if err != nil {
return nil, fmt.Errorf("invalid url %s", err)
}
req.URL = u
err = h.doGetRequest(req, &reply)
if err != nil {
return nil, err
}
return &reply, nil
}