2021-09-29 11:04:37 +01:00
|
|
|
from fastapi.params import Depends
|
|
|
|
from fastapi.param_functions import Query
|
|
|
|
from pydantic.main import BaseModel
|
|
|
|
|
2021-08-20 12:44:03 +01:00
|
|
|
from http import HTTPStatus
|
|
|
|
from lnurl.exceptions import InvalidUrl as LnurlInvalidUrl # type: ignore
|
2021-09-29 11:04:37 +01:00
|
|
|
from starlette.exceptions import HTTPException
|
|
|
|
from starlette.requests import Request
|
|
|
|
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-10-06 14:40:12 +01:00
|
|
|
from .models import CreateWithdrawData
|
2021-09-29 11:04:37 +01:00
|
|
|
|
|
|
|
# from fastapi import FastAPI, Query, Response
|
2021-08-20 12:44:03 +01:00
|
|
|
|
|
|
|
from . import withdraw_ext
|
|
|
|
from .crud import (
|
|
|
|
create_withdraw_link,
|
|
|
|
get_withdraw_link,
|
|
|
|
get_withdraw_links,
|
|
|
|
update_withdraw_link,
|
|
|
|
delete_withdraw_link,
|
|
|
|
create_hash_check,
|
|
|
|
get_hash_check,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-09-29 11:04:37 +01:00
|
|
|
@withdraw_ext.get("/api/v1/links", status_code=HTTPStatus.OK)
|
|
|
|
# @api_check_wallet_key("invoice")
|
2021-10-06 14:40:12 +01:00
|
|
|
async def api_links(req: Request, wallet: WalletTypeInfo = Depends(get_key_type), all_wallets: bool = Query(False)):
|
2021-09-29 11:04:37 +01:00
|
|
|
wallet_ids = [wallet.wallet.id]
|
2021-08-20 12:44:03 +01:00
|
|
|
|
2021-09-29 11:04:37 +01:00
|
|
|
if all_wallets:
|
|
|
|
wallet_ids = (await get_user(wallet.wallet.user)).wallet_ids
|
2021-08-20 12:44:03 +01:00
|
|
|
try:
|
2021-08-25 15:44:18 +01:00
|
|
|
return [
|
2021-08-20 12:44:03 +01:00
|
|
|
{
|
2021-10-06 14:40:12 +01:00
|
|
|
**link.dict(),
|
|
|
|
**{"lnurl": link.lnurl(req)},
|
2021-08-20 12:44:03 +01:00
|
|
|
}
|
|
|
|
for link in await get_withdraw_links(wallet_ids)
|
2021-08-25 15:44:18 +01:00
|
|
|
]
|
2021-09-29 11:04:37 +01:00
|
|
|
|
2021-08-20 12:44:03 +01:00
|
|
|
except LnurlInvalidUrl:
|
2021-09-29 11:04:37 +01:00
|
|
|
raise HTTPException(
|
|
|
|
status_code=HTTPStatus.UPGRADE_REQUIRED,
|
|
|
|
detail="LNURLs need to be delivered over a publically accessible `https` domain or Tor.",
|
|
|
|
)
|
|
|
|
# response.status_code = HTTPStatus.UPGRADE_REQUIRED
|
|
|
|
# return { "message": "LNURLs need to be delivered over a publically accessible `https` domain or Tor." }
|
2021-08-20 12:44:03 +01:00
|
|
|
|
|
|
|
|
2021-09-29 11:04:37 +01:00
|
|
|
@withdraw_ext.get("/api/v1/links/{link_id}", status_code=HTTPStatus.OK)
|
|
|
|
# @api_check_wallet_key("invoice")
|
|
|
|
async def api_link_retrieve(link_id, wallet: WalletTypeInfo = Depends(get_key_type)):
|
2021-08-20 12:44:03 +01:00
|
|
|
link = await get_withdraw_link(link_id, 0)
|
|
|
|
|
|
|
|
if not link:
|
2021-09-29 11:04:37 +01:00
|
|
|
raise HTTPException(
|
|
|
|
detail="Withdraw link does not exist.",
|
|
|
|
status_code=HTTPStatus.NOT_FOUND
|
|
|
|
)
|
|
|
|
# response.status_code = HTTPStatus.NOT_FOUND
|
|
|
|
# return {"message": "Withdraw link does not exist."}
|
2021-08-20 12:44:03 +01:00
|
|
|
|
2021-09-29 11:04:37 +01:00
|
|
|
if link.wallet != wallet.wallet.id:
|
|
|
|
raise HTTPException(
|
|
|
|
detail="Not your withdraw link.",
|
|
|
|
status_code=HTTPStatus.FORBIDDEN
|
|
|
|
)
|
|
|
|
# response.status_code = HTTPStatus.FORBIDDEN
|
|
|
|
# return {"message": "Not your withdraw link."}
|
2021-08-20 12:44:03 +01:00
|
|
|
|
2021-08-25 15:44:18 +01:00
|
|
|
return {**link, **{"lnurl": link.lnurl}}
|
2021-08-20 12:44:03 +01:00
|
|
|
|
2021-10-06 14:40:12 +01:00
|
|
|
# class CreateData(BaseModel):
|
|
|
|
# title: str = Query(...)
|
|
|
|
# min_withdrawable: int = Query(..., ge=1)
|
|
|
|
# max_withdrawable: int = Query(..., ge=1)
|
|
|
|
# uses: int = Query(..., ge=1)
|
|
|
|
# wait_time: int = Query(..., ge=1)
|
|
|
|
# is_unique: bool
|
2021-08-20 12:44:03 +01:00
|
|
|
|
2021-08-25 15:44:18 +01:00
|
|
|
@withdraw_ext.post("/api/v1/links", status_code=HTTPStatus.CREATED)
|
2021-09-29 11:04:37 +01:00
|
|
|
@withdraw_ext.put("/api/v1/links/{link_id}", status_code=HTTPStatus.OK)
|
2021-10-05 09:19:21 +01:00
|
|
|
# @api_check_wallet_key("admin")
|
2021-10-06 14:40:12 +01:00
|
|
|
async def api_link_create_or_update(req: Request, data: CreateWithdrawData, link_id: str = None, wallet: WalletTypeInfo = Depends(get_key_type)):
|
2021-08-20 16:35:24 +01:00
|
|
|
if data.max_withdrawable < data.min_withdrawable:
|
2021-09-29 11:04:37 +01:00
|
|
|
raise HTTPException(
|
|
|
|
detail="`max_withdrawable` needs to be at least `min_withdrawable`.",
|
|
|
|
status_code=HTTPStatus.BAD_REQUEST
|
|
|
|
)
|
|
|
|
# response.status_code = HTTPStatus.BAD_REQUEST
|
|
|
|
# return {
|
|
|
|
# "message": "`max_withdrawable` needs to be at least `min_withdrawable`."
|
|
|
|
# }
|
2021-08-20 12:44:03 +01:00
|
|
|
|
|
|
|
usescsv = ""
|
2021-08-20 16:35:24 +01:00
|
|
|
for i in range(data.uses):
|
|
|
|
if data.is_unique:
|
2021-08-20 12:44:03 +01:00
|
|
|
usescsv += "," + str(i + 1)
|
|
|
|
else:
|
|
|
|
usescsv += "," + str(1)
|
|
|
|
usescsv = usescsv[1:]
|
|
|
|
|
|
|
|
if link_id:
|
|
|
|
link = await get_withdraw_link(link_id, 0)
|
|
|
|
if not link:
|
2021-09-29 11:04:37 +01:00
|
|
|
raise HTTPException(
|
|
|
|
detail="Withdraw link does not exist.",
|
|
|
|
status_code=HTTPStatus.NOT_FOUND
|
|
|
|
)
|
|
|
|
# response.status_code = HTTPStatus.NOT_FOUND
|
|
|
|
# return {"message": "Withdraw link does not exist."}
|
|
|
|
if link.wallet != wallet.wallet.id:
|
|
|
|
raise HTTPException(
|
|
|
|
detail="Not your withdraw link.",
|
|
|
|
status_code=HTTPStatus.FORBIDDEN
|
|
|
|
)
|
|
|
|
# response.status_code = HTTPStatus.FORBIDDEN
|
|
|
|
# return {"message": "Not your withdraw link."}
|
2021-08-20 16:35:24 +01:00
|
|
|
link = await update_withdraw_link(link_id, **data, usescsv=usescsv, used=0)
|
2021-08-20 12:44:03 +01:00
|
|
|
else:
|
|
|
|
link = await create_withdraw_link(
|
2021-10-06 14:40:12 +01:00
|
|
|
wallet_id=wallet.wallet.id, data=data, usescsv=usescsv
|
2021-08-20 12:44:03 +01:00
|
|
|
)
|
2021-09-29 11:04:37 +01:00
|
|
|
# if link_id:
|
|
|
|
# response.status_code = HTTPStatus.OK
|
2021-10-06 14:40:12 +01:00
|
|
|
return {**link.dict(), **{"lnurl": link.lnurl(req)}}
|
2021-08-20 12:44:03 +01:00
|
|
|
|
|
|
|
|
2021-09-29 11:04:37 +01:00
|
|
|
@withdraw_ext.delete("/api/v1/links/{link_id}")
|
|
|
|
# @api_check_wallet_key("admin")
|
|
|
|
async def api_link_delete(link_id, wallet: WalletTypeInfo = Depends(get_key_type)):
|
2021-08-20 12:44:03 +01:00
|
|
|
link = await get_withdraw_link(link_id)
|
|
|
|
|
|
|
|
if not link:
|
2021-09-29 11:04:37 +01:00
|
|
|
raise HTTPException(
|
|
|
|
detail="Withdraw link does not exist.",
|
|
|
|
status_code=HTTPStatus.NOT_FOUND
|
|
|
|
)
|
|
|
|
# response.status_code = HTTPStatus.NOT_FOUND
|
|
|
|
# return {"message": "Withdraw link does not exist."}
|
2021-08-20 12:44:03 +01:00
|
|
|
|
2021-09-29 11:04:37 +01:00
|
|
|
if link.wallet != wallet.wallet.id:
|
|
|
|
raise HTTPException(
|
|
|
|
detail="Not your withdraw link.",
|
|
|
|
status_code=HTTPStatus.FORBIDDEN
|
|
|
|
)
|
|
|
|
# response.status_code = HTTPStatus.FORBIDDEN
|
|
|
|
# return {"message": "Not your withdraw link."}
|
2021-08-20 12:44:03 +01:00
|
|
|
|
|
|
|
await delete_withdraw_link(link_id)
|
2021-09-29 11:04:37 +01:00
|
|
|
raise HTTPException(status_code=HTTPStatus.NO_CONTENT)
|
|
|
|
# return ""
|
2021-08-20 12:44:03 +01:00
|
|
|
|
|
|
|
|
2021-08-25 15:44:18 +01:00
|
|
|
@withdraw_ext.get("/api/v1/links/{the_hash}/{lnurl_id}", status_code=HTTPStatus.OK)
|
2021-09-29 11:04:37 +01:00
|
|
|
# @api_check_wallet_key("invoice")
|
|
|
|
async def api_hash_retrieve(the_hash, lnurl_id, wallet: WalletTypeInfo = Depends(get_key_type)):
|
2021-08-20 12:44:03 +01:00
|
|
|
hashCheck = await get_hash_check(the_hash, lnurl_id)
|
2021-08-25 15:44:18 +01:00
|
|
|
return hashCheck
|