1
- package lsat
1
+ package interceptor
2
2
3
3
import (
4
4
"context"
@@ -11,6 +11,7 @@ import (
11
11
"time"
12
12
13
13
"github.com/btcsuite/btcd/btcutil"
14
+ "github.com/lightninglabs/aperture/lsat"
14
15
"github.com/lightninglabs/lndclient"
15
16
"github.com/lightningnetwork/lnd/lnrpc"
16
17
"github.com/lightningnetwork/lnd/lnwire"
@@ -77,18 +78,18 @@ var (
77
78
// connection to lnd to automatically pay for an authentication token.
78
79
type ClientInterceptor struct {
79
80
lnd * lndclient.LndServices
80
- store Store
81
+ store lsat. Store
81
82
callTimeout time.Duration
82
83
maxCost btcutil.Amount
83
84
maxFee btcutil.Amount
84
85
lock sync.Mutex
85
86
allowInsecure bool
86
87
}
87
88
88
- // NewInterceptor creates a new gRPC client interceptor that uses the provided
89
- // lnd connection to automatically acquire and pay for LSAT tokens, unless the
90
- // indicated store already contains a usable token.
91
- func NewInterceptor (lnd * lndclient.LndServices , store Store ,
89
+ // NewClientInterceptor creates a new gRPC client interceptor that uses the
90
+ // provided lnd connection to automatically acquire and pay for LSAT tokens,
91
+ // unless the indicated store already contains a usable token.
92
+ func NewClientInterceptor (lnd * lndclient.LndServices , store lsat. Store ,
92
93
rpcCallTimeout time.Duration , maxCost ,
93
94
maxFee btcutil.Amount , allowInsecure bool ) * ClientInterceptor {
94
95
@@ -108,7 +109,7 @@ type interceptContext struct {
108
109
mainCtx context.Context
109
110
opts []grpc.CallOption
110
111
metadata * metadata.MD
111
- token * Token
112
+ token * lsat. Token
112
113
}
113
114
114
115
// UnaryInterceptor is an interceptor method that can be used directly by gRPC
@@ -223,7 +224,7 @@ func (i *ClientInterceptor) newInterceptContext(ctx context.Context,
223
224
iCtx .token , err = i .store .CurrentToken ()
224
225
switch {
225
226
// If there is no token yet, nothing to do at this point.
226
- case err == ErrNoToken :
227
+ case err == lsat . ErrNoToken :
227
228
228
229
// Some other error happened that we have to surface.
229
230
case err != nil :
@@ -235,7 +236,7 @@ func (i *ClientInterceptor) newInterceptContext(ctx context.Context,
235
236
// payment just yet, since we don't even know if a token is required for
236
237
// this call. We also never send a pending payment to the server since
237
238
// we know it's not valid.
238
- case ! iCtx .token .isPending ():
239
+ case ! iCtx .token .IsPending ():
239
240
if err = i .addLsatCredentials (iCtx ); err != nil {
240
241
log .Errorf ("Adding macaroon to request failed: %v" , err )
241
242
return nil , fmt .Errorf ("adding macaroon failed: %v" ,
@@ -257,7 +258,7 @@ func (i *ClientInterceptor) newInterceptContext(ctx context.Context,
257
258
func (i * ClientInterceptor ) handlePayment (iCtx * interceptContext ) error {
258
259
switch {
259
260
// Resume/track a pending payment if it was interrupted for some reason.
260
- case iCtx .token != nil && iCtx .token .isPending ():
261
+ case iCtx .token != nil && iCtx .token .IsPending ():
261
262
log .Infof ("Payment of LSAT token is required, resuming/" +
262
263
"tracking previous payment from pending LSAT token" )
263
264
err := i .trackPayment (iCtx .mainCtx , iCtx .token )
@@ -321,7 +322,7 @@ func (i *ClientInterceptor) addLsatCredentials(iCtx *interceptContext) error {
321
322
return err
322
323
}
323
324
iCtx .opts = append (iCtx .opts , grpc .PerRPCCredentials (
324
- NewMacaroonCredential (macaroon , i .allowInsecure ),
325
+ lsat . NewMacaroonCredential (macaroon , i .allowInsecure ),
325
326
))
326
327
return nil
327
328
}
@@ -330,7 +331,7 @@ func (i *ClientInterceptor) addLsatCredentials(iCtx *interceptContext) error {
330
331
// to pay the invoice encoded in them, returning a paid LSAT token if
331
332
// successful.
332
333
func (i * ClientInterceptor ) payLsatToken (ctx context.Context , md * metadata.MD ) (
333
- * Token , error ) {
334
+ * lsat. Token , error ) {
334
335
335
336
// First parse the authentication header that was stored in the
336
337
// metadata.
@@ -367,7 +368,7 @@ func (i *ClientInterceptor) payLsatToken(ctx context.Context, md *metadata.MD) (
367
368
368
369
// Create and store the pending token so we can resume the payment in
369
370
// case the payment is interrupted somehow.
370
- token , err := tokenFromChallenge (macBytes , invoice .PaymentHash )
371
+ token , err := lsat . TokenFromChallenge (macBytes , invoice .PaymentHash )
371
372
if err != nil {
372
373
return nil , fmt .Errorf ("unable to create token: %v" , err )
373
374
}
@@ -407,7 +408,9 @@ func (i *ClientInterceptor) payLsatToken(ctx context.Context, md *metadata.MD) (
407
408
408
409
// trackPayment tries to resume a pending payment by tracking its state and
409
410
// waiting for a conclusive result.
410
- func (i * ClientInterceptor ) trackPayment (ctx context.Context , token * Token ) error {
411
+ func (i * ClientInterceptor ) trackPayment (ctx context.Context ,
412
+ token * lsat.Token ) error {
413
+
411
414
// Lookup state of the payment.
412
415
paymentStateCtx , cancel := context .WithCancel (ctx )
413
416
defer cancel ()
@@ -486,7 +489,7 @@ func IsPaymentRequired(err error) bool {
486
489
487
490
// extractPaymentDetails extracts the preimage and amounts paid for a payment
488
491
// from the payment status and stores them in the token.
489
- func extractPaymentDetails (token * Token , status lndclient.PaymentStatus ) {
492
+ func extractPaymentDetails (token * lsat. Token , status lndclient.PaymentStatus ) {
490
493
token .Preimage = status .Preimage
491
494
token .AmountPaid = status .Value
492
495
token .RoutingFeePaid = status .Fee
0 commit comments