Skip to content

Commit

Permalink
restored original fix
Browse files Browse the repository at this point in the history
  • Loading branch information
arcbtc committed Mar 20, 2024
2 parents a386120 + a386120 commit 0455157
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 86 deletions.
6 changes: 6 additions & 0 deletions crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ async def increment_withdraw_link(link: WithdrawLink) -> None:
open_time=link.wait_time + int(datetime.now().timestamp()),
)

async def unincrement_withdraw_link(link: WithdrawLink) -> None:
await update_withdraw_link(
link.id,
used=link.used - 1,
open_time=link.wait_time + int(datetime.now().timestamp()),
)

async def update_withdraw_link(link_id: str, **kwargs) -> Optional[WithdrawLink]:
if "is_unique" in kwargs:
Expand Down
49 changes: 0 additions & 49 deletions helpers.py

This file was deleted.

55 changes: 18 additions & 37 deletions lnurl.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@
from .crud import (
get_withdraw_link_by_hash,
increment_withdraw_link,
unincrement_withdraw_link,
remove_unique_withdraw_link,
)
from .models import WithdrawLink
from .helpers import NamedLock

withdraw_lock = NamedLock()

@withdraw_ext.get(
"/api/v1/lnurl/{unique_hash}",
Expand Down Expand Up @@ -83,31 +82,6 @@ async def api_lnurl_callback(
pr: str = Query(...),
id_unique_hash=None,
):
link = await _check_withdraw_link_safe(unique_hash, k1, id_unique_hash)

try:
payment_hash = await pay_invoice(
wallet_id=link.wallet,
payment_request=pr,
max_sat=link.max_withdrawable,
extra={"tag": "withdraw", "withdrawal_link_id": link.id},
)
if link.webhook_url:
await dispatch_webhook(link, payment_hash, pr)
return {"status": "OK"}
except Exception as e:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST, detail=f"withdraw not working. {str(e)}"
)

async def _check_withdraw_link_safe(unique_hash, k1, id_unique_hash) -> WithdrawLink:
try:
withdraw_lock.acquire(unique_hash)
return await _check_withdraw_link(unique_hash, k1, id_unique_hash)
finally:
withdraw_lock.release(unique_hash)

async def _check_withdraw_link(unique_hash, k1, id_unique_hash) -> WithdrawLink:
link = await get_withdraw_link_by_hash(unique_hash)
now = int(datetime.now().timestamp())
if not link:
Expand All @@ -119,11 +93,9 @@ async def _check_withdraw_link(unique_hash, k1, id_unique_hash) -> WithdrawLink:
raise HTTPException(
status_code=HTTPStatus.METHOD_NOT_ALLOWED, detail="withdraw is spent."
)

await increment_withdraw_link(link)
if link.k1 != k1:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST, detail="k1 is wrong."
)
raise HTTPException(status_code=HTTPStatus.BAD_REQUEST, detail="k1 is wrong.")

if now < link.open_time:
raise HTTPException(
Expand All @@ -138,13 +110,22 @@ async def _check_withdraw_link(unique_hash, k1, id_unique_hash) -> WithdrawLink:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="withdraw not found."
)
await increment_withdraw_link(link)

return link




try:
payment_hash = await pay_invoice(
wallet_id=link.wallet,
payment_request=pr,
max_sat=link.max_withdrawable,
extra={"tag": "withdraw", "withdrawal_link_id": link.id},
)
if link.webhook_url:
await dispatch_webhook(link, payment_hash, pr)
return {"status": "OK"}
except Exception as e:
await unincrement_withdraw_link(link)
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST, detail=f"withdraw not working. {str(e)}"
)


def check_unique_link(link: WithdrawLink, unique_hash: str) -> bool:
Expand Down

0 comments on commit 0455157

Please sign in to comment.