diff --git a/README.md b/README.md index 3be0856..c7316e3 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/ln/lnd.go b/ln/lnd.go index 8b9549a..0f8cd10 100644 --- a/ln/lnd.go +++ b/ln/lnd.go @@ -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 { diff --git a/lnme.go b/lnme.go index 5c81fda..b9628cd 100644 --- a/lnme.go +++ b/lnme.go @@ -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") @@ -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", @@ -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"}) @@ -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(".") @@ -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") diff --git a/lnurl/types.go b/lnurl/types.go index ba60c09..db4f39e 100644 --- a/lnurl/types.go +++ b/lnurl/types.go @@ -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"` @@ -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) +}