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

127 lines
3.5 KiB
Python
Raw Normal View History

2021-10-14 11:45:30 +01:00
from http import HTTPStatus
2021-10-14 22:30:47 +01:00
from fastapi import Query
from fastapi.params import Depends
from starlette.exceptions import HTTPException
2021-10-14 11:45:30 +01:00
2021-10-20 12:03:11 +01:00
from lnbits.decorators import WalletTypeInfo, get_key_type, require_admin_key
2021-10-14 11:45:30 +01:00
from lnbits.extensions.watchonly import watchonly_ext
2021-10-18 10:58:09 +01:00
2021-10-20 12:03:11 +01:00
from .crud import (
create_mempool,
create_watch_wallet,
delete_watch_wallet,
get_addresses,
get_fresh_address,
get_mempool,
get_watch_wallet,
get_watch_wallets,
update_mempool,
)
2021-10-18 10:58:09 +01:00
from .models import CreateWallet
2021-10-14 11:45:30 +01:00
###################WALLETS#############################
2021-10-14 22:30:47 +01:00
@watchonly_ext.get("/api/v1/wallet")
async def api_wallets_retrieve(wallet: WalletTypeInfo = Depends(get_key_type)):
2021-10-14 11:45:30 +01:00
try:
2021-10-14 22:30:47 +01:00
return [wallet.dict() for wallet in await get_watch_wallets(wallet.wallet.user)]
2021-10-14 11:45:30 +01:00
except:
return ""
2021-10-14 22:30:47 +01:00
@watchonly_ext.get("/api/v1/wallet/{wallet_id}")
2021-10-17 18:33:29 +01:00
async def api_wallet_retrieve(
wallet_id, wallet: WalletTypeInfo = Depends(get_key_type)
):
2021-10-14 22:30:47 +01:00
w_wallet = await get_watch_wallet(wallet_id)
2021-10-14 11:45:30 +01:00
2021-10-14 22:30:47 +01:00
if not w_wallet:
raise HTTPException(
2021-10-17 18:33:29 +01:00
status_code=HTTPStatus.NOT_FOUND, detail="Wallet does not exist."
2021-10-14 22:30:47 +01:00
)
2021-10-14 11:45:30 +01:00
2021-10-14 22:30:47 +01:00
return w_wallet.dict()
2021-10-14 11:45:30 +01:00
2021-10-14 22:30:47 +01:00
@watchonly_ext.post("/api/v1/wallet")
2021-10-17 18:33:29 +01:00
async def api_wallet_create_or_update(
2021-10-20 12:03:11 +01:00
data: CreateWallet, wallet_id=None, w: WalletTypeInfo = Depends(require_admin_key)
2021-10-17 18:33:29 +01:00
):
2021-10-14 11:45:30 +01:00
try:
wallet = await create_watch_wallet(
2021-10-14 22:30:47 +01:00
user=w.wallet.user, masterpub=data.masterpub, title=data.title
2021-10-14 11:45:30 +01:00
)
except Exception as e:
2021-10-17 18:33:29 +01:00
raise HTTPException(status_code=HTTPStatus.BAD_REQUEST, detail=str(e))
2021-10-14 22:30:47 +01:00
mempool = await get_mempool(w.wallet.user)
2021-10-14 11:45:30 +01:00
if not mempool:
2021-10-14 22:30:47 +01:00
create_mempool(user=w.wallet.user)
return wallet.dict()
2021-10-14 11:45:30 +01:00
2021-10-14 22:30:47 +01:00
@watchonly_ext.delete("/api/v1/wallet/{wallet_id}")
2021-10-20 12:03:11 +01:00
async def api_wallet_delete(wallet_id, w: WalletTypeInfo = Depends(require_admin_key)):
2021-10-14 11:45:30 +01:00
wallet = await get_watch_wallet(wallet_id)
if not wallet:
2021-10-14 22:30:47 +01:00
raise HTTPException(
2021-10-17 18:33:29 +01:00
status_code=HTTPStatus.NOT_FOUND, detail="Wallet does not exist."
2021-10-14 22:30:47 +01:00
)
2021-10-14 11:45:30 +01:00
await delete_watch_wallet(wallet_id)
2021-10-14 22:30:47 +01:00
raise HTTPException(status_code=HTTPStatus.NO_CONTENT)
2021-10-14 11:45:30 +01:00
#############################ADDRESSES##########################
2021-10-14 22:30:47 +01:00
@watchonly_ext.get("/api/v1/address/{wallet_id}")
async def api_fresh_address(wallet_id, w: WalletTypeInfo = Depends(get_key_type)):
2021-10-14 11:45:30 +01:00
await get_fresh_address(wallet_id)
addresses = await get_addresses(wallet_id)
2021-10-14 22:30:47 +01:00
return [address.dict() for address in addresses]
2021-10-14 11:45:30 +01:00
2021-10-14 22:30:47 +01:00
@watchonly_ext.get("/api/v1/addresses/{wallet_id}")
async def api_get_addresses(wallet_id, w: WalletTypeInfo = Depends(get_key_type)):
2021-10-14 11:45:30 +01:00
wallet = await get_watch_wallet(wallet_id)
if not wallet:
2021-10-14 22:30:47 +01:00
raise HTTPException(
2021-10-17 18:33:29 +01:00
status_code=HTTPStatus.NOT_FOUND, detail="Wallet does not exist."
2021-10-14 22:30:47 +01:00
)
2021-10-14 11:45:30 +01:00
addresses = await get_addresses(wallet_id)
if not addresses:
await get_fresh_address(wallet_id)
addresses = await get_addresses(wallet_id)
2021-10-14 22:30:47 +01:00
return [address.dict() for address in addresses]
2021-10-14 11:45:30 +01:00
#############################MEMPOOL##########################
2021-10-14 22:30:47 +01:00
@watchonly_ext.put("/api/v1/mempool")
2021-10-17 18:33:29 +01:00
async def api_update_mempool(
2021-10-20 12:03:11 +01:00
endpoint: str = Query(...), w: WalletTypeInfo = Depends(require_admin_key)
2021-10-17 18:33:29 +01:00
):
2021-10-14 22:30:47 +01:00
mempool = await update_mempool(endpoint, user=w.wallet.user)
return mempool.dict()
2021-10-14 11:45:30 +01:00
2021-10-14 22:30:47 +01:00
@watchonly_ext.get("/api/v1/mempool")
2021-10-20 12:03:11 +01:00
async def api_get_mempool(w: WalletTypeInfo = Depends(require_admin_key)):
2021-10-14 22:30:47 +01:00
mempool = await get_mempool(w.wallet.user)
2021-10-14 11:45:30 +01:00
if not mempool:
2021-10-14 22:30:47 +01:00
mempool = await create_mempool(user=w.wallet.user)
return mempool.dict()