Skip to content

Commit

Permalink
Merge pull request #206 from getAlby/fix/timestamps
Browse files Browse the repository at this point in the history
fix: make timestamps numbers to follow NIP-47
  • Loading branch information
rolznz authored Jan 9, 2024
2 parents e6c4236 + f298414 commit 1b76186
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 25 deletions.
18 changes: 15 additions & 3 deletions alby.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
}
}
31 changes: 15 additions & 16 deletions lnd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 != "" {
Expand All @@ -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{
Expand All @@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -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{
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down
7 changes: 4 additions & 3 deletions service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ var mockNodeInfo = NodeInfo{
}

var mockTime = time.Unix(1693876963, 0)
var mockTimeUnix = mockTime.Unix()

var mockTransactions = []Nip47Transaction{
{
Expand All @@ -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,
Expand All @@ -137,7 +138,7 @@ var mockTransactions = []Nip47Transaction{
PaymentHash: "payment_hash_2",
Amount: 2000,
FeesPaid: 75,
SettledAt: &mockTime,
SettledAt: &mockTimeUnix,
},
}
var mockTransaction = &mockTransactions[0]
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 1b76186

Please sign in to comment.