Skip to content

Commit

Permalink
fixup!
Browse files Browse the repository at this point in the history
  • Loading branch information
dni committed Apr 23, 2024
1 parent 3494abc commit aeb1795
Showing 1 changed file with 24 additions and 27 deletions.
51 changes: 24 additions & 27 deletions lnurl/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down Expand Up @@ -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,
Expand All @@ -68,15 +45,16 @@ 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:
req = requests.get(
lnurl.url,
verify=verify,
params={
"k1": k1.hex(),
"key": auth_key.verifying_key.to_string("compressed").hex(),
"sig": sig.hex(),
},
Expand All @@ -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)

0 comments on commit aeb1795

Please sign in to comment.