Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enable multi-path payments in LND #944

Merged
merged 4 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 47 additions & 21 deletions lnclient/lnd/lnd.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,34 +313,54 @@ func (svc *LNDService) LookupInvoice(ctx context.Context, paymentHash string) (t
return transaction, nil
}

func (svc *LNDService) getPaymentResult(stream routerrpc.Router_SendPaymentV2Client) (*lnrpc.Payment, error) {
for {
payment, err := stream.Recv()
if err != nil {
return nil, err
}

if payment.Status != lnrpc.Payment_IN_FLIGHT {
return payment, nil
}
}
}

func (svc *LNDService) SendPaymentSync(ctx context.Context, payReq string, amount *uint64) (*lnclient.PayInvoiceResponse, error) {
sendRequest := &lnrpc.SendRequest{PaymentRequest: payReq}
const MAX_PARTIAL_PAYMENTS = 21
const SEND_PAYMENT_TIMEOUT = 60
sendRequest := &routerrpc.SendPaymentRequest{
PaymentRequest: payReq,
MaxParts: MAX_PARTIAL_PAYMENTS,
TimeoutSeconds: SEND_PAYMENT_TIMEOUT,
FeeLimitSat: math.MaxInt64,
}

if amount != nil {
sendRequest.AmtMsat = int64(*amount)
}

resp, err := svc.client.SendPaymentSync(ctx, sendRequest)
payStream, err := svc.client.SendPayment(ctx, sendRequest)
if err != nil {
return nil, err
}

if resp.PaymentError != "" {
return nil, errors.New(resp.PaymentError)
resp, err := svc.getPaymentResult(payStream)
if err != nil {
return nil, err
}

if resp.PaymentPreimage == nil {
return nil, errors.New("no preimage in response")
if resp.Status != lnrpc.Payment_SUCCEEDED {
return nil, errors.New(resp.FailureReason.String())
}

var fee uint64 = 0
if resp.PaymentRoute != nil {
fee = uint64(resp.PaymentRoute.TotalFeesMsat)
if resp.PaymentPreimage == "" {
return nil, errors.New("no preimage in response")
}

return &lnclient.PayInvoiceResponse{
Preimage: hex.EncodeToString(resp.PaymentPreimage),
Fee: fee,
Preimage: resp.PaymentPreimage,
Fee: uint64(resp.FeeMsat),
}, nil
}

Expand Down Expand Up @@ -372,15 +392,16 @@ func (svc *LNDService) SendKeysend(ctx context.Context, amount uint64, destinati
}
const KEYSEND_CUSTOM_RECORD = 5482373484
destCustomRecords[KEYSEND_CUSTOM_RECORD] = preImageBytes
sendPaymentRequest := &lnrpc.SendRequest{
sendPaymentRequest := &routerrpc.SendPaymentRequest{
Dest: destBytes,
AmtMsat: int64(amount),
PaymentHash: paymentHashBytes,
DestFeatures: []lnrpc.FeatureBit{lnrpc.FeatureBit_TLV_ONION_REQ},
DestCustomRecords: destCustomRecords,
FeeLimitSat: math.MaxInt64,
}

resp, err := svc.client.SendPaymentSync(ctx, sendPaymentRequest)
payStream, err := svc.client.SendPayment(ctx, sendPaymentRequest)
if err != nil {
logger.Logger.WithFields(logrus.Fields{
"amount": amount,
Expand All @@ -392,26 +413,31 @@ func (svc *LNDService) SendKeysend(ctx context.Context, amount uint64, destinati
}).Errorf("Failed to send keysend payment")
return nil, err
}
if resp.PaymentError != "" {

resp, err := svc.getPaymentResult(payStream)
if err != nil {
return nil, err
}

if resp.Status != lnrpc.Payment_SUCCEEDED {
logger.Logger.WithFields(logrus.Fields{
"amount": amount,
"payeePubkey": destination,
"paymentHash": paymentHash,
"preimage": preimage,
"customRecords": custom_records,
"paymentError": resp.PaymentError,
"paymentError": resp.FailureReason.String(),
}).Errorf("Keysend payment has payment error")
return nil, errors.New(resp.PaymentError)
return nil, errors.New(resp.FailureReason.String())
}
respPreimage := hex.EncodeToString(resp.PaymentPreimage)
if respPreimage != preimage {

if resp.PaymentPreimage != preimage {
logger.Logger.WithFields(logrus.Fields{
"amount": amount,
"payeePubkey": destination,
"paymentHash": paymentHash,
"preimage": preimage,
"customRecords": custom_records,
"paymentError": resp.PaymentError,
}).Errorf("Preimage in keysend response does not match")
return nil, errors.New("preimage in keysend response does not match")
}
Expand All @@ -421,11 +447,11 @@ func (svc *LNDService) SendKeysend(ctx context.Context, amount uint64, destinati
"paymentHash": paymentHash,
"preimage": preimage,
"customRecords": custom_records,
"respPreimage": respPreimage,
"respPreimage": resp.PaymentPreimage,
}).Info("Keysend payment successful")

return &lnclient.PayKeysendResponse{
Fee: uint64(resp.PaymentRoute.TotalFeesMsat),
Fee: uint64(resp.FeeMsat),
}, nil
}

Expand Down
4 changes: 2 additions & 2 deletions lnclient/lnd/wrapper/lnd.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ func (wrapper *LNDWrapper) PendingChannels(ctx context.Context, req *lnrpc.Pendi
return wrapper.client.PendingChannels(ctx, req, options...)
}

func (wrapper *LNDWrapper) SendPaymentSync(ctx context.Context, req *lnrpc.SendRequest, options ...grpc.CallOption) (*lnrpc.SendResponse, error) {
return wrapper.client.SendPaymentSync(ctx, req, options...)
func (wrapper *LNDWrapper) SendPayment(ctx context.Context, req *routerrpc.SendPaymentRequest, options ...grpc.CallOption) (routerrpc.Router_SendPaymentV2Client, error) {
return wrapper.routerClient.SendPaymentV2(ctx, req, options...)
}

func (wrapper *LNDWrapper) ChannelBalance(ctx context.Context, req *lnrpc.ChannelBalanceRequest, options ...grpc.CallOption) (*lnrpc.ChannelBalanceResponse, error) {
Expand Down
Loading