Skip to content

Commit

Permalink
Merge branch 'main' into chore/update-strike
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz committed Feb 5, 2024
2 parents 382a67d + af7ecd8 commit ed99874
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 43 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ NOSTR_PRIVKEY="your priv key"
COOKIE_SECRET=secretsecret
COOKIE_DOMAIN=
RELAY=wss://relay.getalby.com/v1
#RELAY=ws://localhost:7447/v1
PUBLIC_RELAY=
PORT=8080

# Backend specific config
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/workflow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
if: ${{ github.ref == 'refs/heads/main' }}
uses: fjogeleit/[email protected]
with:
valueFile: 'alby-simnet-deployment/values.yaml'
valueFile: 'alby-simnet-deployment/values/nwc.yaml'
propertyPath: 'nwc.image.tag'
value: ${{ steps.build.outputs.tags }}
repository: getalby/alby-deployment
Expand Down
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
17 changes: 17 additions & 0 deletions migrations/202401092201_add_events_id_index.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package migrations

import (
"github.com/go-gormigrate/gormigrate/v2"
"gorm.io/gorm"
)

// Create a composite index to improve performance of finding the latest nostr event for an app
var _202401092201_add_events_id_index = &gormigrate.Migration{
ID: "202401092201_add_events_id_index",
Migrate: func(tx *gorm.DB) error {
return tx.Exec("CREATE INDEX IF NOT EXISTS idx_nostr_events_app_id_and_id ON nostr_events(app_id, id)").Error
},
Rollback: func(tx *gorm.DB) error {
return nil
},
}
3 changes: 2 additions & 1 deletion migrations/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ func Migrate(db *gorm.DB) error {
_202309271616_initial_migration,
_202309271617_fix_preimage_null,
_202309271618_add_payment_sum_index,
_202401092201_add_events_id_index,
})

return m.Migrate()
}
}
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
40 changes: 24 additions & 16 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,15 @@ func (svc *Service) GetUser(c echo.Context) (user *User, err error) {
}

func (svc *Service) StartSubscription(ctx context.Context, sub *nostr.Subscription) error {
for {
if sub.Relay.ConnectionError != nil {
return sub.Relay.ConnectionError
}
select {
case <-ctx.Done():
svc.Logger.Info("Exiting subscription.")
return nil
case <-sub.EndOfStoredEvents:
if !svc.ReceivedEOS {
svc.Logger.Info("Received EOS")
}
svc.ReceivedEOS = true
case event := <-sub.Events:
go func() {
go func() {
<-sub.EndOfStoredEvents
svc.ReceivedEOS = true
svc.Logger.Info("Received EOS")
}()

go func() {
for event := range sub.Events {
go func(event *nostr.Event) {
resp, err := svc.HandleEvent(ctx, event)
if err != nil {
svc.Logger.WithFields(logrus.Fields{
Expand Down Expand Up @@ -132,8 +126,22 @@ func (svc *Service) StartSubscription(ctx context.Context, sub *nostr.Subscripti
}).Info("Reply sent but no response from relay (timeout)")
}
}
}()
}(event)
}
svc.Logger.Info("Subscription ended")
}()

select {
case <-sub.Relay.Context().Done():
svc.Logger.Errorf("Relay error %v", sub.Relay.ConnectionError)
return sub.Relay.ConnectionError
case <-ctx.Done():
if ctx.Err() != context.Canceled {
svc.Logger.Errorf("Subscription error %v", ctx.Err())
return ctx.Err()
}
svc.Logger.Info("Exiting subscription.")
return nil
}
}

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 ed99874

Please sign in to comment.