diff --git a/lib/service/invoices.go b/lib/service/invoices.go index 9033d4d..9a33e48 100644 --- a/lib/service/invoices.go +++ b/lib/service/invoices.go @@ -48,6 +48,14 @@ func (svc *LndhubService) FindInvoiceByPaymentHash(ctx context.Context, userId i func (svc *LndhubService) SendInternalPayment(ctx context.Context, invoice *models.Invoice) (SendPaymentResponse, error) { sendPaymentResponse := SendPaymentResponse{} + //Check if it's a keysend payment + //If it is, an invoice will be created on-the-fly + if invoice.Keysend { + err := svc.HandleInternalKeysendPayment(ctx, invoice) + if err != nil { + return sendPaymentResponse, err + } + } // find invoice var incomingInvoice models.Invoice err := svc.DB.NewSelect().Model(&incomingInvoice).Where("type = ? AND payment_request = ? AND state = ? ", common.InvoiceTypeIncoming, invoice.PaymentRequest, common.InvoiceStateOpen).Limit(1).Scan(ctx) diff --git a/lib/service/invoicesubscription.go b/lib/service/invoicesubscription.go index e1466e6..310a75c 100644 --- a/lib/service/invoicesubscription.go +++ b/lib/service/invoicesubscription.go @@ -17,6 +17,33 @@ import ( "github.com/uptrace/bun" ) +func (svc *LndhubService) HandleInternalKeysendPayment(ctx context.Context, invoice *models.Invoice) error { + //Find the payee user + user, err := svc.FindUserByLogin(ctx, string(invoice.DestinationCustomRecords[TLV_WALLET_ID])) + if err != nil { + return err + } + expiry := time.Hour * 24 + incomingInvoice := models.Invoice{ + Type: common.InvoiceTypeIncoming, + UserID: user.ID, + Amount: invoice.Amount, + Internal: true, + Memo: "Keysend payment", + State: common.InvoiceStateInitialized, + ExpiresAt: bun.NullTime{Time: time.Now().Add(expiry)}, + Keysend: true, + RHash: invoice.RHash, + Preimage: invoice.Preimage, + DestinationCustomRecords: invoice.DestinationCustomRecords, + DestinationPubkeyHex: svc.IdentityPubkey, + AddIndex: invoice.AddIndex, + } + //persist the incoming invoice + _, err = svc.DB.NewInsert().Model(&incomingInvoice).Exec(ctx) + return err +} + func (svc *LndhubService) HandleKeysendPayment(ctx context.Context, rawInvoice *lnrpc.Invoice) error { var invoice models.Invoice rHashStr := hex.EncodeToString(rawInvoice.RHash)