feat: pass data through lnurl for webhook (#31)

* feat: pass through data with lnurl
This commit is contained in:
dni ⚡ 2023-11-22 11:53:44 +01:00 committed by GitHub
commit 84179e8eea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 16 deletions

View file

@ -22,9 +22,11 @@ from .crud import (
name="lnurlp.api_lnurl_lnaddr_callback",
)
async def api_lnurl_lnaddr_callback(
request: Request, link_id, amount: int = Query(...)
request: Request, link_id, amount: int = Query(...), webhook_data: str = Query(None)
):
return await api_lnurl_callback(request, link_id, amount, lnaddress=True)
return await api_lnurl_callback(
request, link_id, amount, webhook_data, lnaddress=True
)
@lnurlp_ext.get(
@ -33,7 +35,11 @@ async def api_lnurl_lnaddr_callback(
name="lnurlp.api_lnurl_callback",
)
async def api_lnurl_callback(
request: Request, link_id, amount: int = Query(...), lnaddress=False
request: Request,
link_id,
amount: int = Query(...),
webhook_data: str = Query(None),
lnaddress=False,
):
link = await increment_pay_link(link_id, served_pr=1)
if not link:
@ -64,11 +70,15 @@ async def api_lnurl_callback(
comment = request.query_params.get("comment")
if len(comment or "") > link.comment_chars:
return LnurlErrorResponse(
reason=f"Got a comment with {len(comment)} characters, but can only accept {link.comment_chars}"
reason=(
f"Got a comment with {len(comment or '')} characters, "
f"but can only accept {link.comment_chars}"
)
).dict()
if lnaddress:
# for lnaddress, we have to set this otherwise the metadata won't have the identifier
# for lnaddress, we have to set this otherwise
# the metadata won't have the identifier
link.domain = urlparse(str(request.url)).netloc
extra = {
@ -80,6 +90,9 @@ async def api_lnurl_callback(
if comment:
extra["comment"] = (comment,)
if webhook_data:
extra["webhook_data"] = webhook_data
# nip 57
nostr = request.query_params.get("nostr")
if nostr:
@ -88,13 +101,14 @@ async def api_lnurl_callback(
if lnaddress and link.username and link.domain:
extra["lnaddress"] = f"{link.username}@{link.domain}"
# we take the zap request as the description instead of the metadata if present
unhashed_description = nostr.encode() if nostr else link.lnurlpay_metadata.encode()
payment_hash, payment_request = await create_invoice(
wallet_id=link.wallet,
amount=int(amount / 1000),
memo=link.description,
unhashed_description=nostr.encode()
if nostr # we take the zap request as the description instead of the LNURL metadata if present
else link.lnurlpay_metadata.encode(),
unhashed_description=unhashed_description,
extra=extra,
)
@ -110,7 +124,7 @@ async def api_lnurl_callback(
@lnurlp_ext.get(
"/api/v1/lnurl/{link_id}", # Backwards compatibility for old LNURLs / QR codes (with long URL)
"/api/v1/lnurl/{link_id}", # Backwards compatibility for old LNURLs / QR codes
status_code=HTTPStatus.OK,
name="lnurlp.api_lnurl_response.deprecated",
)
@ -119,7 +133,9 @@ async def api_lnurl_callback(
status_code=HTTPStatus.OK,
name="lnurlp.api_lnurl_response",
)
async def api_lnurl_response(request: Request, link_id, lnaddress=False):
async def api_lnurl_response(
request: Request, link_id, webhook_data: str = Query(None), lnaddress=False
):
link = await increment_pay_link(link_id, served_meta=1)
if not link:
raise HTTPException(
@ -129,13 +145,25 @@ async def api_lnurl_response(request: Request, link_id, lnaddress=False):
rate = await get_fiat_rate_satoshis(link.currency) if link.currency else 1
if lnaddress:
# for lnaddress, we have to set this otherwise the metadata won't have the identifier
# for lnaddress, we have to set this otherwise
# the metadata won't have the identifier
link.domain = urlparse(str(request.url)).netloc
callback = str(
request.url_for("lnurlp.api_lnurl_lnaddr_callback", link_id=link.id)
request.url_for(
"lnurlp.api_lnurl_lnaddr_callback",
link_id=link.id,
)
)
else:
callback = str(request.url_for("lnurlp.api_lnurl_callback", link_id=link.id))
callback = str(
request.url_for(
"lnurlp.api_lnurl_callback",
link_id=link.id,
)
)
if webhook_data:
callback += f"?webhook_data={webhook_data}"
resp = LnurlPayResponse(
callback=callback,

View file

@ -40,6 +40,7 @@ async def on_invoice_paid(payment: Payment):
logger.error("Invoice paid. But no pay link id found.")
return
pay_link = await get_pay_link(pay_link_id)
if not pay_link:
logger.error(
@ -66,6 +67,7 @@ async def send_webhook(payment: Payment, pay_link: PayLink):
"payment_request": payment.bolt11,
"amount": payment.amount,
"comment": payment.extra.get("comment"),
"webhook_data": payment.extra.get("webhook_data") or "",
"lnurlp": pay_link.id,
"body": json.loads(pay_link.webhook_body)
if pay_link.webhook_body

View file

@ -175,9 +175,6 @@
label="Lightning Address"
@input="formDialog.data.username = formDialog.data.username.toLowerCase()"
/>
<span class="label">
&nbsp; @ {% raw %} {{ domain }} {% endraw %}
</span>
</div>
<div class="col" style="margin-top: 10px">
<span class="label">