Compare commits
57
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
139bd96639 | ||
|
|
f6114fc33a | ||
|
|
399e01c6a8 | ||
|
|
abe1e2fb27 | ||
|
|
43900dd6da | ||
|
|
06c553219a | ||
|
|
fc73d83bd9 | ||
|
|
4cb743d952 | ||
|
|
3b6e87b060 | ||
|
|
09e44f18e5 | ||
|
|
cabb58f8fe | ||
|
|
aac04efd0e | ||
|
|
fd9009f760 | ||
|
|
6ab413775a | ||
|
|
7eb9965205 | ||
|
|
4634ad5a5a | ||
|
|
648aaa17c5 | ||
|
|
7db5c986b3 | ||
|
|
26c31b626d | ||
|
|
e39cb20525 | ||
|
|
3cea9a90d1 | ||
|
|
3224e2e774 | ||
|
|
cfc9874517 | ||
|
|
62cd151fd6 | ||
|
|
a4d0aa5db8 | ||
|
|
77630781d7 | ||
|
|
59da350cc9 | ||
|
|
29e980dd67 | ||
|
|
9d9ce63c82 | ||
|
|
748f458b8b | ||
|
|
ce177a73b1 | ||
|
|
2885e71be2 | ||
|
|
4b6f43d274 | ||
|
|
86b5cf9421 | ||
|
|
8f5b7d85aa | ||
|
|
4d51e63924 | ||
|
|
0ce2501e1a | ||
|
|
f2351145f0 | ||
|
|
2eb7d67b2a | ||
|
|
a82093b7ec | ||
|
|
ee595eede1 | ||
|
|
dfdce54e57 | ||
|
|
1367480ec6 | ||
|
|
e2d83b516a | ||
|
|
83699289fc | ||
|
|
f04e88d8bf | ||
|
|
564edfc447 | ||
|
|
b98515df14 | ||
|
|
1e0fc84586 | ||
|
|
a1d94834ae | ||
|
|
5de4239f3c | ||
|
|
c404666d7f | ||
|
|
190a466c0a | ||
|
|
d01e3523d8 | ||
|
|
88672501d8 | ||
|
|
ce57d08163 | ||
|
|
52304e0730 |
@@ -7,10 +7,6 @@ on:
|
|||||||
description: 'The tag name for the release'
|
description: 'The tag name for the release'
|
||||||
required: true
|
required: true
|
||||||
type: string
|
type: string
|
||||||
upload_url:
|
|
||||||
description: 'The upload URL for the release'
|
|
||||||
required: true
|
|
||||||
type: string
|
|
||||||
|
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
inputs:
|
inputs:
|
||||||
@@ -18,10 +14,6 @@ on:
|
|||||||
description: 'The tag name for the release'
|
description: 'The tag name for the release'
|
||||||
required: true
|
required: true
|
||||||
type: string
|
type: string
|
||||||
upload_url:
|
|
||||||
description: 'The upload URL for the release'
|
|
||||||
required: true
|
|
||||||
type: string
|
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build-linux-package:
|
build-linux-package:
|
||||||
@@ -113,11 +105,6 @@ jobs:
|
|||||||
shell: bash
|
shell: bash
|
||||||
|
|
||||||
- name: Upload Linux Release Asset
|
- name: Upload Linux Release Asset
|
||||||
uses: actions/upload-release-asset@v1
|
|
||||||
with:
|
|
||||||
upload_url: ${{ inputs.upload_url }}
|
|
||||||
asset_path: ${{ env.APPIMAGE_NAME }}
|
|
||||||
asset_name: ${{ env.APPIMAGE_NAME }}
|
|
||||||
asset_content_type: application/octet-stream
|
|
||||||
env:
|
env:
|
||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
run: gh release upload "${{ inputs.tag_name }}" "${{ env.APPIMAGE_NAME }}" --clobber
|
||||||
|
|||||||
@@ -10,15 +10,16 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
with:
|
with:
|
||||||
ref: ${{ github.head_ref }}
|
ref: ${{ github.event.pull_request.head.repo.full_name == github.repository && github.head_ref || github.event.pull_request.head.sha }}
|
||||||
- uses: lnbits/lnbits/.github/actions/prepare@dev
|
- uses: lnbits/lnbits/.github/actions/prepare@dev
|
||||||
with:
|
with:
|
||||||
python-version: "3.10"
|
python-version: "3.10"
|
||||||
node-version: "24.x"
|
node-version: "24.x"
|
||||||
npm: true
|
npm: true
|
||||||
- run: make bundle
|
- name: Build and commit bundle (same-repo PR)
|
||||||
- name: Commit and push bundle changes
|
if: github.event.pull_request.head.repo.full_name == github.repository
|
||||||
run: |
|
run: |
|
||||||
|
make bundle
|
||||||
git config user.name "alan"
|
git config user.name "alan"
|
||||||
git config user.email "alan@lnbits.com"
|
git config user.email "alan@lnbits.com"
|
||||||
git add lnbits/static
|
git add lnbits/static
|
||||||
@@ -27,3 +28,6 @@ jobs:
|
|||||||
fi
|
fi
|
||||||
git commit -m "chore: make bundle [skip ci]"
|
git commit -m "chore: make bundle [skip ci]"
|
||||||
git push
|
git push
|
||||||
|
- name: Check bundle is up-to-date (fork PR)
|
||||||
|
if: github.event.pull_request.head.repo.full_name != github.repository
|
||||||
|
run: make checkbundle
|
||||||
|
|||||||
@@ -66,6 +66,7 @@ jobs:
|
|||||||
BOLTZ_MNEMONIC: abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about
|
BOLTZ_MNEMONIC: abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about
|
||||||
LNBITS_MAX_OUTGOING_PAYMENT_AMOUNT_SATS: 1000000000
|
LNBITS_MAX_OUTGOING_PAYMENT_AMOUNT_SATS: 1000000000
|
||||||
LNBITS_MAX_INCOMING_PAYMENT_AMOUNT_SATS: 1000000000
|
LNBITS_MAX_INCOMING_PAYMENT_AMOUNT_SATS: 1000000000
|
||||||
|
LNBITS_FUNDING_SOURCE_PAY_INVOICE_WAIT_SECONDS: ${{ inputs.backend-wallet-class == 'CoreLightningRestWallet' && 60 || 5 }}
|
||||||
ECLAIR_PASS: lnbits
|
ECLAIR_PASS: lnbits
|
||||||
PYTHONUNBUFFERED: 1
|
PYTHONUNBUFFERED: 1
|
||||||
DEBUG: true
|
DEBUG: true
|
||||||
|
|||||||
@@ -12,8 +12,6 @@ jobs:
|
|||||||
|
|
||||||
release:
|
release:
|
||||||
runs-on: ubuntu-24.04
|
runs-on: ubuntu-24.04
|
||||||
outputs:
|
|
||||||
upload_url: ${{ steps.get_upload_url.outputs.upload_url }}
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
- name: Create github pre-release
|
- name: Create github pre-release
|
||||||
@@ -22,14 +20,6 @@ jobs:
|
|||||||
tag: ${{ github.ref_name }}
|
tag: ${{ github.ref_name }}
|
||||||
run: |
|
run: |
|
||||||
gh release create "$tag" --prerelease --generate-notes --draft
|
gh release create "$tag" --prerelease --generate-notes --draft
|
||||||
- id: get_upload_url
|
|
||||||
name: Get upload url of Github release
|
|
||||||
env:
|
|
||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
tag: ${{ github.ref_name }}
|
|
||||||
run: |
|
|
||||||
upload_url=$(gh release view "$tag" --json uploadUrl -q ".uploadUrl")
|
|
||||||
echo "upload_url=$upload_url" >> "$GITHUB_OUTPUT"
|
|
||||||
|
|
||||||
docker:
|
docker:
|
||||||
if: github.repository == 'lnbits/lnbits'
|
if: github.repository == 'lnbits/lnbits'
|
||||||
@@ -74,4 +64,3 @@ jobs:
|
|||||||
uses: ./.github/workflows/appimage.yml
|
uses: ./.github/workflows/appimage.yml
|
||||||
with:
|
with:
|
||||||
tag_name: ${{ github.ref_name }}
|
tag_name: ${{ github.ref_name }}
|
||||||
upload_url: ${{ needs.release.outputs.upload_url }}
|
|
||||||
|
|||||||
@@ -13,8 +13,6 @@ jobs:
|
|||||||
|
|
||||||
release:
|
release:
|
||||||
runs-on: ubuntu-24.04
|
runs-on: ubuntu-24.04
|
||||||
outputs:
|
|
||||||
upload_url: ${{ steps.get_upload_url.outputs.upload_url }}
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v4
|
- uses: actions/checkout@v4
|
||||||
- name: Create github release
|
- name: Create github release
|
||||||
@@ -23,14 +21,6 @@ jobs:
|
|||||||
tag: ${{ github.ref_name }}
|
tag: ${{ github.ref_name }}
|
||||||
run: |
|
run: |
|
||||||
gh release create "$tag" --generate-notes --draft
|
gh release create "$tag" --generate-notes --draft
|
||||||
- id: get_upload_url
|
|
||||||
name: Get upload url of Github release
|
|
||||||
env:
|
|
||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
tag: ${{ github.ref_name }}
|
|
||||||
run: |
|
|
||||||
upload_url=$(gh release view "$tag" --json uploadUrl -q ".uploadUrl")
|
|
||||||
echo "upload_url=$upload_url" >> "$GITHUB_OUTPUT"
|
|
||||||
|
|
||||||
docker:
|
docker:
|
||||||
if: github.repository == 'lnbits/lnbits'
|
if: github.repository == 'lnbits/lnbits'
|
||||||
@@ -85,4 +75,3 @@ jobs:
|
|||||||
uses: ./.github/workflows/appimage.yml
|
uses: ./.github/workflows/appimage.yml
|
||||||
with:
|
with:
|
||||||
tag_name: ${{ github.ref_name }}
|
tag_name: ${{ github.ref_name }}
|
||||||
upload_url: ${{ needs.release.outputs.upload_url }}
|
|
||||||
|
|||||||
+1
-1
@@ -43,4 +43,4 @@ ENV LNBITS_HOST="0.0.0.0"
|
|||||||
|
|
||||||
EXPOSE 5000
|
EXPOSE 5000
|
||||||
|
|
||||||
CMD ["sh", "-c", "uv --offline run lnbits --port $LNBITS_PORT --host $LNBITS_HOST --forwarded-allow-ips='*'"]
|
CMD ["sh", "-c", "uv --offline run --no-sync lnbits --port $LNBITS_PORT --host $LNBITS_HOST --forwarded-allow-ips='*'"]
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ nav_order: 1
|
|||||||
sudo apt-get install jq libfuse2
|
sudo apt-get install jq libfuse2
|
||||||
wget $(curl -s https://api.github.com/repos/lnbits/lnbits/releases/latest | jq -r '.assets[] | select(.name | endswith(".AppImage")) | .browser_download_url') -O LNbits-latest.AppImage
|
wget $(curl -s https://api.github.com/repos/lnbits/lnbits/releases/latest | jq -r '.assets[] | select(.name | endswith(".AppImage")) | .browser_download_url') -O LNbits-latest.AppImage
|
||||||
chmod +x LNbits-latest.AppImage
|
chmod +x LNbits-latest.AppImage
|
||||||
LNBITS_ADMIN_UI=true HOST=0.0.0.0 PORT=5000 ./LNbits-latest.AppImage # most system settings are now in the admin UI, but pass additional .env variables here
|
LNBITS_ADMIN_UI=true HOST=0.0.0.0 PORT=5000 AUTH_HTTPS_ONLY=false ./LNbits-latest.AppImage # most system settings are now in the admin UI, but pass additional .env variables here
|
||||||
```
|
```
|
||||||
|
|
||||||
- LNbits will create a folder for DB and extension files **in the same directory** as the AppImage.
|
- LNbits will create a folder for DB and extension files **in the same directory** as the AppImage.
|
||||||
@@ -285,10 +285,7 @@ but you can also set the env variables or pass command line arguments:
|
|||||||
|
|
||||||
```sh
|
```sh
|
||||||
# .env variables are currently passed when running, but LNbits can be managed with the admin UI.
|
# .env variables are currently passed when running, but LNbits can be managed with the admin UI.
|
||||||
LNBITS_ADMIN_UI=true ./result/bin/lnbits --port 9000 --host 0.0.0.0
|
LNBITS_ADMIN_UI=true AUTH_HTTPS_ONLY=false ./result/bin/lnbits --port 9000 --host 0.0.0.0
|
||||||
|
|
||||||
# Once you have created a user, you can set as the super_user
|
|
||||||
SUPER_USER=be54db7f245346c8833eaa430e1e0405 LNBITS_ADMIN_UI=true ./result/bin/lnbits --port 9000
|
|
||||||
```
|
```
|
||||||
|
|
||||||
> 
|
> 
|
||||||
|
|||||||
+55
-33
@@ -23,29 +23,35 @@ from lnbits.core.crud import (
|
|||||||
get_installed_extensions,
|
get_installed_extensions,
|
||||||
update_installed_extension_state,
|
update_installed_extension_state,
|
||||||
)
|
)
|
||||||
|
from lnbits.core.crud.audit import delete_expired_audit_entries
|
||||||
from lnbits.core.crud.extensions import create_installed_extension
|
from lnbits.core.crud.extensions import create_installed_extension
|
||||||
from lnbits.core.helpers import migrate_extension_database
|
from lnbits.core.helpers import migrate_extension_database
|
||||||
from lnbits.core.models.notifications import NotificationType
|
from lnbits.core.models.notifications import NotificationType
|
||||||
from lnbits.core.services.extensions import deactivate_extension, get_valid_extensions
|
from lnbits.core.services.extensions import deactivate_extension, get_valid_extensions
|
||||||
from lnbits.core.services.notifications import enqueue_admin_notification
|
from lnbits.core.services.funding_source import (
|
||||||
from lnbits.core.services.payments import check_pending_payments
|
check_balance_delta_changed,
|
||||||
|
check_server_balance_against_node,
|
||||||
|
)
|
||||||
|
from lnbits.core.services.notifications import (
|
||||||
|
dispatch_payment_notification,
|
||||||
|
enqueue_admin_notification,
|
||||||
|
process_next_notification,
|
||||||
|
)
|
||||||
|
from lnbits.core.services.payments import (
|
||||||
|
check_pending_payments,
|
||||||
|
fundingsource_invoice_producer,
|
||||||
|
)
|
||||||
from lnbits.core.tasks import (
|
from lnbits.core.tasks import (
|
||||||
audit_queue,
|
audit_queue,
|
||||||
collect_exchange_rates_data,
|
collect_exchange_rates_data,
|
||||||
purge_audit_data,
|
notify_server_status,
|
||||||
run_by_the_minute_tasks,
|
process_next_audit_entry,
|
||||||
wait_for_audit_data,
|
refresh_extension_cache,
|
||||||
wait_for_paid_invoices,
|
|
||||||
wait_notification_messages,
|
|
||||||
)
|
)
|
||||||
from lnbits.exceptions import register_exception_handlers
|
from lnbits.exceptions import register_exception_handlers
|
||||||
from lnbits.helpers import version_parse
|
from lnbits.helpers import version_parse
|
||||||
|
from lnbits.llms_txt import create_llms_txt_route
|
||||||
from lnbits.settings import settings
|
from lnbits.settings import settings
|
||||||
from lnbits.tasks import (
|
|
||||||
cancel_all_tasks,
|
|
||||||
create_permanent_task,
|
|
||||||
register_invoice_listener,
|
|
||||||
)
|
|
||||||
from lnbits.utils.cache import cache
|
from lnbits.utils.cache import cache
|
||||||
from lnbits.utils.logger import (
|
from lnbits.utils.logger import (
|
||||||
configure_logger,
|
configure_logger,
|
||||||
@@ -68,7 +74,7 @@ from .middleware import (
|
|||||||
add_profiler_middleware,
|
add_profiler_middleware,
|
||||||
add_ratelimit_middleware,
|
add_ratelimit_middleware,
|
||||||
)
|
)
|
||||||
from .tasks import internal_invoice_listener, invoice_listener, run_interval
|
from .task_manager import task_manager
|
||||||
|
|
||||||
|
|
||||||
async def startup(app: FastAPI):
|
async def startup(app: FastAPI):
|
||||||
@@ -102,6 +108,9 @@ async def startup(app: FastAPI):
|
|||||||
# register core routes
|
# register core routes
|
||||||
init_core_routers(app)
|
init_core_routers(app)
|
||||||
|
|
||||||
|
# register llms.txt endpoint for AI agents
|
||||||
|
create_llms_txt_route(app)
|
||||||
|
|
||||||
# initialize tasks
|
# initialize tasks
|
||||||
register_async_tasks()
|
register_async_tasks()
|
||||||
|
|
||||||
@@ -129,7 +138,7 @@ async def shutdown():
|
|||||||
settings.lnbits_running = False
|
settings.lnbits_running = False
|
||||||
|
|
||||||
# shutdown event
|
# shutdown event
|
||||||
cancel_all_tasks()
|
task_manager.cancel_all_tasks()
|
||||||
|
|
||||||
# wait a bit to allow them to finish, so that cleanup can run without problems
|
# wait a bit to allow them to finish, so that cleanup can run without problems
|
||||||
await asyncio.sleep(0.1)
|
await asyncio.sleep(0.1)
|
||||||
@@ -469,29 +478,42 @@ async def check_and_register_extensions(app: FastAPI) -> None:
|
|||||||
|
|
||||||
def register_async_tasks() -> None:
|
def register_async_tasks() -> None:
|
||||||
|
|
||||||
create_permanent_task(wait_for_audit_data)
|
task_manager.init()
|
||||||
create_permanent_task(wait_notification_messages)
|
|
||||||
|
|
||||||
create_permanent_task(
|
# listen to all incoming payments and dispatch payment notifications
|
||||||
run_interval(
|
# note: should be the first in task list for a bit quicker notifications
|
||||||
settings.lnbits_funding_source_pending_interval_seconds,
|
task_manager.register_invoice_listener(dispatch_payment_notification, "core")
|
||||||
check_pending_payments,
|
|
||||||
)
|
# periodic tasks
|
||||||
|
task_manager.create_permanent_task(cache.invalidate_cache, interval=10)
|
||||||
|
task_manager.create_permanent_task(delete_expired_audit_entries, interval=60 * 60)
|
||||||
|
task_manager.create_permanent_task(
|
||||||
|
check_pending_payments,
|
||||||
|
interval=settings.lnbits_funding_source_pending_interval_seconds,
|
||||||
)
|
)
|
||||||
create_permanent_task(invoice_listener)
|
task_manager.create_permanent_task(
|
||||||
create_permanent_task(internal_invoice_listener)
|
collect_exchange_rates_data,
|
||||||
create_permanent_task(cache.invalidate_forever)
|
interval=max(60, settings.lnbits_exchange_history_refresh_interval_seconds),
|
||||||
|
)
|
||||||
|
task_manager.create_permanent_task(check_balance_delta_changed, interval=60)
|
||||||
|
task_manager.create_permanent_task(
|
||||||
|
check_server_balance_against_node,
|
||||||
|
interval=60 * settings.lnbits_watchdog_interval_minutes,
|
||||||
|
)
|
||||||
|
task_manager.create_permanent_task(
|
||||||
|
notify_server_status,
|
||||||
|
interval=60 * 60 * settings.lnbits_notification_server_status_hours,
|
||||||
|
)
|
||||||
|
task_manager.create_permanent_task(refresh_extension_cache, interval=60)
|
||||||
|
|
||||||
# core invoice listener
|
# permanent tasks run in a loop, will be restarted if they fail
|
||||||
invoice_queue: asyncio.Queue = asyncio.Queue()
|
task_manager.create_permanent_task(fundingsource_invoice_producer)
|
||||||
register_invoice_listener(invoice_queue, "core")
|
task_manager.create_permanent_task(process_next_notification)
|
||||||
create_permanent_task(lambda: wait_for_paid_invoices(invoice_queue))
|
task_manager.create_permanent_task(process_next_audit_entry)
|
||||||
|
|
||||||
create_permanent_task(run_by_the_minute_tasks)
|
|
||||||
create_permanent_task(purge_audit_data)
|
|
||||||
create_permanent_task(collect_exchange_rates_data)
|
|
||||||
|
|
||||||
# server logs for websocket
|
# server logs for websocket
|
||||||
if settings.lnbits_admin_ui:
|
if settings.lnbits_admin_ui:
|
||||||
server_log_task = initialize_server_websocket_logger()
|
server_log_task = initialize_server_websocket_logger()
|
||||||
create_permanent_task(server_log_task)
|
task_manager.create_permanent_task(
|
||||||
|
server_log_task, name="server_websocket_logger"
|
||||||
|
)
|
||||||
|
|||||||
@@ -290,6 +290,7 @@ async def create_payment(
|
|||||||
webhook=data.webhook,
|
webhook=data.webhook,
|
||||||
fee=-abs(data.fee),
|
fee=-abs(data.fee),
|
||||||
tag=extra.get("tag", None),
|
tag=extra.get("tag", None),
|
||||||
|
extension=data.extension,
|
||||||
extra=extra,
|
extra=extra,
|
||||||
labels=data.labels or [],
|
labels=data.labels or [],
|
||||||
external_id=data.external_id,
|
external_id=data.external_id,
|
||||||
@@ -306,7 +307,7 @@ async def update_payment_checking_id(
|
|||||||
await (conn or db).execute(
|
await (conn or db).execute(
|
||||||
f"""
|
f"""
|
||||||
UPDATE apipayments
|
UPDATE apipayments
|
||||||
SET checking_id = :new_id, updated_at = {db.timestamp_placeholder('now')}
|
SET checking_id = :new_id, updated_at = {db.timestamp_placeholder("now")}
|
||||||
WHERE checking_id = :old_id
|
WHERE checking_id = :old_id
|
||||||
""", # noqa: S608
|
""", # noqa: S608
|
||||||
{
|
{
|
||||||
@@ -321,13 +322,15 @@ async def update_payment(
|
|||||||
payment: Payment,
|
payment: Payment,
|
||||||
new_checking_id: str | None = None,
|
new_checking_id: str | None = None,
|
||||||
conn: Connection | None = None,
|
conn: Connection | None = None,
|
||||||
) -> None:
|
) -> Payment:
|
||||||
payment.updated_at = datetime.now(timezone.utc)
|
payment.updated_at = datetime.now(timezone.utc)
|
||||||
await (conn or db).update(
|
await (conn or db).update(
|
||||||
"apipayments", payment, "WHERE checking_id = :checking_id"
|
"apipayments", payment, "WHERE checking_id = :checking_id"
|
||||||
)
|
)
|
||||||
if new_checking_id and new_checking_id != payment.checking_id:
|
if new_checking_id and new_checking_id != payment.checking_id:
|
||||||
await update_payment_checking_id(payment.checking_id, new_checking_id, conn)
|
await update_payment_checking_id(payment.checking_id, new_checking_id, conn)
|
||||||
|
payment.checking_id = new_checking_id
|
||||||
|
return payment
|
||||||
|
|
||||||
|
|
||||||
async def get_payments_history(
|
async def get_payments_history(
|
||||||
@@ -399,7 +402,6 @@ async def get_payment_count_stats(
|
|||||||
user_id: str | None = None,
|
user_id: str | None = None,
|
||||||
conn: Connection | None = None,
|
conn: Connection | None = None,
|
||||||
) -> list[PaymentCountStat]:
|
) -> list[PaymentCountStat]:
|
||||||
|
|
||||||
if not filters:
|
if not filters:
|
||||||
filters = Filters()
|
filters = Filters()
|
||||||
extra_stmts = []
|
extra_stmts = []
|
||||||
@@ -432,7 +434,6 @@ async def get_daily_stats(
|
|||||||
user_id: str | None = None,
|
user_id: str | None = None,
|
||||||
conn: Connection | None = None,
|
conn: Connection | None = None,
|
||||||
) -> tuple[list[PaymentDailyStats], list[PaymentDailyStats]]:
|
) -> tuple[list[PaymentDailyStats], list[PaymentDailyStats]]:
|
||||||
|
|
||||||
if not filters:
|
if not filters:
|
||||||
filters = Filters()
|
filters = Filters()
|
||||||
|
|
||||||
@@ -482,7 +483,6 @@ async def get_wallets_stats(
|
|||||||
user_id: str | None = None,
|
user_id: str | None = None,
|
||||||
conn: Connection | None = None,
|
conn: Connection | None = None,
|
||||||
) -> list[PaymentWalletStats]:
|
) -> list[PaymentWalletStats]:
|
||||||
|
|
||||||
if not filters:
|
if not filters:
|
||||||
filters = Filters()
|
filters = Filters()
|
||||||
|
|
||||||
|
|||||||
@@ -8,11 +8,18 @@ from lnbits.db import dict_to_model
|
|||||||
from lnbits.settings import (
|
from lnbits.settings import (
|
||||||
AdminSettings,
|
AdminSettings,
|
||||||
EditableSettings,
|
EditableSettings,
|
||||||
|
FundingSourcesSettings,
|
||||||
SettingsField,
|
SettingsField,
|
||||||
SuperSettings,
|
SuperSettings,
|
||||||
settings,
|
settings,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
RESET_PRESERVED_SETTINGS = (
|
||||||
|
"lnbits_webpush_pubkey",
|
||||||
|
"lnbits_webpush_privkey",
|
||||||
|
*FundingSourcesSettings.__fields__,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def get_super_settings() -> SuperSettings | None:
|
async def get_super_settings() -> SuperSettings | None:
|
||||||
data = await get_settings_by_tag("core")
|
data = await get_settings_by_tag("core")
|
||||||
@@ -69,16 +76,14 @@ async def delete_admin_settings(tag: str | None = "core") -> None:
|
|||||||
|
|
||||||
|
|
||||||
async def reset_core_settings() -> None:
|
async def reset_core_settings() -> None:
|
||||||
await db.execute(
|
core_settings = await get_settings_by_tag("core") or {}
|
||||||
"""
|
super_user = await get_settings_field("super_user")
|
||||||
DELETE FROM system_settings WHERE tag = 'core'
|
await delete_admin_settings()
|
||||||
AND id NOT IN (
|
if super_user:
|
||||||
'super_user',
|
await set_settings_field("super_user", super_user.value)
|
||||||
'lnbits_webpush_pubkey',
|
for field in RESET_PRESERVED_SETTINGS:
|
||||||
'lnbits_webpush_privkey'
|
if field in core_settings:
|
||||||
)
|
await set_settings_field(field, core_settings[field])
|
||||||
""",
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
async def create_admin_settings(super_user: str, new_settings: dict) -> SuperSettings:
|
async def create_admin_settings(super_user: str, new_settings: dict) -> SuperSettings:
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ from .payments import (
|
|||||||
PaymentState,
|
PaymentState,
|
||||||
PaymentWalletStats,
|
PaymentWalletStats,
|
||||||
SettleInvoice,
|
SettleInvoice,
|
||||||
|
UpdatePaymentExtra,
|
||||||
)
|
)
|
||||||
from .tinyurl import TinyURL
|
from .tinyurl import TinyURL
|
||||||
from .users import (
|
from .users import (
|
||||||
@@ -90,6 +91,7 @@ __all__ = [
|
|||||||
"SimpleStatus",
|
"SimpleStatus",
|
||||||
"TinyURL",
|
"TinyURL",
|
||||||
"UpdateBalance",
|
"UpdateBalance",
|
||||||
|
"UpdatePaymentExtra",
|
||||||
"UpdateSuperuserPassword",
|
"UpdateSuperuserPassword",
|
||||||
"UpdateUser",
|
"UpdateUser",
|
||||||
"UpdateUserPassword",
|
"UpdateUserPassword",
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import json
|
|||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
import zipfile
|
import zipfile
|
||||||
from asyncio.tasks import create_task
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
@@ -21,6 +20,7 @@ from lnbits.helpers import (
|
|||||||
version_parse,
|
version_parse,
|
||||||
)
|
)
|
||||||
from lnbits.settings import settings
|
from lnbits.settings import settings
|
||||||
|
from lnbits.task_manager import task_manager
|
||||||
from lnbits.utils.cache import cache
|
from lnbits.utils.cache import cache
|
||||||
|
|
||||||
|
|
||||||
@@ -55,9 +55,10 @@ class GitHubRelease(BaseModel):
|
|||||||
|
|
||||||
|
|
||||||
class Manifest(BaseModel):
|
class Manifest(BaseModel):
|
||||||
featured: list[str] = []
|
|
||||||
extensions: list[ExplicitRelease] = []
|
extensions: list[ExplicitRelease] = []
|
||||||
repos: list[GitHubRelease] = []
|
repos: list[GitHubRelease] = []
|
||||||
|
featured: list[str] = []
|
||||||
|
categories: dict[str, list[str]] = {}
|
||||||
|
|
||||||
|
|
||||||
class GitHubRepoRelease(BaseModel):
|
class GitHubRepoRelease(BaseModel):
|
||||||
@@ -308,7 +309,6 @@ class ExtensionRelease(BaseModel):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
async def fetch_release_details(cls, details_link: str) -> dict | None:
|
async def fetch_release_details(cls, details_link: str) -> dict | None:
|
||||||
|
|
||||||
try:
|
try:
|
||||||
async with httpx.AsyncClient() as client:
|
async with httpx.AsyncClient() as client:
|
||||||
resp = await client.get(details_link)
|
resp = await client.get(details_link)
|
||||||
@@ -333,6 +333,7 @@ class ExtensionMeta(BaseModel):
|
|||||||
dependencies: list[str] = []
|
dependencies: list[str] = []
|
||||||
archive: str | None = None
|
archive: str | None = None
|
||||||
featured: bool = False
|
featured: bool = False
|
||||||
|
categories: list[str] = []
|
||||||
paid_features: str | None = None
|
paid_features: str | None = None
|
||||||
has_paid_release: bool = False
|
has_paid_release: bool = False
|
||||||
has_free_release: bool = False
|
has_free_release: bool = False
|
||||||
@@ -641,7 +642,10 @@ class InstallableExtension(BaseModel):
|
|||||||
|
|
||||||
if cache_value.older_than(10 * 60) or post_refresh_cache:
|
if cache_value.older_than(10 * 60) or post_refresh_cache:
|
||||||
# refresh cache in background if older than 10 minutes or requested
|
# refresh cache in background if older than 10 minutes or requested
|
||||||
create_task(cls._refresh_installable_extensions_cache())
|
task_manager.create_task(
|
||||||
|
cls._refresh_installable_extensions_cache(),
|
||||||
|
"refresh_installable_extensions_cache",
|
||||||
|
)
|
||||||
|
|
||||||
extension_list = cache_value.value # type: ignore
|
extension_list = cache_value.value # type: ignore
|
||||||
return extension_list
|
return extension_list
|
||||||
@@ -680,6 +684,11 @@ class InstallableExtension(BaseModel):
|
|||||||
|
|
||||||
meta = ext.meta or ExtensionMeta()
|
meta = ext.meta or ExtensionMeta()
|
||||||
meta.featured = ext.id in manifest.featured
|
meta.featured = ext.id in manifest.featured
|
||||||
|
meta.categories = [
|
||||||
|
category
|
||||||
|
for category, ext_ids in manifest.categories.items()
|
||||||
|
if ext.id in ext_ids
|
||||||
|
]
|
||||||
ext.meta = meta
|
ext.meta = meta
|
||||||
extension_list += [ext]
|
extension_list += [ext]
|
||||||
|
|
||||||
@@ -695,6 +704,11 @@ class InstallableExtension(BaseModel):
|
|||||||
ext.check_release_updates(release)
|
ext.check_release_updates(release)
|
||||||
meta = ext.meta or ExtensionMeta()
|
meta = ext.meta or ExtensionMeta()
|
||||||
meta.featured = ext.id in manifest.featured
|
meta.featured = ext.id in manifest.featured
|
||||||
|
meta.categories = [
|
||||||
|
category
|
||||||
|
for category, ext_ids in manifest.categories.items()
|
||||||
|
if ext.id in ext_ids
|
||||||
|
]
|
||||||
ext.meta = meta
|
ext.meta = meta
|
||||||
extension_list += [ext]
|
extension_list += [ext]
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ class SimpleStatus(BaseModel):
|
|||||||
class SimpleItem(BaseModel):
|
class SimpleItem(BaseModel):
|
||||||
id: str
|
id: str
|
||||||
name: str
|
name: str
|
||||||
|
expires_at: int | None = None
|
||||||
|
|
||||||
|
|
||||||
class DbVersion(BaseModel):
|
class DbVersion(BaseModel):
|
||||||
|
|||||||
@@ -35,6 +35,11 @@ class PaymentExtra(BaseModel):
|
|||||||
lnurl_response: str | None = None
|
lnurl_response: str | None = None
|
||||||
|
|
||||||
|
|
||||||
|
class UpdatePaymentExtra(BaseModel):
|
||||||
|
payment_hash: str
|
||||||
|
extra: dict = Field(default_factory=dict)
|
||||||
|
|
||||||
|
|
||||||
class PayInvoice(BaseModel):
|
class PayInvoice(BaseModel):
|
||||||
payment_request: str
|
payment_request: str
|
||||||
description: str | None = None
|
description: str | None = None
|
||||||
@@ -49,6 +54,7 @@ class CreatePayment(BaseModel):
|
|||||||
amount_msat: int
|
amount_msat: int
|
||||||
memo: str
|
memo: str
|
||||||
extra: dict | None = {}
|
extra: dict | None = {}
|
||||||
|
extension: str | None = None
|
||||||
preimage: str | None = None
|
preimage: str | None = None
|
||||||
expiry: datetime | None = None
|
expiry: datetime | None = None
|
||||||
webhook: str | None = None
|
webhook: str | None = None
|
||||||
@@ -135,22 +141,18 @@ class Payment(BaseModel):
|
|||||||
)
|
)
|
||||||
|
|
||||||
# DEPRECATED: in v1.5.0, use service check_payment_status instead
|
# DEPRECATED: in v1.5.0, use service check_payment_status instead
|
||||||
async def check_status(
|
async def check_status(self) -> PaymentStatus:
|
||||||
self, skip_internal_payment_notifications: bool | None = False
|
|
||||||
) -> PaymentStatus:
|
|
||||||
logger.warning("payment.check_status() is deprecated.")
|
logger.warning("payment.check_status() is deprecated.")
|
||||||
from lnbits.core.services.payments import check_payment_status
|
from lnbits.core.services.payments import check_payment_status
|
||||||
|
|
||||||
return await check_payment_status(self, skip_internal_payment_notifications)
|
return await check_payment_status(self)
|
||||||
|
|
||||||
# DEPRECATED: in v1.5.0, use service check_payment_status instead
|
# DEPRECATED: in v1.5.0, use service check_payment_status instead
|
||||||
async def check_fiat_status(
|
async def check_fiat_status(self) -> FiatPaymentStatus:
|
||||||
self, skip_internal_payment_notifications: bool | None = False
|
|
||||||
) -> FiatPaymentStatus:
|
|
||||||
logger.warning("payment.check_fiat_status() is deprecated.")
|
logger.warning("payment.check_fiat_status() is deprecated.")
|
||||||
from lnbits.core.services.fiat_providers import check_fiat_status
|
from lnbits.core.services.fiat_providers import check_fiat_status
|
||||||
|
|
||||||
return await check_fiat_status(self, skip_internal_payment_notifications)
|
return await check_fiat_status(self)
|
||||||
|
|
||||||
|
|
||||||
class PaymentFilters(FilterModel):
|
class PaymentFilters(FilterModel):
|
||||||
@@ -258,6 +260,7 @@ class CreateInvoice(BaseModel):
|
|||||||
)
|
)
|
||||||
expiry: int | None = None
|
expiry: int | None = None
|
||||||
extra: dict | None = None
|
extra: dict | None = None
|
||||||
|
extension: str | None = None
|
||||||
webhook: str | None = None
|
webhook: str | None = None
|
||||||
bolt11: str | None = None
|
bolt11: str | None = None
|
||||||
lnurl_withdraw: LnurlWithdrawResponse | None = None
|
lnurl_withdraw: LnurlWithdrawResponse | None = None
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import httpx
|
|||||||
from loguru import logger
|
from loguru import logger
|
||||||
|
|
||||||
from lnbits.core.crud import get_wallet
|
from lnbits.core.crud import get_wallet
|
||||||
from lnbits.core.crud.payments import create_payment
|
from lnbits.core.crud.payments import create_payment, update_payment
|
||||||
from lnbits.core.models import CreatePayment, Payment, PaymentState
|
from lnbits.core.models import CreatePayment, Payment, PaymentState
|
||||||
from lnbits.core.models.misc import SimpleStatus
|
from lnbits.core.models.misc import SimpleStatus
|
||||||
from lnbits.db import Connection
|
from lnbits.db import Connection
|
||||||
@@ -20,6 +20,7 @@ from lnbits.fiat.base import (
|
|||||||
FiatPaymentSuccessStatus,
|
FiatPaymentSuccessStatus,
|
||||||
)
|
)
|
||||||
from lnbits.settings import settings
|
from lnbits.settings import settings
|
||||||
|
from lnbits.task_manager import task_manager
|
||||||
|
|
||||||
|
|
||||||
async def handle_fiat_payment_confirmation(
|
async def handle_fiat_payment_confirmation(
|
||||||
@@ -36,9 +37,7 @@ async def handle_fiat_payment_confirmation(
|
|||||||
logger.warning(e)
|
logger.warning(e)
|
||||||
|
|
||||||
|
|
||||||
async def check_fiat_status(
|
async def check_fiat_status(payment: Payment) -> FiatPaymentStatus:
|
||||||
payment: Payment, skip_internal_payment_notifications: bool | None = False
|
|
||||||
) -> FiatPaymentStatus:
|
|
||||||
if not payment.is_internal:
|
if not payment.is_internal:
|
||||||
return FiatPaymentPendingStatus()
|
return FiatPaymentPendingStatus()
|
||||||
if payment.success:
|
if payment.success:
|
||||||
@@ -58,14 +57,11 @@ async def check_fiat_status(
|
|||||||
return FiatPaymentPendingStatus()
|
return FiatPaymentPendingStatus()
|
||||||
fiat_status = await fiat_provider.get_invoice_status(checking_id)
|
fiat_status = await fiat_provider.get_invoice_status(checking_id)
|
||||||
|
|
||||||
if skip_internal_payment_notifications:
|
|
||||||
return fiat_status
|
|
||||||
|
|
||||||
if fiat_status.success:
|
if fiat_status.success:
|
||||||
# notify receivers asynchronously
|
payment.status = PaymentState.SUCCESS.value
|
||||||
from lnbits.tasks import internal_invoice_queue
|
await update_payment(payment)
|
||||||
|
await handle_fiat_payment_confirmation(payment)
|
||||||
await internal_invoice_queue.put(payment.checking_id)
|
task_manager.internal_invoice_queue.put_nowait(payment)
|
||||||
|
|
||||||
return fiat_status
|
return fiat_status
|
||||||
|
|
||||||
|
|||||||
@@ -66,6 +66,8 @@ async def check_server_balance_against_node():
|
|||||||
|
|
||||||
|
|
||||||
async def check_balance_delta_changed():
|
async def check_balance_delta_changed():
|
||||||
|
if settings.notification_balance_delta_threshold_sats <= 0:
|
||||||
|
return
|
||||||
status = await get_balance_delta()
|
status = await get_balance_delta()
|
||||||
if settings.latest_balance_delta_sats is None:
|
if settings.latest_balance_delta_sats is None:
|
||||||
settings.latest_balance_delta_sats = status.delta_sats
|
settings.latest_balance_delta_sats = status.delta_sats
|
||||||
|
|||||||
@@ -74,7 +74,7 @@ async def send_admin_notification(
|
|||||||
message: str,
|
message: str,
|
||||||
message_type: str | None = None,
|
message_type: str | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
return await send_notification(
|
return await send_notification_in_background(
|
||||||
settings.lnbits_telegram_notifications_chat_id,
|
settings.lnbits_telegram_notifications_chat_id,
|
||||||
settings.lnbits_nostr_notifications_identifiers,
|
settings.lnbits_nostr_notifications_identifiers,
|
||||||
settings.lnbits_email_notifications_to_emails,
|
settings.lnbits_email_notifications_to_emails,
|
||||||
@@ -97,7 +97,7 @@ async def send_user_notification(
|
|||||||
if user_notifications.nostr_identifier
|
if user_notifications.nostr_identifier
|
||||||
else []
|
else []
|
||||||
)
|
)
|
||||||
return await send_notification(
|
return await send_notification_in_background(
|
||||||
user_notifications.telegram_chat_id,
|
user_notifications.telegram_chat_id,
|
||||||
nostr_identifiers,
|
nostr_identifiers,
|
||||||
email_address,
|
email_address,
|
||||||
@@ -222,12 +222,29 @@ async def send_email(
|
|||||||
msg["Subject"] = subject
|
msg["Subject"] = subject
|
||||||
msg.attach(MIMEText(message, "plain"))
|
msg.attach(MIMEText(message, "plain"))
|
||||||
username = username if len(username) > 0 else from_email
|
username = username if len(username) > 0 else from_email
|
||||||
with smtplib.SMTP(server, port) as smtp_server:
|
|
||||||
smtp_server.starttls()
|
def _send() -> bool:
|
||||||
smtp_server.login(username, password)
|
with smtplib.SMTP(server, port) as smtp_server:
|
||||||
smtp_server.sendmail(from_email, to_emails, msg.as_string())
|
smtp_server.starttls()
|
||||||
|
smtp_server.login(username, password)
|
||||||
|
smtp_server.sendmail(from_email, to_emails, msg.as_string())
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
try:
|
||||||
|
return await asyncio.to_thread(_send)
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"Sending Email failed. {e!s}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
async def dispatch_payment_notification(payment: Payment) -> None:
|
||||||
|
"""
|
||||||
|
This worker dispatches the payment notifications.
|
||||||
|
"""
|
||||||
|
wallet = await get_wallet(payment.wallet_id)
|
||||||
|
if wallet:
|
||||||
|
await send_payment_notification(wallet, payment)
|
||||||
|
|
||||||
|
|
||||||
async def dispatch_webhook(payment: Payment):
|
async def dispatch_webhook(payment: Payment):
|
||||||
"""
|
"""
|
||||||
@@ -294,6 +311,27 @@ def send_payment_notification_in_background(wallet: Wallet, payment: Payment):
|
|||||||
logger.warning(f"Error sending payment notification: {e}")
|
logger.warning(f"Error sending payment notification: {e}")
|
||||||
|
|
||||||
|
|
||||||
|
async def send_notification_in_background(
|
||||||
|
telegram_chat_id: str | None,
|
||||||
|
nostr_identifiers: list[str] | None,
|
||||||
|
email_addresses: list[str] | None,
|
||||||
|
message: str,
|
||||||
|
message_type: str | None = None,
|
||||||
|
):
|
||||||
|
try:
|
||||||
|
create_task(
|
||||||
|
send_notification(
|
||||||
|
telegram_chat_id,
|
||||||
|
nostr_identifiers,
|
||||||
|
email_addresses,
|
||||||
|
message,
|
||||||
|
message_type,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"Error sending notification in background: {e}")
|
||||||
|
|
||||||
|
|
||||||
async def send_ws_payment_notification(wallet: Wallet, payment: Payment):
|
async def send_ws_payment_notification(wallet: Wallet, payment: Payment):
|
||||||
# TODO: websocket message should be a clean payment model
|
# TODO: websocket message should be a clean payment model
|
||||||
# await websocket_manager.send(wallet.inkey, payment.json())
|
# await websocket_manager.send(wallet.inkey, payment.json())
|
||||||
|
|||||||
@@ -13,13 +13,13 @@ from lnbits.core.crud.payments import get_daily_stats
|
|||||||
from lnbits.core.db import db
|
from lnbits.core.db import db
|
||||||
from lnbits.core.models import PaymentDailyStats, PaymentFilters
|
from lnbits.core.models import PaymentDailyStats, PaymentFilters
|
||||||
from lnbits.core.models.payments import CreateInvoice
|
from lnbits.core.models.payments import CreateInvoice
|
||||||
from lnbits.core.services.fiat_providers import handle_fiat_payment_confirmation
|
|
||||||
from lnbits.db import Connection, Filters
|
from lnbits.db import Connection, Filters
|
||||||
from lnbits.decorators import check_user_extension_access
|
from lnbits.decorators import check_user_extension_access
|
||||||
from lnbits.exceptions import InvoiceError, PaymentError, UnsupportedError
|
from lnbits.exceptions import InvoiceError, PaymentError, UnsupportedError
|
||||||
from lnbits.fiat import get_fiat_provider
|
from lnbits.fiat import get_fiat_provider
|
||||||
from lnbits.helpers import check_callback_url
|
from lnbits.helpers import check_callback_url, daystart_timestamp
|
||||||
from lnbits.settings import settings
|
from lnbits.settings import settings
|
||||||
|
from lnbits.task_manager import task_manager
|
||||||
from lnbits.utils.crypto import fake_privkey, random_secret_and_hash, verify_preimage
|
from lnbits.utils.crypto import fake_privkey, random_secret_and_hash, verify_preimage
|
||||||
from lnbits.utils.exchange_rates import fiat_amount_as_satoshis, satoshis_amount_as_fiat
|
from lnbits.utils.exchange_rates import fiat_amount_as_satoshis, satoshis_amount_as_fiat
|
||||||
from lnbits.wallets import fake_wallet, get_funding_source
|
from lnbits.wallets import fake_wallet, get_funding_source
|
||||||
@@ -171,15 +171,15 @@ async def create_fiat_invoice(
|
|||||||
|
|
||||||
internal_payment.fiat_provider = fiat_provider_name
|
internal_payment.fiat_provider = fiat_provider_name
|
||||||
internal_payment.extra["fiat_checking_id"] = fiat_invoice.checking_id
|
internal_payment.extra["fiat_checking_id"] = fiat_invoice.checking_id
|
||||||
# todo: move to payent
|
# TODO: move to payment
|
||||||
internal_payment.extra["fiat_payment_request"] = fiat_invoice.payment_request
|
internal_payment.extra["fiat_payment_request"] = fiat_invoice.payment_request
|
||||||
new_checking_id = (
|
new_checking_id = (
|
||||||
f"fiat_{fiat_provider_name}_"
|
f"fiat_{fiat_provider_name}_"
|
||||||
f"{fiat_invoice.checking_id or internal_payment.checking_id}"
|
f"{fiat_invoice.checking_id or internal_payment.checking_id}"
|
||||||
)
|
)
|
||||||
await update_payment(internal_payment, new_checking_id, conn=conn)
|
internal_payment = await update_payment(
|
||||||
internal_payment.checking_id = new_checking_id
|
internal_payment, new_checking_id, conn=conn
|
||||||
|
)
|
||||||
return internal_payment
|
return internal_payment
|
||||||
|
|
||||||
|
|
||||||
@@ -215,6 +215,7 @@ async def create_wallet_invoice(wallet_id: str, data: CreateInvoice) -> Payment:
|
|||||||
unhashed_description=unhashed_description,
|
unhashed_description=unhashed_description,
|
||||||
expiry=data.expiry,
|
expiry=data.expiry,
|
||||||
extra=data.extra,
|
extra=data.extra,
|
||||||
|
extension=data.extension,
|
||||||
webhook=data.webhook,
|
webhook=data.webhook,
|
||||||
internal=data.internal,
|
internal=data.internal,
|
||||||
payment_hash=data.payment_hash,
|
payment_hash=data.payment_hash,
|
||||||
@@ -260,6 +261,7 @@ async def create_invoice(
|
|||||||
webhook: str | None = None,
|
webhook: str | None = None,
|
||||||
internal: bool | None = False,
|
internal: bool | None = False,
|
||||||
payment_hash: str | None = None,
|
payment_hash: str | None = None,
|
||||||
|
extension: str | None = None,
|
||||||
labels: list[str] | None = None,
|
labels: list[str] | None = None,
|
||||||
external_id: str | None = None,
|
external_id: str | None = None,
|
||||||
conn: Connection | None = None,
|
conn: Connection | None = None,
|
||||||
@@ -343,6 +345,7 @@ async def create_invoice(
|
|||||||
expiry=invoice.expiry_date,
|
expiry=invoice.expiry_date,
|
||||||
memo=memo,
|
memo=memo,
|
||||||
extra=extra,
|
extra=extra,
|
||||||
|
extension=extension,
|
||||||
webhook=webhook,
|
webhook=webhook,
|
||||||
fee=invoice_response.fee_msat or 0,
|
fee=invoice_response.fee_msat or 0,
|
||||||
labels=labels,
|
labels=labels,
|
||||||
@@ -374,7 +377,7 @@ async def update_pending_payment(
|
|||||||
status = await check_payment_status(payment)
|
status = await check_payment_status(payment)
|
||||||
if status.failed:
|
if status.failed:
|
||||||
payment.status = PaymentState.FAILED
|
payment.status = PaymentState.FAILED
|
||||||
await update_payment(payment, conn=conn)
|
payment = await update_payment(payment, conn=conn)
|
||||||
elif status.success:
|
elif status.success:
|
||||||
payment = await update_payment_success_status(payment, status, conn=conn)
|
payment = await update_payment_success_status(payment, status, conn=conn)
|
||||||
return payment
|
return payment
|
||||||
@@ -514,9 +517,7 @@ async def update_wallet_balance(
|
|||||||
)
|
)
|
||||||
payment.status = PaymentState.SUCCESS
|
payment.status = PaymentState.SUCCESS
|
||||||
await update_payment(payment, conn=conn)
|
await update_payment(payment, conn=conn)
|
||||||
from lnbits.tasks import internal_invoice_queue_put
|
task_manager.internal_invoice_queue.put_nowait(payment)
|
||||||
|
|
||||||
await internal_invoice_queue_put(payment.checking_id)
|
|
||||||
|
|
||||||
|
|
||||||
async def check_wallet_limits(
|
async def check_wallet_limits(
|
||||||
@@ -556,10 +557,9 @@ async def check_wallet_daily_withdraw_limit(
|
|||||||
raise ValueError("It is not allowed to spend funds from this server.")
|
raise ValueError("It is not allowed to spend funds from this server.")
|
||||||
|
|
||||||
payments = await get_payments(
|
payments = await get_payments(
|
||||||
since=int(time.time()) - 60 * 60 * 24,
|
since=daystart_timestamp(),
|
||||||
outgoing=True,
|
outgoing=True,
|
||||||
wallet_id=wallet_id,
|
wallet_id=wallet_id,
|
||||||
limit=1,
|
|
||||||
conn=conn,
|
conn=conn,
|
||||||
)
|
)
|
||||||
if len(payments) == 0:
|
if len(payments) == 0:
|
||||||
@@ -630,18 +630,14 @@ async def check_transaction_status(
|
|||||||
return await check_payment_status(payment)
|
return await check_payment_status(payment)
|
||||||
|
|
||||||
|
|
||||||
async def check_payment_status(
|
async def check_payment_status(payment: Payment) -> PaymentStatus:
|
||||||
payment: Payment, skip_internal_payment_notifications: bool | None = False
|
|
||||||
) -> PaymentStatus:
|
|
||||||
if payment.is_internal:
|
if payment.is_internal:
|
||||||
if payment.success:
|
if payment.success:
|
||||||
return PaymentSuccessStatus()
|
return PaymentSuccessStatus()
|
||||||
if payment.failed:
|
if payment.failed:
|
||||||
return PaymentFailedStatus()
|
return PaymentFailedStatus()
|
||||||
if payment.is_in and payment.fiat_provider:
|
if payment.is_in and payment.fiat_provider:
|
||||||
fiat_status = await check_fiat_status(
|
fiat_status = await check_fiat_status(payment)
|
||||||
payment, skip_internal_payment_notifications
|
|
||||||
)
|
|
||||||
return PaymentStatus(paid=fiat_status.paid)
|
return PaymentStatus(paid=fiat_status.paid)
|
||||||
return PaymentPendingStatus()
|
return PaymentPendingStatus()
|
||||||
funding_source = get_funding_source()
|
funding_source = get_funding_source()
|
||||||
@@ -783,13 +779,16 @@ async def _pay_internal_invoice(
|
|||||||
await update_payment(internal_payment, conn=conn)
|
await update_payment(internal_payment, conn=conn)
|
||||||
logger.success(f"internal payment successful {internal_payment.checking_id}")
|
logger.success(f"internal payment successful {internal_payment.checking_id}")
|
||||||
|
|
||||||
await _send_payment_notification_in_background(wallet.id, payment, conn=conn)
|
await _send_payment_notification_in_background(
|
||||||
|
wallet.id, payment, conn=conn
|
||||||
# notify receiver asynchronously
|
) # notify the sender
|
||||||
from lnbits.tasks import internal_invoice_queue
|
await _send_payment_notification_in_background(
|
||||||
|
internal_payment.wallet_id, internal_payment, conn=conn
|
||||||
|
) # notify the receiver
|
||||||
|
|
||||||
|
# notify receiver asynchronously (extension listeners)
|
||||||
logger.debug(f"enqueuing internal invoice {internal_payment.checking_id}")
|
logger.debug(f"enqueuing internal invoice {internal_payment.checking_id}")
|
||||||
await internal_invoice_queue.put(internal_payment.checking_id)
|
task_manager.internal_invoice_queue.put_nowait(internal_payment)
|
||||||
|
|
||||||
return payment
|
return payment
|
||||||
|
|
||||||
@@ -826,16 +825,15 @@ async def _pay_external_invoice(
|
|||||||
|
|
||||||
fee_reserve_msat = fee_reserve(amount_msat, internal=False)
|
fee_reserve_msat = fee_reserve(amount_msat, internal=False)
|
||||||
|
|
||||||
from lnbits.tasks import create_task
|
task = task_manager.create_task(
|
||||||
|
_fundingsource_pay_invoice(checking_id, payment.bolt11, fee_reserve_msat),
|
||||||
task = create_task(
|
f"fundingsource_pay_invoice_{checking_id}",
|
||||||
_fundingsource_pay_invoice(checking_id, payment.bolt11, fee_reserve_msat)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# make sure a hold invoice or deferred payment is not blocking the server
|
# make sure a hold invoice or deferred payment is not blocking the server
|
||||||
wait_time = max(1, settings.lnbits_funding_source_pay_invoice_wait_seconds)
|
wait_time = max(1, settings.lnbits_funding_source_pay_invoice_wait_seconds)
|
||||||
try:
|
try:
|
||||||
payment_response = await asyncio.wait_for(task, timeout=wait_time)
|
payment_response = await asyncio.wait_for(task.task, timeout=wait_time)
|
||||||
except asyncio.TimeoutError:
|
except asyncio.TimeoutError:
|
||||||
# return pending payment on timeout
|
# return pending payment on timeout
|
||||||
logger.debug(
|
logger.debug(
|
||||||
@@ -876,7 +874,7 @@ async def update_payment_success_status(
|
|||||||
payment.status = PaymentState.SUCCESS
|
payment.status = PaymentState.SUCCESS
|
||||||
payment.fee = -(abs(status.fee_msat or 0) + abs(service_fee_msat))
|
payment.fee = -(abs(status.fee_msat or 0) + abs(service_fee_msat))
|
||||||
payment.preimage = payment.preimage or status.preimage
|
payment.preimage = payment.preimage or status.preimage
|
||||||
await update_payment(payment, conn=conn)
|
payment = await update_payment(payment, conn=conn)
|
||||||
return payment
|
return payment
|
||||||
|
|
||||||
|
|
||||||
@@ -1079,28 +1077,44 @@ async def _send_payment_notification_in_background(
|
|||||||
send_payment_notification_in_background(wallet, payment)
|
send_payment_notification_in_background(wallet, payment)
|
||||||
|
|
||||||
|
|
||||||
async def update_invoice_callback(checking_id: str) -> Payment | None:
|
async def update_invoice_from_paid_invoices_stream(checking_id: str) -> Payment | None:
|
||||||
"""
|
"""
|
||||||
Takes a checking_id of an incoming payment, from either paid_invoices_stream()
|
Takes a checking_id of an incoming payment from paid_invoices_stream()
|
||||||
or internal_invoice_queue. Checks its status, updates and returns it.
|
Checks its status, updates its status and returns it.
|
||||||
returns None if no payment was found or it not and incoming payment.
|
returns None if no incoming payment was found or the status is not successful
|
||||||
"""
|
"""
|
||||||
payment = await get_standalone_payment(checking_id, incoming=True)
|
payment = await get_standalone_payment(checking_id, incoming=True)
|
||||||
if not payment:
|
if not payment:
|
||||||
logger.warning(f"No payment found for '{checking_id}'.")
|
logger.warning(f"No incoming payment found for '{checking_id}'.")
|
||||||
return None
|
return None
|
||||||
if not payment.is_in:
|
|
||||||
logger.warning(f"Payment '{checking_id}' is not incoming, skipping.")
|
status = await check_payment_status(payment)
|
||||||
|
|
||||||
|
if not status.success:
|
||||||
|
logger.error(
|
||||||
|
"Unexpected status response from paid_invoices_stream. Skipping update."
|
||||||
|
)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
status = await check_payment_status(
|
|
||||||
payment, skip_internal_payment_notifications=True
|
|
||||||
)
|
|
||||||
payment.fee = status.fee_msat or payment.fee
|
payment.fee = status.fee_msat or payment.fee
|
||||||
# only overwrite preimage if status.preimage provides it
|
# only overwrite preimage if status.preimage provides it
|
||||||
payment.preimage = status.preimage or payment.preimage
|
payment.preimage = status.preimage or payment.preimage
|
||||||
payment.status = PaymentState.SUCCESS
|
payment.status = PaymentState.SUCCESS
|
||||||
await update_payment(payment)
|
payment = await update_payment(payment)
|
||||||
if payment.fiat_provider:
|
|
||||||
await handle_fiat_payment_confirmation(payment)
|
|
||||||
return payment
|
return payment
|
||||||
|
|
||||||
|
|
||||||
|
async def fundingsource_invoice_producer() -> None:
|
||||||
|
"""
|
||||||
|
will collect all invoices that come directly from the backend wallet.
|
||||||
|
|
||||||
|
Called registered in the app startup sequence and run by taskmanager.
|
||||||
|
"""
|
||||||
|
funding_source = get_funding_source()
|
||||||
|
async for checking_id in funding_source.paid_invoices_stream():
|
||||||
|
logger.info(f"got a payment notification {checking_id}")
|
||||||
|
payment = await update_invoice_from_paid_invoices_stream(checking_id)
|
||||||
|
if payment:
|
||||||
|
logger.success(f"fundingsource invoice {checking_id} settled")
|
||||||
|
task_manager.invoice_queue.put_nowait(payment)
|
||||||
|
|||||||
+47
-118
@@ -2,76 +2,43 @@ import asyncio
|
|||||||
|
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
|
|
||||||
from lnbits.core.crud import (
|
from lnbits.core.crud import create_audit_entry
|
||||||
create_audit_entry,
|
|
||||||
get_wallet,
|
|
||||||
)
|
|
||||||
from lnbits.core.crud.audit import delete_expired_audit_entries
|
|
||||||
from lnbits.core.crud.payments import get_payments_status_count
|
from lnbits.core.crud.payments import get_payments_status_count
|
||||||
from lnbits.core.crud.users import get_accounts
|
from lnbits.core.crud.users import get_accounts
|
||||||
from lnbits.core.crud.wallets import get_wallets_count
|
from lnbits.core.crud.wallets import get_wallets_count
|
||||||
from lnbits.core.models.audit import AuditEntry
|
from lnbits.core.models.audit import AuditEntry
|
||||||
from lnbits.core.models.extensions import InstallableExtension
|
from lnbits.core.models.extensions import InstallableExtension
|
||||||
from lnbits.core.models.notifications import NotificationType
|
from lnbits.core.models.notifications import NotificationType
|
||||||
from lnbits.core.services.funding_source import (
|
from lnbits.core.services.funding_source import get_balance_delta
|
||||||
check_balance_delta_changed,
|
|
||||||
check_server_balance_against_node,
|
|
||||||
get_balance_delta,
|
|
||||||
)
|
|
||||||
from lnbits.core.services.notifications import (
|
from lnbits.core.services.notifications import (
|
||||||
enqueue_admin_notification,
|
enqueue_admin_notification,
|
||||||
process_next_notification,
|
|
||||||
send_payment_notification,
|
|
||||||
)
|
)
|
||||||
from lnbits.db import Filters
|
from lnbits.db import Filters
|
||||||
from lnbits.settings import settings
|
from lnbits.settings import settings
|
||||||
from lnbits.utils.exchange_rates import btc_rates
|
from lnbits.utils.cache import cache
|
||||||
|
from lnbits.utils.exchange_rates import btc_price_from_aggregator, btc_rates
|
||||||
|
|
||||||
audit_queue: asyncio.Queue[AuditEntry] = asyncio.Queue()
|
audit_queue: asyncio.Queue[AuditEntry] = asyncio.Queue()
|
||||||
|
|
||||||
|
|
||||||
async def run_by_the_minute_tasks() -> None:
|
async def process_next_audit_entry() -> None:
|
||||||
minute_counter = 0
|
"""
|
||||||
while settings.lnbits_running:
|
Waits for audit entries to be pushed to the queue.
|
||||||
status_minutes = settings.lnbits_notification_server_status_hours * 60
|
Then it inserts the entries into the DB.
|
||||||
|
"""
|
||||||
if settings.notification_balance_delta_threshold_sats > 0:
|
data = await audit_queue.get()
|
||||||
try:
|
await create_audit_entry(data)
|
||||||
# runs by default every minute, the delta should not change that often
|
|
||||||
await check_balance_delta_changed()
|
|
||||||
except Exception as ex:
|
|
||||||
logger.error(ex)
|
|
||||||
|
|
||||||
if minute_counter % settings.lnbits_watchdog_interval_minutes == 0:
|
|
||||||
try:
|
|
||||||
await check_server_balance_against_node()
|
|
||||||
except Exception as ex:
|
|
||||||
logger.error(ex)
|
|
||||||
|
|
||||||
if minute_counter % status_minutes == 0:
|
|
||||||
try:
|
|
||||||
await _notify_server_status()
|
|
||||||
except Exception as ex:
|
|
||||||
logger.error(ex)
|
|
||||||
|
|
||||||
if minute_counter % 60 == 0:
|
|
||||||
try:
|
|
||||||
# initialize the list of all extensions
|
|
||||||
await InstallableExtension.get_installable_extensions(
|
|
||||||
post_refresh_cache=True
|
|
||||||
)
|
|
||||||
except Exception as ex:
|
|
||||||
logger.error(ex)
|
|
||||||
|
|
||||||
minute_counter += 1
|
|
||||||
await asyncio.sleep(60)
|
|
||||||
|
|
||||||
|
|
||||||
async def _notify_server_status() -> None:
|
async def refresh_extension_cache() -> None:
|
||||||
|
# only refreshes every 10 minutes
|
||||||
|
await InstallableExtension.get_installable_extensions()
|
||||||
|
|
||||||
|
|
||||||
|
async def notify_server_status() -> None:
|
||||||
accounts = await get_accounts(filters=Filters(limit=0))
|
accounts = await get_accounts(filters=Filters(limit=0))
|
||||||
wallets_count = await get_wallets_count()
|
wallets_count = await get_wallets_count()
|
||||||
payments = await get_payments_status_count()
|
payments = await get_payments_status_count()
|
||||||
|
|
||||||
status = await get_balance_delta()
|
status = await get_balance_delta()
|
||||||
values = {
|
values = {
|
||||||
"up_time": settings.lnbits_server_up_time,
|
"up_time": settings.lnbits_server_up_time,
|
||||||
@@ -88,76 +55,38 @@ async def _notify_server_status() -> None:
|
|||||||
enqueue_admin_notification(NotificationType.server_status, values)
|
enqueue_admin_notification(NotificationType.server_status, values)
|
||||||
|
|
||||||
|
|
||||||
async def wait_for_paid_invoices(invoice_paid_queue: asyncio.Queue) -> None:
|
|
||||||
"""
|
|
||||||
This worker dispatches events to all extensions and dispatches webhooks.
|
|
||||||
"""
|
|
||||||
while settings.lnbits_running:
|
|
||||||
payment = await invoice_paid_queue.get()
|
|
||||||
logger.trace("received invoice paid event")
|
|
||||||
# payment notification
|
|
||||||
wallet = await get_wallet(payment.wallet_id)
|
|
||||||
if wallet:
|
|
||||||
await send_payment_notification(wallet, payment)
|
|
||||||
|
|
||||||
|
|
||||||
async def wait_for_audit_data() -> None:
|
|
||||||
"""
|
|
||||||
Waits for audit entries to be pushed to the queue.
|
|
||||||
Then it inserts the entries into the DB.
|
|
||||||
"""
|
|
||||||
while settings.lnbits_running:
|
|
||||||
data = await audit_queue.get()
|
|
||||||
try:
|
|
||||||
await create_audit_entry(data)
|
|
||||||
except Exception as ex:
|
|
||||||
logger.warning(ex)
|
|
||||||
await asyncio.sleep(3)
|
|
||||||
|
|
||||||
|
|
||||||
async def wait_notification_messages() -> None:
|
|
||||||
|
|
||||||
while settings.lnbits_running:
|
|
||||||
try:
|
|
||||||
await process_next_notification()
|
|
||||||
except Exception as ex:
|
|
||||||
logger.warning("Payment notification error", ex)
|
|
||||||
await asyncio.sleep(3)
|
|
||||||
|
|
||||||
|
|
||||||
async def purge_audit_data() -> None:
|
|
||||||
"""
|
|
||||||
Remove audit entries which have passed their retention period.
|
|
||||||
"""
|
|
||||||
while settings.lnbits_running:
|
|
||||||
try:
|
|
||||||
await delete_expired_audit_entries()
|
|
||||||
except Exception as ex:
|
|
||||||
logger.warning(ex)
|
|
||||||
|
|
||||||
# clean every hour
|
|
||||||
await asyncio.sleep(60 * 60)
|
|
||||||
|
|
||||||
|
|
||||||
async def collect_exchange_rates_data() -> None:
|
async def collect_exchange_rates_data() -> None:
|
||||||
"""
|
"""
|
||||||
Collect exchange rates data. Used for monitoring only.
|
Collect exchange rates data. Used for monitoring only.
|
||||||
"""
|
"""
|
||||||
while settings.lnbits_running:
|
currency = settings.lnbits_default_accounting_currency or "USD"
|
||||||
currency = settings.lnbits_default_accounting_currency or "USD"
|
max_history_size = settings.lnbits_exchange_history_size
|
||||||
max_history_size = settings.lnbits_exchange_history_size
|
try:
|
||||||
sleep_time = settings.lnbits_exchange_history_refresh_interval_seconds
|
if (
|
||||||
|
settings.lnbits_price_aggregator_enabled
|
||||||
if sleep_time > 0:
|
and settings.lnbits_price_aggregator_url
|
||||||
try:
|
):
|
||||||
rates = await btc_rates(currency)
|
price = await btc_price_from_aggregator(currency)
|
||||||
if rates:
|
if price:
|
||||||
rates_values = [r[1] for r in rates]
|
cache.set(
|
||||||
lnbits_rate = sum(rates_values) / len(rates_values)
|
f"btc-price-{currency}",
|
||||||
rates.append(("LNbits", lnbits_rate))
|
price,
|
||||||
settings.append_exchange_rate_datapoint(dict(rates), max_history_size)
|
expiry=settings.lnbits_exchange_rate_cache_seconds,
|
||||||
except Exception as ex:
|
)
|
||||||
logger.warning(ex)
|
settings.append_exchange_rate_datapoint(
|
||||||
|
{"Aggregator": price}, max_history_size
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
sleep_time = 60
|
rates = await btc_rates(currency)
|
||||||
await asyncio.sleep(sleep_time)
|
if rates:
|
||||||
|
rates_values = [r[1] for r in rates]
|
||||||
|
lnbits_rate = sum(rates_values) / len(rates_values)
|
||||||
|
rates.append(("LNbits", lnbits_rate))
|
||||||
|
cache.set(
|
||||||
|
f"btc-price-{currency}",
|
||||||
|
lnbits_rate,
|
||||||
|
expiry=settings.lnbits_exchange_rate_cache_seconds,
|
||||||
|
)
|
||||||
|
settings.append_exchange_rate_datapoint(dict(rates), max_history_size)
|
||||||
|
except Exception as ex:
|
||||||
|
logger.warning(ex)
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ from lnbits.core.services.settings import dict_to_settings
|
|||||||
from lnbits.decorators import check_admin, check_super_user
|
from lnbits.decorators import check_admin, check_super_user
|
||||||
from lnbits.server import server_restart
|
from lnbits.server import server_restart
|
||||||
from lnbits.settings import AdminSettings, Settings, UpdateSettings, settings
|
from lnbits.settings import AdminSettings, Settings, UpdateSettings, settings
|
||||||
from lnbits.tasks import invoice_listeners
|
from lnbits.task_manager import PublicTask, task_manager
|
||||||
|
|
||||||
from .. import core_app_extra
|
from .. import core_app_extra
|
||||||
from ..crud import get_admin_settings, reset_core_settings, update_admin_settings
|
from ..crud import get_admin_settings, reset_core_settings, update_admin_settings
|
||||||
@@ -44,11 +44,10 @@ async def api_auditor():
|
|||||||
name="Monitor",
|
name="Monitor",
|
||||||
description="show the current listeners and other monitoring data",
|
description="show the current listeners and other monitoring data",
|
||||||
dependencies=[Depends(check_admin)],
|
dependencies=[Depends(check_admin)],
|
||||||
|
response_model=list[PublicTask],
|
||||||
)
|
)
|
||||||
async def api_monitor():
|
async def api_monitor() -> list[PublicTask]:
|
||||||
return {
|
return task_manager.get_public_tasks()
|
||||||
"invoice_listeners": list(invoice_listeners.keys()),
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@admin_router.get(
|
@admin_router.get(
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ from lnbits.decorators import (
|
|||||||
check_account_exists,
|
check_account_exists,
|
||||||
check_admin,
|
check_admin,
|
||||||
check_user_exists,
|
check_user_exists,
|
||||||
|
optional_user_id,
|
||||||
)
|
)
|
||||||
from lnbits.helpers import (
|
from lnbits.helpers import (
|
||||||
create_access_token,
|
create_access_token,
|
||||||
@@ -294,7 +295,10 @@ async def api_create_user_api_token(
|
|||||||
account.username, api_token_id, data.expiration_time_minutes
|
account.username, api_token_id, data.expiration_time_minutes
|
||||||
)
|
)
|
||||||
|
|
||||||
acl.token_id_list.append(SimpleItem(id=api_token_id, name=data.token_name))
|
expires_at = int(time()) + data.expiration_time_minutes * 60
|
||||||
|
acl.token_id_list.append(
|
||||||
|
SimpleItem(id=api_token_id, name=data.token_name, expires_at=expires_at)
|
||||||
|
)
|
||||||
await update_user_access_control_list(acls)
|
await update_user_access_control_list(acls)
|
||||||
return ApiTokenResponse(id=api_token_id, api_token=api_token)
|
return ApiTokenResponse(id=api_token_id, api_token=api_token)
|
||||||
|
|
||||||
@@ -320,7 +324,10 @@ async def api_delete_user_api_token(
|
|||||||
|
|
||||||
@auth_router.get("/{provider}", description="SSO Provider")
|
@auth_router.get("/{provider}", description="SSO Provider")
|
||||||
async def login_with_sso_provider(
|
async def login_with_sso_provider(
|
||||||
request: Request, provider: str, user_id: str | None = None
|
request: Request,
|
||||||
|
provider: str,
|
||||||
|
user_id: str | None,
|
||||||
|
auth_user_id: str | None = Depends(optional_user_id),
|
||||||
):
|
):
|
||||||
provider_sso = _new_sso(provider)
|
provider_sso = _new_sso(provider)
|
||||||
if not provider_sso:
|
if not provider_sso:
|
||||||
@@ -328,6 +335,8 @@ async def login_with_sso_provider(
|
|||||||
HTTPStatus.FORBIDDEN,
|
HTTPStatus.FORBIDDEN,
|
||||||
f"Login by '{provider}' not allowed.",
|
f"Login by '{provider}' not allowed.",
|
||||||
)
|
)
|
||||||
|
if user_id and user_id != auth_user_id:
|
||||||
|
raise HTTPException(HTTPStatus.FORBIDDEN, "User ID mismatch.")
|
||||||
|
|
||||||
provider_sso.redirect_uri = str(request.base_url) + f"api/v1/auth/{provider}/token"
|
provider_sso.redirect_uri = str(request.base_url) + f"api/v1/auth/{provider}/token"
|
||||||
with provider_sso:
|
with provider_sso:
|
||||||
|
|||||||
@@ -357,16 +357,20 @@ async def handle_revolut_event(event: dict):
|
|||||||
return
|
return
|
||||||
|
|
||||||
payment = await get_standalone_payment(f"fiat_revolut_order_{order_id}")
|
payment = await get_standalone_payment(f"fiat_revolut_order_{order_id}")
|
||||||
if not payment:
|
if payment:
|
||||||
|
await check_fiat_status(payment)
|
||||||
|
return
|
||||||
|
|
||||||
|
if event_type == "ORDER_COMPLETED":
|
||||||
logger.warning(f"No payment found for Revolut order: '{order_id}'.")
|
logger.warning(f"No payment found for Revolut order: '{order_id}'.")
|
||||||
await _handle_revolut_subscription_order_paid(order_id)
|
await _handle_revolut_subscription_order_paid(order_id)
|
||||||
return
|
return
|
||||||
|
|
||||||
await check_fiat_status(payment)
|
logger.info(f"Ignoring Revolut authorised order without payment: '{order_id}'.")
|
||||||
return
|
return
|
||||||
|
|
||||||
if event_type == "SUBSCRIPTION_INITIATED":
|
if event_type == "SUBSCRIPTION_INITIATED":
|
||||||
await _handle_revolut_subscription_initiated(event)
|
logger.info("Revolut subscription initiated event received.")
|
||||||
return
|
return
|
||||||
|
|
||||||
if event_type in [
|
if event_type in [
|
||||||
@@ -380,23 +384,6 @@ async def handle_revolut_event(event: dict):
|
|||||||
logger.warning(f"Unhandled Revolut event type: '{event_type}'.")
|
logger.warning(f"Unhandled Revolut event type: '{event_type}'.")
|
||||||
|
|
||||||
|
|
||||||
async def _handle_revolut_subscription_initiated(event: dict):
|
|
||||||
subscription_id = event.get("subscription_id")
|
|
||||||
if not subscription_id:
|
|
||||||
subscription_id = event.get("id")
|
|
||||||
|
|
||||||
if not subscription_id:
|
|
||||||
logger.warning("Revolut subscription event missing subscription_id.")
|
|
||||||
return
|
|
||||||
|
|
||||||
fiat_provider = await _get_revolut_provider()
|
|
||||||
if not fiat_provider:
|
|
||||||
return
|
|
||||||
|
|
||||||
subscription = await fiat_provider.get_subscription(subscription_id)
|
|
||||||
await _handle_revolut_subscription(subscription, fiat_provider)
|
|
||||||
|
|
||||||
|
|
||||||
async def _get_revolut_provider() -> RevolutWallet | None:
|
async def _get_revolut_provider() -> RevolutWallet | None:
|
||||||
fiat_provider = await get_fiat_provider("revolut")
|
fiat_provider = await get_fiat_provider("revolut")
|
||||||
if not isinstance(fiat_provider, RevolutWallet):
|
if not isinstance(fiat_provider, RevolutWallet):
|
||||||
@@ -406,7 +393,10 @@ async def _get_revolut_provider() -> RevolutWallet | None:
|
|||||||
|
|
||||||
|
|
||||||
async def _handle_revolut_subscription(
|
async def _handle_revolut_subscription(
|
||||||
subscription: dict, fiat_provider: RevolutWallet
|
subscription: dict,
|
||||||
|
fiat_provider: RevolutWallet,
|
||||||
|
order_id: str | None = None,
|
||||||
|
order: dict | None = None,
|
||||||
):
|
):
|
||||||
subscription_id = subscription.get("id")
|
subscription_id = subscription.get("id")
|
||||||
if not subscription_id:
|
if not subscription_id:
|
||||||
@@ -420,16 +410,17 @@ async def _handle_revolut_subscription(
|
|||||||
logger.warning("Revolut subscription event missing LNbits metadata.")
|
logger.warning("Revolut subscription event missing LNbits metadata.")
|
||||||
return
|
return
|
||||||
|
|
||||||
cycle_id = subscription.get("current_cycle_id")
|
|
||||||
if not cycle_id:
|
|
||||||
logger.warning("Revolut subscription missing current_cycle_id.")
|
|
||||||
return
|
|
||||||
|
|
||||||
cycle = await fiat_provider.get_subscription_cycle(subscription_id, cycle_id)
|
|
||||||
order_id = cycle.get("order_id")
|
|
||||||
if not order_id:
|
if not order_id:
|
||||||
logger.warning("Revolut subscription cycle missing order_id.")
|
cycle_id = subscription.get("current_cycle_id")
|
||||||
return
|
if not cycle_id:
|
||||||
|
logger.warning("Revolut subscription missing current_cycle_id.")
|
||||||
|
return
|
||||||
|
|
||||||
|
cycle = await fiat_provider.get_subscription_cycle(subscription_id, cycle_id)
|
||||||
|
order_id = cycle.get("order_id")
|
||||||
|
if not order_id:
|
||||||
|
logger.warning("Revolut subscription cycle missing order_id.")
|
||||||
|
return
|
||||||
|
|
||||||
existing_payment = await get_standalone_payment(f"fiat_revolut_order_{order_id}")
|
existing_payment = await get_standalone_payment(f"fiat_revolut_order_{order_id}")
|
||||||
if existing_payment:
|
if existing_payment:
|
||||||
@@ -439,7 +430,8 @@ async def _handle_revolut_subscription(
|
|||||||
await check_fiat_status(existing_payment)
|
await check_fiat_status(existing_payment)
|
||||||
return
|
return
|
||||||
|
|
||||||
order = await fiat_provider.get_order(order_id)
|
if not order:
|
||||||
|
order = await fiat_provider.get_order(order_id)
|
||||||
amount_minor = order.get("amount")
|
amount_minor = order.get("amount")
|
||||||
currency = (order.get("currency") or "").upper()
|
currency = (order.get("currency") or "").upper()
|
||||||
if amount_minor is None or not currency:
|
if amount_minor is None or not currency:
|
||||||
@@ -447,7 +439,7 @@ async def _handle_revolut_subscription(
|
|||||||
|
|
||||||
extra = {
|
extra = {
|
||||||
**(reference.extra or {}),
|
**(reference.extra or {}),
|
||||||
"subscription_request_id": reference.subscription_request_id,
|
"subscription_request_id": subscription_id,
|
||||||
"fiat_method": "subscription",
|
"fiat_method": "subscription",
|
||||||
"tag": reference.tag,
|
"tag": reference.tag,
|
||||||
"subscription": {
|
"subscription": {
|
||||||
@@ -475,7 +467,9 @@ async def _handle_revolut_subscription_order_paid(order_id: str):
|
|||||||
return
|
return
|
||||||
|
|
||||||
order = await fiat_provider.get_order(order_id)
|
order = await fiat_provider.get_order(order_id)
|
||||||
if order.get("type") != "payment" or order.get("state") != "completed":
|
order_type = (order.get("type") or "").lower()
|
||||||
|
order_state = (order.get("state") or "").upper()
|
||||||
|
if order_type != "payment" or order_state != "COMPLETED":
|
||||||
logger.warning(f"Revolut order is not a completed payment: '{order_id}'.")
|
logger.warning(f"Revolut order is not a completed payment: '{order_id}'.")
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -490,7 +484,9 @@ async def _handle_revolut_subscription_order_paid(order_id: str):
|
|||||||
logger.warning(f"Revolut subscription is not active: '{subscription_id}'.")
|
logger.warning(f"Revolut subscription is not active: '{subscription_id}'.")
|
||||||
return
|
return
|
||||||
|
|
||||||
await _handle_revolut_subscription_initiated(subscription)
|
await _handle_revolut_subscription(
|
||||||
|
subscription, fiat_provider, order_id=order_id, order=order
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def _create_revolut_subscription_payment(
|
async def _create_revolut_subscription_payment(
|
||||||
|
|||||||
@@ -292,7 +292,6 @@ async def api_deactivate_extension(ext_id: str) -> SimpleStatus:
|
|||||||
|
|
||||||
@extension_router.delete("/{ext_id}", dependencies=[Depends(check_admin)])
|
@extension_router.delete("/{ext_id}", dependencies=[Depends(check_admin)])
|
||||||
async def api_uninstall_extension(ext_id: str) -> SimpleStatus:
|
async def api_uninstall_extension(ext_id: str) -> SimpleStatus:
|
||||||
|
|
||||||
extension = await get_installed_extension(ext_id)
|
extension = await get_installed_extension(ext_id)
|
||||||
if not extension:
|
if not extension:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
@@ -567,6 +566,7 @@ async def extensions(account_id: AccountId = Depends(check_account_id_exists)):
|
|||||||
"shortDescription": ext.short_description,
|
"shortDescription": ext.short_description,
|
||||||
"stars": ext.stars,
|
"stars": ext.stars,
|
||||||
"isFeatured": ext.meta.featured if ext.meta else False,
|
"isFeatured": ext.meta.featured if ext.meta else False,
|
||||||
|
"categories": ext.meta.categories if ext.meta else [],
|
||||||
"dependencies": ext.meta.dependencies if ext.meta else "",
|
"dependencies": ext.meta.dependencies if ext.meta else "",
|
||||||
"isInstalled": ext.id in installed_exts_ids,
|
"isInstalled": ext.id in installed_exts_ids,
|
||||||
"hasDatabaseTables": next(
|
"hasDatabaseTables": next(
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ from lnbits.decorators import (
|
|||||||
check_first_install,
|
check_first_install,
|
||||||
check_user_exists,
|
check_user_exists,
|
||||||
)
|
)
|
||||||
from lnbits.helpers import check_callback_url, extension_id_from_path, template_renderer
|
from lnbits.helpers import check_callback_url, template_renderer
|
||||||
from lnbits.settings import settings
|
from lnbits.settings import settings
|
||||||
|
|
||||||
from ..crud import get_user
|
from ..crud import get_user
|
||||||
@@ -198,9 +198,7 @@ admin_ui_checks = [Depends(check_admin), Depends(check_admin_ui)]
|
|||||||
async def index(
|
async def index(
|
||||||
request: Request, user: User = Depends(check_user_exists)
|
request: Request, user: User = Depends(check_user_exists)
|
||||||
) -> HTMLResponse:
|
) -> HTMLResponse:
|
||||||
return template_renderer(
|
return template_renderer().TemplateResponse(
|
||||||
extension_id=extension_id_from_path(request.url.path)
|
|
||||||
).TemplateResponse(
|
|
||||||
request,
|
request,
|
||||||
"base.html",
|
"base.html",
|
||||||
{
|
{
|
||||||
@@ -213,9 +211,7 @@ async def index(
|
|||||||
@generic_router.get("/node/public")
|
@generic_router.get("/node/public")
|
||||||
@generic_router.get("/first_install", dependencies=[Depends(check_first_install)])
|
@generic_router.get("/first_install", dependencies=[Depends(check_first_install)])
|
||||||
async def index_public(request: Request) -> HTMLResponse:
|
async def index_public(request: Request) -> HTMLResponse:
|
||||||
return template_renderer(
|
return template_renderer().TemplateResponse(request, "base.html", {"public": True})
|
||||||
extension_id=extension_id_from_path(request.url.path)
|
|
||||||
).TemplateResponse(request, "base.html", {"public": True})
|
|
||||||
|
|
||||||
|
|
||||||
@generic_router.get("/uuidv4/{hex_value}")
|
@generic_router.get("/uuidv4/{hex_value}")
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ from lnbits.core.models import (
|
|||||||
PaymentWalletStats,
|
PaymentWalletStats,
|
||||||
SettleInvoice,
|
SettleInvoice,
|
||||||
SimpleStatus,
|
SimpleStatus,
|
||||||
|
UpdatePaymentExtra,
|
||||||
)
|
)
|
||||||
from lnbits.core.models.payments import UpdatePaymentLabels
|
from lnbits.core.models.payments import UpdatePaymentLabels
|
||||||
from lnbits.core.models.users import AccountId
|
from lnbits.core.models.users import AccountId
|
||||||
@@ -297,6 +298,38 @@ async def api_update_payment_labels(
|
|||||||
return SimpleStatus(success=True, message="Payment labels updated.")
|
return SimpleStatus(success=True, message="Payment labels updated.")
|
||||||
|
|
||||||
|
|
||||||
|
@payment_router.patch(
|
||||||
|
"/extra",
|
||||||
|
name="Update payment extra",
|
||||||
|
description="Append new extra metadata to a payment.",
|
||||||
|
response_model=Payment,
|
||||||
|
)
|
||||||
|
async def api_update_payment_extra(
|
||||||
|
data: UpdatePaymentExtra,
|
||||||
|
key_type: WalletTypeInfo = Depends(require_admin_key),
|
||||||
|
) -> Payment:
|
||||||
|
payment = await get_standalone_payment(
|
||||||
|
data.payment_hash, wallet_id=key_type.wallet.id
|
||||||
|
)
|
||||||
|
if payment is None:
|
||||||
|
raise HTTPException(HTTPStatus.NOT_FOUND, "Payment does not exist.")
|
||||||
|
if not payment.success:
|
||||||
|
raise HTTPException(
|
||||||
|
HTTPStatus.BAD_REQUEST, "Payment extra can only be updated after success."
|
||||||
|
)
|
||||||
|
|
||||||
|
duplicate_keys = sorted(set(payment.extra).intersection(data.extra))
|
||||||
|
if duplicate_keys:
|
||||||
|
raise HTTPException(
|
||||||
|
HTTPStatus.BAD_REQUEST,
|
||||||
|
f"Extra keys already exist: {', '.join(duplicate_keys)}.",
|
||||||
|
)
|
||||||
|
|
||||||
|
payment.extra.update(data.extra)
|
||||||
|
await update_payment(payment)
|
||||||
|
return payment
|
||||||
|
|
||||||
|
|
||||||
@payment_router.get("/fee-reserve")
|
@payment_router.get("/fee-reserve")
|
||||||
async def api_payments_fee_reserve(invoice: str = Query("invoice")) -> JSONResponse:
|
async def api_payments_fee_reserve(invoice: str = Query("invoice")) -> JSONResponse:
|
||||||
invoice_obj = bolt11.decode(invoice)
|
invoice_obj = bolt11.decode(invoice)
|
||||||
|
|||||||
@@ -158,10 +158,6 @@ async def api_update_user(
|
|||||||
async def api_users_delete_user(
|
async def api_users_delete_user(
|
||||||
user_id: str, account: Account = Depends(check_admin)
|
user_id: str, account: Account = Depends(check_admin)
|
||||||
) -> SimpleStatus:
|
) -> SimpleStatus:
|
||||||
wallets = await get_wallets(user_id, deleted=False)
|
|
||||||
for wallet in wallets:
|
|
||||||
await delete_wallet_by_id(wallet.id)
|
|
||||||
|
|
||||||
if user_id == settings.super_user:
|
if user_id == settings.super_user:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=HTTPStatus.BAD_REQUEST,
|
status_code=HTTPStatus.BAD_REQUEST,
|
||||||
@@ -173,6 +169,11 @@ async def api_users_delete_user(
|
|||||||
status_code=HTTPStatus.BAD_REQUEST,
|
status_code=HTTPStatus.BAD_REQUEST,
|
||||||
detail="Only super_user can delete admin user.",
|
detail="Only super_user can delete admin user.",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
wallets = await get_wallets(user_id, deleted=False)
|
||||||
|
for wallet in wallets:
|
||||||
|
await delete_wallet_by_id(wallet.id)
|
||||||
|
|
||||||
await delete_account(user_id)
|
await delete_account(user_id)
|
||||||
return SimpleStatus(success=True, message="User deleted.")
|
return SimpleStatus(success=True, message="User deleted.")
|
||||||
|
|
||||||
|
|||||||
+3
-3
@@ -131,15 +131,15 @@ class FiatSubscriptionResponse(BaseModel):
|
|||||||
|
|
||||||
|
|
||||||
class FiatPaymentSuccessStatus(FiatPaymentStatus):
|
class FiatPaymentSuccessStatus(FiatPaymentStatus):
|
||||||
paid = True
|
paid = True # type: ignore[reportIncompatibleVariableOverride]
|
||||||
|
|
||||||
|
|
||||||
class FiatPaymentFailedStatus(FiatPaymentStatus):
|
class FiatPaymentFailedStatus(FiatPaymentStatus):
|
||||||
paid = False
|
paid = False # type: ignore[reportIncompatibleVariableOverride]
|
||||||
|
|
||||||
|
|
||||||
class FiatPaymentPendingStatus(FiatPaymentStatus):
|
class FiatPaymentPendingStatus(FiatPaymentStatus):
|
||||||
paid = None
|
paid = None # type: ignore[reportIncompatibleVariableOverride]
|
||||||
|
|
||||||
|
|
||||||
class FiatProvider(ABC):
|
class FiatProvider(ABC):
|
||||||
|
|||||||
+10
-1
@@ -283,7 +283,7 @@ class RevolutWallet(FiatProvider):
|
|||||||
return FiatSubscriptionResponse(
|
return FiatSubscriptionResponse(
|
||||||
ok=True,
|
ok=True,
|
||||||
checkout_session_url=checkout_url,
|
checkout_session_url=checkout_url,
|
||||||
subscription_request_id=payment_options.subscription_request_id,
|
subscription_request_id=revolut_subscription_id,
|
||||||
)
|
)
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
return FiatSubscriptionResponse(
|
return FiatSubscriptionResponse(
|
||||||
@@ -302,6 +302,15 @@ class RevolutWallet(FiatProvider):
|
|||||||
**kwargs,
|
**kwargs,
|
||||||
) -> FiatSubscriptionResponse:
|
) -> FiatSubscriptionResponse:
|
||||||
try:
|
try:
|
||||||
|
subscription = await self.get_subscription(subscription_id)
|
||||||
|
reference = self.deserialize_subscription_reference(
|
||||||
|
subscription.get("external_reference")
|
||||||
|
)
|
||||||
|
if not reference or reference.wallet_id != correlation_id:
|
||||||
|
return FiatSubscriptionResponse(
|
||||||
|
ok=False, error_message="Subscription not found."
|
||||||
|
)
|
||||||
|
|
||||||
r = await self.client.post(
|
r = await self.client.post(
|
||||||
f"/api/subscriptions/{subscription_id}/cancel",
|
f"/api/subscriptions/{subscription_id}/cancel",
|
||||||
timeout=REVOLUT_REQUEST_TIMEOUT,
|
timeout=REVOLUT_REQUEST_TIMEOUT,
|
||||||
|
|||||||
+11
-47
@@ -52,45 +52,7 @@ def static_url_for(static: str, path: str) -> str:
|
|||||||
return f"/{static}/{path}?v={settings.server_startup_time}"
|
return f"/{static}/{path}?v={settings.server_startup_time}"
|
||||||
|
|
||||||
|
|
||||||
def extension_id_from_path(path: str) -> str | None:
|
def template_renderer(additional_folders: list | None = None) -> Jinja2Templates:
|
||||||
parts = [part for part in path.split("/") if part]
|
|
||||||
if not parts:
|
|
||||||
return None
|
|
||||||
|
|
||||||
if len(parts) >= 3 and parts[0] == "upgrades":
|
|
||||||
return parts[2]
|
|
||||||
|
|
||||||
ext_id = parts[0]
|
|
||||||
ext_i18n_dir = Path(
|
|
||||||
settings.lnbits_extensions_path, "extensions", ext_id, "static", "i18n"
|
|
||||||
)
|
|
||||||
if ext_i18n_dir.is_dir():
|
|
||||||
return ext_id
|
|
||||||
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
def extension_i18n_urls(extension_id: str | None) -> list[str]:
|
|
||||||
if not extension_id:
|
|
||||||
return []
|
|
||||||
|
|
||||||
i18n_dir = Path(
|
|
||||||
settings.lnbits_extensions_path, "extensions", extension_id, "static", "i18n"
|
|
||||||
)
|
|
||||||
if not i18n_dir.is_dir():
|
|
||||||
return []
|
|
||||||
|
|
||||||
files = [file.name for file in i18n_dir.glob("*.js") if file.is_file()]
|
|
||||||
return [
|
|
||||||
static_url_for(f"{extension_id}/static", f"i18n/{filename}")
|
|
||||||
for filename in sorted(files, key=lambda name: (name != "en.js", name))
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
def template_renderer(
|
|
||||||
additional_folders: list | None = None,
|
|
||||||
extension_id: str | None = None,
|
|
||||||
) -> Jinja2Templates:
|
|
||||||
folders = [
|
folders = [
|
||||||
"lnbits/templates",
|
"lnbits/templates",
|
||||||
settings.extension_builder_working_dir_path.as_posix(),
|
settings.extension_builder_working_dir_path.as_posix(),
|
||||||
@@ -124,14 +86,6 @@ def template_renderer(
|
|||||||
t.env.globals["INCLUDED_CSS"] = vendor_files["css"]
|
t.env.globals["INCLUDED_CSS"] = vendor_files["css"]
|
||||||
t.env.globals["INCLUDED_COMPONENTS"] = vendor_files["components"]
|
t.env.globals["INCLUDED_COMPONENTS"] = vendor_files["components"]
|
||||||
|
|
||||||
if not extension_id and additional_folders:
|
|
||||||
for folder in additional_folders:
|
|
||||||
parts = Path(folder).parts
|
|
||||||
if parts and parts[-1] == "templates" and len(parts) >= 2:
|
|
||||||
extension_id = parts[-2]
|
|
||||||
break
|
|
||||||
t.env.globals["INCLUDED_EXTENSION_I18N"] = extension_i18n_urls(extension_id)
|
|
||||||
|
|
||||||
# backwards compatibility for extensions (tpos)
|
# backwards compatibility for extensions (tpos)
|
||||||
t.env.globals["LNBITS_DENOMINATION"] = settings.lnbits_denomination
|
t.env.globals["LNBITS_DENOMINATION"] = settings.lnbits_denomination
|
||||||
|
|
||||||
@@ -418,3 +372,13 @@ def sha256s(value: str) -> str:
|
|||||||
Returns the hex as a string.
|
Returns the hex as a string.
|
||||||
"""
|
"""
|
||||||
return hashlib.sha256(value.encode("utf-8")).hexdigest()
|
return hashlib.sha256(value.encode("utf-8")).hexdigest()
|
||||||
|
|
||||||
|
|
||||||
|
def daystart_timestamp(dt: datetime | None = None) -> int:
|
||||||
|
"""
|
||||||
|
Returns the timestamp of the start of the day for the given
|
||||||
|
datetime (or now in UTC if not provided).
|
||||||
|
"""
|
||||||
|
dt = dt or datetime.now(timezone.utc)
|
||||||
|
day_start = dt.replace(hour=0, minute=0, second=0, microsecond=0)
|
||||||
|
return int(day_start.timestamp())
|
||||||
|
|||||||
@@ -0,0 +1,78 @@
|
|||||||
|
"""Generate llms.txt markdown from FastAPI OpenAPI schema for AI agents."""
|
||||||
|
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from fastapi import FastAPI
|
||||||
|
from fastapi.responses import PlainTextResponse
|
||||||
|
|
||||||
|
|
||||||
|
def generate_llms_txt(app: FastAPI) -> str:
|
||||||
|
"""Convert an OpenAPI schema to llms.txt markdown format."""
|
||||||
|
openapi_schema = app.openapi()
|
||||||
|
lines: list[str] = []
|
||||||
|
|
||||||
|
# H1: API Title
|
||||||
|
info = openapi_schema.get("info", {})
|
||||||
|
title = info.get("title", "API")
|
||||||
|
lines.append(f"# {title}")
|
||||||
|
lines.append("")
|
||||||
|
|
||||||
|
# Blockquote: Description
|
||||||
|
description = info.get("description")
|
||||||
|
if description:
|
||||||
|
for line in description.strip().split("\n"):
|
||||||
|
lines.append(f"> {line}")
|
||||||
|
lines.append("")
|
||||||
|
|
||||||
|
# Group endpoints by tag
|
||||||
|
paths = openapi_schema.get("paths", {})
|
||||||
|
endpoints_by_tag: dict[str, list[dict[str, Any]]] = {}
|
||||||
|
|
||||||
|
for path, path_item in paths.items():
|
||||||
|
for method in ["get", "post", "put", "patch", "delete", "head", "options"]:
|
||||||
|
if method not in path_item:
|
||||||
|
continue
|
||||||
|
operation = path_item[method]
|
||||||
|
tags = operation.get("tags", ["Endpoints"])
|
||||||
|
tag = tags[0] if tags else "Endpoints"
|
||||||
|
if tag not in endpoints_by_tag:
|
||||||
|
endpoints_by_tag[tag] = []
|
||||||
|
endpoints_by_tag[tag].append(
|
||||||
|
{
|
||||||
|
"path": path,
|
||||||
|
"method": method.upper(),
|
||||||
|
"operation": operation,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
# Generate sections by tag
|
||||||
|
for tag, endpoints in endpoints_by_tag.items():
|
||||||
|
lines.append(f"## {tag}")
|
||||||
|
lines.append("")
|
||||||
|
for endpoint in endpoints:
|
||||||
|
method = endpoint["method"]
|
||||||
|
path = endpoint["path"]
|
||||||
|
operation = endpoint["operation"]
|
||||||
|
summary = operation.get("summary", "")
|
||||||
|
if summary:
|
||||||
|
lines.append(f"### `{method} {path}` - {summary}")
|
||||||
|
else:
|
||||||
|
lines.append(f"### `{method} {path}`")
|
||||||
|
lines.append("")
|
||||||
|
lines.append("")
|
||||||
|
|
||||||
|
return "\n".join(lines).strip() + "\n"
|
||||||
|
|
||||||
|
|
||||||
|
def create_llms_txt_route(app: FastAPI) -> None:
|
||||||
|
"""Add a /llms.txt endpoint to the app."""
|
||||||
|
|
||||||
|
@app.get(
|
||||||
|
"/llms.txt",
|
||||||
|
response_class=PlainTextResponse,
|
||||||
|
include_in_schema=False,
|
||||||
|
summary="Get LLM-friendly API documentation",
|
||||||
|
)
|
||||||
|
async def get_llms_txt() -> str:
|
||||||
|
"""Return the API documentation in llms.txt markdown format."""
|
||||||
|
return generate_llms_txt(app)
|
||||||
@@ -27,6 +27,8 @@ from lnbits.settings import set_cli_settings, settings
|
|||||||
@click.option(
|
@click.option(
|
||||||
"--reload", is_flag=True, default=False, help="Enable auto-reload for development"
|
"--reload", is_flag=True, default=False, help="Enable auto-reload for development"
|
||||||
)
|
)
|
||||||
|
@click.option("--ws-max-queue", default=128, help="Websocket max queue size")
|
||||||
|
@click.option("--ws-ping-timeout", default=60.0, help="Websocket ping timeout")
|
||||||
def main(
|
def main(
|
||||||
port: int,
|
port: int,
|
||||||
host: str,
|
host: str,
|
||||||
@@ -34,6 +36,8 @@ def main(
|
|||||||
ssl_keyfile: str,
|
ssl_keyfile: str,
|
||||||
ssl_certfile: str,
|
ssl_certfile: str,
|
||||||
reload: bool,
|
reload: bool,
|
||||||
|
ws_max_queue: int,
|
||||||
|
ws_ping_timeout: float,
|
||||||
):
|
):
|
||||||
"""Launched with `uv run lnbits` at root level"""
|
"""Launched with `uv run lnbits` at root level"""
|
||||||
|
|
||||||
@@ -58,6 +62,8 @@ def main(
|
|||||||
ssl_keyfile=ssl_keyfile,
|
ssl_keyfile=ssl_keyfile,
|
||||||
ssl_certfile=ssl_certfile,
|
ssl_certfile=ssl_certfile,
|
||||||
reload=reload or False,
|
reload=reload or False,
|
||||||
|
ws_ping_timeout=ws_ping_timeout,
|
||||||
|
ws_max_queue=ws_max_queue,
|
||||||
)
|
)
|
||||||
|
|
||||||
server = uvicorn.Server(config=config)
|
server = uvicorn.Server(config=config)
|
||||||
|
|||||||
+18
-5
@@ -359,9 +359,11 @@ class FeeSettings(LNbitsSettings):
|
|||||||
|
|
||||||
|
|
||||||
class ExchangeProvidersSettings(LNbitsSettings):
|
class ExchangeProvidersSettings(LNbitsSettings):
|
||||||
lnbits_exchange_rate_cache_seconds: int = Field(default=30, ge=0)
|
lnbits_exchange_rate_cache_seconds: int = Field(default=60, ge=0)
|
||||||
lnbits_exchange_history_size: int = Field(default=60, ge=0)
|
lnbits_exchange_history_size: int = Field(default=60, ge=0)
|
||||||
lnbits_exchange_history_refresh_interval_seconds: int = Field(default=300, ge=0)
|
lnbits_exchange_history_refresh_interval_seconds: int = Field(default=300, ge=0)
|
||||||
|
lnbits_price_aggregator_enabled: bool = Field(default=True)
|
||||||
|
lnbits_price_aggregator_url: str = Field(default="https://price.lnbits.com")
|
||||||
|
|
||||||
lnbits_exchange_rate_providers: list[ExchangeRateProvider] = Field(
|
lnbits_exchange_rate_providers: list[ExchangeRateProvider] = Field(
|
||||||
default=[
|
default=[
|
||||||
@@ -494,6 +496,11 @@ class NotificationsSettings(LNbitsSettings):
|
|||||||
and self.lnbits_telegram_notifications_access_token is not None
|
and self.lnbits_telegram_notifications_access_token is not None
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def is_email_notifications_configured(self) -> bool:
|
||||||
|
return self.lnbits_email_notifications_enabled and bool(
|
||||||
|
self.lnbits_email_notifications_email
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class FakeWalletFundingSource(LNbitsSettings):
|
class FakeWalletFundingSource(LNbitsSettings):
|
||||||
fake_wallet_secret: str = Field(default="ToTheMoon1")
|
fake_wallet_secret: str = Field(default="ToTheMoon1")
|
||||||
@@ -587,6 +594,7 @@ class PhoenixdFundingSource(LNbitsSettings):
|
|||||||
phoenixd_api_password: str | None = Field(default=None)
|
phoenixd_api_password: str | None = Field(default=None)
|
||||||
phoenixd_data_dir: str | None = Field(default=None)
|
phoenixd_data_dir: str | None = Field(default=None)
|
||||||
phoenixd_mnemonic: str | None = Field(default=None)
|
phoenixd_mnemonic: str | None = Field(default=None)
|
||||||
|
phoenixd_mnemonic_backup_confirmed: bool = Field(default=False)
|
||||||
|
|
||||||
|
|
||||||
class AlbyFundingSource(LNbitsSettings):
|
class AlbyFundingSource(LNbitsSettings):
|
||||||
@@ -611,6 +619,7 @@ class SparkL2FundingSource(LNbitsSettings):
|
|||||||
spark_l2_external_endpoint: str | None = Field(default="http://localhost:8765")
|
spark_l2_external_endpoint: str | None = Field(default="http://localhost:8765")
|
||||||
spark_l2_external_api_key: str | None = Field(default=None)
|
spark_l2_external_api_key: str | None = Field(default=None)
|
||||||
spark_l2_mnemonic: str | None = Field(default=None)
|
spark_l2_mnemonic: str | None = Field(default=None)
|
||||||
|
spark_l2_mnemonic_backup_confirmed: bool = Field(default=False)
|
||||||
spark_l2_pay_wait_ms: int = Field(default=4000, ge=0)
|
spark_l2_pay_wait_ms: int = Field(default=4000, ge=0)
|
||||||
spark_l2_pay_poll_ms: int = Field(default=500, ge=0)
|
spark_l2_pay_poll_ms: int = Field(default=500, ge=0)
|
||||||
spark_l2_stream_keepalive_ms: int = Field(default=15000, ge=0)
|
spark_l2_stream_keepalive_ms: int = Field(default=15000, ge=0)
|
||||||
@@ -648,6 +657,7 @@ class BoltzFundingSource(LNbitsSettings):
|
|||||||
boltz_client_password: str = Field(default="")
|
boltz_client_password: str = Field(default="")
|
||||||
boltz_client_cert: str | None = Field(default=None)
|
boltz_client_cert: str | None = Field(default=None)
|
||||||
boltz_mnemonic: str | None = Field(default=None)
|
boltz_mnemonic: str | None = Field(default=None)
|
||||||
|
boltz_mnemonic_backup_confirmed: bool = Field(default=False)
|
||||||
|
|
||||||
|
|
||||||
class StrikeFundingSource(LNbitsSettings):
|
class StrikeFundingSource(LNbitsSettings):
|
||||||
@@ -1047,7 +1057,7 @@ class EditableSettings(
|
|||||||
|
|
||||||
|
|
||||||
class UpdateSettings(EditableSettings):
|
class UpdateSettings(EditableSettings):
|
||||||
class Config:
|
class Config(EditableSettings.Config):
|
||||||
extra = Extra.forbid
|
extra = Extra.forbid
|
||||||
|
|
||||||
|
|
||||||
@@ -1072,11 +1082,12 @@ class EnvSettings(LNbitsSettings):
|
|||||||
log_rotation: str = Field(default="100 MB")
|
log_rotation: str = Field(default="100 MB")
|
||||||
log_retention: str = Field(default="3 months")
|
log_retention: str = Field(default="3 months")
|
||||||
first_install_token: str | None = Field(default=None)
|
first_install_token: str | None = Field(default=None)
|
||||||
|
|
||||||
cleanup_wallets_days: int = Field(default=90, ge=0)
|
cleanup_wallets_days: int = Field(default=90, ge=0)
|
||||||
funding_source_max_retries: int = Field(default=4, ge=0)
|
funding_source_max_retries: int = Field(default=4, ge=0)
|
||||||
lnbits_max_users: int = Field(default=0, ge=0)
|
lnbits_max_users: int = Field(default=0, ge=0)
|
||||||
lnbits_max_extensions: int = Field(default=0, ge=0)
|
lnbits_max_extensions: int = Field(default=0, ge=0)
|
||||||
|
task_heart_beat_verbose: bool = Field(default=False)
|
||||||
|
task_heart_beat_interval: int = Field(default=30)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def has_default_extension_path(self) -> bool:
|
def has_default_extension_path(self) -> bool:
|
||||||
@@ -1198,11 +1209,11 @@ class ReadOnlySettings(
|
|||||||
|
|
||||||
|
|
||||||
class Settings(EditableSettings, ReadOnlySettings, TransientSettings, BaseSettings):
|
class Settings(EditableSettings, ReadOnlySettings, TransientSettings, BaseSettings):
|
||||||
class Config:
|
class Config(EditableSettings.Config, BaseSettings.Config): # type: ignore[misc]
|
||||||
env_file = ".env"
|
env_file = ".env"
|
||||||
env_file_encoding = "utf-8"
|
env_file_encoding = "utf-8"
|
||||||
case_sensitive = False
|
case_sensitive = False
|
||||||
json_loads = list_parse_fallback
|
json_loads = list_parse_fallback # type: ignore[assignment]
|
||||||
|
|
||||||
def is_user_allowed(self, user_id: str) -> bool:
|
def is_user_allowed(self, user_id: str) -> bool:
|
||||||
return (
|
return (
|
||||||
@@ -1289,6 +1300,7 @@ class PublicSettings(BaseModel):
|
|||||||
extensions_reviews_url: str = Field(alias="extensionsReviewsUrl")
|
extensions_reviews_url: str = Field(alias="extensionsReviewsUrl")
|
||||||
ext_builder: bool = Field(alias="extBuilder")
|
ext_builder: bool = Field(alias="extBuilder")
|
||||||
nostr_configured: bool = Field(alias="nostrConfigured")
|
nostr_configured: bool = Field(alias="nostrConfigured")
|
||||||
|
email_configured: bool = Field(alias="emailConfigured")
|
||||||
telegram_configured: bool = Field(alias="telegramConfigured")
|
telegram_configured: bool = Field(alias="telegramConfigured")
|
||||||
wallet_featured_button_label: str | None = Field(alias="walletFeaturedButtonLabel")
|
wallet_featured_button_label: str | None = Field(alias="walletFeaturedButtonLabel")
|
||||||
wallet_featured_button_url: str | None = Field(alias="walletFeaturedButtonUrl")
|
wallet_featured_button_url: str | None = Field(alias="walletFeaturedButtonUrl")
|
||||||
@@ -1353,6 +1365,7 @@ class PublicSettings(BaseModel):
|
|||||||
extensionsReviewsUrl=settings.lnbits_extensions_reviews_url,
|
extensionsReviewsUrl=settings.lnbits_extensions_reviews_url,
|
||||||
extBuilder=settings.lnbits_extensions_builder_activate_non_admins,
|
extBuilder=settings.lnbits_extensions_builder_activate_non_admins,
|
||||||
nostrConfigured=settings.is_nostr_notifications_configured(),
|
nostrConfigured=settings.is_nostr_notifications_configured(),
|
||||||
|
emailConfigured=settings.is_email_notifications_configured(),
|
||||||
telegramConfigured=settings.is_telegram_notifications_configured(),
|
telegramConfigured=settings.is_telegram_notifications_configured(),
|
||||||
walletFeaturedButtonLabel=settings.lnbits_wallet_featured_button_label,
|
walletFeaturedButtonLabel=settings.lnbits_wallet_featured_button_label,
|
||||||
walletFeaturedButtonUrl=settings.lnbits_wallet_featured_button_url,
|
walletFeaturedButtonUrl=settings.lnbits_wallet_featured_button_url,
|
||||||
|
|||||||
+1
-1
File diff suppressed because one or more lines are too long
Vendored
+1
-1
File diff suppressed because one or more lines are too long
Vendored
+10
-10
File diff suppressed because one or more lines are too long
@@ -212,12 +212,15 @@ body.bg-image .q-page-container {
|
|||||||
backdrop-filter: none; /* Ensure the page content is not affected */
|
backdrop-filter: none; /* Ensure the page content is not affected */
|
||||||
}
|
}
|
||||||
|
|
||||||
body.body--dark .q-card:not(.q-dialog .q-card, .lnbits__dialog-card, .q-dialog-plugin--dark),
|
body.body--dark .q-card:not(.q-dialog .q-card, .lnbits__dialog-card, .q-dialog-plugin--dark) {
|
||||||
|
--q-dark: rgba(29, 29, 29, 0.3);
|
||||||
|
background-color: var(--q-dark);
|
||||||
|
}
|
||||||
body.body--dark .q-header,
|
body.body--dark .q-header,
|
||||||
body.body--dark .q-drawer {
|
body.body--dark .q-drawer {
|
||||||
--q-dark: rgba(29, 29, 29, 0.3);
|
--q-dark: rgba(29, 29, 29, 0.3);
|
||||||
background-color: var(--q-dark);
|
background-color: var(--q-dark);
|
||||||
backdrop-filter: blur(6px) brightness(0.8);
|
backdrop-filter: brightness(0.8);
|
||||||
}
|
}
|
||||||
|
|
||||||
body.rounded-ui .q-card,
|
body.rounded-ui .q-card,
|
||||||
@@ -388,11 +391,11 @@ body[data-theme=salvador].card-gradient.body--dark .q-drawer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
body.card-shadow .q-card:not(.q-dialog .q-card, .lnbits__dialog-card, .q-dialog-plugin--dark) {
|
body.card-shadow .q-card:not(.q-dialog .q-card, .lnbits__dialog-card, .q-dialog-plugin--dark) {
|
||||||
filter: drop-shadow(0 10px 24px rgba(0, 0, 0, 0.18));
|
box-shadow: 0 10px 24px rgba(0, 0, 0, 0.18);
|
||||||
}
|
}
|
||||||
|
|
||||||
body.card-shadow.body--dark .q-card:not(.q-dialog .q-card, .lnbits__dialog-card, .q-dialog-plugin--dark) {
|
body.card-shadow.body--dark .q-card:not(.q-dialog .q-card, .lnbits__dialog-card, .q-dialog-plugin--dark) {
|
||||||
filter: drop-shadow(0 12px 28px rgba(0, 0, 0, 0.45));
|
box-shadow: 0 12px 28px rgba(0, 0, 0, 0.45);
|
||||||
}
|
}
|
||||||
|
|
||||||
body.no-burger-background .q-drawer {
|
body.no-burger-background .q-drawer {
|
||||||
|
|||||||
@@ -184,6 +184,7 @@ window.localisation.en = {
|
|||||||
release_notes: 'Release Notes',
|
release_notes: 'Release Notes',
|
||||||
activate_extension_details: 'Make extension available/unavailable for users',
|
activate_extension_details: 'Make extension available/unavailable for users',
|
||||||
featured: 'Featured',
|
featured: 'Featured',
|
||||||
|
categories: 'Categories',
|
||||||
all: 'All',
|
all: 'All',
|
||||||
only_admins_can_install: '(Only admin accounts can install extensions)',
|
only_admins_can_install: '(Only admin accounts can install extensions)',
|
||||||
only_admins_can_create_extensions:
|
only_admins_can_create_extensions:
|
||||||
@@ -481,6 +482,8 @@ window.localisation.en = {
|
|||||||
access_control_list_admin_warning:
|
access_control_list_admin_warning:
|
||||||
'This is an admin account. The generated tokens will have admin privileges.',
|
'This is an admin account. The generated tokens will have admin privileges.',
|
||||||
new_api_acl: 'New Access Control List',
|
new_api_acl: 'New Access Control List',
|
||||||
|
acl_token_active: 'Active',
|
||||||
|
acl_token_expired: 'Expired',
|
||||||
api_token_id: 'Token Id',
|
api_token_id: 'Token Id',
|
||||||
toggle_gradient: 'Toggle Gradient',
|
toggle_gradient: 'Toggle Gradient',
|
||||||
gradient_background: 'Gradient Background',
|
gradient_background: 'Gradient Background',
|
||||||
|
|||||||
@@ -62,12 +62,6 @@ window.app.component('lnbits-admin-exchange-providers', {
|
|||||||
mounted() {
|
mounted() {
|
||||||
this.getExchangeRateHistory()
|
this.getExchangeRateHistory()
|
||||||
},
|
},
|
||||||
created() {
|
|
||||||
const hash = window.location.hash.replace('#', '')
|
|
||||||
if (hash === 'exchange_providers') {
|
|
||||||
this.showExchangeProvidersTab(hash)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
methods: {
|
methods: {
|
||||||
getDefaultSetting(fieldName) {
|
getDefaultSetting(fieldName) {
|
||||||
LNbits.api.getDefaultSetting(fieldName).then(response => {
|
LNbits.api.getDefaultSetting(fieldName).then(response => {
|
||||||
@@ -127,18 +121,21 @@ window.app.component('lnbits-admin-exchange-providers', {
|
|||||||
this.exchangeData.showTickerConversion = true
|
this.exchangeData.showTickerConversion = true
|
||||||
},
|
},
|
||||||
initExchangeChart(data) {
|
initExchangeChart(data) {
|
||||||
|
if (this.exchangeRatesChart) {
|
||||||
|
this.exchangeRatesChart.destroy()
|
||||||
|
this.exchangeRatesChart = null
|
||||||
|
}
|
||||||
const xValues = data.map(d =>
|
const xValues = data.map(d =>
|
||||||
this.utils.formatTimestamp(d.timestamp, 'HH:mm')
|
this.utils.formatTimestamp(d.timestamp, 'HH:mm')
|
||||||
)
|
)
|
||||||
const exchanges = [
|
const exchanges = this.formData.lnbits_price_aggregator_enabled
|
||||||
...this.formData.lnbits_exchange_rate_providers,
|
? [{name: 'Aggregator'}]
|
||||||
{name: 'LNbits'}
|
: [...this.formData.lnbits_exchange_rate_providers, {name: 'LNbits'}]
|
||||||
]
|
|
||||||
const datasets = exchanges.map(exchange => ({
|
const datasets = exchanges.map(exchange => ({
|
||||||
label: exchange.name,
|
label: exchange.name,
|
||||||
data: data.map(d => d.rates[exchange.name]),
|
data: data.map(d => d.rates[exchange.name]),
|
||||||
pointStyle: true,
|
pointStyle: true,
|
||||||
borderWidth: exchange.name === 'LNbits' ? 4 : 1,
|
borderWidth: exchange.name === 'LNbits' ? 4 : 2,
|
||||||
tension: 0.4
|
tension: 0.4
|
||||||
}))
|
}))
|
||||||
this.exchangeRatesChart = new Chart(
|
this.exchangeRatesChart = new Chart(
|
||||||
@@ -148,7 +145,11 @@ window.app.component('lnbits-admin-exchange-providers', {
|
|||||||
options: {
|
options: {
|
||||||
plugins: {
|
plugins: {
|
||||||
legend: {
|
legend: {
|
||||||
display: false
|
display: true
|
||||||
|
},
|
||||||
|
title: {
|
||||||
|
display: true,
|
||||||
|
text: 'Bitcoin Price History'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -0,0 +1,151 @@
|
|||||||
|
window.app.component('lnbits-admin-funding-seed-backup', {
|
||||||
|
props: ['active', 'is-super-user', 'form-data', 'settings'],
|
||||||
|
template: '#lnbits-admin-funding-seed-backup',
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
dialog: {
|
||||||
|
show: false,
|
||||||
|
step: 1,
|
||||||
|
seed: '',
|
||||||
|
visible: false,
|
||||||
|
challenge: [],
|
||||||
|
answers: {},
|
||||||
|
error: '',
|
||||||
|
confirmField: ''
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
watch: {
|
||||||
|
active(isActive) {
|
||||||
|
if (isActive) {
|
||||||
|
this.openIfRequired()
|
||||||
|
}
|
||||||
|
},
|
||||||
|
'formData.lnbits_backend_wallet_class'(walletClass, previousWalletClass) {
|
||||||
|
const source = this.seedBackupSource(walletClass)
|
||||||
|
if (previousWalletClass && source && this.formData[source.seedField]) {
|
||||||
|
this.formData[source.confirmField] = false
|
||||||
|
}
|
||||||
|
this.openIfRequired()
|
||||||
|
},
|
||||||
|
'formData.boltz_mnemonic'() {
|
||||||
|
this.formData.boltz_mnemonic_backup_confirmed =
|
||||||
|
this.formData.boltz_mnemonic === this.settings.boltz_mnemonic
|
||||||
|
? this.settings.boltz_mnemonic_backup_confirmed
|
||||||
|
: false
|
||||||
|
this.openIfRequired()
|
||||||
|
},
|
||||||
|
'formData.phoenixd_mnemonic'() {
|
||||||
|
this.formData.phoenixd_mnemonic_backup_confirmed =
|
||||||
|
this.formData.phoenixd_mnemonic === this.settings.phoenixd_mnemonic
|
||||||
|
? this.settings.phoenixd_mnemonic_backup_confirmed
|
||||||
|
: false
|
||||||
|
this.openIfRequired()
|
||||||
|
},
|
||||||
|
'formData.spark_l2_mnemonic'() {
|
||||||
|
this.formData.spark_l2_mnemonic_backup_confirmed =
|
||||||
|
this.formData.spark_l2_mnemonic === this.settings.spark_l2_mnemonic
|
||||||
|
? this.settings.spark_l2_mnemonic_backup_confirmed
|
||||||
|
: false
|
||||||
|
this.openIfRequired()
|
||||||
|
}
|
||||||
|
},
|
||||||
|
computed: {
|
||||||
|
seedWords() {
|
||||||
|
return this.dialog.seed
|
||||||
|
.split(/\s+/)
|
||||||
|
.filter(Boolean)
|
||||||
|
.map((word, index) => ({index, word}))
|
||||||
|
}
|
||||||
|
},
|
||||||
|
created() {
|
||||||
|
this.openIfRequired()
|
||||||
|
},
|
||||||
|
methods: {
|
||||||
|
seedBackupSource(walletClass = this.formData.lnbits_backend_wallet_class) {
|
||||||
|
if (walletClass === 'BoltzWallet') {
|
||||||
|
return {
|
||||||
|
seedField: 'boltz_mnemonic',
|
||||||
|
confirmField: 'boltz_mnemonic_backup_confirmed'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (walletClass === 'PhoenixdWallet') {
|
||||||
|
return {
|
||||||
|
seedField: 'phoenixd_mnemonic',
|
||||||
|
confirmField: 'phoenixd_mnemonic_backup_confirmed'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (walletClass === 'SparkL2Wallet') {
|
||||||
|
return {
|
||||||
|
seedField: 'spark_l2_mnemonic',
|
||||||
|
confirmField: 'spark_l2_mnemonic_backup_confirmed'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
openIfRequired() {
|
||||||
|
if (!this.active || !this.isSuperUser) return
|
||||||
|
|
||||||
|
const source = this.seedBackupSource()
|
||||||
|
if (!source) return
|
||||||
|
|
||||||
|
const seed = (this.formData[source.seedField] || '').trim()
|
||||||
|
const confirmed = this.formData[source.confirmField]
|
||||||
|
if (!seed || confirmed || this.dialog.show) return
|
||||||
|
|
||||||
|
this.dialog = {
|
||||||
|
show: true,
|
||||||
|
step: 1,
|
||||||
|
seed,
|
||||||
|
visible: false,
|
||||||
|
challenge: [],
|
||||||
|
answers: {},
|
||||||
|
error: '',
|
||||||
|
confirmField: source.confirmField
|
||||||
|
}
|
||||||
|
},
|
||||||
|
prepareChallenge() {
|
||||||
|
const words = this.dialog.seed.split(/\s+/).filter(Boolean)
|
||||||
|
const count = Math.min(4, words.length)
|
||||||
|
const indexes = _.shuffle([...Array(words.length).keys()]).slice(0, count)
|
||||||
|
this.dialog.challenge = indexes
|
||||||
|
.sort((a, b) => a - b)
|
||||||
|
.map(index => ({index, word: words[index]}))
|
||||||
|
this.dialog.answers = {}
|
||||||
|
this.dialog.error = ''
|
||||||
|
this.dialog.step = 2
|
||||||
|
},
|
||||||
|
submitChallenge() {
|
||||||
|
const isValid = this.dialog.challenge.every(({index, word}) => {
|
||||||
|
const answer = this.dialog.answers[index] || ''
|
||||||
|
return answer.trim().toLowerCase() === word.toLowerCase()
|
||||||
|
})
|
||||||
|
if (!isValid) {
|
||||||
|
this.dialog.error =
|
||||||
|
'One or more words are incorrect. Check your backup and try again.'
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const field = this.dialog.confirmField
|
||||||
|
LNbits.api
|
||||||
|
.request(
|
||||||
|
'PATCH',
|
||||||
|
'/admin/api/v1/settings',
|
||||||
|
this.g.user.wallets[0].adminkey,
|
||||||
|
{
|
||||||
|
[field]: true
|
||||||
|
}
|
||||||
|
)
|
||||||
|
.then(() => {
|
||||||
|
this.formData[field] = true
|
||||||
|
this.settings[field] = true
|
||||||
|
this.dialog.show = false
|
||||||
|
Quasar.Notify.create({
|
||||||
|
type: 'positive',
|
||||||
|
message: 'Seed backup confirmed',
|
||||||
|
icon: 'check'
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.catch(LNbits.utils.notifyApiError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
window.app.component('lnbits-admin-funding', {
|
window.app.component('lnbits-admin-funding', {
|
||||||
props: ['is-super-user', 'form-data', 'settings'],
|
props: ['active', 'is-super-user', 'form-data', 'settings'],
|
||||||
template: '#lnbits-admin-funding',
|
template: '#lnbits-admin-funding',
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -360,18 +360,37 @@ window.app.component('lnbits-payment-list', {
|
|||||||
paymentTableRowKey(row) {
|
paymentTableRowKey(row) {
|
||||||
return row.payment_hash + row.amount
|
return row.payment_hash + row.amount
|
||||||
},
|
},
|
||||||
exportCSV(detailed = false) {
|
async exportCSV(detailed = false) {
|
||||||
// status is important for export but it is not in paymentsTable
|
// status is important for export but it is not in paymentsTable
|
||||||
// because it is manually added with payment detail link and icons
|
// because it is manually added with payment detail link and icons
|
||||||
// and would cause duplication in the list
|
// and would cause duplication in the list
|
||||||
const pagination = this.paymentsTable.pagination
|
const pagination = this.paymentsTable.pagination
|
||||||
const query = {
|
const maxPages = 100
|
||||||
sortby: pagination.sortBy ?? 'time',
|
const limit = 1000
|
||||||
direction: pagination.descending ? 'desc' : 'asc'
|
let payments = []
|
||||||
}
|
|
||||||
const params = new URLSearchParams(query)
|
this.paymentsCSV.loading = true
|
||||||
LNbits.api.getPayments(this.wallet, params).then(response => {
|
try {
|
||||||
let payments = response.data.data.map(this.mapPayment)
|
for (let page = 0; page < maxPages; page++) {
|
||||||
|
const query = {
|
||||||
|
sortby: pagination.sortBy ?? 'time',
|
||||||
|
direction: pagination.descending ? 'desc' : 'asc',
|
||||||
|
limit,
|
||||||
|
offset: page * limit
|
||||||
|
}
|
||||||
|
const params = new URLSearchParams(query)
|
||||||
|
const response = await LNbits.api.getPayments(this.wallet, params)
|
||||||
|
const pagePayments = response.data.data || []
|
||||||
|
payments = payments.concat(pagePayments.map(this.mapPayment))
|
||||||
|
|
||||||
|
if (
|
||||||
|
pagePayments.length < limit ||
|
||||||
|
payments.length >= response.data.total
|
||||||
|
) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let columns = this.paymentsCSV.columns
|
let columns = this.paymentsCSV.columns
|
||||||
|
|
||||||
if (detailed) {
|
if (detailed) {
|
||||||
@@ -400,7 +419,11 @@ window.app.component('lnbits-payment-list', {
|
|||||||
payments,
|
payments,
|
||||||
this.wallet.name + '-payments'
|
this.wallet.name + '-payments'
|
||||||
)
|
)
|
||||||
})
|
} catch (err) {
|
||||||
|
LNbits.utils.notifyApiError(err)
|
||||||
|
} finally {
|
||||||
|
this.paymentsCSV.loading = false
|
||||||
|
}
|
||||||
},
|
},
|
||||||
addFilterTag() {
|
addFilterTag() {
|
||||||
if (!this.exportTagName) return
|
if (!this.exportTagName) return
|
||||||
|
|||||||
@@ -8,6 +8,10 @@ window.app.component('lnbits-qrcode-lnurl', {
|
|||||||
prefix: {
|
prefix: {
|
||||||
type: String,
|
type: String,
|
||||||
default: 'lnurlp'
|
default: 'lnurlp'
|
||||||
|
},
|
||||||
|
href: {
|
||||||
|
type: String,
|
||||||
|
default: ''
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
data() {
|
data() {
|
||||||
@@ -21,7 +25,10 @@ window.app.component('lnbits-qrcode-lnurl', {
|
|||||||
if (this.tab == 'bech32') {
|
if (this.tab == 'bech32') {
|
||||||
const bytes = new TextEncoder().encode(this.url)
|
const bytes = new TextEncoder().encode(this.url)
|
||||||
const bech32 = NostrTools.nip19.encodeBytes('lnurl', bytes)
|
const bech32 = NostrTools.nip19.encodeBytes('lnurl', bytes)
|
||||||
this.lnurl = `lightning:${bech32.toUpperCase()}`
|
this.lnurl =
|
||||||
|
this.href && this.href.trim() !== ''
|
||||||
|
? `${this.href}?lightning=${bech32.toUpperCase()}`
|
||||||
|
: `lightning:${bech32.toUpperCase()}`
|
||||||
} else if (this.tab == 'lud17') {
|
} else if (this.tab == 'lud17') {
|
||||||
if (this.url.startsWith('http://')) {
|
if (this.url.startsWith('http://')) {
|
||||||
this.lnurl = this.url.replace('http://', this.prefix + '://')
|
this.lnurl = this.url.replace('http://', this.prefix + '://')
|
||||||
|
|||||||
@@ -85,6 +85,9 @@ window.app.component('lnbits-qrcode', {
|
|||||||
event.preventDefault()
|
event.preventDefault()
|
||||||
event.stopPropagation()
|
event.stopPropagation()
|
||||||
return false
|
return false
|
||||||
|
} else if (this.href && this.href.startsWith('http')) {
|
||||||
|
window.open(this.href, '_blank')
|
||||||
|
event.preventDefault()
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
async writeNfcTag() {
|
async writeNfcTag() {
|
||||||
|
|||||||
@@ -27,6 +27,14 @@ const DynamicComponent = {
|
|||||||
name: r.name,
|
name: r.name,
|
||||||
component: async () => {
|
component: async () => {
|
||||||
await LNbits.utils.loadTemplate(r.template)
|
await LNbits.utils.loadTemplate(r.template)
|
||||||
|
if (r.i18n) {
|
||||||
|
const locale =
|
||||||
|
window.i18n?.global?.locale?.value ??
|
||||||
|
window.i18n?.global?.locale ??
|
||||||
|
window.g.locale ??
|
||||||
|
'en'
|
||||||
|
await LNbits.utils.loadExtI18n(r.i18n, locale)
|
||||||
|
}
|
||||||
await LNbits.utils.loadScript(r.component)
|
await LNbits.utils.loadScript(r.component)
|
||||||
return window[r.name]
|
return window[r.name]
|
||||||
}
|
}
|
||||||
@@ -151,6 +159,30 @@ window.i18n = new VueI18n.createI18n({
|
|||||||
fallbackLocale: 'en',
|
fallbackLocale: 'en',
|
||||||
messages: window.localisation
|
messages: window.localisation
|
||||||
})
|
})
|
||||||
|
;(function () {
|
||||||
|
let _applying = false
|
||||||
|
let _target = null
|
||||||
|
Vue.watch(
|
||||||
|
() => window.i18n.global.locale,
|
||||||
|
async (locale, prevLocale) => {
|
||||||
|
if (_applying || !LNbits.utils._extI18nDirs.size) return
|
||||||
|
_target = locale
|
||||||
|
_applying = true
|
||||||
|
window.i18n.global.locale = prevLocale
|
||||||
|
_applying = false
|
||||||
|
await Promise.all(
|
||||||
|
[...LNbits.utils._extI18nDirs].map(dir =>
|
||||||
|
LNbits.utils.loadExtI18n(dir, locale)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
if (_target !== locale) return
|
||||||
|
_applying = true
|
||||||
|
window.i18n.global.locale = locale
|
||||||
|
_applying = false
|
||||||
|
},
|
||||||
|
{flush: 'sync'}
|
||||||
|
)
|
||||||
|
})()
|
||||||
|
|
||||||
window.app.mixin({
|
window.app.mixin({
|
||||||
data() {
|
data() {
|
||||||
|
|||||||
@@ -217,6 +217,35 @@ window.PageAccount = {
|
|||||||
computed: {
|
computed: {
|
||||||
isUserTouched() {
|
isUserTouched() {
|
||||||
return !_.isEqual(this.g.user, this.untouchedUser)
|
return !_.isEqual(this.g.user, this.untouchedUser)
|
||||||
|
},
|
||||||
|
selectedApiToken() {
|
||||||
|
return this.selectedApiAcl.token_id_list.find(
|
||||||
|
token => token.id === this.apiAcl.selectedTokenId
|
||||||
|
)
|
||||||
|
},
|
||||||
|
expiryAt() {
|
||||||
|
if (this.selectedApiToken.expires_at) {
|
||||||
|
return `${this.$t('expiry')}: ${LNbits.utils.formatTimestamp(this.selectedApiToken.expires_at)}`
|
||||||
|
} else {
|
||||||
|
return ''
|
||||||
|
}
|
||||||
|
},
|
||||||
|
tokenStatus() {
|
||||||
|
if (this.selectedApiToken.expires_at) {
|
||||||
|
const now = new Date()
|
||||||
|
const expiresAt = new Date(this.selectedApiToken.expires_at * 1000)
|
||||||
|
let status = ''
|
||||||
|
let badgeColor = 'positive'
|
||||||
|
if (expiresAt < now) {
|
||||||
|
status = this.$t('acl_token_expired')
|
||||||
|
badgeColor = 'negative'
|
||||||
|
} else {
|
||||||
|
status = this.$t('acl_token_active')
|
||||||
|
}
|
||||||
|
return {status, badgeColor}
|
||||||
|
} else {
|
||||||
|
return ''
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ window.PageExtensions = {
|
|||||||
tab: 'installed',
|
tab: 'installed',
|
||||||
manageExtensionTab: 'releases',
|
manageExtensionTab: 'releases',
|
||||||
filteredExtensions: [],
|
filteredExtensions: [],
|
||||||
|
categories: new Set(),
|
||||||
updatableExtensions: [],
|
updatableExtensions: [],
|
||||||
showUninstallDialog: false,
|
showUninstallDialog: false,
|
||||||
showManageExtensionDialog: false,
|
showManageExtensionDialog: false,
|
||||||
@@ -106,6 +107,10 @@ window.PageExtensions = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const isCategoryTab = !['installed', 'all', 'featured'].includes(tab)
|
||||||
|
const isInSelectedCategory = extension =>
|
||||||
|
extension.categories?.includes(tab) ?? false
|
||||||
|
|
||||||
this.filteredExtensions = this.extensions
|
this.filteredExtensions = this.extensions
|
||||||
.filter(e => (tab === 'all' ? !e.isInstalled : true))
|
.filter(e => (tab === 'all' ? !e.isInstalled : true))
|
||||||
.filter(e => (tab === 'installed' ? e.isInstalled : true))
|
.filter(e => (tab === 'installed' ? e.isInstalled : true))
|
||||||
@@ -113,6 +118,7 @@ window.PageExtensions = {
|
|||||||
tab === 'installed' ? (e.isActive ? true : !!this.g.user.admin) : true
|
tab === 'installed' ? (e.isActive ? true : !!this.g.user.admin) : true
|
||||||
)
|
)
|
||||||
.filter(e => (tab === 'featured' ? e.isFeatured : true))
|
.filter(e => (tab === 'featured' ? e.isFeatured : true))
|
||||||
|
.filter(e => (isCategoryTab ? isInSelectedCategory(e) : true))
|
||||||
.filter(extensionNameContains(term))
|
.filter(extensionNameContains(term))
|
||||||
.map(e => ({
|
.map(e => ({
|
||||||
...e,
|
...e,
|
||||||
@@ -832,6 +838,9 @@ window.PageExtensions = {
|
|||||||
async fetchAllExtensions() {
|
async fetchAllExtensions() {
|
||||||
try {
|
try {
|
||||||
const {data} = await LNbits.api.request('GET', `/api/v1/extension/all`)
|
const {data} = await LNbits.api.request('GET', `/api/v1/extension/all`)
|
||||||
|
data.forEach(ext => {
|
||||||
|
ext.categories?.forEach(category => this.categories.add(category))
|
||||||
|
})
|
||||||
return data
|
return data
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.warn(error)
|
console.warn(error)
|
||||||
|
|||||||
@@ -223,6 +223,12 @@ window.PageWallet = {
|
|||||||
if (data.tag === 'payRequest') {
|
if (data.tag === 'payRequest') {
|
||||||
this.parse.lnurlpay = Object.freeze(data)
|
this.parse.lnurlpay = Object.freeze(data)
|
||||||
this.parse.data.amount = data.minSendable / 1000
|
this.parse.data.amount = data.minSendable / 1000
|
||||||
|
this.receive.units = [
|
||||||
|
'sats',
|
||||||
|
...(this.g.allowedCurrencies.length > 0
|
||||||
|
? this.g.allowedCurrencies
|
||||||
|
: this.g.currencies)
|
||||||
|
]
|
||||||
} else if (data.tag === 'login') {
|
} else if (data.tag === 'login') {
|
||||||
this.parse.lnurlauth = Object.freeze(data)
|
this.parse.lnurlauth = Object.freeze(data)
|
||||||
} else if (data.tag === 'withdrawRequest') {
|
} else if (data.tag === 'withdrawRequest') {
|
||||||
@@ -408,12 +414,19 @@ window.PageWallet = {
|
|||||||
switch (action.tag) {
|
switch (action.tag) {
|
||||||
case 'url':
|
case 'url':
|
||||||
Quasar.Notify.create({
|
Quasar.Notify.create({
|
||||||
message: `<a target="_blank" style="color: inherit" href="${action.url}">${action.url}</a>`,
|
message: action.url,
|
||||||
caption: action.description,
|
caption: action.description,
|
||||||
html: true,
|
html: false,
|
||||||
type: 'positive',
|
type: 'positive',
|
||||||
timeout: 0,
|
timeout: 0,
|
||||||
closeBtn: true
|
closeBtn: true,
|
||||||
|
actions: [
|
||||||
|
{
|
||||||
|
label: 'Open link',
|
||||||
|
color: 'white',
|
||||||
|
handler: () => this.utils.openUrlInNewTab(action.url)
|
||||||
|
}
|
||||||
|
]
|
||||||
})
|
})
|
||||||
break
|
break
|
||||||
case 'message':
|
case 'message':
|
||||||
@@ -425,15 +438,29 @@ window.PageWallet = {
|
|||||||
})
|
})
|
||||||
break
|
break
|
||||||
case 'aes':
|
case 'aes':
|
||||||
this.utils.decryptLnurlPayAES(action, response.data.preimage)
|
this.utils
|
||||||
Quasar.Notify.create({
|
.decryptLnurlPayAES(action, response.data.preimage)
|
||||||
message: value,
|
.then(value => {
|
||||||
caption: extra.success_action.description,
|
Quasar.Notify.create({
|
||||||
html: true,
|
message: value,
|
||||||
type: 'positive',
|
caption: action.description,
|
||||||
timeout: 0,
|
html: false,
|
||||||
closeBtn: true
|
type: 'positive',
|
||||||
})
|
timeout: 0,
|
||||||
|
closeBtn: true
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
Quasar.Notify.create({
|
||||||
|
message: action.description || 'Payment successful.',
|
||||||
|
caption: 'Could not decrypt success action.',
|
||||||
|
html: false,
|
||||||
|
type: 'warning',
|
||||||
|
timeout: 0,
|
||||||
|
closeBtn: true
|
||||||
|
})
|
||||||
|
})
|
||||||
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -323,6 +323,20 @@ window._lnbitsUtils = {
|
|||||||
converter.setOption('simpleLineBreaks', true)
|
converter.setOption('simpleLineBreaks', true)
|
||||||
return converter.makeHtml(text)
|
return converter.makeHtml(text)
|
||||||
},
|
},
|
||||||
|
_extI18nDirs: new Set(),
|
||||||
|
_extI18nLoaded: {},
|
||||||
|
loadExtI18n(dir, locale) {
|
||||||
|
this._extI18nDirs.add(dir)
|
||||||
|
const loaded = (this._extI18nLoaded[dir] ??= {})
|
||||||
|
if (loaded[locale]) return loaded[locale]
|
||||||
|
loaded[locale] = this.loadScript(`${dir}/${locale}.js`).catch(() => {
|
||||||
|
if (locale !== 'en') {
|
||||||
|
loaded['en'] ??= this.loadScript(`${dir}/en.js`).catch(() => {})
|
||||||
|
return loaded['en']
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return loaded[locale]
|
||||||
|
},
|
||||||
async decryptLnurlPayAES(success_action, preimage) {
|
async decryptLnurlPayAES(success_action, preimage) {
|
||||||
let keyb = new Uint8Array(
|
let keyb = new Uint8Array(
|
||||||
preimage.match(/[\da-f]{2}/gi).map(h => parseInt(h, 16))
|
preimage.match(/[\da-f]{2}/gi).map(h => parseInt(h, 16))
|
||||||
@@ -351,5 +365,27 @@ window._lnbitsUtils = {
|
|||||||
let decoder = new TextDecoder('utf-8')
|
let decoder = new TextDecoder('utf-8')
|
||||||
return decoder.decode(valueb)
|
return decoder.decode(valueb)
|
||||||
})
|
})
|
||||||
|
},
|
||||||
|
validateBrowsableUrl(urlString, allowLoopback = false) {
|
||||||
|
const url = new URL(urlString)
|
||||||
|
if (url.protocol !== 'http:' && url.protocol !== 'https:') {
|
||||||
|
throw new Error('Invalid protocol')
|
||||||
|
}
|
||||||
|
if (!allowLoopback) {
|
||||||
|
const host = url.hostname
|
||||||
|
if (
|
||||||
|
host === 'localhost' ||
|
||||||
|
host === '[::1]' ||
|
||||||
|
host === '::1' ||
|
||||||
|
host.startsWith('127.') ||
|
||||||
|
host.startsWith('::ffff:127.')
|
||||||
|
) {
|
||||||
|
throw new Error('Loopback addresses are not allowed')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
openUrlInNewTab(urlString, allowLoopback = false) {
|
||||||
|
this.validateBrowsableUrl(urlString, allowLoopback)
|
||||||
|
window.open(urlString, '_blank', 'noopener,noreferrer')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -58,11 +58,15 @@ body.bg-image {
|
|||||||
}
|
}
|
||||||
// transparent background for specific elements
|
// transparent background for specific elements
|
||||||
body.body--dark {
|
body.body--dark {
|
||||||
.q-card:not(.q-dialog .q-card, .lnbits__dialog-card, .q-dialog-plugin--dark),
|
.q-card:not(.q-dialog .q-card, .lnbits__dialog-card, .q-dialog-plugin--dark) {
|
||||||
|
--q-dark: #{color.adjust(#1d1d1d, $alpha: -0.7)};
|
||||||
|
background-color: var(--q-dark);
|
||||||
|
}
|
||||||
|
|
||||||
.q-header,
|
.q-header,
|
||||||
.q-drawer {
|
.q-drawer {
|
||||||
--q-dark: #{color.adjust(#1d1d1d, $alpha: -0.7)};
|
--q-dark: #{color.adjust(#1d1d1d, $alpha: -0.7)};
|
||||||
background-color: var(--q-dark);
|
background-color: var(--q-dark);
|
||||||
backdrop-filter: blur(6px) brightness(0.8);
|
backdrop-filter: brightness(0.8);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -61,13 +61,13 @@ body.rounded-ui {
|
|||||||
|
|
||||||
body.card-shadow {
|
body.card-shadow {
|
||||||
.q-card:not(.q-dialog .q-card, .lnbits__dialog-card, .q-dialog-plugin--dark) {
|
.q-card:not(.q-dialog .q-card, .lnbits__dialog-card, .q-dialog-plugin--dark) {
|
||||||
filter: drop-shadow(0 10px 24px rgba(0, 0, 0, 0.18));
|
box-shadow: 0 10px 24px rgba(0, 0, 0, 0.18);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
body.card-shadow.body--dark {
|
body.card-shadow.body--dark {
|
||||||
.q-card:not(.q-dialog .q-card, .lnbits__dialog-card, .q-dialog-plugin--dark) {
|
.q-card:not(.q-dialog .q-card, .lnbits__dialog-card, .q-dialog-plugin--dark) {
|
||||||
filter: drop-shadow(0 12px 28px rgba(0, 0, 0, 0.45));
|
box-shadow: 0 12px 28px rgba(0, 0, 0, 0.45);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -58,6 +58,7 @@
|
|||||||
"js/pages/users.js",
|
"js/pages/users.js",
|
||||||
"js/pages/account.js",
|
"js/pages/account.js",
|
||||||
"js/pages/admin.js",
|
"js/pages/admin.js",
|
||||||
|
"js/components/admin/lnbits-admin-funding-seed-backup.js",
|
||||||
"js/components/admin/lnbits-admin-funding.js",
|
"js/components/admin/lnbits-admin-funding.js",
|
||||||
"js/components/admin/lnbits-admin-funding-sources.js",
|
"js/components/admin/lnbits-admin-funding-sources.js",
|
||||||
"js/components/admin/lnbits-admin-fiat-providers.js",
|
"js/components/admin/lnbits-admin-fiat-providers.js",
|
||||||
|
|||||||
@@ -0,0 +1,200 @@
|
|||||||
|
import asyncio
|
||||||
|
import traceback
|
||||||
|
import uuid
|
||||||
|
from collections.abc import Callable, Coroutine
|
||||||
|
from datetime import datetime, timezone
|
||||||
|
|
||||||
|
from loguru import logger
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
from lnbits.core.models import Payment
|
||||||
|
from lnbits.settings import settings
|
||||||
|
|
||||||
|
|
||||||
|
class PublicTask(BaseModel):
|
||||||
|
"""Public model used to expose task information via the API."""
|
||||||
|
|
||||||
|
name: str
|
||||||
|
created_at: datetime
|
||||||
|
|
||||||
|
|
||||||
|
class Task:
|
||||||
|
"""Model used on the backend to keep track of background tasks."""
|
||||||
|
|
||||||
|
coro: Coroutine
|
||||||
|
name: str
|
||||||
|
created_at: datetime
|
||||||
|
task: asyncio.Task
|
||||||
|
invoice_queue: asyncio.Queue[Payment] | None = None
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
coro: Coroutine,
|
||||||
|
name: str | None = None,
|
||||||
|
invoice_queue: asyncio.Queue | None = None,
|
||||||
|
) -> None:
|
||||||
|
self.coro = coro
|
||||||
|
self.name = name or f"task_{uuid.uuid4()}"
|
||||||
|
self.created_at = datetime.now(timezone.utc)
|
||||||
|
self.task = asyncio.create_task(self.coro, name=self.name)
|
||||||
|
self.invoice_queue = invoice_queue
|
||||||
|
|
||||||
|
|
||||||
|
class TaskManager:
|
||||||
|
"""Singleton class to manage background tasks."""
|
||||||
|
|
||||||
|
tasks: list[Task] = []
|
||||||
|
invoice_queue: asyncio.Queue[Payment] = asyncio.Queue()
|
||||||
|
internal_invoice_queue: asyncio.Queue[Payment] = asyncio.Queue()
|
||||||
|
|
||||||
|
def init(self) -> None:
|
||||||
|
self.create_permanent_task(
|
||||||
|
func=self._heart_beat,
|
||||||
|
interval=settings.task_heart_beat_interval,
|
||||||
|
)
|
||||||
|
self.create_permanent_task(self._invoice_listener_consumer)
|
||||||
|
self.create_permanent_task(self._internal_invoice_listener_consumer)
|
||||||
|
|
||||||
|
def get_task(self, name: str) -> Task | None:
|
||||||
|
"""Get a running task by name."""
|
||||||
|
for task in self.tasks:
|
||||||
|
if task.name == name:
|
||||||
|
return task
|
||||||
|
return None
|
||||||
|
|
||||||
|
def get_public_tasks(self) -> list[PublicTask]:
|
||||||
|
"""Get a list of public tasks."""
|
||||||
|
return [PublicTask(name=t.name, created_at=t.created_at) for t in self.tasks]
|
||||||
|
|
||||||
|
def cancel_task(self, task: Task) -> None:
|
||||||
|
"""Cancel a running task."""
|
||||||
|
self.tasks.remove(task)
|
||||||
|
try:
|
||||||
|
task.task.cancel()
|
||||||
|
except Exception as exc:
|
||||||
|
logger.warning(f"error while cancelling task `{task.name}`: {exc!s}")
|
||||||
|
|
||||||
|
def cancel_all_tasks(self) -> None:
|
||||||
|
"""Cancel all running tasks."""
|
||||||
|
for task in list(self.tasks):
|
||||||
|
self.cancel_task(task)
|
||||||
|
|
||||||
|
def create_task(
|
||||||
|
self,
|
||||||
|
coro: Coroutine,
|
||||||
|
name: str | None = None,
|
||||||
|
invoice_queue: asyncio.Queue | None = None,
|
||||||
|
) -> Task:
|
||||||
|
"""Create a task. If a task with the same name exists, it will be cancelled."""
|
||||||
|
if name:
|
||||||
|
task = self.get_task(name)
|
||||||
|
if task:
|
||||||
|
self.cancel_task(task)
|
||||||
|
task = Task(coro=coro, name=name, invoice_queue=invoice_queue)
|
||||||
|
self.tasks.append(task)
|
||||||
|
return task
|
||||||
|
|
||||||
|
def create_permanent_task(
|
||||||
|
self,
|
||||||
|
func: Callable[[], Coroutine],
|
||||||
|
invoice_queue: asyncio.Queue | None = None,
|
||||||
|
name: str | None = None,
|
||||||
|
interval: int = 0,
|
||||||
|
) -> Task:
|
||||||
|
"""Create a task that runs forever and restarts on failure."""
|
||||||
|
|
||||||
|
async def wrapper():
|
||||||
|
while settings.lnbits_running:
|
||||||
|
await self._catch_everything_and_restart(func)
|
||||||
|
if interval > 0:
|
||||||
|
await asyncio.sleep(interval)
|
||||||
|
|
||||||
|
return self.create_task(
|
||||||
|
coro=wrapper(), name=name or func.__name__, invoice_queue=invoice_queue
|
||||||
|
)
|
||||||
|
|
||||||
|
def register_invoice_listener(
|
||||||
|
self,
|
||||||
|
func: Callable[[Payment], Coroutine],
|
||||||
|
name: str | None = None,
|
||||||
|
) -> Task:
|
||||||
|
"""
|
||||||
|
A method intended for extensions to call when they want to be notified about
|
||||||
|
incoming payments. Will call provided Coroutine with the updated payment.
|
||||||
|
"""
|
||||||
|
name = f"{name or uuid.uuid4()}_invoice_listener"
|
||||||
|
queue: asyncio.Queue[Payment] = asyncio.Queue()
|
||||||
|
return self.create_permanent_task(
|
||||||
|
self._invoice_listener_worker(func, queue),
|
||||||
|
name=name,
|
||||||
|
invoice_queue=queue,
|
||||||
|
)
|
||||||
|
|
||||||
|
async def _heart_beat(self) -> None:
|
||||||
|
"""A heartbeat that removes done tasks logs the number of tasks."""
|
||||||
|
for task in self.tasks:
|
||||||
|
state = task.task._state if task.task else "NOT RUNNING"
|
||||||
|
if settings.task_heart_beat_verbose:
|
||||||
|
logger.debug(
|
||||||
|
f"Task Manager: `{task.name}` state: `{state}` "
|
||||||
|
f"created: {task.created_at.strftime('%Y-%m-%d %H:%M:%S')}`"
|
||||||
|
)
|
||||||
|
if task.task and task.task.done():
|
||||||
|
logger.debug(f"Task Manager: task `{task.name}` is done.")
|
||||||
|
self.cancel_task(task)
|
||||||
|
listeners_count = sum(1 for task in self.tasks if task.invoice_queue)
|
||||||
|
logger.debug(
|
||||||
|
f"Task Manager: {len(self.tasks) - listeners_count} tasks "
|
||||||
|
f"and {listeners_count} invoice listeners."
|
||||||
|
)
|
||||||
|
|
||||||
|
async def _catch_everything_and_restart(
|
||||||
|
self,
|
||||||
|
func: Callable[[], Coroutine],
|
||||||
|
restart_interval: int = 5,
|
||||||
|
) -> None:
|
||||||
|
"""Catches all exceptions from a function and restarts it after 5 seconds."""
|
||||||
|
while settings.lnbits_running:
|
||||||
|
try:
|
||||||
|
return await func()
|
||||||
|
except asyncio.CancelledError:
|
||||||
|
raise # because we must pass this up
|
||||||
|
except Exception as exc:
|
||||||
|
if not settings.lnbits_running:
|
||||||
|
return
|
||||||
|
logger.error(f"exception in background task `{func.__name__}`:", exc)
|
||||||
|
logger.error(traceback.format_exc())
|
||||||
|
logger.info(
|
||||||
|
f"`{func.__name__}` restarts in {restart_interval} seconds."
|
||||||
|
)
|
||||||
|
await asyncio.sleep(restart_interval)
|
||||||
|
|
||||||
|
def _invoice_listener_worker(
|
||||||
|
self, func: Callable[[Payment], Coroutine], queue: asyncio.Queue[Payment]
|
||||||
|
) -> Callable:
|
||||||
|
async def wrapper() -> None:
|
||||||
|
payment: Payment = await queue.get()
|
||||||
|
await func(payment)
|
||||||
|
|
||||||
|
return wrapper
|
||||||
|
|
||||||
|
def _invoice_dispatcher(self, payment: Payment) -> None:
|
||||||
|
"""Dispatches a payment to all registered invoice listeners."""
|
||||||
|
for task in self.tasks:
|
||||||
|
if not task.invoice_queue:
|
||||||
|
continue
|
||||||
|
logger.debug(f"Enqueing payment to task {task.name}")
|
||||||
|
task.invoice_queue.put_nowait(payment)
|
||||||
|
|
||||||
|
async def _invoice_listener_consumer(self) -> None:
|
||||||
|
payment = await self.invoice_queue.get()
|
||||||
|
logger.info(f"got a payment notification {payment.checking_id}")
|
||||||
|
self._invoice_dispatcher(payment)
|
||||||
|
|
||||||
|
async def _internal_invoice_listener_consumer(self) -> None:
|
||||||
|
payment = await self.internal_invoice_queue.get()
|
||||||
|
logger.info(f"got an internal payment notification {payment.checking_id}")
|
||||||
|
self._invoice_dispatcher(payment)
|
||||||
|
|
||||||
|
|
||||||
|
task_manager = TaskManager()
|
||||||
+33
-117
@@ -1,143 +1,83 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import traceback
|
|
||||||
import uuid
|
import uuid
|
||||||
from collections.abc import Callable, Coroutine
|
from collections.abc import Callable, Coroutine
|
||||||
|
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
|
|
||||||
from lnbits.core.models import Payment
|
from lnbits.core.models import Payment
|
||||||
from lnbits.core.services.payments import update_invoice_callback
|
from lnbits.core.services.payments import get_standalone_payment
|
||||||
from lnbits.settings import settings
|
from lnbits.settings import settings
|
||||||
from lnbits.wallets import get_funding_source
|
from lnbits.task_manager import task_manager
|
||||||
|
|
||||||
tasks: list[asyncio.Task] = []
|
|
||||||
unique_tasks: dict[str, asyncio.Task] = {}
|
|
||||||
|
|
||||||
|
|
||||||
|
# DEPRECATED: use task_manager.create_task instead.
|
||||||
def create_task(coro: Coroutine) -> asyncio.Task:
|
def create_task(coro: Coroutine) -> asyncio.Task:
|
||||||
task = asyncio.create_task(coro)
|
logger.debug("DEPRECATED: use task_manager.create_task instead.")
|
||||||
tasks.append(task)
|
return task_manager.create_task(coro).task
|
||||||
return task
|
|
||||||
|
|
||||||
|
|
||||||
|
# DEPRECATED: use task_manager.create_task with `name` kwarg.
|
||||||
def create_unique_task(name: str, coro: Coroutine) -> asyncio.Task:
|
def create_unique_task(name: str, coro: Coroutine) -> asyncio.Task:
|
||||||
if unique_tasks.get(name):
|
logger.debug("DEPRECATED: use task_manager.create_task instead.")
|
||||||
logger.warning(f"task `{name}` already exists, cancelling it")
|
return task_manager.create_task(coro, name=name).task
|
||||||
try:
|
|
||||||
unique_tasks[name].cancel()
|
|
||||||
except Exception as exc:
|
|
||||||
logger.warning(f"error while cancelling task `{name}`: {exc!s}")
|
|
||||||
task = asyncio.create_task(coro)
|
|
||||||
unique_tasks[name] = task
|
|
||||||
return task
|
|
||||||
|
|
||||||
|
|
||||||
|
# DEPRECATED: use task_manager.create_permanent_task instead.
|
||||||
def create_permanent_task(func: Callable[[], Coroutine]) -> asyncio.Task:
|
def create_permanent_task(func: Callable[[], Coroutine]) -> asyncio.Task:
|
||||||
return create_task(catch_everything_and_restart(func))
|
logger.debug("DEPRECATED: use task_manager.create_permanent_task instead.")
|
||||||
|
return task_manager.create_permanent_task(func).task
|
||||||
|
|
||||||
|
|
||||||
|
# DEPRECATED: use task_manager.create_permanent_task with `name` argument instead.
|
||||||
def create_permanent_unique_task(
|
def create_permanent_unique_task(
|
||||||
name: str, coro: Callable[[], Coroutine]
|
name: str, coro: Callable[[], Coroutine]
|
||||||
) -> asyncio.Task:
|
) -> asyncio.Task:
|
||||||
return create_unique_task(name, catch_everything_and_restart(coro, name))
|
return create_unique_task(name, catch_everything_and_restart(coro, name))
|
||||||
|
|
||||||
|
|
||||||
def cancel_all_tasks() -> None:
|
# DEPRECATED don't use this, use task_manager.create_permanent_task instead.
|
||||||
for task in tasks:
|
|
||||||
try:
|
|
||||||
task.cancel()
|
|
||||||
except Exception as exc:
|
|
||||||
logger.warning(f"error while cancelling task: {exc!s}")
|
|
||||||
for name, task in unique_tasks.items():
|
|
||||||
try:
|
|
||||||
task.cancel()
|
|
||||||
except Exception as exc:
|
|
||||||
logger.warning(f"error while cancelling task `{name}`: {exc!s}")
|
|
||||||
|
|
||||||
|
|
||||||
async def catch_everything_and_restart(
|
async def catch_everything_and_restart(
|
||||||
func: Callable[[], Coroutine],
|
func: Callable[[], Coroutine],
|
||||||
name: str = "unnamed",
|
name: str = "unnamed",
|
||||||
) -> Coroutine:
|
) -> None:
|
||||||
try:
|
_ = name
|
||||||
return await func()
|
return await task_manager._catch_everything_and_restart(func)
|
||||||
except asyncio.CancelledError:
|
|
||||||
raise # because we must pass this up
|
|
||||||
except Exception as exc:
|
|
||||||
logger.error(f"exception in background task `{name}`:", exc)
|
|
||||||
logger.error(traceback.format_exc())
|
|
||||||
logger.error("will restart the task in 5 seconds.")
|
|
||||||
await asyncio.sleep(5)
|
|
||||||
return await catch_everything_and_restart(func, name)
|
|
||||||
|
|
||||||
|
|
||||||
invoice_listeners: dict[str, asyncio.Queue] = {}
|
|
||||||
|
|
||||||
|
|
||||||
# TODO: name should not be optional
|
|
||||||
# some extensions still dont use a name, but they should
|
|
||||||
def register_invoice_listener(send_chan: asyncio.Queue, name: str | None = None):
|
def register_invoice_listener(send_chan: asyncio.Queue, name: str | None = None):
|
||||||
"""
|
"""
|
||||||
A method intended for extensions (and core/tasks.py) to call when they want to be
|
DEPRECATED: use task_manager.register_invoice_listener instead,
|
||||||
notified about new invoice payments incoming. Will emit all incoming payments.
|
which also allows to pass a callback instead of a queue.
|
||||||
|
This method will still work but it is not recommended for new code.
|
||||||
"""
|
"""
|
||||||
if not name:
|
logger.debug("DEPRECATED: use task_manager.register_invoice_listener instead.")
|
||||||
# fallback to a random name if extension didn't provide one
|
name = f"forward_{name or str(uuid.uuid4())[:8]}"
|
||||||
name = f"no_name_{str(uuid.uuid4())[:8]}"
|
|
||||||
|
|
||||||
if invoice_listeners.get(name):
|
# here we just forwarding the payments to the provided queue
|
||||||
logger.warning(f"invoice listener `{name}` already exists, replacing it")
|
async def forward_queue(payment: Payment):
|
||||||
|
send_chan.put_nowait(payment)
|
||||||
|
|
||||||
logger.trace(f"registering invoice listener `{name}`")
|
task_manager.register_invoice_listener(forward_queue, name=name)
|
||||||
invoice_listeners[name] = send_chan
|
|
||||||
|
|
||||||
|
|
||||||
internal_invoice_queue: asyncio.Queue = asyncio.Queue(0)
|
|
||||||
|
|
||||||
|
|
||||||
async def internal_invoice_queue_put(checking_id: str) -> None:
|
async def internal_invoice_queue_put(checking_id: str) -> None:
|
||||||
"""
|
"""
|
||||||
|
DEPRECATED: use task_manager.internal_invoice_queue instead,
|
||||||
A method to call when it wants to notify about an internal invoice payment.
|
A method to call when it wants to notify about an internal invoice payment.
|
||||||
"""
|
"""
|
||||||
await internal_invoice_queue.put(checking_id)
|
payment = await get_standalone_payment(checking_id, incoming=True)
|
||||||
|
if not payment:
|
||||||
|
logger.warning(f"internal_invoice_queue_put: payment {checking_id} not found")
|
||||||
async def internal_invoice_listener() -> None:
|
return
|
||||||
"""
|
await task_manager.internal_invoice_queue.put(payment)
|
||||||
internal_invoice_queue will be filled directly in core/services.py
|
|
||||||
after the payment was deemed to be settled internally.
|
|
||||||
|
|
||||||
Called by the app startup sequence.
|
|
||||||
"""
|
|
||||||
while settings.lnbits_running:
|
|
||||||
checking_id = await internal_invoice_queue.get()
|
|
||||||
logger.info(f"got an internal payment notification {checking_id}")
|
|
||||||
payment = await update_invoice_callback(checking_id)
|
|
||||||
if payment:
|
|
||||||
logger.success(f"internal invoice {checking_id} settled")
|
|
||||||
await invoice_callback_dispatcher(payment)
|
|
||||||
|
|
||||||
|
|
||||||
async def invoice_listener() -> None:
|
|
||||||
"""
|
|
||||||
invoice_listener will collect all invoices that come directly
|
|
||||||
from the backend wallet.
|
|
||||||
|
|
||||||
Called by the app startup sequence.
|
|
||||||
"""
|
|
||||||
funding_source = get_funding_source()
|
|
||||||
async for checking_id in funding_source.paid_invoices_stream():
|
|
||||||
logger.info(f"got a payment notification {checking_id}")
|
|
||||||
payment = await update_invoice_callback(checking_id)
|
|
||||||
if payment:
|
|
||||||
logger.success(f"fundingsource invoice {checking_id} settled")
|
|
||||||
await invoice_callback_dispatcher(payment)
|
|
||||||
|
|
||||||
|
|
||||||
|
# DEPRECATED use task_manager.register_invoice_listener(coro, name="myext")
|
||||||
def wait_for_paid_invoices(
|
def wait_for_paid_invoices(
|
||||||
invoice_listener_name: str,
|
invoice_listener_name: str,
|
||||||
func: Callable[[Payment], Coroutine],
|
func: Callable[[Payment], Coroutine],
|
||||||
) -> Callable[[], Coroutine]:
|
) -> Callable[[], Coroutine]:
|
||||||
|
logger.debug("DEPRECATED: use task_manager.register_invoice_listener instead.")
|
||||||
|
|
||||||
async def wrapper() -> None:
|
async def wrapper() -> None:
|
||||||
invoice_queue: asyncio.Queue = asyncio.Queue()
|
invoice_queue: asyncio.Queue = asyncio.Queue()
|
||||||
@@ -147,27 +87,3 @@ def wait_for_paid_invoices(
|
|||||||
await func(payment)
|
await func(payment)
|
||||||
|
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
def run_interval(
|
|
||||||
interval_seconds: int,
|
|
||||||
func: Callable[[], Coroutine],
|
|
||||||
) -> Callable[[], Coroutine]:
|
|
||||||
"""Run a function at a specified interval in seconds, while the server is running"""
|
|
||||||
|
|
||||||
async def wrapper() -> None:
|
|
||||||
while settings.lnbits_running:
|
|
||||||
try:
|
|
||||||
await func()
|
|
||||||
except Exception as e:
|
|
||||||
logger.error(f"Error occurred in interval task: {e}")
|
|
||||||
logger.warning(traceback.format_exc())
|
|
||||||
await asyncio.sleep(interval_seconds)
|
|
||||||
|
|
||||||
return wrapper
|
|
||||||
|
|
||||||
|
|
||||||
async def invoice_callback_dispatcher(payment: Payment):
|
|
||||||
for name, send_chan in invoice_listeners.items():
|
|
||||||
logger.trace(f"invoice listeners: sending to `{name}`")
|
|
||||||
await send_chan.put(payment)
|
|
||||||
|
|||||||
@@ -90,9 +90,7 @@
|
|||||||
window.g.isPublicPage = false
|
window.g.isPublicPage = false
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</script>
|
</script>
|
||||||
{% endif %} {% for url in INCLUDED_EXTENSION_I18N %}
|
{% endif %}
|
||||||
<script src="{{ url }}"></script>
|
|
||||||
{% endfor %}
|
|
||||||
<!-- app init -->
|
<!-- app init -->
|
||||||
<script>
|
<script>
|
||||||
window.app = Vue.createApp({
|
window.app = Vue.createApp({
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
{% include('components/admin/funding.vue') %} {%
|
{% include('components/admin/funding_seed_backup.vue') %} {%
|
||||||
|
include('components/admin/funding.vue') %} {%
|
||||||
include('components/admin/funding_sources.vue') %} {%
|
include('components/admin/funding_sources.vue') %} {%
|
||||||
include('components/admin/fiat_providers.vue') %} {%
|
include('components/admin/fiat_providers.vue') %} {%
|
||||||
include('components/admin/exchange_providers.vue') %} {%
|
include('components/admin/exchange_providers.vue') %} {%
|
||||||
|
|||||||
@@ -1,7 +1,46 @@
|
|||||||
<template id="lnbits-admin-exchange-providers">
|
<template id="lnbits-admin-exchange-providers">
|
||||||
<h6 class="q-my-none q-mb-sm">
|
<h6 class="q-my-none q-mb-xs">LNbits Price Aggregator</h6>
|
||||||
<span v-text="$t('exchange_providers')"></span>
|
<p class="q-mb-md text-caption text-grey">
|
||||||
</h6>
|
A privacy-friendly, open-source Bitcoin price aggregator maintained by the
|
||||||
|
LNbits team. Aggregates prices from multiple exchanges and returns a median,
|
||||||
|
no API keys required.
|
||||||
|
<a href="https://price.lnbits.com" target="_blank" rel="noopener"
|
||||||
|
>price.lnbits.com</a
|
||||||
|
>
|
||||||
|
—
|
||||||
|
<a
|
||||||
|
href="https://github.com/lnbits/lnbits-price-aggregator"
|
||||||
|
target="_blank"
|
||||||
|
rel="noopener"
|
||||||
|
>GitHub</a
|
||||||
|
>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<div class="row q-mb-md items-start">
|
||||||
|
<div class="col-auto q-mr-md q-mt-sm">
|
||||||
|
<q-toggle
|
||||||
|
v-model="formData.lnbits_price_aggregator_enabled"
|
||||||
|
@update:model-value="formData.touch = null"
|
||||||
|
label="Use Price Aggregator"
|
||||||
|
>
|
||||||
|
</q-toggle>
|
||||||
|
</div>
|
||||||
|
<div class="col-12 col-md-7">
|
||||||
|
<q-input
|
||||||
|
filled
|
||||||
|
v-model="formData.lnbits_price_aggregator_url"
|
||||||
|
type="text"
|
||||||
|
label="Price Aggregator URL"
|
||||||
|
hint="Fetch BTC price from this aggregator instead of individual providers below."
|
||||||
|
:disable="!formData.lnbits_price_aggregator_enabled"
|
||||||
|
@update:model-value="formData.touch = null"
|
||||||
|
>
|
||||||
|
</q-input>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<q-separator class="q-my-md"></q-separator>
|
||||||
|
<h6 class="q-my-none q-mb-sm">Bitcoin Price History</h6>
|
||||||
|
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-12 col-md-8">
|
<div class="col-12 col-md-8">
|
||||||
@@ -53,6 +92,11 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<q-separator class="q-my-md"></q-separator>
|
||||||
|
<h6 class="q-my-none q-mb-sm">
|
||||||
|
<span v-text="$t('exchange_providers')"></span>
|
||||||
|
</h6>
|
||||||
|
|
||||||
<div class="row q-mt-md">
|
<div class="row q-mt-md">
|
||||||
<div class="col-6">
|
<div class="col-6">
|
||||||
<q-btn
|
<q-btn
|
||||||
@@ -60,6 +104,7 @@
|
|||||||
label="Add Exchange Provider"
|
label="Add Exchange Provider"
|
||||||
color="primary"
|
color="primary"
|
||||||
class="q-mb-md"
|
class="q-mb-md"
|
||||||
|
:disable="formData.lnbits_price_aggregator_enabled"
|
||||||
>
|
>
|
||||||
</q-btn>
|
</q-btn>
|
||||||
</div>
|
</div>
|
||||||
@@ -70,12 +115,20 @@
|
|||||||
:label="$t('reset_defaults')"
|
:label="$t('reset_defaults')"
|
||||||
color="primary"
|
color="primary"
|
||||||
class="float-right"
|
class="float-right"
|
||||||
|
:disable="formData.lnbits_price_aggregator_enabled"
|
||||||
>
|
>
|
||||||
</q-btn>
|
</q-btn>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="overflow-auto">
|
<div
|
||||||
|
class="overflow-auto"
|
||||||
|
:style="
|
||||||
|
formData.lnbits_price_aggregator_enabled
|
||||||
|
? 'opacity:0.4;pointer-events:none'
|
||||||
|
: ''
|
||||||
|
"
|
||||||
|
>
|
||||||
<q-table
|
<q-table
|
||||||
row-key="name"
|
row-key="name"
|
||||||
:rows="formData.lnbits_exchange_rate_providers"
|
:rows="formData.lnbits_exchange_rate_providers"
|
||||||
|
|||||||
@@ -301,5 +301,11 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<lnbits-admin-funding-seed-backup
|
||||||
|
:active="active"
|
||||||
|
:is-super-user="isSuperUser"
|
||||||
|
:form-data="formData"
|
||||||
|
:settings="settings"
|
||||||
|
></lnbits-admin-funding-seed-backup>
|
||||||
</q-card-section>
|
</q-card-section>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
@@ -0,0 +1,136 @@
|
|||||||
|
<template id="lnbits-admin-funding-seed-backup">
|
||||||
|
<q-dialog v-model="dialog.show">
|
||||||
|
<q-card style="width: 760px; max-width: 95vw; border-radius: 8px">
|
||||||
|
<q-card-section class="q-pb-md">
|
||||||
|
<div class="row q-col-gutter-sm">
|
||||||
|
<div class="col-6">
|
||||||
|
<q-chip
|
||||||
|
square
|
||||||
|
class="full-width"
|
||||||
|
icon="looks_one"
|
||||||
|
:color="dialog.step === 1 ? 'primary' : 'grey-9'"
|
||||||
|
text-color="white"
|
||||||
|
label="Backup"
|
||||||
|
></q-chip>
|
||||||
|
</div>
|
||||||
|
<div class="col-6">
|
||||||
|
<q-chip
|
||||||
|
square
|
||||||
|
class="full-width"
|
||||||
|
icon="looks_two"
|
||||||
|
:color="dialog.step === 2 ? 'primary' : 'grey-9'"
|
||||||
|
text-color="white"
|
||||||
|
label="Verify"
|
||||||
|
></q-chip>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</q-card-section>
|
||||||
|
|
||||||
|
<q-separator></q-separator>
|
||||||
|
|
||||||
|
<q-card-section v-if="dialog.step === 1">
|
||||||
|
<div class="row items-center justify-between q-mb-md">
|
||||||
|
<div>
|
||||||
|
<div
|
||||||
|
class="text-subtitle1"
|
||||||
|
v-text="`${seedWords.length}-word recovery phrase`"
|
||||||
|
></div>
|
||||||
|
<div
|
||||||
|
class="text-caption text-grey-5"
|
||||||
|
v-text="'Write these words down in order.'"
|
||||||
|
></div>
|
||||||
|
</div>
|
||||||
|
<q-btn
|
||||||
|
outline
|
||||||
|
no-caps
|
||||||
|
color="primary"
|
||||||
|
:icon="dialog.visible ? 'visibility_off' : 'visibility'"
|
||||||
|
:label="dialog.visible ? 'Hide words' : 'Show words'"
|
||||||
|
@click="dialog.visible = !dialog.visible"
|
||||||
|
></q-btn>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="row q-col-gutter-sm">
|
||||||
|
<div
|
||||||
|
class="col-4 col-md-3"
|
||||||
|
v-for="word in seedWords"
|
||||||
|
:key="word.index"
|
||||||
|
>
|
||||||
|
<div
|
||||||
|
class="row items-center no-wrap rounded-borders"
|
||||||
|
style="
|
||||||
|
min-height: 42px;
|
||||||
|
border: 1px solid rgba(255, 255, 255, 0.14);
|
||||||
|
background: rgba(255, 255, 255, 0.035);
|
||||||
|
"
|
||||||
|
>
|
||||||
|
<div
|
||||||
|
class="text-caption text-grey-5 text-center"
|
||||||
|
style="
|
||||||
|
width: 42px;
|
||||||
|
border-right: 1px solid rgba(255, 255, 255, 0.1);
|
||||||
|
"
|
||||||
|
v-text="word.index + 1"
|
||||||
|
></div>
|
||||||
|
<div
|
||||||
|
class="text-body2 text-weight-medium q-px-sm"
|
||||||
|
style="min-width: 0; overflow-wrap: anywhere"
|
||||||
|
v-text="dialog.visible ? word.word : '••••••'"
|
||||||
|
></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="row justify-end q-mt-lg">
|
||||||
|
<q-btn
|
||||||
|
color="primary"
|
||||||
|
no-caps
|
||||||
|
label="I have written it down"
|
||||||
|
@click="prepareChallenge"
|
||||||
|
></q-btn>
|
||||||
|
</div>
|
||||||
|
</q-card-section>
|
||||||
|
|
||||||
|
<q-card-section v-if="dialog.step === 2">
|
||||||
|
<div class="q-mb-md">
|
||||||
|
<div class="text-subtitle1" v-text="'Confirm your backup'"></div>
|
||||||
|
<div
|
||||||
|
class="text-caption text-grey-5"
|
||||||
|
v-text="
|
||||||
|
'Enter the requested words from your written recovery phrase.'
|
||||||
|
"
|
||||||
|
></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="row q-col-gutter-md">
|
||||||
|
<div
|
||||||
|
class="col-12 col-sm-6"
|
||||||
|
v-for="word in dialog.challenge"
|
||||||
|
:key="word.index"
|
||||||
|
>
|
||||||
|
<q-input
|
||||||
|
v-model.trim="dialog.answers[word.index]"
|
||||||
|
filled
|
||||||
|
:label="`Word ${word.index + 1}`"
|
||||||
|
></q-input>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div
|
||||||
|
class="text-negative q-mt-sm"
|
||||||
|
v-if="dialog.error"
|
||||||
|
v-text="dialog.error"
|
||||||
|
></div>
|
||||||
|
<div class="row justify-between q-mt-lg">
|
||||||
|
<q-btn flat no-caps label="Back" @click="dialog.step = 1"></q-btn>
|
||||||
|
<q-btn
|
||||||
|
color="primary"
|
||||||
|
icon="check"
|
||||||
|
no-caps
|
||||||
|
label="Confirm backup"
|
||||||
|
@click="submitChallenge"
|
||||||
|
></q-btn>
|
||||||
|
</div>
|
||||||
|
</q-card-section>
|
||||||
|
</q-card>
|
||||||
|
</q-dialog>
|
||||||
|
</template>
|
||||||
@@ -6,6 +6,9 @@
|
|||||||
:content-inset-level="0.5"
|
:content-inset-level="0.5"
|
||||||
>
|
>
|
||||||
<q-card-section>
|
<q-card-section>
|
||||||
|
<q-banner dense rounded class="bg-warning text-black q-mb-md">
|
||||||
|
These keys should be kept safe, sharing them could risk losing funds.
|
||||||
|
</q-banner>
|
||||||
<q-list>
|
<q-list>
|
||||||
<q-item dense class="q-pa-none">
|
<q-item dense class="q-pa-none">
|
||||||
<q-item-section>
|
<q-item-section>
|
||||||
|
|||||||
@@ -889,6 +889,17 @@
|
|||||||
></q-btn>
|
></q-btn>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<div
|
||||||
|
v-if="selectedApiToken && selectedApiToken.expires_at"
|
||||||
|
class="row items-center q-mb-md q-gutter-sm"
|
||||||
|
>
|
||||||
|
<span v-text="expiryAt"></span>
|
||||||
|
<span v-text="$t('status') + ':'"></span>
|
||||||
|
<q-badge
|
||||||
|
:color="tokenStatus.badgeColor"
|
||||||
|
:label="tokenStatus.status"
|
||||||
|
></q-badge>
|
||||||
|
</div>
|
||||||
<div v-if="apiAcl.apiToken" class="row q-mb-md">
|
<div v-if="apiAcl.apiToken" class="row q-mb-md">
|
||||||
<div class="col-12">
|
<div class="col-12">
|
||||||
<q-badge>
|
<q-badge>
|
||||||
|
|||||||
@@ -199,6 +199,7 @@
|
|||||||
>
|
>
|
||||||
<q-tab-panel name="funding">
|
<q-tab-panel name="funding">
|
||||||
<lnbits-admin-funding
|
<lnbits-admin-funding
|
||||||
|
:active="tab === 'funding'"
|
||||||
:is-super-user="isSuperUser"
|
:is-super-user="isSuperUser"
|
||||||
:settings="settings"
|
:settings="settings"
|
||||||
:form-data="formData"
|
:form-data="formData"
|
||||||
|
|||||||
@@ -7,7 +7,29 @@
|
|||||||
<q-tabs v-model="tab" active-color="primary" align="left">
|
<q-tabs v-model="tab" active-color="primary" align="left">
|
||||||
<q-tab name="installed" :label="$t('installed')"></q-tab>
|
<q-tab name="installed" :label="$t('installed')"></q-tab>
|
||||||
<q-tab name="all" :label="$t('all')"></q-tab>
|
<q-tab name="all" :label="$t('all')"></q-tab>
|
||||||
<q-tab name="featured" :label="$t('featured')"></q-tab>
|
<q-tab
|
||||||
|
v-show="$q.screen.gt.xs"
|
||||||
|
name="featured"
|
||||||
|
:label="$t('featured')"
|
||||||
|
></q-tab>
|
||||||
|
<q-btn-dropdown auto-close stretch flat :label="$t('categories')">
|
||||||
|
<q-list>
|
||||||
|
<q-item
|
||||||
|
clickable
|
||||||
|
v-close-popup
|
||||||
|
v-for="category in categories"
|
||||||
|
@click="tab = category"
|
||||||
|
:key="category"
|
||||||
|
>
|
||||||
|
<q-item-section>
|
||||||
|
<q-item-label
|
||||||
|
class="text-capitalize"
|
||||||
|
v-text="category"
|
||||||
|
></q-item-label>
|
||||||
|
</q-item-section>
|
||||||
|
</q-item>
|
||||||
|
</q-list>
|
||||||
|
</q-btn-dropdown>
|
||||||
<i
|
<i
|
||||||
v-if="!g.user.admin && tab != 'installed'"
|
v-if="!g.user.admin && tab != 'installed'"
|
||||||
v-text="$t('only_admins_can_install')"
|
v-text="$t('only_admins_can_install')"
|
||||||
|
|||||||
@@ -136,7 +136,7 @@
|
|||||||
</q-card>
|
</q-card>
|
||||||
</div>
|
</div>
|
||||||
<div
|
<div
|
||||||
v-show="chartData.showPaymentStatus"
|
v-show="chartData.showPaymentTags"
|
||||||
class="col-lg-3 col-md-6 col-sm-12 text-center"
|
class="col-lg-3 col-md-6 col-sm-12 text-center"
|
||||||
>
|
>
|
||||||
<q-card class="q-pt-sm">
|
<q-card class="q-pt-sm">
|
||||||
|
|||||||
+6
-17
@@ -1,13 +1,8 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import asyncio
|
|
||||||
from time import time
|
from time import time
|
||||||
from typing import Any, NamedTuple
|
from typing import Any, NamedTuple
|
||||||
|
|
||||||
from loguru import logger
|
|
||||||
|
|
||||||
from lnbits.settings import settings
|
|
||||||
|
|
||||||
|
|
||||||
class Cached(NamedTuple):
|
class Cached(NamedTuple):
|
||||||
value: Any
|
value: Any
|
||||||
@@ -22,8 +17,7 @@ class Cache:
|
|||||||
Small caching utility providing simple get/set interface (very much like redis)
|
Small caching utility providing simple get/set interface (very much like redis)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, interval: float = 10) -> None:
|
def __init__(self) -> None:
|
||||||
self.interval = interval
|
|
||||||
self._values: dict[Any, Cached] = {}
|
self._values: dict[Any, Cached] = {}
|
||||||
|
|
||||||
def value(self, key: str) -> Cached | None:
|
def value(self, key: str) -> Cached | None:
|
||||||
@@ -59,16 +53,11 @@ class Cache:
|
|||||||
self.set(key, value, expiry=expiry)
|
self.set(key, value, expiry=expiry)
|
||||||
return value
|
return value
|
||||||
|
|
||||||
async def invalidate_forever(self):
|
async def invalidate_cache(self):
|
||||||
while settings.lnbits_running:
|
ts = time()
|
||||||
try:
|
expired = [k for k, v in self._values.items() if v.expiry < ts]
|
||||||
await asyncio.sleep(self.interval)
|
for k in expired:
|
||||||
ts = time()
|
self._values.pop(k)
|
||||||
expired = [k for k, v in self._values.items() if v.expiry < ts]
|
|
||||||
for k in expired:
|
|
||||||
self._values.pop(k)
|
|
||||||
except Exception:
|
|
||||||
logger.error("Error invalidating cache")
|
|
||||||
|
|
||||||
|
|
||||||
cache = Cache()
|
cache = Cache()
|
||||||
|
|||||||
@@ -0,0 +1,439 @@
|
|||||||
|
"""
|
||||||
|
Electrum protocol client (https://github.com/spesmilo/electrum-protocol).
|
||||||
|
|
||||||
|
JSON-RPC 2.0 over TCP / SSL (newline-delimited), with request/response
|
||||||
|
correlation, subscription dispatch, and automatic keepalive pings.
|
||||||
|
server.version is sent automatically on connect as required by the spec.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
import hashlib
|
||||||
|
import itertools
|
||||||
|
import json
|
||||||
|
import ssl
|
||||||
|
from collections.abc import Callable
|
||||||
|
from typing import Any
|
||||||
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
|
from loguru import logger
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
||||||
|
class ElectrumError(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def scripthash_from_scriptpubkey(scriptpubkey: bytes) -> str:
|
||||||
|
"""Electrum script hash: SHA-256 of scriptPubKey, byte-reversed to hex."""
|
||||||
|
return hashlib.sha256(scriptpubkey).digest()[::-1].hex()
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Response models
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
class Balance(BaseModel):
|
||||||
|
confirmed: int
|
||||||
|
unconfirmed: int
|
||||||
|
|
||||||
|
|
||||||
|
class HistoryEntry(BaseModel):
|
||||||
|
tx_hash: str
|
||||||
|
height: int
|
||||||
|
fee: int | None = None # present for mempool entries
|
||||||
|
|
||||||
|
|
||||||
|
class MempoolEntry(BaseModel):
|
||||||
|
tx_hash: str
|
||||||
|
height: int
|
||||||
|
fee: int
|
||||||
|
|
||||||
|
|
||||||
|
class UTXO(BaseModel):
|
||||||
|
tx_hash: str
|
||||||
|
tx_pos: int
|
||||||
|
height: int
|
||||||
|
value: int # satoshis
|
||||||
|
|
||||||
|
|
||||||
|
class BlockHeader(BaseModel):
|
||||||
|
height: int
|
||||||
|
hex: str
|
||||||
|
|
||||||
|
|
||||||
|
class BlockHeaderProof(BaseModel):
|
||||||
|
"""Returned by get_block_header when cp_height > 0."""
|
||||||
|
|
||||||
|
branch: list[str]
|
||||||
|
header: str
|
||||||
|
root: str
|
||||||
|
|
||||||
|
|
||||||
|
class BlockHeaders(BaseModel):
|
||||||
|
count: int
|
||||||
|
hex: str
|
||||||
|
max: int
|
||||||
|
|
||||||
|
|
||||||
|
class MerkleProof(BaseModel):
|
||||||
|
block_height: int
|
||||||
|
merkle: list[str]
|
||||||
|
pos: int
|
||||||
|
|
||||||
|
|
||||||
|
class TxIdWithMerkle(BaseModel):
|
||||||
|
tx_hash: str
|
||||||
|
merkle: list[str]
|
||||||
|
|
||||||
|
|
||||||
|
class FeeHistogramEntry(BaseModel):
|
||||||
|
fee_rate: float
|
||||||
|
vsize: float
|
||||||
|
|
||||||
|
|
||||||
|
class ServerFeatures(BaseModel):
|
||||||
|
class Config:
|
||||||
|
extra = "allow"
|
||||||
|
|
||||||
|
genesis_hash: str = ""
|
||||||
|
protocol_max: str = ""
|
||||||
|
protocol_min: str = ""
|
||||||
|
server_version: str = ""
|
||||||
|
pruning: int | None = None
|
||||||
|
hash_function: str = "sha256d"
|
||||||
|
hosts: dict[str, Any] = {}
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Client
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
class ElectrumClient:
|
||||||
|
"""
|
||||||
|
Async Electrum protocol client over plain TCP or SSL.
|
||||||
|
|
||||||
|
Messages are newline-terminated JSON-RPC 2.0, as required by the spec.
|
||||||
|
Handles request/response correlation by id, routes push notifications to
|
||||||
|
registered callbacks, and sends periodic pings to keep the connection alive.
|
||||||
|
|
||||||
|
Usage::
|
||||||
|
|
||||||
|
# Plain TCP
|
||||||
|
async with ElectrumClient("tcp://blockstream.info:110") as client:
|
||||||
|
height = await client.get_height()
|
||||||
|
|
||||||
|
# SSL
|
||||||
|
async with ElectrumClient("ssl://electrum.blockstream.info:50002") as c:
|
||||||
|
height = await c.get_height()
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
url: str,
|
||||||
|
client_name: str = "lnbits",
|
||||||
|
protocol_version: str = "1.4",
|
||||||
|
ping_interval: float = 60.0,
|
||||||
|
) -> None:
|
||||||
|
parsed = urlparse(url)
|
||||||
|
self.host = parsed.hostname or ""
|
||||||
|
self.port = parsed.port or (
|
||||||
|
50002 if parsed.scheme in ("ssl", "https") else 50001
|
||||||
|
)
|
||||||
|
self.use_ssl = parsed.scheme in ("ssl", "https")
|
||||||
|
self.client_name = client_name
|
||||||
|
self.protocol_version = protocol_version
|
||||||
|
self.ping_interval = ping_interval
|
||||||
|
self._counter = itertools.count(1)
|
||||||
|
self._pending: dict[int, asyncio.Future[Any]] = {}
|
||||||
|
self._subscriptions: dict[str, list[Callable[[list[Any]], Any]]] = {}
|
||||||
|
self._recv_task: asyncio.Task[None] | None = None
|
||||||
|
self._ping_task: asyncio.Task[None] | None = None
|
||||||
|
self._reader: asyncio.StreamReader | None = None
|
||||||
|
self._writer: asyncio.StreamWriter | None = None
|
||||||
|
self.server_version: str = ""
|
||||||
|
self.negotiated_protocol: str = ""
|
||||||
|
|
||||||
|
async def connect(self, timeout: float = 10.0) -> None:
|
||||||
|
ssl_ctx: ssl.SSLContext | None = None
|
||||||
|
if self.use_ssl:
|
||||||
|
ssl_ctx = ssl.create_default_context()
|
||||||
|
self._reader, self._writer = await asyncio.wait_for(
|
||||||
|
asyncio.open_connection(
|
||||||
|
self.host, self.port, ssl=ssl_ctx, limit=4 * 1024 * 1024
|
||||||
|
),
|
||||||
|
timeout=timeout,
|
||||||
|
)
|
||||||
|
self._recv_task = asyncio.create_task(self._recv_loop())
|
||||||
|
result = await self._call(
|
||||||
|
"server.version", [self.client_name, self.protocol_version], timeout=timeout
|
||||||
|
)
|
||||||
|
self.server_version, self.negotiated_protocol = result[0], result[1]
|
||||||
|
logger.debug(
|
||||||
|
f"Electrum connected: server={self.server_version}"
|
||||||
|
f" protocol={self.negotiated_protocol}"
|
||||||
|
)
|
||||||
|
if self.ping_interval > 0:
|
||||||
|
self._ping_task = asyncio.create_task(self._ping_loop())
|
||||||
|
|
||||||
|
async def close(self) -> None:
|
||||||
|
for task in (self._ping_task, self._recv_task):
|
||||||
|
if task:
|
||||||
|
task.cancel()
|
||||||
|
try:
|
||||||
|
await task
|
||||||
|
except asyncio.CancelledError:
|
||||||
|
pass
|
||||||
|
except Exception:
|
||||||
|
logger.debug("Electrum: error while cancelling task")
|
||||||
|
self._ping_task = None
|
||||||
|
self._recv_task = None
|
||||||
|
if self._writer:
|
||||||
|
self._writer.close()
|
||||||
|
try:
|
||||||
|
await asyncio.wait_for(self._writer.wait_closed(), timeout=5.0)
|
||||||
|
except Exception:
|
||||||
|
logger.debug("Electrum: error while closing writer")
|
||||||
|
self._reader = None
|
||||||
|
self._writer = None
|
||||||
|
|
||||||
|
async def __aenter__(self) -> "ElectrumClient":
|
||||||
|
try:
|
||||||
|
await self.connect()
|
||||||
|
except BaseException:
|
||||||
|
await self.close()
|
||||||
|
raise
|
||||||
|
return self
|
||||||
|
|
||||||
|
async def __aexit__(self, *_: Any) -> None:
|
||||||
|
await self.close()
|
||||||
|
|
||||||
|
# ---- internal plumbing ----
|
||||||
|
|
||||||
|
async def _call(
|
||||||
|
self,
|
||||||
|
method: str,
|
||||||
|
params: list[Any] | dict[str, Any] | None = None,
|
||||||
|
timeout: float = 30.0,
|
||||||
|
) -> Any:
|
||||||
|
if not self._writer:
|
||||||
|
raise ElectrumError("Not connected")
|
||||||
|
req_id = next(self._counter)
|
||||||
|
fut: asyncio.Future[Any] = asyncio.get_running_loop().create_future()
|
||||||
|
self._pending[req_id] = fut
|
||||||
|
self._writer.write(
|
||||||
|
json.dumps(
|
||||||
|
{
|
||||||
|
"jsonrpc": "2.0",
|
||||||
|
"id": req_id,
|
||||||
|
"method": method,
|
||||||
|
"params": params if params is not None else [],
|
||||||
|
}
|
||||||
|
).encode()
|
||||||
|
+ b"\n"
|
||||||
|
)
|
||||||
|
await self._writer.drain()
|
||||||
|
try:
|
||||||
|
return await asyncio.wait_for(asyncio.shield(fut), timeout=timeout)
|
||||||
|
except asyncio.TimeoutError as exc:
|
||||||
|
self._pending.pop(req_id, None)
|
||||||
|
raise ElectrumError(f"Timeout waiting for response to {method!r}") from exc
|
||||||
|
|
||||||
|
def _dispatch(self, msg: dict[str, Any]) -> None:
|
||||||
|
msg_id = msg.get("id")
|
||||||
|
if msg_id is not None:
|
||||||
|
fut = self._pending.pop(msg_id, None)
|
||||||
|
if fut and not fut.done():
|
||||||
|
err = msg.get("error")
|
||||||
|
if err:
|
||||||
|
fut.set_exception(ElectrumError(err))
|
||||||
|
else:
|
||||||
|
fut.set_result(msg.get("result"))
|
||||||
|
else:
|
||||||
|
method = msg.get("method", "")
|
||||||
|
params = msg.get("params", [])
|
||||||
|
for cb in list(self._subscriptions.get(method, [])):
|
||||||
|
try:
|
||||||
|
result = cb(params)
|
||||||
|
if asyncio.iscoroutine(result):
|
||||||
|
self._bg_tasks.add(asyncio.create_task(result))
|
||||||
|
except Exception:
|
||||||
|
logger.exception(f"Electrum: callback error for {method!r}")
|
||||||
|
|
||||||
|
async def _recv_loop(self) -> None:
|
||||||
|
assert self._reader
|
||||||
|
self._bg_tasks: set[asyncio.Task[Any]] = set()
|
||||||
|
buf = b""
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
chunk = await self._reader.read(65536)
|
||||||
|
if not chunk:
|
||||||
|
break
|
||||||
|
buf += chunk
|
||||||
|
while b"\n" in buf:
|
||||||
|
line, buf = buf.split(b"\n", 1)
|
||||||
|
if not line.strip():
|
||||||
|
continue
|
||||||
|
try:
|
||||||
|
msg: dict[str, Any] = json.loads(line)
|
||||||
|
except json.JSONDecodeError:
|
||||||
|
logger.warning(f"Electrum: invalid JSON: {line!r}")
|
||||||
|
continue
|
||||||
|
self._dispatch(msg)
|
||||||
|
except asyncio.CancelledError:
|
||||||
|
pass
|
||||||
|
except Exception:
|
||||||
|
logger.exception("Electrum: recv loop error")
|
||||||
|
finally:
|
||||||
|
for fut in self._pending.values():
|
||||||
|
if not fut.done():
|
||||||
|
fut.set_exception(ElectrumError("Connection closed"))
|
||||||
|
self._pending.clear()
|
||||||
|
|
||||||
|
async def _ping_loop(self) -> None:
|
||||||
|
try:
|
||||||
|
while True:
|
||||||
|
await asyncio.sleep(self.ping_interval)
|
||||||
|
await self._call("server.ping")
|
||||||
|
except asyncio.CancelledError:
|
||||||
|
pass
|
||||||
|
except Exception:
|
||||||
|
logger.exception("Electrum: ping loop error")
|
||||||
|
|
||||||
|
# ---- subscription management ----
|
||||||
|
|
||||||
|
def on(self, method: str, callback: Callable[[list[Any]], Any]) -> None:
|
||||||
|
"""Register a notification callback for a subscription method."""
|
||||||
|
self._subscriptions.setdefault(method, []).append(callback)
|
||||||
|
|
||||||
|
def off(self, method: str, callback: Callable[[list[Any]], Any]) -> None:
|
||||||
|
"""Remove a previously registered notification callback."""
|
||||||
|
cbs = self._subscriptions.get(method)
|
||||||
|
if cbs and callback in cbs:
|
||||||
|
cbs.remove(callback)
|
||||||
|
|
||||||
|
# ---- server methods ----
|
||||||
|
|
||||||
|
async def server_ping(self) -> None:
|
||||||
|
await self._call("server.ping")
|
||||||
|
|
||||||
|
async def server_banner(self) -> str:
|
||||||
|
return await self._call("server.banner")
|
||||||
|
|
||||||
|
async def server_features(self) -> ServerFeatures:
|
||||||
|
data = await self._call("server.features")
|
||||||
|
return ServerFeatures.parse_obj(data)
|
||||||
|
|
||||||
|
async def server_peers(self) -> list[Any]:
|
||||||
|
return await self._call("server.peers.subscribe")
|
||||||
|
|
||||||
|
# ---- scripthash methods ----
|
||||||
|
|
||||||
|
async def get_balance(self, scripthash: str) -> Balance:
|
||||||
|
data = await self._call("blockchain.scripthash.get_balance", [scripthash])
|
||||||
|
return Balance.parse_obj(data)
|
||||||
|
|
||||||
|
async def get_history(self, scripthash: str) -> list[HistoryEntry]:
|
||||||
|
data = await self._call("blockchain.scripthash.get_history", [scripthash])
|
||||||
|
return [HistoryEntry.parse_obj(e) for e in data]
|
||||||
|
|
||||||
|
async def get_mempool(self, scripthash: str) -> list[MempoolEntry]:
|
||||||
|
data = await self._call("blockchain.scripthash.get_mempool", [scripthash])
|
||||||
|
return [MempoolEntry.parse_obj(e) for e in data]
|
||||||
|
|
||||||
|
async def listunspent(self, scripthash: str) -> list[UTXO]:
|
||||||
|
data = await self._call("blockchain.scripthash.listunspent", [scripthash])
|
||||||
|
return [UTXO.parse_obj(e) for e in data]
|
||||||
|
|
||||||
|
async def subscribe_scripthash(
|
||||||
|
self,
|
||||||
|
scripthash: str,
|
||||||
|
callback: Callable[[list[Any]], Any] | None = None,
|
||||||
|
) -> str | None:
|
||||||
|
"""Subscribe to status changes; returns current status hash or None."""
|
||||||
|
if callback:
|
||||||
|
self.on("blockchain.scripthash.subscribe", callback)
|
||||||
|
return await self._call("blockchain.scripthash.subscribe", [scripthash])
|
||||||
|
|
||||||
|
async def unsubscribe_scripthash(
|
||||||
|
self,
|
||||||
|
scripthash: str,
|
||||||
|
callback: Callable[[list[Any]], Any] | None = None,
|
||||||
|
) -> bool:
|
||||||
|
if callback:
|
||||||
|
self.off("blockchain.scripthash.subscribe", callback)
|
||||||
|
return await self._call("blockchain.scripthash.unsubscribe", [scripthash])
|
||||||
|
|
||||||
|
async def subscribe_headers(
|
||||||
|
self,
|
||||||
|
callback: Callable[[list[Any]], Any] | None = None,
|
||||||
|
) -> BlockHeader:
|
||||||
|
"""Subscribe to new block headers; returns current tip."""
|
||||||
|
if callback:
|
||||||
|
self.on("blockchain.headers.subscribe", callback)
|
||||||
|
data = await self._call("blockchain.headers.subscribe")
|
||||||
|
return BlockHeader.parse_obj(data)
|
||||||
|
|
||||||
|
# ---- transaction methods ----
|
||||||
|
|
||||||
|
async def broadcast(self, raw_tx: str) -> str:
|
||||||
|
"""Broadcast a raw transaction hex; returns txid on success."""
|
||||||
|
return await self._call("blockchain.transaction.broadcast", [raw_tx])
|
||||||
|
|
||||||
|
async def get_transaction(
|
||||||
|
self, txid: str, verbose: bool = False
|
||||||
|
) -> str | dict[str, Any]:
|
||||||
|
return await self._call("blockchain.transaction.get", [txid, verbose])
|
||||||
|
|
||||||
|
async def get_merkle(self, txid: str, height: int) -> MerkleProof:
|
||||||
|
data = await self._call("blockchain.transaction.get_merkle", [txid, height])
|
||||||
|
return MerkleProof.parse_obj(data)
|
||||||
|
|
||||||
|
async def get_tx_id_from_pos(
|
||||||
|
self, height: int, tx_pos: int, merkle: bool = False
|
||||||
|
) -> str | TxIdWithMerkle:
|
||||||
|
data = await self._call(
|
||||||
|
"blockchain.transaction.id_from_pos", [height, tx_pos, merkle]
|
||||||
|
)
|
||||||
|
if isinstance(data, dict):
|
||||||
|
return TxIdWithMerkle.parse_obj(data)
|
||||||
|
return data
|
||||||
|
|
||||||
|
# ---- block methods ----
|
||||||
|
|
||||||
|
async def get_tip(self) -> BlockHeader:
|
||||||
|
"""Returns current chain tip."""
|
||||||
|
data = await self._call("blockchain.headers.subscribe")
|
||||||
|
return BlockHeader.parse_obj(data)
|
||||||
|
|
||||||
|
async def get_height(self) -> int:
|
||||||
|
"""Returns the current best block height."""
|
||||||
|
return (await self.get_tip()).height
|
||||||
|
|
||||||
|
async def get_block_header(
|
||||||
|
self, height: int, cp_height: int = 0
|
||||||
|
) -> str | BlockHeaderProof:
|
||||||
|
data = await self._call("blockchain.block.header", [height, cp_height])
|
||||||
|
if isinstance(data, dict):
|
||||||
|
return BlockHeaderProof.parse_obj(data)
|
||||||
|
return data
|
||||||
|
|
||||||
|
async def get_block_headers(
|
||||||
|
self, start_height: int, count: int, cp_height: int = 0
|
||||||
|
) -> BlockHeaders:
|
||||||
|
data = await self._call(
|
||||||
|
"blockchain.block.headers", [start_height, count, cp_height]
|
||||||
|
)
|
||||||
|
return BlockHeaders.parse_obj(data)
|
||||||
|
|
||||||
|
# ---- fee methods ----
|
||||||
|
|
||||||
|
async def estimate_fee(self, num_blocks: int) -> float:
|
||||||
|
"""Returns estimated fee rate in BTC/kB for confirmation within num_blocks."""
|
||||||
|
return await self._call("blockchain.estimatefee", [num_blocks])
|
||||||
|
|
||||||
|
async def fee_histogram(self) -> list[FeeHistogramEntry]:
|
||||||
|
"""Returns mempool fee histogram as FeeHistogramEntry(fee_rate, vsize) list."""
|
||||||
|
data = await self._call("mempool.get_fee_histogram")
|
||||||
|
return [FeeHistogramEntry(fee_rate=r[0], vsize=r[1]) for r in data]
|
||||||
@@ -289,7 +289,32 @@ async def btc_rates(currency: str) -> list[tuple[str, float]]:
|
|||||||
return apply_trimmed_mean_filter(all_rates)
|
return apply_trimmed_mean_filter(all_rates)
|
||||||
|
|
||||||
|
|
||||||
|
async def btc_price_from_aggregator(currency: str) -> float | None:
|
||||||
|
url = settings.lnbits_price_aggregator_url.rstrip("/")
|
||||||
|
try:
|
||||||
|
headers = {"User-Agent": settings.user_agent}
|
||||||
|
async with httpx.AsyncClient(headers=headers) as client:
|
||||||
|
r = await client.get(f"{url}/rate/{currency.upper()}", timeout=3)
|
||||||
|
r.raise_for_status()
|
||||||
|
data = r.json()
|
||||||
|
median = data.get("rates", {}).get("median")
|
||||||
|
if median:
|
||||||
|
return float(median)
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"Failed to fetch price from aggregator {url}: {e}")
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
async def btc_price(currency: str) -> float:
|
async def btc_price(currency: str) -> float:
|
||||||
|
if (
|
||||||
|
settings.lnbits_price_aggregator_enabled
|
||||||
|
and settings.lnbits_price_aggregator_url
|
||||||
|
):
|
||||||
|
price = await btc_price_from_aggregator(currency)
|
||||||
|
if price:
|
||||||
|
return price
|
||||||
|
logger.warning("Price aggregator failed, falling back to exchange providers.")
|
||||||
|
|
||||||
rates = await btc_rates(currency)
|
rates = await btc_rates(currency)
|
||||||
if not rates:
|
if not rates:
|
||||||
logger.warning("Could not fetch any Bitcoin price.")
|
logger.warning("Could not fetch any Bitcoin price.")
|
||||||
|
|||||||
@@ -41,19 +41,16 @@ def log_server_info():
|
|||||||
|
|
||||||
def initialize_server_websocket_logger() -> Callable:
|
def initialize_server_websocket_logger() -> Callable:
|
||||||
super_user_hash = sha256(settings.super_user.encode("utf-8")).hexdigest()
|
super_user_hash = sha256(settings.super_user.encode("utf-8")).hexdigest()
|
||||||
|
|
||||||
serverlog_queue: asyncio.Queue = asyncio.Queue()
|
serverlog_queue: asyncio.Queue = asyncio.Queue()
|
||||||
|
|
||||||
async def update_websocket_serverlog():
|
|
||||||
while settings.lnbits_running:
|
|
||||||
msg = await serverlog_queue.get()
|
|
||||||
await websocket_updater(super_user_hash, msg)
|
|
||||||
|
|
||||||
logger.add(
|
logger.add(
|
||||||
lambda msg: serverlog_queue.put_nowait(msg),
|
lambda msg: serverlog_queue.put_nowait(msg),
|
||||||
format=Formatter().format,
|
format=Formatter().format,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
async def update_websocket_serverlog():
|
||||||
|
msg = await serverlog_queue.get()
|
||||||
|
await websocket_updater(super_user_hash, msg)
|
||||||
|
|
||||||
return update_websocket_serverlog
|
return update_websocket_serverlog
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -94,15 +94,15 @@ class PaymentStatus(NamedTuple):
|
|||||||
|
|
||||||
|
|
||||||
class PaymentSuccessStatus(PaymentStatus):
|
class PaymentSuccessStatus(PaymentStatus):
|
||||||
paid = True
|
paid = True # type: ignore[reportIncompatibleVariableOverride]
|
||||||
|
|
||||||
|
|
||||||
class PaymentFailedStatus(PaymentStatus):
|
class PaymentFailedStatus(PaymentStatus):
|
||||||
paid = False
|
paid = False # type: ignore[reportIncompatibleVariableOverride]
|
||||||
|
|
||||||
|
|
||||||
class PaymentPendingStatus(PaymentStatus):
|
class PaymentPendingStatus(PaymentStatus):
|
||||||
paid = None
|
paid = None # type: ignore[reportIncompatibleVariableOverride]
|
||||||
|
|
||||||
|
|
||||||
class Wallet(ABC):
|
class Wallet(ABC):
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ from loguru import logger
|
|||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
from websockets import Subprotocol, connect
|
from websockets import Subprotocol, connect
|
||||||
|
|
||||||
from lnbits import bolt11
|
from lnbits import bolt11 as bolt11_lib
|
||||||
from lnbits.helpers import normalize_endpoint
|
from lnbits.helpers import normalize_endpoint
|
||||||
from lnbits.settings import settings
|
from lnbits.settings import settings
|
||||||
|
|
||||||
@@ -164,15 +164,13 @@ class BlinkWallet(Wallet):
|
|||||||
ok=False, error_message=f"Unable to connect to {self.endpoint}."
|
ok=False, error_message=f"Unable to connect to {self.endpoint}."
|
||||||
)
|
)
|
||||||
|
|
||||||
async def pay_invoice(
|
async def pay_invoice(self, bolt11: str, fee_limit_msat: int) -> PaymentResponse:
|
||||||
self, bolt11_invoice: str, fee_limit_msat: int
|
|
||||||
) -> PaymentResponse:
|
|
||||||
# https://dev.blink.sv/api/btc-ln-send
|
# https://dev.blink.sv/api/btc-ln-send
|
||||||
# Future: add check fee estimate is < fee_limit_msat before paying invoice
|
# Future: add check fee estimate is < fee_limit_msat before paying invoice
|
||||||
|
|
||||||
payment_variables = {
|
payment_variables = {
|
||||||
"input": {
|
"input": {
|
||||||
"paymentRequest": bolt11_invoice,
|
"paymentRequest": bolt11,
|
||||||
"walletId": self.wallet_id,
|
"walletId": self.wallet_id,
|
||||||
"memo": "Payment memo",
|
"memo": "Payment memo",
|
||||||
}
|
}
|
||||||
@@ -190,7 +188,7 @@ class BlinkWallet(Wallet):
|
|||||||
error_message = errors[0].get("message")
|
error_message = errors[0].get("message")
|
||||||
return PaymentResponse(ok=False, error_message=error_message)
|
return PaymentResponse(ok=False, error_message=error_message)
|
||||||
|
|
||||||
checking_id = bolt11.decode(bolt11_invoice).payment_hash
|
checking_id = bolt11_lib.decode(bolt11).payment_hash
|
||||||
|
|
||||||
payment_status = await self.get_payment_status(checking_id)
|
payment_status = await self.get_payment_status(checking_id)
|
||||||
fee_msat = payment_status.fee_msat
|
fee_msat = payment_status.fee_msat
|
||||||
@@ -199,7 +197,7 @@ class BlinkWallet(Wallet):
|
|||||||
ok=True, checking_id=checking_id, fee_msat=fee_msat, preimage=preimage
|
ok=True, checking_id=checking_id, fee_msat=fee_msat, preimage=preimage
|
||||||
)
|
)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
logger.info(f"Failed to pay invoice {bolt11_invoice}")
|
logger.info(f"Failed to pay invoice {bolt11}")
|
||||||
logger.warning(exc)
|
logger.warning(exc)
|
||||||
return PaymentResponse(
|
return PaymentResponse(
|
||||||
error_message=f"Unable to connect to {self.endpoint}."
|
error_message=f"Unable to connect to {self.endpoint}."
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ else:
|
|||||||
|
|
||||||
from bolt11 import Bolt11Exception
|
from bolt11 import Bolt11Exception
|
||||||
from bolt11 import decode as bolt11_decode
|
from bolt11 import decode as bolt11_decode
|
||||||
from breez_sdk import (
|
from breez_sdk import ( # type: ignore[reportMissingImports]
|
||||||
BreezEvent,
|
BreezEvent,
|
||||||
ConnectRequest,
|
ConnectRequest,
|
||||||
EnvironmentType,
|
EnvironmentType,
|
||||||
@@ -39,7 +39,9 @@ else:
|
|||||||
default_config,
|
default_config,
|
||||||
mnemonic_to_seed,
|
mnemonic_to_seed,
|
||||||
)
|
)
|
||||||
from breez_sdk import PaymentStatus as BreezPaymentStatus
|
from breez_sdk import (
|
||||||
|
PaymentStatus as BreezPaymentStatus, # type: ignore[reportMissingImports]
|
||||||
|
)
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
|
|
||||||
from lnbits.settings import settings
|
from lnbits.settings import settings
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ else:
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from bolt11 import decode as bolt11_decode
|
from bolt11 import decode as bolt11_decode
|
||||||
from breez_sdk_liquid import (
|
from breez_sdk_liquid import ( # type: ignore[reportMissingImports]
|
||||||
ConnectRequest,
|
ConnectRequest,
|
||||||
EventListener,
|
EventListener,
|
||||||
GetInfoResponse,
|
GetInfoResponse,
|
||||||
|
|||||||
@@ -103,7 +103,7 @@ class FakeWallet(Wallet):
|
|||||||
preimage=preimage.hex(),
|
preimage=preimage.hex(),
|
||||||
)
|
)
|
||||||
|
|
||||||
async def pay_invoice(self, bolt11: str, _: int) -> PaymentResponse:
|
async def pay_invoice(self, bolt11: str, fee_limit_msat: int) -> PaymentResponse:
|
||||||
try:
|
try:
|
||||||
invoice = decode(bolt11)
|
invoice = decode(bolt11)
|
||||||
except Bolt11Exception as exc:
|
except Bolt11Exception as exc:
|
||||||
@@ -130,7 +130,7 @@ class FakeWallet(Wallet):
|
|||||||
return PaymentPendingStatus()
|
return PaymentPendingStatus()
|
||||||
return PaymentFailedStatus()
|
return PaymentFailedStatus()
|
||||||
|
|
||||||
async def get_payment_status(self, _: str) -> PaymentStatus:
|
async def get_payment_status(self, checking_id: str) -> PaymentStatus:
|
||||||
return PaymentPendingStatus()
|
return PaymentPendingStatus()
|
||||||
|
|
||||||
async def paid_invoices_stream(self) -> AsyncGenerator[str, None]:
|
async def paid_invoices_stream(self) -> AsyncGenerator[str, None]:
|
||||||
|
|||||||
@@ -118,7 +118,7 @@ class LndWallet(Wallet):
|
|||||||
|
|
||||||
cert = open(cert_path, "rb").read()
|
cert = open(cert_path, "rb").read()
|
||||||
creds = grpc.ssl_channel_credentials(cert)
|
creds = grpc.ssl_channel_credentials(cert)
|
||||||
auth_creds = grpc.metadata_call_credentials(self.metadata_callback)
|
auth_creds = grpc.metadata_call_credentials(self.metadata_callback) # type: ignore[reportArgumentType]
|
||||||
composite_creds = grpc.composite_channel_credentials(creds, auth_creds)
|
composite_creds = grpc.composite_channel_credentials(creds, auth_creds)
|
||||||
channel = grpc.aio.secure_channel(
|
channel = grpc.aio.secure_channel(
|
||||||
f"{self.endpoint}:{self.port}", composite_creds
|
f"{self.endpoint}:{self.port}", composite_creds
|
||||||
@@ -192,6 +192,8 @@ class LndWallet(Wallet):
|
|||||||
fee_limit_msat=fee_limit_msat,
|
fee_limit_msat=fee_limit_msat,
|
||||||
timeout_seconds=30,
|
timeout_seconds=30,
|
||||||
no_inflight_updates=True,
|
no_inflight_updates=True,
|
||||||
|
max_parts=16,
|
||||||
|
time_pref=0.9,
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
res: Payment = await self.router_rpc.SendPaymentV2(req).read()
|
res: Payment = await self.router_rpc.SendPaymentV2(req).read()
|
||||||
|
|||||||
+41
-40
@@ -3,6 +3,7 @@ import base64
|
|||||||
import hashlib
|
import hashlib
|
||||||
import json
|
import json
|
||||||
from collections.abc import AsyncGenerator
|
from collections.abc import AsyncGenerator
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
@@ -123,8 +124,8 @@ class LndRestWallet(Wallet):
|
|||||||
hashlib.sha256(unhashed_description).digest()
|
hashlib.sha256(unhashed_description).digest()
|
||||||
).decode("ascii")
|
).decode("ascii")
|
||||||
|
|
||||||
preimage, _payment_hash = random_secret_and_hash()
|
preimage, payment_hash = random_secret_and_hash()
|
||||||
_data["r_hash"] = base64.b64encode(bytes.fromhex(_payment_hash)).decode()
|
_data["r_hash"] = base64.b64encode(bytes.fromhex(payment_hash)).decode()
|
||||||
_data["r_preimage"] = base64.b64encode(bytes.fromhex(preimage)).decode()
|
_data["r_preimage"] = base64.b64encode(bytes.fromhex(preimage)).decode()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -132,34 +133,7 @@ class LndRestWallet(Wallet):
|
|||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
data = r.json()
|
data = r.json()
|
||||||
|
|
||||||
if len(data) == 0:
|
return self._parse_create_invoice_response(r, data, preimage)
|
||||||
return InvoiceResponse(ok=False, error_message="no data")
|
|
||||||
|
|
||||||
if "error" in data:
|
|
||||||
return InvoiceResponse(
|
|
||||||
ok=False, error_message=f"""Server error: '{data["error"]}'"""
|
|
||||||
)
|
|
||||||
|
|
||||||
if r.is_error:
|
|
||||||
return InvoiceResponse(
|
|
||||||
ok=False, error_message=f"Server error: '{r.text}'"
|
|
||||||
)
|
|
||||||
|
|
||||||
if "payment_request" not in data or "r_hash" not in data:
|
|
||||||
return InvoiceResponse(
|
|
||||||
ok=False, error_message="Server error: 'missing required fields'"
|
|
||||||
)
|
|
||||||
|
|
||||||
payment_request = data["payment_request"]
|
|
||||||
payment_hash = base64.b64decode(data["r_hash"]).hex()
|
|
||||||
checking_id = payment_hash
|
|
||||||
return InvoiceResponse(
|
|
||||||
ok=True,
|
|
||||||
checking_id=checking_id,
|
|
||||||
payment_request=payment_request,
|
|
||||||
preimage=preimage,
|
|
||||||
)
|
|
||||||
|
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
return InvoiceResponse(
|
return InvoiceResponse(
|
||||||
ok=False, error_message="Server error: 'invalid json response'"
|
ok=False, error_message="Server error: 'invalid json response'"
|
||||||
@@ -242,12 +216,13 @@ class LndRestWallet(Wallet):
|
|||||||
logger.warning(f"Error getting invoice status: {e}")
|
logger.warning(f"Error getting invoice status: {e}")
|
||||||
return PaymentPendingStatus()
|
return PaymentPendingStatus()
|
||||||
|
|
||||||
if r.is_error or data.get("settled") is None:
|
if r.is_error or data.get("state") is None:
|
||||||
# this must also work when checking_id is not a hex recognizable by lnd
|
# this must also work when checking_id is not a hex recognizable by lnd
|
||||||
# it will return an error and no "settled" attribute on the object
|
# it will return an error and no "state" attribute on the object
|
||||||
|
logger.warning(f"Error checking invoice from LND REST API: {r.text}")
|
||||||
return PaymentPendingStatus()
|
return PaymentPendingStatus()
|
||||||
|
|
||||||
if data.get("settled") is True:
|
if data.get("state") == "SETTLED":
|
||||||
return PaymentSuccessStatus()
|
return PaymentSuccessStatus()
|
||||||
|
|
||||||
if data.get("state") == "CANCELED":
|
if data.get("state") == "CANCELED":
|
||||||
@@ -264,10 +239,11 @@ class LndRestWallet(Wallet):
|
|||||||
"ascii"
|
"ascii"
|
||||||
)
|
)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
|
logger.warning("Invalid checking_id format, must be hex: {checking_id}")
|
||||||
return PaymentPendingStatus()
|
return PaymentPendingStatus()
|
||||||
|
|
||||||
url = f"/v2/router/track/{checking_id}"
|
url = f"/v2/router/track/{checking_id}"
|
||||||
async with self.client.stream("GET", url, timeout=None) as r:
|
async with self.client.stream("GET", url, timeout=30) as r:
|
||||||
async for json_line in r.aiter_lines():
|
async for json_line in r.aiter_lines():
|
||||||
try:
|
try:
|
||||||
line = json.loads(json_line)
|
line = json.loads(json_line)
|
||||||
@@ -298,7 +274,7 @@ class LndRestWallet(Wallet):
|
|||||||
return PaymentFailedStatus()
|
return PaymentFailedStatus()
|
||||||
elif status == "IN_FLIGHT":
|
elif status == "IN_FLIGHT":
|
||||||
logger.info(f"LNDRest Payment in flight: {checking_id}")
|
logger.info(f"LNDRest Payment in flight: {checking_id}")
|
||||||
return PaymentPendingStatus()
|
continue
|
||||||
|
|
||||||
logger.info(f"LNDRest Payment non-existent: {checking_id}")
|
logger.info(f"LNDRest Payment non-existent: {checking_id}")
|
||||||
return PaymentPendingStatus()
|
return PaymentPendingStatus()
|
||||||
@@ -311,13 +287,12 @@ class LndRestWallet(Wallet):
|
|||||||
async for line in r.aiter_lines():
|
async for line in r.aiter_lines():
|
||||||
try:
|
try:
|
||||||
inv = json.loads(line)["result"]
|
inv = json.loads(line)["result"]
|
||||||
if not inv["settled"]:
|
if not inv.get("state") == "SETTLED":
|
||||||
continue
|
continue
|
||||||
|
payment_hash = base64.b64decode(inv.get("r_hash")).hex()
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
logger.debug(exc)
|
logger.debug(exc)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
payment_hash = base64.b64decode(inv["r_hash"]).hex()
|
|
||||||
yield payment_hash
|
yield payment_hash
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
logger.warning(
|
logger.warning(
|
||||||
@@ -363,8 +338,6 @@ class LndRestWallet(Wallet):
|
|||||||
return InvoiceResponse(ok=False, error_message=str(exc))
|
return InvoiceResponse(ok=False, error_message=str(exc))
|
||||||
|
|
||||||
payment_request = data["payment_request"]
|
payment_request = data["payment_request"]
|
||||||
payment_hash = base64.b64encode(bytes.fromhex(payment_hash)).decode("ascii")
|
|
||||||
|
|
||||||
return InvoiceResponse(
|
return InvoiceResponse(
|
||||||
ok=True, checking_id=payment_hash, payment_request=payment_request
|
ok=True, checking_id=payment_hash, payment_request=payment_request
|
||||||
)
|
)
|
||||||
@@ -399,3 +372,31 @@ class LndRestWallet(Wallet):
|
|||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
logger.warning(exc)
|
logger.warning(exc)
|
||||||
return InvoiceResponse(ok=False, error_message=str(exc))
|
return InvoiceResponse(ok=False, error_message=str(exc))
|
||||||
|
|
||||||
|
def _parse_create_invoice_response(
|
||||||
|
self, r: Any, data: dict, preimage: str
|
||||||
|
) -> InvoiceResponse:
|
||||||
|
if not data:
|
||||||
|
return InvoiceResponse(ok=False, error_message="no data")
|
||||||
|
if "error" in data:
|
||||||
|
return InvoiceResponse(
|
||||||
|
ok=False, error_message=f"Server error: '{data['error']}'"
|
||||||
|
)
|
||||||
|
if r.is_error:
|
||||||
|
return InvoiceResponse(ok=False, error_message=f"Server error: '{r.text}'")
|
||||||
|
if "payment_request" not in data or "r_hash" not in data:
|
||||||
|
return InvoiceResponse(
|
||||||
|
ok=False, error_message="Server error: 'missing required fields'"
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
payment_hash = base64.b64decode(data["r_hash"]).hex()
|
||||||
|
except Exception:
|
||||||
|
return InvoiceResponse(
|
||||||
|
ok=False, error_message=f"Unable to b64decode to {data['r_hash']}."
|
||||||
|
)
|
||||||
|
return InvoiceResponse(
|
||||||
|
ok=True,
|
||||||
|
checking_id=payment_hash,
|
||||||
|
payment_request=data["payment_request"],
|
||||||
|
preimage=preimage,
|
||||||
|
)
|
||||||
|
|||||||
+921
-132
File diff suppressed because it is too large
Load Diff
@@ -117,11 +117,12 @@ class PhoenixdWallet(Wallet):
|
|||||||
# PhoenixD description limited to 128 characters
|
# PhoenixD description limited to 128 characters
|
||||||
if description_hash:
|
if description_hash:
|
||||||
data["descriptionHash"] = description_hash.hex()
|
data["descriptionHash"] = description_hash.hex()
|
||||||
|
elif unhashed_description:
|
||||||
|
data["descriptionHash"] = hashlib.sha256(
|
||||||
|
unhashed_description
|
||||||
|
).hexdigest()
|
||||||
else:
|
else:
|
||||||
desc = memo
|
desc = memo or ""
|
||||||
if desc is None and unhashed_description:
|
|
||||||
desc = unhashed_description.decode()
|
|
||||||
desc = desc or ""
|
|
||||||
if len(desc) > 128:
|
if len(desc) > 128:
|
||||||
data["descriptionHash"] = hashlib.sha256(desc.encode()).hexdigest()
|
data["descriptionHash"] = hashlib.sha256(desc.encode()).hexdigest()
|
||||||
else:
|
else:
|
||||||
|
|||||||
Generated
+8
-5
@@ -24,7 +24,7 @@
|
|||||||
"clean-css-cli": "^5.6.3",
|
"clean-css-cli": "^5.6.3",
|
||||||
"concat": "^1.0.3",
|
"concat": "^1.0.3",
|
||||||
"prettier": "^3.8.3",
|
"prettier": "^3.8.3",
|
||||||
"pyright": "1.1.289",
|
"pyright": "1.1.409",
|
||||||
"sass": "^1.99.0",
|
"sass": "^1.99.0",
|
||||||
"terser": "^5.47.1"
|
"terser": "^5.47.1"
|
||||||
}
|
}
|
||||||
@@ -1808,9 +1808,9 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/pyright": {
|
"node_modules/pyright": {
|
||||||
"version": "1.1.289",
|
"version": "1.1.409",
|
||||||
"resolved": "https://registry.npmjs.org/pyright/-/pyright-1.1.289.tgz",
|
"resolved": "https://registry.npmjs.org/pyright/-/pyright-1.1.409.tgz",
|
||||||
"integrity": "sha512-fG3STxnwAt3i7bxbXUPJdYNFrcOWHLwCSEOySH2foUqtYdzWLcxDez0Kgl1X8LMQx0arMJ6HRkKghxfRD1/z6g==",
|
"integrity": "sha512-13VFQyw4mJzshZxcxiYbNjo1hG/WHSRDj70Y3lbJEHqCkI2dvBAUTti8VV6Ezsr5gT93pFvC0e/jAQS4JdHarA==",
|
||||||
"dev": true,
|
"dev": true,
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"bin": {
|
"bin": {
|
||||||
@@ -1818,7 +1818,10 @@
|
|||||||
"pyright-langserver": "langserver.index.js"
|
"pyright-langserver": "langserver.index.js"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=12.0.0"
|
"node": ">=14.0.0"
|
||||||
|
},
|
||||||
|
"optionalDependencies": {
|
||||||
|
"fsevents": "~2.3.3"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/qrcode.vue": {
|
"node_modules/qrcode.vue": {
|
||||||
|
|||||||
+2
-1
@@ -16,7 +16,7 @@
|
|||||||
"clean-css-cli": "^5.6.3",
|
"clean-css-cli": "^5.6.3",
|
||||||
"concat": "^1.0.3",
|
"concat": "^1.0.3",
|
||||||
"prettier": "^3.8.3",
|
"prettier": "^3.8.3",
|
||||||
"pyright": "1.1.289",
|
"pyright": "1.1.409",
|
||||||
"sass": "^1.99.0",
|
"sass": "^1.99.0",
|
||||||
"terser": "^5.47.1"
|
"terser": "^5.47.1"
|
||||||
},
|
},
|
||||||
@@ -111,6 +111,7 @@
|
|||||||
"js/pages/users.js",
|
"js/pages/users.js",
|
||||||
"js/pages/account.js",
|
"js/pages/account.js",
|
||||||
"js/pages/admin.js",
|
"js/pages/admin.js",
|
||||||
|
"js/components/admin/lnbits-admin-funding-seed-backup.js",
|
||||||
"js/components/admin/lnbits-admin-funding.js",
|
"js/components/admin/lnbits-admin-funding.js",
|
||||||
"js/components/admin/lnbits-admin-funding-sources.js",
|
"js/components/admin/lnbits-admin-funding-sources.js",
|
||||||
"js/components/admin/lnbits-admin-fiat-providers.js",
|
"js/components/admin/lnbits-admin-fiat-providers.js",
|
||||||
|
|||||||
Generated
+18
-5
@@ -1,4 +1,4 @@
|
|||||||
# This file is automatically @generated by Poetry 2.4.1 and should not be changed by hand.
|
# This file is automatically @generated by Poetry 2.2.1 and should not be changed by hand.
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "aiohappyeyeballs"
|
name = "aiohappyeyeballs"
|
||||||
@@ -665,6 +665,19 @@ bitstring = "*"
|
|||||||
click = "*"
|
click = "*"
|
||||||
coincurve = "*"
|
coincurve = "*"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "boltz-client"
|
||||||
|
version = "0.4.0"
|
||||||
|
description = "Boltz Swap library"
|
||||||
|
optional = true
|
||||||
|
python-versions = ">=3.10"
|
||||||
|
groups = ["main"]
|
||||||
|
markers = "extra == \"liquid\""
|
||||||
|
files = [
|
||||||
|
{file = "boltz_client-0.4.0-py3-none-manylinux_2_34_x86_64.whl", hash = "sha256:ed0b520209cf1b05a8523002f5d8f26fa29c04d2faecc9cbde3621b4ddc417e6"},
|
||||||
|
{file = "boltz_client-0.4.0.tar.gz", hash = "sha256:a3f5a6b637350267856e3ab680cd92158de720fa2d5805fc075e4583d020cb2a"},
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "breez-sdk"
|
name = "breez-sdk"
|
||||||
version = "0.8.0"
|
version = "0.8.0"
|
||||||
@@ -2134,7 +2147,7 @@ files = [
|
|||||||
|
|
||||||
[package.dependencies]
|
[package.dependencies]
|
||||||
attrs = ">=22.2.0"
|
attrs = ">=22.2.0"
|
||||||
jsonschema-specifications = ">=2023.3.6"
|
jsonschema-specifications = ">=2023.03.6"
|
||||||
referencing = ">=0.28.4"
|
referencing = ">=0.28.4"
|
||||||
rpds-py = ">=0.25.0"
|
rpds-py = ">=0.25.0"
|
||||||
|
|
||||||
@@ -2295,7 +2308,7 @@ colorama = {version = ">=0.3.4", markers = "sys_platform == \"win32\""}
|
|||||||
win32-setctime = {version = ">=1.0.0", markers = "sys_platform == \"win32\""}
|
win32-setctime = {version = ">=1.0.0", markers = "sys_platform == \"win32\""}
|
||||||
|
|
||||||
[package.extras]
|
[package.extras]
|
||||||
dev = ["Sphinx (==8.1.3) ; python_version >= \"3.11\"", "build (==1.2.2) ; python_version >= \"3.11\"", "colorama (==0.4.5) ; python_version < \"3.8\"", "colorama (==0.4.6) ; python_version >= \"3.8\"", "exceptiongroup (==1.1.3) ; python_version >= \"3.7\" and python_version < \"3.11\"", "freezegun (==1.1.0) ; python_version < \"3.8\"", "freezegun (==1.5.0) ; python_version >= \"3.8\"", "mypy (==0.910) ; python_version < \"3.6\"", "mypy (==0.971) ; python_version == \"3.6\"", "mypy (==1.13.0) ; python_version >= \"3.8\"", "mypy (==1.4.1) ; python_version == \"3.7\"", "myst-parser (==4.0.0) ; python_version >= \"3.11\"", "pre-commit (==4.0.1) ; python_version >= \"3.9\"", "pytest (==6.1.2) ; python_version < \"3.8\"", "pytest (==8.3.2) ; python_version >= \"3.8\"", "pytest-cov (==2.12.1) ; python_version < \"3.8\"", "pytest-cov (==5.0.0) ; python_version == \"3.8\"", "pytest-cov (==6.0.0) ; python_version >= \"3.9\"", "pytest-mypy-plugins (==1.9.3) ; python_version >= \"3.6\" and python_version < \"3.8\"", "pytest-mypy-plugins (==3.1.0) ; python_version >= \"3.8\"", "sphinx-rtd-theme (==3.0.2) ; python_version >= \"3.11\"", "tox (==3.27.1) ; python_version < \"3.8\"", "tox (==4.23.2) ; python_version >= \"3.8\"", "twine (==6.0.1) ; python_version >= \"3.11\""]
|
dev = ["Sphinx (==8.1.3) ; python_version >= \"3.11\"", "build (==1.2.2) ; python_version >= \"3.11\"", "colorama (==0.4.5) ; python_version < \"3.8\"", "colorama (==0.4.6) ; python_version >= \"3.8\"", "exceptiongroup (==1.1.3) ; python_version >= \"3.7\" and python_version < \"3.11\"", "freezegun (==1.1.0) ; python_version < \"3.8\"", "freezegun (==1.5.0) ; python_version >= \"3.8\"", "mypy (==v0.910) ; python_version < \"3.6\"", "mypy (==v0.971) ; python_version == \"3.6\"", "mypy (==v1.13.0) ; python_version >= \"3.8\"", "mypy (==v1.4.1) ; python_version == \"3.7\"", "myst-parser (==4.0.0) ; python_version >= \"3.11\"", "pre-commit (==4.0.1) ; python_version >= \"3.9\"", "pytest (==6.1.2) ; python_version < \"3.8\"", "pytest (==8.3.2) ; python_version >= \"3.8\"", "pytest-cov (==2.12.1) ; python_version < \"3.8\"", "pytest-cov (==5.0.0) ; python_version == \"3.8\"", "pytest-cov (==6.0.0) ; python_version >= \"3.9\"", "pytest-mypy-plugins (==1.9.3) ; python_version >= \"3.6\" and python_version < \"3.8\"", "pytest-mypy-plugins (==3.1.0) ; python_version >= \"3.8\"", "sphinx-rtd-theme (==3.0.2) ; python_version >= \"3.11\"", "tox (==3.27.1) ; python_version < \"3.8\"", "tox (==4.23.2) ; python_version >= \"3.8\"", "twine (==6.0.1) ; python_version >= \"3.11\""]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "markdown-it-py"
|
name = "markdown-it-py"
|
||||||
@@ -5104,10 +5117,10 @@ propcache = ">=0.2.1"
|
|||||||
|
|
||||||
[extras]
|
[extras]
|
||||||
breez = ["breez-sdk", "breez-sdk-liquid"]
|
breez = ["breez-sdk", "breez-sdk-liquid"]
|
||||||
liquid = ["wallycore"]
|
liquid = ["boltz-client", "wallycore"]
|
||||||
migration = ["psycopg2-binary"]
|
migration = ["psycopg2-binary"]
|
||||||
|
|
||||||
[metadata]
|
[metadata]
|
||||||
lock-version = "2.1"
|
lock-version = "2.1"
|
||||||
python-versions = ">=3.10,<3.13"
|
python-versions = ">=3.10,<3.13"
|
||||||
content-hash = "4050934800e6dfcc5242d1847d3db69eb6e91d634c09368a28255ad3c908b568"
|
content-hash = "7c70bdad0089089d383cf8f54c37c4473180cea175689b91322beaed1ab42e94"
|
||||||
|
|||||||
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
[project]
|
[project]
|
||||||
name = "lnbits"
|
name = "lnbits"
|
||||||
version = "1.5.4"
|
version = "1.5.6"
|
||||||
requires-python = ">=3.10,<3.13"
|
requires-python = ">=3.10,<3.13"
|
||||||
description = "LNbits, free and open-source Lightning wallet and accounts system."
|
description = "LNbits, free and open-source Lightning wallet and accounts system."
|
||||||
authors = [{ name = "Alan Bits", email = "alan@lnbits.com" }]
|
authors = [{ name = "Alan Bits", email = "alan@lnbits.com" }]
|
||||||
@@ -61,7 +61,7 @@ lnbits-cli = "lnbits.commands:main"
|
|||||||
|
|
||||||
[project.optional-dependencies]
|
[project.optional-dependencies]
|
||||||
breez = ["breez-sdk~=0.8.0", "breez-sdk-liquid~=0.11.11"]
|
breez = ["breez-sdk~=0.8.0", "breez-sdk-liquid~=0.11.11"]
|
||||||
liquid = ["wallycore~=1.5.1"]
|
liquid = ["wallycore~=1.5.1", "boltz-client==0.4.0"]
|
||||||
migration = ["psycopg2-binary~=2.9.11"]
|
migration = ["psycopg2-binary~=2.9.11"]
|
||||||
|
|
||||||
[dependency-groups]
|
[dependency-groups]
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ from pathlib import Path
|
|||||||
import pytest
|
import pytest
|
||||||
from httpx import AsyncClient
|
from httpx import AsyncClient
|
||||||
|
|
||||||
|
from lnbits.core.crud.settings import get_settings_field, set_settings_field
|
||||||
from lnbits.server import server_restart
|
from lnbits.server import server_restart
|
||||||
from lnbits.settings import Settings
|
from lnbits.settings import Settings
|
||||||
|
|
||||||
@@ -81,7 +82,8 @@ async def test_admin_audit_monitor_and_test_email(
|
|||||||
headers={"Authorization": f"Bearer {superuser_token}"},
|
headers={"Authorization": f"Bearer {superuser_token}"},
|
||||||
)
|
)
|
||||||
assert monitor.status_code == 200
|
assert monitor.status_code == 200
|
||||||
assert "invoice_listeners" in monitor.json()
|
task_names = [t["name"] for t in monitor.json()]
|
||||||
|
assert any("invoice_listener" in name for name in task_names)
|
||||||
|
|
||||||
test_email = await client.get(
|
test_email = await client.get(
|
||||||
"/admin/api/v1/testemail",
|
"/admin/api/v1/testemail",
|
||||||
@@ -150,6 +152,15 @@ async def test_admin_partial_reset_restart_and_backup(
|
|||||||
async def test_admin_delete_settings_requires_superuser(
|
async def test_admin_delete_settings_requires_superuser(
|
||||||
client: AsyncClient, superuser_token: str
|
client: AsyncClient, superuser_token: str
|
||||||
):
|
):
|
||||||
|
await set_settings_field("lnbits_site_title", "Reset me")
|
||||||
|
await set_settings_field("lnbits_backend_wallet_class", "BoltzWallet")
|
||||||
|
await set_settings_field("boltz_mnemonic", "keep boltz seed")
|
||||||
|
await set_settings_field("boltz_mnemonic_backup_confirmed", True)
|
||||||
|
await set_settings_field("phoenixd_mnemonic", "keep phoenixd seed")
|
||||||
|
await set_settings_field("phoenixd_mnemonic_backup_confirmed", True)
|
||||||
|
await set_settings_field("spark_l2_mnemonic", "keep spark seed")
|
||||||
|
await set_settings_field("spark_l2_mnemonic_backup_confirmed", True)
|
||||||
|
|
||||||
server_restart.clear()
|
server_restart.clear()
|
||||||
response = await client.delete(
|
response = await client.delete(
|
||||||
"/admin/api/v1/settings",
|
"/admin/api/v1/settings",
|
||||||
@@ -157,4 +168,21 @@ async def test_admin_delete_settings_requires_superuser(
|
|||||||
)
|
)
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
assert server_restart.is_set() is True
|
assert server_restart.is_set() is True
|
||||||
|
assert await get_settings_field("lnbits_site_title") is None
|
||||||
|
|
||||||
|
backend_wallet = await get_settings_field("lnbits_backend_wallet_class")
|
||||||
|
boltz_seed = await get_settings_field("boltz_mnemonic")
|
||||||
|
boltz_confirmed = await get_settings_field("boltz_mnemonic_backup_confirmed")
|
||||||
|
phoenixd_seed = await get_settings_field("phoenixd_mnemonic")
|
||||||
|
phoenixd_confirmed = await get_settings_field("phoenixd_mnemonic_backup_confirmed")
|
||||||
|
spark_l2_seed = await get_settings_field("spark_l2_mnemonic")
|
||||||
|
spark_l2_confirmed = await get_settings_field("spark_l2_mnemonic_backup_confirmed")
|
||||||
|
assert backend_wallet and backend_wallet.value == "BoltzWallet"
|
||||||
|
assert boltz_seed and boltz_seed.value == "keep boltz seed"
|
||||||
|
assert boltz_confirmed and boltz_confirmed.value is True
|
||||||
|
assert phoenixd_seed and phoenixd_seed.value == "keep phoenixd seed"
|
||||||
|
assert phoenixd_confirmed and phoenixd_confirmed.value is True
|
||||||
|
assert spark_l2_seed and spark_l2_seed.value == "keep spark seed"
|
||||||
|
assert spark_l2_confirmed and spark_l2_confirmed.value is True
|
||||||
|
|
||||||
server_restart.clear()
|
server_restart.clear()
|
||||||
|
|||||||
@@ -1745,10 +1745,14 @@ async def test_api_create_user_api_token_success(
|
|||||||
), "Expiration time should be 60 minutes from now."
|
), "Expiration time should be 60 minutes from now."
|
||||||
|
|
||||||
token_id = payload["api_token_id"]
|
token_id = payload["api_token_id"]
|
||||||
assert any(
|
stored_token = next(
|
||||||
token_id in [token.id for token in acl.token_id_list]
|
token
|
||||||
for acl in acls.access_control_list
|
for acl in acls.access_control_list
|
||||||
), "API token should be part of at least one ACL."
|
for token in acl.token_id_list
|
||||||
|
if token.id == token_id
|
||||||
|
)
|
||||||
|
assert stored_token.expires_at is not None
|
||||||
|
assert abs(stored_token.expires_at - expiration_time) <= 1
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
@pytest.mark.anyio
|
||||||
|
|||||||
@@ -72,9 +72,43 @@ async def test_auth_api_sso_login_and_callback(http_client: AsyncClient, mocker)
|
|||||||
login_sso = _FakeSSO()
|
login_sso = _FakeSSO()
|
||||||
mocker.patch("lnbits.core.views.auth_api._new_sso", return_value=login_sso)
|
mocker.patch("lnbits.core.views.auth_api._new_sso", return_value=login_sso)
|
||||||
|
|
||||||
response = await http_client.get(
|
unauthenticated = await http_client.get(
|
||||||
f"/api/v1/auth/{provider}", params={"user_id": user.id}
|
f"/api/v1/auth/{provider}", params={"user_id": user.id}
|
||||||
)
|
)
|
||||||
|
assert unauthenticated.status_code == 403
|
||||||
|
assert unauthenticated.json()["detail"] == "User ID mismatch."
|
||||||
|
|
||||||
|
other_user = await create_user_account(
|
||||||
|
Account(
|
||||||
|
id=uuid4().hex,
|
||||||
|
username=f"user_{uuid4().hex[:8]}",
|
||||||
|
email=f"user_{uuid4().hex[:8]}@lnbits.com",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
other_login = await http_client.post(
|
||||||
|
"/api/v1/auth/usr", json={"usr": other_user.id}
|
||||||
|
)
|
||||||
|
http_client.cookies.clear()
|
||||||
|
assert other_login.status_code == 200
|
||||||
|
other_headers = {
|
||||||
|
"Authorization": f"Bearer {other_login.json()['access_token']}",
|
||||||
|
}
|
||||||
|
wrong_user = await http_client.get(
|
||||||
|
f"/api/v1/auth/{provider}",
|
||||||
|
params={"user_id": user.id},
|
||||||
|
headers=other_headers,
|
||||||
|
)
|
||||||
|
assert wrong_user.status_code == 403
|
||||||
|
assert wrong_user.json()["detail"] == "User ID mismatch."
|
||||||
|
|
||||||
|
login = await http_client.post("/api/v1/auth/usr", json={"usr": user.id})
|
||||||
|
http_client.cookies.clear()
|
||||||
|
assert login.status_code == 200
|
||||||
|
headers = {"Authorization": f"Bearer {login.json()['access_token']}"}
|
||||||
|
|
||||||
|
response = await http_client.get(
|
||||||
|
f"/api/v1/auth/{provider}", params={"user_id": user.id}, headers=headers
|
||||||
|
)
|
||||||
assert response.status_code == 307
|
assert response.status_code == 307
|
||||||
assert response.headers["location"] == "https://example.com/sso/login"
|
assert response.headers["location"] == "https://example.com/sso/login"
|
||||||
assert login_sso.redirect_uri == f"{http_client.base_url}/api/v1/auth/github/token"
|
assert login_sso.redirect_uri == f"{http_client.base_url}/api/v1/auth/github/token"
|
||||||
|
|||||||
@@ -194,7 +194,7 @@ async def test_callback_api_handles_revolut_subscription_event(
|
|||||||
settings.revolut_api_secret_key = "revolut-secret"
|
settings.revolut_api_secret_key = "revolut-secret"
|
||||||
settings.revolut_api_version = "2026-04-20"
|
settings.revolut_api_version = "2026-04-20"
|
||||||
revolut_provider = RevolutWallet()
|
revolut_provider = RevolutWallet()
|
||||||
mocker.patch.object(
|
get_subscription_mock = mocker.patch.object(
|
||||||
revolut_provider,
|
revolut_provider,
|
||||||
"get_subscription",
|
"get_subscription",
|
||||||
return_value={
|
return_value={
|
||||||
@@ -253,23 +253,10 @@ async def test_callback_api_handles_revolut_subscription_event(
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
assert create_wallet_invoice_mock.await_count == 1
|
get_subscription_mock.assert_not_awaited()
|
||||||
called_wallet_id, invoice = create_wallet_invoice_mock.await_args.args
|
create_wallet_invoice_mock.assert_not_awaited()
|
||||||
assert called_wallet_id == "wallet_1"
|
update_payment_mock.assert_not_awaited()
|
||||||
assert invoice.amount == 9.25
|
fiat_status_mock.assert_not_awaited()
|
||||||
assert invoice.memo == "Revolut Members"
|
|
||||||
assert invoice.external_id == "SUBSCRIPTION_1"
|
|
||||||
assert invoice.internal is True
|
|
||||||
assert invoice.extra["fiat_method"] == "subscription"
|
|
||||||
assert invoice.extra["subscription"]["checking_id"] == "order_ORDER_SUB_1"
|
|
||||||
assert payment.fiat_provider == "revolut"
|
|
||||||
assert payment.fee == -2
|
|
||||||
assert payment.extra["fiat_checking_id"] == "order_ORDER_SUB_1"
|
|
||||||
assert payment.checking_id == "fiat_revolut_order_ORDER_SUB_1"
|
|
||||||
update_payment_mock.assert_awaited_once_with(
|
|
||||||
payment, "fiat_revolut_order_ORDER_SUB_1"
|
|
||||||
)
|
|
||||||
fiat_status_mock.assert_awaited_once_with(payment)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
@pytest.mark.anyio
|
||||||
@@ -353,10 +340,9 @@ async def test_callback_api_handles_revolut_subscription_order_event(
|
|||||||
|
|
||||||
assert get_payment_mock.await_count == 2
|
assert get_payment_mock.await_count == 2
|
||||||
get_payment_mock.assert_any_await("fiat_revolut_order_ORDER_SUB_1")
|
get_payment_mock.assert_any_await("fiat_revolut_order_ORDER_SUB_1")
|
||||||
assert get_order_mock.await_count == 2
|
assert get_order_mock.await_count == 1
|
||||||
assert [call.args for call in get_subscription_mock.await_args_list] == [
|
assert [call.args for call in get_subscription_mock.await_args_list] == [
|
||||||
("SUBSCRIPTION_1",),
|
("SUBSCRIPTION_1",),
|
||||||
("SUBSCRIPTION_1",),
|
|
||||||
]
|
]
|
||||||
assert create_wallet_invoice_mock.await_count == 1
|
assert create_wallet_invoice_mock.await_count == 1
|
||||||
called_wallet_id, invoice = create_wallet_invoice_mock.await_args.args
|
called_wallet_id, invoice = create_wallet_invoice_mock.await_args.args
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
from typing import cast
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
@@ -106,7 +107,7 @@ async def test_lnurl_api_auth_and_pay_flow(mocker):
|
|||||||
await api_perform_lnurlauth(auth_response, wallet_info)
|
await api_perform_lnurlauth(auth_response, wallet_info)
|
||||||
|
|
||||||
action_response = LnurlPayActionResponse(
|
action_response = LnurlPayActionResponse(
|
||||||
pr=LightningInvoice(TEST_BOLT11),
|
pr=cast(LightningInvoice, LightningInvoice(TEST_BOLT11)),
|
||||||
disposable=False,
|
disposable=False,
|
||||||
successAction=parse_obj_as(MessageAction, {"message": "paid"}),
|
successAction=parse_obj_as(MessageAction, {"message": "paid"}),
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import pytest
|
|||||||
from fastapi import HTTPException
|
from fastapi import HTTPException
|
||||||
from pydantic import ValidationError
|
from pydantic import ValidationError
|
||||||
|
|
||||||
from lnbits.core.crud.payments import create_payment, get_payments
|
from lnbits.core.crud.payments import create_payment, get_payment, get_payments
|
||||||
from lnbits.core.models import Account, CreateInvoice, PaymentFilters, PaymentState
|
from lnbits.core.models import Account, CreateInvoice, PaymentFilters, PaymentState
|
||||||
from lnbits.core.models.payments import CancelInvoice, CreatePayment, SettleInvoice
|
from lnbits.core.models.payments import CancelInvoice, CreatePayment, SettleInvoice
|
||||||
from lnbits.core.models.users import AccountId
|
from lnbits.core.models.users import AccountId
|
||||||
@@ -161,7 +161,7 @@ async def test_payment_api_fee_reserve_and_hold_invoice_actions(mocker):
|
|||||||
wallet.id, CreateInvoice(out=False, amount=42, memo="reserve")
|
wallet.id, CreateInvoice(out=False, amount=42, memo="reserve")
|
||||||
)
|
)
|
||||||
reserve = await api_payments_fee_reserve(invoice.bolt11)
|
reserve = await api_payments_fee_reserve(invoice.bolt11)
|
||||||
assert json.loads(reserve.body)["fee_reserve"] >= 0
|
assert json.loads(bytes(reserve.body))["fee_reserve"] >= 0
|
||||||
|
|
||||||
with pytest.raises(HTTPException, match="Invoice has no amount."):
|
with pytest.raises(HTTPException, match="Invoice has no amount."):
|
||||||
await api_payments_fee_reserve(ZERO_AMOUNT_INVOICE)
|
await api_payments_fee_reserve(ZERO_AMOUNT_INVOICE)
|
||||||
@@ -218,6 +218,164 @@ async def test_payment_api_fee_reserve_and_hold_invoice_actions(mocker):
|
|||||||
cancel_mock.assert_awaited_once()
|
cancel_mock.assert_awaited_once()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_payment_extra_update_appends_new_keys(
|
||||||
|
client,
|
||||||
|
to_wallet,
|
||||||
|
adminkey_headers_to,
|
||||||
|
):
|
||||||
|
payment_hash = uuid4().hex
|
||||||
|
checking_id = await _create_payment(
|
||||||
|
to_wallet.id,
|
||||||
|
amount_msat=1_000,
|
||||||
|
payment_hash=payment_hash,
|
||||||
|
tag="splitpayments",
|
||||||
|
)
|
||||||
|
|
||||||
|
response = await client.patch(
|
||||||
|
"/api/v1/payments/extra",
|
||||||
|
headers=adminkey_headers_to,
|
||||||
|
json={
|
||||||
|
"payment_hash": payment_hash,
|
||||||
|
"extra": {"child": "daughter", "compliance_note": "reviewed"},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
|
assert response.status_code == 200
|
||||||
|
extra = response.json()["extra"]
|
||||||
|
assert extra["tag"] == "splitpayments"
|
||||||
|
assert extra["child"] == "daughter"
|
||||||
|
assert extra["compliance_note"] == "reviewed"
|
||||||
|
|
||||||
|
payment = await get_payment(checking_id)
|
||||||
|
assert payment.extra == extra
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_payment_extra_update_creates_extra_when_missing(
|
||||||
|
client,
|
||||||
|
to_wallet,
|
||||||
|
adminkey_headers_to,
|
||||||
|
):
|
||||||
|
payment_hash = uuid4().hex
|
||||||
|
checking_id = await _create_payment(
|
||||||
|
to_wallet.id,
|
||||||
|
amount_msat=1_000,
|
||||||
|
payment_hash=payment_hash,
|
||||||
|
)
|
||||||
|
|
||||||
|
response = await client.patch(
|
||||||
|
"/api/v1/payments/extra",
|
||||||
|
headers=adminkey_headers_to,
|
||||||
|
json={"payment_hash": payment_hash, "extra": {"note": "reviewed"}},
|
||||||
|
)
|
||||||
|
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert response.json()["extra"] == {"note": "reviewed"}
|
||||||
|
|
||||||
|
payment = await get_payment(checking_id)
|
||||||
|
assert payment.extra == {"note": "reviewed"}
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_payment_extra_update_rejects_existing_keys(
|
||||||
|
client,
|
||||||
|
to_wallet,
|
||||||
|
adminkey_headers_to,
|
||||||
|
):
|
||||||
|
payment_hash = uuid4().hex
|
||||||
|
checking_id = await _create_payment(
|
||||||
|
to_wallet.id,
|
||||||
|
amount_msat=1_000,
|
||||||
|
payment_hash=payment_hash,
|
||||||
|
tag="original",
|
||||||
|
)
|
||||||
|
|
||||||
|
response = await client.patch(
|
||||||
|
"/api/v1/payments/extra",
|
||||||
|
headers=adminkey_headers_to,
|
||||||
|
json={"payment_hash": payment_hash, "extra": {"tag": "overwritten"}},
|
||||||
|
)
|
||||||
|
|
||||||
|
assert response.status_code == 400
|
||||||
|
assert response.json()["detail"] == "Extra keys already exist: tag."
|
||||||
|
|
||||||
|
payment = await get_payment(checking_id)
|
||||||
|
assert payment.extra == {"tag": "original"}
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_payment_extra_update_requires_admin_key(
|
||||||
|
client,
|
||||||
|
to_wallet,
|
||||||
|
inkey_headers_to,
|
||||||
|
):
|
||||||
|
payment_hash = uuid4().hex
|
||||||
|
await _create_payment(
|
||||||
|
to_wallet.id,
|
||||||
|
amount_msat=1_000,
|
||||||
|
payment_hash=payment_hash,
|
||||||
|
)
|
||||||
|
|
||||||
|
response = await client.patch(
|
||||||
|
"/api/v1/payments/extra",
|
||||||
|
headers=inkey_headers_to,
|
||||||
|
json={"payment_hash": payment_hash, "extra": {"note": "invoice key"}},
|
||||||
|
)
|
||||||
|
|
||||||
|
assert response.status_code == 403
|
||||||
|
assert response.json()["detail"] == "Invalid adminkey."
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_payment_extra_update_is_wallet_scoped(
|
||||||
|
client,
|
||||||
|
from_wallet,
|
||||||
|
adminkey_headers_to,
|
||||||
|
):
|
||||||
|
payment_hash = uuid4().hex
|
||||||
|
await _create_payment(
|
||||||
|
from_wallet.id,
|
||||||
|
amount_msat=1_000,
|
||||||
|
payment_hash=payment_hash,
|
||||||
|
)
|
||||||
|
|
||||||
|
response = await client.patch(
|
||||||
|
"/api/v1/payments/extra",
|
||||||
|
headers=adminkey_headers_to,
|
||||||
|
json={"payment_hash": payment_hash, "extra": {"note": "wrong wallet"}},
|
||||||
|
)
|
||||||
|
|
||||||
|
assert response.status_code == 404
|
||||||
|
assert response.json()["detail"] == "Payment does not exist."
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_payment_extra_update_requires_successful_payment(
|
||||||
|
client,
|
||||||
|
to_wallet,
|
||||||
|
adminkey_headers_to,
|
||||||
|
):
|
||||||
|
payment_hash = uuid4().hex
|
||||||
|
await _create_payment(
|
||||||
|
to_wallet.id,
|
||||||
|
amount_msat=1_000,
|
||||||
|
payment_hash=payment_hash,
|
||||||
|
status=PaymentState.PENDING,
|
||||||
|
)
|
||||||
|
|
||||||
|
response = await client.patch(
|
||||||
|
"/api/v1/payments/extra",
|
||||||
|
headers=adminkey_headers_to,
|
||||||
|
json={"payment_hash": payment_hash, "extra": {"note": "too early"}},
|
||||||
|
)
|
||||||
|
|
||||||
|
assert response.status_code == 400
|
||||||
|
assert (
|
||||||
|
response.json()["detail"] == "Payment extra can only be updated after success."
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def _create_payment(
|
async def _create_payment(
|
||||||
wallet_id: str,
|
wallet_id: str,
|
||||||
*,
|
*,
|
||||||
|
|||||||
@@ -26,8 +26,6 @@ docker_bitcoin_cli = [
|
|||||||
"exec",
|
"exec",
|
||||||
"lnbits-bitcoind-1",
|
"lnbits-bitcoind-1",
|
||||||
"bitcoin-cli",
|
"bitcoin-cli",
|
||||||
"-rpcuser=lnbits",
|
|
||||||
"-rpcpassword=lnbits",
|
|
||||||
"-regtest",
|
"-regtest",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,184 @@
|
|||||||
|
"""
|
||||||
|
Electrum client integration tests against the regtest electrs container.
|
||||||
|
Requires the regtest docker-compose stack (docker/regtest/docker-compose.yml).
|
||||||
|
electrs is exposed on localhost:19001 (plain TCP) and localhost:3002 (HTTP).
|
||||||
|
"""
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
import httpx
|
||||||
|
import pytest
|
||||||
|
from loguru import logger
|
||||||
|
|
||||||
|
from lnbits.utils.electrum import ElectrumClient, scripthash_from_scriptpubkey
|
||||||
|
|
||||||
|
from .helpers import docker_bitcoin_cli, run_cmd, run_cmd_json
|
||||||
|
|
||||||
|
ELECTRS_HOST = "localhost"
|
||||||
|
ELECTRS_PORT = 19001
|
||||||
|
ELECTRS_HTTP = "http://localhost:3002"
|
||||||
|
|
||||||
|
|
||||||
|
def bitcoin_height() -> int:
|
||||||
|
return run_cmd_json([*docker_bitcoin_cli, "getblockchaininfo"])["blocks"]
|
||||||
|
|
||||||
|
|
||||||
|
def mine_blocks(n: int = 1) -> int:
|
||||||
|
"""Mine n blocks and return the new chain height."""
|
||||||
|
run_cmd([*docker_bitcoin_cli, "-generate", str(n)])
|
||||||
|
return bitcoin_height()
|
||||||
|
|
||||||
|
|
||||||
|
def new_address() -> str:
|
||||||
|
return run_cmd([*docker_bitcoin_cli, "getnewaddress", "bech32"])
|
||||||
|
|
||||||
|
|
||||||
|
def get_scriptpubkey(address: str) -> bytes:
|
||||||
|
info = run_cmd_json([*docker_bitcoin_cli, "getaddressinfo", address])
|
||||||
|
return bytes.fromhex(info["scriptPubKey"])
|
||||||
|
|
||||||
|
|
||||||
|
def send_to_address(address: str, sats: int) -> str:
|
||||||
|
btc = f"{sats * 1e-8:.8f}"
|
||||||
|
return run_cmd([*docker_bitcoin_cli, "sendtoaddress", address, btc])
|
||||||
|
|
||||||
|
|
||||||
|
async def wait_for_electrs(height: int, timeout: float = 15.0) -> None:
|
||||||
|
"""Poll electrs HTTP until it has indexed up to `height`."""
|
||||||
|
loop = asyncio.get_running_loop()
|
||||||
|
deadline = loop.time() + timeout
|
||||||
|
async with httpx.AsyncClient() as http:
|
||||||
|
while loop.time() < deadline:
|
||||||
|
try:
|
||||||
|
r = await http.get(f"{ELECTRS_HTTP}/blocks/tip/height", timeout=2)
|
||||||
|
if int(r.text) >= height:
|
||||||
|
return
|
||||||
|
except Exception:
|
||||||
|
logger.debug("electrs not ready yet")
|
||||||
|
await asyncio.sleep(0.25)
|
||||||
|
raise TimeoutError(f"electrs did not reach height {height} within {timeout}s")
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(scope="module", autouse=True)
|
||||||
|
async def wait_after_electrum_tests():
|
||||||
|
yield
|
||||||
|
await asyncio.sleep(1)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_connect_and_height():
|
||||||
|
async with ElectrumClient(f"tcp://{ELECTRS_HOST}:{ELECTRS_PORT}") as client:
|
||||||
|
before = await client.get_height()
|
||||||
|
|
||||||
|
target = mine_blocks(3)
|
||||||
|
await wait_for_electrs(target)
|
||||||
|
|
||||||
|
async with ElectrumClient(f"tcp://{ELECTRS_HOST}:{ELECTRS_PORT}") as client:
|
||||||
|
after = await client.get_height()
|
||||||
|
|
||||||
|
assert isinstance(before, int) and before >= 0
|
||||||
|
assert after == before + 3
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_get_tip():
|
||||||
|
async with ElectrumClient(f"tcp://{ELECTRS_HOST}:{ELECTRS_PORT}") as client:
|
||||||
|
tip = await client.get_tip()
|
||||||
|
assert isinstance(tip.height, int)
|
||||||
|
assert isinstance(tip.hex, str)
|
||||||
|
assert len(tip.hex) == 160 # 80-byte serialised header
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_server_banner():
|
||||||
|
async with ElectrumClient(f"tcp://{ELECTRS_HOST}:{ELECTRS_PORT}") as client:
|
||||||
|
banner = await client.server_banner()
|
||||||
|
assert isinstance(banner, str)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_balance_after_payment():
|
||||||
|
address = new_address()
|
||||||
|
scripthash = scripthash_from_scriptpubkey(get_scriptpubkey(address))
|
||||||
|
|
||||||
|
async with ElectrumClient(f"tcp://{ELECTRS_HOST}:{ELECTRS_PORT}") as client:
|
||||||
|
empty = await client.get_balance(scripthash)
|
||||||
|
assert empty.confirmed == 0
|
||||||
|
assert empty.unconfirmed == 0
|
||||||
|
|
||||||
|
send_to_address(address, 500_000)
|
||||||
|
target = mine_blocks(1)
|
||||||
|
await wait_for_electrs(target)
|
||||||
|
|
||||||
|
async with ElectrumClient(f"tcp://{ELECTRS_HOST}:{ELECTRS_PORT}") as client:
|
||||||
|
confirmed = await client.get_balance(scripthash)
|
||||||
|
assert confirmed.confirmed == 500_000
|
||||||
|
assert confirmed.unconfirmed == 0
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_history_and_utxos():
|
||||||
|
address = new_address()
|
||||||
|
scripthash = scripthash_from_scriptpubkey(get_scriptpubkey(address))
|
||||||
|
|
||||||
|
send_to_address(address, 250_000)
|
||||||
|
target = mine_blocks(1)
|
||||||
|
await wait_for_electrs(target)
|
||||||
|
|
||||||
|
async with ElectrumClient(f"tcp://{ELECTRS_HOST}:{ELECTRS_PORT}") as client:
|
||||||
|
history = await client.get_history(scripthash)
|
||||||
|
assert len(history) >= 1
|
||||||
|
assert history[0].tx_hash
|
||||||
|
assert history[0].height > 0
|
||||||
|
|
||||||
|
utxos = await client.listunspent(scripthash)
|
||||||
|
assert len(utxos) == 1
|
||||||
|
assert utxos[0].value == 250_000
|
||||||
|
|
||||||
|
raw_tx = await client.get_transaction(utxos[0].tx_hash)
|
||||||
|
assert isinstance(raw_tx, str) and len(raw_tx) > 0
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_subscribe_scripthash_payment():
|
||||||
|
address = new_address()
|
||||||
|
scripthash = scripthash_from_scriptpubkey(get_scriptpubkey(address))
|
||||||
|
|
||||||
|
received: list = []
|
||||||
|
event = asyncio.Event()
|
||||||
|
|
||||||
|
def on_change(params: list) -> None:
|
||||||
|
received.append(params)
|
||||||
|
event.set()
|
||||||
|
|
||||||
|
async with ElectrumClient(f"tcp://{ELECTRS_HOST}:{ELECTRS_PORT}") as client:
|
||||||
|
initial_status = await client.subscribe_scripthash(
|
||||||
|
scripthash, callback=on_change
|
||||||
|
)
|
||||||
|
assert initial_status is None # fresh address has no history
|
||||||
|
|
||||||
|
send_to_address(address, 777_000)
|
||||||
|
target = mine_blocks(1)
|
||||||
|
await wait_for_electrs(target)
|
||||||
|
|
||||||
|
await asyncio.wait_for(event.wait(), timeout=10)
|
||||||
|
|
||||||
|
assert len(received) == 1
|
||||||
|
assert received[0][0] == scripthash # first param is the scripthash
|
||||||
|
assert received[0][1] is not None # second param is the new status hash
|
||||||
|
|
||||||
|
balance = await client.get_balance(scripthash)
|
||||||
|
assert balance.confirmed == 777_000
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_subscribe_headers():
|
||||||
|
async with ElectrumClient(f"tcp://{ELECTRS_HOST}:{ELECTRS_PORT}") as client:
|
||||||
|
notifications: list = []
|
||||||
|
tip = await client.subscribe_headers(callback=lambda p: notifications.append(p))
|
||||||
|
height_before = tip.height
|
||||||
|
|
||||||
|
target = mine_blocks(1)
|
||||||
|
await wait_for_electrs(target)
|
||||||
|
|
||||||
|
assert await client.get_height() == height_before + 1
|
||||||
@@ -13,10 +13,13 @@ from lnbits.core.services import (
|
|||||||
fee_reserve_total,
|
fee_reserve_total,
|
||||||
get_balance_delta,
|
get_balance_delta,
|
||||||
)
|
)
|
||||||
from lnbits.core.services.payments import pay_invoice, update_wallet_balance
|
from lnbits.core.services.payments import (
|
||||||
|
pay_invoice,
|
||||||
|
update_wallet_balance,
|
||||||
|
)
|
||||||
from lnbits.core.services.users import create_user_account
|
from lnbits.core.services.users import create_user_account
|
||||||
from lnbits.exceptions import PaymentError
|
from lnbits.exceptions import PaymentError
|
||||||
from lnbits.tasks import create_task, wait_for_paid_invoices
|
from lnbits.task_manager import task_manager
|
||||||
from lnbits.wallets import get_funding_source
|
from lnbits.wallets import get_funding_source
|
||||||
|
|
||||||
from ..helpers import is_fake, is_regtest
|
from ..helpers import is_fake, is_regtest
|
||||||
@@ -160,12 +163,11 @@ async def test_create_real_invoice(
|
|||||||
assert not payment_status["paid"]
|
assert not payment_status["paid"]
|
||||||
|
|
||||||
on_paid_mock = mocker.AsyncMock()
|
on_paid_mock = mocker.AsyncMock()
|
||||||
create_task(wait_for_paid_invoices("test_create_invoice", on_paid_mock)())
|
task_manager.register_invoice_listener(on_paid_mock, "test_create_invoice")
|
||||||
|
|
||||||
pay_real_invoice(invoice["bolt11"])
|
pay_real_invoice(invoice["bolt11"])
|
||||||
|
|
||||||
await asyncio.sleep(1)
|
await asyncio.sleep(1)
|
||||||
|
|
||||||
assert on_paid_mock.call_count == 1
|
assert on_paid_mock.call_count == 1
|
||||||
payment = on_paid_mock.call_args_list[0][0][0]
|
payment = on_paid_mock.call_args_list[0][0][0]
|
||||||
|
|
||||||
@@ -393,12 +395,11 @@ async def test_receive_real_invoice_set_pending_and_check_state(
|
|||||||
assert not payment_status["paid"]
|
assert not payment_status["paid"]
|
||||||
|
|
||||||
on_paid_mock = mocker.AsyncMock()
|
on_paid_mock = mocker.AsyncMock()
|
||||||
create_task(wait_for_paid_invoices("test_create_invoice", on_paid_mock)())
|
task_manager.register_invoice_listener(on_paid_mock, "test_create_invoice")
|
||||||
|
|
||||||
pay_real_invoice(invoice["bolt11"])
|
pay_real_invoice(invoice["bolt11"])
|
||||||
|
|
||||||
await asyncio.sleep(1)
|
await asyncio.sleep(1)
|
||||||
|
|
||||||
assert on_paid_mock.call_count == 1
|
assert on_paid_mock.call_count == 1
|
||||||
payment = on_paid_mock.call_args_list[0][0][0]
|
payment = on_paid_mock.call_args_list[0][0][0]
|
||||||
|
|
||||||
@@ -412,6 +413,8 @@ async def test_receive_real_invoice_set_pending_and_check_state(
|
|||||||
payment_status = response.json()
|
payment_status = response.json()
|
||||||
assert payment_status["paid"]
|
assert payment_status["paid"]
|
||||||
|
|
||||||
|
assert payment
|
||||||
|
|
||||||
# set the incoming invoice to pending
|
# set the incoming invoice to pending
|
||||||
payment.status = PaymentState.PENDING
|
payment.status = PaymentState.PENDING
|
||||||
await update_payment(payment)
|
await update_payment(payment)
|
||||||
|
|||||||
+24
-14
@@ -5,6 +5,7 @@ import pytest
|
|||||||
from pytest_mock.plugin import MockerFixture
|
from pytest_mock.plugin import MockerFixture
|
||||||
|
|
||||||
from lnbits.settings import Settings
|
from lnbits.settings import Settings
|
||||||
|
from lnbits.task_manager import task_manager
|
||||||
from lnbits.utils.cache import Cache, Cached
|
from lnbits.utils.cache import Cache, Cached
|
||||||
|
|
||||||
key = "foo"
|
key = "foo"
|
||||||
@@ -13,11 +14,10 @@ value = "bar"
|
|||||||
|
|
||||||
@pytest.fixture
|
@pytest.fixture
|
||||||
async def cache():
|
async def cache():
|
||||||
cache = Cache(interval=0.1)
|
cache = Cache()
|
||||||
|
task = task_manager.create_permanent_task(cache.invalidate_cache, interval=1)
|
||||||
task = asyncio.create_task(cache.invalidate_forever())
|
|
||||||
yield cache
|
yield cache
|
||||||
task.cancel()
|
task_manager.cancel_task(task)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
@pytest.mark.anyio
|
||||||
@@ -31,13 +31,13 @@ async def test_cache_get_set(cache):
|
|||||||
@pytest.mark.anyio
|
@pytest.mark.anyio
|
||||||
async def test_cache_expiry(cache):
|
async def test_cache_expiry(cache):
|
||||||
# gets expired by `get` call
|
# gets expired by `get` call
|
||||||
cache.set(key, value, expiry=0.01)
|
cache.set(key, value, expiry=1)
|
||||||
await asyncio.sleep(0.02)
|
await asyncio.sleep(2)
|
||||||
assert not cache.get(key)
|
assert not cache.get(key)
|
||||||
|
|
||||||
# gets expired by invalidation task
|
# gets expired by invalidation task
|
||||||
cache.set(key, value, expiry=0.1)
|
cache.set(key, value, expiry=1)
|
||||||
await asyncio.sleep(0.2)
|
await asyncio.sleep(2)
|
||||||
assert key not in cache._values
|
assert key not in cache._values
|
||||||
assert not cache.get(key)
|
assert not cache.get(key)
|
||||||
|
|
||||||
@@ -94,23 +94,33 @@ async def test_cache_pop_expired_returns_default(cache):
|
|||||||
async def test_invalidate_forever_logs_and_recovers_from_errors(
|
async def test_invalidate_forever_logs_and_recovers_from_errors(
|
||||||
settings: Settings, mocker: MockerFixture
|
settings: Settings, mocker: MockerFixture
|
||||||
):
|
):
|
||||||
test_cache = Cache(interval=0)
|
test_cache = Cache()
|
||||||
logger_error = mocker.patch("lnbits.utils.cache.logger.error")
|
|
||||||
original_running = settings.lnbits_running
|
original_running = settings.lnbits_running
|
||||||
calls = 0
|
calls = 0
|
||||||
|
|
||||||
async def fake_sleep(_interval):
|
original_invalidate = test_cache.invalidate_cache
|
||||||
|
|
||||||
|
async def fake_invalidate():
|
||||||
nonlocal calls
|
nonlocal calls
|
||||||
calls += 1
|
calls += 1
|
||||||
if calls == 1:
|
if calls == 1:
|
||||||
raise RuntimeError("boom")
|
raise RuntimeError("boom")
|
||||||
settings.lnbits_running = False
|
settings.lnbits_running = False
|
||||||
|
await original_invalidate()
|
||||||
|
|
||||||
|
mocker.patch.object(test_cache, "invalidate_cache", side_effect=fake_invalidate)
|
||||||
|
mocker.patch("lnbits.task_manager.asyncio.sleep")
|
||||||
|
logger_error = mocker.patch("lnbits.task_manager.logger.error")
|
||||||
|
|
||||||
|
bg_task = None
|
||||||
try:
|
try:
|
||||||
settings.lnbits_running = True
|
settings.lnbits_running = True
|
||||||
mocker.patch("lnbits.utils.cache.asyncio.sleep", side_effect=fake_sleep)
|
bg_task = task_manager.create_permanent_task(test_cache.invalidate_cache)
|
||||||
await test_cache.invalidate_forever()
|
await bg_task.task
|
||||||
finally:
|
finally:
|
||||||
settings.lnbits_running = original_running
|
settings.lnbits_running = original_running
|
||||||
|
if bg_task:
|
||||||
|
task_manager.cancel_task(bg_task)
|
||||||
|
|
||||||
logger_error.assert_called_once_with("Error invalidating cache")
|
assert logger_error.called
|
||||||
|
assert calls == 2
|
||||||
|
|||||||
@@ -275,6 +275,10 @@ async def test_btc_rates_skips_unsupported_and_failing_providers(
|
|||||||
|
|
||||||
@pytest.mark.anyio
|
@pytest.mark.anyio
|
||||||
async def test_btc_price_handles_empty_single_and_multiple_rates(mocker: MockerFixture):
|
async def test_btc_price_handles_empty_single_and_multiple_rates(mocker: MockerFixture):
|
||||||
|
mocker.patch(
|
||||||
|
"lnbits.utils.exchange_rates.btc_price_from_aggregator",
|
||||||
|
AsyncMock(return_value=None),
|
||||||
|
)
|
||||||
mocker.patch("lnbits.utils.exchange_rates.btc_rates", AsyncMock(return_value=[]))
|
mocker.patch("lnbits.utils.exchange_rates.btc_rates", AsyncMock(return_value=[]))
|
||||||
assert await btc_price("usd") == 0.0
|
assert await btc_price("usd") == 0.0
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ from unittest.mock import AsyncMock
|
|||||||
import pytest
|
import pytest
|
||||||
from pytest_mock.plugin import MockerFixture
|
from pytest_mock.plugin import MockerFixture
|
||||||
|
|
||||||
from lnbits.core.crud.payments import get_payments
|
from lnbits.core.crud.payments import get_payment, get_payments
|
||||||
from lnbits.core.crud.users import get_user
|
from lnbits.core.crud.users import get_user
|
||||||
from lnbits.core.crud.wallets import create_wallet
|
from lnbits.core.crud.wallets import create_wallet
|
||||||
from lnbits.core.models.payments import CreateInvoice, Payment, PaymentState
|
from lnbits.core.models.payments import CreateInvoice, Payment, PaymentState
|
||||||
@@ -919,8 +919,10 @@ async def test_revolut_wallet_create_subscription(settings: Settings):
|
|||||||
"PLAN_VARIATION_123", 1, payment_options
|
"PLAN_VARIATION_123", 1, payment_options
|
||||||
)
|
)
|
||||||
|
|
||||||
|
subscription_request_id = payment_options.subscription_request_id
|
||||||
assert response.ok is True
|
assert response.ok is True
|
||||||
assert response.subscription_request_id is not None
|
assert response.subscription_request_id == "SUBSCRIPTION123"
|
||||||
|
assert subscription_request_id is not None
|
||||||
assert (
|
assert (
|
||||||
response.checkout_session_url
|
response.checkout_session_url
|
||||||
== "https://checkout.revolut.com/payment-link/sub_123"
|
== "https://checkout.revolut.com/payment-link/sub_123"
|
||||||
@@ -933,16 +935,14 @@ async def test_revolut_wallet_create_subscription(settings: Settings):
|
|||||||
assert payload["plan_variation_id"] == "PLAN_VARIATION_123"
|
assert payload["plan_variation_id"] == "PLAN_VARIATION_123"
|
||||||
assert payload["customer_id"] == "CUSTOMER123"
|
assert payload["customer_id"] == "CUSTOMER123"
|
||||||
assert client.calls[1][1]["timeout"] == 30
|
assert client.calls[1][1]["timeout"] == 30
|
||||||
assert client.calls[1][1]["headers"]["Idempotency-Key"] == (
|
assert client.calls[1][1]["headers"]["Idempotency-Key"] == (subscription_request_id)
|
||||||
response.subscription_request_id
|
|
||||||
)
|
|
||||||
assert payload["setup_order_redirect_url"] == (
|
assert payload["setup_order_redirect_url"] == (
|
||||||
"https://lnbits.example/subscription-success"
|
"https://lnbits.example/subscription-success"
|
||||||
)
|
)
|
||||||
reference = json.loads(payload["external_reference"])
|
reference = json.loads(payload["external_reference"])
|
||||||
assert reference["wallet_id"] == "wallet_1"
|
assert reference["wallet_id"] == "wallet_1"
|
||||||
assert reference["tag"] == "gold"
|
assert reference["tag"] == "gold"
|
||||||
assert reference["subscription_request_id"] == response.subscription_request_id
|
assert reference["subscription_request_id"] == subscription_request_id
|
||||||
assert reference["memo"] == "Monthly Gold"
|
assert reference["memo"] == "Monthly Gold"
|
||||||
assert reference["extra"]["link"] == "link-1"
|
assert reference["extra"]["link"] == "link-1"
|
||||||
assert client.calls[2][0] == "/api/orders/ORDER123"
|
assert client.calls[2][0] == "/api/orders/ORDER123"
|
||||||
@@ -1235,13 +1235,55 @@ async def test_revolut_wallet_cancel_subscription(settings: Settings):
|
|||||||
settings.revolut_api_version = "2026-04-20"
|
settings.revolut_api_version = "2026-04-20"
|
||||||
|
|
||||||
wallet = RevolutWallet()
|
wallet = RevolutWallet()
|
||||||
client = MockHTTPClient([MockHTTPResponse(json_data={})])
|
client = MockHTTPClient(
|
||||||
|
[
|
||||||
|
MockHTTPResponse(
|
||||||
|
json_data={
|
||||||
|
"external_reference": json.dumps({"wallet_id": "wallet_1"}),
|
||||||
|
}
|
||||||
|
),
|
||||||
|
MockHTTPResponse(json_data={}),
|
||||||
|
]
|
||||||
|
)
|
||||||
wallet.client = client # type: ignore[assignment]
|
wallet.client = client # type: ignore[assignment]
|
||||||
|
|
||||||
response = await wallet.cancel_subscription("SUBSCRIPTION123", "wallet_1")
|
response = await wallet.cancel_subscription("SUBSCRIPTION123", "wallet_1")
|
||||||
|
|
||||||
assert response.ok is True
|
assert response.ok is True
|
||||||
assert client.calls[0][0] == "/api/subscriptions/SUBSCRIPTION123/cancel"
|
assert client.calls[0][0] == "/api/subscriptions/SUBSCRIPTION123"
|
||||||
|
assert client.calls[1][0] == "/api/subscriptions/SUBSCRIPTION123/cancel"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_revolut_wallet_cancel_subscription_checks_wallet_id(
|
||||||
|
settings: Settings,
|
||||||
|
):
|
||||||
|
settings.revolut_api_endpoint = "https://sandbox-merchant.revolut.com"
|
||||||
|
settings.revolut_api_secret_key = "revolut-secret"
|
||||||
|
settings.revolut_api_version = "2026-04-20"
|
||||||
|
|
||||||
|
wallet = RevolutWallet()
|
||||||
|
client = MockHTTPClient(
|
||||||
|
[
|
||||||
|
MockHTTPResponse(
|
||||||
|
json_data={
|
||||||
|
"external_reference": json.dumps({"wallet_id": "wallet_2"}),
|
||||||
|
}
|
||||||
|
),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
wallet.client = client # type: ignore[assignment]
|
||||||
|
|
||||||
|
response = await wallet.cancel_subscription("SUBSCRIPTION123", "wallet_1")
|
||||||
|
|
||||||
|
assert response.ok is False
|
||||||
|
assert response.error_message == "Subscription not found."
|
||||||
|
assert client.calls == [
|
||||||
|
(
|
||||||
|
"/api/subscriptions/SUBSCRIPTION123",
|
||||||
|
{"timeout": 30},
|
||||||
|
)
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
@pytest.mark.anyio
|
||||||
@@ -1391,7 +1433,7 @@ def test_check_revolut_signature_multiple_v1_headers():
|
|||||||
check_revolut_signature(payload, sig_header, timestamp, secret)
|
check_revolut_signature(payload, sig_header, timestamp, secret)
|
||||||
|
|
||||||
|
|
||||||
def test_check_revolut_signature_docs_vector():
|
def test_check_revolut_signature_docs_vector(mocker: MockerFixture):
|
||||||
payload = (
|
payload = (
|
||||||
b'{"data":{"id":"645a7696-22f3-aa47-9c74-cbae0449cc46",'
|
b'{"data":{"id":"645a7696-22f3-aa47-9c74-cbae0449cc46",'
|
||||||
b'"new_state":"completed","old_state":"pending",'
|
b'"new_state":"completed","old_state":"pending",'
|
||||||
@@ -1403,9 +1445,14 @@ def test_check_revolut_signature_docs_vector():
|
|||||||
secret = "wsk_r59a4HfWVAKycbCaNO1RvgCJec02gRd8"
|
secret = "wsk_r59a4HfWVAKycbCaNO1RvgCJec02gRd8"
|
||||||
sig = "v1=bca326fb378d0da7f7c490ad584a8106bab9723d8d9cdd0d50b4c5b3be3837c0"
|
sig = "v1=bca326fb378d0da7f7c490ad584a8106bab9723d8d9cdd0d50b4c5b3be3837c0"
|
||||||
|
|
||||||
check_revolut_signature(
|
# This is a fixed vector straight from Revolut's docs, so its timestamp is
|
||||||
payload, sig, timestamp, secret, tolerance_seconds=100000000
|
# necessarily in the past. Freeze time to it instead of growing
|
||||||
|
# tolerance_seconds indefinitely as real time marches on.
|
||||||
|
mocker.patch(
|
||||||
|
"lnbits.core.services.fiat_providers.time.time",
|
||||||
|
return_value=int(timestamp) / 1000,
|
||||||
)
|
)
|
||||||
|
check_revolut_signature(payload, sig, timestamp, secret)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
@pytest.mark.anyio
|
||||||
@@ -1675,7 +1722,9 @@ async def test_check_fiat_status_handles_internal_states(mocker: MockerFixture):
|
|||||||
"lnbits.core.services.fiat_providers.get_fiat_provider",
|
"lnbits.core.services.fiat_providers.get_fiat_provider",
|
||||||
AsyncMock(return_value=provider),
|
AsyncMock(return_value=provider),
|
||||||
)
|
)
|
||||||
queue_put = mocker.patch("lnbits.tasks.internal_invoice_queue.put", AsyncMock())
|
queue_put = mocker.patch(
|
||||||
|
"lnbits.task_manager.task_manager.internal_invoice_queue.put_nowait"
|
||||||
|
)
|
||||||
|
|
||||||
success_status = await check_fiat_status(
|
success_status = await check_fiat_status(
|
||||||
Payment(
|
Payment(
|
||||||
@@ -1692,7 +1741,8 @@ async def test_check_fiat_status_handles_internal_states(mocker: MockerFixture):
|
|||||||
)
|
)
|
||||||
|
|
||||||
assert success_status.success is True
|
assert success_status.success is True
|
||||||
queue_put.assert_awaited_once_with("fiat_pending")
|
queue_put.assert_called_once()
|
||||||
|
assert queue_put.call_args[0][0].checking_id == "fiat_pending"
|
||||||
|
|
||||||
await check_fiat_status(
|
await check_fiat_status(
|
||||||
Payment(
|
Payment(
|
||||||
@@ -1702,13 +1752,59 @@ async def test_check_fiat_status_handles_internal_states(mocker: MockerFixture):
|
|||||||
amount=1000,
|
amount=1000,
|
||||||
fee=0,
|
fee=0,
|
||||||
bolt11="bolt11",
|
bolt11="bolt11",
|
||||||
status=PaymentState.PENDING,
|
status=PaymentState.SUCCESS,
|
||||||
fiat_provider="stripe",
|
fiat_provider="stripe",
|
||||||
extra={"fiat_checking_id": "stripe_checking_id"},
|
extra={"fiat_checking_id": "stripe_checking_id"},
|
||||||
),
|
)
|
||||||
skip_internal_payment_notifications=True,
|
|
||||||
)
|
)
|
||||||
assert queue_put.await_count == 1
|
assert queue_put.call_count == 1
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_check_fiat_status_persists_successful_payment(
|
||||||
|
to_wallet: Wallet, settings: Settings, mocker: MockerFixture
|
||||||
|
):
|
||||||
|
settings.stripe_enabled = True
|
||||||
|
settings.stripe_api_secret_key = "mock_sk_test_4eC39HqLyjWDarjtT1zdp7dc"
|
||||||
|
settings.stripe_limits.service_min_amount_sats = 0
|
||||||
|
settings.stripe_limits.service_max_amount_sats = 0
|
||||||
|
settings.stripe_limits.service_fee_wallet_id = None
|
||||||
|
settings.stripe_limits.service_faucet_wallet_id = None
|
||||||
|
|
||||||
|
invoice_data = CreateInvoice(
|
||||||
|
unit="USD", amount=1.0, memo="Test", fiat_provider="stripe"
|
||||||
|
)
|
||||||
|
fiat_mock_response = FiatInvoiceResponse(
|
||||||
|
ok=True,
|
||||||
|
checking_id=f"session_paid_{get_random_string(10)}",
|
||||||
|
payment_request="https://stripe.com/pay/session_paid",
|
||||||
|
)
|
||||||
|
mocker.patch(
|
||||||
|
"lnbits.fiat.StripeWallet.create_invoice",
|
||||||
|
AsyncMock(return_value=fiat_mock_response),
|
||||||
|
)
|
||||||
|
mocker.patch(
|
||||||
|
"lnbits.utils.exchange_rates.get_fiat_rate_satoshis",
|
||||||
|
AsyncMock(return_value=1000),
|
||||||
|
)
|
||||||
|
payment = await payments.create_fiat_invoice(to_wallet.id, invoice_data)
|
||||||
|
assert payment.status == PaymentState.PENDING
|
||||||
|
|
||||||
|
mocker.patch(
|
||||||
|
"lnbits.fiat.StripeWallet.get_invoice_status",
|
||||||
|
AsyncMock(return_value=FiatPaymentStatus(paid=True)),
|
||||||
|
)
|
||||||
|
queue_put = mocker.patch(
|
||||||
|
"lnbits.task_manager.task_manager.internal_invoice_queue.put_nowait"
|
||||||
|
)
|
||||||
|
|
||||||
|
status = await check_fiat_status(payment)
|
||||||
|
|
||||||
|
assert status.success is True
|
||||||
|
assert payment.status == PaymentState.SUCCESS
|
||||||
|
updated_payment = await get_payment(payment.checking_id)
|
||||||
|
assert updated_payment.status == PaymentState.SUCCESS
|
||||||
|
queue_put.assert_called_once_with(payment)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
@pytest.mark.anyio
|
||||||
|
|||||||
@@ -12,10 +12,12 @@ from lnbits.core.crud import create_wallet, get_standalone_payment, get_wallet
|
|||||||
from lnbits.core.crud.payments import get_payment, get_payments_paginated
|
from lnbits.core.crud.payments import get_payment, get_payments_paginated
|
||||||
from lnbits.core.models import PaymentState, Wallet
|
from lnbits.core.models import PaymentState, Wallet
|
||||||
from lnbits.core.services import create_invoice, create_user_account, pay_invoice
|
from lnbits.core.services import create_invoice, create_user_account, pay_invoice
|
||||||
from lnbits.core.services.payments import update_wallet_balance
|
from lnbits.core.services.payments import (
|
||||||
|
update_wallet_balance,
|
||||||
|
)
|
||||||
from lnbits.exceptions import InvoiceError, PaymentError
|
from lnbits.exceptions import InvoiceError, PaymentError
|
||||||
from lnbits.settings import Settings
|
from lnbits.settings import Settings
|
||||||
from lnbits.tasks import create_task, wait_for_paid_invoices
|
from lnbits.task_manager import task_manager
|
||||||
from lnbits.wallets.base import PaymentResponse
|
from lnbits.wallets.base import PaymentResponse
|
||||||
from lnbits.wallets.fake import FakeWallet
|
from lnbits.wallets.fake import FakeWallet
|
||||||
|
|
||||||
@@ -231,17 +233,31 @@ async def test_notification_for_internal_payment(
|
|||||||
):
|
):
|
||||||
test_name = "test_notification_for_internal_payment"
|
test_name = "test_notification_for_internal_payment"
|
||||||
|
|
||||||
|
# Drain stale items left by session-scoped fixtures (e.g. update_wallet_balance)
|
||||||
|
while not task_manager.internal_invoice_queue.empty():
|
||||||
|
try:
|
||||||
|
task_manager.internal_invoice_queue.get_nowait()
|
||||||
|
except asyncio.QueueEmpty:
|
||||||
|
break
|
||||||
|
|
||||||
on_paid_mock = mocker.AsyncMock()
|
on_paid_mock = mocker.AsyncMock()
|
||||||
create_task(wait_for_paid_invoices(test_name, on_paid_mock)())
|
# create_task(internal_invoice_listener())
|
||||||
|
|
||||||
|
task_manager.register_invoice_listener(on_paid_mock, test_name)
|
||||||
|
|
||||||
payment = await create_invoice(
|
payment = await create_invoice(
|
||||||
wallet_id=to_wallet.id,
|
wallet_id=to_wallet.id,
|
||||||
amount=123,
|
amount=123,
|
||||||
memo=test_name,
|
memo=test_name,
|
||||||
webhook="http://test.404.lnbits.com",
|
webhook="http://test.404.lnbits.com",
|
||||||
)
|
)
|
||||||
await pay_invoice(
|
paid_payment = await pay_invoice(
|
||||||
wallet_id=to_wallet.id, payment_request=payment.bolt11, extra={"tag": "lnurlp"}
|
wallet_id=to_wallet.id, payment_request=payment.bolt11, extra={"tag": "lnurlp"}
|
||||||
)
|
)
|
||||||
|
assert paid_payment.status == PaymentState.SUCCESS.value
|
||||||
|
assert paid_payment.bolt11 == payment.bolt11
|
||||||
|
assert paid_payment.amount == -123_000
|
||||||
|
|
||||||
await asyncio.sleep(1)
|
await asyncio.sleep(1)
|
||||||
|
|
||||||
assert on_paid_mock.call_count == 1
|
assert on_paid_mock.call_count == 1
|
||||||
@@ -251,6 +267,8 @@ async def test_notification_for_internal_payment(
|
|||||||
assert _payment.status == PaymentState.SUCCESS.value
|
assert _payment.status == PaymentState.SUCCESS.value
|
||||||
assert _payment.bolt11 == payment.bolt11
|
assert _payment.bolt11 == payment.bolt11
|
||||||
assert _payment.amount == 123_000
|
assert _payment.amount == 123_000
|
||||||
|
assert _payment.checking_id == payment.checking_id
|
||||||
|
|
||||||
updated_payment = await get_payment(_payment.checking_id)
|
updated_payment = await get_payment(_payment.checking_id)
|
||||||
assert (
|
assert (
|
||||||
updated_payment.webhook_status is not None
|
updated_payment.webhook_status is not None
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
from typing import cast
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
@@ -86,7 +87,9 @@ async def test_get_pr_from_lnurl_success_and_error(mocker: MockerFixture):
|
|||||||
mocker.patch(
|
mocker.patch(
|
||||||
"lnbits.core.services.lnurl.execute_pay_request",
|
"lnbits.core.services.lnurl.execute_pay_request",
|
||||||
mocker.AsyncMock(
|
mocker.AsyncMock(
|
||||||
return_value=LnurlPayActionResponse(pr=LightningInvoice(TEST_BOLT11))
|
return_value=LnurlPayActionResponse(
|
||||||
|
pr=cast(LightningInvoice, LightningInvoice(TEST_BOLT11))
|
||||||
|
)
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -106,7 +109,7 @@ async def test_fetch_lnurl_pay_request_converts_currency_and_stores_paylink(
|
|||||||
):
|
):
|
||||||
pay_response = make_lnurl_pay_response(min_sendable_msat=1, text="Test")
|
pay_response = make_lnurl_pay_response(min_sendable_msat=1, text="Test")
|
||||||
action_response = LnurlPayActionResponse(
|
action_response = LnurlPayActionResponse(
|
||||||
pr=LightningInvoice(TEST_BOLT11), disposable=False
|
pr=cast(LightningInvoice, LightningInvoice(TEST_BOLT11)), disposable=False
|
||||||
)
|
)
|
||||||
mocker.patch(
|
mocker.patch(
|
||||||
"lnbits.core.services.lnurl.fiat_amount_as_satoshis",
|
"lnbits.core.services.lnurl.fiat_amount_as_satoshis",
|
||||||
@@ -143,7 +146,7 @@ async def test_store_paylink_appends_and_updates_existing():
|
|||||||
wallet = await _create_wallet()
|
wallet = await _create_wallet()
|
||||||
pay_response = make_lnurl_pay_response(min_sendable_msat=1, text="Test")
|
pay_response = make_lnurl_pay_response(min_sendable_msat=1, text="Test")
|
||||||
action_response = LnurlPayActionResponse(
|
action_response = LnurlPayActionResponse(
|
||||||
pr=LightningInvoice(TEST_BOLT11), disposable=False
|
pr=cast(LightningInvoice, LightningInvoice(TEST_BOLT11)), disposable=False
|
||||||
)
|
)
|
||||||
|
|
||||||
await store_paylink(
|
await store_paylink(
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ from lnbits.core.services.notifications import (
|
|||||||
send_nostr_notification,
|
send_nostr_notification,
|
||||||
send_nostr_notifications,
|
send_nostr_notifications,
|
||||||
send_notification,
|
send_notification,
|
||||||
|
send_notification_in_background,
|
||||||
send_payment_notification,
|
send_payment_notification,
|
||||||
send_payment_push_notification,
|
send_payment_push_notification,
|
||||||
send_push_notification,
|
send_push_notification,
|
||||||
@@ -117,7 +118,7 @@ async def test_send_admin_and_user_notification_use_expected_targets(
|
|||||||
settings: Settings, mocker: MockerFixture
|
settings: Settings, mocker: MockerFixture
|
||||||
):
|
):
|
||||||
send_mock = mocker.patch(
|
send_mock = mocker.patch(
|
||||||
"lnbits.core.services.notifications.send_notification",
|
"lnbits.core.services.notifications.send_notification_in_background",
|
||||||
mocker.AsyncMock(),
|
mocker.AsyncMock(),
|
||||||
)
|
)
|
||||||
original_chat_id = settings.lnbits_telegram_notifications_chat_id
|
original_chat_id = settings.lnbits_telegram_notifications_chat_id
|
||||||
@@ -159,6 +160,45 @@ async def test_send_admin_and_user_notification_use_expected_targets(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_send_notification_in_background_schedules_notification(
|
||||||
|
mocker: MockerFixture,
|
||||||
|
):
|
||||||
|
scheduled = []
|
||||||
|
|
||||||
|
def create_task(coro):
|
||||||
|
scheduled.append(coro)
|
||||||
|
coro.close()
|
||||||
|
return mocker.Mock()
|
||||||
|
|
||||||
|
create_task_mock = mocker.patch(
|
||||||
|
"lnbits.core.services.notifications.create_task",
|
||||||
|
side_effect=create_task,
|
||||||
|
)
|
||||||
|
send_mock = mocker.patch(
|
||||||
|
"lnbits.core.services.notifications.send_notification",
|
||||||
|
mocker.AsyncMock(),
|
||||||
|
)
|
||||||
|
|
||||||
|
await send_notification_in_background(
|
||||||
|
"chat-id",
|
||||||
|
["alice@example.com"],
|
||||||
|
["admin@example.com"],
|
||||||
|
"hello",
|
||||||
|
"settings_update",
|
||||||
|
)
|
||||||
|
|
||||||
|
create_task_mock.assert_called_once()
|
||||||
|
send_mock.assert_called_once_with(
|
||||||
|
"chat-id",
|
||||||
|
["alice@example.com"],
|
||||||
|
["admin@example.com"],
|
||||||
|
"hello",
|
||||||
|
"settings_update",
|
||||||
|
)
|
||||||
|
assert len(scheduled) == 1
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
@pytest.mark.anyio
|
||||||
async def test_send_notification_uses_available_channels_and_swallows_exceptions(
|
async def test_send_notification_uses_available_channels_and_swallows_exceptions(
|
||||||
settings: Settings, mocker: MockerFixture
|
settings: Settings, mocker: MockerFixture
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ from lnbits.core.services.payments import (
|
|||||||
check_pending_payments,
|
check_pending_payments,
|
||||||
check_time_limit_between_transactions,
|
check_time_limit_between_transactions,
|
||||||
check_transaction_status,
|
check_transaction_status,
|
||||||
|
check_wallet_daily_withdraw_limit,
|
||||||
check_wallet_limits,
|
check_wallet_limits,
|
||||||
create_payment_request,
|
create_payment_request,
|
||||||
get_payments_daily_stats,
|
get_payments_daily_stats,
|
||||||
@@ -197,8 +198,7 @@ async def test_update_wallet_balance_validates_credit_and_debit(
|
|||||||
|
|
||||||
settings.lnbits_wallet_limit_max_balance = 0
|
settings.lnbits_wallet_limit_max_balance = 0
|
||||||
queue_mock = mocker.patch(
|
queue_mock = mocker.patch(
|
||||||
"lnbits.tasks.internal_invoice_queue_put",
|
"lnbits.task_manager.task_manager.internal_invoice_queue.put_nowait",
|
||||||
mocker.AsyncMock(),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
await update_wallet_balance(wallet, 5)
|
await update_wallet_balance(wallet, 5)
|
||||||
@@ -212,7 +212,8 @@ async def test_update_wallet_balance_validates_credit_and_debit(
|
|||||||
]
|
]
|
||||||
assert credit_payments
|
assert credit_payments
|
||||||
assert credit_payments[0].status == PaymentState.SUCCESS
|
assert credit_payments[0].status == PaymentState.SUCCESS
|
||||||
queue_mock.assert_awaited_once_with(credit_payments[0].checking_id)
|
queue_mock.assert_called_once()
|
||||||
|
assert queue_mock.call_args[0][0].checking_id == credit_payments[0].checking_id
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
@pytest.mark.anyio
|
||||||
@@ -248,6 +249,23 @@ async def test_check_wallet_limits_and_time_limit(
|
|||||||
settings.lnbits_wallet_limit_secs_between_trans = original_limit
|
settings.lnbits_wallet_limit_secs_between_trans = original_limit
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_check_wallet_daily_limit_counts_all_daily_payments(settings: Settings):
|
||||||
|
wallet = await _create_wallet()
|
||||||
|
await _create_payment(wallet, amount_msat=-2_000, status=PaymentState.SUCCESS)
|
||||||
|
await _create_payment(wallet, amount_msat=-3_000, status=PaymentState.SUCCESS)
|
||||||
|
|
||||||
|
original_limit = settings.lnbits_wallet_limit_daily_max_withdraw
|
||||||
|
try:
|
||||||
|
settings.lnbits_wallet_limit_daily_max_withdraw = 5
|
||||||
|
with pytest.raises(
|
||||||
|
ValueError, match="Daily withdrawal limit of 5 sats reached."
|
||||||
|
):
|
||||||
|
await check_wallet_daily_withdraw_limit(wallet.id, 1_000)
|
||||||
|
finally:
|
||||||
|
settings.lnbits_wallet_limit_daily_max_withdraw = original_limit
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.anyio
|
@pytest.mark.anyio
|
||||||
async def test_calculate_fiat_amounts_handles_conversion_and_errors(
|
async def test_calculate_fiat_amounts_handles_conversion_and_errors(
|
||||||
mocker: MockerFixture,
|
mocker: MockerFixture,
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
from typing import Any
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
@@ -66,7 +67,7 @@ async def test_create_user_account_no_check_rejects_duplicate_identity_fields(
|
|||||||
existing = _account(**existing_data)
|
existing = _account(**existing_data)
|
||||||
await create_account(existing)
|
await create_account(existing)
|
||||||
|
|
||||||
resolved = {
|
resolved: dict[str, Any] = {
|
||||||
key: (value(existing) if callable(value) else value)
|
key: (value(existing) if callable(value) else value)
|
||||||
for key, value in new_data.items()
|
for key, value in new_data.items()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
from typing import Any
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from pytest_mock.plugin import MockerFixture
|
from pytest_mock.plugin import MockerFixture
|
||||||
|
|
||||||
@@ -14,22 +16,22 @@ from lnbits.settings import (
|
|||||||
set_cli_settings,
|
set_cli_settings,
|
||||||
)
|
)
|
||||||
|
|
||||||
lnurlp_redirect_path = {
|
lnurlp_redirect_path: dict[str, Any] = {
|
||||||
"from_path": "/.well-known/lnurlp",
|
"from_path": "/.well-known/lnurlp",
|
||||||
"redirect_to_path": "/api/v1/well-known",
|
"redirect_to_path": "/api/v1/well-known",
|
||||||
}
|
}
|
||||||
lnurlp_redirect_path_with_headers = {
|
lnurlp_redirect_path_with_headers: dict[str, Any] = {
|
||||||
"from_path": "/.well-known/lnurlp",
|
"from_path": "/.well-known/lnurlp",
|
||||||
"redirect_to_path": "/api/v1/well-known",
|
"redirect_to_path": "/api/v1/well-known",
|
||||||
"header_filters": {"accept": "application/nostr+json"},
|
"header_filters": {"accept": "application/nostr+json"},
|
||||||
}
|
}
|
||||||
|
|
||||||
lnaddress_redirect_path = {
|
lnaddress_redirect_path: dict[str, Any] = {
|
||||||
"from_path": "/.well-known/lnurlp",
|
"from_path": "/.well-known/lnurlp",
|
||||||
"redirect_to_path": "/api/v1/well-known",
|
"redirect_to_path": "/api/v1/well-known",
|
||||||
}
|
}
|
||||||
|
|
||||||
nostrrelay_redirect_path = {
|
nostrrelay_redirect_path: dict[str, Any] = {
|
||||||
"from_path": "/",
|
"from_path": "/",
|
||||||
"redirect_to_path": "/api/v1/relay-info",
|
"redirect_to_path": "/api/v1/relay-info",
|
||||||
"header_filters": {"accept": "application/nostr+json"},
|
"header_filters": {"accept": "application/nostr+json"},
|
||||||
|
|||||||
@@ -2073,7 +2073,7 @@
|
|||||||
{
|
{
|
||||||
"response_type": "json",
|
"response_type": "json",
|
||||||
"response": {
|
"response": {
|
||||||
"settled": true
|
"state": "SETTLED"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@@ -2155,8 +2155,15 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
"lndrest": {
|
"lndrest": {
|
||||||
"description": "lndrest.py doesn't handle the 'failed' status for `get_invoice_status`",
|
"get_invoice_status_endpoint": [
|
||||||
"get_invoice_status_endpoint": []
|
{
|
||||||
|
"description": "error status",
|
||||||
|
"response_type": "json",
|
||||||
|
"response": {
|
||||||
|
"state": "CANCELED"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"alby": {
|
"alby": {
|
||||||
"description": "alby.py doesn't handle the 'failed' status for `get_invoice_status`",
|
"description": "alby.py doesn't handle the 'failed' status for `get_invoice_status`",
|
||||||
@@ -2243,13 +2250,6 @@
|
|||||||
"response_type": "json",
|
"response_type": "json",
|
||||||
"response": {}
|
"response": {}
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"description": "error status",
|
|
||||||
"response_type": "json",
|
|
||||||
"response": {
|
|
||||||
"seetled": false
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"description": "bad json",
|
"description": "bad json",
|
||||||
"response_type": "data",
|
"response_type": "data",
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import os
|
import os
|
||||||
|
from typing import cast
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
@@ -29,7 +30,7 @@ logger.info(f"settings.blink_api_endpoint: {settings.blink_api_endpoint}")
|
|||||||
logger.info(f"settings.blink_token: {settings.blink_token}")
|
logger.info(f"settings.blink_token: {settings.blink_token}")
|
||||||
|
|
||||||
set_funding_source()
|
set_funding_source()
|
||||||
funding_source = get_funding_source()
|
funding_source = cast(BlinkWallet, get_funding_source())
|
||||||
assert isinstance(funding_source, BlinkWallet)
|
assert isinstance(funding_source, BlinkWallet)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user