-
Notifications
You must be signed in to change notification settings - Fork 3
/
tasks.py
434 lines (407 loc) · 14.9 KB
/
tasks.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
import asyncio
import time
from math import ceil
from typing import Any, Dict, List, Optional, Tuple
from bolt11 import decode as bolt11_decode
from lnbits.core.crud import get_payments, get_wallet, get_wallet_payment
from lnbits.core.models import Payment
from lnbits.core.services import (
check_transaction_status,
create_invoice,
pay_invoice,
)
from lnbits.db import Filters
from lnbits.exceptions import PaymentError
from lnbits.settings import settings
from lnbits.wallets.base import PaymentStatus
from loguru import logger
from .crud import get_config_nwc, get_nwc, tracked_spend_nwc
from .execution_queue import execution_queue
from .models import NWCKey
from .nwcp import NWCServiceProvider
from .permission import nwc_permissions
async def _check(nwc: Optional[NWCKey], method: str, payload: Dict) -> Optional[Dict]:
# check
if not nwc:
return {
"code": "UNAUTHORIZED",
"message": "This public key has no wallet connected.",
}
# check permissions
allowed = False
permissions = nwc.get_permissions()
for p in permissions:
permissions_data: Dict[str, Any] = nwc_permissions.get(p, {})
allowed_methods: List[str] = permissions_data.get("methods", [])
if method in allowed_methods:
allowed = True
break
if not allowed:
return {
"code": "RESTRICTED",
"message": "This public key is not allowed to do this operation.",
}
return None
async def _process_invoice(
wallet_id: str,
pubkey: str,
invoice: str,
amount_msats: int,
description: Optional[str] = None,
):
async def execute_payment():
return await pay_invoice(
wallet_id=wallet_id,
payment_request=invoice,
max_sat=int(ceil(amount_msats / 1000)),
description=description or "",
)
payment_hash = None
try:
in_budget, payment_hash = await tracked_spend_nwc(
pubkey, amount_msats, execute_payment
)
if not in_budget:
error = {
"code": "QUOTA_EXCEEDED",
"message": "The wallet has exceeded its spending quota.",
}
return {"error": error, "in_budget": False}
except PaymentError as e:
status = e.status
message = e.message
if status == "failed":
error = {"code": "PAYMENT_FAILED", "message": message}
return {"error": error, "in_budget": False}
else:
raise e
if not payment_hash:
raise Exception("Payment hash not found")
wait_for_preimage = (
True # currently required by nip 47 specs, might change in future
)
payment_status: Optional[PaymentStatus] = None
while wait_for_preimage:
payment_status = await check_transaction_status(wallet_id, payment_hash)
if payment_status.success:
break
await asyncio.sleep(0.05)
if not payment_status:
raise Exception("Payment status not found")
return {
"preimage": payment_status.preimage
or "0000000000000000000000000000000000000000000000000000000000000000",
"fee_msats": payment_status.fee_msat,
"paid": payment_status.paid,
"payment_hash": payment_hash,
"in_budget": in_budget,
}
async def _on_pay_invoice(
sp: NWCServiceProvider, pubkey: str, payload: Dict
) -> List[Tuple[Optional[Dict], Optional[Dict], List]]:
nwc = await get_nwc(pubkey, None, False, True)
error = await _check(nwc, "pay_invoice", payload)
if error:
return [(None, error, [])]
if not nwc:
raise Exception("Pubkey has no associated wallet")
params = payload.get("params", {})
invoice = params.get("invoice", None)
# Ensures invoice is provided
if not invoice:
raise Exception("Missing invoice")
invoice_data = bolt11_decode(invoice)
amount_msats = int(invoice_data.amount_msat or 0)
res = await _process_invoice(
nwc.wallet, pubkey, invoice, amount_msats, invoice_data.description
)
error = res.get("error")
if error:
return [(None, error, [])]
preimage = res.get("preimage")
out = {
"preimage": preimage,
}
# await log_nwc(pubkey, payload)
return [(out, None, [])]
async def _on_multi_pay_invoice(
sp: NWCServiceProvider, pubkey: str, payload: Dict
) -> List[Tuple[Optional[Dict], Optional[Dict], List]]:
nwc = await get_nwc(pubkey, None, False, True)
error = await _check(nwc, "multi_pay_invoice", payload)
if error:
return [(None, error, [])]
if not nwc:
raise Exception("Pubkey has no associated wallet")
params = payload.get("params", {})
invoices = params.get("invoices", [])
results: List[Tuple[Optional[Dict], Optional[Dict], List]] = []
# Ensures all invoices are provided
for i in invoices:
invoice = i.get("invoice", None)
if not invoice:
raise Exception("Missing invoice")
for i in invoices:
try:
invoice_id = i.get("id", None)
invoice = i.get("invoice", None)
invoice_data = bolt11_decode(invoice)
amount_msats = int(invoice_data.amount_msat or 0)
res = await _process_invoice(
nwc.wallet, pubkey, invoice, amount_msats, invoice_data.description
)
error = res.get("error")
if error:
results.append((None, error, []))
else:
r = (
{
"preimage": res.get("preimage"),
},
None,
[["d", invoice_id if invoice_id else res.get("payment_hash")]],
)
results.append(r)
except Exception as e:
results.append((None, {"code": "INTERNAL", "message": str(e)}, []))
# await log_nwc(pubkey, payload)
return results
async def _on_make_invoice(
sp: NWCServiceProvider, pubkey: str, payload: Dict
) -> List[Tuple[Optional[Dict], Optional[Dict], List]]:
nwc = await get_nwc(pubkey, None, False, True)
error = await _check(nwc, "make_invoice", payload)
if error:
return [(None, error, [])]
if not nwc:
raise Exception("Pubkey has no associated wallet")
params = payload.get("params", {})
amount_msats = params.get("amount", None)
# Ensures amount is provided
if not amount_msats:
raise Exception("Missing amount")
description = params.get("description", "")
description_hash = params.get("description_hash", None)
expiry = params.get("expiry", None)
payment_hash, payment_request = await create_invoice(
wallet_id=nwc.wallet,
amount=int(amount_msats / 1000),
currency="sat",
memo=description,
description_hash=bytes.fromhex(description_hash) if description_hash else None,
unhashed_description=description.encode("utf-8"),
expiry=expiry,
)
payment_status = await check_transaction_status(
wallet_id=nwc.wallet, payment_hash=payment_hash
)
preimage = payment_status.preimage
if (
not preimage
): # Some backend do not return a preimage (eg. FakeWallet), so we fake it
preimage = "0000000000000000000000000000000000000000000000000000000000000000"
res = {
"type": "incoming",
"invoice": payment_request,
"description": description,
"description_hash": description_hash,
"preimage": preimage,
"payment_hash": payment_hash,
"amount": amount_msats,
# "fees_paid":None,
"created_at": int(time.time()),
"metadata": {},
}
if expiry:
res["expires_at"] = int(time.time()) + int(expiry)
# await log_nwc(pubkey, payload)
return [(res, None, [])]
async def _on_lookup_invoice(
sp: NWCServiceProvider, pubkey: str, payload: Dict
) -> List[Tuple[Optional[Dict], Optional[Dict], List]]:
nwc = await get_nwc(pubkey, None, False, True)
error = await _check(nwc, "lookup_invoice", payload)
if error:
return [(None, error, [])]
if not nwc:
raise Exception("Pubkey has no associated wallet")
params = payload.get("params", {})
payment_hash = params.get("payment_hash", None)
invoice = params.get("invoice", None)
# Ensure payment_hash or invoice are provided
if not payment_hash and not invoice:
raise Exception("Missing payment_hash or invoice")
# Extract hash from invoice if not provided
if not payment_hash:
invoice_data = bolt11_decode(invoice)
payment_hash = invoice_data.payment_hash
# Get payment data
payment = await get_wallet_payment(nwc.wallet, payment_hash)
if not payment:
raise Exception("Payment not found")
invoice_data = bolt11_decode(payment.bolt11)
is_settled = not payment.pending
timestamp = payment.time or invoice_data.date
res: Dict = {
"type": "outgoing" if payment.is_out else "incoming",
"invoice": payment.bolt11,
"description": (
invoice_data.description if invoice_data.description else payment.memo
),
"preimage": payment.preimage if is_settled or payment.is_in else None,
"payment_hash": payment.payment_hash,
"amount": abs(payment.msat),
"fees_paid": abs(payment.fee),
"created_at": timestamp,
"expires_at": payment.expiry if payment.expiry else timestamp + 3600,
"settled_at": timestamp if is_settled else None,
"metadata": {},
}
if invoice_data.description_hash:
res["description_hash"] = invoice_data.description_hash
# await log_nwc(pubkey, payload)
return [(res, None, [])]
async def _on_list_transactions(
sp: NWCServiceProvider, pubkey: str, payload: Dict
) -> List[Tuple[Optional[Dict], Optional[Dict], List]]:
nwc = await get_nwc(pubkey, None, False, True)
error = await _check(nwc, "list_transactions", payload)
if error:
return [(None, error, [])]
if not nwc:
raise Exception("Pubkey has no associated wallet")
tfrom = payload.get("from", 0)
tto = payload.get("to", int(time.time()))
limit = payload.get("limit", 10)
offset = payload.get("offset", 0)
unpaid = payload.get("unpaid", False)
tx_type = payload.get("type", None)
values = []
filters: Filters = Filters()
filters.where(["time <= ?"])
values.append(tto)
filters.values(values)
history = await get_payments(
wallet_id=nwc.wallet,
complete=True,
pending=unpaid,
outgoing=not tx_type or tx_type == "outgoing",
incoming=not tx_type or tx_type == "incoming",
since=tfrom,
exclude_uncheckable=False,
filters=filters,
limit=limit,
offset=offset,
)
transactions: List[Dict] = []
p: Payment
for p in history:
invoice_data = bolt11_decode(p.bolt11)
is_settled = not p.pending
transactions.append(
{
"type": "outgoing" if p.is_out else "incoming",
"invoice": p.bolt11,
"description": invoice_data.description,
"description_hash": invoice_data.description_hash,
"preimage": p.preimage if is_settled or p.is_in else None,
"payment_hash": p.payment_hash,
"amount": abs(p.msat),
"fees_paid": p.fee,
"created_at": p.time,
"settled_at": p.time if is_settled else None,
"metadata": {},
}
)
# await log_nwc(pubkey, payload)
return [({"transactions": transactions}, None, [])]
async def _on_get_balance(
sp: NWCServiceProvider, pubkey: str, payload: Dict
) -> List[Tuple[Optional[Dict], Optional[Dict], List]]:
nwc = await get_nwc(pubkey, None, False, True)
error = await _check(nwc, "get_balance", payload)
if error:
return [(None, error, [])]
if not nwc:
raise Exception("Pubkey has no associated wallet")
balance = 0
wallet = await get_wallet(nwc.wallet)
if not wallet:
raise Exception("Wallet not found")
balance = wallet.balance_msat
# await log_nwc(pubkey, payload)
return [({"balance": balance}, None, [])]
async def _on_get_info(
sp: NWCServiceProvider, pubkey: str, payload: Dict
) -> List[Tuple[Optional[Dict], Optional[Dict], List]]:
nwc = await get_nwc(pubkey, None, False, True)
error = await _check(nwc, "get_info", payload)
if error:
return [(None, error, [])]
if not nwc:
raise Exception("Pubkey has no associated wallet")
sp_methods = sp.get_supported_methods()
permissions = nwc.get_permissions()
# Filter only methods supported by the extension and allowed by the permissions
account_methods = []
for spm in sp_methods:
for p in permissions:
permissions_data: Dict[str, Any] = nwc_permissions.get(p, {})
allowed_methods: List[str] = permissions_data.get("methods", [])
if spm in allowed_methods:
account_methods.append(spm)
break
# await log_nwc(pubkey, payload)
return [
(
{
"alias": settings.lnbits_site_title,
"color": "",
"network": "mainnet",
"block_height": 0,
"block_hash": "",
"methods": account_methods,
},
None,
[],
)
]
async def handle_nwc():
priv_key = await get_config_nwc("provider_key")
relay = await get_config_nwc("relay")
nwcsp = NWCServiceProvider(priv_key, relay)
nwcsp.add_request_listener("pay_invoice", _on_pay_invoice)
nwcsp.add_request_listener("multi_pay_invoice", _on_multi_pay_invoice)
nwcsp.add_request_listener("make_invoice", _on_make_invoice)
nwcsp.add_request_listener("lookup_invoice", _on_lookup_invoice)
nwcsp.add_request_listener("list_transactions", _on_list_transactions)
nwcsp.add_request_listener("get_balance", _on_get_balance)
nwcsp.add_request_listener("get_info", _on_get_info)
# currently not supported by lnbits
# nwcsp.addRequestListener("pay_keysend", _on_pay_keysend)
# nwcsp.addRequestListener("multi_pay_keysend", _on_multi_pay_keysend)
###
await nwcsp.start()
try:
while True:
await asyncio.sleep(3600)
except asyncio.CancelledError:
await nwcsp.cleanup()
raise
async def handle_execution_queue():
while True:
try:
task = await execution_queue.get()
action = task.get("action")
future = task.get("future")
try:
if not action:
raise Exception("Invalid action")
res = await action()
if future:
future.set_result(res)
except Exception as e:
if future:
future.set_exception(e)
except Exception as e:
logger.error(str(e))