-
Notifications
You must be signed in to change notification settings - Fork 1
/
server_test.go
228 lines (208 loc) · 7.12 KB
/
server_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package main
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"testing"
"time"
"github.com/breez/breez-lnurl/channel"
"github.com/breez/breez-lnurl/lnurl"
"github.com/breez/breez-lnurl/persist"
"github.com/breez/lspd/lightning"
"github.com/btcsuite/btcd/btcec/v2/ecdsa"
"github.com/decred/dcrd/dcrec/secp256k1/v4"
"github.com/gorilla/mux"
"github.com/tv42/zbase32"
)
const (
serverAddress = "localhost:8080"
hookServerAddress = "localhost:8085"
testFeature = "testFeature"
testEndpoint = "testEndpoint"
)
func setupServer(storage persist.Store) {
serverURL, err := url.Parse(fmt.Sprintf("http://%v", serverAddress))
if err != nil {
log.Fatalf("failed to parse server URL %v", err)
}
server := NewServer(serverURL, serverURL, storage)
go func() {
persist.NewCleanupService(storage).Start(context.Background())
}()
go func() {
if err := server.Serve(); err != nil {
log.Printf("server.Serve error: %v", err)
}
}()
}
func setupHookServer(t *testing.T) {
callbackRouter := mux.NewRouter()
callbackRouter.HandleFunc("/callback", func(w http.ResponseWriter, r *http.Request) {
allBody, _ := io.ReadAll(r.Body)
var payload channel.WebhookMessage
if err := json.Unmarshal(allBody, &payload); err != nil {
t.Errorf("unmarshal proxy payload, expected no error, got %v", err)
}
replyURL, ok := payload.Data["reply_url"].(string)
if !ok {
t.Errorf("failed to extract reply_url %+v", payload)
}
response, err := http.Post(replyURL, "application/json", bytes.NewBuffer([]byte(`{"status": "ok"}`)))
if err != nil {
t.Errorf("failed to invoke hook callback %v", err)
}
if response.StatusCode != 200 {
t.Errorf("expected status code 200, got %v", response.StatusCode)
}
}).Methods("POST")
go func() {
if err := http.ListenAndServe(hookServerAddress, callbackRouter); err != nil {
t.Errorf("failed to start hook server %v", err)
}
}()
}
func TestRegisterWebhook(t *testing.T) {
storage := &persist.MemoryStore{}
setupServer(storage)
setupHookServer(t)
// Test adding webhook
url := fmt.Sprintf("http://%v/callback", hookServerAddress)
time := time.Now().Unix()
messgeToSign := fmt.Sprintf("%v-%v", time, url)
msg := append(lightning.SignedMsgPrefix, []byte(messgeToSign)...)
first := sha256.Sum256([]byte(msg))
second := sha256.Sum256(first[:])
privKey, err := secp256k1.GeneratePrivateKey()
if err != nil {
t.Errorf("failed to generate private key %v", err)
}
pubkey := privKey.PubKey()
sig, err := ecdsa.SignCompact(privKey, second[:], true)
if err != nil {
t.Errorf("failed to sign signature %v", err)
}
serializedPubkey := hex.EncodeToString(pubkey.SerializeCompressed())
addWebhookPayload, _ := json.Marshal(lnurl.RegisterLnurlPayRequest{
Time: time,
WebhookUrl: url,
Signature: zbase32.EncodeToString(sig),
})
httpRes, err := http.Post(fmt.Sprintf("http://%v/lnurlpay/%v", serverAddress, serializedPubkey), "application/json", bytes.NewBuffer(addWebhookPayload))
if err != nil {
t.Errorf("expected no error, got %v", err)
}
if httpRes.StatusCode != 200 {
t.Errorf("expected status code 200, got %v", httpRes.StatusCode)
}
webhook, _ := storage.GetLastUpdated(context.Background(), serializedPubkey)
if webhook == nil {
t.Errorf("expected webhook to be registered")
}
// Test lnurlpay info endpoint
u := fmt.Sprintf("http://%v/lnurlp/%v", serverAddress, serializedPubkey)
proxyRes, err := http.Get(u)
if err != nil {
t.Errorf("expected no error, got %v", err)
}
if proxyRes.StatusCode != 200 {
t.Errorf("expected status code 200, got %v", proxyRes.StatusCode)
}
// Test lnurlpay info endpoint with invalid amount
u = fmt.Sprintf("http://%v/lnurlpay/%v/invoice", serverAddress, serializedPubkey)
response := testInvoiceRequest(t, u, serializedPubkey)
if response.Status != "ERROR" {
t.Errorf("Got error from lnurlpay invoice response %v", response.Status)
}
// Test lnurlpay info endpoint with valid amount
u = fmt.Sprintf("http://%v/lnurlpay/%v/invoice?amount=100", serverAddress, serializedPubkey)
response = testInvoiceRequest(t, u, serializedPubkey)
if response.Status == "ERROR" {
t.Errorf("Got error from lnurlpay invoice response %v", response.Status)
}
}
func TestRegisterWebhookWithUsername(t *testing.T) {
storage := &persist.MemoryStore{}
setupServer(storage)
setupHookServer(t)
// Test adding webhook
url := fmt.Sprintf("http://%v/callback", hookServerAddress)
time := time.Now().Unix()
username := "testuser"
messgeToSign := fmt.Sprintf("%v-%v-%v", time, url, username)
msg := append(lightning.SignedMsgPrefix, []byte(messgeToSign)...)
first := sha256.Sum256([]byte(msg))
second := sha256.Sum256(first[:])
privKey, err := secp256k1.GeneratePrivateKey()
if err != nil {
t.Errorf("failed to generate private key %v", err)
}
pubkey := privKey.PubKey()
sig, err := ecdsa.SignCompact(privKey, second[:], true)
if err != nil {
t.Errorf("failed to sign signature %v", err)
}
serializedPubkey := hex.EncodeToString(pubkey.SerializeCompressed())
addWebhookPayload, _ := json.Marshal(lnurl.RegisterLnurlPayRequest{
Time: time,
WebhookUrl: url,
Username: &username,
Signature: zbase32.EncodeToString(sig),
})
httpRes, err := http.Post(fmt.Sprintf("http://%v/lnurlpay/%v", serverAddress, serializedPubkey), "application/json", bytes.NewBuffer(addWebhookPayload))
if err != nil {
t.Errorf("expected no error, got %v", err)
}
if httpRes.StatusCode != 200 {
t.Errorf("expected status code 200, got %v", httpRes.StatusCode)
}
webhook, _ := storage.GetLastUpdated(context.Background(), serializedPubkey)
if webhook == nil {
t.Errorf("expected webhook to be registered")
}
// Test lnurlpay info endpoint
u := fmt.Sprintf("http://%v/.well-known/lnurlp/%v", serverAddress, username)
proxyRes, err := http.Get(u)
if err != nil {
t.Errorf("expected no error, got %v", err)
}
if proxyRes.StatusCode != 200 {
t.Errorf("expected status code 200, got %v", proxyRes.StatusCode)
}
// Test lnurlpay info endpoint with invalid amount
u = fmt.Sprintf("http://%v/lnurlpay/%v/invoice", serverAddress, username)
response := testInvoiceRequest(t, u, serializedPubkey)
if response.Status != "ERROR" {
t.Errorf("Got error from lnurlpay invoice response %v", response.Status)
}
// Test lnurlpay info endpoint with valid amount
u = fmt.Sprintf("http://%v/lnurlpay/%v/invoice?amount=100", serverAddress, username)
response = testInvoiceRequest(t, u, serializedPubkey)
if response.Status == "ERROR" {
t.Errorf("Got error from lnurlpay invoice response %v", response.Status)
}
}
func testInvoiceRequest(t *testing.T, url string, serializedPubkey string) lnurl.LnurlPayStatus {
proxyRes, err := http.Get(url)
if err != nil {
t.Errorf("expected no error, got %v", err)
}
if proxyRes.StatusCode != 200 {
t.Errorf("expected status code 200, got %v", proxyRes.StatusCode)
}
body, err := io.ReadAll(proxyRes.Body)
if err != nil {
t.Errorf("failed to read lnurlpay invoice response body %v", err)
}
var response lnurl.LnurlPayStatus
if err := json.Unmarshal(body, &response); err != nil {
t.Errorf("failed to unmarhsal lnurlpay invoice response %v", err)
}
return response
}