diff --git a/go.mod b/go.mod index 3c3af6ee..aa8bcb0a 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 8ef56b15..b86d2a02 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/ldk.go b/ldk.go index 8af10181..9fa368c2 100644 --- a/ldk.go +++ b/ldk.go @@ -9,6 +9,7 @@ import ( "path/filepath" "sort" "strconv" + "strings" "sync" "time" @@ -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 @@ -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, @@ -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 } @@ -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) } } @@ -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 { @@ -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 @@ -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 }