/
checkers.go
629 lines (536 loc) · 18.2 KB
/
checkers.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
package accounts
import (
"context"
"fmt"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg"
mid "github.com/lightninglabs/lightning-terminal/rpcmiddleware"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lnrpc/routerrpc"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/zpay32"
"google.golang.org/protobuf/proto"
)
var (
// DecodePayReqPassThrough is a pass-through checker that allows calls
// to DecodePayReq through unchanged.
DecodePayReqPassThrough = mid.NewPassThrough(
&lnrpc.PayReqString{}, &lnrpc.PayReq{},
)
// GetNodeInfoPassThrough is a pass-through checker that allows calls
// to GetNodeInfo through unchanged.
GetNodeInfoPassThrough = mid.NewPassThrough(
&lnrpc.NodeInfoRequest{}, &lnrpc.NodeInfo{},
)
// PendingChannelsEmptyRewriter is a response re-writer that returns a
// response to PendingChannels with zero channels shown.
PendingChannelsEmptyRewriter = mid.NewResponseEmptier[
*lnrpc.PendingChannelsRequest, *lnrpc.PendingChannelsResponse,
]()
// ListChannelsEmptyRewriter is a response re-writer that returns a
// response to ListChannels with zero channels shown.
ListChannelsEmptyRewriter = mid.NewResponseEmptier[
*lnrpc.ListChannelsRequest, *lnrpc.ListChannelsResponse,
]()
// ClosedChannelsEmptyRewriter is a response re-writer that returns a
// response to ClosedChannels with zero channels shown.
ClosedChannelsEmptyRewriter = mid.NewResponseEmptier[
*lnrpc.ClosedChannelsRequest, *lnrpc.ClosedChannelsResponse,
]()
// WalletBalanceEmptyRewriter is a response re-writer that returns a
// response to WalletBalance with a zero balance shown.
WalletBalanceEmptyRewriter = mid.NewResponseEmptier[
*lnrpc.WalletBalanceRequest, *lnrpc.WalletBalanceResponse,
]()
// GetTransactionsEmptyRewriter is a response re-writer that returns a
// response to GetTransactions with zero transactions shown.
GetTransactionsEmptyRewriter = mid.NewResponseEmptier[
*lnrpc.GetTransactionsRequest, *lnrpc.TransactionDetails,
]()
// ListPeersEmptyRewriter is a response re-writer that returns a
// response to ListPeers with zero peers shown.
ListPeersEmptyRewriter = mid.NewResponseEmptier[
*lnrpc.ListPeersRequest, *lnrpc.ListPeersResponse,
]()
)
// CheckerMap is a type alias that maps gRPC request URIs to their
// rpcmiddleware.RoundTripChecker types.
type CheckerMap map[string]mid.RoundTripChecker
// AccountChecker is a type that can check all account related requests,
// including invoices, payments and account balances.
type AccountChecker struct {
checkers CheckerMap
}
// NewAccountChecker creates a new account checker that can keep track of all
// account related requests, including invoices, payments and account balances.
func NewAccountChecker(service Service,
chainParams *chaincfg.Params) *AccountChecker {
// sendResponseHandler is a response handler function that is used by
// multiple RPC checkers for checking an RPC response sent for a payment
// attempt.
sendResponseHandler := func(ctx context.Context,
r *lnrpc.SendResponse) (proto.Message, error) {
status := lnrpc.Payment_IN_FLIGHT
if len(r.PaymentError) > 0 {
status = lnrpc.Payment_FAILED
}
hash, err := lntypes.MakeHash(r.PaymentHash)
if err != nil {
return nil, fmt.Errorf("error parsing payment hash: %v",
err)
}
route := r.PaymentRoute
totalAmount := int64(0)
if route != nil {
totalAmount = route.TotalAmtMsat + route.TotalFeesMsat
}
return checkSendResponse(
ctx, service, status, hash, totalAmount,
)
}
checkers := CheckerMap{
// Invoices:
"/lnrpc.Lightning/AddInvoice": mid.NewResponseRewriter(
&lnrpc.Invoice{},
&lnrpc.AddInvoiceResponse{},
func(ctx context.Context,
t *lnrpc.AddInvoiceResponse) (proto.Message,
error) {
hash, err := lntypes.MakeHash(t.RHash)
if err != nil {
return nil, fmt.Errorf("error parsing "+
"invoice hash: %v", err)
}
acct, err := AccountFromContext(ctx)
if err != nil {
return nil, err
}
return nil, service.AssociateInvoice(
acct.ID, hash,
)
}, mid.PassThroughErrorHandler,
),
"/lnrpc.Lightning/ListInvoices": mid.NewResponseRewriter(
&lnrpc.ListInvoiceRequest{},
&lnrpc.ListInvoiceResponse{},
func(ctx context.Context,
t *lnrpc.ListInvoiceResponse) (proto.Message,
error) {
filteredInvoices, err := filterInvoices(ctx, t)
if err != nil {
return nil, fmt.Errorf("error "+
"filtering invoices: %v", err)
}
t.Invoices = filteredInvoices
return t, nil
}, mid.PassThroughErrorHandler,
),
"/lnrpc.Lightning/LookupInvoice": mid.NewRequestChecker(
&lnrpc.PaymentHash{},
&lnrpc.Invoice{},
func(ctx context.Context, t *lnrpc.PaymentHash) error {
acct, err := AccountFromContext(ctx)
if err != nil {
return err
}
hash, err := lntypes.MakeHash(t.RHash)
if err != nil {
return fmt.Errorf("error parsing "+
"payment hash: %v", err)
}
if _, ok := acct.Invoices[hash]; !ok {
return fmt.Errorf("invoice does not " +
"belong to this account")
}
return nil
},
),
// Payments:
"/lnrpc.Lightning/SendPayment": mid.NewFullChecker(
&lnrpc.SendRequest{},
&lnrpc.SendResponse{},
func(ctx context.Context, r *lnrpc.SendRequest) error {
return checkSend(
ctx, chainParams, service, r.Amt,
r.AmtMsat, r.PaymentRequest, r.FeeLimit,
)
}, sendResponseHandler, mid.PassThroughErrorHandler,
),
"/lnrpc.Lightning/SendPaymentSync": mid.NewFullChecker(
&lnrpc.SendRequest{},
&lnrpc.SendResponse{},
func(ctx context.Context, r *lnrpc.SendRequest) error {
return checkSend(
ctx, chainParams, service, r.Amt,
r.AmtMsat, r.PaymentRequest, r.FeeLimit,
)
}, sendResponseHandler, mid.PassThroughErrorHandler,
),
// routerrpc.Router/SendPayment is deprecated.
"/routerrpc.Router/SendPaymentV2": mid.NewFullChecker(
&routerrpc.SendPaymentRequest{},
&lnrpc.Payment{},
func(ctx context.Context,
r *routerrpc.SendPaymentRequest) error {
feeLimitMsat := r.FeeLimitMsat
if r.FeeLimitSat > 0 {
feeLimitMsat = r.FeeLimitSat * 1000
}
return checkSend(
ctx, chainParams, service, r.Amt,
r.AmtMsat, r.PaymentRequest,
&lnrpc.FeeLimit{
Limit: &lnrpc.FeeLimit_FixedMsat{
FixedMsat: feeLimitMsat,
},
},
)
},
func(ctx context.Context,
r *lnrpc.Payment) (proto.Message, error) {
hash, err := lntypes.MakeHashFromStr(
r.PaymentHash,
)
if err != nil {
return nil, fmt.Errorf("error parsing "+
"payment hash: %v", err)
}
fullAmt := r.ValueMsat + r.FeeMsat
return checkSendResponse(
ctx, service, r.Status, hash, fullAmt,
)
}, mid.PassThroughErrorHandler,
),
"/lnrpc.Lightning/SendToRoute": mid.NewFullChecker(
&lnrpc.SendToRouteRequest{},
&lnrpc.SendResponse{},
func(ctx context.Context,
r *lnrpc.SendToRouteRequest) error {
return checkSendToRoute(ctx, service, r.Route)
}, sendResponseHandler, mid.PassThroughErrorHandler,
),
"/lnrpc.Lightning/SendToRouteSync": mid.NewFullChecker(
&lnrpc.SendToRouteRequest{},
&lnrpc.SendResponse{},
func(ctx context.Context,
r *lnrpc.SendToRouteRequest) error {
return checkSendToRoute(ctx, service, r.Route)
}, sendResponseHandler, mid.PassThroughErrorHandler,
),
// routerrpc.Router/SendToRoute is deprecated.
"/routerrpc.Router/SendToRouteV2": mid.NewRequestChecker(
&routerrpc.SendToRouteRequest{},
&lnrpc.HTLCAttempt{},
func(ctx context.Context,
r *routerrpc.SendToRouteRequest) error {
return checkSendToRoute(ctx, service, r.Route)
},
// We don't get the payment hash in the response to this
// call. So we can't optimize things and need to rely on
// the payment being tracked by the hash sent in the
// request.
),
"/lnrpc.Lightning/DecodePayReq": DecodePayReqPassThrough,
"/lnrpc.Lightning/ListPayments": mid.NewResponseRewriter(
&lnrpc.ListPaymentsRequest{},
&lnrpc.ListPaymentsResponse{},
func(ctx context.Context,
t *lnrpc.ListPaymentsResponse) (proto.Message,
error) {
filteredPayments, err := filterPayments(ctx, t)
if err != nil {
return nil, fmt.Errorf("error "+
"filtering payments: %v", err)
}
t.Payments = filteredPayments
return t, nil
}, mid.PassThroughErrorHandler,
),
// routerrpc.Router/TrackPayment is deprecated.
"/routerrpc.Router/TrackPaymentV2": mid.NewRequestChecker(
&routerrpc.TrackPaymentRequest{},
&lnrpc.Payment{},
func(ctx context.Context,
t *routerrpc.TrackPaymentRequest) error {
acct, err := AccountFromContext(ctx)
if err != nil {
return err
}
hash, err := lntypes.MakeHash(t.PaymentHash)
if err != nil {
return fmt.Errorf("error parsing "+
"payment hash: %v", err)
}
if _, ok := acct.Payments[hash]; !ok {
return fmt.Errorf("payment does not " +
"belong to this account")
}
return nil
}),
// Channels:
"/lnrpc.Lightning/PendingChannels": PendingChannelsEmptyRewriter,
"/lnrpc.Lightning/ListChannels": ListChannelsEmptyRewriter,
"/lnrpc.Lightning/ClosedChannels": ClosedChannelsEmptyRewriter,
// Balances:
"/lnrpc.Lightning/ChannelBalance": mid.NewResponseRewriter(
&lnrpc.ChannelBalanceRequest{},
&lnrpc.ChannelBalanceResponse{},
func(ctx context.Context,
t *lnrpc.ChannelBalanceResponse) (proto.Message,
error) {
acct, err := AccountFromContext(ctx)
if err != nil {
return nil, err
}
balanceSat := acct.CurrentBalanceSats()
emptyAmount := &lnrpc.Amount{}
return &lnrpc.ChannelBalanceResponse{
Balance: balanceSat,
LocalBalance: &lnrpc.Amount{
Sat: uint64(balanceSat),
Msat: uint64(
acct.CurrentBalance,
),
},
RemoteBalance: emptyAmount,
UnsettledLocalBalance: emptyAmount,
UnsettledRemoteBalance: emptyAmount,
PendingOpenLocalBalance: emptyAmount,
PendingOpenRemoteBalance: emptyAmount,
}, nil
}, mid.PassThroughErrorHandler,
),
"/lnrpc.Lightning/WalletBalance": WalletBalanceEmptyRewriter,
// Transactions:
"/lnrpc.Lightning/GetTransactions": GetTransactionsEmptyRewriter,
// Peers:
"/lnrpc.Lightning/ListPeers": ListPeersEmptyRewriter,
// Info:
"/lnrpc.Lightning/GetInfo": mid.NewResponseRewriter(
&lnrpc.GetInfoRequest{},
&lnrpc.GetInfoResponse{},
func(_ context.Context,
t *lnrpc.GetInfoResponse) (proto.Message,
error) {
return &lnrpc.GetInfoResponse{
Version: t.Version,
CommitHash: t.CommitHash,
IdentityPubkey: t.IdentityPubkey,
Alias: t.Alias,
Color: t.Color,
BlockHeight: t.BlockHeight,
BlockHash: t.BlockHash,
BestHeaderTimestamp: t.BestHeaderTimestamp,
SyncedToChain: t.SyncedToChain,
SyncedToGraph: t.SyncedToGraph,
Testnet: t.Testnet, // nolint
Chains: t.Chains,
Uris: t.Uris,
Features: t.Features,
RequireHtlcInterceptor: t.RequireHtlcInterceptor,
}, nil
}, mid.PassThroughErrorHandler,
),
"/lnrpc.Lightning/GetNodeInfo": GetNodeInfoPassThrough,
}
return &AccountChecker{
checkers: checkers,
}
}
// checkIncomingRequest makes sure the type of incoming call is supported and
// if it is, that it is allowed with the current account balance.
func (a *AccountChecker) checkIncomingRequest(ctx context.Context,
fullUri string, req proto.Message) error {
// If we don't have a handler for the URI, it means we don't support
// that RPC.
checker, ok := a.checkers[fullUri]
if !ok {
return ErrNotSupportedWithAccounts
}
// This is just a sanity check to make sure the implementation for the
// checker actually matches the correct request type.
if !checker.HandlesRequest(req.ProtoReflect().Type()) {
return fmt.Errorf("invalid implementation, checker for URI "+
"%s does not accept request of type %v", fullUri,
req.ProtoReflect().Type())
}
req, err := checker.HandleRequest(ctx, req)
if err != nil {
return err
}
if req != nil {
return fmt.Errorf("request editing checkers not supported " +
"for accounts")
}
return nil
}
// replaceOutgoingResponse inspects the responses before sending them out to the
// lightningClient and replaces them if needed.
func (a *AccountChecker) replaceOutgoingResponse(ctx context.Context,
fullUri string, resp proto.Message) (proto.Message, error) {
// If we don't have a handler for the URI, it means we don't support
// that RPC. We shouldn't get here in the first place, since the initial
// client request to lnd should've already been refused and no response
// could've been generated by lnd.
checker, ok := a.checkers[fullUri]
if !ok {
return nil, ErrNotSupportedWithAccounts
}
// This is just a sanity check to make sure the implementation for the
// checker actually matches the correct request type.
if !checker.HandlesResponse(resp.ProtoReflect().Type()) {
return nil, fmt.Errorf("invalid implementation, checker for "+
"URI %s does not accept response of type %v", fullUri,
resp.ProtoReflect().Type())
}
return checker.HandleResponse(ctx, resp)
}
// filterInvoices filters the total response of all invoices returned by lnd and
// only includes those that are related to the account in the context.
func filterInvoices(ctx context.Context,
t *lnrpc.ListInvoiceResponse) ([]*lnrpc.Invoice, error) {
acct, err := AccountFromContext(ctx)
if err != nil {
return nil, err
}
// We don't pre-allocate, since we don't know how many invoices we have
// after filtering.
var filteredInvoices []*lnrpc.Invoice
for _, invoice := range t.Invoices {
invoice := invoice
hash, err := lntypes.MakeHash(invoice.RHash)
if err != nil {
return nil, err
}
if _, ok := acct.Invoices[hash]; ok {
filteredInvoices = append(filteredInvoices, invoice)
}
}
return filteredInvoices, nil
}
// filterPayments filters the total response of all payments returned by lnd and
// only includes those that are related to the account in the context.
func filterPayments(ctx context.Context,
t *lnrpc.ListPaymentsResponse) ([]*lnrpc.Payment, error) {
acct, err := AccountFromContext(ctx)
if err != nil {
return nil, err
}
// We don't pre-allocate, since we don't know how many payments we have
// after filtering.
var filteredPayments []*lnrpc.Payment
for _, payment := range t.Payments {
payment := payment
hash, err := lntypes.MakeHashFromStr(payment.PaymentHash)
if err != nil {
return nil, err
}
if _, ok := acct.Payments[hash]; ok {
filteredPayments = append(filteredPayments, payment)
}
}
return filteredPayments, nil
}
// checkSend checks if a payment can be initiated by making sure the account in
// the context has enough balance to pay for it.
func checkSend(ctx context.Context, chainParams *chaincfg.Params,
service Service, amt, amtMsat int64, invoice string,
feeLimit *lnrpc.FeeLimit) error {
acct, err := AccountFromContext(ctx)
if err != nil {
return err
}
sendAmt := lnwire.NewMSatFromSatoshis(btcutil.Amount(amt))
if lnwire.MilliSatoshi(amtMsat) > sendAmt {
sendAmt = lnwire.MilliSatoshi(amtMsat)
}
// The invoice is optional.
var paymentHash lntypes.Hash
if len(invoice) > 0 {
payReq, err := zpay32.Decode(invoice, chainParams)
if err != nil {
return fmt.Errorf("error decoding pay req: %w", err)
}
if payReq.MilliSat != nil && *payReq.MilliSat > sendAmt {
sendAmt = *payReq.MilliSat
}
if payReq.PaymentHash != nil {
paymentHash = *payReq.PaymentHash
}
}
// We also add the max fee to the amount to check. This might mean that
// not every single satoshi of an account can be used up. But it
// prevents an account from going into a negative balance if we only
// check for the amount to send but then later debit the full amount.
limit := feeLimit
if limit == nil {
limit = &lnrpc.FeeLimit{}
}
fee := lnrpc.CalculateFeeLimit(limit, sendAmt)
sendAmt += fee
err = service.CheckBalance(acct.ID, sendAmt)
if err != nil {
return fmt.Errorf("error validating account balance: %w", err)
}
emptyHash := lntypes.Hash{}
if paymentHash != emptyHash {
err = service.AssociatePayment(acct.ID, paymentHash, sendAmt)
if err != nil {
return fmt.Errorf("error associating payment: %w", err)
}
}
return nil
}
// checkSendResponse makes sure that a payment that is in flight is tracked
// by the payment service in order for it to eventually be debited from the
// account.
func checkSendResponse(ctx context.Context, service Service,
status lnrpc.Payment_PaymentStatus, hash lntypes.Hash,
fullAmt int64) (proto.Message, error) {
acct, err := AccountFromContext(ctx)
if err != nil {
return nil, err
}
// If we directly observe a failure, make sure
// we stop tracking the payment and then exit
// early.
if status == lnrpc.Payment_FAILED {
return nil, service.RemovePayment(hash)
}
// If there is no immediate failure, make sure
// we track the payment.
return nil, service.TrackPayment(
acct.ID, hash, lnwire.MilliSatoshi(fullAmt),
)
}
// checkSendToRoute checks if a payment can be sent to the route by making sure
// the account in the context has enough balance to pay for it.
func checkSendToRoute(ctx context.Context, service Service,
route *lnrpc.Route) error {
acct, err := AccountFromContext(ctx)
if err != nil {
return err
}
if route == nil {
return fmt.Errorf("invalid route")
}
sendAmt := lnwire.NewMSatFromSatoshis(btcutil.Amount(route.TotalAmt)) // nolint
if lnwire.MilliSatoshi(route.TotalAmtMsat) > sendAmt {
sendAmt = lnwire.MilliSatoshi(route.TotalAmtMsat)
}
// We also add the max fee to the amount to check. This might mean that
// not every single satoshi of an account can be used up. But it
// prevents an account from going into a negative balance if we only
// check for the amount to send but then later debit the full amount.
fee := lnwire.NewMSatFromSatoshis(btcutil.Amount(route.TotalFees)) // nolint
if lnwire.MilliSatoshi(route.TotalFeesMsat) > fee {
fee = lnwire.MilliSatoshi(route.TotalFeesMsat)
}
sendAmt += fee
err = service.CheckBalance(acct.ID, sendAmt)
if err != nil {
return fmt.Errorf("error validating account balance: %w", err)
}
return nil
}