From 514329045f2cf9ab570b250e460542dd61562fd4 Mon Sep 17 00:00:00 2001 From: benarc Date: Wed, 17 Mar 2021 16:33:12 +0000 Subject: [PATCH] Should work --- lnbits/extensions/withdraw/crud.py | 25 +++++++++++++++++++++++++ lnbits/extensions/withdraw/models.py | 1 - lnbits/extensions/withdraw/views_api.py | 16 ++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/lnbits/extensions/withdraw/crud.py b/lnbits/extensions/withdraw/crud.py index 78fd7f566..64d4373e2 100644 --- a/lnbits/extensions/withdraw/crud.py +++ b/lnbits/extensions/withdraw/crud.py @@ -98,3 +98,28 @@ async def delete_withdraw_link(link_id: str) -> None: def chunks(lst, n): for i in range(0, len(lst), n): yield lst[i : i + n] + +async def create_hash_check( + the_hash: str, + lnurl_id: str, +) -> HashCheck: + await db.execute( + """ + INSERT INTO hash_check ( + id, + lnurl_id + ) + VALUES (?, ?) + """, + ( + the_hash, + lnurl_id, + ), + ) + hashCheck = await get_hash_check(the_hash, lnurl_id) + row = await db.fetchone("SELECT * FROM hash_check WHERE id = ?", (the_hash,)) + return HashCheck.from_row(row) if row else None + +async def get_hash_check(the_hash: str, lnurl_id: str) -> Optional[HashCheck]: + row = await db.fetchone("SELECT * FROM hash_check WHERE id = ?", (the_hash,)) + return HashCheck.from_row(row) if row else None \ No newline at end of file diff --git a/lnbits/extensions/withdraw/models.py b/lnbits/extensions/withdraw/models.py index 1903dba69..4d147f254 100644 --- a/lnbits/extensions/withdraw/models.py +++ b/lnbits/extensions/withdraw/models.py @@ -63,7 +63,6 @@ class WithdrawLink(NamedTuple): class HashCheck(NamedTuple): id: str lnurl_id: str - @classmethod def from_row(cls, row: Row) -> "Hash": return cls(**dict(row)) \ No newline at end of file diff --git a/lnbits/extensions/withdraw/views_api.py b/lnbits/extensions/withdraw/views_api.py index cb8b7f0a7..d34422baa 100644 --- a/lnbits/extensions/withdraw/views_api.py +++ b/lnbits/extensions/withdraw/views_api.py @@ -12,6 +12,8 @@ from .crud import ( get_withdraw_links, update_withdraw_link, delete_withdraw_link, + create_hash_check, + get_hash_check, ) @@ -111,3 +113,17 @@ async def api_link_delete(link_id): await delete_withdraw_link(link_id) return "", HTTPStatus.NO_CONTENT + +@withdraw_ext.route("/api/v1/links//", methods=["GET"]) +@api_check_wallet_key("invoice") +async def api_hash_retrieve(the_hash, lnurl_id): + hashCheck = await get_hash_check(the_hash, lnurl_id) + + if not hashCheck: + hashCheck = await create_hash_check(the_hash, lnurl_id) + return jsonify({"status": False}), HTTPStatus.OK + + if link.wallet != g.wallet.id: + return jsonify({"status": True}), HTTPStatus.OK + + return jsonify({"status": True}), HTTPStatus.OK