-
Notifications
You must be signed in to change notification settings - Fork 3
/
api.go
678 lines (570 loc) · 17.2 KB
/
api.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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
package lightning
import (
"context"
"encoding/json"
"fmt"
"math"
"strings"
"time"
entities "github.com/bolt-observer/go_common/entities"
"github.com/getsentry/sentry-go"
"github.com/golang/glog"
)
// APIType enum.
type APIType int
// ApiTypes.
const (
LndGrpc APIType = iota
LndRest
ClnSocket
ClnCommando
)
// GetAPIType from integer.
func GetAPIType(t *int) (*APIType, error) {
if t == nil {
return nil, fmt.Errorf("no api type specified")
}
if *t != int(LndGrpc) && *t != int(LndRest) && *t != int(ClnSocket) && *t != int(ClnCommando) {
return nil, fmt.Errorf("invalid api type specified")
}
ret := APIType(*t)
return &ret, nil
}
// InfoAPI struct.
type InfoAPI struct {
IdentityPubkey string
Alias string
Chain string
Network string
Version string
IsSyncedToGraph bool
IsSyncedToChain bool
BlockHeight int
}
// ChannelsAPI struct.
type ChannelsAPI struct {
Channels []ChannelAPI
}
// ChannelAPI struct.
type ChannelAPI struct {
Private bool
Active bool
RemotePubkey string
Initiator bool
CommitFee uint64
ChanID uint64
RemoteBalance uint64
LocalBalance uint64
Capacity uint64
PendingHtlcs []HtlcAPI
TotalSatoshisSent uint64
TotalSatoshisReceived uint64
NumUpdates uint64
}
// HtlcAPI struct.
type HtlcAPI struct {
Amount uint64
Incoming bool
ForwardingChannel uint64
ForwardingHtlcIndex uint64
}
// DescribeGraphAPI struct.
type DescribeGraphAPI struct {
Nodes []DescribeGraphNodeAPI
Channels []NodeChannelAPI
}
// DescribeGraphNodeAPI struct.
type DescribeGraphNodeAPI struct {
PubKey string `json:"pub_key,omitempty"`
Alias string `json:"alias,omitempty"`
Color string `json:"color,omitempty"`
Addresses []NodeAddressAPI `json:"addresses,omitempty"`
Features map[string]NodeFeatureAPI `json:"features,omitempty"`
LastUpdate entities.JsonTime `json:"last_update,omitempty"`
}
// NodeAddressAPI struct.
type NodeAddressAPI struct {
Network string `json:"network,omitempty"`
Addr string `json:"addr,omitempty"`
}
// NodeFeatureAPI struct.
type NodeFeatureAPI struct {
Name string `json:"name,omitempty"`
IsRequired bool `json:"is_required,omitempty"`
IsKnown bool `json:"is_known,omitempty"`
}
// NodeChannelAPI struct.
type NodeChannelAPI struct {
ChannelID uint64 `json:"channel_id,omitempty"`
ChanPoint string `json:"chan_point"`
Node1Pub string `json:"node1_pub,omitempty"`
Node2Pub string `json:"node2_pub,omitempty"`
Capacity uint64 `json:"capacity,omitempty"`
Node1Policy *RoutingPolicyAPI `json:"node1_policy,omitempty"`
Node2Policy *RoutingPolicyAPI `json:"node2_policy,omitempty"`
LastUpdate entities.JsonTime `json:"last_update,omitempty"`
}
// RoutingPolicyAPI struct.
type RoutingPolicyAPI struct {
TimeLockDelta uint32 `json:"time_lock_delta"`
MinHtlc uint64 `json:"min_htlc"`
BaseFee uint64 `json:"fee_base_msat"`
FeeRate uint64 `json:"fee_rate_milli_msat"`
Disabled bool `json:"disabled,omitempty"`
LastUpdate entities.JsonTime `json:"last_update,omitempty"`
MaxHtlc uint64 `json:"max_htlc_msat"`
}
// NodeInfoAPI struct.
type NodeInfoAPI struct {
Node DescribeGraphNodeAPI `json:"node,omitempty"`
Channels []NodeChannelAPI `json:"channels"`
NumChannels uint32 `json:"num_channels"`
TotalCapacity uint64 `json:"total_capacity"`
}
// NodeChannelAPIExtended struct.
type NodeChannelAPIExtended struct {
Private bool `json:"private,omitempty"`
NodeChannelAPI
}
// NodeInfoAPIExtended struct.
type NodeInfoAPIExtended struct {
NodeInfoAPI
Channels []NodeChannelAPIExtended `json:"channels"`
}
////////////////////////////////////////////////////////////////
// Pagination struct.
type Pagination struct {
Offset uint64 // Exclusive thus 1 means start from 2 (0 will start from beginning)
BatchSize uint64 // limit is 10k or so
Reversed bool
From *time.Time
To *time.Time
}
// PaymentStatus enum.
type PaymentStatus int
// PaymentStatus values.
const (
PaymentUnknown PaymentStatus = iota
PaymentInFlight
PaymentSucceeded
PaymentFailed
)
// StringToPaymentStatus creates PaymentStatus based on a string.
func StringToPaymentStatus(in string) PaymentStatus {
switch strings.ToLower(in) {
case "unknown":
return PaymentUnknown
case "in_flight":
return PaymentInFlight
case "succeeded":
return PaymentSucceeded
case "failed":
return PaymentFailed
}
return PaymentUnknown
}
// PaymentFailureReason enum.
type PaymentFailureReason int
// PaymentFailureReason values.
const (
FailureReasonNone PaymentFailureReason = 0
FailureReasonTimeout
FailureReasonNoRoute
FailureReasonError
FailureReasonIncorrectPaymentDetails
FailureReasonInsufficientBalance
)
// HTLCStatus enum.
type HTLCStatus int
// HTLCStatus values.
const (
HTLCInFlight HTLCStatus = 0
HTLCSucceeded
HTLCFailed
)
// Payment struct.
type Payment struct {
PaymentHash string
ValueMsat int64
FeeMsat int64
PaymentPreimage string
PaymentRequest string
PaymentStatus PaymentStatus
CreationTime time.Time
Index uint64
FailureReason PaymentFailureReason
HTLCAttempts []HTLCAttempt
}
// HTLCAttempt struct.
type HTLCAttempt struct {
ID uint64
Status HTLCStatus
Attempt time.Time
Resolve time.Time
Route Route
}
// Route struct.
type Route struct {
TotalTimeLock uint32
TotalFeesMsat int64
TotalAmtMsat int64
Hops []Hop
}
// Hop struct.
type Hop struct {
ChanID uint64
Expiry uint32
AmtToForwardMsat int64
FeeMsat int64
}
// ForwardingEvent struct.
type ForwardingEvent struct {
Timestamp time.Time
ChanIDIn uint64
ChanIDOut uint64
AmountInMsat uint64
AmountOutMsat uint64
FeeMsat uint64
IsSuccess bool
FailureString string
}
// ResponseForwardPagination struct.
type ResponseForwardPagination struct {
LastOffsetIndex uint64
}
// ResponsePagination struct.
type ResponsePagination struct {
ResponseForwardPagination
FirstOffsetIndex uint64
}
// InvoicesResponse struct.
type InvoicesResponse struct {
Invoices []Invoice
ResponsePagination
}
// Invoice struct.
type Invoice struct {
Memo string
ValueMsat int64
PaidMsat int64
CreationDate time.Time
SettleDate time.Time
PaymentRequest string
DescriptionHash string
Expiry int64
FallbackAddr string
CltvExpiry uint64
Private bool
IsKeySend bool
IsAmp bool
State InvoiceHTLCState
AddIndex uint64
SettleIndex uint64
}
// InvoiceHTLCState enum.
type InvoiceHTLCState int
// StringToInvoiceHTLCState creates InvoiceHTLCState based on a string.
func StringToInvoiceHTLCState(in string) InvoiceHTLCState {
switch strings.ToLower(in) {
case "accepted":
return InvoiceAccepted
case "settled":
return InvoiceSettled
case "cacelled":
return InvoiceCancelled
}
return InvoiceCancelled
}
// InvoiceHTLCState values.
const (
InvoiceAccepted InvoiceHTLCState = 0
InvoiceSettled
InvoiceCancelled
)
// PaymentsResponse struct.
type PaymentsResponse struct {
Payments []Payment
ResponsePagination
}
// RawMessage struct.
type RawMessage struct {
Timestamp time.Time `json:"timestamp"`
Implementation string `json:"implementation,omitempty"`
Message json.RawMessage `json:"message,omitempty"`
}
// ResponseRawPagination struct.
type ResponseRawPagination struct {
UseTimestamp bool
FirstTime time.Time
LastTime time.Time
ResponsePagination
}
// RawPagination struct.
type RawPagination struct {
UseTimestamp bool
FirstTime time.Time
LastTime time.Time
Pagination
}
// Funds struct.
type Funds struct {
TotalBalance int64
ConfirmedBalance int64
LockedBalance int64
}
// PaymentStatusEnum basic enum.
type PaymentStatusEnum int
// PaymentStatusEnum enum.
const (
Failed PaymentStatusEnum = iota
Pending
Success
)
// PaymentResp struct.
type PaymentResp struct {
Hash string
Preimage string
Status PaymentStatusEnum
}
// InvoiceResp struct.
type InvoiceResp struct {
Hash string
PaymentRequest string
}
// Urgency of the on-chain sending
type Urgency int
// Urgency enum.
const (
Low Urgency = iota
Normal
Urgent
)
// CommonInitiator enum.
type CommonInitiator string
const (
Unknown CommonInitiator = "unknown"
Local CommonInitiator = "local"
Remote CommonInitiator = "remote"
)
// Close type enum.
type CommonCloseType string
const (
UnknownType CommonCloseType = "unknown"
CooperativeType CommonCloseType = "cooperative"
ForceType CommonCloseType = "force"
)
// Close info struct.
type CloseInfo struct {
ChanID uint64 `json:"channel_id,omitempty"`
Opener CommonInitiator `json:"opener"`
Closer CommonInitiator `json:"closer"`
CloseType CommonCloseType `json:"close_type"`
}
var UnknownCloseInfo = CloseInfo{ChanID: 0, Opener: Unknown, Closer: Unknown, CloseType: UnknownType}
////////////////////////////////////////////////////////////////
// API - generic API settings.
type API struct {
Name string
GetNodeInfoFullThreshUseDescribeGraph int // If node has more than that number of channels use DescribeGraph else do GetChanInfo for each one
}
// GetDefaultBatchSize returns the default batch size.
func (a *API) GetDefaultBatchSize() uint16 {
return 50
}
// GetNodeInfoFull - GetNodeInfoFull API (GRPC interface).
func (l *LndGrpcLightningAPI) GetNodeInfoFull(ctx context.Context, channels, unnanounced bool) (*NodeInfoAPIExtended, error) {
return getNodeInfoFullTemplate(ctx, l, l.GetNodeInfoFullThreshUseDescribeGraph, channels, unnanounced)
}
// GetNodeInfoFull - GetNodeInfoFull API (REST interface).
func (l *LndRestLightningAPI) GetNodeInfoFull(ctx context.Context, channels, unnanounced bool) (*NodeInfoAPIExtended, error) {
return getNodeInfoFullTemplate(ctx, l, l.GetNodeInfoFullThreshUseDescribeGraph, channels, unnanounced)
}
// getNodeInfoFullTemplate returns info for local node possibly including unnanounced channels (as soon as that can be obtained via GetNodeInfo this method is useless).
func getNodeInfoFullTemplate(ctx context.Context, l LightingAPICalls, threshUseDescribeGraph int, channels, unnanounced bool) (*NodeInfoAPIExtended, error) {
info, err := l.GetInfo(ctx)
if err != nil {
return nil, err
}
nodeInfo, err := l.GetNodeInfo(ctx, info.IdentityPubkey, channels)
if err != nil {
return nil, err
}
extendedNodeInfo := &NodeInfoAPIExtended{NodeInfoAPI: *nodeInfo}
if !unnanounced {
// We have full info already (fast bailout)
capacity := uint64(0)
all := make([]NodeChannelAPIExtended, 0)
for _, ch := range nodeInfo.Channels {
all = append(all, NodeChannelAPIExtended{NodeChannelAPI: ch, Private: false})
capacity += ch.Capacity
}
extendedNodeInfo.Channels = all
extendedNodeInfo.NumChannels = uint32(len(all))
extendedNodeInfo.TotalCapacity = capacity
return extendedNodeInfo, err
}
// Else the channel stats are wrong (unnanounced channels did not count)
chans, err := l.GetChannels(ctx)
if err != nil {
// TODO: Bit of a hack but nodeInfo is pretty much correct
return extendedNodeInfo, err
}
numChans := 0
totalCapacity := uint64(0)
privateMapping := make(map[uint64]bool)
for _, ch := range chans.Channels {
if ch.Private && !unnanounced {
continue
}
privateMapping[ch.ChanID] = ch.Private
totalCapacity += ch.Capacity
numChans++
}
extendedNodeInfo.NumChannels = uint32(numChans)
extendedNodeInfo.TotalCapacity = totalCapacity
if !channels {
return extendedNodeInfo, nil
}
extendedNodeInfo.Channels = make([]NodeChannelAPIExtended, 0)
if len(chans.Channels) <= threshUseDescribeGraph {
for _, ch := range chans.Channels {
if ch.Private && !unnanounced {
continue
}
c, err := l.GetChanInfo(ctx, ch.ChanID)
if err != nil {
glog.Warningf("Could not get channel info for %v (%v): %v", ch.ChanID, info.IdentityPubkey, err)
extendedNodeInfo.NumChannels--
continue
}
private, ok := privateMapping[ch.ChanID]
extendedNodeInfo.Channels = append(extendedNodeInfo.Channels, NodeChannelAPIExtended{NodeChannelAPI: *c, Private: ok && private})
}
} else {
graph, err := l.DescribeGraph(ctx, unnanounced)
if err != nil {
// This could happen due to too big response (btcpay example with limited nginx), retry with other mode
return getNodeInfoFullTemplate(ctx, l, math.MaxInt, channels, unnanounced)
}
for _, one := range graph.Channels {
if one.Node1Pub != info.IdentityPubkey && one.Node2Pub != info.IdentityPubkey {
continue
}
// No need to filter private channels (since we used unnanounced in DescribeGraph)
private, ok := privateMapping[one.ChannelID]
extendedNodeInfo.Channels = append(extendedNodeInfo.Channels, NodeChannelAPIExtended{NodeChannelAPI: one, Private: ok && private})
}
}
return extendedNodeInfo, nil
}
// ErrorData struct.
type ErrorData struct {
Error error
IsStillRunning bool
}
// RouteElement struct.
type RouteElement struct {
PubKey string
OutgoingChannelId uint64
}
// Route alias.
type DeterminedRoute []RouteElement
// ExclusionBase struct.
type ExclusionBase struct {
}
// ExcludedNode struct.
type ExcludedNode struct {
PubKey string
ExclusionBase
}
// ExcludedEdge struct.
type ExcludedEdge struct {
ChannelId uint64
ExclusionBase
}
// Exclusion interface.
type Exclusion interface {
IsNodeExclusion() bool
}
// IsNodeExclusion for edge
func (e ExcludedEdge) IsNodeExclusion() bool {
return false
}
// IsNodeExclusion for node
func (e ExcludedNode) IsNodeExclusion() bool {
return true
}
// OptimizeRouteFor enum.
type OptimizeRouteFor int
// OptimizeRouteFor values.
const (
Reliability OptimizeRouteFor = iota
Price
None
)
// LightingAPICalls is the interface for lightning API.
type LightingAPICalls interface {
Cleanup()
GetAPIType() APIType
GetInfo(ctx context.Context) (*InfoAPI, error)
GetChannels(ctx context.Context) (*ChannelsAPI, error)
DescribeGraph(ctx context.Context, unannounced bool) (*DescribeGraphAPI, error)
GetNodeInfoFull(ctx context.Context, channels, unannounced bool) (*NodeInfoAPIExtended, error)
GetNodeInfo(ctx context.Context, pubKey string, channels bool) (*NodeInfoAPI, error)
GetChanInfo(ctx context.Context, chanID uint64) (*NodeChannelAPI, error)
GetInvoices(ctx context.Context, pendingOnly bool, pagination Pagination) (*InvoicesResponse, error)
GetPayments(ctx context.Context, includeIncomplete bool, pagination Pagination) (*PaymentsResponse, error)
SubscribeForwards(ctx context.Context, since time.Time, batchSize uint16, maxErrors uint16) (<-chan []ForwardingEvent, <-chan ErrorData)
GetInvoicesRaw(ctx context.Context, pendingOnly bool, pagination RawPagination) ([]RawMessage, *ResponseRawPagination, error)
GetPaymentsRaw(ctx context.Context, includeIncomplete bool, pagination RawPagination) ([]RawMessage, *ResponseRawPagination, error)
GetForwardsRaw(ctx context.Context, pagination RawPagination) ([]RawMessage, *ResponseRawPagination, error)
ConnectPeer(ctx context.Context, id string) error
GetOnChainAddress(ctx context.Context) (string, error)
GetOnChainFunds(ctx context.Context) (*Funds, error)
SendToOnChainAddress(ctx context.Context, address string, sats int64, useUnconfirmed bool, urgency Urgency) (string, error)
PayInvoice(ctx context.Context, paymentRequest string, sats int64, outgoingChanIds []uint64) (*PaymentResp, error)
GetPaymentStatus(ctx context.Context, paymentRequest string) (*PaymentResp, error)
CreateInvoice(ctx context.Context, sats int64, preimage string, memo string, expiry time.Duration) (*InvoiceResp, error)
IsInvoicePaid(ctx context.Context, paymentHash string) (bool, error)
GetChannelCloseInfo(ctx context.Context, chanIDs []uint64) ([]CloseInfo, error)
// Includes source but NOT destination addreses in response
GetRoute(ctx context.Context, source string, destination string, exclusions []Exclusion, optimizeFor OptimizeRouteFor, msats int64) (DeterminedRoute, error)
GetRoutes(ctx context.Context, source string, destination string, exclusions []Exclusion, optimizeFor OptimizeRouteFor, msats int64) (<-chan DeterminedRoute, error)
}
// GetDataCall - signature of function for retrieving data.
type GetDataCall func() (*entities.Data, error)
// NewAPI - gets new lightning API.
func NewAPI(apiType APIType, getData GetDataCall) (LightingAPICalls, error) {
if getData == nil {
sentry.CaptureMessage("getData was nil")
return nil, fmt.Errorf("getData error")
}
data, err := getData()
if err != nil {
sentry.CaptureException(err)
return nil, err
}
t := LndGrpc
if data.ApiType != nil {
foo, err := GetAPIType(data.ApiType)
if err != nil {
t = LndGrpc
} else {
t = *foo
}
} else {
t = apiType
}
switch t {
case LndGrpc:
return NewLndGrpcLightningAPI(getData), nil
case LndRest:
return NewLndRestLightningAPI(getData), nil
case ClnSocket:
return NewClnSocketLightningAPI(getData), nil
case ClnCommando:
return NewClnCommandoLightningAPI(getData), nil
}
sentry.CaptureMessage("Invalid api type")
return nil, fmt.Errorf("invalid API type")
}