+3








bc81dc5a28
* [FIX] correct amount in fiat tracking (#1934)
* better debug log
* fix: convert msat to sat for fiat amount
* [CHORE] update to version 0.11.0 (#1933)
* [FIX] codeql for dev branch (#1935)
* [FEAT] create data folder if it doesnt exist (#1930)
* [FEAT] improve update_admin_settings (#1903)
* [FEAT] improve update_admin_settings
while working on the push notification pr i found it very hard just to update
2 settings inside the db, so i improved upon update_admin_settings.
now you just need to provide a dict with key/values you want to update inside db.
also debugging the endpoints for update_settings i found despite the type of `EditableSettings`
fastapi did in fact pass a dict.
* t
* use `EditableSettings` as param in update_settings
* fix settings model validation
we previously overrode the pydantic validation with our own method
* make `LnbitsSettings` a `BaseModel` and only add `BaseSettings` later
this allows us to instantiate `EditableSettings` without the environment values being loaded in
* add test
* forbid extra fields in update api
* fixup
* add test
* test datadir
* move UpdateSettings
* fix compat
* fixup webpush
---------
Co-authored-by: jacksn <jkranawetter05@gmail.com>
* [TEST] use clean db in postgres tests (#1928)
* [CORE] Document CoreLightningRestWallet (#1920)
* document CoreLightningRestWallet
* pushed to comment
* fixed base64 comment
---------
Co-authored-by: Ben <ben@arc.wales>
* [TEST] use test data as mockdata (#1929)
* add db group to cli
* delete mock_data.zip
* generate migration data through tests
* [BUG] self payments for the same wallet (#1914)
* [BUG] self payments for fakewallet
make it possible to pay to yourself on fakewallet
f
* bugfix selfpayments for fakewallet
* delete by wallet and fix previous_payment check (#1919)
---------
Co-authored-by: callebtc <93376500+callebtc@users.noreply.github.com>
* [REFACTOR] core/__init__ to not have circular import issues (#1876)
* F541 fix
remove unused workflow and combine linters into one
add lnbits/static to ruff ignore
remote setupnode
ignore upgrades for mypy
ignore F401 for __init__ files
unused noqa
ignore upgrades for black
F821: undefine name
disabled and logged webhook_listener for opennode and lnpay because they are obvisouly not working
E402: module level import not at top of file
fixup
revert breaking changes wait for PR #1876
https://github.com/lnbits/lnbits/pull/1876
E721 fixes, only popped up for python3.9 not 3.10
[REFACTOR] core/__init__ to not have circular import issues
WIP
add db for backwards compat
fix pyright
make mypy happy again
pyright did not catch those, i think mypy got confused with relative imports. maybe we should use absolute ones everywhere
E402: module level import not at top of file
dont forget to add core_app
rebase on ruff pr
f
remo
format
* fix clnrest
* ignore E402 in conftest
* refactoring issues
---------
Co-authored-by: jacksn <jkranawetter05@gmail.com>
* make wallets name prettier! (#1918)
* make wallets name prettier!
* Update lnbits/core/templates/admin/index.html
Co-authored-by: dni ⚡ <office@dnilabs.com>
* Update lnbits/core/templates/admin/index.html
Co-authored-by: dni ⚡ <office@dnilabs.com>
---------
Co-authored-by: Arc <33088785+arcbtc@users.noreply.github.com>
Co-authored-by: dni ⚡ <office@dnilabs.com>
* Fix payments chart (#1851)
* feat: payment history endpoint
* test payment history
* use new endpoint in frontend
* refactor tests
* [CHORE] bugfix formatting prettier (#1936)
* do not show deleted wallets (#1942)
* do not show deleted wallets
* rename parameter for clarity
* extension loader: do not panic! (#1945)
* Revert "do not show deleted wallets (#1942)" (#1947)
This reverts commit 1ee2d53069.
* remove LNURL-p description_hash validation (#1951)
* [CHORE] update dependencies, unsafe pip packages, fastapi (#1609)
* d
* middleware needs to be initialised before startup
runs on python3.11
update secp256k1
update psycopg
* update fastapi to v0.103
* fix webpush
* bump dependencies
* ruff issue
* lock versions
* bump versions
---------
Co-authored-by: jacksn <jkranawetter05@gmail.com>
* [FEAT] Focus cursor to textarea (#1959)
* Focus cursor to textarea
Fix need for additional click to textarea before pasting
* [FEAT] autofocus textarea on paste request
closes #1959
* format arba
* bundle
---------
Co-authored-by: dni ⚡ <office@dnilabs.com>
* add editorconfig config, fix issues (only whitespace changes) (#1958)
* adding bolt11 lib and removing bolt11.py from the codebase (#1817)
* add latest bolt11 lib
decode exception handling for create_payment
fix json response for decode
bugfix hexing description hash
improvement on bolt11 lib
update to bolt11 2.0.1
fix clnrest
* bolt 2.0.4
* refactor core/crud.py
* catch bolt11 erxception clnrest
* Updated guide for extension development for current extension management process (#1962)
* Startup optimization: nonblocking expiry check (#1943)
* nonblocking expiry check
* autoruff
* smaller interval
---------
Co-authored-by: dni ⚡ <office@dnilabs.com>
* Support custom path for installed extensions (#1940)
* feat: the more you fuck around the more you learn
* feat: backwards compatible static file loading
* refactor: update paths for extension files
* refactor: var renaming
* doc: update `LNBITS_EXTENSIONS_PATH` documentation
* fix: default folder install
* feat: install ext without external path
* doc: `PYTHONPATH` no longer required
* fix: add warnings
* fix: missing path
* refactor: re-order statements
* fix: hardcoded path separator
---------
Co-authored-by: dni ⚡ <office@dnilabs.com>
* [FEAT] Node Managment (#1895)
* [FEAT] Node Managment
feat: node dashboard channels and transactions
fix: update channel variables
better types
refactor ui
add onchain balances and backend_name
mock values for fake wallet
remove app tab
start implementing peers and channel management
peer and channel management
implement channel closing
add channel states, better errors
seperate payments and invoices on transactions tab
display total channel balance
feat: optional public page
feat: show node address
fix: port conversion
feat: details dialog on transactions
fix: peer info without alias
fix: rename channel balances
small improvements to channels tab
feat: pagination on transactions tab
test caching transactions
refactor: move WALLET into wallets module
fix: backwards compatibility
refactor: move get_node_class to nodes modules
post merge bundle fundle
feat: disconnect peer
feat: initial lnd support
only use filtered channels for total balance
adjust closing logic
add basic node tests
add setting for disabling transactions tab
revert unnecessary changes
add tests for invoices and payments
improve payment and invoice implementations
the previously used invoice fixture has a session scope, but a new invoice is required
tests and bug fixes for channels api
use query instead of body in channel delete
delete requests should generally not use a body
take node id through path instead of body for delete endpoint
add peer management tests
more tests for errors
improve error handling
rename id and pubkey to peer_id for consistency
remove dead code
fix http status codes
make cache keys safer
cache node public info
comments for node settings
rename node prop in frontend
adjust tests to new status codes
cln: use amount_msat instead of value for onchain balance
turn transactions tab off by default
enable transactions in tests
only allow super user to create or delete
fix prop name in admin navbar
---------
Co-authored-by: jacksn <jkranawetter05@gmail.com>
* [FEAT] cleanup GET /wallet endpoint, add wallet api routes (#1932)
* [FEAT] cleanup GET /wallet endpoint, add wallet api route
this removes the functionalitiy to create accounts and wallets via
the GET /wallet endpoint in generic.py
it add endpoints inside the api.py for it and the frontend is modified to use the api endpoints
this also simplifies for the `feat/login` for the route.
* remove stale generic tests and add api tests
* bug wrong endpoint create account
* vlad nitpick
* added checkif deleted is 404
* reload after renaming wallet
* another iteration with vlad
* create new wallet if it none exist
* fix delete refresh
* formatting
* Don't create user if allowed users is set (#1822)
* dont allow creating of users when allowed users are set
* add info to .env.example
---------
Co-authored-by: dni ⚡ <office@dnilabs.com>
* fix typos (#1969)
* fix: node ranks (#1968)
* [BUG] bundle is broken (#1972)
* [BUG] Fix bad transation call (#1973)
* fix: `:label="$('xxx')` to :label="$t('xxx')
* chore: commit bundles
* chore: remove extra file
* fix: catch sse exceptions (#1971)
* Add Markdown capability globally (#1965)
* add markdown capability globally
* add markdown to site description
* add showdown to package.json
remove it from vendor.json, its bundled
* formatting
* io
* Update lnbits/core/templates/core/index.html
---------
Co-authored-by: dni ⚡ <office@dnilabs.com>
* feat: remove commit version (#1980)
* nix: fix nixosModule and build (#1979)
* lnbits/settings: get LNBITS_COMMIT from envvar if available
There is no other way of setting the git commit at runtime, for build systems like Nix that allow you to hermetically define envvars derived from the source that will be available reproducibly at runtime
* nix: update potree2nix and nixosModule
This is a general refactor that gets everything building and passing the vmTest again
* Update nix/modules/lnbits-service.nix
---------
Co-authored-by: Pavol Rusnak <pavol@rusnak.io>
* catch exceptions in migrations for extensions (#1987)
* fix: admin_ui js error (#1982)
* fix: admin_ui js error
undefined formData.lnbits_allowed_currencies JS error in console
* vlad suggestion
* update: to 0.11.1
* revert: update
---------
Co-authored-by: jackstar12 <62219658+jackstar12@users.noreply.github.com>
Co-authored-by: dni ⚡ <office@dnilabs.com>
Co-authored-by: jacksn <jkranawetter05@gmail.com>
Co-authored-by: Ben <ben@arc.wales>
Co-authored-by: Tiago Vasconcelos <talvasconcelos@gmail.com>
Co-authored-by: Arc <33088785+arcbtc@users.noreply.github.com>
Co-authored-by: arbadacarba <63317640+arbadacarbaYK@users.noreply.github.com>
Co-authored-by: Pavol Rusnak <pavol@rusnak.io>
Co-authored-by: blackcoffeexbt <87530449+blackcoffeexbt@users.noreply.github.com>
Co-authored-by: Vlad Stan <stan.v.vlad@gmail.com>
Co-authored-by: Matthew Croughan <matt@croughan.sh>
653 lines
23 KiB
Python
653 lines
23 KiB
Python
import asyncio
|
|
import json
|
|
from io import BytesIO
|
|
from pathlib import Path
|
|
from typing import Dict, List, Optional, Tuple, TypedDict
|
|
from urllib.parse import parse_qs, urlparse
|
|
|
|
import httpx
|
|
from cryptography.hazmat.primitives import serialization
|
|
from fastapi import Depends, WebSocket
|
|
from lnurl import LnurlErrorResponse
|
|
from lnurl import decode as decode_lnurl
|
|
from loguru import logger
|
|
from py_vapid import Vapid
|
|
from py_vapid.utils import b64urlencode
|
|
|
|
from lnbits import bolt11
|
|
from lnbits.core.db import db
|
|
from lnbits.db import Connection
|
|
from lnbits.decorators import WalletTypeInfo, require_admin_key
|
|
from lnbits.helpers import url_for
|
|
from lnbits.settings import (
|
|
EditableSettings,
|
|
SuperSettings,
|
|
readonly_variables,
|
|
send_admin_user_to_saas,
|
|
settings,
|
|
)
|
|
from lnbits.utils.exchange_rates import fiat_amount_as_satoshis, satoshis_amount_as_fiat
|
|
from lnbits.wallets import FAKE_WALLET, get_wallet_class, set_wallet_class
|
|
from lnbits.wallets.base import PaymentResponse, PaymentStatus
|
|
|
|
from .crud import (
|
|
check_internal,
|
|
check_internal_pending,
|
|
create_account,
|
|
create_admin_settings,
|
|
create_payment,
|
|
create_wallet,
|
|
delete_wallet_payment,
|
|
get_account,
|
|
get_standalone_payment,
|
|
get_super_settings,
|
|
get_total_balance,
|
|
get_wallet,
|
|
get_wallet_payment,
|
|
update_admin_settings,
|
|
update_payment_details,
|
|
update_payment_status,
|
|
update_super_user,
|
|
)
|
|
from .helpers import to_valid_user_id
|
|
from .models import Payment, Wallet
|
|
|
|
|
|
class PaymentFailure(Exception):
|
|
pass
|
|
|
|
|
|
class InvoiceFailure(Exception):
|
|
pass
|
|
|
|
|
|
async def calculate_fiat_amounts(
|
|
amount: float,
|
|
wallet_id: str,
|
|
currency: Optional[str] = None,
|
|
extra: Optional[Dict] = None,
|
|
conn: Optional[Connection] = None,
|
|
) -> Tuple[int, Optional[Dict]]:
|
|
wallet = await get_wallet(wallet_id, conn=conn)
|
|
assert wallet, "invalid wallet_id"
|
|
wallet_currency = wallet.currency or settings.lnbits_default_accounting_currency
|
|
|
|
if currency and currency != "sat":
|
|
amount_sat = await fiat_amount_as_satoshis(amount, currency)
|
|
extra = extra or {}
|
|
if currency != wallet_currency:
|
|
extra["fiat_currency"] = currency
|
|
extra["fiat_amount"] = round(amount, ndigits=3)
|
|
extra["fiat_rate"] = amount_sat / amount
|
|
else:
|
|
amount_sat = int(amount)
|
|
|
|
if wallet_currency:
|
|
if wallet_currency == currency:
|
|
fiat_amount = amount
|
|
else:
|
|
fiat_amount = await satoshis_amount_as_fiat(amount_sat, wallet_currency)
|
|
extra = extra or {}
|
|
extra["wallet_fiat_currency"] = wallet_currency
|
|
extra["wallet_fiat_amount"] = round(fiat_amount, ndigits=3)
|
|
extra["wallet_fiat_rate"] = amount_sat / fiat_amount
|
|
|
|
logger.debug(
|
|
f"Calculated fiat amounts {wallet.id=} {amount=} {currency=}: {extra=}"
|
|
)
|
|
|
|
return amount_sat, extra
|
|
|
|
|
|
async def create_invoice(
|
|
*,
|
|
wallet_id: str,
|
|
amount: float,
|
|
currency: Optional[str] = "sat",
|
|
memo: str,
|
|
description_hash: Optional[bytes] = None,
|
|
unhashed_description: Optional[bytes] = None,
|
|
expiry: Optional[int] = None,
|
|
extra: Optional[Dict] = None,
|
|
webhook: Optional[str] = None,
|
|
internal: Optional[bool] = False,
|
|
conn: Optional[Connection] = None,
|
|
) -> Tuple[str, str]:
|
|
if not amount > 0:
|
|
raise InvoiceFailure("Amountless invoices not supported.")
|
|
|
|
if await get_wallet(wallet_id, conn=conn) is None:
|
|
raise InvoiceFailure("Wallet does not exist.")
|
|
|
|
invoice_memo = None if description_hash else memo
|
|
|
|
# use the fake wallet if the invoice is for internal use only
|
|
wallet = FAKE_WALLET if internal else get_wallet_class()
|
|
|
|
amount_sat, extra = await calculate_fiat_amounts(
|
|
amount, wallet_id, currency=currency, extra=extra, conn=conn
|
|
)
|
|
|
|
ok, checking_id, payment_request, error_message = await wallet.create_invoice(
|
|
amount=amount_sat,
|
|
memo=invoice_memo,
|
|
description_hash=description_hash,
|
|
unhashed_description=unhashed_description,
|
|
expiry=expiry or settings.lightning_invoice_expiry,
|
|
)
|
|
if not ok or not payment_request or not checking_id:
|
|
raise InvoiceFailure(error_message or "unexpected backend error.")
|
|
|
|
invoice = bolt11.decode(payment_request)
|
|
|
|
amount_msat = 1000 * amount_sat
|
|
await create_payment(
|
|
wallet_id=wallet_id,
|
|
checking_id=checking_id,
|
|
payment_request=payment_request,
|
|
payment_hash=invoice.payment_hash,
|
|
amount=amount_msat,
|
|
memo=memo,
|
|
extra=extra,
|
|
webhook=webhook,
|
|
conn=conn,
|
|
)
|
|
|
|
return invoice.payment_hash, payment_request
|
|
|
|
|
|
async def pay_invoice(
|
|
*,
|
|
wallet_id: str,
|
|
payment_request: str,
|
|
max_sat: Optional[int] = None,
|
|
extra: Optional[Dict] = None,
|
|
description: str = "",
|
|
conn: Optional[Connection] = None,
|
|
) -> str:
|
|
"""
|
|
Pay a Lightning invoice.
|
|
First, we create a temporary payment in the database with fees set to the reserve
|
|
fee. We then check whether the balance of the payer would go negative.
|
|
We then attempt to pay the invoice through the backend. If the payment is
|
|
successful, we update the payment in the database with the payment details.
|
|
If the payment is unsuccessful, we delete the temporary payment.
|
|
If the payment is still in flight, we hope that some other process
|
|
will regularly check for the payment.
|
|
"""
|
|
invoice = bolt11.decode(payment_request)
|
|
|
|
if not invoice.amount_msat or not invoice.amount_msat > 0:
|
|
raise ValueError("Amountless invoices not supported.")
|
|
if max_sat and invoice.amount_msat > max_sat * 1000:
|
|
raise ValueError("Amount in invoice is too high.")
|
|
|
|
fee_reserve_msat = fee_reserve(invoice.amount_msat)
|
|
async with db.reuse_conn(conn) if conn else db.connect() as conn:
|
|
temp_id = invoice.payment_hash
|
|
internal_id = f"internal_{invoice.payment_hash}"
|
|
|
|
if invoice.amount_msat == 0:
|
|
raise ValueError("Amountless invoices not supported.")
|
|
if max_sat and invoice.amount_msat > max_sat * 1000:
|
|
raise ValueError("Amount in invoice is too high.")
|
|
|
|
_, extra = await calculate_fiat_amounts(
|
|
invoice.amount_msat / 1000, wallet_id, extra=extra, conn=conn
|
|
)
|
|
|
|
# put all parameters that don't change here
|
|
class PaymentKwargs(TypedDict):
|
|
wallet_id: str
|
|
payment_request: str
|
|
payment_hash: str
|
|
amount: int
|
|
memo: str
|
|
extra: Optional[Dict]
|
|
|
|
payment_kwargs: PaymentKwargs = PaymentKwargs(
|
|
wallet_id=wallet_id,
|
|
payment_request=payment_request,
|
|
payment_hash=invoice.payment_hash,
|
|
amount=-invoice.amount_msat,
|
|
memo=description or invoice.description or "",
|
|
extra=extra,
|
|
)
|
|
|
|
# we check if an internal invoice exists that has already been paid
|
|
# (not pending anymore)
|
|
if not await check_internal_pending(invoice.payment_hash, conn=conn):
|
|
raise PaymentFailure("Internal invoice already paid.")
|
|
|
|
# check_internal() returns the checking_id of the invoice we're waiting for
|
|
# (pending only)
|
|
internal_checking_id = await check_internal(invoice.payment_hash, conn=conn)
|
|
if internal_checking_id:
|
|
# perform additional checks on the internal payment
|
|
# the payment hash is not enough to make sure that this is the same invoice
|
|
internal_invoice = await get_standalone_payment(
|
|
internal_checking_id, incoming=True, conn=conn
|
|
)
|
|
assert internal_invoice is not None
|
|
if (
|
|
internal_invoice.amount != invoice.amount_msat
|
|
or internal_invoice.bolt11 != payment_request.lower()
|
|
):
|
|
raise PaymentFailure("Invalid invoice.")
|
|
|
|
logger.debug(f"creating temporary internal payment with id {internal_id}")
|
|
# create a new payment from this wallet
|
|
new_payment = await create_payment(
|
|
checking_id=internal_id,
|
|
fee=0,
|
|
pending=False,
|
|
conn=conn,
|
|
**payment_kwargs,
|
|
)
|
|
else:
|
|
logger.debug(f"creating temporary payment with id {temp_id}")
|
|
# create a temporary payment here so we can check if
|
|
# the balance is enough in the next step
|
|
try:
|
|
new_payment = await create_payment(
|
|
checking_id=temp_id,
|
|
fee=-fee_reserve_msat,
|
|
conn=conn,
|
|
**payment_kwargs,
|
|
)
|
|
except Exception as e:
|
|
logger.error(f"could not create temporary payment: {e}")
|
|
# happens if the same wallet tries to pay an invoice twice
|
|
raise PaymentFailure("Could not make payment.")
|
|
|
|
# do the balance check
|
|
wallet = await get_wallet(wallet_id, conn=conn)
|
|
assert wallet, "Wallet for balancecheck could not be fetched"
|
|
if wallet.balance_msat < 0:
|
|
logger.debug("balance is too low, deleting temporary payment")
|
|
if not internal_checking_id and wallet.balance_msat > -fee_reserve_msat:
|
|
raise PaymentFailure(
|
|
f"You must reserve at least ({round(fee_reserve_msat/1000)} sat) to"
|
|
" cover potential routing fees."
|
|
)
|
|
raise PermissionError("Insufficient balance.")
|
|
|
|
if internal_checking_id:
|
|
logger.debug(f"marking temporary payment as not pending {internal_checking_id}")
|
|
# mark the invoice from the other side as not pending anymore
|
|
# so the other side only has access to his new money when we are sure
|
|
# the payer has enough to deduct from
|
|
async with db.connect() as conn:
|
|
await update_payment_status(
|
|
checking_id=internal_checking_id, pending=False, conn=conn
|
|
)
|
|
await send_payment_notification(wallet, new_payment)
|
|
|
|
# notify receiver asynchronously
|
|
from lnbits.tasks import internal_invoice_queue
|
|
|
|
logger.debug(f"enqueuing internal invoice {internal_checking_id}")
|
|
await internal_invoice_queue.put(internal_checking_id)
|
|
else:
|
|
logger.debug(f"backend: sending payment {temp_id}")
|
|
# actually pay the external invoice
|
|
WALLET = get_wallet_class()
|
|
payment: PaymentResponse = await WALLET.pay_invoice(
|
|
payment_request, fee_reserve_msat
|
|
)
|
|
|
|
if payment.checking_id and payment.checking_id != temp_id:
|
|
logger.warning(
|
|
f"backend sent unexpected checking_id (expected: {temp_id} got:"
|
|
f" {payment.checking_id})"
|
|
)
|
|
|
|
logger.debug(f"backend: pay_invoice finished {temp_id}")
|
|
if payment.checking_id and payment.ok is not False:
|
|
# payment.ok can be True (paid) or None (pending)!
|
|
logger.debug(f"updating payment {temp_id}")
|
|
async with db.connect() as conn:
|
|
await update_payment_details(
|
|
checking_id=temp_id,
|
|
pending=payment.ok is not True,
|
|
fee=payment.fee_msat,
|
|
preimage=payment.preimage,
|
|
new_checking_id=payment.checking_id,
|
|
conn=conn,
|
|
)
|
|
wallet = await get_wallet(wallet_id, conn=conn)
|
|
updated = await get_wallet_payment(
|
|
wallet_id, payment.checking_id, conn=conn
|
|
)
|
|
if wallet and updated:
|
|
await send_payment_notification(wallet, updated)
|
|
logger.debug(f"payment successful {payment.checking_id}")
|
|
elif payment.checking_id is None and payment.ok is False:
|
|
# payment failed
|
|
logger.warning("backend sent payment failure")
|
|
async with db.connect() as conn:
|
|
logger.debug(f"deleting temporary payment {temp_id}")
|
|
await delete_wallet_payment(temp_id, wallet_id, conn=conn)
|
|
raise PaymentFailure(
|
|
f"Payment failed: {payment.error_message}"
|
|
or "Payment failed, but backend didn't give us an error message."
|
|
)
|
|
else:
|
|
logger.warning(
|
|
"didn't receive checking_id from backend, payment may be stuck in"
|
|
f" database: {temp_id}"
|
|
)
|
|
|
|
return invoice.payment_hash
|
|
|
|
|
|
async def redeem_lnurl_withdraw(
|
|
wallet_id: str,
|
|
lnurl_request: str,
|
|
memo: Optional[str] = None,
|
|
extra: Optional[Dict] = None,
|
|
wait_seconds: int = 0,
|
|
conn: Optional[Connection] = None,
|
|
) -> None:
|
|
if not lnurl_request:
|
|
return None
|
|
|
|
res = {}
|
|
|
|
async with httpx.AsyncClient() as client:
|
|
lnurl = decode_lnurl(lnurl_request)
|
|
r = await client.get(str(lnurl))
|
|
res = r.json()
|
|
|
|
try:
|
|
_, payment_request = await create_invoice(
|
|
wallet_id=wallet_id,
|
|
amount=int(res["maxWithdrawable"] / 1000),
|
|
memo=memo or res["defaultDescription"] or "",
|
|
extra=extra,
|
|
conn=conn,
|
|
)
|
|
except Exception:
|
|
logger.warning(
|
|
f"failed to create invoice on redeem_lnurl_withdraw "
|
|
f"from {lnurl}. params: {res}"
|
|
)
|
|
return None
|
|
|
|
if wait_seconds:
|
|
await asyncio.sleep(wait_seconds)
|
|
|
|
params = {"k1": res["k1"], "pr": payment_request}
|
|
|
|
try:
|
|
params["balanceNotify"] = url_for(
|
|
f"/withdraw/notify/{urlparse(lnurl_request).netloc}",
|
|
external=True,
|
|
wal=wallet_id,
|
|
)
|
|
except Exception:
|
|
pass
|
|
|
|
async with httpx.AsyncClient() as client:
|
|
try:
|
|
await client.get(res["callback"], params=params)
|
|
except Exception:
|
|
pass
|
|
|
|
|
|
async def perform_lnurlauth(
|
|
callback: str,
|
|
wallet: WalletTypeInfo = Depends(require_admin_key),
|
|
conn: Optional[Connection] = None,
|
|
) -> Optional[LnurlErrorResponse]:
|
|
cb = urlparse(callback)
|
|
|
|
k1 = bytes.fromhex(parse_qs(cb.query)["k1"][0])
|
|
|
|
key = wallet.wallet.lnurlauth_key(cb.netloc)
|
|
|
|
def int_to_bytes_suitable_der(x: int) -> bytes:
|
|
"""for strict DER we need to encode the integer with some quirks"""
|
|
b = x.to_bytes((x.bit_length() + 7) // 8, "big")
|
|
|
|
if len(b) == 0:
|
|
# ensure there's at least one byte when the int is zero
|
|
return bytes([0])
|
|
|
|
if b[0] & 0x80 != 0:
|
|
# ensure it doesn't start with a 0x80 and so it isn't
|
|
# interpreted as a negative number
|
|
return bytes([0]) + b
|
|
|
|
return b
|
|
|
|
def encode_strict_der(r: int, s: int, order: int):
|
|
# if s > order/2 verification will fail sometimes
|
|
# so we must fix it here see:
|
|
# https://github.com/indutny/elliptic/blob/e71b2d9359c5fe9437fbf46f1f05096de447de57/lib/elliptic/ec/index.js#L146-L147
|
|
if s > order // 2:
|
|
s = order - s
|
|
|
|
# now we do the strict DER encoding copied from
|
|
# https://github.com/KiriKiri/bip66 (without any checks)
|
|
r_temp = int_to_bytes_suitable_der(r)
|
|
s_temp = int_to_bytes_suitable_der(s)
|
|
|
|
r_len = len(r_temp)
|
|
s_len = len(s_temp)
|
|
sign_len = 6 + r_len + s_len
|
|
|
|
signature = BytesIO()
|
|
signature.write(0x30.to_bytes(1, "big", signed=False))
|
|
signature.write((sign_len - 2).to_bytes(1, "big", signed=False))
|
|
signature.write(0x02.to_bytes(1, "big", signed=False))
|
|
signature.write(r_len.to_bytes(1, "big", signed=False))
|
|
signature.write(r_temp)
|
|
signature.write(0x02.to_bytes(1, "big", signed=False))
|
|
signature.write(s_len.to_bytes(1, "big", signed=False))
|
|
signature.write(s_temp)
|
|
|
|
return signature.getvalue()
|
|
|
|
sig = key.sign_digest_deterministic(k1, sigencode=encode_strict_der)
|
|
|
|
async with httpx.AsyncClient() as client:
|
|
assert key.verifying_key, "LNURLauth verifying_key does not exist"
|
|
r = await client.get(
|
|
callback,
|
|
params={
|
|
"k1": k1.hex(),
|
|
"key": key.verifying_key.to_string("compressed").hex(),
|
|
"sig": sig.hex(),
|
|
},
|
|
)
|
|
try:
|
|
resp = json.loads(r.text)
|
|
if resp["status"] == "OK":
|
|
return None
|
|
|
|
return LnurlErrorResponse(reason=resp["reason"])
|
|
except (KeyError, json.decoder.JSONDecodeError):
|
|
return LnurlErrorResponse(
|
|
reason=r.text[:200] + "..." if len(r.text) > 200 else r.text
|
|
)
|
|
|
|
|
|
async def check_transaction_status(
|
|
wallet_id: str, payment_hash: str, conn: Optional[Connection] = None
|
|
) -> PaymentStatus:
|
|
payment: Optional[Payment] = await get_wallet_payment(
|
|
wallet_id, payment_hash, conn=conn
|
|
)
|
|
if not payment:
|
|
return PaymentStatus(None)
|
|
if not payment.pending:
|
|
# note: before, we still checked the status of the payment again
|
|
return PaymentStatus(True, fee_msat=payment.fee)
|
|
|
|
status: PaymentStatus = await payment.check_status()
|
|
return status
|
|
|
|
|
|
# WARN: this same value must be used for balance check and passed to
|
|
# WALLET.pay_invoice(), it may cause a vulnerability if the values differ
|
|
def fee_reserve(amount_msat: int) -> int:
|
|
reserve_min = settings.lnbits_reserve_fee_min
|
|
reserve_percent = settings.lnbits_reserve_fee_percent
|
|
return max(int(reserve_min), int(amount_msat * reserve_percent / 100.0))
|
|
|
|
|
|
async def send_payment_notification(wallet: Wallet, payment: Payment):
|
|
await websocketUpdater(
|
|
wallet.id,
|
|
json.dumps(
|
|
{
|
|
"wallet_balance": wallet.balance,
|
|
"payment": payment.dict(),
|
|
}
|
|
),
|
|
)
|
|
|
|
|
|
async def update_wallet_balance(wallet_id: str, amount: int):
|
|
payment_hash, _ = await create_invoice(
|
|
wallet_id=wallet_id,
|
|
amount=amount,
|
|
memo="Admin top up",
|
|
internal=True,
|
|
)
|
|
async with db.connect() as conn:
|
|
checking_id = await check_internal(payment_hash, conn=conn)
|
|
assert checking_id, "newly created checking_id cannot be retrieved"
|
|
await update_payment_status(checking_id=checking_id, pending=False, conn=conn)
|
|
# notify receiver asynchronously
|
|
from lnbits.tasks import internal_invoice_queue
|
|
|
|
await internal_invoice_queue.put(checking_id)
|
|
|
|
|
|
async def check_admin_settings():
|
|
if settings.super_user:
|
|
settings.super_user = to_valid_user_id(settings.super_user).hex
|
|
|
|
if settings.lnbits_admin_ui:
|
|
settings_db = await get_super_settings()
|
|
if not settings_db:
|
|
# create new settings if table is empty
|
|
logger.warning("Settings DB empty. Inserting default settings.")
|
|
settings_db = await init_admin_settings(settings.super_user)
|
|
logger.warning("Initialized settings from environment variables.")
|
|
|
|
if settings.super_user and settings.super_user != settings_db.super_user:
|
|
# .env super_user overwrites DB super_user
|
|
settings_db = await update_super_user(settings.super_user)
|
|
|
|
update_cached_settings(settings_db.dict())
|
|
|
|
# saving superuser to {data_dir}/.super_user file
|
|
with open(Path(settings.lnbits_data_folder) / ".super_user", "w") as file:
|
|
file.write(settings.super_user)
|
|
|
|
# callback for saas
|
|
if (
|
|
settings.lnbits_saas_callback
|
|
and settings.lnbits_saas_secret
|
|
and settings.lnbits_saas_instance_id
|
|
):
|
|
send_admin_user_to_saas()
|
|
|
|
logger.success(
|
|
"✔️ Admin UI is enabled. run `poetry run lnbits-cli superuser` "
|
|
"to get the superuser."
|
|
)
|
|
|
|
|
|
async def check_webpush_settings():
|
|
if not settings.lnbits_webpush_privkey:
|
|
vapid = Vapid()
|
|
vapid.generate_keys()
|
|
privkey = vapid.private_pem()
|
|
assert vapid.public_key, "VAPID public key does not exist"
|
|
pubkey = b64urlencode(
|
|
vapid.public_key.public_bytes(
|
|
serialization.Encoding.X962,
|
|
serialization.PublicFormat.UncompressedPoint,
|
|
)
|
|
)
|
|
push_settings = {
|
|
"lnbits_webpush_privkey": privkey.decode(),
|
|
"lnbits_webpush_pubkey": pubkey,
|
|
}
|
|
update_cached_settings(push_settings)
|
|
await update_admin_settings(EditableSettings(**push_settings))
|
|
|
|
logger.info("Initialized webpush settings with generated VAPID key pair.")
|
|
logger.info(f"Pubkey: {settings.lnbits_webpush_pubkey}")
|
|
|
|
|
|
def update_cached_settings(sets_dict: dict):
|
|
for key, value in sets_dict.items():
|
|
if key not in readonly_variables:
|
|
try:
|
|
setattr(settings, key, value)
|
|
except Exception:
|
|
logger.warning(f"Failed overriding setting: {key}, value: {value}")
|
|
if "super_user" in sets_dict:
|
|
setattr(settings, "super_user", sets_dict["super_user"])
|
|
|
|
|
|
async def init_admin_settings(super_user: Optional[str] = None) -> SuperSettings:
|
|
account = None
|
|
if super_user:
|
|
account = await get_account(super_user)
|
|
if not account:
|
|
account = await create_account(user_id=super_user)
|
|
if not account.wallets or len(account.wallets) == 0:
|
|
await create_wallet(user_id=account.id)
|
|
|
|
editable_settings = EditableSettings.from_dict(settings.dict())
|
|
|
|
return await create_admin_settings(account.id, editable_settings.dict())
|
|
|
|
|
|
class WebsocketConnectionManager:
|
|
def __init__(self) -> None:
|
|
self.active_connections: List[WebSocket] = []
|
|
|
|
async def connect(self, websocket: WebSocket, item_id: str):
|
|
logger.debug(f"Websocket connected to {item_id}")
|
|
await websocket.accept()
|
|
self.active_connections.append(websocket)
|
|
|
|
def disconnect(self, websocket: WebSocket):
|
|
self.active_connections.remove(websocket)
|
|
|
|
async def send_data(self, message: str, item_id: str):
|
|
for connection in self.active_connections:
|
|
if connection.path_params["item_id"] == item_id:
|
|
await connection.send_text(message)
|
|
|
|
|
|
websocketManager = WebsocketConnectionManager()
|
|
|
|
|
|
async def websocketUpdater(item_id, data):
|
|
return await websocketManager.send_data(f"{data}", item_id)
|
|
|
|
|
|
async def switch_to_voidwallet() -> None:
|
|
WALLET = get_wallet_class()
|
|
if WALLET.__class__.__name__ == "VoidWallet":
|
|
return
|
|
set_wallet_class("VoidWallet")
|
|
settings.lnbits_backend_wallet_class = "VoidWallet"
|
|
|
|
|
|
async def get_balance_delta() -> Tuple[int, int, int]:
|
|
WALLET = get_wallet_class()
|
|
total_balance = await get_total_balance()
|
|
error_message, node_balance = await WALLET.status()
|
|
if error_message:
|
|
raise Exception(error_message)
|
|
return node_balance - total_balance, node_balance, total_balance
|