2019-01-07 00:35:26 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"github.com/bumi/lntip/ln"
|
|
|
|
"github.com/labstack/echo"
|
|
|
|
"github.com/labstack/echo/middleware"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
var stdOutLogger = log.New(os.Stdout, "", log.LstdFlags)
|
|
|
|
|
2019-01-07 20:29:08 +00:00
|
|
|
type Invoice struct {
|
|
|
|
Amount int64 `json:"amount"`
|
|
|
|
Memo string `json:"memo"`
|
|
|
|
}
|
|
|
|
|
2019-01-07 00:35:26 +00:00
|
|
|
func main() {
|
|
|
|
address := flag.String("address", "localhost:10009", "The host and port of the ln gRPC server")
|
2019-01-07 22:51:41 +00:00
|
|
|
certFile := flag.String("cert", "~/.lnd/tls.cert", "Path to the lnd tls.cert file")
|
|
|
|
macaroonFile := flag.String("macaroon", "~/.lnd/data/chain/bitcoin/mainnet/invoice.macaroon", "Path to the lnd macaroon file")
|
2019-01-07 20:29:08 +00:00
|
|
|
bind := flag.String("bind", ":1323", "Host and port to bind on")
|
2019-01-07 00:35:26 +00:00
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
e := echo.New()
|
2019-01-07 15:52:38 +00:00
|
|
|
e.Static("/static", "assets")
|
2019-01-07 00:35:26 +00:00
|
|
|
e.Use(middleware.CORS())
|
|
|
|
e.Use(middleware.Recover())
|
|
|
|
|
2019-01-07 20:29:08 +00:00
|
|
|
lndOptions := ln.LNDoptions{
|
2019-01-07 00:35:26 +00:00
|
|
|
Address: *address,
|
|
|
|
CertFile: *certFile,
|
|
|
|
MacaroonFile: *macaroonFile,
|
|
|
|
}
|
|
|
|
lnClient, err := ln.NewLNDclient(lndOptions)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-01-07 20:29:08 +00:00
|
|
|
e.POST("/invoice", func(c echo.Context) error {
|
|
|
|
i := new(Invoice)
|
|
|
|
if err := c.Bind(i); err != nil {
|
|
|
|
return c.JSON(http.StatusBadRequest, "bad request")
|
|
|
|
}
|
|
|
|
|
|
|
|
invoice, err := lnClient.GenerateInvoice(i.Amount, i.Memo)
|
2019-01-07 00:35:26 +00:00
|
|
|
if err != nil {
|
2019-01-07 20:29:08 +00:00
|
|
|
return c.JSON(http.StatusInternalServerError, "invoice creation error")
|
2019-01-07 00:35:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, invoice)
|
|
|
|
})
|
|
|
|
|
|
|
|
e.GET("/settled/:invoiceId", func(c echo.Context) error {
|
|
|
|
invoiceId := c.Param("invoiceId")
|
|
|
|
invoice, _ := lnClient.CheckInvoice(invoiceId)
|
|
|
|
return c.JSON(http.StatusOK, invoice)
|
|
|
|
})
|
|
|
|
|
2019-01-07 20:29:08 +00:00
|
|
|
e.Logger.Fatal(e.Start(*bind))
|
2019-01-07 00:35:26 +00:00
|
|
|
}
|