Skip to content
This repository has been archived by the owner on Sep 2, 2024. It is now read-only.

Feat: read bolt11 invoice from LDK payment #89

Merged
merged 8 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/breez/breez-sdk-go v0.2.14
github.com/davrux/echo-logrus/v4 v4.0.3
github.com/getAlby/glalby-go v0.0.0-20240305115032-de77adfd67c1
github.com/getAlby/ldk-node-go v0.0.0-20240305114848-601fcc2107e9
github.com/getAlby/ldk-node-go v0.0.0-20240306112940-da4da77651d8
github.com/go-gormigrate/gormigrate/v2 v2.1.1
github.com/gorilla/sessions v1.2.1
github.com/labstack/echo-contrib v0.14.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWo
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/getAlby/glalby-go v0.0.0-20240305115032-de77adfd67c1 h1:RF76EWDgukygUZR4nhBLCbN1gBTn6x47KJRpIydDHGw=
github.com/getAlby/glalby-go v0.0.0-20240305115032-de77adfd67c1/go.mod h1:ViyJvjlvv0GCesTJ7mb3fBo4G+/qsujDAFN90xZ7a9U=
github.com/getAlby/ldk-node-go v0.0.0-20240305114848-601fcc2107e9 h1:TCs3FTsfTAiOYtbaPjN2ztkhf78u2LVNlRs/f0+tQNk=
github.com/getAlby/ldk-node-go v0.0.0-20240305114848-601fcc2107e9/go.mod h1:8BRjtKcz8E0RyYTPEbMS8VIdgredcGSLne8vHDtcRLg=
github.com/getAlby/ldk-node-go v0.0.0-20240306112940-da4da77651d8 h1:jBHPI3pGnAC3MITXaOQhjpr8eqHiKmYiftUnf1yQfSM=
github.com/getAlby/ldk-node-go v0.0.0-20240306112940-da4da77651d8/go.mod h1:8BRjtKcz8E0RyYTPEbMS8VIdgredcGSLne8vHDtcRLg=
github.com/getsentry/raven-go v0.2.0 h1:no+xWJRb5ZI7eE8TWgIq1jLulQiIoLG0IfYxv5JYMGs=
github.com/getsentry/raven-go v0.2.0/go.mod h1:KungGk8q33+aIAZUIVWZDr2OfAEBsO49PX4NzFV5kcQ=
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
Expand Down
68 changes: 55 additions & 13 deletions ldk.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path/filepath"
"sort"
"strconv"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -228,12 +229,16 @@ func (gs *LDKService) SendPaymentSync(ctx context.Context, payReq string) (preim
}

func (gs *LDKService) SendKeysend(ctx context.Context, amount int64, destination, preimage string, custom_records []lnclient.TLVRecord) (preImage string, err error) {
customTlvs := []ldk_node.TlvEntry{}

if len(custom_records) > 0 {
log.Printf("FIXME: TLVs not supported")
for _, customRecord := range custom_records {
customTlvs = append(customTlvs, ldk_node.TlvEntry{
Type: customRecord.Type,
Value: []uint8(customRecord.Value),
})
}

paymentHash, err := gs.node.SendSpontaneousPayment(uint64(amount), destination)
paymentHash, err := gs.node.SendSpontaneousPayment(uint64(amount), destination, customTlvs)
if err != nil {
gs.svc.Logger.Errorf("Keysend failed: %v", err)
return "", err
Expand Down Expand Up @@ -293,7 +298,7 @@ func (gs *LDKService) MakeInvoice(ctx context.Context, amount int64, description
Invoice: invoice,
PaymentHash: paymentRequest.PaymentHash,
Amount: amount,
CreatedAt: time.Now().Unix(),
CreatedAt: int64(paymentRequest.CreatedAt),
ExpiresAt: expiresAt,
Description: description,
DescriptionHash: descriptionHash,
Expand All @@ -310,7 +315,12 @@ func (gs *LDKService) LookupInvoice(ctx context.Context, paymentHash string) (tr
return nil, errors.New("Payment not found")
}

transaction = ldkPaymentToTransaction(payment)
transaction, err = gs.ldkPaymentToTransaction(payment)

if err != nil {
gs.svc.Logger.Errorf("Failed to map transaction: %v", err)
return nil, err
}

return transaction, nil
}
Expand All @@ -323,7 +333,14 @@ func (gs *LDKService) ListTransactions(ctx context.Context, from, until, limit,

for _, payment := range payments {
if payment.Status == ldk_node.PaymentStatusSucceeded {
transactions = append(transactions, *ldkPaymentToTransaction(&payment))
transaction, err := gs.ldkPaymentToTransaction(&payment)

if err != nil {
gs.svc.Logger.Errorf("Failed to map transaction: %v", err)
continue
}

transactions = append(transactions, *transaction)
}
}

Expand Down Expand Up @@ -460,12 +477,34 @@ func (gs *LDKService) GetOnchainBalance(ctx context.Context) (int64, error) {
return int64(balances.SpendableOnchainBalanceSats), nil
}

func ldkPaymentToTransaction(payment *ldk_node.PaymentDetails) *Nip47Transaction {
func (gs *LDKService) ldkPaymentToTransaction(payment *ldk_node.PaymentDetails) (*Nip47Transaction, error) {
transactionType := "incoming"
if payment.Direction == ldk_node.PaymentDirectionOutbound {
transactionType = "outgoing"
}

var expiresAt *int64
var createdAt int64
var description string
var descriptionHash string
var bolt11Invoice string
if payment.Bolt11Invoice != nil {
bolt11Invoice = *payment.Bolt11Invoice
paymentRequest, err := decodepay.Decodepay(strings.ToLower(bolt11Invoice))
if err != nil {
gs.svc.Logger.WithFields(logrus.Fields{
"bolt11": bolt11Invoice,
}).Errorf("Failed to decode bolt11 invoice: %v", err)

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
}

preimage := ""
var settledAt *int64
if payment.Status == ldk_node.PaymentStatusSucceeded {
Expand All @@ -474,8 +513,7 @@ func ldkPaymentToTransaction(payment *ldk_node.PaymentDetails) *Nip47Transaction
preimage = *payment.Preimage
}
// TODO: use payment settle time
now := time.Now().Unix()
settledAt = &now
settledAt = &createdAt
}

var amount uint64 = 0
Expand All @@ -484,12 +522,16 @@ func ldkPaymentToTransaction(payment *ldk_node.PaymentDetails) *Nip47Transaction
}

return &Nip47Transaction{
Type: transactionType,
// TODO: get bolt11 invoice from payment
//Invoice: payment.,
Type: transactionType,
Preimage: preimage,
PaymentHash: payment.Hash,
SettledAt: settledAt,
Amount: int64(amount),
}
Invoice: bolt11Invoice,
//FeesPaid: payment.FeeMsat,
CreatedAt: createdAt,
Description: description,
DescriptionHash: descriptionHash,
ExpiresAt: expiresAt,
}, nil
}
Loading