2021-08-30 19:55:02 +02:00
|
|
|
import asyncio
|
2023-02-06 10:07:40 +01:00
|
|
|
import hmac
|
|
|
|
from http import HTTPStatus
|
2022-07-16 14:23:03 +02:00
|
|
|
from typing import AsyncGenerator, Optional
|
2020-01-16 13:58:27 +01:00
|
|
|
|
2022-07-16 14:23:03 +02:00
|
|
|
import httpx
|
2023-02-06 10:07:40 +01:00
|
|
|
from fastapi import HTTPException
|
|
|
|
from loguru import logger
|
2022-07-07 14:30:16 +02:00
|
|
|
|
2022-10-05 13:01:41 +02:00
|
|
|
from lnbits.settings import settings
|
2022-07-16 14:23:03 +02:00
|
|
|
|
2021-03-24 00:40:32 -03:00
|
|
|
from .base import (
|
|
|
|
InvoiceResponse,
|
|
|
|
PaymentResponse,
|
|
|
|
PaymentStatus,
|
2022-07-16 14:23:03 +02:00
|
|
|
StatusResponse,
|
2021-03-24 00:40:32 -03:00
|
|
|
Unsupported,
|
2022-07-16 14:23:03 +02:00
|
|
|
Wallet,
|
2021-03-24 00:40:32 -03:00
|
|
|
)
|
2020-01-16 13:58:27 +01:00
|
|
|
|
2020-01-12 00:26:40 +00:00
|
|
|
|
2020-01-15 15:16:10 +00:00
|
|
|
class OpenNodeWallet(Wallet):
|
2020-03-31 19:05:25 +02:00
|
|
|
"""https://developers.opennode.com/"""
|
2020-01-12 00:26:40 +00:00
|
|
|
|
2020-03-31 19:05:25 +02:00
|
|
|
def __init__(self):
|
2022-10-05 13:01:41 +02:00
|
|
|
endpoint = settings.opennode_api_endpoint
|
2021-03-24 00:40:32 -03:00
|
|
|
key = (
|
2022-10-05 13:01:41 +02:00
|
|
|
settings.opennode_key
|
|
|
|
or settings.opennode_admin_key
|
|
|
|
or settings.opennode_invoice_key
|
2021-03-24 00:40:32 -03:00
|
|
|
)
|
2023-02-02 12:57:36 +00:00
|
|
|
if not endpoint or not key:
|
|
|
|
raise Exception("cannot initialize opennode")
|
|
|
|
|
|
|
|
self.endpoint = endpoint[:-1] if endpoint.endswith("/") else endpoint
|
2020-10-08 16:03:18 -03:00
|
|
|
self.auth = {"Authorization": key}
|
2023-06-19 12:12:00 +02:00
|
|
|
self.client = httpx.AsyncClient(base_url=self.endpoint, headers=self.auth)
|
|
|
|
|
|
|
|
async def cleanup(self):
|
2023-07-21 09:50:50 +02:00
|
|
|
try:
|
|
|
|
await self.client.aclose()
|
|
|
|
except RuntimeError as e:
|
|
|
|
logger.warning(f"Error closing wallet connection: {e}")
|
2020-11-09 09:38:06 -03:00
|
|
|
|
2021-03-24 01:01:09 -03:00
|
|
|
async def status(self) -> StatusResponse:
|
2020-10-12 22:25:55 -03:00
|
|
|
try:
|
2023-06-19 12:12:00 +02:00
|
|
|
r = await self.client.get("/v1/account/balance", timeout=40)
|
2020-10-12 22:25:55 -03:00
|
|
|
except (httpx.ConnectError, httpx.RequestError):
|
2020-10-13 14:46:23 -03:00
|
|
|
return StatusResponse(f"Unable to connect to '{self.endpoint}'", 0)
|
2020-10-12 22:25:55 -03:00
|
|
|
|
2020-11-08 23:15:27 +00:00
|
|
|
data = r.json()["data"]
|
2020-10-12 22:25:55 -03:00
|
|
|
if r.is_error:
|
|
|
|
return StatusResponse(data["message"], 0)
|
|
|
|
|
2022-08-30 13:28:58 +02:00
|
|
|
return StatusResponse(None, data["balance"]["BTC"] * 1000)
|
2020-10-12 22:25:55 -03:00
|
|
|
|
2021-03-24 01:01:09 -03:00
|
|
|
async def create_invoice(
|
2021-03-24 00:40:32 -03:00
|
|
|
self,
|
|
|
|
amount: int,
|
|
|
|
memo: Optional[str] = None,
|
|
|
|
description_hash: Optional[bytes] = None,
|
2022-08-13 14:29:04 +02:00
|
|
|
unhashed_description: Optional[bytes] = None,
|
2023-02-06 12:19:16 +01:00
|
|
|
**kwargs,
|
2020-08-30 23:48:46 -03:00
|
|
|
) -> InvoiceResponse:
|
2022-08-13 14:29:04 +02:00
|
|
|
if description_hash or unhashed_description:
|
2020-06-07 19:46:16 -03:00
|
|
|
raise Unsupported("description_hash")
|
|
|
|
|
2023-06-19 12:12:00 +02:00
|
|
|
r = await self.client.post(
|
|
|
|
"/v1/charges",
|
|
|
|
json={
|
|
|
|
"amount": amount,
|
|
|
|
"description": memo or "",
|
|
|
|
# "callback_url": url_for("/webhook_listener", _external=True),
|
|
|
|
},
|
|
|
|
timeout=40,
|
|
|
|
)
|
2020-03-31 19:05:25 +02:00
|
|
|
|
2020-10-02 17:13:33 -03:00
|
|
|
if r.is_error:
|
2020-03-31 19:05:25 +02:00
|
|
|
error_message = r.json()["message"]
|
2020-10-02 17:13:33 -03:00
|
|
|
return InvoiceResponse(False, None, None, error_message)
|
2020-01-12 00:26:40 +00:00
|
|
|
|
2020-10-02 17:13:33 -03:00
|
|
|
data = r.json()["data"]
|
|
|
|
checking_id = data["id"]
|
|
|
|
payment_request = data["lightning_invoice"]["payreq"]
|
2020-10-05 08:46:20 -03:00
|
|
|
return InvoiceResponse(True, checking_id, payment_request, None)
|
2020-01-12 00:26:40 +00:00
|
|
|
|
2022-03-16 07:20:15 +01:00
|
|
|
async def pay_invoice(self, bolt11: str, fee_limit_msat: int) -> PaymentResponse:
|
2023-06-19 12:12:00 +02:00
|
|
|
r = await self.client.post(
|
|
|
|
"/v2/withdrawals",
|
|
|
|
json={"type": "ln", "address": bolt11},
|
|
|
|
timeout=None,
|
|
|
|
)
|
2020-03-31 19:05:25 +02:00
|
|
|
|
2020-10-02 17:13:33 -03:00
|
|
|
if r.is_error:
|
2020-03-31 19:05:25 +02:00
|
|
|
error_message = r.json()["message"]
|
2022-08-30 13:28:58 +02:00
|
|
|
return PaymentResponse(False, None, None, None, error_message)
|
2020-03-31 19:05:25 +02:00
|
|
|
|
2020-10-02 17:13:33 -03:00
|
|
|
data = r.json()["data"]
|
2020-10-05 08:46:20 -03:00
|
|
|
checking_id = data["id"]
|
2022-08-30 13:28:58 +02:00
|
|
|
fee_msat = -data["fee"] * 1000
|
|
|
|
|
|
|
|
if data["status"] != "paid":
|
|
|
|
return PaymentResponse(None, checking_id, fee_msat, None, "payment failed")
|
|
|
|
|
2020-10-12 23:18:37 -03:00
|
|
|
return PaymentResponse(True, checking_id, fee_msat, None, None)
|
2020-01-12 00:26:40 +00:00
|
|
|
|
2021-03-24 01:01:09 -03:00
|
|
|
async def get_invoice_status(self, checking_id: str) -> PaymentStatus:
|
2023-06-19 12:12:00 +02:00
|
|
|
r = await self.client.get(f"/v1/charge/{checking_id}")
|
2020-10-02 17:13:33 -03:00
|
|
|
if r.is_error:
|
2020-03-31 19:05:25 +02:00
|
|
|
return PaymentStatus(None)
|
2022-08-30 13:28:58 +02:00
|
|
|
data = r.json()["data"]
|
|
|
|
statuses = {"processing": None, "paid": True, "unpaid": None}
|
|
|
|
return PaymentStatus(statuses[data.get("status")])
|
2020-01-12 00:26:40 +00:00
|
|
|
|
2021-03-24 01:01:09 -03:00
|
|
|
async def get_payment_status(self, checking_id: str) -> PaymentStatus:
|
2023-06-19 12:12:00 +02:00
|
|
|
r = await self.client.get(f"/v1/withdrawal/{checking_id}")
|
2020-01-12 00:26:40 +00:00
|
|
|
|
2020-10-02 17:13:33 -03:00
|
|
|
if r.is_error:
|
2020-03-31 19:05:25 +02:00
|
|
|
return PaymentStatus(None)
|
2020-01-12 00:26:40 +00:00
|
|
|
|
2022-08-30 13:28:58 +02:00
|
|
|
data = r.json()["data"]
|
2021-03-24 00:40:32 -03:00
|
|
|
statuses = {
|
|
|
|
"initial": None,
|
|
|
|
"pending": None,
|
|
|
|
"confirmed": True,
|
2022-08-30 13:28:58 +02:00
|
|
|
"error": None,
|
2021-03-24 00:40:32 -03:00
|
|
|
"failed": False,
|
|
|
|
}
|
2022-08-30 13:28:58 +02:00
|
|
|
fee_msat = -data.get("fee") * 1000
|
|
|
|
return PaymentStatus(statuses[data.get("status")], fee_msat)
|
2020-09-29 00:52:27 -03:00
|
|
|
|
|
|
|
async def paid_invoices_stream(self) -> AsyncGenerator[str, None]:
|
2022-07-19 18:51:35 +02:00
|
|
|
self.queue: asyncio.Queue = asyncio.Queue(0)
|
2021-08-30 19:55:02 +02:00
|
|
|
while True:
|
|
|
|
value = await self.queue.get()
|
2020-10-03 21:57:14 -03:00
|
|
|
yield value
|
2020-09-29 00:52:27 -03:00
|
|
|
|
2023-02-06 10:07:40 +01:00
|
|
|
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)
|
|
|
|
|
|
|
|
await self.queue.put(charge_id)
|
|
|
|
raise HTTPException(status_code=HTTPStatus.NO_CONTENT)
|