Skip to content

Commit

Permalink
feat: add optional password to bitcoinswitch
Browse files Browse the repository at this point in the history
  • Loading branch information
dni committed Jan 20, 2025
1 parent 6f1f0c2 commit 01da07d
Show file tree
Hide file tree
Showing 7 changed files with 510 additions and 460 deletions.
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
8 changes: 8 additions & 0 deletions migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,11 @@ async def m001_initial(db):
);
"""
)

async def m002_add_password(db):
await db.execute(
f"""
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

0 comments on commit 01da07d

Please sign in to comment.