Merge branch 'main' into fix/disapearingAdminExt

This commit is contained in:
Arc 2022-02-11 11:17:37 +00:00 committed by GitHub
commit 728ab517f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 59 additions and 36 deletions

View File

@ -1,15 +1,15 @@
import datetime
import json
from typing import Any, Dict, List, Optional
from urllib.parse import urlparse
import datetime
from uuid import uuid4
from typing import List, Optional, Dict, Any
from urllib.parse import urlparse
from lnbits import bolt11
from lnbits.db import COCKROACH, POSTGRES, Connection
from lnbits.db import Connection, POSTGRES, COCKROACH
from lnbits.settings import DEFAULT_WALLET_NAME, LNBITS_ADMIN_USERS
from . import db
from .models import BalanceCheck, Payment, User, Wallet
from .models import User, Wallet, Payment, BalanceCheck
# accounts
# --------
@ -53,7 +53,6 @@ async def get_user(user_id: str, conn: Optional[Connection] = None) -> Optional[
""",
(user_id,),
)
else:
return None

View File

@ -15,8 +15,8 @@ from lnbits.core.models import User
from lnbits.decorators import check_user_exists
from lnbits.helpers import template_renderer, url_for
from lnbits.settings import (
LNBITS_ADMIN_USERS,
LNBITS_ALLOWED_USERS,
LNBITS_ADMIN_USERS,
LNBITS_SITE_TITLE,
SERVICE_FEE,
)
@ -118,6 +118,8 @@ async def wallet(
return template_renderer().TemplateResponse(
"error.html", {"request": request, "err": "User not authorized."}
)
if LNBITS_ADMIN_USERS and user_id in LNBITS_ADMIN_USERS:
user.admin = True
if not wallet_id:
if user.wallets and not wallet_name:
wallet = user.wallets[0]

View File

@ -30,6 +30,6 @@ from .views import * # noqa
from .views_api import * # noqa
def lnticket_start():
def livestream_start():
loop = asyncio.get_event_loop()
loop.create_task(catch_everything_and_restart(wait_for_paid_invoices))

View File

@ -1,5 +1,5 @@
from http import HTTPStatus
from mmap import MAP_DENYWRITE
# from mmap import MAP_DENYWRITE
from fastapi.param_functions import Depends
from fastapi.params import Query

View File

@ -1,15 +1,17 @@
import asyncio
import json
from http import HTTPStatus
import httpx
from starlette.exceptions import HTTPException
from lnbits.core import db as core_db
from lnbits.core.models import Payment
from lnbits.tasks import register_invoice_listener
from lnbits.core.views.api import api_wallet
from lnbits.core.crud import get_wallet
from lnbits.core.views.api import api_payment, api_payments_decode, pay_invoice
from lnbits.core.models import Payment
from lnbits.core.services import pay_invoice
from lnbits.core.views.api import api_payments_decode
from lnbits.tasks import register_invoice_listener
from .crud import get_lnurlpayout, get_lnurlpayout_from_wallet
from .crud import get_lnurlpayout_from_wallet
async def wait_for_paid_invoices():
@ -25,16 +27,16 @@ async def on_invoice_paid(payment: Payment) -> None:
try:
# Check its got a payout associated with it
lnurlpayout_link = await get_lnurlpayout_from_wallet(payment.wallet_id)
print("LNURLpayout", lnurlpayout_link)
if lnurlpayout_link:
# Check the wallet balance is more than the threshold
wallet = await get_wallet(lnurlpayout_link.wallet)
if wallet.balance < lnurlpayout_link.threshold + (
lnurlpayout_link.threshold * 0.02
):
return
threshold = lnurlpayout_link.threshold + (lnurlpayout_link.threshold * 0.02)
if wallet.balance < threshold:
return
# Get the invoice from the LNURL to pay
async with httpx.AsyncClient() as client:
try:
@ -43,6 +45,7 @@ async def on_invoice_paid(payment: Payment) -> None:
raise HTTPException(
status_code=HTTPStatus.FORBIDDEN, detail="LNURL broken"
)
try:
r = await client.get(str(url["domain"]), timeout=40)
res = r.json()
@ -56,6 +59,12 @@ async def on_invoice_paid(payment: Payment) -> None:
timeout=40,
)
res = r.json()
if hasattr(res, "status") and res["status"] == "ERROR":
raise HTTPException(
status_code=HTTPStatus.FORBIDDEN,
detail=res["reason"],
)
try:
await pay_invoice(
wallet_id=payment.wallet_id,
@ -65,7 +74,9 @@ async def on_invoice_paid(payment: Payment) -> None:
return
except:
pass
except:
except Exception as e:
print("ERROR", str(e))
return
except (httpx.ConnectError, httpx.RequestError):
return

View File

@ -4,7 +4,8 @@ from fastapi import Query
from fastapi.params import Depends
from starlette.exceptions import HTTPException
from lnbits.core.crud import get_user, get_payments
from lnbits.core.crud import get_payments, get_user
from lnbits.core.models import Payment
from lnbits.core.services import create_invoice
from lnbits.core.views.api import api_payment, api_payments_decode
from lnbits.decorators import WalletTypeInfo, get_key_type, require_admin_key
@ -14,10 +15,10 @@ from .crud import (
create_lnurlpayout,
delete_lnurlpayout,
get_lnurlpayout,
get_lnurlpayouts,
get_lnurlpayout_from_wallet,
get_lnurlpayouts,
)
from .models import lnurlpayout, CreateLnurlPayoutData
from .models import CreateLnurlPayoutData, lnurlpayout
from .tasks import on_invoice_paid
@ -87,14 +88,24 @@ async def api_lnurlpayout_check(
lnurlpayout_id: str, wallet: WalletTypeInfo = Depends(get_key_type)
):
lnurlpayout = await get_lnurlpayout(lnurlpayout_id)
payments = await get_payments(
wallet_id=lnurlpayout.wallet,
complete=True,
## THIS
mock_payment = Payment(
checking_id="mock",
pending=False,
outgoing=True,
incoming=True,
amount=1,
fee=1,
time=0000,
bolt11="mock",
preimage="mock",
payment_hash="mock",
wallet_id=lnurlpayout.wallet,
)
result = await on_invoice_paid(payments[0])
## INSTEAD OF THIS
# payments = await get_payments(
# wallet_id=lnurlpayout.wallet, complete=True, pending=False, outgoing=True, incoming=True
# )
result = await on_invoice_paid(mock_payment)
return

View File

@ -22,7 +22,7 @@
<code>[&lt;withdraw_link_object&gt;, ...]</code>
<h5 class="text-caption q-mt-sm q-mb-none">Curl example</h5>
<code
>curl -X GET {{ request.base_url }}api/v1/links -H "X-Api-Key: {{
>curl -X GET {{ request.base_url }}withdraw/api/v1/links -H "X-Api-Key: {{
user.wallets[0].inkey }}"
</code>
</q-card-section>
@ -49,7 +49,7 @@
<code>{"lnurl": &lt;string&gt;}</code>
<h5 class="text-caption q-mt-sm q-mb-none">Curl example</h5>
<code
>curl -X GET {{ request.base_url }}api/v1/links/&lt;withdraw_id&gt; -H
>curl -X GET {{ request.base_url }}withdraw/api/v1/links/&lt;withdraw_id&gt; -H
"X-Api-Key: {{ user.wallets[0].inkey }}"
</code>
</q-card-section>
@ -78,7 +78,7 @@
<code>{"lnurl": &lt;string&gt;}</code>
<h5 class="text-caption q-mt-sm q-mb-none">Curl example</h5>
<code
>curl -X POST {{ request.base_url }}api/v1/links -d '{"title":
>curl -X POST {{ request.base_url }}withdraw/api/v1/links -d '{"title":
&lt;string&gt;, "min_withdrawable": &lt;integer&gt;,
"max_withdrawable": &lt;integer&gt;, "uses": &lt;integer&gt;,
"wait_time": &lt;integer&gt;, "is_unique": &lt;boolean&gt;}' -H
@ -114,7 +114,7 @@
<code>{"lnurl": &lt;string&gt;}</code>
<h5 class="text-caption q-mt-sm q-mb-none">Curl example</h5>
<code
>curl -X PUT {{ request.base_url }}api/v1/links/&lt;withdraw_id&gt; -d
>curl -X PUT {{ request.base_url }}withdraw/api/v1/links/&lt;withdraw_id&gt; -d
'{"title": &lt;string&gt;, "min_withdrawable": &lt;integer&gt;,
"max_withdrawable": &lt;integer&gt;, "uses": &lt;integer&gt;,
"wait_time": &lt;integer&gt;, "is_unique": &lt;boolean&gt;}' -H
@ -142,7 +142,7 @@
<code></code>
<h5 class="text-caption q-mt-sm q-mb-none">Curl example</h5>
<code
>curl -X DELETE {{ request.base_url }}api/v1/links/&lt;withdraw_id&gt;
>curl -X DELETE {{ request.base_url }}withdraw/api/v1/links/&lt;withdraw_id&gt;
-H "X-Api-Key: {{ user.wallets[0].adminkey }}"
</code>
</q-card-section>
@ -170,7 +170,7 @@
<h5 class="text-caption q-mt-sm q-mb-none">Curl example</h5>
<code
>curl -X GET {{ request.base_url
}}api/v1/links/&lt;the_hash&gt;/&lt;lnurl_id&gt; -H "X-Api-Key: {{
}}withdraw/api/v1/links/&lt;the_hash&gt;/&lt;lnurl_id&gt; -H "X-Api-Key: {{
user.wallets[0].inkey }}"
</code>
</q-card-section>
@ -191,7 +191,7 @@
>
<h5 class="text-caption q-mt-sm q-mb-none">Curl example</h5>
<code
>curl -X GET {{ request.base_url }}/withdraw/img/&lt;lnurl_id&gt;"
>curl -X GET {{ request.base_url }}withdraw/img/&lt;lnurl_id&gt;"
</code>
</q-card-section>
</q-card>