2020-12-28 22:40:46 +01:00
|
|
|
import re
|
|
|
|
from quart import g, jsonify, request
|
|
|
|
from http import HTTPStatus
|
2020-12-28 19:51:45 +01:00
|
|
|
|
2020-12-28 22:40:46 +01:00
|
|
|
from lnbits.core.crud import get_user, get_wallet
|
|
|
|
from lnbits.core.services import create_invoice, check_invoice_status
|
|
|
|
from lnbits.decorators import api_check_wallet_key, api_validate_post_request
|
2020-12-28 19:51:45 +01:00
|
|
|
|
2021-01-02 22:38:28 +01:00
|
|
|
from .util import isValidDomain, isvalidIPAddress
|
2020-12-28 23:24:47 +01:00
|
|
|
from . import subdomains_ext
|
2020-12-28 22:40:46 +01:00
|
|
|
from .crud import (
|
|
|
|
create_subdomain,
|
|
|
|
get_subdomain,
|
|
|
|
get_subdomains,
|
|
|
|
delete_subdomain,
|
|
|
|
create_domain,
|
|
|
|
update_domain,
|
|
|
|
get_domain,
|
|
|
|
get_domains,
|
|
|
|
delete_domain,
|
|
|
|
)
|
2020-12-28 19:51:45 +01:00
|
|
|
|
|
|
|
|
2020-12-28 22:40:46 +01:00
|
|
|
# domainS
|
|
|
|
|
|
|
|
|
2020-12-28 23:24:47 +01:00
|
|
|
@subdomains_ext.route("/api/v1/domains", methods=["GET"])
|
2020-12-28 22:40:46 +01:00
|
|
|
@api_check_wallet_key("invoice")
|
|
|
|
async def api_domains():
|
|
|
|
wallet_ids = [g.wallet.id]
|
|
|
|
|
|
|
|
if "all_wallets" in request.args:
|
|
|
|
wallet_ids = (await get_user(g.wallet.user)).wallet_ids
|
|
|
|
|
|
|
|
return jsonify([domain._asdict() for domain in await get_domains(wallet_ids)]), HTTPStatus.OK
|
|
|
|
|
|
|
|
|
2020-12-28 23:24:47 +01:00
|
|
|
@subdomains_ext.route("/api/v1/domains", methods=["POST"])
|
|
|
|
@subdomains_ext.route("/api/v1/domains/<domain_id>", methods=["PUT"])
|
2020-12-28 22:40:46 +01:00
|
|
|
@api_check_wallet_key("invoice")
|
|
|
|
@api_validate_post_request(
|
|
|
|
schema={
|
|
|
|
"wallet": {"type": "string", "empty": False, "required": True},
|
|
|
|
"domain": {"type": "string", "empty": False, "required": True},
|
2020-12-30 11:50:49 +01:00
|
|
|
"cf_token": {"type": "string", "empty": False, "required": True},
|
|
|
|
"cf_zone_id": {"type": "string", "empty": False, "required": True},
|
2020-12-28 22:40:46 +01:00
|
|
|
"webhook": {"type": "string", "empty": False, "required": False},
|
|
|
|
"description": {"type": "string", "min": 0, "required": True},
|
|
|
|
"cost": {"type": "integer", "min": 0, "required": True},
|
2020-12-31 18:39:16 +01:00
|
|
|
"allowed_record_types": {"type": "string", "required": True},
|
2020-12-28 22:40:46 +01:00
|
|
|
}
|
|
|
|
)
|
|
|
|
async def api_domain_create(domain_id=None):
|
|
|
|
if domain_id:
|
|
|
|
domain = await get_domain(domain_id)
|
|
|
|
|
|
|
|
if not domain:
|
|
|
|
return jsonify({"message": "domain does not exist."}), HTTPStatus.NOT_FOUND
|
|
|
|
|
|
|
|
if domain.wallet != g.wallet.id:
|
|
|
|
return jsonify({"message": "Not your domain."}), HTTPStatus.FORBIDDEN
|
|
|
|
|
|
|
|
domain = await update_domain(domain_id, **g.data)
|
|
|
|
else:
|
|
|
|
domain = await create_domain(**g.data)
|
|
|
|
return jsonify(domain._asdict()), HTTPStatus.CREATED
|
|
|
|
|
|
|
|
|
2020-12-28 23:24:47 +01:00
|
|
|
@subdomains_ext.route("/api/v1/domains/<domain_id>", methods=["DELETE"])
|
2020-12-28 22:40:46 +01:00
|
|
|
@api_check_wallet_key("invoice")
|
|
|
|
async def api_domain_delete(domain_id):
|
|
|
|
domain = await get_domain(domain_id)
|
|
|
|
|
|
|
|
if not domain:
|
|
|
|
return jsonify({"message": "domain does not exist."}), HTTPStatus.NOT_FOUND
|
|
|
|
|
|
|
|
if domain.wallet != g.wallet.id:
|
|
|
|
return jsonify({"message": "Not your domain."}), HTTPStatus.FORBIDDEN
|
|
|
|
|
|
|
|
await delete_domain(domain_id)
|
|
|
|
|
|
|
|
return "", HTTPStatus.NO_CONTENT
|
2020-12-28 19:51:45 +01:00
|
|
|
|
|
|
|
|
2020-12-28 22:40:46 +01:00
|
|
|
#########subdomains##########
|
2020-12-28 19:51:45 +01:00
|
|
|
|
|
|
|
|
2020-12-28 23:24:47 +01:00
|
|
|
@subdomains_ext.route("/api/v1/subdomains", methods=["GET"])
|
2020-12-28 22:40:46 +01:00
|
|
|
@api_check_wallet_key("invoice")
|
2020-12-28 19:51:45 +01:00
|
|
|
async def api_subdomains():
|
2020-12-28 22:40:46 +01:00
|
|
|
wallet_ids = [g.wallet.id]
|
|
|
|
|
|
|
|
if "all_wallets" in request.args:
|
|
|
|
wallet_ids = (await get_user(g.wallet.user)).wallet_ids
|
|
|
|
|
|
|
|
return jsonify([domain._asdict() for domain in await get_subdomains(wallet_ids)]), HTTPStatus.OK
|
|
|
|
|
|
|
|
|
2020-12-28 23:24:47 +01:00
|
|
|
@subdomains_ext.route("/api/v1/subdomains/<domain_id>", methods=["POST"])
|
2020-12-28 22:40:46 +01:00
|
|
|
@api_validate_post_request(
|
|
|
|
schema={
|
|
|
|
"domain": {"type": "string", "empty": False, "required": True},
|
|
|
|
"subdomain": {"type": "string", "empty": False, "required": True},
|
|
|
|
"email": {"type": "string", "empty": True, "required": True},
|
|
|
|
"ip": {"type": "string", "empty": False, "required": True},
|
|
|
|
"sats": {"type": "integer", "min": 0, "required": True},
|
2020-12-29 20:52:54 +01:00
|
|
|
"duration": {"type": "integer", "empty": False, "required": True},
|
2020-12-31 18:39:16 +01:00
|
|
|
"record_type": {"type": "string", "empty": False, "required": True},
|
2020-12-28 22:40:46 +01:00
|
|
|
}
|
|
|
|
)
|
|
|
|
async def api_subdomain_make_subdomain(domain_id):
|
|
|
|
domain = await get_domain(domain_id)
|
2021-01-02 22:38:28 +01:00
|
|
|
|
2020-12-28 22:40:46 +01:00
|
|
|
if not domain:
|
|
|
|
return jsonify({"message": "LNsubdomain does not exist."}), HTTPStatus.NOT_FOUND
|
2021-01-02 22:38:28 +01:00
|
|
|
if not isvalidIPAddress(g.data["ip"]):
|
2021-01-02 19:21:39 +01:00
|
|
|
return jsonify({"message": g.data["ip"] + " Not a valid IP address"}), HTTPStatus.BAD_REQUEST
|
2021-01-02 22:38:28 +01:00
|
|
|
if not isValidDomain(g.data["subdomain"] + "." + domain.domain):
|
|
|
|
return (
|
|
|
|
jsonify({"message": g.data["subdomain"] + "." + domain.domain + " Bad domain name"}),
|
|
|
|
HTTPStatus.BAD_REQUEST,
|
|
|
|
)
|
2021-01-02 19:21:39 +01:00
|
|
|
if g.data["record_type"] not in domain.allowed_record_types:
|
|
|
|
return jsonify({"message": g.data["record_type"] + "Not a valid record"}), HTTPStatus.BAD_REQUEST
|
2020-12-28 22:40:46 +01:00
|
|
|
|
2020-12-29 20:52:54 +01:00
|
|
|
subdomain = g.data["subdomain"]
|
|
|
|
duration = g.data["duration"]
|
2020-12-28 22:40:46 +01:00
|
|
|
sats = g.data["sats"]
|
|
|
|
payment_hash, payment_request = await create_invoice(
|
|
|
|
wallet_id=domain.wallet,
|
|
|
|
amount=sats,
|
2020-12-29 20:52:54 +01:00
|
|
|
memo=f"subdomain {subdomain}.{domain.domain} for {sats} sats for {duration} days",
|
2020-12-28 22:40:46 +01:00
|
|
|
extra={"tag": "lnsubdomain"},
|
|
|
|
)
|
|
|
|
|
|
|
|
subdomain = await create_subdomain(payment_hash=payment_hash, wallet=domain.wallet, **g.data)
|
|
|
|
|
|
|
|
if not subdomain:
|
|
|
|
return jsonify({"message": "LNsubdomain could not be fetched."}), HTTPStatus.NOT_FOUND
|
|
|
|
|
|
|
|
return jsonify({"payment_hash": payment_hash, "payment_request": payment_request}), HTTPStatus.OK
|
|
|
|
|
|
|
|
|
2020-12-28 23:24:47 +01:00
|
|
|
@subdomains_ext.route("/api/v1/subdomains/<payment_hash>", methods=["GET"])
|
2020-12-28 22:40:46 +01:00
|
|
|
async def api_subdomain_send_subdomain(payment_hash):
|
|
|
|
subdomain = await get_subdomain(payment_hash)
|
|
|
|
try:
|
|
|
|
status = await check_invoice_status(subdomain.wallet, payment_hash)
|
|
|
|
is_paid = not status.pending
|
|
|
|
except Exception:
|
|
|
|
return jsonify({"paid": False}), HTTPStatus.OK
|
|
|
|
|
|
|
|
if is_paid:
|
2021-01-02 22:55:52 +01:00
|
|
|
return jsonify({"paid": True}), HTTPStatus.OK
|
|
|
|
|
|
|
|
return jsonify({"paid": False}), HTTPStatus.OK
|
2020-12-28 22:40:46 +01:00
|
|
|
|
|
|
|
|
2020-12-28 23:24:47 +01:00
|
|
|
@subdomains_ext.route("/api/v1/subdomains/<subdomain_id>", methods=["DELETE"])
|
2020-12-28 22:40:46 +01:00
|
|
|
@api_check_wallet_key("invoice")
|
|
|
|
async def api_subdomain_delete(subdomain_id):
|
|
|
|
subdomain = await get_subdomain(subdomain_id)
|
|
|
|
|
|
|
|
if not subdomain:
|
|
|
|
return jsonify({"message": "Paywall does not exist."}), HTTPStatus.NOT_FOUND
|
|
|
|
|
|
|
|
if subdomain.wallet != g.wallet.id:
|
|
|
|
return jsonify({"message": "Not your subdomain."}), HTTPStatus.FORBIDDEN
|
|
|
|
|
|
|
|
await delete_subdomain(subdomain_id)
|
2020-12-28 22:32:04 +01:00
|
|
|
|
2020-12-28 22:40:46 +01:00
|
|
|
return "", HTTPStatus.NO_CONTENT
|