-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathviews_api.py
191 lines (164 loc) · 5.63 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
import time
from base64 import urlsafe_b64encode
from bolt11 import decode as bolt11_decode
from fastapi import APIRouter, Depends, Query
from lnbits.core.crud import get_payments
from lnbits.core.models import WalletTypeInfo
from lnbits.core.services import create_invoice, pay_invoice
from lnbits.settings import settings
from .decorators import lndhub_require_admin_key, lndhub_require_invoice_key
from .models import LndhubAddInvoice, LndhubAuthData, LndhubCreateInvoice
from .utils import decoded_as_lndhub, to_buffer
lndhub_api_router = APIRouter(prefix="/ext")
@lndhub_api_router.get("/getinfo")
async def lndhub_getinfo():
return {"alias": settings.lnbits_site_title}
@lndhub_api_router.post("/auth")
async def lndhub_auth(data: LndhubAuthData):
token = (
data.refresh_token
if data.refresh_token
else urlsafe_b64encode((data.login + ":" + data.password).encode()).decode(
"ascii"
)
)
return {"refresh_token": token, "access_token": token}
@lndhub_api_router.post("/addinvoice")
async def lndhub_addinvoice(
data: LndhubAddInvoice, wallet: WalletTypeInfo = Depends(lndhub_require_invoice_key)
):
try:
payment = await create_invoice(
wallet_id=wallet.wallet.id,
amount=data.amt,
memo=data.memo or settings.lnbits_site_title,
extra={"tag": "lndhub"},
)
except Exception as exc:
return {"error": f"Failed to create invoice: {exc!s}"}
return {
"pay_req": payment.bolt11,
"payment_request": payment.bolt11,
"add_index": "500",
"r_hash": to_buffer(payment.payment_hash),
"hash": payment.payment_hash,
}
@lndhub_api_router.post("/payinvoice")
async def lndhub_payinvoice(
r_invoice: LndhubCreateInvoice,
key_type: WalletTypeInfo = Depends(lndhub_require_admin_key),
):
try:
invoice = bolt11_decode(r_invoice.invoice)
await pay_invoice(
wallet_id=key_type.wallet.id,
payment_request=r_invoice.invoice,
extra={"tag": "lndhub"},
)
except Exception:
return {"error": "Payment failed"}
return {
"payment_error": "",
"payment_preimage": "0" * 64,
"route": {},
"payment_hash": invoice.payment_hash,
"decoded": decoded_as_lndhub(invoice),
"fee_msat": 0,
"type": "paid_invoice",
"fee": 0,
"value": invoice.amount_msat / 1000 if invoice.amount_msat else 0,
"timestamp": int(time.time()),
"memo": invoice.description,
}
@lndhub_api_router.get("/balance")
async def lndhub_balance(
key_type: WalletTypeInfo = Depends(lndhub_require_invoice_key),
):
return {"BTC": {"AvailableBalance": key_type.wallet.balance}}
@lndhub_api_router.get("/gettxs")
async def lndhub_gettxs(
key_type: WalletTypeInfo = Depends(lndhub_require_invoice_key),
limit: int = Query(20, ge=1, le=200),
offset: int = Query(0, ge=0),
):
return [
{
"payment_preimage": payment.preimage,
"payment_hash": payment.payment_hash,
"fee_msat": payment.fee,
"type": "paid_invoice",
"fee": payment.fee / 1000,
"value": int(payment.amount / 1000),
"timestamp": int(payment.time.timestamp()),
"memo": (
payment.extra and payment.extra.get("comment") or payment.memo
if not payment.pending
else "Payment in transition"
),
}
for payment in reversed(
await get_payments(
wallet_id=key_type.wallet.id,
pending=True,
complete=True,
outgoing=True,
incoming=False,
limit=limit,
offset=offset,
)
)
]
@lndhub_api_router.get("/getuserinvoices")
async def lndhub_getuserinvoices(
key_type: WalletTypeInfo = Depends(lndhub_require_invoice_key),
limit: int = Query(20, ge=1, le=200),
offset: int = Query(0, ge=0),
):
return [
{
"r_hash": to_buffer(payment.payment_hash),
"payment_request": payment.bolt11,
"add_index": "500",
"description": (
payment.extra and payment.extra.get("comment") or payment.memo
),
"payment_hash": payment.payment_hash,
"ispaid": payment.success,
"amt": int(payment.amount / 1000),
"expire_time": (
int(payment.expiry.timestamp())
if payment.expiry
else payment.time.timestamp() + 3600
),
"timestamp": int(payment.time.timestamp()),
"type": "user_invoice",
}
for payment in reversed(
await get_payments(
wallet_id=key_type.wallet.id,
pending=True,
complete=True,
incoming=True,
outgoing=False,
limit=limit,
offset=offset,
)
)
]
@lndhub_api_router.get("/getbtc", dependencies=[Depends(lndhub_require_invoice_key)])
async def lndhub_getbtc():
"load an address for incoming onchain btc"
return []
@lndhub_api_router.get(
"/getpending", dependencies=[Depends(lndhub_require_invoice_key)]
)
async def lndhub_getpending():
"pending onchain transactions"
return []
@lndhub_api_router.get("/decodeinvoice")
async def lndhub_decodeinvoice(invoice: str):
inv = bolt11_decode(invoice)
return decoded_as_lndhub(inv)
@lndhub_api_router.get("/checkrouteinvoice")
async def lndhub_checkrouteinvoice():
"not implemented on canonical lndhub"