From aeb1795c62f3318ec5b83ca78e85f88086f674e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dni=20=E2=9A=A1?= Date: Tue, 23 Apr 2024 08:50:50 +0200 Subject: [PATCH] fixup! --- lnurl/core.py | 51 ++++++++++++++++++++++++--------------------------- 1 file changed, 24 insertions(+), 27 deletions(-) diff --git a/lnurl/core.py b/lnurl/core.py index 62af6cb..6684292 100644 --- a/lnurl/core.py +++ b/lnurl/core.py @@ -5,8 +5,8 @@ from .exceptions import InvalidLnurl, InvalidUrl, LnurlResponseException from .helpers import encode_strict_der, lnurlauth_key, url_encode -from .models import LnurlResponse, LnurlResponseModel -from .types import ClearnetUrl, DebugUrl, Lnurl, OnionUrl +from .models import LnurlAuthResponse, LnurlResponse, LnurlResponseModel +from .types import ClearnetUrl, DebugUrl, Lnurl, OnionUrl, LnAddress def decode(bech32_lnurl: str) -> Union[OnionUrl, ClearnetUrl, DebugUrl]: @@ -37,29 +37,6 @@ def get(url: str, *, response_class: Optional[Any] = None, verify: Union[str, bo return LnurlResponse.from_dict(req.json()) -def handle( - bech32_lnurl: str, - response_class: Optional[LnurlResponseModel] = None, - verify: Union[str, bool] = True, - login_id: Optional[str] = None, -) -> LnurlResponseModel: - try: - if "@" in bech32_lnurl: - lnaddress = LnAddress(bech32_lnurl) - return get(lnaddress.url, response_class=response_class, verify=verify) - lnurl = Lnurl(bech32_lnurl) - except (ValidationError, ValueError): - raise InvalidLnurl - - if lnurl.is_login: - try: - return auth(lnurl, login_id=login_id, verify=verify) - except (ValidationError, ValueError): - raise InvalidLnurl - - return get(lnurl.url, response_class=response_class, verify=verify) - - def auth( lnurl: Lnurl, response_class: Optional[Any] = None, @@ -68,7 +45,9 @@ def auth( ) -> LnurlResponseModel: k1 = bytes.fromhex(lnurl.url.query_params["k1"]) assert login_id, "Provide a valid login_id to sign the message." - auth_key = lnurlauth_key(lnurl.url.netloc, login_id) + print(lnurl.url.host) + assert lnurl.url.host, "LNURLauth host does not exist" + auth_key = lnurlauth_key(lnurl.url.host, login_id) sig = auth_key.sign_digest_deterministic(k1, sigencode=encode_strict_der) assert auth_key.verifying_key, "LNURLauth verifying_key does not exist" try: @@ -76,7 +55,6 @@ def auth( lnurl.url, verify=verify, params={ - "k1": k1.hex(), "key": auth_key.verifying_key.to_string("compressed").hex(), "sig": sig.hex(), }, @@ -91,3 +69,22 @@ def auth( return LnurlResponse.from_dict(req.json()) # return LnurlErrorResponse(reason=withdraw.reason) + + +def handle( + bech32_lnurl: str, + response_class: Optional[LnurlResponseModel] = None, + verify: Union[str, bool] = True, +) -> LnurlResponseModel: + try: + if "@" in bech32_lnurl: + lnaddress = LnAddress(bech32_lnurl) + return get(lnaddress.url, response_class=response_class, verify=verify) + lnurl = Lnurl(bech32_lnurl) + except (ValidationError, ValueError): + raise InvalidLnurl + + if lnurl.is_login: + return LnurlAuthResponse(callback=lnurl.url, k1=lnurl.url.query_params["k1"]) + + return get(lnurl.url, response_class=response_class, verify=verify)