fixing withdraw

This commit is contained in:
Ben Arc 2021-08-20 22:21:15 +01:00
parent c4b37c6508
commit 8deea85999
2 changed files with 28 additions and 41 deletions

View File

@ -12,41 +12,39 @@ from .crud import get_withdraw_link_by_hash, update_withdraw_link
# FOR LNURLs WHICH ARE NOT UNIQUE # FOR LNURLs WHICH ARE NOT UNIQUE
@withdraw_ext.route("/api/v1/lnurl/<unique_hash>", methods=["GET"]) @withdraw_ext.get("/api/v1/lnurl/<unique_hash>")
async def api_lnurl_response(unique_hash): async def api_lnurl_response(unique_hash):
link = await get_withdraw_link_by_hash(unique_hash) link = await get_withdraw_link_by_hash(unique_hash)
if not link: if not link:
return ( return ({"status": "ERROR", "reason": "LNURL-withdraw not found."},
jsonify({"status": "ERROR", "reason": "LNURL-withdraw not found."}),
HTTPStatus.OK, HTTPStatus.OK,
) )
if link.is_spent: if link.is_spent:
return ( return ({"status": "ERROR", "reason": "Withdraw is spent."},
jsonify({"status": "ERROR", "reason": "Withdraw is spent."}),
HTTPStatus.OK, HTTPStatus.OK,
) )
return jsonify(link.lnurl_response.dict()), HTTPStatus.OK return link.lnurl_response.dict(), HTTPStatus.OK
# FOR LNURLs WHICH ARE UNIQUE # FOR LNURLs WHICH ARE UNIQUE
@withdraw_ext.route("/api/v1/lnurl/<unique_hash>/<id_unique_hash>", methods=["GET"]) @withdraw_ext.route("/api/v1/lnurl/<unique_hash>/<id_unique_hash>")
async def api_lnurl_multi_response(unique_hash, id_unique_hash): async def api_lnurl_multi_response(unique_hash, id_unique_hash):
link = await get_withdraw_link_by_hash(unique_hash) link = await get_withdraw_link_by_hash(unique_hash)
if not link: if not link:
return ( return (
jsonify({"status": "ERROR", "reason": "LNURL-withdraw not found."}), {"status": "ERROR", "reason": "LNURL-withdraw not found."},
HTTPStatus.OK, HTTPStatus.OK,
) )
if link.is_spent: if link.is_spent:
return ( return (
jsonify({"status": "ERROR", "reason": "Withdraw is spent."}), {"status": "ERROR", "reason": "Withdraw is spent."},
HTTPStatus.OK, HTTPStatus.OK,
) )
@ -58,17 +56,17 @@ async def api_lnurl_multi_response(unique_hash, id_unique_hash):
found = True found = True
if not found: if not found:
return ( return (
jsonify({"status": "ERROR", "reason": "LNURL-withdraw not found."}), {"status": "ERROR", "reason": "LNURL-withdraw not found."},
HTTPStatus.OK, HTTPStatus.OK,
) )
return jsonify(link.lnurl_response.dict()), HTTPStatus.OK return link.lnurl_response.dict(), HTTPStatus.OK
# CALLBACK # CALLBACK
@withdraw_ext.route("/api/v1/lnurl/cb/<unique_hash>", methods=["GET"]) @withdraw_ext.get("/api/v1/lnurl/cb/<unique_hash>")
async def api_lnurl_callback(unique_hash): async def api_lnurl_callback(unique_hash):
link = await get_withdraw_link_by_hash(unique_hash) link = await get_withdraw_link_by_hash(unique_hash)
k1 = request.args.get("k1", type=str) k1 = request.args.get("k1", type=str)
@ -77,24 +75,22 @@ async def api_lnurl_callback(unique_hash):
if not link: if not link:
return ( return (
jsonify({"status": "ERROR", "reason": "LNURL-withdraw not found."}), {"status": "ERROR", "reason": "LNURL-withdraw not found."},
HTTPStatus.OK, HTTPStatus.OK,
) )
if link.is_spent: if link.is_spent:
return ( return (
jsonify({"status": "ERROR", "reason": "Withdraw is spent."}), {"status": "ERROR", "reason": "Withdraw is spent."},
HTTPStatus.OK, HTTPStatus.OK,
) )
if link.k1 != k1: if link.k1 != k1:
return jsonify({"status": "ERROR", "reason": "Bad request."}), HTTPStatus.OK return {"status": "ERROR", "reason": "Bad request."}, HTTPStatus.OK
if now < link.open_time: if now < link.open_time:
return ( return (
jsonify( {"status": "ERROR", "reason": f"Wait {link.open_time - now} seconds."},
{"status": "ERROR", "reason": f"Wait {link.open_time - now} seconds."}
),
HTTPStatus.OK, HTTPStatus.OK,
) )
@ -128,12 +124,12 @@ async def api_lnurl_callback(unique_hash):
) )
except ValueError as e: except ValueError as e:
await update_withdraw_link(link.id, **changesback) await update_withdraw_link(link.id, **changesback)
return jsonify({"status": "ERROR", "reason": str(e)}) return {"status": "ERROR", "reason": str(e)}
except PermissionError: except PermissionError:
await update_withdraw_link(link.id, **changesback) await update_withdraw_link(link.id, **changesback)
return jsonify({"status": "ERROR", "reason": "Withdraw link is empty."}) return {"status": "ERROR", "reason": "Withdraw link is empty."}
except Exception as e: except Exception as e:
await update_withdraw_link(link.id, **changesback) await update_withdraw_link(link.id, **changesback)
return jsonify({"status": "ERROR", "reason": str(e)}) return {"status": "ERROR", "reason": str(e)}
return jsonify({"status": "OK"}), HTTPStatus.OK return {"status": "OK"}, HTTPStatus.OK

View File

@ -29,24 +29,20 @@ async def api_links():
wallet_ids = (await get_user(g.wallet.user)).wallet_ids wallet_ids = (await get_user(g.wallet.user)).wallet_ids
try: try:
return ( return (
jsonable_encoder(
[ [
{ {
**link._asdict(), **link._asdict(),
**{"lnurl": link.lnurl}, **{"lnurl": link.lnurl},
} }
for link in await get_withdraw_links(wallet_ids) for link in await get_withdraw_links(wallet_ids)
] ],
),
HTTPStatus.OK, HTTPStatus.OK,
) )
except LnurlInvalidUrl: except LnurlInvalidUrl:
return ( return (
jsonable_encoder(
{ {
"message": "LNURLs need to be delivered over a publically accessible `https` domain or Tor." "message": "LNURLs need to be delivered over a publically accessible `https` domain or Tor."
} },
),
HTTPStatus.UPGRADE_REQUIRED, HTTPStatus.UPGRADE_REQUIRED,
) )
@ -57,15 +53,14 @@ async def api_link_retrieve(link_id):
link = await get_withdraw_link(link_id, 0) link = await get_withdraw_link(link_id, 0)
if not link: if not link:
return ( return ({"message": "Withdraw link does not exist."},
jsonable_encoder({"message": "Withdraw link does not exist."}),
HTTPStatus.NOT_FOUND, HTTPStatus.NOT_FOUND,
) )
if link.wallet != g.wallet.id: if link.wallet != g.wallet.id:
return jsonable_encoder({"message": "Not your withdraw link."}), HTTPStatus.FORBIDDEN return {"message": "Not your withdraw link."}, HTTPStatus.FORBIDDEN
return jsonable_encoder({**link, **{"lnurl": link.lnurl}}), HTTPStatus.OK return {**link, **{"lnurl": link.lnurl}}, HTTPStatus.OK
class CreateData(BaseModel): class CreateData(BaseModel):
title: str = Query(...) title: str = Query(...)
@ -81,11 +76,9 @@ class CreateData(BaseModel):
async def api_link_create_or_update(data: CreateData, link_id: str = None): async def api_link_create_or_update(data: CreateData, link_id: str = None):
if data.max_withdrawable < data.min_withdrawable: if data.max_withdrawable < data.min_withdrawable:
return ( return (
jsonable_encoder(
{ {
"message": "`max_withdrawable` needs to be at least `min_withdrawable`." "message": "`max_withdrawable` needs to be at least `min_withdrawable`."
} },
),
HTTPStatus.BAD_REQUEST, HTTPStatus.BAD_REQUEST,
) )
@ -112,8 +105,7 @@ async def api_link_create_or_update(data: CreateData, link_id: str = None):
wallet_id=g.wallet.id, **data, usescsv=usescsv wallet_id=g.wallet.id, **data, usescsv=usescsv
) )
return ( return ({**link, **{"lnurl": link.lnurl}},
jsonable_encoder({**link, **{"lnurl": link.lnurl}}),
HTTPStatus.OK if link_id else HTTPStatus.CREATED, HTTPStatus.OK if link_id else HTTPStatus.CREATED,
) )
@ -124,13 +116,12 @@ async def api_link_delete(link_id):
link = await get_withdraw_link(link_id) link = await get_withdraw_link(link_id)
if not link: if not link:
return ( return ({"message": "Withdraw link does not exist."},
jsonable_encoder({"message": "Withdraw link does not exist."}),
HTTPStatus.NOT_FOUND, HTTPStatus.NOT_FOUND,
) )
if link.wallet != g.wallet.id: if link.wallet != g.wallet.id:
return jsonable_encoder({"message": "Not your withdraw link."}), HTTPStatus.FORBIDDEN return {"message": "Not your withdraw link."}, HTTPStatus.FORBIDDEN
await delete_withdraw_link(link_id) await delete_withdraw_link(link_id)
@ -141,4 +132,4 @@ async def api_link_delete(link_id):
@api_check_wallet_key("invoice") @api_check_wallet_key("invoice")
async def api_hash_retrieve(the_hash, lnurl_id): async def api_hash_retrieve(the_hash, lnurl_id):
hashCheck = await get_hash_check(the_hash, lnurl_id) hashCheck = await get_hash_check(the_hash, lnurl_id)
return jsonable_encoder(hashCheck), HTTPStatus.OK return hashCheck, HTTPStatus.OK