From 1d91b50a679ff8411413abd84bdaa0a435bac0aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dni=20=E2=9A=A1?= Date: Mon, 18 Aug 2025 10:49:25 +0200 Subject: [PATCH] refactor: dont use model property for lnurlpaymetadata (#97) * refactor: dont use model property for lnurlpaymetadata domain is really only needed if you do it with via the property. cleans up code and makes it more readable --- crud.py | 1 - models.py | 17 +++-------------- views_lnurl.py | 34 ++++++++++++++++++++-------------- 3 files changed, 23 insertions(+), 29 deletions(-) diff --git a/crud.py b/crud.py index c1b5335..76678ad 100644 --- a/crud.py +++ b/crud.py @@ -55,7 +55,6 @@ async def create_pay_link(data: CreatePayLinkData) -> PayLink: served_pr=0, username=data.username, zaps=data.zaps, - domain=None, webhook_url=data.webhook_url, webhook_headers=data.webhook_headers, webhook_body=data.webhook_body, diff --git a/models.py b/models.py index 2c9f412..40d952c 100644 --- a/models.py +++ b/models.py @@ -1,10 +1,9 @@ -import json from datetime import datetime, timezone +from typing import Optional from fastapi import Query, Request from lnbits.helpers import normalize_path from lnurl import encode as lnurl_encode -from lnurl.types import LnurlPayMetadata from pydantic import BaseModel, Field from .helpers import parse_nostr_private_key @@ -54,7 +53,6 @@ class PayLink(BaseModel): updated_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc)) username: str | None = None zaps: bool | None = None - domain: str | None = None webhook_url: str | None = None webhook_headers: str | None = None webhook_body: str | None = None @@ -64,6 +62,8 @@ class PayLink(BaseModel): fiat_base_multiplier: int | None = None disposable: bool + # TODO deprecated, unused in the code, should be deleted from db. + domain: Optional[str] = None def lnurl(self, req: Request) -> str: url = req.url_for("lnurlp.api_lnurl_response", link_id=self.id) @@ -73,14 +73,3 @@ class PayLink(BaseModel): # change url string scheme to http url_str = url_str.replace("https://", "http://") return lnurl_encode(url_str) - - @property - def lnurlpay_metadata(self) -> LnurlPayMetadata: - if self.domain and self.username: - text = f"Payment to {self.username}" - identifier = f"{self.username}@{self.domain}" - metadata = [["text/plain", text], ["text/identifier", identifier]] - else: - metadata = [["text/plain", self.description]] - - return LnurlPayMetadata(json.dumps(metadata)) diff --git a/views_lnurl.py b/views_lnurl.py index fd9cec7..82f470f 100644 --- a/views_lnurl.py +++ b/views_lnurl.py @@ -1,3 +1,4 @@ +import json from http import HTTPStatus from typing import Optional @@ -9,6 +10,7 @@ from lnurl import ( LightningInvoice, LnurlErrorResponse, LnurlPayActionResponse, + LnurlPayMetadata, LnurlPayResponse, LnurlPaySuccessActionTag, Max144Str, @@ -78,10 +80,6 @@ async def api_lnurl_callback( ) ) - # for lnaddress, we have to set this otherwise - # the metadata won't have the identifier - link.domain = request.url.netloc - extra = { "tag": "lnurlp", "link": link.id, @@ -99,11 +97,18 @@ async def api_lnurl_callback( if nostr: extra["nostr"] = nostr # put it here for later publishing in tasks.py - if link.username and link.domain: - extra["lnaddress"] = f"{link.username}@{link.domain}" + if link.username: + identifier = f"{link.username}@{request.url.netloc}" + text = f"Payment to {link.username}" + _metadata = [["text/plain", text], ["text/identifier", identifier]] + extra["lnaddress"] = identifier + else: + _metadata = [["text/plain", link.description]] + + metadata = LnurlPayMetadata(json.dumps(_metadata)) # 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() + unhashed_description = nostr.encode() if nostr else metadata.encode() payment = await create_invoice( wallet_id=link.wallet, @@ -159,19 +164,20 @@ async def api_lnurl_response( if webhook_data: url = url.include_query_params(webhook_data=webhook_data) - link.domain = request.url.netloc callback_url = parse_obj_as(CallbackUrl, str(url)) + if link.username: + identifier = f"{link.username}@{request.url.netloc}" + text = f"Payment to {link.username}" + metadata = [["text/plain", text], ["text/identifier", identifier]] + else: + metadata = [["text/plain", link.description]] + res = LnurlPayResponse( callback=callback_url, minSendable=MilliSatoshi(round(link.min * rate) * 1000), maxSendable=MilliSatoshi(round(link.max * rate) * 1000), - metadata=link.lnurlpay_metadata, - # todo library bug should not be in issue to onot specify - payerData=None, - commentAllowed=None, - allowsNostr=None, - nostrPubkey=None, + metadata=LnurlPayMetadata(json.dumps(metadata)), ) if link.comment_chars > 0: