Allow comments for LNURL-PAY invoices
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing

Allows senders to add a short message to payments, which will be stored
as invoice memo by LND/LndHub.
This commit is contained in:
Basti 2022-03-01 11:13:07 -06:00
parent 6790e8383d
commit 4c51b9c966
Signed by untrusted user: basti
GPG Key ID: 9F88009D31D99C72
2 changed files with 16 additions and 3 deletions

View File

@ -3,6 +3,7 @@ class LnurlpayController < ApplicationController
MIN_SATS = 100
MAX_SATS = 1_000_000
MAX_COMMENT_CHARS = 100
def index
render json: {
@ -12,22 +13,29 @@ class LnurlpayController < ApplicationController
maxSendable: MAX_SATS * 1000, # msat
minSendable: MIN_SATS * 1000, # msat
metadata: metadata(@user.address),
commentAllowed: 0
commentAllowed: MAX_COMMENT_CHARS
}
end
def invoice
amount = params[:amount].to_i / 1000 # msats
address = params[:address]
comment = params[:comment]
if !valid_amount?(amount)
render json: { status: "ERROR", reason: "Invalid amount" }
return
end
if !valid_comment?(comment)
render json: { status: "ERROR", reason: "Comment too long" }
return
end
payment_request = @user.ln_create_invoice({
amount: amount, # we create invoices in sats
description_hash: Digest::SHA2.hexdigest(metadata(address))
description_hash: Digest::SHA2.hexdigest(metadata(address)),
memo: comment
})
render json: {
@ -57,4 +65,8 @@ class LnurlpayController < ApplicationController
amount_in_sats <= MAX_SATS && amount_in_sats >= MIN_SATS
end
def valid_comment?(comment)
comment.length <= MAX_COMMENT_CHARS
end
end

View File

@ -49,7 +49,8 @@ class Lndhub
def addinvoice(payload)
invoice = post "addinvoice", {
amt: payload[:amount],
description_hash: payload[:description_hash]
description_hash: payload[:description_hash],
memo: payload[:memo]
}
invoice["payment_request"]