bugfixes and fix topup wallet

This commit is contained in:
dni ⚡ 2022-10-12 13:08:59 +02:00
parent 761fc427de
commit 620fd25696
5 changed files with 44 additions and 30 deletions

View file

@ -369,10 +369,10 @@
topupWallet() { topupWallet() {
LNbits.api LNbits.api
.request( .request(
'POST', 'PUT',
'/admin/api/v1/topup/?usr=' + this.g.user.id, '/admin/api/v1/topup/?usr=' + this.g.user.id,
this.wallet.id, this.g.user.wallets[0].adminkey,
this.wallet.amount this.wallet
) )
.then(response => { .then(response => {
this.$q.notify({ this.$q.notify({

View file

@ -1,7 +1,6 @@
from http import HTTPStatus from http import HTTPStatus
from fastapi import Body, Depends, Request from fastapi import Body, Depends, Query
from loguru import logger
from starlette.exceptions import HTTPException from starlette.exceptions import HTTPException
from lnbits.core.crud import get_wallet from lnbits.core.crud import get_wallet
@ -9,47 +8,50 @@ from lnbits.core.models import User
from lnbits.decorators import check_admin from lnbits.decorators import check_admin
from lnbits.extensions.admin import admin_ext from lnbits.extensions.admin import admin_ext
from lnbits.extensions.admin.models import UpdateSettings from lnbits.extensions.admin.models import UpdateSettings
from lnbits.requestvars import g
from lnbits.server import server_restart from lnbits.server import server_restart
from lnbits.settings import settings
from .crud import delete_settings, update_settings, update_wallet_balance from .crud import delete_settings, update_settings, update_wallet_balance
@admin_ext.get("/api/v1/restart/", status_code=HTTPStatus.OK) @admin_ext.get(
async def api_restart_server(user: User = Depends(check_admin)): "/api/v1/restart/", status_code=HTTPStatus.OK, dependencies=[Depends(check_admin)]
)
async def api_restart_server() -> dict[str, str]:
server_restart.set() server_restart.set()
return {"status": "Success"} return {"status": "Success"}
@admin_ext.put("/api/v1/topup/", status_code=HTTPStatus.OK) @admin_ext.put(
"/api/v1/topup/", status_code=HTTPStatus.OK, dependencies=[Depends(check_admin)]
)
async def api_update_balance( async def api_update_balance(
wallet_id, topup_amount: int, user: User = Depends(check_admin) id: str = Body(...), amount: int = Body(...)
): ) -> dict[str, str]:
try: try:
wallet = await get_wallet(wallet_id) await get_wallet(id)
except: except:
raise HTTPException( raise HTTPException(
status_code=HTTPStatus.FORBIDDEN, detail="wallet does not exist." status_code=HTTPStatus.FORBIDDEN, detail="wallet does not exist."
) )
await update_wallet_balance(wallet_id=wallet_id, amount=int(topup_amount)) await update_wallet_balance(wallet_id=id, amount=int(amount))
return {"status": "Success"} return {"status": "Success"}
@admin_ext.put("/api/v1/settings/", status_code=HTTPStatus.OK) @admin_ext.put(
"/api/v1/settings/", status_code=HTTPStatus.OK, dependencies=[Depends(check_admin)]
)
async def api_update_settings( async def api_update_settings(
user: User = Depends(check_admin),
data: UpdateSettings = Body(...), data: UpdateSettings = Body(...),
): ):
settings = await update_settings(data) settings = await update_settings(data)
return {"status": "Success", "settings": settings.dict()} return {"status": "Success", "settings": settings.dict()}
@admin_ext.delete("/api/v1/settings/", status_code=HTTPStatus.OK) @admin_ext.delete(
async def api_delete_settings( "/api/v1/settings/", status_code=HTTPStatus.OK, dependencies=[Depends(check_admin)]
user: User = Depends(check_admin), )
): async def api_delete_settings() -> dict[str, str]:
await delete_settings() await delete_settings()
return {"status": "Success"} return {"status": "Success"}

View file

@ -52,6 +52,7 @@ def main(ctx, port: int, host: str, ssl_keyfile: str, ssl_certfile: str, reload:
port=port, port=port,
host=host, host=host,
reload=reload, reload=reload,
forwarded_allow_ips="*",
ssl_keyfile=ssl_keyfile, ssl_keyfile=ssl_keyfile,
ssl_certfile=ssl_certfile, ssl_certfile=ssl_certfile,
**d **d

View file

@ -1,20 +1,19 @@
import importlib import importlib
import json import json
import subprocess import subprocess
import httpx
from os import path from os import path
from sqlite3 import Row from sqlite3 import Row
from typing import List, Optional from typing import List, Optional
import httpx
from loguru import logger from loguru import logger
from pydantic import BaseSettings, Field, validator from pydantic import BaseSettings, Field, validator
def list_parse_fallback(v): def list_parse_fallback(v):
try: try:
return json.loads(v) return json.loads(v)
except Exception as e: except Exception:
replaced = v.replace(" ", "") replaced = v.replace(" ", "")
if replaced: if replaced:
return replaced.split(",") return replaced.split(",")
@ -238,24 +237,36 @@ async def check_admin_settings():
http = "https" if settings.lnbits_force_https else "http" http = "https" if settings.lnbits_force_https else "http"
user = settings.lnbits_admin_users[0] user = settings.lnbits_admin_users[0]
admin_url = f"{http}://{settings.host}:{settings.port}/wallet?usr={user}" admin_url = (
f"{http}://{settings.host}:{settings.port}/wallet?usr={user}"
)
logger.warning(f"✔️ Access admin user account at: {admin_url}") logger.warning(f"✔️ Access admin user account at: {admin_url}")
if settings.lnbits_saas_callback and settings.lnbits_saas_secret and settings.lnbits_saas_instance_id: if (
settings.lnbits_saas_callback
and settings.lnbits_saas_secret
and settings.lnbits_saas_instance_id
):
with httpx.Client() as client: with httpx.Client() as client:
headers = { headers = {
"Content-Type": "application/json; charset=utf-8", "Content-Type": "application/json; charset=utf-8",
"X-API-KEY": settings.lnbits_saas_secret "X-API-KEY": settings.lnbits_saas_secret,
} }
payload = { payload = {
"instance_id": settings.lnbits_saas_instance_id, "instance_id": settings.lnbits_saas_instance_id,
"adminuser": user "adminuser": user,
} }
try: try:
r = client.post(settings.lnbits_saas_callback, headers=headers, json=payload) client.post(
settings.lnbits_saas_callback,
headers=headers,
json=payload,
)
logger.warning("sent admin user to saas application") logger.warning("sent admin user to saas application")
except: except:
logger.error(f"error sending admin user to saas: {settings.lnbits_saas_callback}") logger.error(
f"error sending admin user to saas: {settings.lnbits_saas_callback}"
)
except: except:
logger.error("admin.settings tables does not exist.") logger.error("admin.settings tables does not exist.")

View file

@ -19,7 +19,6 @@ from .base import (
class FakeWallet(Wallet): class FakeWallet(Wallet):
queue: asyncio.Queue = asyncio.Queue(0)
secret: str = settings.fake_wallet_secret secret: str = settings.fake_wallet_secret
privkey: str = hashlib.pbkdf2_hmac( privkey: str = hashlib.pbkdf2_hmac(
"sha256", "sha256",
@ -98,6 +97,7 @@ class FakeWallet(Wallet):
return PaymentStatus(None) return PaymentStatus(None)
async def paid_invoices_stream(self) -> AsyncGenerator[str, None]: async def paid_invoices_stream(self) -> AsyncGenerator[str, None]:
self.queue: asyncio.Queue = asyncio.Queue(0)
while True: while True:
value: Invoice = await self.queue.get() value: Invoice = await self.queue.get()
yield value.payment_hash yield value.payment_hash