From abfee29676eebe860f8f4e57e78e4356cce6d245 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dni=20=E2=9A=A1?= Date: Mon, 6 Feb 2023 10:07:40 +0100 Subject: [PATCH] uncomment webhook_listerner and add TODO --- lnbits/wallets/lnpay.py | 50 +++++++++++++++++++++----------------- lnbits/wallets/opennode.py | 36 +++++++++++++-------------- 2 files changed, 46 insertions(+), 40 deletions(-) diff --git a/lnbits/wallets/lnpay.py b/lnbits/wallets/lnpay.py index d75e169c3..f53c5c59f 100644 --- a/lnbits/wallets/lnpay.py +++ b/lnbits/wallets/lnpay.py @@ -1,8 +1,12 @@ import asyncio import hashlib +import json +from http import HTTPStatus from typing import AsyncGenerator, Dict, Optional import httpx +from fastapi import HTTPException +from loguru import logger from lnbits.settings import settings @@ -133,27 +137,29 @@ class LNPayWallet(Wallet): value = await self.queue.get() yield value - # async def webhook_listener(self): - # text: str = await request.get_data() - # try: - # data = json.loads(text) - # except json.decoder.JSONDecodeError: - # logger.error(f"got something wrong on lnpay webhook endpoint: {text[:200]}") - # data = None - # if ( - # type(data) is not dict - # or "event" not in data - # or data["event"].get("name") != "wallet_receive" - # ): - # raise HTTPException(status_code=HTTPStatus.NO_CONTENT) + async def webhook_listener(self): + # TODO: request.get_data is undefined, was it something with Flask or quart? + # probably issue introduced when refactoring? + text: str = await request.get_data() # type: ignore + try: + data = json.loads(text) + except json.decoder.JSONDecodeError: + logger.error(f"got something wrong on lnpay webhook endpoint: {text[:200]}") + data = None + if ( + type(data) is not dict + or "event" not in data + or data["event"].get("name") != "wallet_receive" + ): + raise HTTPException(status_code=HTTPStatus.NO_CONTENT) - # lntx_id = data["data"]["wtx"]["lnTx"]["id"] - # async with httpx.AsyncClient() as client: - # r = await client.get( - # f"{self.endpoint}/lntx/{lntx_id}?fields=settled", headers=self.auth - # ) - # data = r.json() - # if data["settled"]: - # await self.queue.put(lntx_id) + lntx_id = data["data"]["wtx"]["lnTx"]["id"] + async with httpx.AsyncClient() as client: + r = await client.get( + f"{self.endpoint}/lntx/{lntx_id}?fields=settled", headers=self.auth + ) + data = r.json() + if data["settled"]: + await self.queue.put(lntx_id) - # raise HTTPException(status_code=HTTPStatus.NO_CONTENT) + raise HTTPException(status_code=HTTPStatus.NO_CONTENT) diff --git a/lnbits/wallets/opennode.py b/lnbits/wallets/opennode.py index 2aa3b8371..a491cfab3 100644 --- a/lnbits/wallets/opennode.py +++ b/lnbits/wallets/opennode.py @@ -1,10 +1,11 @@ import asyncio - -# import hmac -# from http import HTTPStatus +import hmac +from http import HTTPStatus from typing import AsyncGenerator, Optional import httpx +from fastapi import HTTPException +from loguru import logger from lnbits.settings import settings @@ -17,9 +18,6 @@ from .base import ( Wallet, ) -# from fastapi import Request, HTTPException -# from loguru import logger - class OpenNodeWallet(Wallet): """https://developers.opennode.com/""" @@ -142,17 +140,19 @@ class OpenNodeWallet(Wallet): value = await self.queue.get() yield value - # async def webhook_listener(self): - # data = await request.form - # if "status" not in data or data["status"] != "paid": - # raise HTTPException(status_code=HTTPStatus.NO_CONTENT) + async def webhook_listener(self): + # TODO: request.form is undefined, was it something with Flask or quart? + # probably issue introduced when refactoring? + data = await request.form # type: ignore + if "status" not in data or data["status"] != "paid": + raise HTTPException(status_code=HTTPStatus.NO_CONTENT) - # charge_id = data["id"] - # x = hmac.new(self.auth["Authorization"].encode("ascii"), digestmod="sha256") - # x.update(charge_id.encode("ascii")) - # if x.hexdigest() != data["hashed_order"]: - # logger.error("invalid webhook, not from opennode") - # raise HTTPException(status_code=HTTPStatus.NO_CONTENT) + charge_id = data["id"] + x = hmac.new(self.auth["Authorization"].encode("ascii"), digestmod="sha256") + x.update(charge_id.encode("ascii")) + if x.hexdigest() != data["hashed_order"]: + logger.error("invalid webhook, not from opennode") + raise HTTPException(status_code=HTTPStatus.NO_CONTENT) - # await self.queue.put(charge_id) - # raise HTTPException(status_code=HTTPStatus.NO_CONTENT) + await self.queue.put(charge_id) + raise HTTPException(status_code=HTTPStatus.NO_CONTENT)