1- package lsat
1+ package interceptor
22
33import (
44 "context"
@@ -11,6 +11,7 @@ import (
1111 "time"
1212
1313 "github.com/btcsuite/btcd/btcutil"
14+ "github.com/lightninglabs/aperture/lsat"
1415 "github.com/lightninglabs/lndclient"
1516 "github.com/lightningnetwork/lnd/lnrpc"
1617 "github.com/lightningnetwork/lnd/lnwire"
@@ -77,18 +78,18 @@ var (
7778// connection to lnd to automatically pay for an authentication token.
7879type ClientInterceptor struct {
7980 lnd * lndclient.LndServices
80- store Store
81+ store lsat. Store
8182 callTimeout time.Duration
8283 maxCost btcutil.Amount
8384 maxFee btcutil.Amount
8485 lock sync.Mutex
8586 allowInsecure bool
8687}
8788
88- // NewInterceptor creates a new gRPC client interceptor that uses the provided
89+ // NewClientInterceptor creates a new gRPC client interceptor that uses the provided
8990// lnd connection to automatically acquire and pay for LSAT tokens, unless the
9091// indicated store already contains a usable token.
91- func NewInterceptor (lnd * lndclient.LndServices , store Store ,
92+ func NewClientInterceptor (lnd * lndclient.LndServices , store lsat. Store ,
9293 rpcCallTimeout time.Duration , maxCost ,
9394 maxFee btcutil.Amount , allowInsecure bool ) * ClientInterceptor {
9495
@@ -108,7 +109,7 @@ type interceptContext struct {
108109 mainCtx context.Context
109110 opts []grpc.CallOption
110111 metadata * metadata.MD
111- token * Token
112+ token * lsat. Token
112113}
113114
114115// UnaryInterceptor is an interceptor method that can be used directly by gRPC
@@ -223,7 +224,7 @@ func (i *ClientInterceptor) newInterceptContext(ctx context.Context,
223224 iCtx .token , err = i .store .CurrentToken ()
224225 switch {
225226 // If there is no token yet, nothing to do at this point.
226- case err == ErrNoToken :
227+ case err == lsat . ErrNoToken :
227228
228229 // Some other error happened that we have to surface.
229230 case err != nil :
@@ -235,7 +236,7 @@ func (i *ClientInterceptor) newInterceptContext(ctx context.Context,
235236 // payment just yet, since we don't even know if a token is required for
236237 // this call. We also never send a pending payment to the server since
237238 // we know it's not valid.
238- case ! iCtx .token .isPending ():
239+ case ! iCtx .token .IsPending ():
239240 if err = i .addLsatCredentials (iCtx ); err != nil {
240241 log .Errorf ("Adding macaroon to request failed: %v" , err )
241242 return nil , fmt .Errorf ("adding macaroon failed: %v" ,
@@ -257,7 +258,7 @@ func (i *ClientInterceptor) newInterceptContext(ctx context.Context,
257258func (i * ClientInterceptor ) handlePayment (iCtx * interceptContext ) error {
258259 switch {
259260 // 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 ():
261262 log .Infof ("Payment of LSAT token is required, resuming/" +
262263 "tracking previous payment from pending LSAT token" )
263264 err := i .trackPayment (iCtx .mainCtx , iCtx .token )
@@ -321,7 +322,7 @@ func (i *ClientInterceptor) addLsatCredentials(iCtx *interceptContext) error {
321322 return err
322323 }
323324 iCtx .opts = append (iCtx .opts , grpc .PerRPCCredentials (
324- NewMacaroonCredential (macaroon , i .allowInsecure ),
325+ lsat . NewMacaroonCredential (macaroon , i .allowInsecure ),
325326 ))
326327 return nil
327328}
@@ -330,7 +331,7 @@ func (i *ClientInterceptor) addLsatCredentials(iCtx *interceptContext) error {
330331// to pay the invoice encoded in them, returning a paid LSAT token if
331332// successful.
332333func (i * ClientInterceptor ) payLsatToken (ctx context.Context , md * metadata.MD ) (
333- * Token , error ) {
334+ * lsat. Token , error ) {
334335
335336 // First parse the authentication header that was stored in the
336337 // metadata.
@@ -367,7 +368,7 @@ func (i *ClientInterceptor) payLsatToken(ctx context.Context, md *metadata.MD) (
367368
368369 // Create and store the pending token so we can resume the payment in
369370 // case the payment is interrupted somehow.
370- token , err := tokenFromChallenge (macBytes , invoice .PaymentHash )
371+ token , err := lsat . TokenFromChallenge (macBytes , invoice .PaymentHash )
371372 if err != nil {
372373 return nil , fmt .Errorf ("unable to create token: %v" , err )
373374 }
@@ -407,7 +408,9 @@ func (i *ClientInterceptor) payLsatToken(ctx context.Context, md *metadata.MD) (
407408
408409// trackPayment tries to resume a pending payment by tracking its state and
409410// 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+
411414 // Lookup state of the payment.
412415 paymentStateCtx , cancel := context .WithCancel (ctx )
413416 defer cancel ()
@@ -486,7 +489,7 @@ func IsPaymentRequired(err error) bool {
486489
487490// extractPaymentDetails extracts the preimage and amounts paid for a payment
488491// 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 ) {
490493 token .Preimage = status .Preimage
491494 token .AmountPaid = status .Value
492495 token .RoutingFeePaid = status .Fee
0 commit comments