88 "sync"
99 "time"
1010
11+ "github.com/btcsuite/btcutil"
1112 "github.com/lightninglabs/loop/lndclient"
1213 "github.com/lightningnetwork/lnd/lnrpc/routerrpc"
1314 "github.com/lightningnetwork/lnd/lnwire"
@@ -67,19 +68,24 @@ type Interceptor struct {
6768 lnd * lndclient.LndServices
6869 store Store
6970 callTimeout time.Duration
71+ maxCost btcutil.Amount
72+ maxFee btcutil.Amount
7073 lock sync.Mutex
7174}
7275
7376// NewInterceptor creates a new gRPC client interceptor that uses the provided
7477// lnd connection to automatically acquire and pay for LSAT tokens, unless the
7578// indicated store already contains a usable token.
7679func NewInterceptor (lnd * lndclient.LndServices , store Store ,
77- rpcCallTimeout time.Duration ) * Interceptor {
80+ rpcCallTimeout time.Duration , maxCost ,
81+ maxFee btcutil.Amount ) * Interceptor {
7882
7983 return & Interceptor {
8084 lnd : lnd ,
8185 store : store ,
8286 callTimeout : rpcCallTimeout ,
87+ maxCost : maxCost ,
88+ maxFee : maxFee ,
8389 }
8490}
8591
@@ -226,6 +232,14 @@ func (i *Interceptor) payLsatToken(ctx context.Context, md *metadata.MD) (
226232 return nil , fmt .Errorf ("unable to decode invoice: %v" , err )
227233 }
228234
235+ // Check that the charged amount does not exceed our maximum cost.
236+ maxCostMsat := lnwire .NewMSatFromSatoshis (i .maxCost )
237+ if invoice .MilliSat != nil && * invoice .MilliSat > maxCostMsat {
238+ return nil , fmt .Errorf ("cannot pay for LSAT automatically, " +
239+ "cost of %d msat exceeds configured max cost of %d " +
240+ "msat" , * invoice .MilliSat , maxCostMsat )
241+ }
242+
229243 // Create and store the pending token so we can resume the payment in
230244 // case the payment is interrupted somehow.
231245 token , err := tokenFromChallenge (macBytes , invoice .PaymentHash )
@@ -242,7 +256,7 @@ func (i *Interceptor) payLsatToken(ctx context.Context, md *metadata.MD) (
242256 payCtx , cancel := context .WithTimeout (ctx , PaymentTimeout )
243257 defer cancel ()
244258 respChan := i .lnd .Client .PayInvoice (
245- payCtx , invoiceStr , DefaultMaxRoutingFeeSats , nil ,
259+ payCtx , invoiceStr , i . maxFee , nil ,
246260 )
247261 select {
248262 case result := <- respChan :
0 commit comments