lnbits-legend/lnbits/core/views/generic.py

314 lines
11 KiB
Python
Raw Normal View History

import asyncio
from http import HTTPStatus
from typing import Optional
from fastapi import Request, status
2021-08-29 19:38:42 +02:00
from fastapi.exceptions import HTTPException
from fastapi.params import Depends, Query
from fastapi.responses import FileResponse, RedirectResponse
from fastapi.routing import APIRouter
2022-07-16 14:23:03 +02:00
from loguru import logger
from pydantic.types import UUID4
from starlette.responses import HTMLResponse, JSONResponse
from lnbits.core import db
2021-09-11 20:44:22 +02:00
from lnbits.core.models import User
from lnbits.decorators import check_user_exists
2021-10-25 20:26:21 +02:00
from lnbits.helpers import template_renderer, url_for
2022-01-31 17:29:42 +01:00
from lnbits.settings import (
LNBITS_ADMIN_USERS,
2022-02-17 22:01:14 +01:00
LNBITS_ALLOWED_USERS,
LNBITS_CUSTOM_LOGO,
2022-01-31 17:29:42 +01:00
LNBITS_SITE_TITLE,
SERVICE_FEE,
)
2021-10-17 19:33:29 +02:00
from ...helpers import get_valid_extensions
2021-10-17 19:33:29 +02:00
from ..crud import (
create_account,
create_wallet,
delete_wallet,
get_balance_check,
get_user,
save_balance_notify,
update_user_extension,
)
from ..services import pay_invoice, redeem_lnurl_withdraw
core_html_routes: APIRouter = APIRouter(tags=["Core NON-API Website Routes"])
2021-10-17 19:33:29 +02:00
@core_html_routes.get("/favicon.ico", response_class=FileResponse)
async def favicon():
return FileResponse("lnbits/core/static/favicon.ico")
2021-10-17 19:33:29 +02:00
@core_html_routes.get("/", response_class=HTMLResponse)
2021-08-21 02:55:07 +02:00
async def home(request: Request, lightning: str = None):
2021-10-17 19:33:29 +02:00
return template_renderer().TemplateResponse(
"core/index.html", {"request": request, "lnurl": lightning}
)
@core_html_routes.get(
2022-01-30 20:43:30 +01:00
"/extensions", name="core.extensions", response_class=HTMLResponse
)
2021-09-11 20:44:22 +02:00
async def extensions(
2021-10-17 19:33:29 +02:00
request: Request,
2022-07-25 13:30:45 +02:00
user: User = Depends(check_user_exists), # type: ignore
enable: str = Query(None), # type: ignore
disable: str = Query(None), # type: ignore
2021-10-17 19:33:29 +02:00
):
2021-08-23 00:05:39 +02:00
extension_to_enable = enable
extension_to_disable = disable
if extension_to_enable and extension_to_disable:
2021-10-17 19:33:29 +02:00
raise HTTPException(
HTTPStatus.BAD_REQUEST, "You can either `enable` or `disable` an extension."
)
# check if extension exists
if extension_to_enable or extension_to_disable:
ext = extension_to_enable or extension_to_disable
if ext not in [e.code for e in get_valid_extensions()]:
raise HTTPException(
HTTPStatus.BAD_REQUEST, f"Extension '{ext}' doesn't exist."
)
if extension_to_enable:
logger.info(f"Enabling extension: {extension_to_enable} for user {user.id}")
await update_user_extension(
2021-09-11 20:44:22 +02:00
user_id=user.id, extension=extension_to_enable, active=True
)
elif extension_to_disable:
logger.info(f"Disabling extension: {extension_to_disable} for user {user.id}")
await update_user_extension(
2021-09-11 20:44:22 +02:00
user_id=user.id, extension=extension_to_disable, active=False
)
2021-09-11 20:44:22 +02:00
# Update user as his extensions have been updated
if extension_to_enable or extension_to_disable:
2022-07-25 13:30:45 +02:00
user = await get_user(user.id) # type: ignore
2021-09-11 20:44:22 +02:00
2021-10-17 19:33:29 +02:00
return template_renderer().TemplateResponse(
"core/extensions.html", {"request": request, "user": user.dict()}
)
@core_html_routes.get(
"/wallet",
response_class=HTMLResponse,
description="""
Args:
just **wallet_name**: create a new user, then create a new wallet for user with wallet_name<br>
just **user_id**: return the first user wallet or create one if none found (with default wallet_name)<br>
**user_id** and **wallet_name**: create a new wallet for user with wallet_name<br>
**user_id** and **wallet_id**: return that wallet if user is the owner<br>
nothing: create everything<br>
""",
)
2021-10-17 19:33:29 +02:00
async def wallet(
2022-07-25 13:30:45 +02:00
request: Request = Query(None), # type: ignore
nme: Optional[str] = Query(None), # type: ignore
usr: Optional[UUID4] = Query(None), # type: ignore
wal: Optional[UUID4] = Query(None), # type: ignore
2021-10-17 19:33:29 +02:00
):
user_id = usr.hex if usr else None
wallet_id = wal.hex if wal else None
2021-08-21 20:04:10 +02:00
wallet_name = nme
service_fee = int(SERVICE_FEE) if int(SERVICE_FEE) == SERVICE_FEE else SERVICE_FEE
if not user_id:
user = await get_user((await create_account()).id)
logger.info(f"Create user {user.id}") # type: ignore
else:
user = await get_user(user_id)
if not user:
2021-10-17 19:33:29 +02:00
return template_renderer().TemplateResponse(
"error.html", {"request": request, "err": "User does not exist."}
)
if LNBITS_ALLOWED_USERS and user_id not in LNBITS_ALLOWED_USERS:
2021-10-17 19:33:29 +02:00
return template_renderer().TemplateResponse(
"error.html", {"request": request, "err": "User not authorized."}
)
if LNBITS_ADMIN_USERS and user_id in LNBITS_ADMIN_USERS:
user.admin = True
if not wallet_id:
if user.wallets and not wallet_name: # type: ignore
wallet = user.wallets[0] # type: ignore
else:
wallet = await create_wallet(user_id=user.id, wallet_name=wallet_name) # type: ignore
logger.info(
f"Created new wallet {wallet_name if wallet_name else '(no name)'} for user {user.id}" # type: ignore
)
2021-10-17 19:33:29 +02:00
return RedirectResponse(
f"/wallet?usr={user.id}&wal={wallet.id}", # type: ignore
2021-10-17 19:33:29 +02:00
status_code=status.HTTP_307_TEMPORARY_REDIRECT,
)
logger.debug(
f"Access {'user '+ user.id + ' ' if user else ''} {'wallet ' + wallet_name if wallet_name else ''}"
)
userwallet = user.get_wallet(wallet_id) # type: ignore
2022-07-25 13:27:31 +02:00
if not userwallet:
2021-10-17 19:33:29 +02:00
return template_renderer().TemplateResponse(
"error.html", {"request": request, "err": "Wallet not found"}
)
return template_renderer().TemplateResponse(
2021-10-17 19:33:29 +02:00
"core/wallet.html",
{
"request": request,
"user": user.dict(), # type: ignore
2022-07-25 13:27:31 +02:00
"wallet": userwallet.dict(),
2021-10-17 19:33:29 +02:00
"service_fee": service_fee,
"web_manifest": f"/manifest/{user.id}.webmanifest", # type: ignore
2021-10-17 19:33:29 +02:00
},
)
@core_html_routes.get("/withdraw", response_class=JSONResponse)
async def lnurl_full_withdraw(request: Request):
user = await get_user(request.query_params.get("usr"))
2021-04-17 23:27:15 +02:00
if not user:
2021-08-20 22:31:01 +02:00
return {"status": "ERROR", "reason": "User does not exist."}
2021-04-17 23:27:15 +02:00
wallet = user.get_wallet(request.query_params.get("wal"))
2021-04-17 23:27:15 +02:00
if not wallet:
2021-10-17 19:33:29 +02:00
return {"status": "ERROR", "reason": "Wallet does not exist."}
2021-04-17 23:27:15 +02:00
2021-08-20 22:31:01 +02:00
return {
2021-10-17 19:33:29 +02:00
"tag": "withdrawRequest",
"callback": url_for("/withdraw/cb", external=True, usr=user.id, wal=wallet.id),
"k1": "0",
"minWithdrawable": 1000 if wallet.withdrawable_balance else 0,
"maxWithdrawable": wallet.withdrawable_balance,
"defaultDescription": f"{LNBITS_SITE_TITLE} balance withdraw from {wallet.id[0:5]}",
"balanceCheck": url_for("/withdraw", external=True, usr=user.id, wal=wallet.id),
}
2021-04-17 23:27:15 +02:00
@core_html_routes.get("/withdraw/cb", response_class=JSONResponse)
async def lnurl_full_withdraw_callback(request: Request):
user = await get_user(request.query_params.get("usr"))
2021-04-17 23:27:15 +02:00
if not user:
2021-08-20 22:31:01 +02:00
return {"status": "ERROR", "reason": "User does not exist."}
2021-04-17 23:27:15 +02:00
wallet = user.get_wallet(request.query_params.get("wal"))
2021-04-17 23:27:15 +02:00
if not wallet:
2021-08-20 22:31:01 +02:00
return {"status": "ERROR", "reason": "Wallet does not exist."}
2021-04-17 23:27:15 +02:00
pr = request.query_params.get("pr")
2021-04-17 23:27:15 +02:00
async def pay():
try:
await pay_invoice(wallet_id=wallet.id, payment_request=pr)
except:
pass
2021-04-17 23:27:15 +02:00
asyncio.create_task(pay())
2021-04-17 23:27:15 +02:00
balance_notify = request.query_params.get("balanceNotify")
2021-04-17 23:27:15 +02:00
if balance_notify:
await save_balance_notify(wallet.id, balance_notify)
2021-08-20 22:31:01 +02:00
return {"status": "OK"}
2021-04-17 23:27:15 +02:00
@core_html_routes.get("/deletewallet", response_class=RedirectResponse)
2022-07-26 09:38:31 +02:00
async def deletewallet(request: Request, wal: str = Query(...), usr: str = Query(...)): # type: ignore
2021-10-25 20:26:21 +02:00
user = await get_user(usr)
user_wallet_ids = [u.id for u in user.wallets] # type: ignore
2021-10-25 20:26:21 +02:00
if wal not in user_wallet_ids:
2021-08-29 19:38:42 +02:00
raise HTTPException(HTTPStatus.FORBIDDEN, "Not your wallet.")
else:
await delete_wallet(user_id=user.id, wallet_id=wal) # type: ignore
2021-10-25 20:26:21 +02:00
user_wallet_ids.remove(wal)
logger.debug("Deleted wallet {wal} of user {user.id}")
if user_wallet_ids:
2021-10-17 19:33:29 +02:00
return RedirectResponse(
url_for("/wallet", usr=user.id, wal=user_wallet_ids[0]), # type: ignore
2021-10-17 19:33:29 +02:00
status_code=status.HTTP_307_TEMPORARY_REDIRECT,
)
2021-10-17 19:33:29 +02:00
return RedirectResponse(
url_for("/"), status_code=status.HTTP_307_TEMPORARY_REDIRECT
)
@core_html_routes.get("/withdraw/notify/{service}")
async def lnurl_balance_notify(request: Request, service: str):
bc = await get_balance_check(request.query_params.get("wal"), service)
2021-04-17 23:27:15 +02:00
if bc:
2022-07-19 18:51:35 +02:00
await redeem_lnurl_withdraw(bc.wallet, bc.url)
2021-04-17 23:27:15 +02:00
2022-06-01 14:53:05 +02:00
@core_html_routes.get(
"/lnurlwallet", response_class=RedirectResponse, name="core.lnurlwallet"
)
async def lnurlwallet(request: Request):
async with db.connect() as conn:
account = await create_account(conn=conn)
user = await get_user(account.id, conn=conn)
wallet = await create_wallet(user_id=user.id, conn=conn) # type: ignore
asyncio.create_task(
redeem_lnurl_withdraw(
2021-08-29 19:38:42 +02:00
wallet.id,
request.query_params.get("lightning"),
2021-08-29 19:38:42 +02:00
"LNbits initial funding: voucher redeem.",
{"tag": "lnurlwallet"},
2021-10-17 19:33:29 +02:00
5, # wait 5 seconds before sending the invoice to the service
)
)
2021-10-17 19:33:29 +02:00
return RedirectResponse(
f"/wallet?usr={user.id}&wal={wallet.id}", # type: ignore
2021-10-17 19:33:29 +02:00
status_code=status.HTTP_307_TEMPORARY_REDIRECT,
)
@core_html_routes.get("/service-worker.js", response_class=FileResponse)
async def service_worker():
return FileResponse("lnbits/core/static/js/service-worker.js")
2022-07-05 17:14:24 +02:00
@core_html_routes.get("/manifest/{usr}.webmanifest")
async def manifest(usr: str):
user = await get_user(usr)
if not user:
2021-09-11 11:02:48 +02:00
raise HTTPException(status_code=HTTPStatus.NOT_FOUND)
2021-08-20 22:31:01 +02:00
return {
"short_name": LNBITS_SITE_TITLE,
"name": LNBITS_SITE_TITLE + " Wallet",
2021-10-17 19:33:29 +02:00
"icons": [
{
2022-07-05 17:14:24 +02:00
"src": LNBITS_CUSTOM_LOGO
if LNBITS_CUSTOM_LOGO
else "https://cdn.jsdelivr.net/gh/lnbits/lnbits@0.3.0/docs/logos/lnbits.png",
2021-10-17 19:33:29 +02:00
"type": "image/png",
"sizes": "900x900",
}
],
"start_url": "/wallet?usr=" + usr + "&wal=" + user.wallets[0].id,
"background_color": "#1F2234",
"description": "Bitcoin Lightning Wallet",
2021-10-17 19:33:29 +02:00
"display": "standalone",
"scope": "/",
"theme_color": "#1F2234",
2021-10-17 19:33:29 +02:00
"shortcuts": [
{
"name": wallet.name,
"short_name": wallet.name,
"description": wallet.name,
"url": "/wallet?usr=" + usr + "&wal=" + wallet.id,
}
for wallet in user.wallets
],
}