forked from lnbits/myextension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews_api.py
574 lines (479 loc) · 20.2 KB
/
views_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
from http import HTTPStatus
from fastapi import APIRouter, Depends, Query, Request, HTTPException
from fastapi.responses import Response
from lnbits.core.crud import get_user
from lnbits.core.models import User
from lnbits.decorators import WalletTypeInfo, check_user_exists
from lnbits.core.services import create_invoice, pay_invoice
from lnbits.extensions.lnurlp.crud import get_pay_links, get_pay_link
from lnbits.bolt11 import decode as decode_bolt11
from loguru import logger
from typing import Optional
import shortuuid
from lnbits.decorators import get_key_type, require_admin_key, require_invoice_key
from lnbits.helpers import urlsafe_short_hash
from lnurl import encode as lnurl_encode
from .crud import (
create_lnurluniversal,
delete_lnurluniversal,
get_lnurluniversal,
get_lnurluniversals,
update_lnurluniversal,
get_lnurluniversal_balance,
get_universal_comments,
db
)
from .models import CreateLnurlUniversalData, LnurlUniversal
from .utils import get_withdraw_link_info
import time
import logging
lnurluniversal_api_router = APIRouter()
logging.basicConfig(level=logging.INFO)
#######################################
##### ADD YOUR API ENDPOINTS HERE #####
#######################################
## Get all the records belonging to the user
@lnurluniversal_api_router.get("/api/v1/myex/lnurlp_links")
async def api_get_lnurlp_links(wallet: WalletTypeInfo = Depends(get_key_type)):
try:
pay_links = await get_pay_links(wallet_ids=[wallet.wallet.id])
formatted_links = [
{
"id": link.id,
"description": link.description,
"amount": f"{link.min} - {link.max} sats" if link.min != link.max else f"{link.min} sats",
"lnurl": link.lnurl
}
for link in pay_links
]
return {"pay_links": formatted_links}
except Exception as e:
print(f"Error in api_get_lnurlp_links: {str(e)}")
raise HTTPException(
status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
detail=f"Error fetching LNURL Pay links: {str(e)}"
)
@lnurluniversal_api_router.get("/api/v1/lnurluniversal/withdraw/{withdraw_id}")
async def api_get_withdraw_link(withdraw_id: str, user: User = Depends(check_user_exists)):
withdraw_info = await get_withdraw_link_info(withdraw_id)
if "error" in withdraw_info:
raise HTTPException(status_code=404, detail=withdraw_info["error"])
return withdraw_info
@lnurluniversal_api_router.get("/api/v1/myex", status_code=HTTPStatus.OK)
async def api_lnurluniversals(
all_wallets: bool = Query(False),
wallet: WalletTypeInfo = Depends(get_key_type),
):
wallet_ids = [wallet.wallet.id]
if all_wallets:
user = await get_user(wallet.wallet.user)
wallet_ids = user.wallet_ids if user else []
records = await get_lnurluniversals(wallet_ids)
result = []
for record in records:
# Get comment count for each record
comment_count = await db.fetchone(
"SELECT COUNT(*) as count FROM invoice_comments WHERE universal_id = ?",
(record.id,)
)
data = record.dict()
data['comment_count'] = comment_count['count'] if comment_count else 0
result.append(data)
return result
@lnurluniversal_api_router.get("/api/v1/balance/{lnurluniversal_id}")
async def api_get_balance(lnurluniversal_id: str) -> dict:
balance = await get_lnurluniversal_balance(lnurluniversal_id)
if balance is None:
raise HTTPException(status_code=404, detail="LnurlUniversal not found")
return {"balance": balance}
@lnurluniversal_api_router.get("/api/v1/lnurl/{lnurluniversal_id}")
async def api_get_lnurl(request: Request, lnurluniversal_id: str):
# Just construct the URL directly
base_url = str(request.base_url).rstrip('/')
redirect_url = f"{base_url}/lnurluniversal/api/v1/redirect/{lnurluniversal_id}"
logging.info(f"Redirect URL before encoding: {redirect_url}")
encoded_url = "lightning:" + lnurl_encode(redirect_url)
logging.info(f"EncodedURL: {encoded_url}")
return Response(content=encoded_url, media_type="text/plain")
## Get a single record
#@lnurluniversal_api_router.get(
# "/api/v1/myex/{lnurluniversal_id}",
# status_code=HTTPStatus.OK,
# dependencies=[Depends(require_invoice_key)],
#)
#async def api_lnurluniversal(lnurluniversal_id: str):
# lnurluniversal = await get_lnurluniversal(lnurluniversal_id)
# if not lnurluniversal:
# raise HTTPException(
# status_code=HTTPStatus.NOT_FOUND, detail="LnurlUniversal does not exist."
# )
# return lnurluniversal.dict()
@lnurluniversal_api_router.get("/api/v1/qr/{lnurluniversal_id}")
async def api_get_qr_data(request: Request, lnurluniversal_id: str):
lnurluniversal = await get_lnurluniversal(lnurluniversal_id)
if not lnurluniversal:
raise HTTPException(status_code=404, detail="Record not found")
# Generate the URL to our redirect endpoint
redirect_url = str(request.url_for(
"lnurluniversal.api_lnurluniversal_redirect",
lnurluniversal_id=lnurluniversal_id
))
# Encode it as LNURL and add lightning: prefix
encoded_url = "lightning:" + lnurl_encode(redirect_url)
return Response(content=encoded_url, media_type="text/plain")
#@lnurluniversal_api_router.get(
# "/api/v1/redirect/{lnurluniversal_id}",
# name="lnurluniversal.api_lnurluniversal_redirect"
#)
@lnurluniversal_api_router.get("/api/v1/redirect/{lnurluniversal_id}")
async def api_lnurluniversal_redirect(request: Request, lnurluniversal_id: str):
logging.info(f"Redirect request for id: {lnurluniversal_id}")
lnurluniversal = await get_lnurluniversal(lnurluniversal_id)
if not lnurluniversal:
raise HTTPException(status_code=404, detail="Record not found")
# First check balance
universal_balance = await get_lnurluniversal_balance(lnurluniversal_id)
universal_balance_sats = universal_balance // 1000 # Convert to sats for comparison
logging.info(f"Universal balance: {universal_balance} msats ({universal_balance_sats} sats)")
# Check actual wallet balance
from lnbits.core.crud import get_wallet
wallet = await get_wallet(lnurluniversal.wallet)
actual_balance = wallet.balance_msat // 1000 # Convert to sats
logging.info(f"LNbits wallet balance: {actual_balance} sats")
# If wallet balance is less than 60 sats AND no universal balance, force payment mode
if actual_balance < 60 and universal_balance == 0:
logging.info("Wallet balance below 60 sats and no universal balance, switching to payment mode")
lnurluniversal.state = "payment"
await update_lnurluniversal(lnurluniversal)
# Handle payment case
pay_link = await get_pay_link(lnurluniversal.selectedLnurlp)
if not pay_link:
raise HTTPException(status_code=404, detail="Payment link not found")
callback_url = str(request.url_for(
"lnurluniversal.api_lnurl_callback",
lnurluniversal_id=lnurluniversal_id
))
return {
"tag": "payRequest",
"callback": callback_url,
"minSendable": pay_link.min * 1000,
"maxSendable": pay_link.max * 1000,
"metadata": f'[["text/plain", "{pay_link.description}"]]'
}
# Continue with normal state handling
if lnurluniversal.state == "withdraw":
logging.info("Processing withdraw request")
callback_url = str(request.url_for(
"lnurluniversal.api_withdraw_callback",
lnurluniversal_id=lnurluniversal_id
))
# Calculate how much can be withdrawn
if actual_balance >= (universal_balance_sats + 50):
# Plenty of balance, allow full universal balance withdrawal
max_withdrawable = universal_balance # Already in msats
logging.info(f"Allowing full universal balance withdrawal: {universal_balance_sats} sats")
elif actual_balance >= universal_balance_sats:
# Can cover universal balance but will go below reserve, allow it anyway
max_withdrawable = universal_balance # Already in msats
logging.info(f"Allowing full universal balance withdrawal (below reserve): {universal_balance_sats} sats")
else:
# Not enough in wallet to cover universal balance
logging.info(f"Not enough in wallet ({actual_balance} sats) to cover withdrawal of {universal_balance_sats} sats")
# Switch to payment mode
lnurluniversal.state = "payment"
await update_lnurluniversal(lnurluniversal)
pay_link = await get_pay_link(lnurluniversal.selectedLnurlp)
if not pay_link:
raise HTTPException(status_code=404, detail="Payment link not found")
return {
"tag": "payRequest",
"callback": callback_url,
"minSendable": pay_link.min * 1000,
"maxSendable": pay_link.max * 1000,
"metadata": f'[["text/plain", "{pay_link.description}"]]'
}
return {
"tag": "withdrawRequest",
"callback": callback_url,
"k1": urlsafe_short_hash(),
"minWithdrawable": 1000, # 1 sat minimum
"maxWithdrawable": max_withdrawable,
"defaultDescription": f"Withdraw from {lnurluniversal.name}"
}
else:
# Handle regular payment case
pay_link = await get_pay_link(lnurluniversal.selectedLnurlp)
if not pay_link:
raise HTTPException(status_code=404, detail="Payment link not found")
callback_url = str(request.url_for(
"lnurluniversal.api_lnurl_callback",
lnurluniversal_id=lnurluniversal_id
))
return {
"tag": "payRequest",
"callback": callback_url,
"minSendable": pay_link.min * 1000,
"maxSendable": pay_link.max * 1000,
"metadata": f'[["text/plain", "{pay_link.description}"]]'
}
@lnurluniversal_api_router.get(
"/api/v1/lnurl/cb/{lnurluniversal_id}",
name="lnurluniversal.api_lnurl_callback"
)
@lnurluniversal_api_router.get(
"/api/v1/lnurl/cb/{lnurluniversal_id}",
name="lnurluniversal.api_lnurl_callback"
)
async def api_lnurl_callback(
request: Request,
lnurluniversal_id: str,
amount: int = Query(...),
comment: Optional[str] = Query(None)
):
lnurluniversal = await get_lnurluniversal(lnurluniversal_id)
if not lnurluniversal:
raise HTTPException(status_code=404, detail="Record not found")
pay_link = await get_pay_link(lnurluniversal.selectedLnurlp)
if not pay_link:
raise HTTPException(status_code=404, detail="Payment link not found")
if comment:
comment_id = urlsafe_short_hash()
await db.execute(
"""
INSERT INTO lnurluniversal.invoice_comments
(id, universal_id, comment, timestamp, amount)
VALUES (?, ?, ?, ?, ?)
""",
(
comment_id,
lnurluniversal_id,
comment,
int(time.time()),
amount
)
)
payment_hash, payment_request = await create_invoice(
wallet_id=pay_link.wallet,
amount=int(amount / 1000),
memo=f"{pay_link.description}{' - ' + comment if comment else ''}",
extra={
"tag": "ext_lnurluniversal",
"universal_id": lnurluniversal_id,
"selectedLnurlp": lnurluniversal.selectedLnurlp,
"link": pay_link.id,
"comment": comment if comment else None
}
)
new_total = lnurluniversal.total + amount
lnurluniversal.total = new_total
lnurluniversal.state = "withdraw" if new_total > 0 else "payment"
await update_lnurluniversal(lnurluniversal)
current_balance = await get_lnurluniversal_balance(lnurluniversal_id)
return {
"pr": payment_request,
"successAction": {
"tag": "message",
"message": f"Payment received. Current balance: {current_balance / 1000} sats"
},
"routes": [],
"balance": current_balance # Add this line to include the balance in the response
}
@lnurluniversal_api_router.get(
"/api/v1/lnurl/withdraw/cb/{lnurluniversal_id}",
name="lnurluniversal.api_withdraw_callback"
)
async def api_withdraw_callback(
request: Request,
lnurluniversal_id: str,
k1: str = Query(...),
pr: str = Query(...)
):
lnurluniversal = await get_lnurluniversal(lnurluniversal_id)
if not lnurluniversal:
raise HTTPException(status_code=404, detail="Record not found")
amount = decode_bolt11(pr).amount_msat // 1000 # Convert to sats
available_balance = await get_lnurluniversal_balance(lnurluniversal_id)
if amount > available_balance:
raise HTTPException(status_code=400, detail="Insufficient balance for withdrawal")
withdraw_id = urlsafe_short_hash()
await db.execute(
"""
INSERT INTO pending_withdrawals (id, universal_id, amount, created_time, payment_request)
VALUES (?, ?, ?, ?, ?)
""",
(withdraw_id, lnurluniversal_id, amount, int(time.time()), pr)
)
try:
payment_hash = await pay_invoice(
wallet_id=lnurluniversal.wallet,
payment_request=pr,
extra={
"tag": "ext_lnurluniversal",
"lnurlwithdraw": True,
"universal_id": lnurluniversal_id,
"selectedLnurlw": lnurluniversal.selectedLnurlw,
"withdraw_id": withdraw_id
}
)
# Withdrawal processed
await db.execute(
"""
UPDATE pending_withdrawals
SET status = 'completed'
WHERE payment_request = ?
""",
(pr,)
)
if amount >= lnurluniversal.total // 1000:
lnurluniversal.uses += 1
new_total = max(0, lnurluniversal.total - (amount * 1000))
lnurluniversal.total = new_total
if new_total == 0:
lnurluniversal.state = "payment"
await update_lnurluniversal(lnurluniversal)
return {"status": "OK"}
except Exception as e:
await db.execute(
"""
UPDATE pending_withdrawals
SET status = 'failed'
WHERE payment_request = ?
""",
(pr,)
)
raise HTTPException(status_code=500, detail=str(e))
# Metadata endpoint
@lnurluniversal_api_router.get("/api/v1/lnurl/{lnurluniversal_id}")
async def api_lnurl_response(request: Request, lnurluniversal_id: str):
lnurluniversal = await get_lnurluniversal(lnurluniversal_id)
if not lnurluniversal:
raise HTTPException(status_code=404, detail="Record not found")
if lnurluniversal.state == "payment":
pay_link = await get_pay_link(lnurluniversal.selectedLnurlp)
if not pay_link:
raise HTTPException(status_code=404, detail="Payment link not found")
# Generate callback URL for this endpoint
callback_url = str(request.url_for(
"lnurluniversal.api_lnurl_callback",
lnurluniversal_id=lnurluniversal_id
))
# Return metadata format like LNURLP
return {
"callback": callback_url,
"maxSendable": pay_link.max * 1000,
"minSendable": pay_link.min * 1000,
"metadata": pay_link.lnurlpay_metadata,
"tag": "payRequest",
"commentAllowed": pay_link.comment_chars if pay_link.comment_chars > 0 else None
}
@lnurluniversal_api_router.put("/api/v1/myex/{lnurluniversal_id}")
async def api_lnurluniversal_update(
data: CreateLnurlUniversalData,
lnurluniversal_id: str,
wallet: WalletTypeInfo = Depends(get_key_type),
) -> LnurlUniversal:
if not lnurluniversal_id:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="LnurlUniversal does not exist."
)
lnurluniversal = await get_lnurluniversal(lnurluniversal_id)
assert lnurluniversal, "LnurlUniversal couldn't be retrieved"
if wallet.wallet.id != lnurluniversal.wallet:
raise HTTPException(
status_code=HTTPStatus.FORBIDDEN, detail="Not your LnurlUniversal."
)
# Update only the fields that exist in the new model
lnurluniversal.name = data.name
lnurluniversal.lnurlwithdrawamount = data.lnurlwithdrawamount
lnurluniversal.selectedLnurlp = data.selectedLnurlp
lnurluniversal.selectedLnurlw = data.selectedLnurlw
return await update_lnurluniversal(lnurluniversal)
## Create a new record
@lnurluniversal_api_router.post("/api/v1/myex", status_code=HTTPStatus.CREATED)
async def api_lnurluniversal_create(
request: Request,
data: CreateLnurlUniversalData,
key_type: WalletTypeInfo = Depends(require_admin_key),
) -> LnurlUniversal:
try:
lnurluniversal_id = urlsafe_short_hash()
logger.info(f"Generated lnurluniversal_id: {lnurluniversal_id}")
data.wallet = data.wallet or key_type.wallet.id
myext = LnurlUniversal(
id=lnurluniversal_id,
name=data.name,
wallet=data.wallet,
lnurlwithdrawamount=data.lnurlwithdrawamount,
selectedLnurlp=data.selectedLnurlp,
selectedLnurlw=data.selectedLnurlw,
state="payment", # Always initialize state to "payment"
total=0, # Initialize total to 0
uses=0 # Initialize uses to 0
)
logger.info(f"Creating LnurlUniversal with data: {myext}")
created_lnurluniversal = await create_lnurluniversal(myext)
logger.info(f"Created LnurlUniversal: {created_lnurluniversal}")
# Fetch the created LnurlUniversal to ensure all fields are populated
fetched_lnurluniversal = await get_lnurluniversal(created_lnurluniversal.id)
logger.info(f"Fetched LnurlUniversal after creation: {fetched_lnurluniversal}")
return fetched_lnurluniversal
except Exception as e:
logger.error(f"Error creating LnurlUniversal: {str(e)}")
raise HTTPException(status_code=500, detail=f"Error creating LnurlUniversal: {str(e)}")
## Delete a record
@lnurluniversal_api_router.delete("/api/v1/myex/{lnurluniversal_id}")
async def api_lnurluniversal_delete(
lnurluniversal_id: str, wallet: WalletTypeInfo = Depends(require_admin_key)
):
lnurluniversal = await get_lnurluniversal(lnurluniversal_id)
if not lnurluniversal:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="LnurlUniversal does not exist."
)
if lnurluniversal.wallet != wallet.wallet.id:
raise HTTPException(
status_code=HTTPStatus.FORBIDDEN, detail="Not your LnurlUniversal."
)
await delete_lnurluniversal(lnurluniversal_id)
return "", HTTPStatus.NO_CONTENT
# ANY OTHER ENDPOINTS YOU NEED
## This endpoint creates a payment
@lnurluniversal_api_router.post(
"/api/v1/myex/payment/{lnurluniversal_id}", status_code=HTTPStatus.CREATED
)
async def api_lnurluniversal_create_invoice(
lnurluniversal_id: str, amount: int = Query(..., ge=1), memo: str = ""
) -> dict:
lnurluniversal = await get_lnurluniversal(lnurluniversal_id)
if not lnurluniversal:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="LnurlUniversal does not exist."
)
# we create a payment and add some tags,
# so tasks.py can grab the payment once its paid
try:
payment_hash, payment_request = await create_invoice(
wallet_id=lnurluniversal.wallet,
amount=amount,
memo=f"{memo} to {lnurluniversal.name}" if memo else f"{lnurluniversal.name}",
extra={
"tag": "lnurluniversal",
"amount": amount,
},
)
except Exception as exc:
raise HTTPException(
status_code=HTTPStatus.INTERNAL_SERVER_ERROR, detail=str(exc)
) from exc
return {"payment_hash": payment_hash, "payment_request": payment_request}
@lnurluniversal_api_router.get("/api/v1/comments/{universal_id}")
async def api_get_comments(
universal_id: str
) -> list[dict]:
"""Get comments for a universal"""
universal = await get_lnurluniversal(universal_id)
if not universal:
raise HTTPException(status_code=404, detail="Universal not found")
comments = await get_universal_comments(universal_id)
return comments
# LNURL-specific routes