2019-11-08 09:37:51 +00:00
|
|
|
package lsat
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"sync"
|
2019-11-15 12:50:35 +00:00
|
|
|
"time"
|
2019-11-08 09:37:51 +00:00
|
|
|
|
2020-01-10 11:13:39 +00:00
|
|
|
"github.com/btcsuite/btcutil"
|
2019-11-08 09:37:51 +00:00
|
|
|
"github.com/lightninglabs/loop/lndclient"
|
2020-04-09 14:47:06 +00:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc"
|
2019-11-08 09:37:51 +00:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
|
|
|
"github.com/lightningnetwork/lnd/macaroons"
|
|
|
|
"github.com/lightningnetwork/lnd/zpay32"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/metadata"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// GRPCErrCode is the error code we receive from a gRPC call if the
|
|
|
|
// server expects a payment.
|
|
|
|
GRPCErrCode = codes.Internal
|
|
|
|
|
|
|
|
// GRPCErrMessage is the error message we receive from a gRPC call in
|
|
|
|
// conjunction with the GRPCErrCode to signal the client that a payment
|
|
|
|
// is required to access the service.
|
|
|
|
GRPCErrMessage = "payment required"
|
|
|
|
|
|
|
|
// AuthHeader is is the HTTP response header that contains the payment
|
|
|
|
// challenge.
|
|
|
|
AuthHeader = "WWW-Authenticate"
|
|
|
|
|
2020-01-10 10:21:55 +00:00
|
|
|
// DefaultMaxCostSats is the default maximum amount in satoshis that we
|
|
|
|
// are going to pay for an LSAT automatically. Does not include routing
|
|
|
|
// fees.
|
|
|
|
DefaultMaxCostSats = 1000
|
|
|
|
|
|
|
|
// DefaultMaxRoutingFeeSats is the default maximum routing fee in
|
|
|
|
// satoshis that we are going to pay to acquire an LSAT token.
|
|
|
|
DefaultMaxRoutingFeeSats = 10
|
2019-11-15 12:50:35 +00:00
|
|
|
|
|
|
|
// PaymentTimeout is the maximum time we allow a payment to take before
|
|
|
|
// we stop waiting for it.
|
|
|
|
PaymentTimeout = 60 * time.Second
|
|
|
|
|
|
|
|
// manualRetryHint is the error text we return to tell the user how a
|
|
|
|
// token payment can be retried if the payment fails.
|
|
|
|
manualRetryHint = "consider removing pending token file if error " +
|
|
|
|
"persists. use 'listauth' command to find out token file name"
|
2019-11-08 09:37:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// authHeaderRegex is the regular expression the payment challenge must
|
|
|
|
// match for us to be able to parse the macaroon and invoice.
|
|
|
|
authHeaderRegex = regexp.MustCompile(
|
2020-01-07 14:41:40 +00:00
|
|
|
"LSAT macaroon=\"(.*?)\", invoice=\"(.*?)\"",
|
2019-11-08 09:37:51 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
// Interceptor is a gRPC client interceptor that can handle LSAT authentication
|
|
|
|
// challenges with embedded payment requests. It uses a connection to lnd to
|
|
|
|
// automatically pay for an authentication token.
|
|
|
|
type Interceptor struct {
|
2019-11-27 12:36:48 +00:00
|
|
|
lnd *lndclient.LndServices
|
|
|
|
store Store
|
|
|
|
callTimeout time.Duration
|
2020-01-10 11:13:39 +00:00
|
|
|
maxCost btcutil.Amount
|
|
|
|
maxFee btcutil.Amount
|
2019-11-27 12:36:48 +00:00
|
|
|
lock sync.Mutex
|
2019-11-08 09:37:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewInterceptor creates a new gRPC client interceptor that uses the provided
|
|
|
|
// lnd connection to automatically acquire and pay for LSAT tokens, unless the
|
|
|
|
// indicated store already contains a usable token.
|
2019-11-27 12:36:48 +00:00
|
|
|
func NewInterceptor(lnd *lndclient.LndServices, store Store,
|
2020-01-10 11:13:39 +00:00
|
|
|
rpcCallTimeout time.Duration, maxCost,
|
|
|
|
maxFee btcutil.Amount) *Interceptor {
|
2019-11-27 12:36:48 +00:00
|
|
|
|
2019-11-08 09:37:51 +00:00
|
|
|
return &Interceptor{
|
2019-11-27 12:36:48 +00:00
|
|
|
lnd: lnd,
|
|
|
|
store: store,
|
|
|
|
callTimeout: rpcCallTimeout,
|
2020-01-10 11:13:39 +00:00
|
|
|
maxCost: maxCost,
|
|
|
|
maxFee: maxFee,
|
2019-11-08 09:37:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-03 09:09:32 +00:00
|
|
|
// interceptContext is a struct that contains all information about a call that
|
|
|
|
// is intercepted by the interceptor.
|
|
|
|
type interceptContext struct {
|
|
|
|
mainCtx context.Context
|
|
|
|
opts []grpc.CallOption
|
|
|
|
metadata *metadata.MD
|
|
|
|
token *Token
|
|
|
|
}
|
|
|
|
|
2019-11-08 09:37:51 +00:00
|
|
|
// UnaryInterceptor is an interceptor method that can be used directly by gRPC
|
|
|
|
// for unary calls. If the store contains a token, it is attached as credentials
|
|
|
|
// to every call before patching it through. The response error is also
|
|
|
|
// intercepted for every call. If there is an error returned and it is
|
|
|
|
// indicating a payment challenge, a token is acquired and paid for
|
|
|
|
// automatically. The original request is then repeated back to the server, now
|
|
|
|
// with the new token attached.
|
|
|
|
func (i *Interceptor) UnaryInterceptor(ctx context.Context, method string,
|
|
|
|
req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker,
|
|
|
|
opts ...grpc.CallOption) error {
|
|
|
|
|
|
|
|
// To avoid paying for a token twice if two parallel requests are
|
|
|
|
// happening, we require an exclusive lock here.
|
|
|
|
i.lock.Lock()
|
|
|
|
defer i.lock.Unlock()
|
|
|
|
|
2020-02-03 09:09:32 +00:00
|
|
|
// Create the context that we'll use to initiate the real request. This
|
|
|
|
// contains the means to extract response headers and possibly also an
|
|
|
|
// auth token, if we already have paid for one.
|
|
|
|
iCtx, err := i.newInterceptContext(ctx, opts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try executing the call now. If anything goes wrong, we only handle
|
|
|
|
// the LSAT error message that comes in the form of a gRPC status error.
|
|
|
|
rpcCtx, cancel := context.WithTimeout(ctx, i.callTimeout)
|
|
|
|
defer cancel()
|
|
|
|
err = invoker(rpcCtx, method, req, reply, cc, iCtx.opts...)
|
|
|
|
if !isPaymentRequired(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find out if we need to pay for a new token or perhaps resume
|
|
|
|
// a previously aborted payment.
|
|
|
|
err = i.handlePayment(iCtx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute the same request again, now with the LSAT
|
|
|
|
// token added as an RPC credential.
|
|
|
|
rpcCtx2, cancel2 := context.WithTimeout(ctx, i.callTimeout)
|
|
|
|
defer cancel2()
|
|
|
|
return invoker(rpcCtx2, method, req, reply, cc, iCtx.opts...)
|
|
|
|
}
|
|
|
|
|
2020-02-06 09:49:02 +00:00
|
|
|
// StreamInterceptor is an interceptor method that can be used directly by gRPC
|
|
|
|
// for streaming calls. If the store contains a token, it is attached as
|
|
|
|
// credentials to every stream establishment call before patching it through.
|
|
|
|
// The response error is also intercepted for every initial stream initiation.
|
|
|
|
// If there is an error returned and it is indicating a payment challenge, a
|
|
|
|
// token is acquired and paid for automatically. The original request is then
|
|
|
|
// repeated back to the server, now with the new token attached.
|
|
|
|
func (i *Interceptor) StreamInterceptor(ctx context.Context,
|
|
|
|
desc *grpc.StreamDesc, cc *grpc.ClientConn, method string,
|
|
|
|
streamer grpc.Streamer, opts ...grpc.CallOption) (grpc.ClientStream,
|
|
|
|
error) {
|
|
|
|
|
|
|
|
// To avoid paying for a token twice if two parallel requests are
|
|
|
|
// happening, we require an exclusive lock here.
|
|
|
|
i.lock.Lock()
|
|
|
|
defer i.lock.Unlock()
|
|
|
|
|
|
|
|
// Create the context that we'll use to initiate the real request. This
|
|
|
|
// contains the means to extract response headers and possibly also an
|
|
|
|
// auth token, if we already have paid for one.
|
|
|
|
iCtx, err := i.newInterceptContext(ctx, opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try establishing the stream now. If anything goes wrong, we only
|
|
|
|
// handle the LSAT error message that comes in the form of a gRPC status
|
|
|
|
// error. The context of a stream will be used for the whole lifetime of
|
|
|
|
// it, so we can't really clamp down on the initial call with a timeout.
|
|
|
|
stream, err := streamer(ctx, desc, cc, method, iCtx.opts...)
|
|
|
|
if !isPaymentRequired(err) {
|
|
|
|
return stream, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find out if we need to pay for a new token or perhaps resume
|
|
|
|
// a previously aborted payment.
|
|
|
|
err = i.handlePayment(iCtx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute the same request again, now with the LSAT token added
|
|
|
|
// as an RPC credential.
|
|
|
|
return streamer(ctx, desc, cc, method, iCtx.opts...)
|
|
|
|
}
|
|
|
|
|
2020-02-03 09:09:32 +00:00
|
|
|
// newInterceptContext creates the initial intercept context that can capture
|
|
|
|
// metadata from the server and sends the local token to the server if one
|
|
|
|
// already exists.
|
|
|
|
func (i *Interceptor) newInterceptContext(ctx context.Context,
|
|
|
|
opts []grpc.CallOption) (*interceptContext, error) {
|
|
|
|
|
|
|
|
iCtx := &interceptContext{
|
|
|
|
mainCtx: ctx,
|
|
|
|
opts: opts,
|
|
|
|
metadata: &metadata.MD{},
|
2019-11-08 09:37:51 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 12:50:35 +00:00
|
|
|
// Let's see if the store already contains a token and what state it
|
|
|
|
// might be in. If a previous call was aborted, we might have a pending
|
|
|
|
// token that needs to be handled separately.
|
2020-02-03 09:09:32 +00:00
|
|
|
var err error
|
|
|
|
iCtx.token, err = i.store.CurrentToken()
|
2019-11-15 12:50:35 +00:00
|
|
|
switch {
|
|
|
|
// If there is no token yet, nothing to do at this point.
|
|
|
|
case err == ErrNoToken:
|
|
|
|
|
|
|
|
// Some other error happened that we have to surface.
|
|
|
|
case err != nil:
|
|
|
|
log.Errorf("Failed to get token from store: %v", err)
|
2020-02-03 09:09:32 +00:00
|
|
|
return nil, fmt.Errorf("getting token from store failed: %v",
|
|
|
|
err)
|
2019-11-15 12:50:35 +00:00
|
|
|
|
|
|
|
// Only if we have a paid token append it. We don't resume a pending
|
|
|
|
// payment just yet, since we don't even know if a token is required for
|
|
|
|
// this call. We also never send a pending payment to the server since
|
|
|
|
// we know it's not valid.
|
2020-02-03 09:09:32 +00:00
|
|
|
case !iCtx.token.isPending():
|
|
|
|
if err = i.addLsatCredentials(iCtx); err != nil {
|
2019-11-15 12:50:35 +00:00
|
|
|
log.Errorf("Adding macaroon to request failed: %v", err)
|
2020-02-03 09:09:32 +00:00
|
|
|
return nil, fmt.Errorf("adding macaroon failed: %v",
|
|
|
|
err)
|
2019-11-08 09:37:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-15 12:50:35 +00:00
|
|
|
// We need a way to extract the response headers sent by the server.
|
|
|
|
// This can only be done through the experimental grpc.Trailer call
|
|
|
|
// option. We execute the request and inspect the error. If it's the
|
|
|
|
// LSAT specific payment required error, we might execute the same
|
|
|
|
// method again later with the paid LSAT token.
|
2020-02-03 09:09:32 +00:00
|
|
|
iCtx.opts = append(iCtx.opts, grpc.Trailer(iCtx.metadata))
|
|
|
|
return iCtx, nil
|
2019-11-08 09:37:51 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 12:50:35 +00:00
|
|
|
// handlePayment tries to obtain a valid token by either tracking the payment
|
|
|
|
// status of a pending token or paying for a new one.
|
2020-02-03 09:09:32 +00:00
|
|
|
func (i *Interceptor) handlePayment(iCtx *interceptContext) error {
|
2019-11-15 12:50:35 +00:00
|
|
|
switch {
|
|
|
|
// Resume/track a pending payment if it was interrupted for some reason.
|
2020-02-03 09:09:32 +00:00
|
|
|
case iCtx.token != nil && iCtx.token.isPending():
|
2019-11-15 12:50:35 +00:00
|
|
|
log.Infof("Payment of LSAT token is required, resuming/" +
|
|
|
|
"tracking previous payment from pending LSAT token")
|
2020-02-03 09:09:32 +00:00
|
|
|
err := i.trackPayment(iCtx.mainCtx, iCtx.token)
|
2019-11-15 12:50:35 +00:00
|
|
|
if err != nil {
|
2020-02-03 09:09:32 +00:00
|
|
|
return err
|
2019-11-15 12:50:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We don't have a token yet, try to get a new one.
|
2020-02-03 09:09:32 +00:00
|
|
|
case iCtx.token == nil:
|
2019-11-15 12:50:35 +00:00
|
|
|
// We don't have a token yet, get a new one.
|
|
|
|
log.Infof("Payment of LSAT token is required, paying invoice")
|
2020-02-03 09:09:32 +00:00
|
|
|
var err error
|
|
|
|
iCtx.token, err = i.payLsatToken(iCtx.mainCtx, iCtx.metadata)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-15 12:50:35 +00:00
|
|
|
|
|
|
|
// We have a token and it's valid, nothing more to do here.
|
|
|
|
default:
|
|
|
|
log.Debugf("Found valid LSAT token to add to request")
|
|
|
|
}
|
2020-02-03 09:09:32 +00:00
|
|
|
|
|
|
|
if err := i.addLsatCredentials(iCtx); err != nil {
|
|
|
|
log.Errorf("Adding macaroon to request failed: %v", err)
|
|
|
|
return fmt.Errorf("adding macaroon failed: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// addLsatCredentials adds an LSAT token to the given intercept context.
|
|
|
|
func (i *Interceptor) addLsatCredentials(iCtx *interceptContext) error {
|
|
|
|
if iCtx.token == nil {
|
|
|
|
return fmt.Errorf("cannot add nil token to context")
|
|
|
|
}
|
|
|
|
|
|
|
|
macaroon, err := iCtx.token.PaidMacaroon()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
iCtx.opts = append(iCtx.opts, grpc.PerRPCCredentials(
|
|
|
|
macaroons.NewMacaroonCredential(macaroon),
|
|
|
|
))
|
|
|
|
return nil
|
2019-11-15 12:50:35 +00:00
|
|
|
}
|
|
|
|
|
2019-11-08 09:37:51 +00:00
|
|
|
// payLsatToken reads the payment challenge from the response metadata and tries
|
|
|
|
// to pay the invoice encoded in them, returning a paid LSAT token if
|
|
|
|
// successful.
|
|
|
|
func (i *Interceptor) payLsatToken(ctx context.Context, md *metadata.MD) (
|
|
|
|
*Token, error) {
|
|
|
|
|
|
|
|
// First parse the authentication header that was stored in the
|
|
|
|
// metadata.
|
|
|
|
authHeader := md.Get(AuthHeader)
|
|
|
|
if len(authHeader) == 0 {
|
|
|
|
return nil, fmt.Errorf("auth header not found in response")
|
|
|
|
}
|
|
|
|
matches := authHeaderRegex.FindStringSubmatch(authHeader[0])
|
|
|
|
if len(matches) != 3 {
|
|
|
|
return nil, fmt.Errorf("invalid auth header "+
|
|
|
|
"format: %s", authHeader[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode the base64 macaroon and the invoice so we can store the
|
|
|
|
// information in our store later.
|
|
|
|
macBase64, invoiceStr := matches[1], matches[2]
|
|
|
|
macBytes, err := base64.StdEncoding.DecodeString(macBase64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("base64 decode of macaroon failed: "+
|
|
|
|
"%v", err)
|
|
|
|
}
|
|
|
|
invoice, err := zpay32.Decode(invoiceStr, i.lnd.ChainParams)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to decode invoice: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-01-10 11:13:39 +00:00
|
|
|
// Check that the charged amount does not exceed our maximum cost.
|
|
|
|
maxCostMsat := lnwire.NewMSatFromSatoshis(i.maxCost)
|
|
|
|
if invoice.MilliSat != nil && *invoice.MilliSat > maxCostMsat {
|
|
|
|
return nil, fmt.Errorf("cannot pay for LSAT automatically, "+
|
|
|
|
"cost of %d msat exceeds configured max cost of %d "+
|
|
|
|
"msat", *invoice.MilliSat, maxCostMsat)
|
|
|
|
}
|
|
|
|
|
2019-11-15 12:50:35 +00:00
|
|
|
// Create and store the pending token so we can resume the payment in
|
|
|
|
// case the payment is interrupted somehow.
|
|
|
|
token, err := tokenFromChallenge(macBytes, invoice.PaymentHash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to create token: %v", err)
|
|
|
|
}
|
|
|
|
err = i.store.StoreToken(token)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("unable to store pending token: %v", err)
|
|
|
|
}
|
|
|
|
|
2019-11-08 09:37:51 +00:00
|
|
|
// Pay invoice now and wait for the result to arrive or the main context
|
|
|
|
// being canceled.
|
2019-11-15 12:50:35 +00:00
|
|
|
payCtx, cancel := context.WithTimeout(ctx, PaymentTimeout)
|
|
|
|
defer cancel()
|
2019-11-08 09:37:51 +00:00
|
|
|
respChan := i.lnd.Client.PayInvoice(
|
2020-01-10 11:13:39 +00:00
|
|
|
payCtx, invoiceStr, i.maxFee, nil,
|
2019-11-08 09:37:51 +00:00
|
|
|
)
|
|
|
|
select {
|
|
|
|
case result := <-respChan:
|
|
|
|
if result.Err != nil {
|
|
|
|
return nil, result.Err
|
|
|
|
}
|
2019-11-15 12:50:35 +00:00
|
|
|
token.Preimage = result.Preimage
|
|
|
|
token.AmountPaid = lnwire.NewMSatFromSatoshis(result.PaidAmt)
|
|
|
|
token.RoutingFeePaid = lnwire.NewMSatFromSatoshis(
|
|
|
|
result.PaidFee,
|
2019-11-08 09:37:51 +00:00
|
|
|
)
|
|
|
|
return token, i.store.StoreToken(token)
|
|
|
|
|
2019-11-15 12:50:35 +00:00
|
|
|
case <-payCtx.Done():
|
|
|
|
return nil, fmt.Errorf("payment timed out. try again to track "+
|
|
|
|
"payment. %s", manualRetryHint)
|
|
|
|
|
2019-11-08 09:37:51 +00:00
|
|
|
case <-ctx.Done():
|
2019-11-15 12:50:35 +00:00
|
|
|
return nil, fmt.Errorf("parent context canceled. try again to"+
|
|
|
|
"track payment. %s", manualRetryHint)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// trackPayment tries to resume a pending payment by tracking its state and
|
|
|
|
// waiting for a conclusive result.
|
|
|
|
func (i *Interceptor) trackPayment(ctx context.Context, token *Token) error {
|
|
|
|
// Lookup state of the payment.
|
|
|
|
paymentStateCtx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
|
|
|
payStatusChan, payErrChan, err := i.lnd.Router.TrackPayment(
|
|
|
|
paymentStateCtx, token.PaymentHash,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Errorf("Could not call TrackPayment on lnd: %v", err)
|
|
|
|
return fmt.Errorf("track payment call to lnd failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We can't wait forever, so we give the payment tracking the same
|
|
|
|
// timeout as the original payment.
|
|
|
|
payCtx, cancel := context.WithTimeout(ctx, PaymentTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
// We'll consume status updates until we reach a conclusive state or
|
|
|
|
// reach the timeout.
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
// If we receive a state without an error, the payment has been
|
|
|
|
// initiated. Loop until the payment
|
|
|
|
case result := <-payStatusChan:
|
|
|
|
switch result.State {
|
|
|
|
// If the payment was successful, we have all the
|
|
|
|
// information we need and we can return the fully paid
|
|
|
|
// token.
|
2020-04-09 14:47:06 +00:00
|
|
|
case lnrpc.Payment_SUCCEEDED:
|
2019-11-15 12:50:35 +00:00
|
|
|
extractPaymentDetails(token, result)
|
|
|
|
return i.store.StoreToken(token)
|
|
|
|
|
|
|
|
// The payment is still in transit, we'll give it more
|
|
|
|
// time to complete.
|
2020-04-09 14:47:06 +00:00
|
|
|
case lnrpc.Payment_IN_FLIGHT:
|
2019-11-15 12:50:35 +00:00
|
|
|
|
|
|
|
// Any other state means either error or timeout.
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("payment tracking failed "+
|
|
|
|
"with state %s. %s",
|
|
|
|
result.State.String(), manualRetryHint)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Abort the payment execution for any error.
|
|
|
|
case err := <-payErrChan:
|
|
|
|
return fmt.Errorf("payment tracking failed: %v. %s",
|
|
|
|
err, manualRetryHint)
|
|
|
|
|
|
|
|
case <-payCtx.Done():
|
|
|
|
return fmt.Errorf("payment tracking timed out. %s",
|
|
|
|
manualRetryHint)
|
|
|
|
}
|
2019-11-08 09:37:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// isPaymentRequired inspects an error to find out if it's the specific gRPC
|
|
|
|
// error returned by the server to indicate a payment is required to access the
|
|
|
|
// service.
|
|
|
|
func isPaymentRequired(err error) bool {
|
|
|
|
statusErr, ok := status.FromError(err)
|
|
|
|
return ok &&
|
|
|
|
statusErr.Message() == GRPCErrMessage &&
|
|
|
|
statusErr.Code() == GRPCErrCode
|
|
|
|
}
|
2019-11-15 12:50:35 +00:00
|
|
|
|
|
|
|
// extractPaymentDetails extracts the preimage and amounts paid for a payment
|
|
|
|
// from the payment status and stores them in the token.
|
|
|
|
func extractPaymentDetails(token *Token, status lndclient.PaymentStatus) {
|
|
|
|
token.Preimage = status.Preimage
|
2020-04-09 14:47:06 +00:00
|
|
|
token.AmountPaid = status.Value
|
|
|
|
token.RoutingFeePaid = status.Fee
|
2019-11-15 12:50:35 +00:00
|
|
|
}
|