internal keysend payments

This commit is contained in:
kiwiidb
2022-05-18 11:46:15 +02:00
parent 9a0ade8a38
commit d01b9c492f
2 changed files with 35 additions and 0 deletions

View File

@@ -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)

View File

@@ -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)