Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
dni committed Aug 23, 2024
1 parent e58a9c2 commit d55f44b
Show file tree
Hide file tree
Showing 11 changed files with 1,328 additions and 949 deletions.
30 changes: 25 additions & 5 deletions crud.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import json
from typing import List, Optional

import shortuuid
from fastapi import Request
from lnbits.db import Database
from lnbits.helpers import urlsafe_short_hash
from lnurl import encode as lnurl_encode

from lnbits.helpers import urlsafe_short_hash
from .models import CreateLnurldevice, Lnurldevice, LnurldevicePayment

db = Database("ext_lnurldevice")
Expand All @@ -29,7 +29,7 @@ async def create_lnurldevice(data: CreateLnurldevice, req: Request) -> Lnurldevi
+ f"&duration={_extra.duration}"
+ f"&variable={_extra.variable}"
+ f"&comment={_extra.comment}"
+ f"&disabletime=0"
+ "&disabletime=0"
)

await db.execute(
Expand All @@ -42,14 +42,19 @@ async def create_lnurldevice(data: CreateLnurldevice, req: Request) -> Lnurldevi
data.profit,
data.currency,
data.device,
json.dumps(data.extra, default=lambda x: x.dict()) if data.extra != "boltz" else data.extra,
(
json.dumps(data.extra, default=lambda x: x.dict())
if data.extra != "boltz"
else data.extra
),
),
)

device = await get_lnurldevice(lnurldevice_id, req)
assert device, "Lnurldevice was created but could not be retrieved"
return device


async def update_lnurldevice(
lnurldevice_id: str, data: CreateLnurldevice, req: Request
) -> Lnurldevice:
Expand Down Expand Up @@ -82,14 +87,19 @@ async def update_lnurldevice(
data.profit,
data.currency,
data.device,
json.dumps(data.extra, default=lambda x: x.dict()) if data.extra != "boltz" else data.extra,
(
json.dumps(data.extra, default=lambda x: x.dict())
if data.extra != "boltz"
else data.extra
),
lnurldevice_id,
),
)
device = await get_lnurldevice(lnurldevice_id, req)
assert device, "Lnurldevice was updated but could not be retrieved"
return device


async def get_lnurldevice(lnurldevice_id: str, req: Request) -> Optional[Lnurldevice]:
row = await db.fetchone(
"SELECT * FROM lnurldevice.lnurldevice WHERE id = ?", (lnurldevice_id,)
Expand Down Expand Up @@ -145,11 +155,13 @@ async def get_lnurldevices(wallet_ids: List[str], req: Request) -> List[Lnurldev

return devices


async def delete_lnurldevice(lnurldevice_id: str) -> None:
await db.execute(
"DELETE FROM lnurldevice.lnurldevice WHERE id = ?", (lnurldevice_id,)
)


async def create_lnurldevicepayment(
deviceid: str,
payload: Optional[str] = None,
Expand Down Expand Up @@ -182,6 +194,7 @@ async def create_lnurldevicepayment(
assert dpayment, "Couldnt retrieve newly created LnurldevicePayment"
return dpayment


async def update_lnurldevicepayment(
lnurldevicepayment_id: str, **kwargs
) -> LnurldevicePayment:
Expand All @@ -194,6 +207,7 @@ async def update_lnurldevicepayment(
assert dpayment, "Couldnt retrieve update LnurldevicePayment"
return dpayment


async def get_lnurldevicepayment(
lnurldevicepayment_id: str,
) -> Optional[LnurldevicePayment]:
Expand All @@ -203,7 +217,10 @@ async def get_lnurldevicepayment(
)
return LnurldevicePayment(**row) if row else None

async def get_lnurldevicepayments(lnurldevice_ids: List[str]) -> List[LnurldevicePayment]:

async def get_lnurldevicepayments(
lnurldevice_ids: List[str],
) -> List[LnurldevicePayment]:
q = ",".join(["?"] * len(lnurldevice_ids))
rows = await db.fetchall(
f"""
Expand All @@ -214,6 +231,7 @@ async def get_lnurldevicepayments(lnurldevice_ids: List[str]) -> List[Lnurldevic
)
return [LnurldevicePayment(**row) for row in rows]


async def get_lnurldevicepayment_by_p(
p: str,
) -> Optional[LnurldevicePayment]:
Expand All @@ -233,6 +251,7 @@ async def get_lnurlpayload(
)
return LnurldevicePayment(**row) if row else None


async def get_recent_lnurldevicepayment(
p: str,
) -> Optional[LnurldevicePayment]:
Expand All @@ -242,6 +261,7 @@ async def get_recent_lnurldevicepayment(
)
return LnurldevicePayment(**row) if row else None


async def delete_atm_payment_link(atm_id: str) -> None:
await db.execute(
"DELETE FROM lnurldevice.lnurldevicepayment WHERE id = ?", (atm_id,)
Expand Down
31 changes: 24 additions & 7 deletions helpers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from io import BytesIO
from embit import compact
import base64
import hmac
from io import BytesIO

from embit import compact
from lnbits.utils.exchange_rates import fiat_amount_as_satoshis
from .crud import get_recent_lnurldevicepayment, create_lnurldevicepayment

from .crud import create_lnurldevicepayment, get_recent_lnurldevicepayment


async def register_atm_payment(device, p):
"""
Expand All @@ -14,17 +17,31 @@ async def register_atm_payment(device, p):
if lnurldevicepayment and lnurldevicepayment.payload == lnurldevicepayment.payhash:
return None, None
# If the payment is already registered and not been paid, return lnurlpayment record
elif lnurldevicepayment and lnurldevicepayment.payload != lnurldevicepayment.payhash:
elif (
lnurldevicepayment and lnurldevicepayment.payload != lnurldevicepayment.payhash
):
return lnurldevicepayment, None
# else create a new lnurlpayment record
else:
data = base64.urlsafe_b64decode(p)
decrypted = xor_decrypt(device.key.encode(), data)
price_msat = await fiat_amount_as_satoshis(float(decrypted[1]) / 100, device.currency) * 1000 if device.currency != "sat" else decrypted[1] * 1000
price_msat = (
await fiat_amount_as_satoshis(float(decrypted[1]) / 100, device.currency)
* 1000
if device.currency != "sat"
else decrypted[1] * 1000
)
price_msat = int(price_msat * ((device.profit / 100) + 1))
lnurldevicepayment = await create_lnurldevicepayment(deviceid=device.id, payload=p, sats=price_msat / 1000, pin=decrypted[0], payhash="payment_hash")
lnurldevicepayment = await create_lnurldevicepayment(
deviceid=device.id,
payload=p,
sats=price_msat / 1000,
pin=decrypted[0],
payhash="payment_hash",
)
return lnurldevicepayment, price_msat


def xor_decrypt(key, blob):
s = BytesIO(blob)
variant = s.read(1)[0]
Expand Down Expand Up @@ -60,4 +77,4 @@ def xor_decrypt(key, blob):
s = BytesIO(payload)
pin = compact.read_from(s)
amount_in_cent = compact.read_from(s)
return str(pin), amount_in_cent
return str(pin), amount_in_cent
3 changes: 2 additions & 1 deletion migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,9 @@ async def m005_redux(db):
# drop old table columns
await db.execute(f"DROP TABLE {old_db}")


async def m006_redux(db):
# Rename switches so we can also use for atm
await db.execute(
"ALTER TABLE lnurldevice.lnurldevice RENAME COLUMN switches TO extra"
)
)
3 changes: 2 additions & 1 deletion models.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,6 @@ class LnurldevicePayment(BaseModel):
def from_row(cls, row: Row) -> "LnurldevicePayment":
return cls(**dict(row))


class Lnurlencode(BaseModel):
url: str
url: str
Loading

0 comments on commit d55f44b

Please sign in to comment.