2020-10-06 05:39:54 +02:00
|
|
|
import trio # type: ignore
|
|
|
|
import json
|
2020-10-13 19:46:23 +02:00
|
|
|
import lnurl # type: ignore
|
2020-10-12 03:19:27 +02:00
|
|
|
import httpx
|
2020-10-08 21:03:18 +02:00
|
|
|
import traceback
|
2020-10-12 23:15:27 +02:00
|
|
|
from urllib.parse import urlparse, urlunparse, urlencode, parse_qs, ParseResult
|
2020-10-06 05:39:54 +02:00
|
|
|
from quart import g, jsonify, request, make_response
|
2020-05-03 15:57:05 +02:00
|
|
|
from http import HTTPStatus
|
2020-06-08 00:46:16 +02:00
|
|
|
from binascii import unhexlify
|
2020-10-12 23:15:27 +02:00
|
|
|
from typing import Dict, Union
|
2020-03-04 23:11:15 +01:00
|
|
|
|
2020-09-01 03:12:46 +02:00
|
|
|
from lnbits import bolt11
|
2020-04-16 17:27:36 +02:00
|
|
|
from lnbits.decorators import api_check_wallet_key, api_validate_post_request
|
2020-03-04 23:11:15 +01:00
|
|
|
|
2020-10-06 05:39:54 +02:00
|
|
|
from .. import core_app
|
2020-11-10 04:25:46 +01:00
|
|
|
from ..services import create_invoice, pay_invoice, perform_lnurlauth
|
2020-10-06 05:39:54 +02:00
|
|
|
from ..crud import delete_expired_invoices
|
|
|
|
from ..tasks import sse_listeners
|
|
|
|
|
2020-03-04 23:11:15 +01:00
|
|
|
|
2020-09-29 20:43:11 +02:00
|
|
|
@core_app.route("/api/v1/wallet", methods=["GET"])
|
|
|
|
@api_check_wallet_key("invoice")
|
|
|
|
async def api_wallet():
|
|
|
|
return (
|
2020-10-07 02:53:43 +02:00
|
|
|
jsonify(
|
|
|
|
{
|
|
|
|
"id": g.wallet.id,
|
|
|
|
"name": g.wallet.name,
|
|
|
|
"balance": g.wallet.balance_msat,
|
|
|
|
}
|
|
|
|
),
|
2020-09-29 20:43:11 +02:00
|
|
|
HTTPStatus.OK,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-03-07 22:27:00 +01:00
|
|
|
@core_app.route("/api/v1/payments", methods=["GET"])
|
2020-04-16 17:27:36 +02:00
|
|
|
@api_check_wallet_key("invoice")
|
2020-09-14 02:31:05 +02:00
|
|
|
async def api_payments():
|
2020-03-07 22:27:00 +01:00
|
|
|
if "check_pending" in request.args:
|
2020-09-02 05:58:21 +02:00
|
|
|
delete_expired_invoices()
|
2020-04-17 21:13:57 +02:00
|
|
|
|
2020-09-12 03:14:25 +02:00
|
|
|
for payment in g.wallet.get_payments(complete=False, pending=True, exclude_uncheckable=True):
|
2020-09-28 04:12:55 +02:00
|
|
|
payment.check_pending()
|
2020-03-07 22:27:00 +01:00
|
|
|
|
2020-09-29 20:43:11 +02:00
|
|
|
return jsonify(g.wallet.get_payments(pending=True)), HTTPStatus.OK
|
2020-03-07 22:27:00 +01:00
|
|
|
|
|
|
|
|
2020-04-16 17:27:36 +02:00
|
|
|
@api_check_wallet_key("invoice")
|
2020-04-11 19:47:25 +02:00
|
|
|
@api_validate_post_request(
|
|
|
|
schema={
|
|
|
|
"amount": {"type": "integer", "min": 1, "required": True},
|
2020-06-13 03:46:40 +02:00
|
|
|
"memo": {"type": "string", "empty": False, "required": True, "excludes": "description_hash"},
|
|
|
|
"description_hash": {"type": "string", "empty": False, "required": True, "excludes": "memo"},
|
2020-10-13 04:17:50 +02:00
|
|
|
"lnurl_callback": {"type": "string", "nullable": True, "required": False},
|
2020-04-11 19:47:25 +02:00
|
|
|
}
|
|
|
|
)
|
2020-09-14 02:31:05 +02:00
|
|
|
async def api_payments_create_invoice():
|
2020-06-08 00:46:16 +02:00
|
|
|
if "description_hash" in g.data:
|
|
|
|
description_hash = unhexlify(g.data["description_hash"])
|
|
|
|
memo = ""
|
|
|
|
else:
|
|
|
|
description_hash = b""
|
|
|
|
memo = g.data["memo"]
|
|
|
|
|
2020-03-04 23:11:15 +01:00
|
|
|
try:
|
2020-09-01 03:12:46 +02:00
|
|
|
payment_hash, payment_request = create_invoice(
|
2020-06-08 00:46:16 +02:00
|
|
|
wallet_id=g.wallet.id, amount=g.data["amount"], memo=memo, description_hash=description_hash
|
2020-04-11 22:18:17 +02:00
|
|
|
)
|
2020-03-31 19:05:25 +02:00
|
|
|
except Exception as e:
|
2020-09-07 05:47:13 +02:00
|
|
|
g.db.rollback()
|
2020-05-03 15:57:05 +02:00
|
|
|
return jsonify({"message": str(e)}), HTTPStatus.INTERNAL_SERVER_ERROR
|
2020-03-04 23:11:15 +01:00
|
|
|
|
2020-09-01 03:12:46 +02:00
|
|
|
invoice = bolt11.decode(payment_request)
|
2020-10-12 23:15:27 +02:00
|
|
|
|
|
|
|
lnurl_response: Union[None, bool, str] = None
|
2020-10-13 04:17:50 +02:00
|
|
|
if g.data.get("lnurl_callback"):
|
2020-10-15 17:58:37 +02:00
|
|
|
async with httpx.AsyncClient() as client:
|
|
|
|
try:
|
|
|
|
r = await client.get(
|
|
|
|
g.data["lnurl_callback"],
|
|
|
|
params={"pr": payment_request},
|
|
|
|
timeout=10,
|
|
|
|
)
|
|
|
|
if r.is_error:
|
|
|
|
lnurl_response = r.text
|
2020-10-12 23:15:27 +02:00
|
|
|
else:
|
2020-10-15 17:58:37 +02:00
|
|
|
resp = json.loads(r.text)
|
|
|
|
if resp["status"] != "OK":
|
|
|
|
lnurl_response = resp["reason"]
|
|
|
|
else:
|
|
|
|
lnurl_response = True
|
|
|
|
except (httpx.ConnectError, httpx.RequestError):
|
|
|
|
lnurl_response = False
|
2020-10-12 23:15:27 +02:00
|
|
|
|
2020-09-01 03:12:46 +02:00
|
|
|
return (
|
|
|
|
jsonify(
|
|
|
|
{
|
|
|
|
"payment_hash": invoice.payment_hash,
|
|
|
|
"payment_request": payment_request,
|
|
|
|
# maintain backwards compatibility with API clients:
|
|
|
|
"checking_id": invoice.payment_hash,
|
2020-10-12 23:15:27 +02:00
|
|
|
"lnurl_response": lnurl_response,
|
2020-09-01 03:12:46 +02:00
|
|
|
}
|
|
|
|
),
|
|
|
|
HTTPStatus.CREATED,
|
|
|
|
)
|
2020-03-04 23:11:15 +01:00
|
|
|
|
|
|
|
|
2020-04-16 17:27:36 +02:00
|
|
|
@api_check_wallet_key("admin")
|
2020-04-11 19:47:25 +02:00
|
|
|
@api_validate_post_request(schema={"bolt11": {"type": "string", "empty": False, "required": True}})
|
2020-09-14 02:31:05 +02:00
|
|
|
async def api_payments_pay_invoice():
|
2020-03-07 22:27:00 +01:00
|
|
|
try:
|
2020-09-01 03:12:46 +02:00
|
|
|
payment_hash = pay_invoice(wallet_id=g.wallet.id, payment_request=g.data["bolt11"])
|
2020-04-16 17:10:53 +02:00
|
|
|
except ValueError as e:
|
2020-05-03 15:57:05 +02:00
|
|
|
return jsonify({"message": str(e)}), HTTPStatus.BAD_REQUEST
|
2020-04-16 17:10:53 +02:00
|
|
|
except PermissionError as e:
|
2020-05-03 15:57:05 +02:00
|
|
|
return jsonify({"message": str(e)}), HTTPStatus.FORBIDDEN
|
2020-10-08 21:03:18 +02:00
|
|
|
except Exception as exc:
|
|
|
|
traceback.print_exc(7)
|
2020-09-07 05:47:13 +02:00
|
|
|
g.db.rollback()
|
2020-10-08 21:03:18 +02:00
|
|
|
return jsonify({"message": str(exc)}), HTTPStatus.INTERNAL_SERVER_ERROR
|
2020-03-07 22:27:00 +01:00
|
|
|
|
2020-09-01 03:12:46 +02:00
|
|
|
return (
|
|
|
|
jsonify(
|
|
|
|
{
|
|
|
|
"payment_hash": payment_hash,
|
|
|
|
# maintain backwards compatibility with API clients:
|
|
|
|
"checking_id": payment_hash,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
HTTPStatus.CREATED,
|
|
|
|
)
|
2020-03-07 22:27:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
@core_app.route("/api/v1/payments", methods=["POST"])
|
2020-04-11 19:47:25 +02:00
|
|
|
@api_validate_post_request(schema={"out": {"type": "boolean", "required": True}})
|
2020-09-14 02:31:05 +02:00
|
|
|
async def api_payments_create():
|
2020-03-07 22:27:00 +01:00
|
|
|
if g.data["out"] is True:
|
2020-09-14 02:31:05 +02:00
|
|
|
return await api_payments_pay_invoice()
|
|
|
|
return await api_payments_create_invoice()
|
2020-03-07 22:27:00 +01:00
|
|
|
|
|
|
|
|
2020-10-12 23:15:27 +02:00
|
|
|
@core_app.route("/api/v1/payments/lnurl", methods=["POST"])
|
|
|
|
@api_check_wallet_key("admin")
|
|
|
|
@api_validate_post_request(
|
|
|
|
schema={
|
|
|
|
"description_hash": {"type": "string", "empty": False, "required": True},
|
|
|
|
"callback": {"type": "string", "empty": False, "required": True},
|
|
|
|
"amount": {"type": "number", "empty": False, "required": True},
|
2020-10-13 20:18:34 +02:00
|
|
|
"comment": {"type": "string", "nullable": True, "empty": True, "required": False},
|
|
|
|
"description": {"type": "string", "nullable": True, "empty": True, "required": False},
|
2020-10-12 23:15:27 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
async def api_payments_pay_lnurl():
|
2020-10-15 03:44:23 +02:00
|
|
|
domain = urlparse(g.data["callback"]).netloc
|
|
|
|
|
2020-10-15 17:58:37 +02:00
|
|
|
async with httpx.AsyncClient() as client:
|
|
|
|
try:
|
|
|
|
r = await client.get(
|
|
|
|
g.data["callback"],
|
|
|
|
params={"amount": g.data["amount"], "comment": g.data["comment"]},
|
|
|
|
timeout=40,
|
|
|
|
)
|
|
|
|
if r.is_error:
|
|
|
|
return jsonify({"message": "failed to connect"}), HTTPStatus.BAD_REQUEST
|
|
|
|
except (httpx.ConnectError, httpx.RequestError):
|
2020-10-12 23:15:27 +02:00
|
|
|
return jsonify({"message": "failed to connect"}), HTTPStatus.BAD_REQUEST
|
|
|
|
|
|
|
|
params = json.loads(r.text)
|
|
|
|
if params.get("status") == "ERROR":
|
|
|
|
return jsonify({"message": f"{domain} said: '{params.get('reason', '')}'"}), HTTPStatus.BAD_REQUEST
|
|
|
|
|
|
|
|
invoice = bolt11.decode(params["pr"])
|
|
|
|
if invoice.amount_msat != g.data["amount"]:
|
|
|
|
return (
|
|
|
|
jsonify(
|
|
|
|
{
|
|
|
|
"message": f"{domain} returned an invalid invoice. Expected {g.data['amount']} msat, got {invoice.amount_msat}."
|
|
|
|
}
|
|
|
|
),
|
|
|
|
HTTPStatus.BAD_REQUEST,
|
|
|
|
)
|
|
|
|
if invoice.description_hash != g.data["description_hash"]:
|
|
|
|
return (
|
|
|
|
jsonify(
|
|
|
|
{
|
|
|
|
"message": f"{domain} returned an invalid invoice. Expected description_hash == {g.data['description_hash']}, got {invoice.description_hash}."
|
|
|
|
}
|
|
|
|
),
|
|
|
|
HTTPStatus.BAD_REQUEST,
|
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
2020-10-21 14:50:45 +02:00
|
|
|
extra = {}
|
|
|
|
|
|
|
|
if params.get("successAction"):
|
|
|
|
extra["success_action"] = params["successAction"]
|
|
|
|
if g.data["comment"]:
|
|
|
|
extra["comment"] = g.data["comment"]
|
|
|
|
|
2020-10-12 23:15:27 +02:00
|
|
|
payment_hash = pay_invoice(
|
|
|
|
wallet_id=g.wallet.id,
|
|
|
|
payment_request=params["pr"],
|
|
|
|
description=g.data.get("description", ""),
|
2020-10-21 14:50:45 +02:00
|
|
|
extra=extra,
|
2020-10-12 23:15:27 +02:00
|
|
|
)
|
|
|
|
except Exception as exc:
|
|
|
|
traceback.print_exc(7)
|
|
|
|
g.db.rollback()
|
|
|
|
return jsonify({"message": str(exc)}), HTTPStatus.INTERNAL_SERVER_ERROR
|
|
|
|
|
|
|
|
return (
|
|
|
|
jsonify(
|
|
|
|
{
|
|
|
|
"success_action": params.get("successAction"),
|
|
|
|
"payment_hash": payment_hash,
|
|
|
|
# maintain backwards compatibility with API clients:
|
|
|
|
"checking_id": payment_hash,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
HTTPStatus.CREATED,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-09-01 03:12:46 +02:00
|
|
|
@core_app.route("/api/v1/payments/<payment_hash>", methods=["GET"])
|
2020-04-16 17:27:36 +02:00
|
|
|
@api_check_wallet_key("invoice")
|
2020-09-14 02:31:05 +02:00
|
|
|
async def api_payment(payment_hash):
|
2020-09-01 03:12:46 +02:00
|
|
|
payment = g.wallet.get_payment(payment_hash)
|
2020-03-07 22:27:00 +01:00
|
|
|
|
|
|
|
if not payment:
|
2020-05-03 15:57:05 +02:00
|
|
|
return jsonify({"message": "Payment does not exist."}), HTTPStatus.NOT_FOUND
|
2020-03-07 22:27:00 +01:00
|
|
|
elif not payment.pending:
|
2020-10-13 18:57:26 +02:00
|
|
|
return jsonify({"paid": True, "preimage": payment.preimage}), HTTPStatus.OK
|
2020-03-04 23:11:15 +01:00
|
|
|
|
|
|
|
try:
|
2020-09-28 04:12:55 +02:00
|
|
|
payment.check_pending()
|
2020-03-04 23:11:15 +01:00
|
|
|
except Exception:
|
2020-05-03 15:57:05 +02:00
|
|
|
return jsonify({"paid": False}), HTTPStatus.OK
|
2020-03-04 23:11:15 +01:00
|
|
|
|
2020-10-13 18:57:26 +02:00
|
|
|
return jsonify({"paid": not payment.pending, "preimage": payment.preimage}), HTTPStatus.OK
|
2020-10-06 05:39:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
@core_app.route("/api/v1/payments/sse", methods=["GET"])
|
2020-10-15 05:18:56 +02:00
|
|
|
@api_check_wallet_key("invoice", accept_querystring=True)
|
2020-10-06 05:39:54 +02:00
|
|
|
async def api_payments_sse():
|
|
|
|
g.db.close()
|
2020-10-06 06:50:55 +02:00
|
|
|
this_wallet_id = g.wallet.id
|
2020-10-06 05:39:54 +02:00
|
|
|
|
|
|
|
send_payment, receive_payment = trio.open_memory_channel(0)
|
|
|
|
|
|
|
|
print("adding sse listener", send_payment)
|
|
|
|
sse_listeners.append(send_payment)
|
|
|
|
|
2020-10-15 05:18:56 +02:00
|
|
|
send_event, event_to_send = trio.open_memory_channel(0)
|
2020-10-06 05:39:54 +02:00
|
|
|
|
|
|
|
async def payment_received() -> None:
|
|
|
|
async for payment in receive_payment:
|
2020-10-06 06:50:55 +02:00
|
|
|
if payment.wallet_id == this_wallet_id:
|
2020-10-15 05:18:56 +02:00
|
|
|
await send_event.send(("payment-received", payment))
|
2020-10-06 05:39:54 +02:00
|
|
|
|
|
|
|
async def repeat_keepalive():
|
|
|
|
await trio.sleep(1)
|
|
|
|
while True:
|
|
|
|
await send_event.send(("keepalive", ""))
|
|
|
|
await trio.sleep(25)
|
|
|
|
|
|
|
|
g.nursery.start_soon(payment_received)
|
|
|
|
g.nursery.start_soon(repeat_keepalive)
|
|
|
|
|
|
|
|
async def send_events():
|
|
|
|
try:
|
2020-10-15 05:18:56 +02:00
|
|
|
async for typ, data in event_to_send:
|
2020-10-06 05:39:54 +02:00
|
|
|
message = [f"event: {typ}".encode("utf-8")]
|
|
|
|
|
|
|
|
if data:
|
2020-10-10 03:05:39 +02:00
|
|
|
jdata = json.dumps(dict(data._asdict(), pending=False))
|
2020-10-06 05:39:54 +02:00
|
|
|
message.append(f"data: {jdata}".encode("utf-8"))
|
|
|
|
|
|
|
|
yield b"\n".join(message) + b"\r\n\r\n"
|
|
|
|
except trio.Cancelled:
|
|
|
|
return
|
|
|
|
|
|
|
|
response = await make_response(
|
|
|
|
send_events(),
|
|
|
|
{
|
|
|
|
"Content-Type": "text/event-stream",
|
|
|
|
"Cache-Control": "no-cache",
|
|
|
|
"X-Accel-Buffering": "no",
|
|
|
|
"Connection": "keep-alive",
|
|
|
|
"Transfer-Encoding": "chunked",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
response.timeout = None
|
|
|
|
return response
|
2020-09-21 04:58:02 +02:00
|
|
|
|
|
|
|
|
|
|
|
@core_app.route("/api/v1/lnurlscan/<code>", methods=["GET"])
|
|
|
|
@api_check_wallet_key("invoice")
|
|
|
|
async def api_lnurlscan(code: str):
|
|
|
|
try:
|
|
|
|
url = lnurl.Lnurl(code)
|
|
|
|
except ValueError:
|
2020-11-11 02:59:50 +01:00
|
|
|
return jsonify({"message": "invalid lnurl"}), HTTPStatus.BAD_REQUEST
|
2020-09-21 04:58:02 +02:00
|
|
|
|
|
|
|
domain = urlparse(url.url).netloc
|
2020-11-10 04:25:46 +01:00
|
|
|
|
|
|
|
# params is what will be returned to the client
|
|
|
|
params: Dict = {"domain": domain}
|
|
|
|
|
2020-09-21 04:58:02 +02:00
|
|
|
if url.is_login:
|
2020-11-10 04:25:46 +01:00
|
|
|
params.update(kind="auth")
|
|
|
|
params.update(callback=url.url) # with k1 already in it
|
|
|
|
params.update(pubkey=g.wallet.lnurlauth_key.verifying_key.to_string("compressed").hex())
|
|
|
|
else:
|
|
|
|
async with httpx.AsyncClient() as client:
|
|
|
|
r = await client.get(url.url, timeout=40)
|
|
|
|
if r.is_error:
|
2020-11-11 02:59:50 +01:00
|
|
|
return (
|
|
|
|
jsonify({"domain": domain, "message": "failed to get parameters"}),
|
|
|
|
HTTPStatus.SERVICE_UNAVAILABLE,
|
|
|
|
)
|
2020-09-21 04:58:02 +02:00
|
|
|
|
2020-11-10 04:25:46 +01:00
|
|
|
try:
|
|
|
|
jdata = json.loads(r.text)
|
|
|
|
data: lnurl.LnurlResponseModel = lnurl.LnurlResponse.from_dict(jdata)
|
|
|
|
except (json.decoder.JSONDecodeError, lnurl.exceptions.LnurlResponseException):
|
|
|
|
return (
|
2020-11-11 02:59:50 +01:00
|
|
|
jsonify({"domain": domain, "message": f"got invalid response '{r.text[:200]}'"}),
|
2020-11-10 04:25:46 +01:00
|
|
|
HTTPStatus.SERVICE_UNAVAILABLE,
|
|
|
|
)
|
2020-09-21 04:58:02 +02:00
|
|
|
|
2020-11-10 04:25:46 +01:00
|
|
|
if type(data) is lnurl.LnurlChannelResponse:
|
2020-11-11 02:59:50 +01:00
|
|
|
return jsonify({"domain": domain, "kind": "channel", "message": "unsupported"}), HTTPStatus.BAD_REQUEST
|
2020-11-10 04:25:46 +01:00
|
|
|
|
|
|
|
params.update(**data.dict())
|
|
|
|
|
|
|
|
if type(data) is lnurl.LnurlWithdrawResponse:
|
|
|
|
params.update(kind="withdraw")
|
|
|
|
params.update(fixed=data.min_withdrawable == data.max_withdrawable)
|
|
|
|
|
|
|
|
# callback with k1 already in it
|
|
|
|
parsed_callback: ParseResult = urlparse(data.callback)
|
|
|
|
qs: Dict = parse_qs(parsed_callback.query)
|
|
|
|
qs["k1"] = data.k1
|
|
|
|
parsed_callback = parsed_callback._replace(query=urlencode(qs, doseq=True))
|
|
|
|
params.update(callback=urlunparse(parsed_callback))
|
|
|
|
|
|
|
|
if type(data) is lnurl.LnurlPayResponse:
|
|
|
|
params.update(kind="pay")
|
|
|
|
params.update(fixed=data.min_sendable == data.max_sendable)
|
|
|
|
params.update(description_hash=data.metadata.h)
|
|
|
|
params.update(description=data.metadata.text)
|
|
|
|
if data.metadata.images:
|
|
|
|
image = min(data.metadata.images, key=lambda image: len(image[1]))
|
|
|
|
data_uri = "data:" + image[0] + "," + image[1]
|
|
|
|
params.update(image=data_uri)
|
|
|
|
params.update(commentAllowed=jdata.get("commentAllowed", 0))
|
2020-10-12 03:19:27 +02:00
|
|
|
|
2020-09-21 04:58:02 +02:00
|
|
|
return jsonify(params)
|
2020-11-10 04:25:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
@core_app.route("/api/v1/lnurlauth", methods=["POST"])
|
|
|
|
@api_check_wallet_key("admin")
|
|
|
|
@api_validate_post_request(
|
|
|
|
schema={
|
|
|
|
"callback": {"type": "string", "required": True},
|
|
|
|
}
|
|
|
|
)
|
|
|
|
async def api_perform_lnurlauth():
|
2020-11-11 03:01:55 +01:00
|
|
|
err = await perform_lnurlauth(g.data["callback"])
|
|
|
|
if err:
|
|
|
|
return jsonify({"reason": err.reason}), HTTPStatus.SERVICE_UNAVAILABLE
|
|
|
|
return "", HTTPStatus.OK
|