Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ class WithdrawLink(BaseModel):
webhook_body: str = Query(None)
custom_url: str = Query(None)
created_at: datetime
lnurl: str | None = Field(
default=None,
no_database=True,
deprecated=True,
description=(
"Deprecated: Instead of using this bech32 encoded string, dynamically "
"generate your own static link (lud17/bech32) on the client side. "
"Example: lnurlw://${window.location.hostname}/lnurlw/${id}"
),
)

@property
def is_spent(self) -> bool:
Expand Down
41 changes: 38 additions & 3 deletions views_api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
from http import HTTPStatus

from fastapi import APIRouter, Depends, HTTPException, Query
from fastapi import APIRouter, Depends, HTTPException, Query, Request
from lnbits.core.crud import get_user
from lnbits.core.models import SimpleStatus, WalletTypeInfo
from lnbits.decorators import require_admin_key, require_invoice_key
Expand All @@ -14,13 +14,15 @@
get_withdraw_links,
update_withdraw_link,
)
from .helpers import create_lnurl
from .models import CreateWithdrawData, HashCheck, PaginatedWithdraws, WithdrawLink

withdraw_ext_api = APIRouter(prefix="/api/v1")


@withdraw_ext_api.get("/links", status_code=HTTPStatus.OK)
async def api_links(
request: Request,
key_info: WalletTypeInfo = Depends(require_invoice_key),
all_wallets: bool = Query(False),
offset: int = Query(0),
Expand All @@ -32,12 +34,26 @@ async def api_links(
user = await get_user(key_info.wallet.user)
wallet_ids = user.wallet_ids if user else []

return await get_withdraw_links(wallet_ids, limit, offset)
links = await get_withdraw_links(wallet_ids, limit, offset)

for linkk in links.data:
try:
lnurl = create_lnurl(linkk, request)
except ValueError as exc:
raise HTTPException(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why catch and raise?

If a link is broken then all the others will be inaccessible.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think its fine, if 1 fails all fail, ValueError is raised if there is a problem with HTTPS and generateding a lnurl

status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
detail=str(exc),
) from exc
linkk.lnurl = str(lnurl.bech32)

return links


@withdraw_ext_api.get("/links/{link_id}", status_code=HTTPStatus.OK)
async def api_link_retrieve(
link_id: str, key_info: WalletTypeInfo = Depends(require_invoice_key)
request: Request,
link_id: str,
key_info: WalletTypeInfo = Depends(require_invoice_key),
) -> WithdrawLink:
link = await get_withdraw_link(link_id, 0)

Expand All @@ -50,12 +66,22 @@ async def api_link_retrieve(
raise HTTPException(
detail="Not your withdraw link.", status_code=HTTPStatus.FORBIDDEN
)

try:
lnurl = create_lnurl(link, request)
except ValueError as exc:
raise HTTPException(
status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
detail=str(exc),
) from exc
link.lnurl = str(lnurl.bech32)
return link


@withdraw_ext_api.post("/links", status_code=HTTPStatus.CREATED)
@withdraw_ext_api.put("/links/{link_id}")
async def api_link_create_or_update(
request: Request,
data: CreateWithdrawData,
link_id: str | None = None,
key_info: WalletTypeInfo = Depends(require_admin_key),
Expand Down Expand Up @@ -131,6 +157,15 @@ async def api_link_create_or_update(
link = await update_withdraw_link(link)
else:
link = await create_withdraw_link(wallet_id=key_info.wallet.id, data=data)
try:
lnurl = create_lnurl(link, request)
except ValueError as exc:
raise HTTPException(
status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
detail=str(exc),
) from exc

link.lnurl = str(lnurl.bech32)

return link

Expand Down