2021-08-20 12:44:03 +01:00
|
|
|
from quart import g, jsonify, request
|
|
|
|
from http import HTTPStatus
|
|
|
|
from lnurl.exceptions import InvalidUrl as LnurlInvalidUrl # type: ignore
|
|
|
|
|
|
|
|
from lnbits.core.crud import get_user
|
|
|
|
from lnbits.decorators import api_check_wallet_key, api_validate_post_request
|
2021-08-20 16:35:24 +01:00
|
|
|
from pydantic import BaseModel
|
|
|
|
from fastapi import FastAPI, Query
|
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-08-20 17:15:04 +01:00
|
|
|
@withdraw_ext.get("/api/v1/links")
|
2021-08-20 12:44:03 +01:00
|
|
|
@api_check_wallet_key("invoice")
|
|
|
|
async def api_links():
|
|
|
|
wallet_ids = [g.wallet.id]
|
|
|
|
|
|
|
|
if "all_wallets" in request.args:
|
|
|
|
wallet_ids = (await get_user(g.wallet.user)).wallet_ids
|
|
|
|
try:
|
|
|
|
return (
|
|
|
|
[
|
|
|
|
{
|
|
|
|
**link._asdict(),
|
|
|
|
**{"lnurl": link.lnurl},
|
|
|
|
}
|
|
|
|
for link in await get_withdraw_links(wallet_ids)
|
2021-08-20 22:21:15 +01:00
|
|
|
],
|
2021-08-20 12:44:03 +01:00
|
|
|
HTTPStatus.OK,
|
|
|
|
)
|
|
|
|
except LnurlInvalidUrl:
|
|
|
|
return (
|
|
|
|
{
|
|
|
|
"message": "LNURLs need to be delivered over a publically accessible `https` domain or Tor."
|
2021-08-20 22:21:15 +01:00
|
|
|
},
|
2021-08-20 12:44:03 +01:00
|
|
|
HTTPStatus.UPGRADE_REQUIRED,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-08-20 17:15:04 +01:00
|
|
|
@withdraw_ext.get("/api/v1/links/<link_id>")
|
2021-08-20 12:44:03 +01:00
|
|
|
@api_check_wallet_key("invoice")
|
|
|
|
async def api_link_retrieve(link_id):
|
|
|
|
link = await get_withdraw_link(link_id, 0)
|
|
|
|
|
|
|
|
if not link:
|
2021-08-20 22:21:15 +01:00
|
|
|
return ({"message": "Withdraw link does not exist."},
|
2021-08-20 12:44:03 +01:00
|
|
|
HTTPStatus.NOT_FOUND,
|
|
|
|
)
|
|
|
|
|
|
|
|
if link.wallet != g.wallet.id:
|
2021-08-20 22:21:15 +01:00
|
|
|
return {"message": "Not your withdraw link."}, HTTPStatus.FORBIDDEN
|
2021-08-20 12:44:03 +01:00
|
|
|
|
2021-08-20 22:21:15 +01:00
|
|
|
return {**link, **{"lnurl": link.lnurl}}, HTTPStatus.OK
|
2021-08-20 12:44:03 +01:00
|
|
|
|
2021-08-20 16:15:49 +01:00
|
|
|
class CreateData(BaseModel):
|
2021-08-20 20:54:59 +02:00
|
|
|
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)
|
2021-08-20 16:35:24 +01:00
|
|
|
is_unique: bool
|
2021-08-20 12:44:03 +01:00
|
|
|
|
2021-08-20 17:15:04 +01:00
|
|
|
@withdraw_ext.post("/api/v1/links")
|
|
|
|
@withdraw_ext.put("/api/v1/links/<link_id>")
|
2021-08-20 12:44:03 +01:00
|
|
|
@api_check_wallet_key("admin")
|
2021-08-20 16:35:24 +01:00
|
|
|
async def api_link_create_or_update(data: CreateData, link_id: str = None):
|
|
|
|
if data.max_withdrawable < data.min_withdrawable:
|
2021-08-20 12:44:03 +01:00
|
|
|
return (
|
|
|
|
{
|
|
|
|
"message": "`max_withdrawable` needs to be at least `min_withdrawable`."
|
2021-08-20 22:21:15 +01:00
|
|
|
},
|
2021-08-20 12:44:03 +01:00
|
|
|
HTTPStatus.BAD_REQUEST,
|
|
|
|
)
|
|
|
|
|
|
|
|
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:
|
|
|
|
return (
|
|
|
|
jsonify({"message": "Withdraw link does not exist."}),
|
|
|
|
HTTPStatus.NOT_FOUND,
|
|
|
|
)
|
|
|
|
if link.wallet != g.wallet.id:
|
|
|
|
return jsonify({"message": "Not your withdraw link."}), HTTPStatus.FORBIDDEN
|
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-08-20 16:35:24 +01:00
|
|
|
wallet_id=g.wallet.id, **data, usescsv=usescsv
|
2021-08-20 12:44:03 +01:00
|
|
|
)
|
|
|
|
|
2021-08-20 22:21:15 +01:00
|
|
|
return ({**link, **{"lnurl": link.lnurl}},
|
2021-08-20 12:44:03 +01:00
|
|
|
HTTPStatus.OK if link_id else HTTPStatus.CREATED,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-08-20 17:15:04 +01:00
|
|
|
@withdraw_ext.delete("/api/v1/links/<link_id>")
|
2021-08-20 12:44:03 +01:00
|
|
|
@api_check_wallet_key("admin")
|
|
|
|
async def api_link_delete(link_id):
|
|
|
|
link = await get_withdraw_link(link_id)
|
|
|
|
|
|
|
|
if not link:
|
2021-08-20 22:21:15 +01:00
|
|
|
return ({"message": "Withdraw link does not exist."},
|
2021-08-20 12:44:03 +01:00
|
|
|
HTTPStatus.NOT_FOUND,
|
|
|
|
)
|
|
|
|
|
|
|
|
if link.wallet != g.wallet.id:
|
2021-08-20 22:21:15 +01:00
|
|
|
return {"message": "Not your withdraw link."}, HTTPStatus.FORBIDDEN
|
2021-08-20 12:44:03 +01:00
|
|
|
|
|
|
|
await delete_withdraw_link(link_id)
|
|
|
|
|
|
|
|
return "", HTTPStatus.NO_CONTENT
|
|
|
|
|
|
|
|
|
2021-08-20 17:15:04 +01:00
|
|
|
@withdraw_ext.get("/api/v1/links/<the_hash>/<lnurl_id>")
|
2021-08-20 12:44:03 +01:00
|
|
|
@api_check_wallet_key("invoice")
|
|
|
|
async def api_hash_retrieve(the_hash, lnurl_id):
|
|
|
|
hashCheck = await get_hash_check(the_hash, lnurl_id)
|
2021-08-20 22:21:15 +01:00
|
|
|
return hashCheck, HTTPStatus.OK
|