Skip to content

Commit

Permalink
feat: use internal transactions table for keysend payments
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz committed Jul 8, 2024
1 parent d7b163d commit e95f1f5
Show file tree
Hide file tree
Showing 13 changed files with 181 additions and 210 deletions.
16 changes: 15 additions & 1 deletion api/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package api

import (
"context"
"encoding/json"
"errors"
"time"

"github.com/getAlby/hub/logger"
"github.com/getAlby/hub/transactions"
"github.com/sirupsen/logrus"
)

func (api *api) CreateInvoice(ctx context.Context, amount int64, description string) (*MakeInvoiceResponse, error) {
Expand Down Expand Up @@ -72,6 +75,17 @@ func toApiTransaction(transaction *transactions.Transaction) *Transaction {
settledAt = &settledAtValue
}

var metadata interface{}
if transaction.Metadata != "" {
jsonErr := json.Unmarshal([]byte(transaction.Metadata), &metadata)
if jsonErr != nil {
logger.Logger.WithError(jsonErr).WithFields(logrus.Fields{
"id": transaction.ID,
"metadata": transaction.Metadata,
}).Error("Failed to deserialize transaction metadata")
}
}

return &Transaction{
Type: transaction.Type,
Invoice: transaction.PaymentRequest,
Expand All @@ -84,6 +98,6 @@ func toApiTransaction(transaction *transactions.Transaction) *Transaction {
FeesPaid: fee,
CreatedAt: createdAt,
SettledAt: settledAt,
Metadata: transaction.Metadata,
Metadata: metadata,
}
}
12 changes: 8 additions & 4 deletions lnclient/breez/breez.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,16 @@ func (bs *BreezService) SendPaymentSync(ctx context.Context, payReq string) (*ln

}

func (bs *BreezService) SendKeysend(ctx context.Context, amount uint64, destination, preimage string, custom_records []lnclient.TLVRecord) (preImage string, err error) {
func (bs *BreezService) SendKeysend(ctx context.Context, amount uint64, destination string, custom_records []lnclient.TLVRecord) (paymentHash string, preimage string, fee uint64, err error) {
extraTlvs := []breez_sdk.TlvEntry{}
for _, record := range custom_records {
decodedValue, err := hex.DecodeString(record.Value)
if err != nil {
return "", "", 0, err
}
extraTlvs = append(extraTlvs, breez_sdk.TlvEntry{
FieldNumber: record.Type,
Value: []uint8(record.Value),
Value: decodedValue,
})
}

Expand All @@ -132,13 +136,13 @@ func (bs *BreezService) SendKeysend(ctx context.Context, amount uint64, destinat
}
resp, err := bs.svc.SendSpontaneousPayment(sendSpontaneousPaymentRequest)
if err != nil {
return "", err
return "", "", 0, err
}
var lnDetails breez_sdk.PaymentDetailsLn
if resp.Payment.Details != nil {
lnDetails, _ = resp.Payment.Details.(breez_sdk.PaymentDetailsLn)
}
return lnDetails.Data.PaymentPreimage, nil
return lnDetails.Data.PaymentHash, lnDetails.Data.PaymentPreimage, resp.Payment.FeeMsat, nil
}

func (bs *BreezService) GetBalance(ctx context.Context) (balance int64, err error) {
Expand Down
4 changes: 2 additions & 2 deletions lnclient/cashu/cashu.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ func (cs *CashuService) SendPaymentSync(ctx context.Context, invoice string) (re
}, nil
}

func (cs *CashuService) SendKeysend(ctx context.Context, amount uint64, destination, preimage string, custom_records []lnclient.TLVRecord) (preImage string, err error) {
return "", errors.New("Keysend not supported")
func (cs *CashuService) SendKeysend(ctx context.Context, amount uint64, destination string, custom_records []lnclient.TLVRecord) (paymentHash string, preimage string, fee uint64, err error) {
return "", "", 0, errors.New("Keysend not supported")
}

func (cs *CashuService) GetBalance(ctx context.Context) (balance int64, err error) {
Expand Down
12 changes: 7 additions & 5 deletions lnclient/greenlight/greenlight.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package greenlight

import (
"context"
"encoding/hex"
"errors"
"log"
"math/rand"
Expand Down Expand Up @@ -126,14 +125,15 @@ func (gs *GreenlightService) SendPaymentSync(ctx context.Context, payReq string)
}, nil
}

func (gs *GreenlightService) SendKeysend(ctx context.Context, amount uint64, destination, preimage string, custom_records []lnclient.TLVRecord) (preImage string, err error) {
func (gs *GreenlightService) SendKeysend(ctx context.Context, amount uint64, destination string, custom_records []lnclient.TLVRecord) (paymentHash string, preimage string, fee uint64, err error) {

extraTlvs := []glalby.TlvEntry{}

for _, customRecord := range custom_records {

extraTlvs = append(extraTlvs, glalby.TlvEntry{
Ty: customRecord.Type,
Value: hex.EncodeToString([]byte(customRecord.Value)),
Value: customRecord.Value,
})
}

Expand All @@ -146,10 +146,12 @@ func (gs *GreenlightService) SendKeysend(ctx context.Context, amount uint64, des

if err != nil {
logger.Logger.Errorf("Failed to send keysend payment: %v", err)
return "", err
return "", "", 0, err
}

return response.PaymentPreimage, nil
// TODO: get payment hash from response

return "", response.PaymentPreimage, 0, nil
}

func (gs *GreenlightService) GetBalance(ctx context.Context) (balance int64, err error) {
Expand Down
152 changes: 0 additions & 152 deletions lnclient/greenlight/models.go

This file was deleted.

25 changes: 13 additions & 12 deletions lnclient/ldk/ldk.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,28 +493,30 @@ func (ls *LDKService) SendPaymentSync(ctx context.Context, invoice string) (*lnc
}, nil
}

func (ls *LDKService) SendKeysend(ctx context.Context, amount uint64, destination, preimage string, custom_records []lnclient.TLVRecord) (preImage string, err error) {
func (ls *LDKService) SendKeysend(ctx context.Context, amount uint64, destination string, custom_records []lnclient.TLVRecord) (paymentHash string, preimage string, fee uint64, err error) {
paymentStart := time.Now()
customTlvs := []ldk_node.TlvEntry{}

for _, customRecord := range custom_records {
decodedValue, err := hex.DecodeString(customRecord.Value)
if err != nil {
return "", "", 0, err
}
customTlvs = append(customTlvs, ldk_node.TlvEntry{
Type: customRecord.Type,
Value: []uint8(customRecord.Value),
Value: decodedValue,
})
}

ldkEventSubscription := ls.ldkEventBroadcaster.Subscribe()
defer ls.ldkEventBroadcaster.CancelSubscription(ldkEventSubscription)

paymentHash, err := ls.node.SpontaneousPayment().Send(amount, destination, customTlvs)
paymentHash, err = ls.node.SpontaneousPayment().Send(amount, destination, customTlvs)
if err != nil {
logger.Logger.WithError(err).Error("Keysend failed")
return "", err
return paymentHash, "", 0, err
}

fee := uint64(0)

for start := time.Now(); time.Since(start) < time.Second*60; {
event := <-ldkEventSubscription

Expand All @@ -526,7 +528,7 @@ func (ls *LDKService) SendKeysend(ctx context.Context, amount uint64, destinatio
payment := ls.node.Payment(paymentHash)
if payment == nil {
logger.Logger.Errorf("Couldn't find payment by payment hash: %v", paymentHash)
return "", errors.New("Payment not found")
return paymentHash, "", 0, errors.New("Payment not found")
}

spontaneousPaymentKind, ok := payment.Kind.(ldk_node.PaymentKindSpontaneous)
Expand All @@ -539,7 +541,7 @@ func (ls *LDKService) SendKeysend(ctx context.Context, amount uint64, destinatio

if spontaneousPaymentKind.Preimage == nil {
logger.Logger.Errorf("No payment preimage for payment hash: %v", paymentHash)
return "", errors.New("Payment preimage not found")
return paymentHash, "", 0, errors.New("Payment preimage not found")
}
preimage = *spontaneousPaymentKind.Preimage

Expand Down Expand Up @@ -577,19 +579,18 @@ func (ls *LDKService) SendKeysend(ctx context.Context, amount uint64, destinatio
"failureReasonMessage": failureReasonMessage,
}).Error("Received payment failed event")

return "", fmt.Errorf("payment failed event: %v %s", failureReason, failureReasonMessage)
return paymentHash, "", 0, fmt.Errorf("payment failed event: %v %s", failureReason, failureReasonMessage)
}
}
if preimage == "" {
// TODO: this doesn't necessarily mean it will fail - we should return a different response
return "", errors.New("keysend payment timed out")
return paymentHash, "", 0, lnclient.NewTimeoutError()
}

logger.Logger.WithFields(logrus.Fields{
"duration": time.Since(paymentStart).Milliseconds(),
"fee": fee,
}).Info("Successful keysend payment")
return preimage, nil
return paymentHash, preimage, fee, nil
}

func (ls *LDKService) GetBalance(ctx context.Context) (balance int64, err error) {
Expand Down
Loading

0 comments on commit e95f1f5

Please sign in to comment.