Skip to content

Commit 720aa69

Browse files
authored
fix: revert withdraw to using bech32 lnurl field (#65)
1 parent d0689b7 commit 720aa69

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

models.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ class WithdrawLink(BaseModel):
3737
webhook_body: str = Query(None)
3838
custom_url: str = Query(None)
3939
created_at: datetime
40+
lnurl: str | None = Field(
41+
default=None,
42+
no_database=True,
43+
deprecated=True,
44+
description=(
45+
"Deprecated: Instead of using this bech32 encoded string, dynamically "
46+
"generate your own static link (lud17/bech32) on the client side. "
47+
"Example: lnurlw://${window.location.hostname}/lnurlw/${id}"
48+
),
49+
)
4050

4151
@property
4252
def is_spent(self) -> bool:

views_api.py

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import json
22
from http import HTTPStatus
33

4-
from fastapi import APIRouter, Depends, HTTPException, Query
4+
from fastapi import APIRouter, Depends, HTTPException, Query, Request
55
from lnbits.core.crud import get_user
66
from lnbits.core.models import SimpleStatus, WalletTypeInfo
77
from lnbits.decorators import require_admin_key, require_invoice_key
@@ -14,13 +14,15 @@
1414
get_withdraw_links,
1515
update_withdraw_link,
1616
)
17+
from .helpers import create_lnurl
1718
from .models import CreateWithdrawData, HashCheck, PaginatedWithdraws, WithdrawLink
1819

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

2122

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

35-
return await get_withdraw_links(wallet_ids, limit, offset)
37+
links = await get_withdraw_links(wallet_ids, limit, offset)
38+
39+
for linkk in links.data:
40+
try:
41+
lnurl = create_lnurl(linkk, request)
42+
except ValueError as exc:
43+
raise HTTPException(
44+
status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
45+
detail=str(exc),
46+
) from exc
47+
linkk.lnurl = str(lnurl.bech32)
48+
49+
return links
3650

3751

3852
@withdraw_ext_api.get("/links/{link_id}", status_code=HTTPStatus.OK)
3953
async def api_link_retrieve(
40-
link_id: str, key_info: WalletTypeInfo = Depends(require_invoice_key)
54+
request: Request,
55+
link_id: str,
56+
key_info: WalletTypeInfo = Depends(require_invoice_key),
4157
) -> WithdrawLink:
4258
link = await get_withdraw_link(link_id, 0)
4359

@@ -50,12 +66,22 @@ async def api_link_retrieve(
5066
raise HTTPException(
5167
detail="Not your withdraw link.", status_code=HTTPStatus.FORBIDDEN
5268
)
69+
70+
try:
71+
lnurl = create_lnurl(link, request)
72+
except ValueError as exc:
73+
raise HTTPException(
74+
status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
75+
detail=str(exc),
76+
) from exc
77+
link.lnurl = str(lnurl.bech32)
5378
return link
5479

5580

5681
@withdraw_ext_api.post("/links", status_code=HTTPStatus.CREATED)
5782
@withdraw_ext_api.put("/links/{link_id}")
5883
async def api_link_create_or_update(
84+
request: Request,
5985
data: CreateWithdrawData,
6086
link_id: str | None = None,
6187
key_info: WalletTypeInfo = Depends(require_admin_key),
@@ -131,6 +157,15 @@ async def api_link_create_or_update(
131157
link = await update_withdraw_link(link)
132158
else:
133159
link = await create_withdraw_link(wallet_id=key_info.wallet.id, data=data)
160+
try:
161+
lnurl = create_lnurl(link, request)
162+
except ValueError as exc:
163+
raise HTTPException(
164+
status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
165+
detail=str(exc),
166+
) from exc
167+
168+
link.lnurl = str(lnurl.bech32)
134169

135170
return link
136171

0 commit comments

Comments
 (0)