Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add optional password to bitcoinswitch #6

Merged
merged 3 commits into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ async def create_bitcoinswitch(
wallet=data.wallet,
currency=data.currency,
switches=data.switches,
password=data.password,
)
await db.insert("bitcoinswitch.switch", device)
return device
Expand Down
9 changes: 9 additions & 0 deletions migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,12 @@ async def m001_initial(db):
);
"""
)


async def m002_add_password(db):
await db.execute(
"""
ALTER TABLE bitcoinswitch.switch
ADD COLUMN password TEXT;
"""
)
2 changes: 2 additions & 0 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class CreateBitcoinswitch(BaseModel):
wallet: str
currency: str
switches: list[Switch]
password: Optional[str] = None


class Bitcoinswitch(BaseModel):
Expand All @@ -44,6 +45,7 @@ class Bitcoinswitch(BaseModel):
currency: str
key: str
switches: list[Switch]
password: Optional[str] = None
created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))
updated_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc))

Expand Down
938 changes: 478 additions & 460 deletions poetry.lock

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
from lnbits.core.models import Payment
from lnbits.core.services import websocket_updater
from lnbits.tasks import register_invoice_listener
from loguru import logger

from .crud import (
get_bitcoinswitch,
get_bitcoinswitch_payment,
update_bitcoinswitch_payment,
)
Expand All @@ -28,6 +30,11 @@ async def on_invoice_paid(payment: Payment) -> None:
if not bitcoinswitch_payment or bitcoinswitch_payment.payment_hash == "paid":
return

bitcoinswitch = await get_bitcoinswitch(bitcoinswitch_payment.bitcoinswitch_id)
if not bitcoinswitch:
logger.error("no bitcoinswitch found for payment.")
return

bitcoinswitch_payment.payment_hash = bitcoinswitch_payment.payload
bitcoinswitch_payment = await update_bitcoinswitch_payment(bitcoinswitch_payment)
payload = bitcoinswitch_payment.payload
Expand All @@ -44,6 +51,11 @@ async def on_invoice_paid(payment: Payment) -> None:
if comment:
payload = f"{payload}-{comment}"

if bitcoinswitch.password and bitcoinswitch.password != comment:
# Wrong password
logger.info(f"Wrong password entered for bitcoin switch: {bitcoinswitch.id}")
return

return await websocket_updater(
bitcoinswitch_payment.bitcoinswitch_id,
payload,
Expand Down
8 changes: 8 additions & 0 deletions templates/bitcoinswitch/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,14 @@ <h5 v-html="formDialog.data.device" v-if="formDialog.data.id"></h5>
label="Fiat currency for device"
:options="currency"
></q-select>
<q-input
filled
dense
v-model.trim="formDialog.data.password"
type="text"
label="Password (optional)"
tooltip="Password to protect the device, needs to be entered as comment in the LNURL"
></q-input>
<q-btn
unelevated
class="q-mb-lg"
Expand Down
1 change: 1 addition & 0 deletions views_lnurl.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ async def lnurl_params(
"callback": f"{url}?variable={variable}",
"minSendable": price_msat,
"maxSendable": price_msat,
"commentAllowed": 255,
"metadata": switch.lnurlpay_metadata,
}
if comment:
Expand Down
Loading