-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews_api.py
More file actions
543 lines (470 loc) · 17.3 KB
/
Copy pathviews_api.py
File metadata and controls
543 lines (470 loc) · 17.3 KB
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
from datetime import datetime, timezone
from http import HTTPStatus
from fastapi import APIRouter, Depends, HTTPException, Query
from lnbits.core.models import User
from lnbits.core.services.payments import create_invoice
from lnbits.decorators import check_user_exists
from lnbits.helpers import urlsafe_short_hash
from .crud import (
create_debit,
create_offer,
create_plan,
create_relay,
create_subscription,
delete_debit,
delete_offer,
delete_plan,
delete_relay,
delete_subscription,
get_debit,
get_debits,
get_enabled_relays,
get_offer,
get_offers,
get_or_create_node_key,
get_plan,
get_plans,
get_relays,
get_subscription,
get_subscriptions,
update_debit,
update_offer,
update_plan,
update_subscription,
)
from .models import (
CheckoutRequest,
CreateDebit,
CreateOffer,
CreatePlan,
CreateRelay,
CreateSubscription,
Debit,
Offer,
ParseNofferRequest,
PayRequest,
Plan,
Relay,
Subscription,
UpdateDebit,
UpdateOffer,
UpdatePlan,
UpdateRelay,
UpdateSubscription,
)
from .node import resolve_offer_amount
from .nostr import (
NDebit,
NOffer,
decode_ndebit,
decode_noffer,
encode_ndebit,
encode_noffer,
generate_keypair,
pubkey_from_privkey,
)
from .nostr.bech32 import PRICE_TYPE_FIXED, PRICE_TYPE_SPONTANEOUS
from .pay import PayOfferError, pay_offer
from .subscriptions import add_frequency, renew_subscription
clink_ext_api = APIRouter()
def _wallet_owned(wallet_id: str, user: User) -> None:
if wallet_id not in user.wallet_ids:
raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail="Not your wallet.")
async def _default_relay() -> str | None:
relays = await get_enabled_relays()
return relays[0].url if relays else None
@clink_ext_api.get("/api/v1/info", description="CLINK extension info")
async def api_info():
relays = await get_enabled_relays()
return {
"extension": "clink",
"listener": {"enabled": bool(relays)},
"relays": [r.url for r in relays],
}
# ---------------------------------------------------------------------------
# Relays
# ---------------------------------------------------------------------------
@clink_ext_api.get("/api/v1/relays", description="List configured relays")
async def api_relays(user: User = Depends(check_user_exists)):
return [r.dict() for r in await get_relays()]
@clink_ext_api.post("/api/v1/relays", description="Add a relay")
async def api_relay_create(data: CreateRelay, user: User = Depends(check_user_exists)):
url = data.url.strip().rstrip("/")
if not (url.startswith("wss://") or url.startswith("ws://")):
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST,
detail="Relay URL must start with ws:// or wss://",
)
existing = await get_relays()
if any(r.url == url for r in existing):
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST, detail="Relay already added."
)
relay = Relay(url=url, enabled=data.enabled)
await create_relay(relay)
return relay.dict()
@clink_ext_api.delete("/api/v1/relays/{relay_id}", description="Remove a relay")
async def api_relay_delete(relay_id: str, user: User = Depends(check_user_exists)):
await delete_relay(relay_id)
@clink_ext_api.put("/api/v1/relays/{relay_id}", description="Update a relay")
async def api_relay_update(
relay_id: str, data: UpdateRelay, user: User = Depends(check_user_exists)
):
from .crud import get_relay
from .crud import update_relay as _update_relay
relay = await get_relay(relay_id)
if not relay:
raise HTTPException(status_code=HTTPStatus.NOT_FOUND, detail="Relay not found.")
relay.enabled = data.enabled
return (await _update_relay(relay)).dict()
# ---------------------------------------------------------------------------
# Offers
# ---------------------------------------------------------------------------
@clink_ext_api.get("/api/v1/offers", description="List offers for a wallet")
async def api_offers(wallet: str = Query(...), user: User = Depends(check_user_exists)):
_wallet_owned(wallet, user)
return [o.dict() for o in await get_offers(wallet)]
@clink_ext_api.post("/api/v1/offers", description="Create a CLINK offer")
async def api_offer_create(data: CreateOffer, user: User = Depends(check_user_exists)):
_wallet_owned(data.wallet, user)
relay = data.relay or await _default_relay()
if not relay:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST,
detail="Add a relay first, or pass one explicitly.",
)
offer_id = urlsafe_short_hash()
privkey, _ = generate_keypair()
pubkey = pubkey_from_privkey(privkey)
price_type = PRICE_TYPE_FIXED if data.amount_msat else PRICE_TYPE_SPONTANEOUS
price = (data.amount_msat or 0) // 1000 if data.amount_msat else None
noffer = encode_noffer(
NOffer(
pubkey=pubkey,
relay=relay,
offer=offer_id,
price_type=price_type,
price=price,
)
)
offer = Offer(
id=offer_id,
wallet=data.wallet,
name=data.name,
amount_msat=data.amount_msat,
description=data.description,
relay=relay,
pubkey=pubkey,
privkey=privkey,
noffer=noffer,
)
await create_offer(offer)
return offer.dict()
@clink_ext_api.put("/api/v1/offers/{offer_id}", description="Update an offer")
async def api_offer_update(
offer_id: str, data: UpdateOffer, user: User = Depends(check_user_exists)
):
offer = await get_offer(offer_id)
if not offer:
raise HTTPException(status_code=HTTPStatus.NOT_FOUND, detail="Offer not found.")
_wallet_owned(offer.wallet, user)
offer.active = data.active
offer = await update_offer(offer)
return offer.dict()
@clink_ext_api.delete("/api/v1/offers/{offer_id}", description="Delete an offer")
async def api_offer_delete(offer_id: str, user: User = Depends(check_user_exists)):
offer = await get_offer(offer_id)
if not offer:
raise HTTPException(status_code=HTTPStatus.NOT_FOUND, detail="Offer not found.")
_wallet_owned(offer.wallet, user)
await delete_offer(offer_id)
# ---------------------------------------------------------------------------
# Debits
# ---------------------------------------------------------------------------
@clink_ext_api.get("/api/v1/debits", description="List debit pointers for a wallet")
async def api_debits(wallet: str = Query(...), user: User = Depends(check_user_exists)):
_wallet_owned(wallet, user)
return [d.dict() for d in await get_debits(wallet)]
@clink_ext_api.post("/api/v1/debits", description="Create a debit pointer")
async def api_debit_create(data: CreateDebit, user: User = Depends(check_user_exists)):
_wallet_owned(data.wallet, user)
relay = await _default_relay()
if not relay:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST,
detail="Add a relay first.",
)
node_key = await get_or_create_node_key(data.wallet)
debit_id = urlsafe_short_hash()
ndebit = encode_ndebit(
NDebit(pubkey=node_key.pubkey, relay=relay, pointer=debit_id)
)
service_pubkey = None
if data.rules:
try:
import json
rules = json.loads(data.rules)
if isinstance(rules, dict) and rules.get("allowed_pubkeys"):
service_pubkey = rules["allowed_pubkeys"][0]
except (ValueError, TypeError):
pass
debit = Debit(
id=debit_id,
wallet=data.wallet,
ndebit=ndebit,
service_pubkey=service_pubkey,
amount_msat=data.amount_msat,
frequency_number=data.frequency_number,
frequency_unit=data.frequency_unit,
budget_msat=data.budget_msat,
rules=data.rules,
state=data.state,
)
await create_debit(debit)
return debit.dict()
@clink_ext_api.put("/api/v1/debits/{debit_id}", description="Update a debit pointer")
async def api_debit_update(
debit_id: str, data: UpdateDebit, user: User = Depends(check_user_exists)
):
debit = await get_debit(debit_id)
if not debit:
raise HTTPException(status_code=HTTPStatus.NOT_FOUND, detail="Debit not found.")
_wallet_owned(debit.wallet, user)
debit.state = data.state
debit = await update_debit(debit)
return debit.dict()
@clink_ext_api.delete("/api/v1/debits/{debit_id}", description="Delete a debit pointer")
async def api_debit_delete(debit_id: str, user: User = Depends(check_user_exists)):
debit = await get_debit(debit_id)
if not debit:
raise HTTPException(status_code=HTTPStatus.NOT_FOUND, detail="Debit not found.")
_wallet_owned(debit.wallet, user)
await delete_debit(debit_id)
# ---------------------------------------------------------------------------
# Plans
# ---------------------------------------------------------------------------
@clink_ext_api.get("/api/v1/plans", description="List plans for a wallet")
async def api_plans(wallet: str = Query(...), user: User = Depends(check_user_exists)):
_wallet_owned(wallet, user)
return [p.dict() for p in await get_plans(wallet)]
@clink_ext_api.post("/api/v1/plans", description="Create a recurring plan")
async def api_plan_create(data: CreatePlan, user: User = Depends(check_user_exists)):
_wallet_owned(data.wallet, user)
if (data.amount_msat or 0) <= 0:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST, detail="Amount must be positive."
)
if data.frequency_unit not in ("day", "week", "month"):
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST,
detail="Frequency unit must be day, week or month.",
)
if data.frequency_number < 1:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST, detail="Frequency must be at least 1."
)
plan = Plan(
wallet=data.wallet,
name=data.name,
amount_msat=data.amount_msat,
frequency_number=data.frequency_number,
frequency_unit=data.frequency_unit,
description=data.description,
)
await create_plan(plan)
return plan.dict()
@clink_ext_api.put("/api/v1/plans/{plan_id}", description="Update a plan")
async def api_plan_update(
plan_id: str, data: UpdatePlan, user: User = Depends(check_user_exists)
):
plan = await get_plan(plan_id)
if not plan:
raise HTTPException(status_code=HTTPStatus.NOT_FOUND, detail="Plan not found.")
_wallet_owned(plan.wallet, user)
plan.active = data.active
plan = await update_plan(plan)
return plan.dict()
@clink_ext_api.delete("/api/v1/plans/{plan_id}", description="Delete a plan")
async def api_plan_delete(plan_id: str, user: User = Depends(check_user_exists)):
plan = await get_plan(plan_id)
if not plan:
raise HTTPException(status_code=HTTPStatus.NOT_FOUND, detail="Plan not found.")
_wallet_owned(plan.wallet, user)
await delete_plan(plan_id)
# ---------------------------------------------------------------------------
# Subscriptions
# ---------------------------------------------------------------------------
@clink_ext_api.get(
"/api/v1/subscriptions", description="List subscriptions for a wallet"
)
async def api_subscriptions(
wallet: str = Query(...), user: User = Depends(check_user_exists)
):
_wallet_owned(wallet, user)
subs = await get_subscriptions(wallet)
plans = {p.id: p for p in await get_plans(wallet)}
return [
{
**s.dict(),
"plan_name": plans[s.plan_id].name if s.plan_id in plans else None,
}
for s in subs
]
@clink_ext_api.post("/api/v1/subscriptions", description="Create a subscription")
async def api_subscription_create(
data: CreateSubscription, user: User = Depends(check_user_exists)
):
_wallet_owned(data.wallet, user)
plan = await get_plan(data.plan_id)
if not plan:
raise HTTPException(status_code=HTTPStatus.NOT_FOUND, detail="Plan not found.")
if plan.wallet != data.wallet:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST,
detail="Plan does not belong to this wallet.",
)
try:
decode_ndebit(data.ndebit)
except ValueError as exc:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST, detail=str(exc)
) from exc
now = datetime.now(timezone.utc)
sub = Subscription(
wallet=data.wallet,
plan_id=data.plan_id,
ndebit=data.ndebit,
payer_npub=data.payer_npub,
state="active",
current_period_start=now,
current_period_end=add_frequency(
now, plan.frequency_number, plan.frequency_unit
),
)
await create_subscription(sub)
return sub.dict()
@clink_ext_api.put(
"/api/v1/subscriptions/{subscription_id}", description="Update a subscription"
)
async def api_subscription_update(
subscription_id: str,
data: UpdateSubscription,
user: User = Depends(check_user_exists),
):
sub = await get_subscription(subscription_id)
if not sub:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Subscription not found."
)
_wallet_owned(sub.wallet, user)
if data.state not in ("active", "paused", "cancelled"):
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST,
detail="State must be active, paused or cancelled.",
)
sub.state = data.state
if data.state == "active":
sub.attempts = 0
sub.last_error = None
sub.updated_at = datetime.now(timezone.utc)
sub = await update_subscription(sub)
return sub.dict()
@clink_ext_api.delete(
"/api/v1/subscriptions/{subscription_id}", description="Delete a subscription"
)
async def api_subscription_delete(
subscription_id: str, user: User = Depends(check_user_exists)
):
sub = await get_subscription(subscription_id)
if not sub:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Subscription not found."
)
_wallet_owned(sub.wallet, user)
await delete_subscription(subscription_id)
@clink_ext_api.post(
"/api/v1/subscriptions/{subscription_id}/renew",
description="Bill the current period of a subscription now",
)
async def api_subscription_renew(
subscription_id: str, user: User = Depends(check_user_exists)
):
sub = await get_subscription(subscription_id)
if not sub:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Subscription not found."
)
_wallet_owned(sub.wallet, user)
if sub.state != "active":
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST, detail="Subscription is not active."
)
sub = await renew_subscription(sub)
return sub.dict()
# ---------------------------------------------------------------------------
# Pay (outgoing offers)
# ---------------------------------------------------------------------------
@clink_ext_api.post(
"/api/v1/pay/parse", description="Parse a noffer string without paying"
)
async def api_pay_parse(data: ParseNofferRequest):
try:
decoded = decode_noffer(data.noffer)
except ValueError as exc:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST, detail=str(exc)
) from exc
return {
"pubkey": decoded.pubkey,
"relay": decoded.relay,
"offer": decoded.offer,
"price_type": decoded.price_type,
"price": decoded.price,
}
@clink_ext_api.post("/api/v1/pay", description="Pay a CLINK offer")
async def api_pay(data: PayRequest, user: User = Depends(check_user_exists)):
_wallet_owned(data.wallet, user)
try:
result = await pay_offer(
noffer=data.noffer,
wallet_id=data.wallet,
amount_sats=data.amount_sats,
description=data.description,
)
except ValueError as exc:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST, detail=str(exc)
) from exc
except PayOfferError as exc:
raise HTTPException(
status_code=HTTPStatus.BAD_GATEWAY, detail=exc.payload
) from exc
return result
# ---------------------------------------------------------------------------
# Checkout (public)
# ---------------------------------------------------------------------------
@clink_ext_api.post(
"/api/v1/checkout/{offer_id}", description="Create an invoice for an offer"
)
async def api_checkout(offer_id: str, data: CheckoutRequest | None = None):
offer = await get_offer(offer_id)
if not offer or not offer.active:
raise HTTPException(status_code=HTTPStatus.NOT_FOUND, detail="Offer not found.")
try:
decoded = decode_noffer(offer.noffer) if offer.noffer else None
except ValueError:
decoded = None
amount, error = resolve_offer_amount(decoded, data.amount_sats if data else None)
if error:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST,
detail=error.get("error", "Invalid Amount"),
)
payment = await create_invoice(
wallet_id=offer.wallet,
amount=amount,
memo=offer.name or "CLINK checkout",
extension="clink",
external_id=offer.id,
)
return {"bolt11": payment.bolt11, "payment_hash": payment.payment_hash}