diff --git a/alby.go b/alby.go index 7a0e0f9d..65205486 100644 --- a/alby.go +++ b/alby.go @@ -709,6 +709,18 @@ func albyInvoiceToTransaction(invoice *AlbyInvoice) *Nip47Transaction { preimage = invoice.Preimage } + var expiresAt *int64 + if invoice.ExpiresAt != nil { + expiresAtUnix := invoice.ExpiresAt.Unix() + expiresAt = &expiresAtUnix + } + + var settledAt *int64 + if invoice.SettledAt != nil { + settledAtUnix := invoice.SettledAt.Unix() + settledAt = &settledAtUnix + } + return &Nip47Transaction{ Type: invoice.Type, Invoice: invoice.PaymentRequest, @@ -718,9 +730,9 @@ func albyInvoiceToTransaction(invoice *AlbyInvoice) *Nip47Transaction { PaymentHash: invoice.PaymentHash, Amount: invoice.Amount * 1000, FeesPaid: 0, // TODO: support fees - CreatedAt: invoice.CreatedAt, - ExpiresAt: invoice.ExpiresAt, - SettledAt: invoice.SettledAt, + CreatedAt: invoice.CreatedAt.Unix(), + ExpiresAt: expiresAt, + SettledAt: settledAt, Metadata: invoice.Metadata, } } diff --git a/lnd.go b/lnd.go index 82342c1c..06ab998d 100644 --- a/lnd.go +++ b/lnd.go @@ -7,8 +7,8 @@ import ( "encoding/hex" "errors" "sort" - "time" "strings" + "time" "github.com/getAlby/nostr-wallet-connect/lnd" decodepay "github.com/nbd-wtf/ln-decodepay" @@ -96,7 +96,7 @@ func (svc *LNDService) ListTransactions(ctx context.Context, senderPubkey string continue } var paymentRequest decodepay.Bolt11 - var expiresAt *time.Time + var expiresAt *int64 var description string var descriptionHash string if payment.PaymentRequest != "" { @@ -108,17 +108,17 @@ func (svc *LNDService) ListTransactions(ctx context.Context, senderPubkey string return nil, err } - expiresAt = &time.Time{} - *expiresAt = time.UnixMilli(int64(paymentRequest.CreatedAt) * 1000).Add(time.Duration(paymentRequest.Expiry) * time.Second) + expiresAtUnix := time.UnixMilli(int64(paymentRequest.CreatedAt) * 1000).Add(time.Duration(paymentRequest.Expiry) * time.Second).Unix() + expiresAt = &expiresAtUnix description = paymentRequest.Description descriptionHash = paymentRequest.DescriptionHash } - var settledAt *time.Time + var settledAt *int64 if payment.Status == lnrpc.Payment_SUCCEEDED { // FIXME: how to get the actual settled at time? - settledAt = &time.Time{} - *settledAt = time.Unix(0, payment.CreationTimeNs) + settledAtUnix := time.Unix(0, payment.CreationTimeNs).Unix() + settledAt = &settledAtUnix } transaction := Nip47Transaction{ @@ -128,7 +128,7 @@ func (svc *LNDService) ListTransactions(ctx context.Context, senderPubkey string PaymentHash: payment.PaymentHash, Amount: payment.ValueMsat, FeesPaid: payment.FeeMsat, - CreatedAt: time.Unix(0, payment.CreationTimeNs), + CreatedAt: time.Unix(0, payment.CreationTimeNs).Unix(), Description: description, DescriptionHash: descriptionHash, ExpiresAt: expiresAt, @@ -140,7 +140,7 @@ func (svc *LNDService) ListTransactions(ctx context.Context, senderPubkey string // sort by created date descending sort.SliceStable(transactions, func(i, j int) bool { - return transactions[i].CreatedAt.After(transactions[j].CreatedAt) + return transactions[i].CreatedAt > transactions[j].CreatedAt }) return transactions, nil @@ -357,18 +357,17 @@ func NewLNDService(ctx context.Context, svc *Service, e *echo.Echo) (result *LND } func lndInvoiceToTransaction(invoice *lnrpc.Invoice) *Nip47Transaction { - var settledAt *time.Time + var settledAt *int64 var preimage string if invoice.State == lnrpc.Invoice_SETTLED { - settledAt = &time.Time{} - *settledAt = time.Unix(invoice.SettleDate, 0) + settledAt = &invoice.SettleDate // only set preimage if invoice is settled preimage = hex.EncodeToString(invoice.RPreimage) } - var expiresAt *time.Time + var expiresAt *int64 if invoice.Expiry > 0 { - expiresAt = &time.Time{} - *expiresAt = time.Unix(invoice.SettleDate, 0) + expiresAtUnix := invoice.CreationDate + invoice.Expiry + expiresAt = &expiresAtUnix } return &Nip47Transaction{ @@ -380,7 +379,7 @@ func lndInvoiceToTransaction(invoice *lnrpc.Invoice) *Nip47Transaction { PaymentHash: hex.EncodeToString(invoice.RHash), Amount: invoice.ValueMsat, FeesPaid: invoice.AmtPaidMsat, - CreatedAt: time.Unix(invoice.CreationDate, 0), + CreatedAt: invoice.CreationDate, SettledAt: settledAt, ExpiresAt: expiresAt, // TODO: Metadata (e.g. keysend) diff --git a/models.go b/models.go index 4f56a251..f7ad3c42 100644 --- a/models.go +++ b/models.go @@ -135,9 +135,9 @@ type Nip47Transaction struct { PaymentHash string `json:"payment_hash"` Amount int64 `json:"amount"` FeesPaid int64 `json:"fees_paid"` - CreatedAt time.Time `json:"created_at"` - ExpiresAt *time.Time `json:"expires_at"` - SettledAt *time.Time `json:"settled_at"` + CreatedAt int64 `json:"created_at"` + ExpiresAt *int64 `json:"expires_at"` + SettledAt *int64 `json:"settled_at"` Metadata interface{} `json:"metadata,omitempty"` } diff --git a/service_test.go b/service_test.go index fecefee3..8cc49e45 100644 --- a/service_test.go +++ b/service_test.go @@ -111,6 +111,7 @@ var mockNodeInfo = NodeInfo{ } var mockTime = time.Unix(1693876963, 0) +var mockTimeUnix = mockTime.Unix() var mockTransactions = []Nip47Transaction{ { @@ -122,7 +123,7 @@ var mockTransactions = []Nip47Transaction{ PaymentHash: "payment_hash_1", Amount: 1000, FeesPaid: 50, - SettledAt: &mockTime, + SettledAt: &mockTimeUnix, Metadata: map[string]interface{}{ "key1": "value1", "key2": 42, @@ -137,7 +138,7 @@ var mockTransactions = []Nip47Transaction{ PaymentHash: "payment_hash_2", Amount: 2000, FeesPaid: 75, - SettledAt: &mockTime, + SettledAt: &mockTimeUnix, }, } var mockTransaction = &mockTransactions[0] @@ -571,7 +572,7 @@ func TestHandleEvent(t *testing.T) { assert.Equal(t, mockTransactions[0].PaymentHash, transaction.PaymentHash) assert.Equal(t, mockTransactions[0].Amount, transaction.Amount) assert.Equal(t, mockTransactions[0].FeesPaid, transaction.FeesPaid) - assert.Equal(t, mockTransactions[0].SettledAt.Unix(), transaction.SettledAt.Unix()) + assert.Equal(t, mockTransactions[0].SettledAt, transaction.SettledAt) // get_info: without permission newPayload, err = nip04.Encrypt(nip47GetInfoJson, ss)