fix withdraw routes order

This commit is contained in:
Tiago vasconcelos 2021-11-17 21:20:48 +00:00
parent 2485e8eabf
commit 3f8d1a6fb5
2 changed files with 53 additions and 49 deletions

View file

@ -1,8 +1,8 @@
import json
from datetime import datetime
from http import HTTPStatus
import shortuuid # type: ignore
import json
from fastapi import HTTPException
from fastapi.param_functions import Query
from starlette.requests import Request
@ -22,6 +22,7 @@ from .crud import get_withdraw_link_by_hash, update_withdraw_link
name="withdraw.api_lnurl_response",
)
async def api_lnurl_response(request: Request, unique_hash):
print("NOT UNIQUE")
link = await get_withdraw_link_by_hash(unique_hash)
if not link:
@ -49,68 +50,26 @@ async def api_lnurl_response(request: Request, unique_hash):
return json.dumps(withdrawResponse)
# FOR LNURLs WHICH ARE UNIQUE
@withdraw_ext.get(
"/api/v1/lnurl/{unique_hash}/{id_unique_hash}",
response_class=HTMLResponse,
name="withdraw.api_lnurl_multi_response",
)
async def api_lnurl_multi_response(request: Request, unique_hash, id_unique_hash):
link = await get_withdraw_link_by_hash(unique_hash)
if not link:
raise HTTPException(
status_code=HTTPStatus.OK, detail="LNURL-withdraw not found."
)
if link.is_spent:
raise HTTPException(status_code=HTTPStatus.OK, detail="Withdraw is spent.")
useslist = link.usescsv.split(",")
found = False
for x in useslist:
tohash = link.id + link.unique_hash + str(x)
if id_unique_hash == shortuuid.uuid(name=tohash):
found = True
if not found:
raise HTTPException(
status_code=HTTPStatus.OK, detail="LNURL-withdraw not found."
)
url = request.url_for("withdraw.api_lnurl_callback", unique_hash=link.unique_hash)
withdrawResponse = {
"tag": "withdrawRequest",
"callback": url,
"k1": link.k1,
"minWithdrawable": link.min_withdrawable * 1000,
"maxWithdrawable": link.max_withdrawable * 1000,
"defaultDescription": link.title,
}
return json.dumps(withdrawResponse)
# CALLBACK
#https://5650-2001-8a0-fa12-2900-4c13-748a-fbb9-a47f.ngrok.io/withdraw/api/v1/lnurl/cb/eJHybS8hqcBWajZM63H3FP?k1=MUaYBGrUPuAs8SLpfizmCk&pr=lnbc100n1pse2tsypp5ju0yn3w9j0n8rr3squg0knddawu2ude2cgrm6zje5f34e9jzpmlsdq8w3jhxaqxqyjw5qcqpjsp5tyhu78pamqg5zfy96kup329zt40ramc8gs2ev6jxgp66zca2348qrzjqwac3nxyg3f5mfa4ke9577c4u8kvkx8pqtdsusqdfww0aymk823x6znwa5qqzyqqqyqqqqlgqqqqppgq9q9qy9qsq66zp6pctnlmk59xwtqjga5lvqrkyccmafmn43enhhc6ugew80sanxymepshpv44m9yyhfgh8r2upvxhgk00d36rpqzfy3fxemeu4jhqp96l8hx
@withdraw_ext.get(
"/api/v1/lnurl/cb/{unique_hash}",
status_code=HTTPStatus.OK,
name="withdraw.api_lnurl_callback",
)
async def api_lnurl_callback(
unique_hash,
request: Request,
unique_hash: str = Query(...),
k1: str = Query(...),
payment_request: str = Query(..., alias="pr"),
pr: str = Query(...)
):
link = await get_withdraw_link_by_hash(unique_hash)
now = int(datetime.now().timestamp())
if not link:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="LNURL-withdraw not found."
status_code=HTTPStatus.NOT_FOUND, detail="LNURL-withdraw not found"
)
if link.is_spent:
@ -142,6 +101,8 @@ async def api_lnurl_callback(
"usescsv": usescsv,
}
await update_withdraw_link(link.id, **changes)
payment_request=pr
await pay_invoice(
wallet_id=link.wallet,
@ -154,3 +115,46 @@ async def api_lnurl_callback(
except Exception as e:
await update_withdraw_link(link.id, **changesback)
return {"status": "ERROR", "reason": "Link not working"}
# FOR LNURLs WHICH ARE UNIQUE
@withdraw_ext.get(
"/api/v1/lnurl/{unique_hash}/{id_unique_hash}",
response_class=HTMLResponse,
name="withdraw.api_lnurl_multi_response",
)
async def api_lnurl_multi_response(request: Request, unique_hash, id_unique_hash):
print("UNIQUE")
link = await get_withdraw_link_by_hash(unique_hash)
if not link:
raise HTTPException(
status_code=HTTPStatus.OK, detail="LNURL-withdraw not found."
)
if link.is_spent:
raise HTTPException(status_code=HTTPStatus.OK, detail="Withdraw is spent.")
useslist = link.usescsv.split(",")
found = False
for x in useslist:
tohash = link.id + link.unique_hash + str(x)
if id_unique_hash == shortuuid.uuid(name=tohash):
found = True
if not found:
raise HTTPException(
status_code=HTTPStatus.OK, detail="LNURL-withdraw not found."
)
url = request.url_for("withdraw.api_lnurl_callback", unique_hash=link.unique_hash)
withdrawResponse = {
"tag": "withdrawRequest",
"callback": url,
"k1": link.k1,
"minWithdrawable": link.min_withdrawable * 1000,
"maxWithdrawable": link.max_withdrawable * 1000,
"defaultDescription": link.title,
}
return json.dumps(withdrawResponse)

View file

@ -91,7 +91,7 @@ async def print_qr(request: Request, link_id):
return withdraw_renderer().TemplateResponse(
"withdraw/print_qr.html",
{"request": request, "link": link.dict(), unique: False},
{"request": request, "link": link.dict(), "unique": False},
)
links = []
count = 0