@@ -65,6 +65,7 @@ class ExtensionHostAPI:
|
|||||||
access_token: str | None = None,
|
access_token: str | None = None,
|
||||||
context: str = "user",
|
context: str = "user",
|
||||||
owner_id: str | None = None,
|
owner_id: str | None = None,
|
||||||
|
wallet_id: str | None = None,
|
||||||
invocation_id: str | None = None,
|
invocation_id: str | None = None,
|
||||||
runtime_limits: dict[str, int] | None = None,
|
runtime_limits: dict[str, int] | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
@@ -74,6 +75,7 @@ class ExtensionHostAPI:
|
|||||||
self.access_token = access_token
|
self.access_token = access_token
|
||||||
self.context = context
|
self.context = context
|
||||||
self.owner_id = sha256s(user_id) if user_id else owner_id
|
self.owner_id = sha256s(user_id) if user_id else owner_id
|
||||||
|
self.wallet_id = wallet_id
|
||||||
self.invocation_id = invocation_id
|
self.invocation_id = invocation_id
|
||||||
self.runtime_limits = runtime_limits or {}
|
self.runtime_limits = runtime_limits or {}
|
||||||
from .utils import ExtensionAPIUtils
|
from .utils import ExtensionAPIUtils
|
||||||
@@ -379,15 +381,19 @@ class ExtensionHostAPI:
|
|||||||
from lnbits.core.services.payments import pay_invoice
|
from lnbits.core.services.payments import pay_invoice
|
||||||
from lnbits.exceptions import PaymentError
|
from lnbits.exceptions import PaymentError
|
||||||
|
|
||||||
if not self.user_id:
|
wallet = await get_wallet(request.wallet_id)
|
||||||
|
if wallet is None:
|
||||||
|
raise PermissionError("Paying invoices from this wallet is not allowed.")
|
||||||
|
if self.user_id:
|
||||||
|
if wallet.user != self.user_id:
|
||||||
|
raise PermissionError(
|
||||||
|
"Paying invoices from this wallet is not allowed."
|
||||||
|
)
|
||||||
|
elif not (self.context == "event" and request.wallet_id == self.wallet_id):
|
||||||
raise PermissionError(
|
raise PermissionError(
|
||||||
"Paying an invoice requires an authenticated user context."
|
"Paying an invoice requires an authenticated user context."
|
||||||
)
|
)
|
||||||
|
|
||||||
wallet = await get_wallet(request.wallet_id)
|
|
||||||
if wallet is None or wallet.user != self.user_id:
|
|
||||||
raise PermissionError("Paying invoices from this wallet is not allowed.")
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
payment_request = request.payment_request
|
payment_request = request.payment_request
|
||||||
if _looks_like_lnurl_pay_target(payment_request):
|
if _looks_like_lnurl_pay_target(payment_request):
|
||||||
|
|||||||
@@ -83,6 +83,7 @@ async def invoke_wasm_extension_export(
|
|||||||
access_token=access_token,
|
access_token=access_token,
|
||||||
context=context,
|
context=context,
|
||||||
owner_id=owner_id,
|
owner_id=owner_id,
|
||||||
|
wallet_id=wallet_id,
|
||||||
invocation_id=invocation.id,
|
invocation_id=invocation.id,
|
||||||
runtime_limits=limits,
|
runtime_limits=limits,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -50,3 +50,51 @@ async def test_wasm_wallet_pay_invoice_resolves_ln_address(mocker):
|
|||||||
assert calls["lnurl"] == ("alice@example.com", 21_000, "winner")
|
assert calls["lnurl"] == ("alice@example.com", 21_000, "winner")
|
||||||
assert calls["pay_invoice"]["payment_request"] == "lnbc1resolved"
|
assert calls["pay_invoice"]["payment_request"] == "lnbc1resolved"
|
||||||
assert calls["pay_invoice"]["max_sat"] == 21
|
assert calls["pay_invoice"]["max_sat"] == 21
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.anyio
|
||||||
|
async def test_wasm_wallet_pay_invoice_allows_event_wallet_only(mocker):
|
||||||
|
async def get_wallet(wallet_id: str):
|
||||||
|
return SimpleNamespace(user="other-user")
|
||||||
|
|
||||||
|
async def pay_invoice(**kwargs):
|
||||||
|
return SimpleNamespace(
|
||||||
|
checking_id="checking",
|
||||||
|
payment_hash="hash",
|
||||||
|
status="success",
|
||||||
|
amount=-1_000,
|
||||||
|
fee=0,
|
||||||
|
pending=False,
|
||||||
|
success=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
mocker.patch("lnbits.core.crud.wallets.get_wallet", get_wallet)
|
||||||
|
mocker.patch("lnbits.core.services.payments.pay_invoice", pay_invoice)
|
||||||
|
|
||||||
|
api = ExtensionHostAPI(
|
||||||
|
"demoext",
|
||||||
|
["wallet.pay_invoice"],
|
||||||
|
context="event",
|
||||||
|
owner_id="owner",
|
||||||
|
wallet_id="wallet1",
|
||||||
|
)
|
||||||
|
allowed = await api.wallet_pay_invoice(
|
||||||
|
PayInvoiceRequest(
|
||||||
|
wallet_id="wallet1",
|
||||||
|
payment_request="lnbc1invoice",
|
||||||
|
max_sat=None,
|
||||||
|
description="",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
assert allowed.ok is True
|
||||||
|
|
||||||
|
with pytest.raises(PermissionError, match="authenticated user context"):
|
||||||
|
await api.wallet_pay_invoice(
|
||||||
|
PayInvoiceRequest(
|
||||||
|
wallet_id="wallet2",
|
||||||
|
payment_request="lnbc1invoice",
|
||||||
|
max_sat=None,
|
||||||
|
description="",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user