From 9ae07123d4807c3cad5aa39838fc06ad0e611e52 Mon Sep 17 00:00:00 2001 From: Adithya Vardhan Date: Fri, 2 Aug 2024 19:30:17 +0530 Subject: [PATCH] fix: use private channels in invoice routing (#386) * fix: use private channels in invoice routing * fix: add private hints only if node is private * chore: code cleanup --------- Co-authored-by: Roland Bewick --- lnclient/lnd/lnd.go | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/lnclient/lnd/lnd.go b/lnclient/lnd/lnd.go index c273d4b1..41b6b9e4 100644 --- a/lnclient/lnd/lnd.go +++ b/lnclient/lnd/lnd.go @@ -240,7 +240,27 @@ func (svc *LNDService) MakeInvoice(ctx context.Context, amount int64, descriptio expiry = lnclient.DEFAULT_INVOICE_EXPIRY } - resp, err := svc.client.AddInvoice(ctx, &lnrpc.Invoice{ValueMsat: amount, Memo: description, DescriptionHash: descriptionHashBytes, Expiry: expiry}) + channels, err := svc.ListChannels(ctx) + if err != nil { + return nil, err + } + + hasPublicChannels := false + for _, channel := range channels { + if channel.Active && channel.Public { + hasPublicChannels = true + } + } + + addInvoiceRequest := &lnrpc.Invoice{ + ValueMsat: amount, + Memo: description, + DescriptionHash: descriptionHashBytes, + Expiry: expiry, + Private: !hasPublicChannels, // use private channel hints in the invoice + } + + resp, err := svc.client.AddInvoice(ctx, addInvoiceRequest) if err != nil { return nil, err