2021-08-21 12:14:23 +02:00
|
|
|
from typing import Optional
|
2021-09-28 18:10:22 +01:00
|
|
|
from fastapi.params import Depends
|
2021-08-21 12:14:23 +02:00
|
|
|
from fastapi.param_functions import Query
|
|
|
|
from pydantic.main import BaseModel
|
2021-09-28 18:10:22 +01:00
|
|
|
|
2021-08-20 12:44:03 +01:00
|
|
|
from http import HTTPStatus
|
|
|
|
from lnurl.exceptions import InvalidUrl as LnurlInvalidUrl # type: ignore
|
2021-09-28 18:10:22 +01:00
|
|
|
from starlette.exceptions import HTTPException
|
2021-10-06 11:10:24 +01:00
|
|
|
from fastapi import Request
|
2021-09-28 18:10:22 +01:00
|
|
|
from starlette.responses import HTMLResponse, JSONResponse # type: ignore
|
2021-08-20 12:44:03 +01:00
|
|
|
|
|
|
|
from lnbits.core.crud import get_user
|
2021-10-05 09:19:21 +01:00
|
|
|
from lnbits.decorators import WalletTypeInfo, get_key_type
|
2021-08-20 12:44:03 +01:00
|
|
|
from lnbits.utils.exchange_rates import currencies, get_fiat_rate_satoshis
|
2021-10-05 09:19:21 +01:00
|
|
|
from .models import CreatePayLinkData
|
2021-08-20 12:44:03 +01:00
|
|
|
|
|
|
|
from . import lnurlp_ext
|
|
|
|
from .crud import (
|
|
|
|
create_pay_link,
|
|
|
|
get_pay_link,
|
|
|
|
get_pay_links,
|
|
|
|
update_pay_link,
|
|
|
|
delete_pay_link,
|
|
|
|
)
|
|
|
|
|
2021-08-21 00:34:48 +01:00
|
|
|
@lnurlp_ext.get("/api/v1/currencies")
|
2021-08-20 12:44:03 +01:00
|
|
|
async def api_list_currencies_available():
|
2021-09-28 18:10:22 +01:00
|
|
|
return list(currencies.keys())
|
2021-08-20 12:44:03 +01:00
|
|
|
|
|
|
|
|
2021-09-28 18:10:22 +01:00
|
|
|
@lnurlp_ext.get("/api/v1/links", status_code=HTTPStatus.OK)
|
|
|
|
# @api_check_wallet_key("invoice")
|
2021-10-06 11:10:24 +01:00
|
|
|
async def api_links(req: Request, wallet: WalletTypeInfo = Depends(get_key_type), all_wallets: bool = Query(False)):
|
2021-09-28 18:10:22 +01:00
|
|
|
wallet_ids = [wallet.wallet.id]
|
2021-08-20 12:44:03 +01:00
|
|
|
|
2021-09-29 11:03:02 +01:00
|
|
|
if all_wallets:
|
2021-09-28 18:10:22 +01:00
|
|
|
wallet_ids = (await get_user(wallet.wallet.user)).wallet_ids
|
2021-10-08 12:10:43 +01:00
|
|
|
|
2021-08-20 12:44:03 +01:00
|
|
|
try:
|
2021-10-08 12:10:43 +01:00
|
|
|
return [{**link.dict(), "lnurl": link.lnurl(req)} for link in await get_pay_links(wallet_ids)]
|
2021-10-06 11:10:24 +01:00
|
|
|
# return [
|
|
|
|
# {**link.dict(), "lnurl": link.lnurl}
|
|
|
|
# for link in await get_pay_links(wallet_ids)
|
|
|
|
# ]
|
2021-09-29 11:03:02 +01:00
|
|
|
|
2021-08-20 12:44:03 +01:00
|
|
|
except LnurlInvalidUrl:
|
2021-09-28 18:10:22 +01:00
|
|
|
raise HTTPException(
|
|
|
|
status_code=HTTPStatus.UPGRADE_REQUIRED,
|
|
|
|
detail="LNURLs need to be delivered over a publically accessible `https` domain or Tor.",
|
2021-08-20 12:44:03 +01:00
|
|
|
)
|
2021-09-28 18:10:22 +01:00
|
|
|
# return (
|
|
|
|
# {
|
|
|
|
# "message": "LNURLs need to be delivered over a publically accessible `https` domain or Tor."
|
|
|
|
# },
|
|
|
|
# HTTPStatus.UPGRADE_REQUIRED,
|
|
|
|
# )
|
2021-08-20 12:44:03 +01:00
|
|
|
|
|
|
|
|
2021-09-28 18:10:22 +01:00
|
|
|
@lnurlp_ext.get("/api/v1/links/{link_id}", status_code=HTTPStatus.OK)
|
|
|
|
# @api_check_wallet_key("invoice")
|
2021-10-06 11:10:24 +01:00
|
|
|
async def api_link_retrieve(r: Request, link_id, wallet: WalletTypeInfo = Depends(get_key_type)):
|
2021-08-20 12:44:03 +01:00
|
|
|
link = await get_pay_link(link_id)
|
|
|
|
|
|
|
|
if not link:
|
2021-09-28 18:10:22 +01:00
|
|
|
raise HTTPException(
|
|
|
|
detail="Pay link does not exist.",
|
|
|
|
status_code=HTTPStatus.NOT_FOUND
|
|
|
|
)
|
|
|
|
# return {"message": "Pay link does not exist."}, HTTPStatus.NOT_FOUND
|
2021-08-20 12:44:03 +01:00
|
|
|
|
2021-09-28 18:10:22 +01:00
|
|
|
if link.wallet != wallet.wallet.id:
|
|
|
|
raise HTTPException(
|
|
|
|
detail="Not your pay link.",
|
|
|
|
status_code=HTTPStatus.FORBIDDEN
|
|
|
|
)
|
|
|
|
# return {"message": "Not your pay link."}, HTTPStatus.FORBIDDEN
|
2021-08-21 00:34:48 +01:00
|
|
|
|
2021-10-06 11:10:24 +01:00
|
|
|
return {**link._asdict(), **{"lnurl": link.lnurl(r)}}
|
2021-08-21 00:34:48 +01:00
|
|
|
|
|
|
|
|
2021-09-28 18:10:22 +01:00
|
|
|
@lnurlp_ext.post("/api/v1/links", status_code=HTTPStatus.CREATED)
|
|
|
|
@lnurlp_ext.put("/api/v1/links/{link_id}", status_code=HTTPStatus.OK)
|
|
|
|
# @api_check_wallet_key("invoice")
|
2021-10-05 09:19:21 +01:00
|
|
|
async def api_link_create_or_update(data: CreatePayLinkData, link_id=None, wallet: WalletTypeInfo = Depends(get_key_type)):
|
2021-08-21 00:34:48 +01:00
|
|
|
if data.min > data.max:
|
2021-09-28 18:10:22 +01:00
|
|
|
raise HTTPException(
|
|
|
|
detail="Min is greater than max.",
|
|
|
|
status_code=HTTPStatus.BAD_REQUEST
|
|
|
|
)
|
|
|
|
# return {"message": "Min is greater than max."}, HTTPStatus.BAD_REQUEST
|
2021-08-20 12:44:03 +01:00
|
|
|
|
2021-08-21 00:34:48 +01:00
|
|
|
if data.currency == None and (
|
|
|
|
round(data.min) != data.min or round(data.max) != data.max
|
2021-08-20 12:44:03 +01:00
|
|
|
):
|
2021-09-28 18:10:22 +01:00
|
|
|
raise HTTPException(
|
|
|
|
detail="Must use full satoshis.",
|
|
|
|
status_code=HTTPStatus.BAD_REQUEST
|
|
|
|
)
|
|
|
|
# return {"message": "Must use full satoshis."}, HTTPStatus.BAD_REQUEST
|
2021-08-20 12:44:03 +01:00
|
|
|
|
2021-08-21 00:34:48 +01:00
|
|
|
if "success_url" in data and data.success_url[:8] != "https://":
|
2021-09-28 18:10:22 +01:00
|
|
|
raise HTTPException(
|
|
|
|
detail="Success URL must be secure https://...",
|
|
|
|
status_code=HTTPStatus.BAD_REQUEST
|
2021-08-20 12:44:03 +01:00
|
|
|
)
|
2021-09-28 18:10:22 +01:00
|
|
|
# return (
|
|
|
|
# {"message": "Success URL must be secure https://..."},
|
|
|
|
# HTTPStatus.BAD_REQUEST,
|
|
|
|
# )
|
2021-08-20 12:44:03 +01:00
|
|
|
|
|
|
|
if link_id:
|
|
|
|
link = await get_pay_link(link_id)
|
|
|
|
|
|
|
|
if not link:
|
2021-09-28 18:10:22 +01:00
|
|
|
raise HTTPException(
|
|
|
|
detail="Pay link does not exist.",
|
|
|
|
status_code=HTTPStatus.NOT_FOUND
|
2021-08-20 12:44:03 +01:00
|
|
|
)
|
2021-09-28 18:10:22 +01:00
|
|
|
# return (
|
|
|
|
# {"message": "Pay link does not exist."},
|
|
|
|
# HTTPStatus.NOT_FOUND,
|
|
|
|
# )
|
2021-08-20 12:44:03 +01:00
|
|
|
|
2021-10-05 09:19:21 +01:00
|
|
|
if link.wallet != wallet.wallet.id:
|
2021-09-28 18:10:22 +01:00
|
|
|
raise HTTPException(
|
|
|
|
detail="Not your pay link.",
|
|
|
|
status_code=HTTPStatus.FORBIDDEN
|
|
|
|
)
|
|
|
|
# return {"message": "Not your pay link."}, HTTPStatus.FORBIDDEN
|
2021-08-20 12:44:03 +01:00
|
|
|
|
2021-10-05 09:19:21 +01:00
|
|
|
link = await update_pay_link(link_id, data)
|
2021-08-20 12:44:03 +01:00
|
|
|
else:
|
2021-10-05 09:19:21 +01:00
|
|
|
link = await create_pay_link(data, wallet_id=wallet.wallet.id)
|
|
|
|
print("LINK", link)
|
|
|
|
return {**link.dict(), "lnurl": link.lnurl}
|
2021-08-20 12:44:03 +01:00
|
|
|
|
|
|
|
|
2021-08-22 12:16:31 +01:00
|
|
|
@lnurlp_ext.delete("/api/v1/links/{link_id}")
|
2021-09-28 18:10:22 +01:00
|
|
|
# @api_check_wallet_key("invoice")
|
2021-09-29 11:03:02 +01:00
|
|
|
async def api_link_delete(link_id, wallet: WalletTypeInfo = Depends(get_key_type)):
|
2021-08-20 12:44:03 +01:00
|
|
|
link = await get_pay_link(link_id)
|
|
|
|
|
|
|
|
if not link:
|
2021-09-28 18:10:22 +01:00
|
|
|
raise HTTPException(
|
|
|
|
detail="Pay link does not exist.",
|
|
|
|
status_code=HTTPStatus.NOT_FOUND
|
|
|
|
)
|
|
|
|
# return {"message": "Pay link does not exist."}, HTTPStatus.NOT_FOUND
|
2021-08-20 12:44:03 +01:00
|
|
|
|
2021-09-28 18:10:22 +01:00
|
|
|
if link.wallet != wallet.wallet.id:
|
|
|
|
raise HTTPException(
|
|
|
|
detail="Not your pay link.",
|
|
|
|
status_code=HTTPStatus.FORBIDDEN
|
|
|
|
)
|
|
|
|
# return {"message": "Not your pay link."}, HTTPStatus.FORBIDDEN
|
2021-08-20 12:44:03 +01:00
|
|
|
|
|
|
|
await delete_pay_link(link_id)
|
2021-09-28 18:10:22 +01:00
|
|
|
raise HTTPException(status_code=HTTPStatus.NO_CONTENT)
|
|
|
|
# return "", HTTPStatus.NO_CONTENT
|
2021-08-20 12:44:03 +01:00
|
|
|
|
|
|
|
|
2021-09-28 18:10:22 +01:00
|
|
|
@lnurlp_ext.get("/api/v1/rate/{currency}", status_code=HTTPStatus.OK)
|
2021-08-20 12:44:03 +01:00
|
|
|
async def api_check_fiat_rate(currency):
|
|
|
|
try:
|
|
|
|
rate = await get_fiat_rate_satoshis(currency)
|
|
|
|
except AssertionError:
|
|
|
|
rate = None
|
|
|
|
|
2021-09-28 18:10:22 +01:00
|
|
|
return {"rate": rate}
|