Skip to content

Commit

Permalink
fix: only decode payments with an invoice, add paging
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz committed Jan 31, 2024
1 parent 33ce4db commit d121ac3
Showing 1 changed file with 40 additions and 11 deletions.
51 changes: 40 additions & 11 deletions breez.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,26 @@ func (bs *BreezService) LookupInvoice(ctx context.Context, senderPubkey string,
}

func (bs *BreezService) ListTransactions(ctx context.Context, senderPubkey string, from, until, limit, offset uint64, unpaid bool, invoiceType string) (transactions []Nip47Transaction, err error) {
payments, err := bs.svc.ListPayments(breez_sdk.ListPaymentsRequest{})

request := breez_sdk.ListPaymentsRequest{}
if limit > 0 {
limit32 := uint32(limit)
request.Limit = &limit32
}
if offset > 0 {
offset32 := uint32(offset)
request.Offset = &offset32
}
if from > 0 {
from64 := int64(from)
request.FromTimestamp = &from64
}
if until > 0 {
until64 := int64(until)
request.ToTimestamp = &until64
}

payments, err := bs.svc.ListPayments(request)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -228,15 +247,25 @@ func breezPaymentToTransaction(payment *breez_sdk.Payment) (*Nip47Transaction, e
txType = "incoming"
}

paymentRequest, err := decodepay.Decodepay(strings.ToLower(lnDetails.Data.Bolt11))
if err != nil {
log.Printf("Failed to decode bolt11 invoice: %v", payment)
return nil, err
}
createdAt := payment.PaymentTime
var expiresAt *int64
description := lnDetails.Data.Label
descriptionHash := ""

createdAt := int64(paymentRequest.CreatedAt)
expiresAtUnix := time.UnixMilli(int64(paymentRequest.CreatedAt) * 1000).Add(time.Duration(paymentRequest.Expiry) * time.Second).Unix()
expiresAt := &expiresAtUnix
if lnDetails.Data.Bolt11 != "" {
// TODO: Breez should provide these details so we don't need to manually decode the invoice
paymentRequest, err := decodepay.Decodepay(strings.ToLower(lnDetails.Data.Bolt11))
if err != nil {
log.Printf("Failed to decode bolt11 invoice: %v", payment)
return nil, err
}

createdAt = int64(paymentRequest.CreatedAt)
expiresAtUnix := time.UnixMilli(int64(paymentRequest.CreatedAt) * 1000).Add(time.Duration(paymentRequest.Expiry) * time.Second).Unix()
expiresAt = &expiresAtUnix
description = paymentRequest.Description
descriptionHash = paymentRequest.DescriptionHash
}

tx := &Nip47Transaction{
Type: txType,
Expand All @@ -248,8 +277,8 @@ func breezPaymentToTransaction(payment *breez_sdk.Payment) (*Nip47Transaction, e
CreatedAt: createdAt,
ExpiresAt: expiresAt,
Metadata: nil,
Description: paymentRequest.Description,
DescriptionHash: paymentRequest.DescriptionHash,
Description: description,
DescriptionHash: descriptionHash,
}
if payment.Status == breez_sdk.PaymentStatusComplete {
settledAt := payment.PaymentTime
Expand Down

0 comments on commit d121ac3

Please sign in to comment.