self_payment_detection_test.go raw
1 package transactions
2
3 import (
4 "testing"
5
6 "github.com/stretchr/testify/assert"
7 "github.com/stretchr/testify/require"
8
9 "github.com/getAlby/hub/constants"
10 "github.com/getAlby/hub/db"
11 "github.com/getAlby/hub/tests"
12 )
13
14 func TestSendPaymentSync_SelfPaymentDetection_WithIncomingTransaction(t *testing.T) {
15 svc, err := tests.CreateTestService(t)
16 require.NoError(t, err)
17 defer svc.Remove()
18
19 svc.LNClient.(*tests.MockLn).Pubkey = "03cbd788f5b22bd56e2714bff756372d2293504c064e03250ed16a4dd80ad70e2c"
20
21 mockPreimage := "123preimage"
22 svc.DB.Create(&db.Transaction{
23 State: constants.TRANSACTION_STATE_PENDING,
24 Type: constants.TRANSACTION_TYPE_INCOMING,
25 PaymentRequest: tests.MockInvoice,
26 PaymentHash: tests.MockPaymentHash,
27 Preimage: &mockPreimage,
28 AmountMsat: 123000,
29 })
30
31 transactionsService := NewTransactionsService(svc.DB, svc.EventPublisher)
32 transaction, err := transactionsService.SendPaymentSync(tests.MockInvoice, nil, nil, svc.LNClient, nil, nil)
33
34 assert.NoError(t, err)
35 assert.NotNil(t, transaction)
36 assert.True(t, transaction.SelfPayment)
37 assert.Equal(t, constants.TRANSACTION_STATE_SETTLED, transaction.State)
38 assert.Equal(t, uint64(123000), transaction.AmountMsat)
39 assert.Equal(t, mockPreimage, *transaction.Preimage)
40 assert.Zero(t, transaction.FeeMsat)
41 }
42
43 func TestSendPaymentSync_SelfPaymentDetection_WithoutIncomingTransaction(t *testing.T) {
44 svc, err := tests.CreateTestService(t)
45 require.NoError(t, err)
46 defer svc.Remove()
47
48 svc.LNClient.(*tests.MockLn).Pubkey = "03cbd788f5b22bd56e2714bff756372d2293504c064e03250ed16a4dd80ad70e2c"
49
50 mockPreimage := "123preimage"
51 svc.DB.Create(&db.Transaction{
52 State: constants.TRANSACTION_STATE_FAILED, // otherwise we won't be able to create a new payment with the same hash
53 Type: constants.TRANSACTION_TYPE_OUTGOING,
54 PaymentRequest: tests.MockInvoice,
55 PaymentHash: tests.MockPaymentHash,
56 Preimage: &mockPreimage,
57 AmountMsat: 123000,
58 })
59
60 transactionsService := NewTransactionsService(svc.DB, svc.EventPublisher)
61 transaction, err := transactionsService.SendPaymentSync(tests.MockInvoice, nil, nil, svc.LNClient, nil, nil)
62
63 assert.NoError(t, err)
64 assert.NotNil(t, transaction)
65 assert.False(t, transaction.SelfPayment)
66 assert.Equal(t, constants.TRANSACTION_STATE_SETTLED, transaction.State)
67 assert.Equal(t, uint64(123000), transaction.AmountMsat)
68 }
69
70 // Self-payment detection is based solely on having an incoming transaction for
71 // the same invoice, not on the invoice payee matching our node pubkey. A
72 // mismatching pubkey must not prevent detection: some backends (e.g. Bark)
73 // don't own the node behind the invoice, and the payee is unavailable for
74 // private BOLT12 payments.
75 func TestSendPaymentSync_SelfPaymentDetection_DifferentPubkey(t *testing.T) {
76 svc, err := tests.CreateTestService(t)
77 require.NoError(t, err)
78 defer svc.Remove()
79
80 svc.LNClient.(*tests.MockLn).Pubkey = "different_pubkey_123"
81
82 mockPreimage := "123preimage"
83 svc.DB.Create(&db.Transaction{
84 State: constants.TRANSACTION_STATE_PENDING,
85 Type: constants.TRANSACTION_TYPE_INCOMING,
86 PaymentRequest: tests.MockInvoice,
87 PaymentHash: tests.MockPaymentHash,
88 Preimage: &mockPreimage,
89 AmountMsat: 123000,
90 })
91
92 transactionsService := NewTransactionsService(svc.DB, svc.EventPublisher)
93 transaction, err := transactionsService.SendPaymentSync(tests.MockInvoice, nil, nil, svc.LNClient, nil, nil)
94
95 assert.NoError(t, err)
96 assert.NotNil(t, transaction)
97 assert.True(t, transaction.SelfPayment)
98 assert.Equal(t, constants.TRANSACTION_STATE_SETTLED, transaction.State)
99 }
100