This repository has been archived by the owner on Jul 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
582 lines (483 loc) · 12.9 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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
package main
import (
"context"
"flag"
"fmt"
"os"
"path"
"time"
"github.com/gin-contrib/cors"
"github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin"
"github.com/go-playground/validator/v10"
"github.com/pelletier/go-toml"
log "github.com/sirupsen/logrus"
"gopkg.in/natefinch/lumberjack.v2"
"github.com/lncm/invoicer/bitcoind"
"github.com/lncm/invoicer/common"
"github.com/lncm/invoicer/ln"
)
type (
BitcoinClient interface {
Address(bech32 bool) (string, error)
BlockCount() (int64, error)
ImportAddress(address, label string) error
CheckAddress(address string) (common.AddrsStatus, error)
}
LightningClient interface {
NewAddress(ctx context.Context, bech32 bool) (string, error)
Info(ctx context.Context) (common.Info, error)
NewInvoice(ctx context.Context, amount int64, desc string) (string, string, error)
Status(ctx context.Context, hash string) (common.Status, error)
StatusWait(ctx context.Context, hash string) (common.Status, error)
History(ctx context.Context) (common.Invoices, error)
}
lnStatusFn func(c context.Context, hash string) (common.Status, error)
)
const DefaultInvoicerPort = 8080
var (
version, gitHash string
lnClient LightningClient
btcClient BitcoinClient
conf common.Config
configFilePath = flag.String("config", common.DefaultConfigFile, "Path to a config file in TOML format")
showVersion = flag.Bool("version", false, "Show version and exit")
)
func init() {
flag.Parse()
versionString := "debug"
if version != "" && gitHash != "" {
versionString = fmt.Sprintf("%s (git: %s)", version, gitHash)
}
// if `--version` flag set, just show the version, and exit
if *showVersion {
fmt.Println(versionString)
os.Exit(0)
}
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
})
// Expand configFile file path and load it
configFile, err := toml.LoadFile(common.CleanAndExpandPath(*configFilePath))
if err != nil {
configFile, err = common.DeprecatedConfigLocationCheck(*configFilePath, err)
if err != nil {
panic(fmt.Errorf("unable to load %s:\n\t%w", *configFilePath, err))
}
log.Warningln("WARNING: Default config location (~/.invoicer/) has been changed to ~/.lncm/ !\n" +
"\tPlease rename it, as future versions will no longer check for the config file there.")
}
// Try to understand the config file
err = configFile.Unmarshal(&conf)
if err != nil {
panic(fmt.Errorf("unable to process %s:\n\t%w", *configFilePath, err))
}
// init specified LN client
lnClient, err = ln.Start(conf.Lnd)
if err != nil {
panic(err)
}
// Init BTC client for monitoring on-chain payments
if !conf.OffChainOnly {
btcClient, err = bitcoind.New(conf.Bitcoind)
if err != nil {
panic(err)
}
}
if conf.LogFile == "" {
conf.LogFile = common.DefaultLogFile
}
fields := log.Fields{
"version": versionString,
"client": conf.LnClient,
"users": len(conf.Users),
"conf-file": *configFilePath,
"log-file": conf.LogFile,
}
if conf.LogFile != "none" {
// Write current config to stdout
log.WithFields(fields).Println("invoicer started")
// After all initialization has been done, start logging to log file
log.SetOutput(&lumberjack.Logger{
Filename: common.CleanAndExpandPath(conf.LogFile),
LocalTime: true,
Compress: true,
})
log.SetFormatter(&log.JSONFormatter{
PrettyPrint: false, // Having `false` here makes sure that `jq` always works on `tail -f`.
})
}
// Write current config to log file
log.WithFields(fields).Println("invoicer started")
}
func newPayment(c *gin.Context) {
var data struct {
Amount int64 `json:"amount"`
Description string `json:"desc"`
Only string `json:"only"`
}
err := c.ShouldBindJSON(&data)
if err != nil {
replyStatus(c, common.StatusReply{
Code: 400,
Error: err.Error(),
})
return
}
if data.Only != "" && data.Only != "btc" && data.Only != "ln" {
replyStatus(c, common.StatusReply{
Code: 400,
Error: "only= is an optional parameter that can only take `btc` and `ln` as values",
})
return
}
// Force LN-only, no matter what the request was
if conf.OffChainOnly {
data.Only = "ln"
}
var payment common.NewPayment
if data.Only != "btc" {
if len(data.Description) > common.MaxInvoiceDescLen {
replyStatus(c, common.StatusReply{
Code: 400,
Error: fmt.Errorf("description too long. Max length is %d", common.MaxInvoiceDescLen).Error(),
})
return
}
// Generate new LN invoice
payment.Bolt11, payment.Hash, err = lnClient.NewInvoice(c, data.Amount, data.Description)
if err != nil {
replyStatus(c, common.StatusReply{
Code: 500,
Error: fmt.Errorf("can't create new LN invoice: %w", err).Error(),
})
return
}
// Extract invoice's creation date & expiry
invoice, err := lnClient.Status(c, payment.Hash)
if err != nil {
replyStatus(c, common.StatusReply{
Code: 500,
Error: fmt.Errorf("can't get LN invoice: %w", err).Error(),
})
return
}
payment.CreatedAt = invoice.Ts
payment.Expiry = invoice.Expiry
}
if data.Only != "ln" {
// get BTC address
payment.Address, err = lnClient.NewAddress(c, false)
if err != nil {
replyStatus(c, common.StatusReply{
Code: 500,
Error: fmt.Errorf("can't get Bitcoin address: %w", err).Error(),
})
return
}
label := data.Description
if len(payment.Hash) > 0 {
label = payment.Hash
}
err = btcClient.ImportAddress(payment.Address, label)
if err != nil {
replyStatus(c, common.StatusReply{
Code: 500,
Error: fmt.Errorf("can't import address (%s) to Bitcoin node: %w", payment.Address, err).Error(),
})
return
}
}
log.WithFields(log.Fields{
"in": data,
"out": payment,
}).Println("Payment requested")
c.JSON(200, payment)
}
func checkLnStatus(c context.Context, hash string, statusFn lnStatusFn) *common.StatusReply {
status, err := statusFn(c, hash)
if err != nil {
return &common.StatusReply{
Code: 500,
Error: fmt.Sprintf("unable to fetch invoice: %s", err),
}
}
if status.Settled {
return &common.StatusReply{
Code: 200, Ln: &status,
}
}
if status.IsExpired() {
return &common.StatusReply{
Code: 408,
Error: "expired",
}
}
return &common.StatusReply{Ln: &status}
}
func checkBtcStatus(ctx context.Context, fin time.Time, addr string, lnProvided, flexible bool, desiredAmount int64) *common.StatusReply {
for time.Now().Before(fin) {
time.Sleep(2 * time.Second)
if ctx.Err() != nil {
return &common.StatusReply{
Error: ctx.Err().Error(),
}
}
btcStatuses, err := btcClient.CheckAddress(addr)
if err != nil {
if !lnProvided {
return &common.StatusReply{
Code: 500,
Error: fmt.Sprintf("unable to check status: %s", err),
}
}
// if LN hash available and fetching bitcoin status produced an error, disable checking bitcoin
return nil
}
btcStatus := btcStatuses[0]
receivedAmount := int64(btcStatus.Amount) * 1e8
if btcStatus.Amount == 0 {
continue
}
// no need to return it now; might be useful later
btcStatus.Label = ""
if flexible || desiredAmount == receivedAmount {
return &common.StatusReply{
Code: 200,
Bitcoin: &btcStatus,
}
}
if receivedAmount > desiredAmount {
return &common.StatusReply{
Code: 202,
Bitcoin: &btcStatus,
}
}
if desiredAmount > receivedAmount {
return &common.StatusReply{
Code: 402,
Error: "not enough",
Bitcoin: &btcStatus,
}
}
}
return nil
}
func replyStatus(c *gin.Context, status common.StatusReply) {
if status.Code >= 300 {
c.AbortWithStatusJSON(status.Code, status)
return
}
c.JSON(status.Code, status)
}
func status(c *gin.Context) {
var queryParams struct {
Hash string `form:"hash"`
Addr string `form:"address"`
Flexible bool `form:"flexible"`
}
err := c.BindQuery(&queryParams)
if err != nil {
replyStatus(c, common.StatusReply{
Code: 400,
Error: fmt.Errorf("invalid request: %w", err).Error(),
})
return
}
hash := queryParams.Hash
addr := queryParams.Addr
flexible := queryParams.Flexible
if len(hash) == 0 && len(addr) == 0 {
replyStatus(c, common.StatusReply{
Code: 500,
Error: "At least one of `hash` or `address` needs to be provided",
})
return
}
var desiredAmount int64
fin := time.Now().Add(common.DefaultInvoiceExpiry * time.Second)
// do initial LN invoice check, and adjust expiration if available
if len(hash) > 0 {
status := checkLnStatus(c, hash, lnClient.Status)
if status.Code > 0 {
replyStatus(c, *status)
return
}
fin = time.Unix(status.Ln.Ts, 0).Add(time.Duration(status.Ln.Expiry) * time.Second)
desiredAmount = status.Ln.Value
}
ctx, cancel := context.WithDeadline(c, fin)
defer cancel()
paymentStatus := make(chan *common.StatusReply)
// subscribe to LN invoice status changes
if len(hash) > 0 {
go func() {
paymentStatus <- checkLnStatus(ctx, hash, lnClient.StatusWait)
}()
}
// keep polling for status update every N seconds
if !conf.OffChainOnly && len(addr) > 0 {
go func() {
paymentStatus <- checkBtcStatus(ctx, fin, addr, len(hash) > 0, flexible, desiredAmount)
}()
}
var status *common.StatusReply
// wait until either:
select {
// … payment is received successfully
case status = <-paymentStatus:
// … payment expires
case <-ctx.Done():
status = &common.StatusReply{
Code: 408,
Error: "expired",
}
// … payment is cancelled by user
case <-c.Request.Context().Done():
cancel()
status = &common.StatusReply{
Code: 499,
Error: "cancelled by client",
}
}
log.WithFields(log.Fields{
"in": queryParams,
"status": *status,
}).Println("Payment updated")
replyStatus(c, *status)
}
// TODO: pagination
// TODO: limit
// TODO: bitcoin transactions
func history(c *gin.Context) {
var queryParams struct {
Limit int64 `form:"limit"`
Offset int64 `form:"offset"`
OnlyStatus string `form:"only_status" validate:"omitempty,oneof=paid expired pending"`
}
err := c.BindQuery(&queryParams)
if err != nil {
replyStatus(c, common.StatusReply{
Code: 400,
Error: fmt.Errorf("invalid request: %w", err).Error(),
})
return
}
err = validator.New().Struct(queryParams)
if err != nil {
replyStatus(c, common.StatusReply{
Code: 400,
Error: fmt.Errorf("invalid request: %w", err).Error(),
})
return
}
var warning string
btcHistory := make(map[string]common.AddrStatus)
if !conf.OffChainOnly {
// fetch bitcoin history
btcAllAddresses, err := btcClient.CheckAddress("")
if err != nil {
warning = "Unable to fetch Bitcoin history. Only showing LN."
}
// Convert Bitcoin history from list to easily addressable map
for _, payment := range btcAllAddresses {
if payment.Label != "" {
btcHistory[payment.Label] = payment
}
}
}
// fetch LN history
lnHistory, err := lnClient.History(c)
if err != nil {
replyStatus(c, common.StatusReply{
Code: 500,
Error: fmt.Errorf("can't get history from LN node: %w", err).Error(),
})
return
}
// merge histories
var history []common.Payment
for _, invoice := range lnHistory {
var payment common.Payment
payment.ApplyLn(invoice)
// this check is probably unnecessary, as the map is empty anyway…
if !conf.OffChainOnly {
if btcStatus, ok := btcHistory[payment.Hash]; ok {
payment.ApplyBtc(btcStatus)
}
}
switch queryParams.OnlyStatus {
case "paid":
if !payment.Paid {
continue
}
case "expired":
if !payment.Expired {
continue
}
case "pending":
if payment.Paid || payment.Expired {
continue
}
}
history = append(history, payment)
}
// reverse order before returning (newest on top)
for i, j := 0, len(history)-1; i < j; i, j = i+1, j-1 {
history[i], history[j] = history[j], history[i]
}
c.JSON(200, struct {
History []common.Payment `json:"history"`
Error string `json:"error,omitempty"`
}{
History: history,
Error: warning,
})
}
func info(c *gin.Context) {
info, err := lnClient.Info(c)
if err != nil {
replyStatus(c, common.StatusReply{
Code: 500,
Error: fmt.Errorf("can't get info from LN node: %w", err).Error(),
})
return
}
info.OnChain = true
info.OffChain = true
if conf.OffChainOnly {
info.OnChain = false
}
c.JSON(200, info)
}
func main() {
// gin.SetMode(gin.ReleaseMode)
router := gin.Default()
router.Use(cors.Default())
router.Use(gzip.Gzip(gzip.DefaultCompression))
r := router.Group("/api")
r.POST("/payment", newPayment)
r.GET("/payment", status)
r.GET("/info", info)
// history only available if Basic Auth is enabled
if len(conf.Users) > 0 {
r.GET("/history", gin.BasicAuth(conf.Users), history)
}
var staticFilePath string
if conf.StaticDir != "" {
staticFilePath = path.Join(conf.StaticDir, "index.html")
router.StaticFile("/", staticFilePath)
}
if conf.Port == 0 {
conf.Port = DefaultInvoicerPort
}
log.WithFields(log.Fields{
"routes": common.FormatRoutes(router.Routes()),
"port": conf.Port,
"static-file": staticFilePath,
}).Println("gin router defined")
err := router.Run(fmt.Sprintf(":%d", conf.Port))
if err != nil {
panic(err)
}
}