1
1
mirror of https://github.com/bumi/lntip synced 2025-07-06 17:15:51 +00:00
It should be easy to integrate into any webpage. We do this with a
little JS snippet, so no need for rendering HTML templates etc.
This commit is contained in:
bumi 2019-01-07 16:52:38 +01:00
parent bfa5219b02
commit 08d0eee00c
6 changed files with 26 additions and 83 deletions

View File

@ -13,14 +13,14 @@ LnTip = function (amount, memo, host) {
} }
LnTip.prototype.loadStylesheet = function () { LnTip.prototype.loadStylesheet = function () {
if (this.styleLoaded) { return } if (document.getElementById('lntip-style')) { return; }
var head = document.getElementsByTagName('head')[0]; var head = document.getElementsByTagName('head')[0];
var css = document.createElement('link'); var css = document.createElement('link');
css.id = "lntip-style";
css.rel = "stylesheet"; css.rel = "stylesheet";
css.type = "text/css"; css.type = "text/css";
css.href = `${this.host}/static/lntip.css`; css.href = `${this.host}/static/lntip.css`;
head.appendChild(css); head.appendChild(css);
this.styleLoaded = true;
} }
LnTip.prototype.closePopup = function () { LnTip.prototype.closePopup = function () {
@ -31,8 +31,8 @@ LnTip.prototype.closePopup = function () {
} }
LnTip.prototype.openPopup = function (content) { LnTip.prototype.openPopup = function (content) {
this.closePopup();
this.loadStylesheet(); this.loadStylesheet();
this.closePopup();
this.popup = new jPopup({ this.popup = new jPopup({
content: content, content: content,
shouldSetHash: false shouldSetHash: false
@ -69,14 +69,18 @@ LnTip.prototype.stopWatchingPayment = function () {
} }
LnTip.prototype.payWithWebln = function () { LnTip.prototype.payWithWebln = function () {
console.log(this.invoice)
if (!webln.isEnabled) { if (!webln.isEnabled) {
webln.enable().then((weblnResponse) => { webln.enable().then((weblnResponse) => {
return webln.sendPayment({ paymentRequest: invoice.PaymentRequest }) console.log(this.invoice.PaymentRequest)
return webln.sendPayment({ paymentRequest: this.invoice.PaymentRequest })
}).catch((e) => { }).catch((e) => {
console.log(e);
this.requestPayment(); this.requestPayment();
}) })
} else { } else {
return webln.sendPayment({ paymentRequest: invoice.PaymentRequest }) console.log(this.invoice.PaymentRequest)
return webln.sendPayment({ paymentRequest: this.invoice.PaymentRequest })
} }
} }

12
examples/tipping.html Normal file
View File

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script src="http://localhost:1323/static/lntip.js" lntip-host="http://localhost:1323"></script>
<a href="#" onclick="javascript:new LnTip(1000, 'thanks');return false;">Tip me</a>
</body>
</html>

View File

@ -5,9 +5,6 @@ import (
"github.com/bumi/lntip/ln" "github.com/bumi/lntip/ln"
"github.com/labstack/echo" "github.com/labstack/echo"
"github.com/labstack/echo/middleware" "github.com/labstack/echo/middleware"
"html/template"
"io"
"io/ioutil"
"log" "log"
"net/http" "net/http"
"os" "os"
@ -16,63 +13,17 @@ import (
var stdOutLogger = log.New(os.Stdout, "", log.LstdFlags) var stdOutLogger = log.New(os.Stdout, "", log.LstdFlags)
type TemplateRenderer struct {
templates *template.Template
}
func (t *TemplateRenderer) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
// Add global methods if data is a map
if viewContext, isMap := data.(map[string]interface{}); isMap {
viewContext["reverse"] = c.Echo().Reverse
}
return t.templates.ExecuteTemplate(w, name, data)
}
func main() { func main() {
var indexView = `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript">
function payme(memo, amount) {
fetch('/payme?memo=' + memo + '&amount=' + amount)
.then(function(response) {
console.log(response.json());
});
}
</script>
</head>
<body>
hallo
</body>
</html>`
address := flag.String("address", "localhost:10009", "The host and port of the ln gRPC server") address := flag.String("address", "localhost:10009", "The host and port of the ln gRPC server")
certFile := flag.String("cert", "tls.cert", "Path to the lnd tls.cert file") certFile := flag.String("cert", "tls.cert", "Path to the lnd tls.cert file")
macaroonFile := flag.String("macaroon", "invoice.macaroon", "Path to the lnd macaroon file") macaroonFile := flag.String("macaroon", "invoice.macaroon", "Path to the lnd macaroon file")
viewPath := flag.String("template", "", "Path of a custom HTML template file")
flag.Parse() flag.Parse()
if *viewPath != "" {
content, err := ioutil.ReadFile(*viewPath)
if err != nil {
panic(err)
}
indexView = string(content)
}
e := echo.New() e := echo.New()
e.Static("/static", "views/assets") e.Static("/static", "assets")
e.Use(middleware.CORS()) e.Use(middleware.CORS())
e.Use(middleware.Recover()) e.Use(middleware.Recover())
renderer := &TemplateRenderer{
templates: template.Must(template.New("index").Parse(indexView)),
}
e.Renderer = renderer
lndOptions := ln.LNDoptions{ lndOptions := ln.LNDoptions{
Address: *address, Address: *address,
@ -101,9 +52,5 @@ func main() {
return c.JSON(http.StatusOK, invoice) return c.JSON(http.StatusOK, invoice)
}) })
e.GET("/", func(c echo.Context) error {
return c.Render(http.StatusOK, "index", map[string]interface{}{})
})
e.Logger.Fatal(e.Start(":1323")) e.Logger.Fatal(e.Start(":1323"))
} }

View File

@ -18,6 +18,7 @@ import (
var stdOutLogger = log.New(os.Stdout, "", log.LstdFlags) var stdOutLogger = log.New(os.Stdout, "", log.LstdFlags)
// thanks https://github.com/philippgille/ln-paywall/
// Invoice is a Lightning Network invoice and contains the typical invoice string and the payment hash. // Invoice is a Lightning Network invoice and contains the typical invoice string and the payment hash.
type Invoice struct { type Invoice struct {
ImplDepID string ImplDepID string
@ -40,7 +41,7 @@ func (c LNDclient) GenerateInvoice(amount int64, memo string) (Invoice, error) {
Memo: memo, Memo: memo,
Value: amount, Value: amount,
} }
stdOutLogger.Println("Creating invoice for a new API request") stdOutLogger.Printf("Creating invoice: %s", memo)
res, err := c.lndClient.AddInvoice(c.ctx, &invoice) res, err := c.lndClient.AddInvoice(c.ctx, &invoice)
if err != nil { if err != nil {
return result, err return result, err
@ -62,7 +63,7 @@ func (c LNDclient) CheckInvoice(id string) (bool, error) {
return false, err return false, err
} }
stdOutLogger.Printf("Checking invoice for hash %v\n", id) stdOutLogger.Printf("Lookup invoice with hash %v\n", id)
// Get the invoice for that hash // Get the invoice for that hash
paymentHash := lnrpc.PaymentHash{ paymentHash := lnrpc.PaymentHash{
@ -82,19 +83,9 @@ func (c LNDclient) CheckInvoice(id string) (bool, error) {
return true, nil return true, nil
} }
func (c LNDclient) GetURIs() (bool, error) {
req := &lnrpc.GetInfoRequest{}
info, err := c.lndClient.GetInfo(c.ctx, req)
stdOutLogger.Println(info.Uris)
return true, err
}
// NewLNDclient creates a new LNDclient instance.
func NewLNDclient(lndOptions LNDoptions) (LNDclient, error) { func NewLNDclient(lndOptions LNDoptions) (LNDclient, error) {
result := LNDclient{} result := LNDclient{}
// Set up a connection to the server.
creds, err := credentials.NewClientTLSFromFile(lndOptions.CertFile, "") creds, err := credentials.NewClientTLSFromFile(lndOptions.CertFile, "")
if err != nil { if err != nil {
return result, err return result, err

View File

@ -1,11 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script src="/static/lntip.js" lntip-host="http://localhost:1323"></script>
</body>
</html>