Skip to content

Commit

Permalink
some tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
arcbtc committed Aug 16, 2024
1 parent 4530fe2 commit d19e619
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 36 deletions.
3 changes: 2 additions & 1 deletion helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ async def register_atm_payment(device, p):
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 = 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")
return lnurldevicepayment
return lnurldevicepayment, price_msat

def xor_decrypt(key, blob):
s = BytesIO(blob)
Expand Down
16 changes: 9 additions & 7 deletions templates/lnurldevice/atm.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,16 @@

<q-tab name="ln" label="lightnig" @click="updateTabAndClear(val)"></q-tab>

<q-tab :disable="boltz == 'None' || amount < 100000" name="onchain" label="onchain"
<q-tab :disable="boltz == 'False' || amount < 100000" name="onchain" label="onchain"
@click="updateTabAndClear(val)">
<q-tooltip v-if="amount < 100000">Amount is too small to send over onchain</q-tooltip>
<q-tooltip v-if="boltz == 'True' && amount < 100000">Amount is too small to send over onchain, needs to be 10000+ sats</q-tooltip>
<q-tooltip v-if="boltz == 'False'">Onchain not available</q-tooltip>
</q-tab>

<q-tab :disable="boltz == 'None' || amount < 100000" name="liquid" label="liquid"
<q-tab :disable="boltz == 'False' || amount < 2000" name="liquid" label="liquid"
@click="updateTabAndClear(val)">
<q-tooltip v-if="amount < 100000">Amount is too small to send over onchain</q-tooltip>
<q-tooltip v-if="boltz == 'True' && amount < 2000">Amount is too small to send over liquid, needs to be 2000+ sats</q-tooltip>
<q-tooltip v-if="boltz == 'False'">Onchain not available</q-tooltip>
</q-tab>
</q-tabs>
<q-separator></q-separator>
Expand All @@ -55,7 +57,7 @@
<div class="text-h8">Lightning / LNaddress / LNURL-pay</div>
<q-form @submit="sendLNaddress" class="q-gutter-md">
<q-input filled dense v-model.trim="ln" type="textarea"
:label="'Paste an invoice for ' + amount + ' sats, an LNaddress or LNURL-pay'"
:label="'Paste an LNaddress or LNURL-pay'"
ref="textArea">
</q-input>
<div class="row q-mt-lg">
Expand All @@ -65,7 +67,7 @@
</q-form>
</q-tab-panel>

<q-tab-panel name="onchain" :disable="boltz == 'None'">
<q-tab-panel name="onchain" :disable="boltz === false">
<div class="text-h8">Onchain</div>
<q-form @submit="sendOnchainAddress" class="q-gutter-md">
<q-input filled dense v-model.trim="address" type="textarea"
Expand All @@ -78,7 +80,7 @@
</q-form>
</q-tab-panel>

<q-tab-panel name="liquid" :disable="boltz == 'None'">
<q-tab-panel name="liquid" :disable="boltz === false">
<div class="text-h8">Liquid</div>
<q-form @submit="sendLiquidAddress" class="q-gutter-md">
<q-input filled dense v-model.trim="address" type="textarea" label="Paste a Liquid address"
Expand Down
2 changes: 1 addition & 1 deletion templates/lnurldevice/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ <h5 v-html="formDialoglnurldevice.data.device" v-if="formDialoglnurldevice.data.
</div>
</div>
<q-toggle v-if="formDialoglnurldevice.data.device == 'atm'"
:label="boltzToggleState ? 'Disable onchain/liquid support' : 'Enable onchain/liquid support (boltz ext must be enabled)'"
:label="boltzToggleState ? 'Onchain/liquid support enabled (boltz ext must be enabled)' : 'Onchain/liquid support disabled'"
v-model="boltzToggleState" @input="handleBoltzToggleChange"></q-toggle>
<div class="row q-mt-lg">
<q-btn v-if="formDialoglnurldevice.data.id" unelevated color="primary" :disable="
Expand Down
16 changes: 8 additions & 8 deletions views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
from lnbits.core.models import User
from lnbits.decorators import check_user_exists
from lnbits.lnurl import decode as lnurl_decode
from lnbits.helpers import template_renderer

from . import lnurldevice_ext, lnurldevice_renderer
from .crud import get_lnurldevice, get_lnurldevicepayment, get_recent_lnurldevicepayment
from .views_lnurl import xor_decrypt
from .helpers import xor_decrypt
from urllib.parse import urlparse, parse_qs
from loguru import logger
from lnbits.utils.exchange_rates import fiat_amount_as_satoshis
Expand All @@ -20,7 +20,6 @@
templates = Jinja2Templates(directory="templates")
lnurldevice_generic_router = APIRouter()


def lnurldevice_renderer():
return template_renderer(["lnurldevice/templates"])

Expand All @@ -33,7 +32,7 @@ async def index(request: Request, user: User = Depends(check_user_exists)):
)


@lnurldevice_ext.get("/atm", response_class=HTMLResponse)
@lnurldevice_generic_router.get("/atm", response_class=HTMLResponse)
async def atmpage(request: Request, lightning: str):
# Debug log for the incoming lightning request
logger.debug(lightning)
Expand Down Expand Up @@ -82,6 +81,7 @@ async def atmpage(request: Request, lightning: str):
for extension in installed_extensions:
if extension.id == 'boltz' and extension.active:
access = True
logger.debug(access)

# Attempt to get recent payment information
recentPayAttempt = await get_recent_lnurldevicepayment(p)
Expand All @@ -92,17 +92,17 @@ async def atmpage(request: Request, lightning: str):
{
"request": request,
"lnurl": lightning,
"amount": int(price_msat),
"amount": int(((int(price_msat) / 100) * device.profit) + int(price_msat)),
"device_id": device.id,
"boltz": access or None,
"boltz": True if access else False,
"p": p,
"recentpay": recentPayAttempt.id if recentPayAttempt else False,
"used": True if recentPayAttempt and recentPayAttempt.payload == recentPayAttempt.payhash else False,
},
)


@lnurldevice_ext.get(
@lnurldevice_generic_router.get(
"/{paymentid}", name="lnurldevice.displaypin", response_class=HTMLResponse
)
async def displaypin(request: Request, paymentid: str):
Expand Down Expand Up @@ -132,7 +132,7 @@ async def displaypin(request: Request, paymentid: str):
)


@lnurldevice_ext.get("/print/{payment_id}", response_class=HTMLResponse)
@lnurldevice_generic_router.get("/print/{payment_id}", response_class=HTMLResponse)
async def print_receipt(request: Request, payment_id):
lnurldevicepayment = await get_lnurldevicepayment(payment_id)
if not lnurldevicepayment:
Expand Down
27 changes: 13 additions & 14 deletions views_api.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from http import HTTPStatus

from fastapi import Depends, HTTPException, Query, Request
from fastapi import APIRouter, Depends, HTTPException, Request
from loguru import logger

from lnbits.core.crud import get_user, get_wallet
from lnbits.core.models import WalletTypeInfo
from lnbits.decorators import (
get_key_type,
require_admin_key,
Expand Down Expand Up @@ -33,7 +34,6 @@

lnurldevice_api_router = APIRouter()


@lnurldevice_api_router.get("/api/v1/currencies")
async def api_list_currencies_available():
return list(currencies.keys())
Expand Down Expand Up @@ -92,7 +92,7 @@ async def api_lnurldevice_delete(req: Request, lnurldevice_id: str):
#########ATM API#########


@lnurldevice_ext.get("/api/v1/atm")
@lnurldevice_api_router.get("/api/v1/atm")
async def api_atm_payments_retrieve(
req: Request, wallet: WalletTypeInfo = Depends(get_key_type)
):
Expand All @@ -105,7 +105,7 @@ async def api_atm_payments_retrieve(
deviceids.append(lnurldevice.id)
return await get_lnurldevicepayments(deviceids)

@lnurldevice_ext.post("/api/v1/lnurlencode", dependencies=[Depends(get_key_type)])
@lnurldevice_api_router.post("/api/v1/lnurlencode", dependencies=[Depends(get_key_type)])
async def api_lnurlencode(
data: Lnurlencode
):
Expand All @@ -117,7 +117,7 @@ async def api_lnurlencode(
)
return lnurl

@lnurldevice_ext.delete(
@lnurldevice_api_router.delete(
"/api/v1/atm/{atm_id}", dependencies=[Depends(require_admin_key)]
)
async def api_atm_payment_delete(req: Request, atm_id: str):
Expand All @@ -135,7 +135,7 @@ async def api_atm_payment_delete(req: Request, atm_id: str):
###############


@lnurldevice_ext.get("/api/v1/ln/{lnurldevice_id}/{p}/{ln}")
@lnurldevice_api_router.get("/api/v1/ln/{lnurldevice_id}/{p}/{ln}")
async def get_lnurldevice_payment_lightning(
req: Request, lnurldevice_id: str, p: str, ln: str
):
Expand All @@ -156,18 +156,17 @@ async def get_lnurldevice_payment_lightning(
status_code=HTTPStatus.NOT_FOUND,
detail="Wallet does not exist connected to atm, payment could not be made",
)
lnurldevicepayment = await register_atm_payment(lnurldevice, p)
lnurldevicepayment, price_msat = await register_atm_payment(lnurldevice, p)
if not lnurldevicepayment:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Payment already claimed."
)

# If its an invoice check its a legit invoice
if ln[:4] == "lnbc":
try:
bolt11_decode(ln)
except Exception as exc:
raise HTTPException(status_code=HTTPStatus.BAD_REQUEST, detail=str(exc))
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Invoices not supported."
)

# If its an lnaddress or lnurlp get the request from callback
elif ln[:5] == "lnurl" or "@" in ln and "." in ln.split("@")[-1]:
Expand Down Expand Up @@ -196,7 +195,7 @@ async def get_lnurldevice_payment_lightning(

# Finally log the payment and make the payment
try:
lnurldevicepayment = await register_atm_payment(lnurldevice, p)
lnurldevicepayment, price_msat = await register_atm_payment(lnurldevice, p)
lnurldevicepayment_updated = await update_lnurldevicepayment(
lnurldevicepayment_id=lnurldevicepayment.id, payhash=lnurldevicepayment.payload
)
Expand All @@ -205,7 +204,7 @@ async def get_lnurldevice_payment_lightning(
payment = await pay_invoice(
wallet_id=lnurldevice.wallet,
payment_request=ln,
max_sat=lnurldevicepayment.sats * 1000,
max_sat=price_msat / 1000,
extra={"tag": "lnurldevice", "id": lnurldevicepayment.id},
)
assert payment
Expand All @@ -220,7 +219,7 @@ async def get_lnurldevice_payment_lightning(
###############


@lnurldevice_ext.get("'/api/v1/boltz/{lnurldevice_id}/{p}/{onchain_liquid}/{address}")
@lnurldevice_api_router.get("'/api/v1/boltz/{lnurldevice_id}/{p}/{onchain_liquid}/{address}")
async def get_lnurldevice_payment_boltz(
req: Request, lnurldevice_id: str, p: str, onchain_liquid: str, address: str
):
Expand Down
10 changes: 5 additions & 5 deletions views_lnurl.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@

from fastapi import APIRouter, HTTPException, Query, Request

from lnbits import bolt11
import bolt11
from lnbits.core.services import create_invoice
from lnbits.core.views.api import pay_invoice
from lnbits.utils.exchange_rates import fiat_amount_as_satoshis


from . import lnurldevice_ext
from .crud import (
create_lnurldevicepayment,
get_lnurldevice,
Expand All @@ -18,6 +17,7 @@
)

from .helpers import register_atm_payment, xor_decrypt

lnurldevice_lnurl_router = APIRouter()

@lnurldevice_lnurl_router.get(
Expand Down Expand Up @@ -144,7 +144,7 @@ async def lnurl_params(
)

if atm:
lnurldevicepayment = await register_atm_payment(device, p)
lnurldevicepayment, price_msat = await register_atm_payment(device, p)
if not lnurldevicepayment:
return {"status": "ERROR", "reason": "Could not create ATM payment."}
return {
Expand All @@ -153,8 +153,8 @@ async def lnurl_params(
"lnurldevice.lnurl_callback", paymentid=lnurldevicepayment.id
)),
"k1": p,
"minWithdrawable": price_msat * 1000,
"maxWithdrawable": price_msat * 1000,
"minWithdrawable": price_msat,
"maxWithdrawable": price_msat,
"defaultDescription": f"{device.title} - pin: {lnurldevicepayment.pin}",
}
price_msat = int(price_msat * ((device.profit / 100) + 1))
Expand Down

0 comments on commit d19e619

Please sign in to comment.