From 8deea859995dced0f9d7d6b6885c254673c428e7 Mon Sep 17 00:00:00 2001 From: Ben Arc Date: Fri, 20 Aug 2021 22:21:15 +0100 Subject: [PATCH] fixing withdraw --- lnbits/extensions/withdraw/lnurl.py | 40 +++++++++++-------------- lnbits/extensions/withdraw/views_api.py | 29 +++++++----------- 2 files changed, 28 insertions(+), 41 deletions(-) diff --git a/lnbits/extensions/withdraw/lnurl.py b/lnbits/extensions/withdraw/lnurl.py index 1322b6e25..1c3cde6c5 100644 --- a/lnbits/extensions/withdraw/lnurl.py +++ b/lnbits/extensions/withdraw/lnurl.py @@ -12,41 +12,39 @@ from .crud import get_withdraw_link_by_hash, update_withdraw_link # FOR LNURLs WHICH ARE NOT UNIQUE -@withdraw_ext.route("/api/v1/lnurl/", methods=["GET"]) +@withdraw_ext.get("/api/v1/lnurl/") async def api_lnurl_response(unique_hash): link = await get_withdraw_link_by_hash(unique_hash) if not link: - return ( - jsonify({"status": "ERROR", "reason": "LNURL-withdraw not found."}), + return ({"status": "ERROR", "reason": "LNURL-withdraw not found."}, HTTPStatus.OK, ) if link.is_spent: - return ( - jsonify({"status": "ERROR", "reason": "Withdraw is spent."}), + return ({"status": "ERROR", "reason": "Withdraw is spent."}, HTTPStatus.OK, ) - return jsonify(link.lnurl_response.dict()), HTTPStatus.OK + return link.lnurl_response.dict(), HTTPStatus.OK # FOR LNURLs WHICH ARE UNIQUE -@withdraw_ext.route("/api/v1/lnurl//", methods=["GET"]) +@withdraw_ext.route("/api/v1/lnurl//") async def api_lnurl_multi_response(unique_hash, id_unique_hash): link = await get_withdraw_link_by_hash(unique_hash) if not link: return ( - jsonify({"status": "ERROR", "reason": "LNURL-withdraw not found."}), + {"status": "ERROR", "reason": "LNURL-withdraw not found."}, HTTPStatus.OK, ) if link.is_spent: return ( - jsonify({"status": "ERROR", "reason": "Withdraw is spent."}), + {"status": "ERROR", "reason": "Withdraw is spent."}, HTTPStatus.OK, ) @@ -58,17 +56,17 @@ async def api_lnurl_multi_response(unique_hash, id_unique_hash): found = True if not found: return ( - jsonify({"status": "ERROR", "reason": "LNURL-withdraw not found."}), + {"status": "ERROR", "reason": "LNURL-withdraw not found."}, HTTPStatus.OK, ) - return jsonify(link.lnurl_response.dict()), HTTPStatus.OK + return link.lnurl_response.dict(), HTTPStatus.OK # CALLBACK -@withdraw_ext.route("/api/v1/lnurl/cb/", methods=["GET"]) +@withdraw_ext.get("/api/v1/lnurl/cb/") async def api_lnurl_callback(unique_hash): link = await get_withdraw_link_by_hash(unique_hash) k1 = request.args.get("k1", type=str) @@ -77,24 +75,22 @@ async def api_lnurl_callback(unique_hash): if not link: return ( - jsonify({"status": "ERROR", "reason": "LNURL-withdraw not found."}), + {"status": "ERROR", "reason": "LNURL-withdraw not found."}, HTTPStatus.OK, ) if link.is_spent: return ( - jsonify({"status": "ERROR", "reason": "Withdraw is spent."}), + {"status": "ERROR", "reason": "Withdraw is spent."}, HTTPStatus.OK, ) 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: return ( - jsonify( - {"status": "ERROR", "reason": f"Wait {link.open_time - now} seconds."} - ), + {"status": "ERROR", "reason": f"Wait {link.open_time - now} seconds."}, HTTPStatus.OK, ) @@ -128,12 +124,12 @@ async def api_lnurl_callback(unique_hash): ) except ValueError as e: await update_withdraw_link(link.id, **changesback) - return jsonify({"status": "ERROR", "reason": str(e)}) + return {"status": "ERROR", "reason": str(e)} except PermissionError: 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: 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 diff --git a/lnbits/extensions/withdraw/views_api.py b/lnbits/extensions/withdraw/views_api.py index 6e6d6cab2..7564c5cbd 100644 --- a/lnbits/extensions/withdraw/views_api.py +++ b/lnbits/extensions/withdraw/views_api.py @@ -29,24 +29,20 @@ async def api_links(): wallet_ids = (await get_user(g.wallet.user)).wallet_ids try: return ( - jsonable_encoder( [ { **link._asdict(), **{"lnurl": link.lnurl}, } for link in await get_withdraw_links(wallet_ids) - ] - ), + ], HTTPStatus.OK, ) except LnurlInvalidUrl: return ( - jsonable_encoder( { "message": "LNURLs need to be delivered over a publically accessible `https` domain or Tor." - } - ), + }, HTTPStatus.UPGRADE_REQUIRED, ) @@ -57,15 +53,14 @@ async def api_link_retrieve(link_id): link = await get_withdraw_link(link_id, 0) if not link: - return ( - jsonable_encoder({"message": "Withdraw link does not exist."}), + return ({"message": "Withdraw link does not exist."}, HTTPStatus.NOT_FOUND, ) 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): title: str = Query(...) @@ -81,11 +76,9 @@ class CreateData(BaseModel): async def api_link_create_or_update(data: CreateData, link_id: str = None): if data.max_withdrawable < data.min_withdrawable: return ( - jsonable_encoder( { "message": "`max_withdrawable` needs to be at least `min_withdrawable`." - } - ), + }, 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 ) - return ( - jsonable_encoder({**link, **{"lnurl": link.lnurl}}), + return ({**link, **{"lnurl": link.lnurl}}, 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) if not link: - return ( - jsonable_encoder({"message": "Withdraw link does not exist."}), + return ({"message": "Withdraw link does not exist."}, HTTPStatus.NOT_FOUND, ) 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) @@ -141,4 +132,4 @@ async def api_link_delete(link_id): @api_check_wallet_key("invoice") async def api_hash_retrieve(the_hash, lnurl_id): hashCheck = await get_hash_check(the_hash, lnurl_id) - return jsonable_encoder(hashCheck), HTTPStatus.OK + return hashCheck, HTTPStatus.OK