-
Notifications
You must be signed in to change notification settings - Fork 14
/
views_api.py
376 lines (321 loc) · 11.9 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
import json
from http import HTTPStatus
import httpx
from fastapi import APIRouter, Depends, HTTPException, Query, Request
from lnbits.core.crud import (
get_latest_payments_by_extension,
get_standalone_payment,
get_user,
get_wallet,
)
from lnbits.core.models import WalletTypeInfo
from lnbits.core.services import create_invoice
from lnbits.decorators import (
require_admin_key,
require_invoice_key,
)
from lnbits.utils.exchange_rates import get_fiat_rate_satoshis
from lnurl import decode as decode_lnurl
from .crud import (
create_tpos,
delete_tpos,
get_lnurlcharge,
get_tpos,
get_tposs,
start_lnurlcharge,
update_lnurlcharge,
update_tpos,
)
from .helpers import get_pr
from .models import (
CreateTposData,
CreateTposInvoice,
CreateUpdateItemData,
CreateWithdrawPay,
LnurlCharge,
PayLnurlWData,
Tpos,
)
tpos_api_router = APIRouter()
@tpos_api_router.get("/api/v1/tposs", status_code=HTTPStatus.OK)
async def api_tposs(
all_wallets: bool = Query(False),
key_info: WalletTypeInfo = Depends(require_invoice_key),
) -> list[Tpos]:
wallet_ids = [key_info.wallet.id]
if all_wallets:
user = await get_user(key_info.wallet.user)
wallet_ids = user.wallet_ids if user else []
return await get_tposs(wallet_ids)
@tpos_api_router.post("/api/v1/tposs", status_code=HTTPStatus.CREATED)
async def api_tpos_create(
data: CreateTposData, key_type: WalletTypeInfo = Depends(require_admin_key)
):
data.wallet = key_type.wallet.id
tpos = await create_tpos(data)
return tpos
@tpos_api_router.put("/api/v1/tposs/{tpos_id}")
async def api_tpos_update(
data: CreateTposData,
tpos_id: str,
wallet: WalletTypeInfo = Depends(require_admin_key),
):
tpos = await get_tpos(tpos_id)
if not tpos:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="TPoS does not exist."
)
if wallet.wallet.id != tpos.wallet:
raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail="Not your TPoS.")
for field, value in data.dict().items():
setattr(tpos, field, value)
tpos = await update_tpos(tpos)
return tpos
@tpos_api_router.delete("/api/v1/tposs/{tpos_id}")
async def api_tpos_delete(
tpos_id: str, wallet: WalletTypeInfo = Depends(require_admin_key)
):
tpos = await get_tpos(tpos_id)
if not tpos:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="TPoS does not exist."
)
if tpos.wallet != wallet.wallet.id:
raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail="Not your TPoS.")
await delete_tpos(tpos_id)
return "", HTTPStatus.NO_CONTENT
@tpos_api_router.post(
"/api/v1/tposs/{tpos_id}/invoices", status_code=HTTPStatus.CREATED
)
async def api_tpos_create_invoice(tpos_id: str, data: CreateTposInvoice) -> dict:
tpos = await get_tpos(tpos_id)
if not tpos:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="TPoS does not exist."
)
try:
payment = await create_invoice(
wallet_id=tpos.wallet,
amount=data.amount + (data.tip_amount or 0),
memo=f"{data.memo} to {tpos.name}" if data.memo else f"{tpos.name}",
extra={
"tag": "tpos",
"tip_amount": data.tip_amount,
"tpos_id": tpos_id,
"amount": data.amount,
"details": data.details if data.details else None,
"lnaddress": data.user_lnaddress if data.user_lnaddress else None,
},
)
except Exception as exc:
raise HTTPException(
status_code=HTTPStatus.INTERNAL_SERVER_ERROR, detail=str(exc)
) from exc
return {"payment_hash": payment.payment_hash, "payment_request": payment.bolt11}
@tpos_api_router.get("/api/v1/tposs/{tpos_id}/invoices")
async def api_tpos_get_latest_invoices(tpos_id: str):
payments = await get_latest_payments_by_extension(ext_name="tpos", ext_id=tpos_id)
return [
{
"checking_id": payment.checking_id,
"amount": payment.amount,
"time": payment.time,
"pending": payment.pending,
}
for payment in payments
]
@tpos_api_router.post(
"/api/v1/tposs/{tpos_id}/invoices/{payment_request}/pay", status_code=HTTPStatus.OK
)
async def api_tpos_pay_invoice(
lnurl_data: PayLnurlWData, payment_request: str, tpos_id: str
):
tpos = await get_tpos(tpos_id)
if not tpos:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="TPoS does not exist."
)
lnurl = (
lnurl_data.lnurl.replace("lnurlw://", "")
.replace("lightning://", "")
.replace("LIGHTNING://", "")
.replace("lightning:", "")
.replace("LIGHTNING:", "")
)
if lnurl.lower().startswith("lnurl"):
lnurl = decode_lnurl(lnurl)
else:
lnurl = "https://" + lnurl
async with httpx.AsyncClient() as client:
try:
headers = {"user-agent": "lnbits/tpos"}
r = await client.get(lnurl, follow_redirects=True, headers=headers)
if r.is_error:
lnurl_response = {"success": False, "detail": "Error loading"}
else:
resp = r.json()
if resp["tag"] != "withdrawRequest":
lnurl_response = {"success": False, "detail": "Wrong tag type"}
else:
r2 = await client.get(
resp["callback"],
follow_redirects=True,
headers=headers,
params={
"k1": resp["k1"],
"pr": payment_request,
},
)
resp2 = r2.json()
if r2.is_error:
lnurl_response = {
"success": False,
"detail": "Error loading callback",
}
elif resp2["status"] == "ERROR":
lnurl_response = {"success": False, "detail": resp2["reason"]}
else:
lnurl_response = {"success": True, "detail": resp2}
except (httpx.ConnectError, httpx.RequestError):
lnurl_response = {"success": False, "detail": "Unexpected error occurred"}
return lnurl_response
@tpos_api_router.get(
"/api/v1/tposs/{tpos_id}/invoices/{payment_hash}", status_code=HTTPStatus.OK
)
async def api_tpos_check_invoice(tpos_id: str, payment_hash: str):
tpos = await get_tpos(tpos_id)
if not tpos:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="TPoS does not exist."
)
payment = await get_standalone_payment(payment_hash, incoming=True)
if not payment:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Payment does not exist."
)
return {"paid": payment.success}
@tpos_api_router.get("/api/v1/atm/{tpos_id}/{atmpin}", status_code=HTTPStatus.CREATED)
async def api_tpos_atm_pin_check(tpos_id: str, atmpin: int) -> LnurlCharge:
tpos = await get_tpos(tpos_id)
if not tpos:
raise HTTPException(HTTPStatus.NOT_FOUND, "TPoS does not exist.")
if int(tpos.withdraw_pin or 0) != int(atmpin):
raise HTTPException(HTTPStatus.NOT_FOUND, "Wrong PIN.")
token = await start_lnurlcharge(tpos)
return token
@tpos_api_router.post(
"/api/v1/atm/withdraw/{k1}/{amount}/pay", status_code=HTTPStatus.OK
)
async def api_tpos_atm_pay(
request: Request, k1: str, amount: int, data: CreateWithdrawPay
):
try:
# get the payment_request from the lnurl
pay_link = data.pay_link.replace("lnurlp://", "https://")
async with httpx.AsyncClient() as client:
headers = {"user-agent": "lnbits/tpos"}
r = await client.get(pay_link, follow_redirects=True, headers=headers)
if r.is_error:
return {"success": False, "detail": "Error loading"}
resp = r.json()
amount = amount * 1000 # convert to msats
if resp["tag"] != "payRequest":
return {"success": False, "detail": "Wrong tag type"}
if amount < int(resp["minSendable"]):
return {"success": False, "detail": "Amount too low"}
if amount > int(resp["maxSendable"]):
return {"success": False, "detail": "Amount too high"}
cb_res = await client.get(
resp["callback"],
follow_redirects=True,
headers=headers,
params={"amount": amount},
)
cb_resp = cb_res.json()
if cb_res.is_error:
return {"success": False, "detail": "Error loading callback"}
# pay the invoice
lnurl_cb_url = str(request.url_for("tpos.tposlnurlcharge.callback"))
pay_invoice = await client.get(
lnurl_cb_url,
params={"pr": cb_resp["pr"], "k1": k1},
)
if pay_invoice.status_code != 200:
return {"success": False, "detail": "Error paying invoice"}
return {"success": True, "detail": "Payment successful"}
except AssertionError as exc:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST,
detail=str(exc),
) from exc
except Exception as exc:
raise HTTPException(
status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
detail="Cannot process atm withdraw",
) from exc
@tpos_api_router.get(
"/api/v1/atm/withdraw/{withdraw_token}/{amount}", status_code=HTTPStatus.CREATED
)
async def api_tpos_create_withdraw(
request: Request, withdraw_token: str, amount: str
) -> dict:
lnurlcharge = await get_lnurlcharge(withdraw_token)
if not lnurlcharge:
return {
"status": "ERROR",
"reason": f"lnurlcharge {withdraw_token} not found on this server",
}
tpos = await get_tpos(lnurlcharge.tpos_id)
if not tpos:
return {
"status": "ERROR",
"reason": f"TPoS {lnurlcharge.tpos_id} not found on this server",
}
wallet = await get_wallet(tpos.wallet)
assert wallet
balance = int(wallet.balance_msat / 1000)
if balance < int(amount):
return {
"status": "ERROR",
"reason": f"Insufficient balance. Your balance is {balance} sats",
}
lnurlcharge = await update_lnurlcharge(
LnurlCharge(
id=withdraw_token,
tpos_id=lnurlcharge.tpos_id,
amount=int(amount),
claimed=False,
)
)
return {**lnurlcharge.dict(), **{"lnurl": lnurlcharge.lnurl(request)}}
@tpos_api_router.get("/api/v1/rate/{currency}", status_code=HTTPStatus.OK)
async def api_check_fiat_rate(currency):
try:
rate = await get_fiat_rate_satoshis(currency)
except AssertionError:
rate = None
return {"rate": rate}
@tpos_api_router.put("/api/v1/tposs/{tpos_id}/items", status_code=HTTPStatus.CREATED)
async def api_tpos_create_items(
data: CreateUpdateItemData,
tpos_id: str,
wallet: WalletTypeInfo = Depends(require_admin_key),
):
tpos = await get_tpos(tpos_id)
if not tpos:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="TPoS does not exist."
)
if wallet.wallet.id != tpos.wallet:
raise HTTPException(status_code=HTTPStatus.FORBIDDEN, detail="Not your TPoS.")
tpos.items = json.dumps(data.dict()["items"])
tpos = await update_tpos(tpos)
return tpos
@tpos_api_router.get("/api/v1/tposs/lnaddresscheck", status_code=HTTPStatus.OK)
async def api_tpos_check_lnaddress(lnaddress: str):
check = await get_pr(lnaddress, 1)
if not check:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="TPoS does not exist."
)
return True