-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
390 lines (335 loc) · 9.27 KB
/
main.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package main
import (
"bytes"
"encoding/base64"
"encoding/json"
"flag"
"fmt"
"html/template"
baselog "log"
"net/http"
"os"
"strings"
"github.com/MadAppGang/httplog"
"github.com/btcsuite/btclog"
"github.com/btcsuite/btcutil/bech32"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/macaroons"
"github.com/skip2/go-qrcode"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"gopkg.in/macaroon.v2"
)
type ServerConfig struct {
RPCHost string
InvoiceMacaroonPath string
TLSCertPath string
WorkingDir string
ExternalURL string
ListAllURLs bool
LightningAddresses []string
MinSendableMsat int
MaxSendableMsat int
MaxCommentLength int
Tag string
Metadata [][]string
Thumbnail string
SuccessMessage string
InvoiceCallback string
AddressServerPort int
Nostr *NostrConfig
Notificators []notificatorConfig
}
type LNUrlPay struct {
MinSendable int `json:"minSendable"`
MaxSendable int `json:"maxSendable"`
CommentAllowed int `json:"commentAllowed"`
Tag string `json:"tag"`
Metadata string `json:"metadata"`
Callback string `json:"callback"`
}
type Invoice struct {
Pr string `json:"pr"`
Routes []string `json:"routes"`
SuccessAction *SuccessAction `json:"successAction"`
}
type Error struct {
Status string `json:"status"`
Reason string `json:"reason"`
}
type SuccessAction struct {
Tag string `json:"tag"`
Message string `json:"message,omitempty"`
}
var (
log btclog.Logger
)
type NostrConfig struct {
Names map[string]string `json:"names"`
Relays map[string][]string `json:"relays"`
}
func main() {
c := flag.String(
"config", "./config.json", "Specify the configuration file",
)
flag.Parse()
configBytes, err := os.ReadFile(*c)
if err != nil {
baselog.Fatalf("cannot read config file '%s': %v", *c, err)
}
config := ServerConfig{}
err = json.Unmarshal(configBytes, &config)
if err != nil {
baselog.Fatalf("cannot decode config JSON %v", err)
}
workingDir := config.WorkingDir
log, err = GetLogger(workingDir, "LNADDR")
if err != nil {
baselog.Fatalf("cannot get logger %v", err)
}
log.Infof("Starting lightning address server on port %v...",
config.AddressServerPort)
clientConn, err := getClientConn(
config.RPCHost, config.TLSCertPath, config.InvoiceMacaroonPath,
)
if err != nil {
log.Errorf("unable to get a lnd client connection: %v", err)
return
}
lndClient := lnrpc.NewLightningClient(clientConn)
settlementHandler := NewSettlementHandler(lndClient)
invoiceManager := NewInvoiceManager(
&InvoiceManagerConfig{
LndClient: lndClient,
SettlementHandler: settlementHandler,
},
)
setupHandlerPerAddress(config)
setupNostrHandlers(config.Nostr)
setupNotificators(config)
setupIndexHandler(config)
http.HandleFunc("/invoice/", useLogger(
invoiceManager.handleInvoiceCreation(config),
))
err = http.ListenAndServe(
fmt.Sprintf(":%d", config.AddressServerPort), nil,
)
if err != nil {
log.Errorf("unable to start server: %v", err)
}
}
func useLogger(h http.HandlerFunc) http.HandlerFunc {
logger := httplog.LoggerWithConfig(httplog.LoggerConfig{
Formatter: httplog.ChainLogFormatter(
httplog.DefaultLogFormatter,
httplog.RequestHeaderLogFormatter,
httplog.RequestBodyLogFormatter,
httplog.ResponseHeaderLogFormatter,
httplog.ResponseBodyLogFormatter,
),
CaptureBody: true,
})
return logger(h).ServeHTTP
}
func setupHandlerPerAddress(config ServerConfig) {
metadata, err := metadataToString(config)
if err != nil {
return
}
for _, addr := range config.LightningAddresses {
addr := strings.Split(addr, "@")[0]
endpoint := fmt.Sprintf("/.well-known/lnurlp/%s", addr)
http.HandleFunc(
endpoint, useLogger(handleLNUrlp(config, metadata)),
)
}
}
func handleLNUrlp(config ServerConfig, metadata string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
resp := LNUrlPay{
MinSendable: config.MinSendableMsat,
MaxSendable: config.MaxSendableMsat,
CommentAllowed: config.MaxCommentLength,
Tag: config.Tag,
Metadata: metadata,
Callback: config.InvoiceCallback,
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(resp)
}
}
func setupNostrHandlers(nostr *NostrConfig) {
if nostr == nil {
return
}
http.HandleFunc(
"/.well-known/nostr.json",
useLogger(func(w http.ResponseWriter, r *http.Request) {
log.Infof("Nostr request: %#v\n", *r)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(nostr)
}),
)
}
func setupIndexHandler(config ServerConfig) {
if !config.ListAllURLs || len(config.LightningAddresses) == 0 ||
config.ExternalURL == "" {
return
}
type user struct {
User string
Encoded string
QRCode string
}
var users []user
for _, addr := range config.LightningAddresses {
userName := strings.Split(addr, "@")[0]
url := fmt.Sprintf("%s/.well-known/lnurlp/%s",
config.ExternalURL, userName)
converted, err := bech32.ConvertBits([]byte(url), 8, 5, true)
if err != nil {
log.Errorf("Unable to convert url: %v", err)
}
lnurl, err := bech32.Encode("lnurl", converted)
if err != nil {
log.Errorf("Unable to encode url: %v", err)
continue
}
png, err := qrcode.Encode(lnurl, qrcode.Highest, 256)
if err != nil {
log.Errorf("Unable to encode QR code: %v", err)
continue
}
users = append(users, user{
User: userName,
Encoded: lnurl,
QRCode: base64.StdEncoding.EncodeToString(png),
})
}
htmlTemplate := `<!DOCTYPE html>
<html>
<head>
<title>LNURLs</title>
</head>
<body>
<h1>LNURLs</h1>
<ul>
{{range .}}
<li>
<h2>User: {{.User}}</h2>
<img src="data::image/png;base64, {{.QRCode}}" style="margin-left:-18px"/><br/>
<pre>{{.Encoded}}</pre>
</li>
{{end}}
</ul>
</body>
</html>
`
bodyTemlate, err := template.New("html").Parse(htmlTemplate)
if err != nil {
log.Errorf("Error building URL template: %w", err)
return
}
var buf bytes.Buffer
err = bodyTemlate.Execute(&buf, users)
if err != nil {
log.Errorf("Error executing URL template: %w", err)
return
}
http.HandleFunc(
"/", useLogger(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(http.StatusOK)
_, _ = w.Write(buf.Bytes())
}),
)
}
func metadataToString(config ServerConfig) (string, error) {
thumbnailMetadata, err := thumbnailToMetadata(config.Thumbnail)
if thumbnailMetadata != nil {
config.Metadata = append(config.Metadata, thumbnailMetadata)
}
marshalledMetadata, err := json.Marshal(config.Metadata)
return string(marshalledMetadata), err
}
func thumbnailToMetadata(thumbnailPath string) ([]string, error) {
bytes, err := os.ReadFile(thumbnailPath)
if err != nil {
return nil, err
}
encoding := http.DetectContentType(bytes)
switch encoding {
case "image/jpeg":
encoding = "image/jpeg;base64"
case "image/png":
encoding = "image/png;base64"
default:
return nil, fmt.Errorf("Unsupported encodeing %s of "+
"thumbnail %s.\n", encoding, thumbnailPath)
}
encodedThumbnail := base64.StdEncoding.EncodeToString(bytes)
return []string{encoding, encodedThumbnail}, nil
}
func badRequestError(w http.ResponseWriter, reason string,
args ...interface{}) {
w.WriteHeader(http.StatusBadRequest)
_ = json.NewEncoder(w).Encode(Error{
Status: "Error",
Reason: fmt.Sprintf(reason, args...),
})
}
// maxMsgRecvSize is the largest message our client will receive. We
// set this to 200MiB atm.
var (
maxMsgRecvSize = grpc.MaxCallRecvMsgSize(1 * 1024 * 1024 * 200)
macaroonTimeout int64 = 60
)
func getClientConn(address, tlsCertPath, macaroonPath string) (*grpc.ClientConn,
error) {
// We always need to send a macaroon.
macOption, err := readMacaroon(macaroonPath)
if err != nil {
return nil, err
}
// TODO (hieblmi) Support Tor dialing
opts := []grpc.DialOption{
grpc.WithDefaultCallOptions(maxMsgRecvSize),
macOption,
}
// TLS cannot be disabled, we'll always have a cert file to read.
creds, err := credentials.NewClientTLSFromFile(tlsCertPath, "")
if err != nil {
return nil, err
}
opts = append(opts, grpc.WithTransportCredentials(creds))
conn, err := grpc.Dial(address, opts...)
if err != nil {
return nil, fmt.Errorf("unable to connect to RPC server: %v",
err)
}
return conn, nil
}
// readMacaroon tries to read the macaroon file at the specified path and create
// gRPC dial options from it.
func readMacaroon(macPath string) (grpc.DialOption, error) {
// Load the specified macaroon file.
macBytes, err := os.ReadFile(macPath)
if err != nil {
return nil, fmt.Errorf("unable to read macaroon path : %v", err)
}
mac := &macaroon.Macaroon{}
if err = mac.UnmarshalBinary(macBytes); err != nil {
return nil, fmt.Errorf("unable to decode macaroon: %v", err)
}
// Now we append the macaroon credentials to the dial options.
cred, err := macaroons.NewMacaroonCredential(mac)
if err != nil {
return nil, fmt.Errorf("error creating macaroon credential: %v",
err)
}
return grpc.WithPerRPCCredentials(cred), nil
}