Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configuration property for PNG/JPG thumbnail used in LNURL-pay metadata #45

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ Instead of the path to the macaroon and cert files you can also provide the hex
#### Other configuration

- `static-path`: Path to a folder that you want to serve with LnMe (e.g. /home/bitcoin/lnme/website). Use this if you want to customize your ⚡website. default: disabled
- `lnurlp-min-sendable`: Min sendable amount in sats via LNURL-pay. (default: 1)
- `lnurlp-max-sendable`: Max sendable amount in sats via LNURL-pay. (default: 1000000)
- `lnurlp-thumbnail-dir`: Path to a PNG thumbnail directory for LNURL-pay metadata.
- `lnurlp-comment-allowed`: Allowed length of LNURL-pay comments, maximum around [~2000 characters](https://stackoverflow.com/a/417184). (default: 210)
- `disable-website`: Disable the default LnMe website. Disable the website if you only want to embed the LnMe widget on your existing website.
- `disable-cors`: Disable CORS headers. (default: false)
Expand Down
6 changes: 3 additions & 3 deletions ln/lnd.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ type LNDclient struct {
}

// AddInvoice generates an invoice with the given price and memo.
func (c LNDclient) AddInvoice(value int64, memo string, descriptionHash []byte) (Invoice, error) {
func (c LNDclient) AddInvoice(msats int64, memo string, descriptionHash []byte) (Invoice, error) {
result := Invoice{}

stdOutLogger.Printf("Adding invoice: memo=%s value=%v", memo, value)
stdOutLogger.Printf("Adding invoice: memo=%s msats=%v", memo, msats)
invoice := lnrpc.Invoice{
Memo: memo,
DescriptionHash: descriptionHash,
Value: value,
ValueMsat: msats,
}
res, err := c.lndClient.AddInvoice(c.ctx, &invoice)
if err != nil {
Expand Down
31 changes: 24 additions & 7 deletions lnme.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func main() {
return c.JSON(http.StatusBadRequest, "Bad request")
}

invoice, err := lnClient.AddInvoice(i.Value, i.Memo, nil)
invoice, err := lnClient.AddInvoice(msats(i.Value), i.Memo, nil)
if err != nil {
stdOutLogger.Printf("Error creating invoice: %s", err)
return c.JSON(http.StatusInternalServerError, "Error adding invoice")
Expand Down Expand Up @@ -162,15 +162,26 @@ func main() {
}
name := c.Param("name")
lightningAddress := name + "@" + host
lnurlMetadata := "[[\"text/identifier\", \"" + lightningAddress + "\"], [\"text/plain\", \"Sats for " + lightningAddress + "\"]]"
lnurlpMinSendable := msats(cfg.Int64("lnurlp-min-sendable"))
lnurlpMaxSendable := msats(cfg.Int64("lnurlp-max-sendable"))
lnurlpThumbnailPath := cfg.String("lnurlp-thumbnail-dir") + "/" + name + ".png"
lnurlpThumbnailData, err := os.ReadFile(lnurlpThumbnailPath)
if lnurlpThumbnailPath != "" && err != nil {
stdOutLogger.Println("Error reading thumbnail:", err)
}
lnurlMetadata := lnurl.Metadata{}.
Identifier(lightningAddress).
Description("Sats for " + lightningAddress).
Thumbnail(lnurlpThumbnailData).
String()
lnurlpCommentAllowed := cfg.Int64("lnurlp-comment-allowed")

if amount := c.QueryParam("amount"); amount == "" {
lnurlPayResponse1 := lnurl.LNURLPayResponse1{
LNURLResponse: lnurl.LNURLResponse{Status: "OK"},
Callback: fmt.Sprintf("%s://%s%s", proto, host, c.Request().URL.Path),
MinSendable: 1000,
MaxSendable: 100000000,
MinSendable: lnurlpMinSendable,
MaxSendable: lnurlpMaxSendable,
EncodedMetadata: lnurlMetadata,
CommentAllowed: lnurlpCommentAllowed,
Tag: "payRequest",
Expand All @@ -179,18 +190,17 @@ func main() {
} else {
stdOutLogger.Printf("New LightningAddress request amount: %s", amount)
msats, err := strconv.ParseInt(amount, 10, 64)
if err != nil || msats < 1000 {
if err != nil || msats < lnurlpMinSendable || msats > lnurlpMaxSendable {
stdOutLogger.Printf("Invalid amount: %s", amount)
return c.JSON(http.StatusOK, lnurl.LNURLErrorResponse{Status: "ERROR", Reason: "Invalid Amount"})
}
sats := msats / 1000 // we need sats
comment := c.QueryParam("comment")
if commentLength := int64(len(comment)); commentLength > lnurlpCommentAllowed {
stdOutLogger.Printf("Invalid comment length: %d", commentLength)
return c.JSON(http.StatusOK, lnurl.LNURLErrorResponse{Status: "ERROR", Reason: "Invalid comment length"})
}
metadataHash := sha256.Sum256([]byte(lnurlMetadata))
invoice, err := lnClient.AddInvoice(sats, comment, metadataHash[:])
invoice, err := lnClient.AddInvoice(msats, comment, metadataHash[:])
if err != nil {
stdOutLogger.Printf("Error creating invoice: %s", err)
return c.JSON(http.StatusOK, lnurl.LNURLErrorResponse{Status: "ERROR", Reason: "Server Error"})
Expand Down Expand Up @@ -235,6 +245,10 @@ func main() {
e.Logger.Fatal(e.Start(listen))
}

func msats(sats int64) int64 {
return sats * 1000
}

func LoadConfig() *koanf.Koanf {
k := koanf.New(".")

Expand All @@ -244,6 +258,9 @@ func LoadConfig() *koanf.Koanf {
f.String("lnd-macaroon", "", "HEX string of LND macaroon file.")
f.String("lnd-cert-path", "~/.lnd/tls.cert", "Path to the LND tls.cert file.")
f.String("lnd-cert", "", "HEX string of LND tls cert file.")
f.Int64("lnurlp-min-sendable", 1, "Min sendable amount in sats via LNURL-pay.")
f.Int64("lnurlp-max-sendable", 1000000, "Max sendable amount in sats via LNURL-pay.")
f.String("lnurlp-thumbnail-dir", "", "Path to a PNG thumbnail directory for LNURL-pay metadata.")
f.Int64("lnurlp-comment-allowed", 210, "Allowed length of LNURL-pay comments.")
f.Bool("disable-website", false, "Disable default embedded website.")
f.Bool("disable-ln-address", false, "Disable Lightning Address handling")
Expand Down
32 changes: 31 additions & 1 deletion lnurl/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
// only using the LNURL types here
package lnurl

import "net/url"
import (
"encoding/base64"
"encoding/json"
"net/http"
"net/url"
)

type LNURLResponse struct {
Status string `json:"status,omitempty"`
Expand Down Expand Up @@ -50,3 +55,28 @@ type LNURLErrorResponse struct {
}

type Metadata [][]string

func (metadata Metadata) Identifier(identifier string) Metadata {
return append(metadata, []string{"text/identifier", identifier})
}

func (metadata Metadata) Description(description string) Metadata {
return append(metadata, []string{"text/plain", description})
}

func (metadata Metadata) Thumbnail(imageData []byte) Metadata {
if len(imageData) == 0 {
return metadata
}

mimeType := http.DetectContentType(imageData)
imageDataBase64 := base64.StdEncoding.EncodeToString(imageData)

return append(metadata, []string{mimeType + ";base64", imageDataBase64})
}

func (metadata Metadata) String() string {
bytes, _ := json.Marshal(metadata)

return string(bytes)
}