mirror of
https://github.com/lnbits/lnbits.git
synced 2025-10-10 20:42:32 +02:00
fastAPI refactoring
This commit is contained in:
@@ -2,6 +2,11 @@ import time
|
|||||||
from base64 import urlsafe_b64encode
|
from base64 import urlsafe_b64encode
|
||||||
from quart import jsonify, g, request
|
from quart import jsonify, g, request
|
||||||
|
|
||||||
|
from fastapi import FastAPI, Query
|
||||||
|
from fastapi.encoders import jsonable_encoder
|
||||||
|
from fastapi.responses import JSONResponse
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
from lnbits.core.services import pay_invoice, create_invoice
|
from lnbits.core.services import pay_invoice, create_invoice
|
||||||
from lnbits.core.crud import get_payments, delete_expired_invoices
|
from lnbits.core.crud import get_payments, delete_expired_invoices
|
||||||
from lnbits.decorators import api_validate_post_request
|
from lnbits.decorators import api_validate_post_request
|
||||||
@@ -13,62 +18,62 @@ from .decorators import check_wallet
|
|||||||
from .utils import to_buffer, decoded_as_lndhub
|
from .utils import to_buffer, decoded_as_lndhub
|
||||||
|
|
||||||
|
|
||||||
@lndhub_ext.route("/ext/getinfo", methods=["GET"])
|
@lndhub_ext.get("/ext/getinfo")
|
||||||
async def lndhub_getinfo():
|
async def lndhub_getinfo():
|
||||||
return jsonify({"error": True, "code": 1, "message": "bad auth"})
|
return {"error": True, "code": 1, "message": "bad auth"}
|
||||||
|
|
||||||
|
|
||||||
@lndhub_ext.route("/ext/auth", methods=["POST"])
|
@lndhub_ext.post("/ext/auth")
|
||||||
@api_validate_post_request(
|
# @api_validate_post_request(
|
||||||
schema={
|
# schema={
|
||||||
"login": {"type": "string", "required": True, "excludes": "refresh_token"},
|
# "login": {"type": "string", "required": True, "excludes": "refresh_token"},
|
||||||
"password": {"type": "string", "required": True, "excludes": "refresh_token"},
|
# "password": {"type": "string", "required": True, "excludes": "refresh_token"},
|
||||||
"refresh_token": {
|
# "refresh_token": {
|
||||||
"type": "string",
|
# "type": "string",
|
||||||
"required": True,
|
# "required": True,
|
||||||
"excludes": ["login", "password"],
|
# "excludes": ["login", "password"],
|
||||||
},
|
# },
|
||||||
}
|
# }
|
||||||
)
|
# )
|
||||||
async def lndhub_auth():
|
async def lndhub_auth(login: str, password: str, refresh_token: str): #missing the "excludes" thing
|
||||||
token = (
|
token = (
|
||||||
g.data["refresh_token"]
|
refresh_token
|
||||||
if "refresh_token" in g.data and g.data["refresh_token"]
|
if refresh_token
|
||||||
else urlsafe_b64encode(
|
else urlsafe_b64encode(
|
||||||
(g.data["login"] + ":" + g.data["password"]).encode("utf-8")
|
(login + ":" + password).encode("utf-8")
|
||||||
).decode("ascii")
|
).decode("ascii")
|
||||||
)
|
)
|
||||||
return jsonify({"refresh_token": token, "access_token": token})
|
return {"refresh_token": token, "access_token": token}
|
||||||
|
|
||||||
|
|
||||||
@lndhub_ext.route("/ext/addinvoice", methods=["POST"])
|
@lndhub_ext.post("/ext/addinvoice")
|
||||||
@check_wallet()
|
@check_wallet()
|
||||||
@api_validate_post_request(
|
# @api_validate_post_request(
|
||||||
schema={
|
# schema={
|
||||||
"amt": {"type": "string", "required": True},
|
# "amt": {"type": "string", "required": True},
|
||||||
"memo": {"type": "string", "required": True},
|
# "memo": {"type": "string", "required": True},
|
||||||
"preimage": {"type": "string", "required": False},
|
# "preimage": {"type": "string", "required": False},
|
||||||
}
|
# }
|
||||||
)
|
# )
|
||||||
async def lndhub_addinvoice():
|
async def lndhub_addinvoice(amt: str, memo: str, preimage: str = ""):
|
||||||
try:
|
try:
|
||||||
_, pr = await create_invoice(
|
_, pr = await create_invoice(
|
||||||
wallet_id=g.wallet.id,
|
wallet_id=g.wallet.id,
|
||||||
amount=int(g.data["amt"]),
|
amount=int(amt),
|
||||||
memo=g.data["memo"],
|
memo=memo,
|
||||||
extra={"tag": "lndhub"},
|
extra={"tag": "lndhub"},
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return jsonify(
|
return
|
||||||
{
|
{
|
||||||
"error": True,
|
"error": True,
|
||||||
"code": 7,
|
"code": 7,
|
||||||
"message": "Failed to create invoice: " + str(e),
|
"message": "Failed to create invoice: " + str(e),
|
||||||
}
|
}
|
||||||
)
|
|
||||||
|
|
||||||
invoice = bolt11.decode(pr)
|
invoice = bolt11.decode(pr)
|
||||||
return jsonify(
|
return
|
||||||
{
|
{
|
||||||
"pay_req": pr,
|
"pay_req": pr,
|
||||||
"payment_request": pr,
|
"payment_request": pr,
|
||||||
@@ -76,30 +81,30 @@ async def lndhub_addinvoice():
|
|||||||
"r_hash": to_buffer(invoice.payment_hash),
|
"r_hash": to_buffer(invoice.payment_hash),
|
||||||
"hash": invoice.payment_hash,
|
"hash": invoice.payment_hash,
|
||||||
}
|
}
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@lndhub_ext.route("/ext/payinvoice", methods=["POST"])
|
|
||||||
|
@lndhub_ext.post("/ext/payinvoice")
|
||||||
@check_wallet(requires_admin=True)
|
@check_wallet(requires_admin=True)
|
||||||
@api_validate_post_request(schema={"invoice": {"type": "string", "required": True}})
|
# @api_validate_post_request(schema={"invoice": {"type": "string", "required": True}})
|
||||||
async def lndhub_payinvoice():
|
async def lndhub_payinvoice(invoice: str):
|
||||||
try:
|
try:
|
||||||
await pay_invoice(
|
await pay_invoice(
|
||||||
wallet_id=g.wallet.id,
|
wallet_id=g.wallet.id,
|
||||||
payment_request=g.data["invoice"],
|
payment_request=invoice,
|
||||||
extra={"tag": "lndhub"},
|
extra={"tag": "lndhub"},
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return jsonify(
|
return
|
||||||
{
|
{
|
||||||
"error": True,
|
"error": True,
|
||||||
"code": 10,
|
"code": 10,
|
||||||
"message": "Payment failed: " + str(e),
|
"message": "Payment failed: " + str(e),
|
||||||
}
|
}
|
||||||
)
|
|
||||||
|
|
||||||
invoice: bolt11.Invoice = bolt11.decode(g.data["invoice"])
|
|
||||||
return jsonify(
|
invoice: bolt11.Invoice = bolt11.decode(invoice)
|
||||||
|
return
|
||||||
{
|
{
|
||||||
"payment_error": "",
|
"payment_error": "",
|
||||||
"payment_preimage": "0" * 64,
|
"payment_preimage": "0" * 64,
|
||||||
@@ -113,16 +118,16 @@ async def lndhub_payinvoice():
|
|||||||
"timestamp": int(time.time()),
|
"timestamp": int(time.time()),
|
||||||
"memo": invoice.description,
|
"memo": invoice.description,
|
||||||
}
|
}
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@lndhub_ext.route("/ext/balance", methods=["GET"])
|
|
||||||
|
@lndhub_ext.get("/ext/balance")
|
||||||
@check_wallet()
|
@check_wallet()
|
||||||
async def lndhub_balance():
|
async def lndhub_balance():
|
||||||
return jsonify({"BTC": {"AvailableBalance": g.wallet.balance}})
|
return {"BTC": {"AvailableBalance": g.wallet.balance}}
|
||||||
|
|
||||||
|
|
||||||
@lndhub_ext.route("/ext/gettxs", methods=["GET"])
|
@lndhub_ext.get("/ext/gettxs")
|
||||||
@check_wallet()
|
@check_wallet()
|
||||||
async def lndhub_gettxs():
|
async def lndhub_gettxs():
|
||||||
for payment in await get_payments(
|
for payment in await get_payments(
|
||||||
@@ -138,7 +143,7 @@ async def lndhub_gettxs():
|
|||||||
)
|
)
|
||||||
|
|
||||||
limit = int(request.args.get("limit", 200))
|
limit = int(request.args.get("limit", 200))
|
||||||
return jsonify(
|
return
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
"payment_preimage": payment.preimage,
|
"payment_preimage": payment.preimage,
|
||||||
@@ -164,10 +169,10 @@ async def lndhub_gettxs():
|
|||||||
)[:limit]
|
)[:limit]
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@lndhub_ext.route("/ext/getuserinvoices", methods=["GET"])
|
|
||||||
|
@lndhub_ext.get("/ext/getuserinvoices")
|
||||||
@check_wallet()
|
@check_wallet()
|
||||||
async def lndhub_getuserinvoices():
|
async def lndhub_getuserinvoices():
|
||||||
await delete_expired_invoices()
|
await delete_expired_invoices()
|
||||||
@@ -184,7 +189,7 @@ async def lndhub_getuserinvoices():
|
|||||||
)
|
)
|
||||||
|
|
||||||
limit = int(request.args.get("limit", 200))
|
limit = int(request.args.get("limit", 200))
|
||||||
return jsonify(
|
return
|
||||||
[
|
[
|
||||||
{
|
{
|
||||||
"r_hash": to_buffer(invoice.payment_hash),
|
"r_hash": to_buffer(invoice.payment_hash),
|
||||||
@@ -210,31 +215,31 @@ async def lndhub_getuserinvoices():
|
|||||||
)[:limit]
|
)[:limit]
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@lndhub_ext.route("/ext/getbtc", methods=["GET"])
|
|
||||||
|
@lndhub_ext.get("/ext/getbtc")
|
||||||
@check_wallet()
|
@check_wallet()
|
||||||
async def lndhub_getbtc():
|
async def lndhub_getbtc():
|
||||||
"load an address for incoming onchain btc"
|
"load an address for incoming onchain btc"
|
||||||
return jsonify([])
|
return []
|
||||||
|
|
||||||
|
|
||||||
@lndhub_ext.route("/ext/getpending", methods=["GET"])
|
@lndhub_ext.get("/ext/getpending")
|
||||||
@check_wallet()
|
@check_wallet()
|
||||||
async def lndhub_getpending():
|
async def lndhub_getpending():
|
||||||
"pending onchain transactions"
|
"pending onchain transactions"
|
||||||
return jsonify([])
|
return []
|
||||||
|
|
||||||
|
|
||||||
@lndhub_ext.route("/ext/decodeinvoice", methods=["GET"])
|
@lndhub_ext.get("/ext/decodeinvoice")
|
||||||
async def lndhub_decodeinvoice():
|
async def lndhub_decodeinvoice():
|
||||||
invoice = request.args.get("invoice")
|
invoice = request.args.get("invoice")
|
||||||
inv = bolt11.decode(invoice)
|
inv = bolt11.decode(invoice)
|
||||||
return jsonify(decoded_as_lndhub(inv))
|
return decoded_as_lndhub(inv)
|
||||||
|
|
||||||
|
|
||||||
@lndhub_ext.route("/ext/checkrouteinvoice", methods=["GET"])
|
@lndhub_ext.get("/ext/checkrouteinvoice")
|
||||||
async def lndhub_checkrouteinvoice():
|
async def lndhub_checkrouteinvoice():
|
||||||
"not implemented on canonical lndhub"
|
"not implemented on canonical lndhub"
|
||||||
pass
|
pass
|
||||||
|
Reference in New Issue
Block a user