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

Allow configuration of LNURL-pay min/max sendable #44

Open
wants to merge 1 commit 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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ 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-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,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensures that the same value is returned to the client as was provided. Easily reproduced with the current version e.g. via Phoenix when you want to donate 1 EUR since the donate value is not divisible by 1000.

}
res, err := c.lndClient.AddInvoice(c.ctx, &invoice)
if err != nil {
Expand Down
19 changes: 13 additions & 6 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,17 @@ func main() {
}
name := c.Param("name")
lightningAddress := name + "@" + host
lnurlpMinSendable := msats(cfg.Int64("lnurlp-min-sendable"))
lnurlpMaxSendable := msats(cfg.Int64("lnurlp-max-sendable"))
lnurlMetadata := "[[\"text/identifier\", \"" + lightningAddress + "\"], [\"text/plain\", \"Sats for " + lightningAddress + "\"]]"
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 +181,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 +236,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 +249,8 @@ 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.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