lnbits-legend/lnbits/extensions/lndhub/views_api.py

221 lines
6.2 KiB
Python
Raw Normal View History

2021-10-13 16:47:24 +01:00
import time
from base64 import urlsafe_b64encode
2021-11-03 10:30:43 +00:00
from http import HTTPStatus
from fastapi.param_functions import Query
from fastapi.params import Depends
2021-10-15 16:20:23 +01:00
from pydantic import BaseModel
2021-11-03 10:30:43 +00:00
from starlette.exceptions import HTTPException
2021-10-13 16:47:24 +01:00
2021-11-03 10:30:43 +00:00
from lnbits import bolt11
from lnbits.core.crud import delete_expired_invoices, get_payments
from lnbits.core.services import create_invoice, pay_invoice
2021-10-15 19:55:24 +02:00
from lnbits.decorators import WalletTypeInfo
2021-10-13 16:47:24 +01:00
from lnbits.settings import WALLET
from . import lndhub_ext
from .decorators import check_wallet, require_admin_key
2021-11-03 10:30:43 +00:00
from .utils import decoded_as_lndhub, to_buffer
2021-10-13 16:47:24 +01:00
@lndhub_ext.get("/ext/getinfo")
async def lndhub_getinfo():
2021-10-17 18:33:29 +01:00
raise HTTPException(status_code=HTTPStatus.UNAUTHORIZED, detail="bad auth")
2021-10-13 16:47:24 +01:00
2021-10-15 16:20:23 +01:00
class AuthData(BaseModel):
login: str = Query(None)
password: str = Query(None)
refresh_token: str = Query(None)
2021-10-13 16:47:24 +01:00
@lndhub_ext.post("/ext/auth")
2021-10-17 18:33:29 +01:00
async def lndhub_auth(data: AuthData):
2021-10-13 16:47:24 +01:00
token = (
2021-10-15 16:20:23 +01:00
data.refresh_token
if data.refresh_token
2021-10-17 18:33:29 +01:00
else urlsafe_b64encode(
(data.login + ":" + data.password).encode("utf-8")
).decode("ascii")
2021-10-13 16:47:24 +01:00
)
return {"refresh_token": token, "access_token": token}
2021-10-17 18:33:29 +01:00
2021-10-15 16:39:45 +01:00
class AddInvoice(BaseModel):
2021-11-03 10:30:43 +00:00
amt: str = Query(...)
memo: str = Query(...)
2021-10-15 16:39:45 +01:00
preimage: str = Query(None)
2021-10-13 16:47:24 +01:00
@lndhub_ext.post("/ext/addinvoice")
async def lndhub_addinvoice(
2021-10-17 18:33:29 +01:00
data: AddInvoice, wallet: WalletTypeInfo = Depends(check_wallet)
2021-10-13 16:47:24 +01:00
):
try:
_, pr = await create_invoice(
wallet_id=wallet.wallet.id,
2021-10-15 16:39:45 +01:00
amount=int(data.amt),
2021-11-03 10:30:43 +00:00
memo=data.memo or "received sats",
2021-10-13 16:47:24 +01:00
extra={"tag": "lndhub"},
)
except:
raise HTTPException(
2021-10-17 18:33:29 +01:00
status_code=HTTPStatus.NOT_FOUND, detail="Failed to create invoice"
2021-10-13 16:47:24 +01:00
)
invoice = bolt11.decode(pr)
return {
"pay_req": pr,
"payment_request": pr,
"add_index": "500",
"r_hash": to_buffer(invoice.payment_hash),
"hash": invoice.payment_hash,
}
2021-10-17 18:33:29 +01:00
class Invoice(BaseModel):
2021-11-03 10:30:43 +00:00
invoice: str = Query(...)
2021-10-13 16:47:24 +01:00
2021-10-17 18:33:29 +01:00
2021-10-13 16:47:24 +01:00
@lndhub_ext.post("/ext/payinvoice")
async def lndhub_payinvoice(
r_invoice: Invoice, wallet: WalletTypeInfo = Depends(require_admin_key)
2021-10-13 16:47:24 +01:00
):
try:
await pay_invoice(
wallet_id=wallet.wallet.id,
payment_request=r_invoice.invoice,
2021-10-13 16:47:24 +01:00
extra={"tag": "lndhub"},
)
except:
2021-10-17 18:33:29 +01:00
raise HTTPException(status_code=HTTPStatus.NOT_FOUND, detail="Payment failed")
2021-10-13 16:47:24 +01:00
invoice: bolt11.Invoice = bolt11.decode(r_invoice.invoice)
2021-11-12 04:14:55 +00:00
2021-10-13 16:47:24 +01:00
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,
"timestamp": int(time.time()),
"memo": invoice.description,
}
@lndhub_ext.get("/ext/balance")
2021-10-17 18:33:29 +01:00
async def lndhub_balance(wallet: WalletTypeInfo = Depends(check_wallet),):
2021-10-13 16:47:24 +01:00
return {"BTC": {"AvailableBalance": wallet.wallet.balance}}
2021-10-15 16:20:23 +01:00
@lndhub_ext.get("/ext/gettxs")
2021-10-13 16:47:24 +01:00
async def lndhub_gettxs(
2021-10-15 19:55:24 +02:00
wallet: WalletTypeInfo = Depends(check_wallet), limit: int = Query(0, ge=0, lt=200)
2021-10-13 16:47:24 +01:00
):
for payment in await get_payments(
wallet_id=wallet.wallet.id,
complete=False,
pending=True,
outgoing=True,
incoming=False,
exclude_uncheckable=True,
):
await payment.set_pending(
(await WALLET.get_payment_status(payment.checking_id)).pending
)
return [
{
"payment_preimage": payment.preimage,
"payment_hash": payment.payment_hash,
"fee_msat": payment.fee * 1000,
"type": "paid_invoice",
"fee": payment.fee,
"value": int(payment.amount / 1000),
"timestamp": payment.time,
"memo": payment.memo if not payment.pending else "Payment in transition",
}
for payment in reversed(
(
await get_payments(
wallet_id=wallet.wallet.id,
pending=True,
complete=True,
outgoing=True,
incoming=False,
)
)[:limit]
)
]
2021-10-15 16:20:23 +01:00
@lndhub_ext.get("/ext/getuserinvoices")
2021-10-13 16:47:24 +01:00
async def lndhub_getuserinvoices(
2021-10-15 19:55:24 +02:00
wallet: WalletTypeInfo = Depends(check_wallet), limit: int = Query(0, ge=0, lt=200)
2021-10-13 16:47:24 +01:00
):
await delete_expired_invoices()
for invoice in await get_payments(
wallet_id=wallet.wallet.id,
complete=False,
pending=True,
outgoing=False,
incoming=True,
exclude_uncheckable=True,
):
await invoice.set_pending(
(await WALLET.get_invoice_status(invoice.checking_id)).pending
)
2021-10-13 16:47:24 +01:00
return [
{
"r_hash": to_buffer(invoice.payment_hash),
"payment_request": invoice.bolt11,
"add_index": "500",
"description": invoice.memo,
"payment_hash": invoice.payment_hash,
"ispaid": not invoice.pending,
"amt": int(invoice.amount / 1000),
"expire_time": int(time.time() + 1800),
"timestamp": invoice.time,
"type": "user_invoice",
}
for invoice in reversed(
(
await get_payments(
wallet_id=wallet.wallet.id,
pending=True,
complete=True,
incoming=True,
outgoing=False,
)
)[:limit]
)
]
@lndhub_ext.get("/ext/getbtc")
2021-10-15 19:55:24 +02:00
async def lndhub_getbtc(wallet: WalletTypeInfo = Depends(check_wallet)):
2021-10-13 16:47:24 +01:00
"load an address for incoming onchain btc"
return []
@lndhub_ext.get("/ext/getpending")
async def lndhub_getpending(wallet: WalletTypeInfo = Depends(check_wallet)):
2021-10-13 16:47:24 +01:00
"pending onchain transactions"
return []
@lndhub_ext.get("/ext/decodeinvoice")
async def lndhub_decodeinvoice(invoice: str = Query(None)):
inv = bolt11.decode(invoice)
return decoded_as_lndhub(inv)
@lndhub_ext.get("/ext/checkrouteinvoice")
async def lndhub_checkrouteinvoice():
"not implemented on canonical lndhub"
pass