2024-10-29 09:58:22 +01:00
|
|
|
from http import HTTPStatus
|
2024-03-28 08:59:28 +01:00
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
from fastapi import (
|
|
|
|
APIRouter,
|
|
|
|
Body,
|
|
|
|
Depends,
|
2024-10-29 09:58:22 +01:00
|
|
|
HTTPException,
|
2024-03-28 08:59:28 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
from lnbits.core.models import (
|
|
|
|
CreateWallet,
|
2024-05-13 16:26:25 +02:00
|
|
|
KeyType,
|
2024-03-28 08:59:28 +01:00
|
|
|
Wallet,
|
|
|
|
)
|
|
|
|
from lnbits.decorators import (
|
|
|
|
WalletTypeInfo,
|
|
|
|
require_admin_key,
|
2024-09-24 10:56:34 +02:00
|
|
|
require_invoice_key,
|
2024-03-28 08:59:28 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
from ..crud import (
|
|
|
|
create_wallet,
|
|
|
|
delete_wallet,
|
2024-10-29 09:58:22 +01:00
|
|
|
get_wallet,
|
2024-03-28 08:59:28 +01:00
|
|
|
update_wallet,
|
|
|
|
)
|
|
|
|
|
|
|
|
wallet_router = APIRouter(prefix="/api/v1/wallet", tags=["Wallet"])
|
|
|
|
|
|
|
|
|
|
|
|
@wallet_router.get("")
|
2024-10-29 09:58:22 +01:00
|
|
|
async def api_wallet(key_info: WalletTypeInfo = Depends(require_invoice_key)):
|
2024-09-24 10:56:34 +02:00
|
|
|
res = {
|
2024-10-29 09:58:22 +01:00
|
|
|
"name": key_info.wallet.name,
|
|
|
|
"balance": key_info.wallet.balance_msat,
|
2024-09-24 10:56:34 +02:00
|
|
|
}
|
2024-10-29 09:58:22 +01:00
|
|
|
if key_info.key_type == KeyType.admin:
|
|
|
|
res["id"] = key_info.wallet.id
|
2024-09-24 10:56:34 +02:00
|
|
|
return res
|
2024-03-28 08:59:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
@wallet_router.put("/{new_name}")
|
|
|
|
async def api_update_wallet_name(
|
2024-10-29 09:58:22 +01:00
|
|
|
new_name: str, key_info: WalletTypeInfo = Depends(require_admin_key)
|
2024-03-28 08:59:28 +01:00
|
|
|
):
|
2024-10-29 09:58:22 +01:00
|
|
|
wallet = await get_wallet(key_info.wallet.id)
|
|
|
|
if not wallet:
|
|
|
|
raise HTTPException(status_code=HTTPStatus.NOT_FOUND, detail="Wallet not found")
|
|
|
|
wallet.name = new_name
|
|
|
|
await update_wallet(wallet)
|
2024-03-28 08:59:28 +01:00
|
|
|
return {
|
2024-10-29 09:58:22 +01:00
|
|
|
"id": wallet.id,
|
|
|
|
"name": wallet.name,
|
|
|
|
"balance": wallet.balance_msat,
|
2024-03-28 08:59:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-10-29 09:58:22 +01:00
|
|
|
@wallet_router.patch("")
|
2024-03-28 08:59:28 +01:00
|
|
|
async def api_update_wallet(
|
|
|
|
name: Optional[str] = Body(None),
|
|
|
|
currency: Optional[str] = Body(None),
|
2024-10-29 09:58:22 +01:00
|
|
|
key_info: WalletTypeInfo = Depends(require_admin_key),
|
|
|
|
) -> Wallet:
|
|
|
|
wallet = await get_wallet(key_info.wallet.id)
|
|
|
|
if not wallet:
|
|
|
|
raise HTTPException(status_code=HTTPStatus.NOT_FOUND, detail="Wallet not found")
|
|
|
|
wallet.name = name or wallet.name
|
|
|
|
wallet.currency = currency if currency is not None else wallet.currency
|
|
|
|
await update_wallet(wallet)
|
|
|
|
return wallet
|
2024-03-28 08:59:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
@wallet_router.delete("")
|
|
|
|
async def api_delete_wallet(
|
|
|
|
wallet: WalletTypeInfo = Depends(require_admin_key),
|
|
|
|
) -> None:
|
|
|
|
await delete_wallet(
|
|
|
|
user_id=wallet.wallet.user,
|
|
|
|
wallet_id=wallet.wallet.id,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2024-10-29 09:58:22 +01:00
|
|
|
@wallet_router.post("")
|
2024-03-28 08:59:28 +01:00
|
|
|
async def api_create_wallet(
|
|
|
|
data: CreateWallet,
|
2024-10-29 09:58:22 +01:00
|
|
|
key_info: WalletTypeInfo = Depends(require_admin_key),
|
2024-03-28 08:59:28 +01:00
|
|
|
) -> Wallet:
|
2024-10-29 09:58:22 +01:00
|
|
|
return await create_wallet(user_id=key_info.wallet.user, wallet_name=data.name)
|