2020-09-29 23:24:08 +02:00
|
|
|
from http import HTTPStatus
|
2021-08-22 20:07:24 +02:00
|
|
|
from typing import Optional
|
|
|
|
|
2021-08-27 20:54:42 +02:00
|
|
|
from fastapi import Request, status
|
2021-08-29 19:38:42 +02:00
|
|
|
from fastapi.exceptions import HTTPException
|
2021-08-27 20:54:42 +02:00
|
|
|
from fastapi.param_functions import Body
|
|
|
|
from fastapi.params import Depends, Query
|
|
|
|
from fastapi.responses import FileResponse, RedirectResponse
|
|
|
|
from fastapi.routing import APIRouter
|
|
|
|
from pydantic.types import UUID4
|
2021-08-22 20:07:24 +02:00
|
|
|
from starlette.responses import HTMLResponse
|
2021-08-29 19:38:42 +02:00
|
|
|
import trio
|
2020-01-31 21:07:05 +01:00
|
|
|
|
2021-08-27 20:54:42 +02:00
|
|
|
from lnbits.core import db
|
|
|
|
from lnbits.helpers import template_renderer, url_for
|
|
|
|
from lnbits.requestvars import g
|
|
|
|
from lnbits.settings import (LNBITS_ALLOWED_USERS, LNBITS_SITE_TITLE,
|
|
|
|
SERVICE_FEE)
|
|
|
|
|
|
|
|
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
|
2020-01-31 21:07:05 +01:00
|
|
|
|
2021-08-23 21:17:46 +02:00
|
|
|
core_html_routes: APIRouter = APIRouter(tags=["Core NON-API Website Routes"])
|
2020-01-31 21:07:05 +01:00
|
|
|
|
2021-08-23 21:17:46 +02:00
|
|
|
@core_html_routes.get("/favicon.ico")
|
2020-09-14 02:31:05 +02:00
|
|
|
async def favicon():
|
2021-08-22 20:07:24 +02:00
|
|
|
return FileResponse("lnbits/core/static/favicon.ico")
|
|
|
|
|
2020-01-31 21:07:05 +01:00
|
|
|
|
2021-08-23 21:17:46 +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-08-27 20:54:42 +02:00
|
|
|
return template_renderer().TemplateResponse("core/index.html", {"request": request, "lnurl": lightning})
|
2020-03-04 23:11:15 +01:00
|
|
|
|
|
|
|
|
2021-08-23 21:17:46 +02:00
|
|
|
@core_html_routes.get("/extensions")
|
2021-08-27 20:54:42 +02:00
|
|
|
# @validate_uuids(["usr"], required=True)
|
|
|
|
# @check_user_exists()
|
|
|
|
async def extensions(request: Request, enable: str, disable: str):
|
2021-08-23 00:05:39 +02:00
|
|
|
extension_to_enable = enable
|
|
|
|
extension_to_disable = disable
|
2020-03-04 23:11:15 +01:00
|
|
|
|
|
|
|
if extension_to_enable and extension_to_disable:
|
2021-08-29 19:38:42 +02:00
|
|
|
raise HTTPException(HTTPStatus.BAD_REQUEST, "You can either `enable` or `disable` an extension.")
|
2020-03-04 23:11:15 +01:00
|
|
|
|
|
|
|
if extension_to_enable:
|
2021-03-24 04:40:32 +01:00
|
|
|
await update_user_extension(
|
2021-06-22 04:22:52 +02:00
|
|
|
user_id=g.user.id, extension=extension_to_enable, active=True
|
2021-03-24 04:40:32 +01:00
|
|
|
)
|
2020-03-04 23:11:15 +01:00
|
|
|
elif extension_to_disable:
|
2021-03-24 04:40:32 +01:00
|
|
|
await update_user_extension(
|
2021-06-22 04:22:52 +02:00
|
|
|
user_id=g.user.id, extension=extension_to_disable, active=False
|
2021-03-24 04:40:32 +01:00
|
|
|
)
|
2021-08-27 20:54:42 +02:00
|
|
|
return template_renderer().TemplateResponse("core/extensions.html", {"request": request, "user": get_user(g.user.id)})
|
2020-03-04 23:11:15 +01:00
|
|
|
|
|
|
|
|
2021-08-24 21:23:18 +02:00
|
|
|
@core_html_routes.get("/wallet", response_class=HTMLResponse)
|
2021-08-21 20:04:10 +02:00
|
|
|
#Not sure how to validate
|
2021-08-27 20:54:42 +02:00
|
|
|
# @validate_uuids(["usr", "nme"])
|
|
|
|
async def wallet(request: Request = Query(None), nme: Optional[str] = Query(None),
|
|
|
|
usr: Optional[UUID4] = Query(None), wal: Optional[UUID4] = Query(None)):
|
|
|
|
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
|
2020-04-23 21:25:46 +02:00
|
|
|
service_fee = int(SERVICE_FEE) if int(SERVICE_FEE) == SERVICE_FEE else SERVICE_FEE
|
2020-03-04 23:11:15 +01:00
|
|
|
|
|
|
|
# just wallet_name: create a new user, then create a new wallet for user with wallet_name
|
|
|
|
# just user_id: return the first user wallet or create one if none found (with default wallet_name)
|
|
|
|
# user_id and wallet_name: create a new wallet for user with wallet_name
|
|
|
|
# user_id and wallet_id: return that wallet if user is the owner
|
|
|
|
# nothing: create everything
|
|
|
|
|
|
|
|
if not user_id:
|
2021-08-27 20:54:42 +02:00
|
|
|
user = await get_user((await create_account()).id)
|
2020-03-04 23:11:15 +01:00
|
|
|
else:
|
2021-08-27 20:54:42 +02:00
|
|
|
user = await get_user(user_id)
|
|
|
|
if not user:
|
|
|
|
return template_renderer().TemplateResponse("error.html", {"request": request, "err": "User does not exist."})
|
2020-09-05 08:00:44 +02:00
|
|
|
if LNBITS_ALLOWED_USERS and user_id not in LNBITS_ALLOWED_USERS:
|
2021-08-27 20:54:42 +02:00
|
|
|
return template_renderer().TemplateResponse("error.html", {"request": request, "err": "User not authorized."})
|
2020-03-04 23:11:15 +01:00
|
|
|
if not wallet_id:
|
2021-08-27 20:54:42 +02:00
|
|
|
if user.wallets and not wallet_name:
|
|
|
|
wallet = user.wallets[0]
|
2020-03-04 23:11:15 +01:00
|
|
|
else:
|
2021-08-27 20:54:42 +02:00
|
|
|
wallet = await create_wallet(user_id=user.id, wallet_name=wallet_name)
|
2020-03-04 23:11:15 +01:00
|
|
|
|
2021-08-27 20:54:42 +02:00
|
|
|
return RedirectResponse(f"/wallet?usr={user.id}&wal={wallet.id}", status_code=status.HTTP_307_TEMPORARY_REDIRECT)
|
2020-03-04 23:11:15 +01:00
|
|
|
|
2021-08-27 20:54:42 +02:00
|
|
|
wallet = user.get_wallet(wallet_id)
|
|
|
|
if not wallet:
|
|
|
|
return template_renderer().TemplateResponse("error.html", {"request": request, "err": "Wallet not found"})
|
2020-03-04 23:11:15 +01:00
|
|
|
|
2021-08-27 20:54:42 +02:00
|
|
|
return template_renderer().TemplateResponse(
|
|
|
|
"core/wallet.html", {"request":request,"user":user.dict(), "wallet":wallet.dict(), "service_fee":service_fee}
|
2021-03-24 04:40:32 +01:00
|
|
|
)
|
2020-03-04 23:11:15 +01:00
|
|
|
|
|
|
|
|
2021-08-23 21:17:46 +02:00
|
|
|
@core_html_routes.get("/withdraw")
|
2021-08-27 20:54:42 +02:00
|
|
|
# @validate_uuids(["usr", "wal"], required=True)
|
|
|
|
async def lnurl_full_withdraw(request: Request):
|
2021-04-17 23:27:15 +02:00
|
|
|
user = await get_user(request.args.get("usr"))
|
|
|
|
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.args.get("wal"))
|
|
|
|
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
|
|
|
|
2021-08-20 22:31:01 +02:00
|
|
|
return {
|
2021-04-17 23:27:15 +02:00
|
|
|
"tag": "withdrawRequest",
|
|
|
|
"callback": url_for(
|
2021-08-27 20:54:42 +02:00
|
|
|
"/withdraw/cb",
|
|
|
|
external=True,
|
2021-04-17 23:27:15 +02:00
|
|
|
usr=user.id,
|
|
|
|
wal=wallet.id,
|
|
|
|
),
|
|
|
|
"k1": "0",
|
2021-06-11 12:57:46 +02:00
|
|
|
"minWithdrawable": 1000 if wallet.withdrawable_balance else 0,
|
2021-04-17 23:27:15 +02:00
|
|
|
"maxWithdrawable": wallet.withdrawable_balance,
|
|
|
|
"defaultDescription": f"{LNBITS_SITE_TITLE} balance withdraw from {wallet.id[0:5]}",
|
2021-08-27 20:54:42 +02:00
|
|
|
"balanceCheck": url_for("/withdraw", external=True, usr=user.id, wal=wallet.id),
|
2021-04-17 23:27:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-23 21:17:46 +02:00
|
|
|
@core_html_routes.get("/withdraw/cb")
|
2021-08-27 20:54:42 +02:00
|
|
|
# @validate_uuids(["usr", "wal"], required=True)
|
|
|
|
async def lnurl_full_withdraw_callback(request: Request):
|
2021-04-17 23:27:15 +02:00
|
|
|
user = await get_user(request.args.get("usr"))
|
|
|
|
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.args.get("wal"))
|
|
|
|
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.args.get("pr")
|
|
|
|
|
|
|
|
async def pay():
|
2021-06-01 18:27:39 +02:00
|
|
|
try:
|
|
|
|
await pay_invoice(wallet_id=wallet.id, payment_request=pr)
|
|
|
|
except:
|
|
|
|
pass
|
2021-04-17 23:27:15 +02:00
|
|
|
|
2021-08-29 19:38:42 +02:00
|
|
|
async with trio.open_nursery() as n:
|
|
|
|
n.start_soon(pay)
|
2021-04-17 23:27:15 +02:00
|
|
|
|
|
|
|
balance_notify = request.args.get("balanceNotify")
|
|
|
|
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
|
|
|
|
|
|
|
|
2021-08-23 21:17:46 +02:00
|
|
|
@core_html_routes.get("/deletewallet")
|
2021-08-27 20:54:42 +02:00
|
|
|
# @validate_uuids(["usr", "wal"], required=True)
|
|
|
|
# @check_user_exists()
|
|
|
|
async def deletewallet(request: Request):
|
|
|
|
wallet_id = request.path_params.get("wal", type=str)
|
|
|
|
user_wallet_ids = g().user.wallet_ids
|
2020-03-04 23:11:15 +01:00
|
|
|
|
2020-03-07 22:27:00 +01:00
|
|
|
if wallet_id not in user_wallet_ids:
|
2021-08-29 19:38:42 +02:00
|
|
|
raise HTTPException(HTTPStatus.FORBIDDEN, "Not your wallet.")
|
2020-03-04 23:11:15 +01:00
|
|
|
else:
|
2021-08-27 20:54:42 +02:00
|
|
|
await delete_wallet(user_id=g().user.id, wallet_id=wallet_id)
|
2020-03-07 22:27:00 +01:00
|
|
|
user_wallet_ids.remove(wallet_id)
|
2020-03-04 23:11:15 +01:00
|
|
|
|
2020-03-07 22:27:00 +01:00
|
|
|
if user_wallet_ids:
|
2021-08-27 20:54:42 +02:00
|
|
|
return RedirectResponse(url_for("/wallet", usr=g().user.id, wal=user_wallet_ids[0]),
|
|
|
|
status_code=status.HTTP_307_TEMPORARY_REDIRECT)
|
2020-03-04 23:11:15 +01:00
|
|
|
|
2021-08-27 20:54:42 +02:00
|
|
|
return RedirectResponse(url_for("/"), status_code=status.HTTP_307_TEMPORARY_REDIRECT)
|
2020-09-29 23:24:08 +02:00
|
|
|
|
|
|
|
|
2021-08-23 21:17:46 +02:00
|
|
|
@core_html_routes.get("/withdraw/notify/{service}")
|
2021-08-27 20:54:42 +02:00
|
|
|
# @validate_uuids(["wal"], required=True)
|
|
|
|
async def lnurl_balance_notify(request: Request, service: str):
|
2021-04-17 23:27:15 +02:00
|
|
|
bc = await get_balance_check(request.args.get("wal"), service)
|
|
|
|
if bc:
|
|
|
|
redeem_lnurl_withdraw(bc.wallet, bc.url)
|
|
|
|
|
|
|
|
|
2021-08-23 21:17:46 +02:00
|
|
|
@core_html_routes.get("/lnurlwallet")
|
2021-08-27 20:54:42 +02:00
|
|
|
async def lnurlwallet(request: Request):
|
2021-03-26 23:10:30 +01:00
|
|
|
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)
|
2020-09-29 23:24:08 +02:00
|
|
|
|
2021-08-29 19:38:42 +02:00
|
|
|
async with trio.open_nursery() as n:
|
|
|
|
n.start_soon(
|
|
|
|
redeem_lnurl_withdraw,
|
|
|
|
wallet.id,
|
|
|
|
request.args.get("lightning"),
|
|
|
|
"LNbits initial funding: voucher redeem.",
|
|
|
|
{"tag": "lnurlwallet"},
|
|
|
|
5, # wait 5 seconds before sending the invoice to the service
|
2021-03-24 04:40:32 +01:00
|
|
|
)
|
2020-09-29 23:24:08 +02:00
|
|
|
|
2021-08-29 19:38:42 +02:00
|
|
|
return RedirectResponse(f"/wallet?usr={user.id}&wal={wallet.id}", status_code=status.HTTP_307_TEMPORARY_REDIRECT)
|
2021-04-13 14:30:52 +02:00
|
|
|
|
|
|
|
|
2021-08-23 21:17:46 +02:00
|
|
|
@core_html_routes.get("/manifest/{usr}.webmanifest")
|
2021-04-13 14:30:52 +02:00
|
|
|
async def manifest(usr: str):
|
|
|
|
user = await get_user(usr)
|
|
|
|
if not user:
|
|
|
|
return "", HTTPStatus.NOT_FOUND
|
|
|
|
|
2021-08-20 22:31:01 +02:00
|
|
|
return {
|
2021-04-13 14:30:52 +02:00
|
|
|
"short_name": "LNbits",
|
|
|
|
"name": "LNbits Wallet",
|
|
|
|
"icons": [
|
|
|
|
{
|
|
|
|
"src": "https://cdn.jsdelivr.net/gh/lnbits/lnbits@0.3.0/docs/logos/lnbits.png",
|
|
|
|
"type": "image/png",
|
|
|
|
"sizes": "900x900",
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"start_url": "/wallet?usr=" + usr,
|
|
|
|
"background_color": "#3367D6",
|
|
|
|
"description": "Weather forecast information",
|
|
|
|
"display": "standalone",
|
|
|
|
"scope": "/",
|
|
|
|
"theme_color": "#3367D6",
|
|
|
|
"shortcuts": [
|
|
|
|
{
|
|
|
|
"name": wallet.name,
|
|
|
|
"short_name": wallet.name,
|
|
|
|
"description": wallet.name,
|
|
|
|
"url": "/wallet?usr=" + usr + "&wal=" + wallet.id,
|
|
|
|
}
|
|
|
|
for wallet in user.wallets
|
2021-08-20 22:31:01 +02:00
|
|
|
],}
|