lnbits-legend/lnbits/extensions/watchonly/views_api.py

139 lines
4 KiB
Python
Raw Normal View History

2020-12-01 19:54:16 +00:00
import hashlib
2021-03-31 23:49:36 +01:00
from quart import g, jsonify, url_for, request
2020-12-01 19:54:16 +00:00
from http import HTTPStatus
2021-04-04 13:28:46 +01:00
import httpx
import json
2020-12-01 19:54:16 +00:00
from lnbits.core.crud import get_user
from lnbits.decorators import api_check_wallet_key, api_validate_post_request
from lnbits.extensions.watchonly import watchonly_ext
from .crud import (
create_watch_wallet,
get_watch_wallet,
get_watch_wallets,
update_watch_wallet,
delete_watch_wallet,
get_fresh_address,
get_addresses,
2020-12-01 19:54:16 +00:00
create_mempool,
update_mempool,
get_mempool,
)
###################WALLETS#############################
2021-04-04 13:28:46 +01:00
2020-12-01 19:54:16 +00:00
@watchonly_ext.route("/api/v1/wallet", methods=["GET"])
@api_check_wallet_key("invoice")
async def api_wallets_retrieve():
try:
return (
2021-04-09 00:41:19 +01:00
jsonify(
[wallet._asdict() for wallet in await get_watch_wallets(g.wallet.user)]
),
HTTPStatus.OK,
2020-12-01 19:54:16 +00:00
)
except:
2020-12-02 22:47:33 +00:00
return ""
2020-12-01 19:54:16 +00:00
2021-04-04 13:28:46 +01:00
2020-12-01 19:54:16 +00:00
@watchonly_ext.route("/api/v1/wallet/<wallet_id>", methods=["GET"])
@api_check_wallet_key("invoice")
async def api_wallet_retrieve(wallet_id):
2021-04-04 13:17:24 +01:00
wallet = await get_watch_wallet(wallet_id)
2021-04-04 13:28:46 +01:00
2020-12-01 19:54:16 +00:00
if not wallet:
return jsonify({"message": "wallet does not exist"}), HTTPStatus.NOT_FOUND
2021-04-04 13:17:24 +01:00
return jsonify(wallet._asdict()), HTTPStatus.OK
2020-12-01 19:54:16 +00:00
@watchonly_ext.route("/api/v1/wallet", methods=["POST"])
2021-04-04 13:17:24 +01:00
@api_check_wallet_key("admin")
2020-12-01 19:54:16 +00:00
@api_validate_post_request(
schema={
"masterpub": {"type": "string", "empty": False, "required": True},
"title": {"type": "string", "empty": False, "required": True},
}
)
async def api_wallet_create_or_update(wallet_id=None):
2021-04-07 19:27:26 +02:00
try:
2021-04-09 00:41:19 +01:00
wallet = await create_watch_wallet(
user=g.wallet.user, masterpub=g.data["masterpub"], title=g.data["title"]
)
2021-04-07 19:27:26 +02:00
except Exception as e:
return jsonify({"message": str(e)}), HTTPStatus.BAD_REQUEST
2021-04-04 13:28:46 +01:00
mempool = await get_mempool(g.wallet.user)
2021-04-04 13:17:24 +01:00
if not mempool:
create_mempool(user=g.wallet.user)
return jsonify(wallet._asdict()), HTTPStatus.CREATED
2020-12-01 19:54:16 +00:00
@watchonly_ext.route("/api/v1/wallet/<wallet_id>", methods=["DELETE"])
2021-04-04 13:17:24 +01:00
@api_check_wallet_key("admin")
2020-12-01 19:54:16 +00:00
async def api_wallet_delete(wallet_id):
2020-12-01 21:39:33 +00:00
wallet = await get_watch_wallet(wallet_id)
2020-12-01 19:54:16 +00:00
if not wallet:
return jsonify({"message": "Wallet link does not exist."}), HTTPStatus.NOT_FOUND
2020-12-01 21:39:33 +00:00
await delete_watch_wallet(wallet_id)
2020-12-01 19:54:16 +00:00
return jsonify({"deleted": "true"}), HTTPStatus.NO_CONTENT
#############################ADDRESSES##########################
2021-04-09 00:41:19 +01:00
@watchonly_ext.route("/api/v1/address/<wallet_id>", methods=["GET"])
@api_check_wallet_key("invoice")
async def api_fresh_address(wallet_id):
2021-04-04 13:28:46 +01:00
await get_fresh_address(wallet_id)
addresses = await get_addresses(wallet_id)
return jsonify([address._asdict() for address in addresses]), HTTPStatus.OK
@watchonly_ext.route("/api/v1/addresses/<wallet_id>", methods=["GET"])
@api_check_wallet_key("invoice")
async def api_get_addresses(wallet_id):
2021-04-04 13:28:46 +01:00
wallet = await get_watch_wallet(wallet_id)
if not wallet:
return jsonify({"message": "wallet does not exist"}), HTTPStatus.NOT_FOUND
2021-04-04 13:28:46 +01:00
addresses = await get_addresses(wallet_id)
if not addresses:
await get_fresh_address(wallet_id)
2021-04-04 13:28:46 +01:00
addresses = await get_addresses(wallet_id)
return jsonify([address._asdict() for address in addresses]), HTTPStatus.OK
2020-12-01 19:54:16 +00:00
#############################MEMPOOL##########################
2021-04-09 00:41:19 +01:00
2020-12-01 19:54:16 +00:00
@watchonly_ext.route("/api/v1/mempool", methods=["PUT"])
2021-04-04 13:17:24 +01:00
@api_check_wallet_key("admin")
2020-12-01 19:54:16 +00:00
@api_validate_post_request(
schema={
"endpoint": {"type": "string", "empty": False, "required": True},
}
)
async def api_update_mempool():
2020-12-01 21:39:33 +00:00
mempool = await update_mempool(user=g.wallet.user, **g.data)
2021-04-04 13:28:46 +01:00
return jsonify(mempool._asdict()), HTTPStatus.OK
2020-12-01 19:54:16 +00:00
@watchonly_ext.route("/api/v1/mempool", methods=["GET"])
2021-04-04 13:17:24 +01:00
@api_check_wallet_key("admin")
2020-12-01 19:54:16 +00:00
async def api_get_mempool():
2021-04-04 13:28:46 +01:00
mempool = await get_mempool(g.wallet.user)
2020-12-01 19:54:16 +00:00
if not mempool:
2020-12-01 21:39:33 +00:00
mempool = await create_mempool(user=g.wallet.user)
2021-03-31 23:49:36 +01:00
return jsonify(mempool._asdict()), HTTPStatus.OK