2021-08-22 20:07:24 +02:00
|
|
|
from fastapi.param_functions import Depends
|
|
|
|
from lnbits.auth_bearer import AuthBearer
|
2021-08-21 12:14:23 +02:00
|
|
|
from pydantic import BaseModel
|
2021-06-21 02:11:12 +02:00
|
|
|
import trio
|
2020-10-06 05:39:54 +02:00
|
|
|
import json
|
2020-10-12 03:19:27 +02:00
|
|
|
import httpx
|
2021-07-31 02:01:19 +02:00
|
|
|
import hashlib
|
2020-10-12 23:15:27 +02:00
|
|
|
from urllib.parse import urlparse, urlunparse, urlencode, parse_qs, ParseResult
|
2021-08-22 20:07:24 +02:00
|
|
|
from quart import current_app, make_response, url_for
|
2021-08-16 20:27:39 +02:00
|
|
|
|
2021-08-20 20:54:59 +02:00
|
|
|
from fastapi import Query
|
2021-08-16 20:27:39 +02:00
|
|
|
|
2020-05-03 15:57:05 +02:00
|
|
|
from http import HTTPStatus
|
2020-06-08 00:46:16 +02:00
|
|
|
from binascii import unhexlify
|
2021-08-20 20:54:59 +02:00
|
|
|
from typing import Dict, List, Optional, Union
|
2020-03-04 23:11:15 +01:00
|
|
|
|
2021-07-31 02:01:19 +02:00
|
|
|
from lnbits import bolt11, lnurl
|
2020-04-16 17:27:36 +02:00
|
|
|
from lnbits.decorators import api_check_wallet_key, api_validate_post_request
|
2021-06-11 17:48:13 +02:00
|
|
|
from lnbits.utils.exchange_rates import currencies, fiat_amount_as_satoshis
|
2021-08-22 20:07:24 +02:00
|
|
|
from lnbits.requestvars import g
|
2020-03-04 23:11:15 +01:00
|
|
|
|
2020-11-21 22:04:39 +01:00
|
|
|
from .. import core_app, db
|
2021-08-06 12:15:07 +02:00
|
|
|
from ..crud import get_payments, save_balance_check, update_wallet
|
2021-04-10 22:37:48 +02:00
|
|
|
from ..services import (
|
|
|
|
PaymentFailure,
|
|
|
|
InvoiceFailure,
|
|
|
|
create_invoice,
|
|
|
|
pay_invoice,
|
|
|
|
perform_lnurlauth,
|
|
|
|
)
|
2021-06-02 22:04:56 +02:00
|
|
|
from ..tasks import api_invoice_listeners
|
2020-10-06 05:39:54 +02:00
|
|
|
|
2020-03-04 23:11:15 +01:00
|
|
|
|
2021-08-22 20:07:24 +02:00
|
|
|
@core_app.get(
|
|
|
|
"/api/v1/wallet",
|
|
|
|
# dependencies=[Depends(AuthBearer())]
|
|
|
|
)
|
|
|
|
# @api_check_wallet_key("invoice")
|
2020-09-29 20:43:11 +02:00
|
|
|
async def api_wallet():
|
|
|
|
return (
|
2021-08-22 20:07:24 +02:00
|
|
|
{"id": g().wallet.id, "name": g().wallet.name, "balance": g().wallet.balance_msat},
|
2020-09-29 20:43:11 +02:00
|
|
|
HTTPStatus.OK,
|
|
|
|
)
|
|
|
|
|
2021-08-16 20:27:39 +02:00
|
|
|
|
2021-08-23 00:05:39 +02:00
|
|
|
@core_app.put("/api/v1/wallet/{new_name}")
|
2021-08-06 12:15:07 +02:00
|
|
|
@api_check_wallet_key("invoice")
|
2021-08-16 20:27:39 +02:00
|
|
|
async def api_update_wallet(new_name: str):
|
2021-08-22 20:07:24 +02:00
|
|
|
await update_wallet(g().wallet.id, new_name)
|
2021-08-06 12:15:07 +02:00
|
|
|
return (
|
|
|
|
{
|
2021-08-22 20:07:24 +02:00
|
|
|
"id": g().wallet.id,
|
|
|
|
"name": g().wallet.name,
|
|
|
|
"balance": g().wallet.balance_msat,
|
2021-08-21 01:39:31 +02:00
|
|
|
},
|
2021-08-06 12:15:07 +02:00
|
|
|
HTTPStatus.OK,
|
|
|
|
)
|
|
|
|
|
2020-09-29 20:43:11 +02:00
|
|
|
|
2021-08-16 20:27:39 +02:00
|
|
|
@core_app.get("/api/v1/payments")
|
2021-03-21 21:10:53 +01:00
|
|
|
@api_check_wallet_key("invoice")
|
|
|
|
async def api_payments():
|
2021-08-16 20:27:39 +02:00
|
|
|
return (
|
2021-08-22 20:07:24 +02:00
|
|
|
await get_payments(wallet_id=g().wallet.id, pending=True, complete=True),
|
2021-08-16 20:27:39 +02:00
|
|
|
HTTPStatus.OK,
|
|
|
|
)
|
2020-03-07 22:27:00 +01:00
|
|
|
|
2021-08-20 22:31:01 +02:00
|
|
|
class CreateInvoiceData(BaseModel):
|
|
|
|
amount: int = Query(None, ge=1)
|
|
|
|
memo: str = None
|
|
|
|
unit: Optional[str] = None
|
|
|
|
description_hash: str = None
|
|
|
|
lnurl_callback: Optional[str] = None
|
|
|
|
lnurl_balance_check: Optional[str] = None
|
|
|
|
extra: Optional[dict] = None
|
|
|
|
webhook: Optional[str] = None
|
2020-03-07 22:27:00 +01:00
|
|
|
|
2020-04-16 17:27:36 +02:00
|
|
|
@api_check_wallet_key("invoice")
|
2021-08-18 22:40:50 +02:00
|
|
|
# async def api_payments_create_invoice(amount: List[str] = Query([type: str = Query(None)])):
|
2021-08-20 22:31:01 +02:00
|
|
|
async def api_payments_create_invoice(data: CreateInvoiceData):
|
|
|
|
if "description_hash" in data:
|
|
|
|
description_hash = unhexlify(data.description_hash)
|
2020-06-08 00:46:16 +02:00
|
|
|
memo = ""
|
|
|
|
else:
|
|
|
|
description_hash = b""
|
2021-08-20 22:31:01 +02:00
|
|
|
memo = data.memo
|
2020-06-08 00:46:16 +02:00
|
|
|
|
2021-08-20 22:31:01 +02:00
|
|
|
if data.unit or "sat" == "sat":
|
|
|
|
amount = data.amount
|
2021-06-19 14:12:16 +02:00
|
|
|
else:
|
2021-08-20 22:31:01 +02:00
|
|
|
price_in_sats = await fiat_amount_as_satoshis(data.amount, data.unit)
|
2021-06-19 14:12:16 +02:00
|
|
|
amount = price_in_sats
|
2021-06-11 17:48:13 +02:00
|
|
|
|
2021-03-26 23:10:30 +01:00
|
|
|
async with db.connect() as conn:
|
2021-04-10 22:37:48 +02:00
|
|
|
try:
|
|
|
|
payment_hash, payment_request = await create_invoice(
|
2021-08-22 20:07:24 +02:00
|
|
|
wallet_id=g().wallet.id,
|
2021-06-19 14:12:16 +02:00
|
|
|
amount=amount,
|
2021-04-10 22:37:48 +02:00
|
|
|
memo=memo,
|
|
|
|
description_hash=description_hash,
|
2021-08-20 22:31:01 +02:00
|
|
|
extra=data.extra,
|
|
|
|
webhook=data.webhook,
|
2021-04-10 22:37:48 +02:00
|
|
|
conn=conn,
|
|
|
|
)
|
|
|
|
except InvoiceFailure as e:
|
2021-08-20 22:31:01 +02:00
|
|
|
return {"message": str(e)}, 520
|
2021-04-10 22:37:48 +02:00
|
|
|
except Exception as exc:
|
|
|
|
raise exc
|
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
|
2021-08-20 22:31:01 +02:00
|
|
|
if data.lnurl_callback:
|
2021-08-22 20:07:24 +02:00
|
|
|
if "lnurl_balance_check" in g().data:
|
|
|
|
save_balance_check(g().wallet.id, data.lnurl_balance_check)
|
2021-04-17 23:27:15 +02:00
|
|
|
|
2020-10-15 17:58:37 +02:00
|
|
|
async with httpx.AsyncClient() as client:
|
|
|
|
try:
|
2020-12-31 18:50:16 +01:00
|
|
|
r = await client.get(
|
2021-08-20 22:31:01 +02:00
|
|
|
data.lnurl_callback,
|
2021-04-17 23:27:15 +02:00
|
|
|
params={
|
|
|
|
"pr": payment_request,
|
|
|
|
"balanceNotify": url_for(
|
|
|
|
"core.lnurl_balance_notify",
|
2021-08-20 22:31:01 +02:00
|
|
|
service=urlparse(data.lnurl_callback).netloc,
|
2021-08-22 20:07:24 +02:00
|
|
|
wal=g().wallet.id,
|
2021-04-17 23:27:15 +02:00
|
|
|
_external=True,
|
|
|
|
),
|
|
|
|
},
|
2020-12-31 18:50:16 +01:00
|
|
|
timeout=10,
|
|
|
|
)
|
2020-10-15 17:58:37 +02:00
|
|
|
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 (
|
|
|
|
{
|
|
|
|
"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,
|
2021-08-20 22:31:01 +02:00
|
|
|
},
|
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")
|
2021-08-16 21:30:11 +02:00
|
|
|
async def api_payments_pay_invoice(
|
|
|
|
bolt11: str = Query(...), wallet: Optional[List[str]] = Query(None)
|
|
|
|
):
|
2020-03-07 22:27:00 +01:00
|
|
|
try:
|
2021-03-24 04:40:32 +01:00
|
|
|
payment_hash = await pay_invoice(
|
2021-08-16 21:30:11 +02:00
|
|
|
wallet_id=wallet.id,
|
|
|
|
payment_request=bolt11,
|
2021-03-24 04:40:32 +01:00
|
|
|
)
|
2020-04-16 17:10:53 +02:00
|
|
|
except ValueError as e:
|
2021-08-20 22:31:01 +02:00
|
|
|
return {"message": str(e)}, HTTPStatus.BAD_REQUEST
|
2020-04-16 17:10:53 +02:00
|
|
|
except PermissionError as e:
|
2021-08-20 22:31:01 +02:00
|
|
|
return {"message": str(e)}, HTTPStatus.FORBIDDEN
|
2021-04-06 19:57:51 +02:00
|
|
|
except PaymentFailure as e:
|
2021-08-20 22:31:01 +02:00
|
|
|
return {"message": str(e)}, 520
|
2020-10-08 21:03:18 +02:00
|
|
|
except Exception as exc:
|
2020-11-21 22:04:39 +01:00
|
|
|
raise exc
|
2020-03-07 22:27:00 +01:00
|
|
|
|
2020-09-01 03:12:46 +02:00
|
|
|
return (
|
|
|
|
{
|
|
|
|
"payment_hash": payment_hash,
|
|
|
|
# maintain backwards compatibility with API clients:
|
|
|
|
"checking_id": payment_hash,
|
2021-08-20 22:31:01 +02:00
|
|
|
},
|
2020-09-01 03:12:46 +02:00
|
|
|
HTTPStatus.CREATED,
|
|
|
|
)
|
2020-03-07 22:27:00 +01:00
|
|
|
|
|
|
|
|
2021-08-20 22:31:01 +02:00
|
|
|
@core_app.post("/api/v1/payments")
|
|
|
|
async def api_payments_create(out: bool = True):
|
|
|
|
if 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
|
|
|
|
2021-08-20 22:31:01 +02:00
|
|
|
class CreateLNURLData(BaseModel):
|
|
|
|
description_hash: str
|
|
|
|
callback: str
|
|
|
|
amount: int
|
|
|
|
comment: Optional[str] = None
|
|
|
|
description: Optional[str] = None
|
2020-03-07 22:27:00 +01:00
|
|
|
|
2021-08-20 22:31:01 +02:00
|
|
|
@core_app.post("/api/v1/payments/lnurl")
|
2020-10-12 23:15:27 +02:00
|
|
|
@api_check_wallet_key("admin")
|
2021-08-20 22:31:01 +02:00
|
|
|
async def api_payments_pay_lnurl(data: CreateLNURLData):
|
|
|
|
domain = urlparse(data.callback).netloc
|
2020-10-15 03:44:23 +02:00
|
|
|
|
2020-10-15 17:58:37 +02:00
|
|
|
async with httpx.AsyncClient() as client:
|
|
|
|
try:
|
|
|
|
r = await client.get(
|
2021-08-20 22:31:01 +02:00
|
|
|
data.callback,
|
|
|
|
params={"amount": data.amount, "comment": data.comment},
|
2020-12-31 18:50:16 +01:00
|
|
|
timeout=40,
|
2020-10-15 17:58:37 +02:00
|
|
|
)
|
|
|
|
if r.is_error:
|
2021-07-31 02:01:19 +02:00
|
|
|
raise httpx.ConnectError
|
2020-10-15 17:58:37 +02:00
|
|
|
except (httpx.ConnectError, httpx.RequestError):
|
2021-07-31 02:01:19 +02:00
|
|
|
return (
|
2021-08-20 22:31:01 +02:00
|
|
|
{"message": f"Failed to connect to {domain}."},
|
2021-07-31 02:01:19 +02:00
|
|
|
HTTPStatus.BAD_REQUEST,
|
|
|
|
)
|
2020-10-12 23:15:27 +02:00
|
|
|
|
|
|
|
params = json.loads(r.text)
|
|
|
|
if params.get("status") == "ERROR":
|
2021-08-20 22:31:01 +02:00
|
|
|
return ({"message": f"{domain} said: '{params.get('reason', '')}'"},
|
2021-03-24 04:40:32 +01:00
|
|
|
HTTPStatus.BAD_REQUEST,
|
|
|
|
)
|
2020-10-12 23:15:27 +02:00
|
|
|
|
|
|
|
invoice = bolt11.decode(params["pr"])
|
2021-08-20 22:31:01 +02:00
|
|
|
if invoice.amount_msat != data.amount:
|
2020-10-12 23:15:27 +02:00
|
|
|
return (
|
|
|
|
{
|
2021-08-22 20:07:24 +02:00
|
|
|
"message": f"{domain} returned an invalid invoice. Expected {g().data['amount']} msat, got {invoice.amount_msat}."
|
2021-08-20 22:31:01 +02:00
|
|
|
},
|
2020-10-12 23:15:27 +02:00
|
|
|
HTTPStatus.BAD_REQUEST,
|
|
|
|
)
|
2021-08-22 20:07:24 +02:00
|
|
|
if invoice.description_hash != g().data["description_hash"]:
|
2020-10-12 23:15:27 +02:00
|
|
|
return (
|
|
|
|
{
|
2021-08-22 20:07:24 +02:00
|
|
|
"message": f"{domain} returned an invalid invoice. Expected description_hash == {g().data['description_hash']}, got {invoice.description_hash}."
|
2021-08-20 22:31:01 +02:00
|
|
|
},
|
2020-10-12 23:15:27 +02:00
|
|
|
HTTPStatus.BAD_REQUEST,
|
|
|
|
)
|
|
|
|
|
2021-03-26 23:10:30 +01:00
|
|
|
extra = {}
|
2020-10-21 14:50:45 +02:00
|
|
|
|
2021-03-26 23:10:30 +01:00
|
|
|
if params.get("successAction"):
|
|
|
|
extra["success_action"] = params["successAction"]
|
2021-08-20 22:31:01 +02:00
|
|
|
if data.comment:
|
|
|
|
extra["comment"] = data.comment
|
2020-10-21 14:50:45 +02:00
|
|
|
|
2021-03-26 23:10:30 +01:00
|
|
|
payment_hash = await pay_invoice(
|
2021-08-22 20:07:24 +02:00
|
|
|
wallet_id=g().wallet.id,
|
2021-03-26 23:10:30 +01:00
|
|
|
payment_request=params["pr"],
|
2021-08-20 22:31:01 +02:00
|
|
|
description=data.description,
|
2021-03-26 23:10:30 +01:00
|
|
|
extra=extra,
|
|
|
|
)
|
2020-10-12 23:15:27 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
{
|
|
|
|
"success_action": params.get("successAction"),
|
|
|
|
"payment_hash": payment_hash,
|
|
|
|
# maintain backwards compatibility with API clients:
|
|
|
|
"checking_id": payment_hash,
|
2021-08-20 22:31:01 +02:00
|
|
|
},
|
2020-10-12 23:15:27 +02:00
|
|
|
HTTPStatus.CREATED,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-08-23 00:05:39 +02:00
|
|
|
@core_app.get("/api/v1/payments/{payment_hash}")
|
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):
|
2021-08-22 20:07:24 +02:00
|
|
|
payment = await g().wallet.get_payment(payment_hash)
|
2020-03-07 22:27:00 +01:00
|
|
|
|
|
|
|
if not payment:
|
2021-08-20 22:31:01 +02:00
|
|
|
return {"message": "Payment does not exist."}, HTTPStatus.NOT_FOUND
|
2020-03-07 22:27:00 +01:00
|
|
|
elif not payment.pending:
|
2021-08-20 22:31:01 +02:00
|
|
|
return {"paid": True, "preimage": payment.preimage}, HTTPStatus.OK
|
2020-03-04 23:11:15 +01:00
|
|
|
|
|
|
|
try:
|
2020-11-21 22:04:39 +01:00
|
|
|
await payment.check_pending()
|
2020-03-04 23:11:15 +01:00
|
|
|
except Exception:
|
2021-08-20 22:31:01 +02:00
|
|
|
return {"paid": False}, HTTPStatus.OK
|
2020-03-04 23:11:15 +01:00
|
|
|
|
2021-03-24 04:40:32 +01:00
|
|
|
return (
|
2021-08-20 22:31:01 +02:00
|
|
|
{"paid": not payment.pending, "preimage": payment.preimage},
|
2021-03-24 04:40:32 +01:00
|
|
|
HTTPStatus.OK,
|
|
|
|
)
|
2020-10-06 05:39:54 +02:00
|
|
|
|
|
|
|
|
2021-08-20 22:31:01 +02:00
|
|
|
@core_app.get("/api/v1/payments/sse")
|
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():
|
2021-08-22 20:07:24 +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)
|
2021-06-02 22:04:56 +02:00
|
|
|
api_invoice_listeners.append(send_payment)
|
2020-10-06 05:39:54 +02:00
|
|
|
|
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)
|
|
|
|
|
2021-05-06 17:41:44 +02:00
|
|
|
current_app.nursery.start_soon(payment_received)
|
|
|
|
current_app.nursery.start_soon(repeat_keepalive)
|
2020-10-06 05:39:54 +02:00
|
|
|
|
|
|
|
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:
|
2021-08-12 03:53:11 +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
|
|
|
|
|
|
|
|
2021-08-23 00:05:39 +02:00
|
|
|
@core_app.get("/api/v1/lnurlscan/{code}")
|
2020-09-21 04:58:02 +02:00
|
|
|
@api_check_wallet_key("invoice")
|
|
|
|
async def api_lnurlscan(code: str):
|
|
|
|
try:
|
2021-07-31 02:01:19 +02:00
|
|
|
url = lnurl.decode(code)
|
|
|
|
domain = urlparse(url).netloc
|
|
|
|
except:
|
|
|
|
# parse internet identifier (user@domain.com)
|
|
|
|
name_domain = code.split("@")
|
|
|
|
if len(name_domain) == 2 and len(name_domain[1].split(".")) == 2:
|
|
|
|
name, domain = name_domain
|
|
|
|
url = (
|
|
|
|
("http://" if domain.endswith(".onion") else "https://")
|
|
|
|
+ domain
|
|
|
|
+ "/.well-known/lnurlp/"
|
|
|
|
+ name
|
|
|
|
)
|
|
|
|
# will proceed with these values
|
|
|
|
else:
|
2021-08-20 22:31:01 +02:00
|
|
|
return {"message": "invalid lnurl"}, HTTPStatus.BAD_REQUEST
|
2020-11-10 04:25:46 +01:00
|
|
|
|
|
|
|
# params is what will be returned to the client
|
|
|
|
params: Dict = {"domain": domain}
|
|
|
|
|
2021-07-31 02:01:19 +02:00
|
|
|
if "tag=login" in url:
|
2020-11-10 04:25:46 +01:00
|
|
|
params.update(kind="auth")
|
2021-07-31 02:01:19 +02:00
|
|
|
params.update(callback=url) # with k1 already in it
|
2020-11-12 02:37:55 +01:00
|
|
|
|
2021-08-22 20:07:24 +02:00
|
|
|
lnurlauth_key = g().wallet.lnurlauth_key(domain)
|
2020-11-12 02:37:55 +01:00
|
|
|
params.update(pubkey=lnurlauth_key.verifying_key.to_string("compressed").hex())
|
2020-11-10 04:25:46 +01:00
|
|
|
else:
|
|
|
|
async with httpx.AsyncClient() as client:
|
2021-07-31 02:01:19 +02:00
|
|
|
r = await client.get(url, timeout=5)
|
2020-11-10 04:25:46 +01:00
|
|
|
if r.is_error:
|
2020-11-11 02:59:50 +01:00
|
|
|
return (
|
2021-08-20 22:31:01 +02:00
|
|
|
{"domain": domain, "message": "failed to get parameters"},
|
2020-11-11 02:59:50 +01:00
|
|
|
HTTPStatus.SERVICE_UNAVAILABLE,
|
|
|
|
)
|
2020-09-21 04:58:02 +02:00
|
|
|
|
2020-11-10 04:25:46 +01:00
|
|
|
try:
|
2021-07-31 02:01:19 +02:00
|
|
|
data = json.loads(r.text)
|
|
|
|
except json.decoder.JSONDecodeError:
|
2020-11-10 04:25:46 +01:00
|
|
|
return (
|
2021-03-24 04:40:32 +01:00
|
|
|
{
|
|
|
|
"domain": domain,
|
|
|
|
"message": f"got invalid response '{r.text[:200]}'",
|
2021-08-20 22:31:01 +02:00
|
|
|
},
|
2020-11-10 04:25:46 +01:00
|
|
|
HTTPStatus.SERVICE_UNAVAILABLE,
|
|
|
|
)
|
2020-09-21 04:58:02 +02:00
|
|
|
|
2021-07-31 02:01:19 +02:00
|
|
|
try:
|
|
|
|
tag = data["tag"]
|
|
|
|
if tag == "channelRequest":
|
|
|
|
return (
|
2021-08-20 22:31:01 +02:00
|
|
|
{"domain": domain, "kind": "channel", "message": "unsupported"},
|
2021-07-31 02:01:19 +02:00
|
|
|
HTTPStatus.BAD_REQUEST,
|
|
|
|
)
|
|
|
|
|
|
|
|
params.update(**data)
|
|
|
|
|
|
|
|
if tag == "withdrawRequest":
|
|
|
|
params.update(kind="withdraw")
|
|
|
|
params.update(fixed=data["minWithdrawable"] == data["maxWithdrawable"])
|
|
|
|
|
|
|
|
# callback with k1 already in it
|
|
|
|
parsed_callback: ParseResult = urlparse(data["callback"])
|
|
|
|
qs: Dict = parse_qs(parsed_callback.query)
|
|
|
|
qs["k1"] = data["k1"]
|
|
|
|
|
|
|
|
# balanceCheck/balanceNotify
|
|
|
|
if "balanceCheck" in data:
|
|
|
|
params.update(balanceCheck=data["balanceCheck"])
|
|
|
|
|
|
|
|
# format callback url and send to client
|
|
|
|
parsed_callback = parsed_callback._replace(
|
|
|
|
query=urlencode(qs, doseq=True)
|
|
|
|
)
|
|
|
|
params.update(callback=urlunparse(parsed_callback))
|
|
|
|
|
|
|
|
if tag == "payRequest":
|
|
|
|
params.update(kind="pay")
|
|
|
|
params.update(fixed=data["minSendable"] == data["maxSendable"])
|
|
|
|
|
|
|
|
params.update(
|
|
|
|
description_hash=hashlib.sha256(
|
|
|
|
data["metadata"].encode("utf-8")
|
|
|
|
).hexdigest()
|
|
|
|
)
|
|
|
|
metadata = json.loads(data["metadata"])
|
|
|
|
for [k, v] in metadata:
|
|
|
|
if k == "text/plain":
|
|
|
|
params.update(description=v)
|
|
|
|
if k == "image/jpeg;base64" or k == "image/png;base64":
|
|
|
|
data_uri = "data:" + k + "," + v
|
|
|
|
params.update(image=data_uri)
|
|
|
|
if k == "text/email" or k == "text/identifier":
|
|
|
|
params.update(targetUser=v)
|
|
|
|
|
|
|
|
params.update(commentAllowed=data.get("commentAllowed", 0))
|
|
|
|
except KeyError as exc:
|
2021-03-24 04:40:32 +01:00
|
|
|
return (
|
2021-07-31 02:01:19 +02:00
|
|
|
{
|
|
|
|
"domain": domain,
|
|
|
|
"message": f"lnurl JSON response invalid: {exc}",
|
2021-08-20 22:31:01 +02:00
|
|
|
},
|
2021-07-31 02:01:19 +02:00
|
|
|
HTTPStatus.SERVICE_UNAVAILABLE,
|
2021-03-24 04:40:32 +01:00
|
|
|
)
|
2021-08-20 22:31:01 +02:00
|
|
|
return params
|
2020-11-10 04:25:46 +01:00
|
|
|
|
|
|
|
|
2021-08-21 12:14:23 +02:00
|
|
|
@core_app.post("/api/v1/lnurlauth")
|
2020-11-10 04:25:46 +01:00
|
|
|
@api_check_wallet_key("admin")
|
2021-08-20 22:31:01 +02:00
|
|
|
async def api_perform_lnurlauth(callback: str):
|
|
|
|
err = await perform_lnurlauth(callback)
|
2020-11-11 03:01:55 +01:00
|
|
|
if err:
|
2021-08-20 22:31:01 +02:00
|
|
|
return {"reason": err.reason}, HTTPStatus.SERVICE_UNAVAILABLE
|
2020-11-11 03:01:55 +01:00
|
|
|
return "", HTTPStatus.OK
|
2021-06-11 17:48:13 +02:00
|
|
|
|
2021-06-19 14:12:16 +02:00
|
|
|
|
2021-06-11 17:48:13 +02:00
|
|
|
@core_app.route("/api/v1/currencies", methods=["GET"])
|
|
|
|
async def api_list_currencies_available():
|
2021-08-20 22:31:01 +02:00
|
|
|
return list(currencies.keys())
|