Web socket working

This commit is contained in:
ben 2022-10-26 20:19:04 +01:00
parent 937654e4f7
commit b6b54d6842
5 changed files with 64 additions and 69 deletions

View file

@ -9,6 +9,7 @@ from embit import bech32, compact
from fastapi import Request
from fastapi.param_functions import Query
from starlette.exceptions import HTTPException
from loguru import logger
from lnbits.core.services import create_invoice
from lnbits.core.views.api import pay_invoice
@ -113,6 +114,14 @@ async def lnurl_v1_params(
else amount_in_cent
) * 1000
# Check they're not trying to trick the switch!
check = False
for switch in device.switches(request):
if switch[0] == gpio and switch[1] == profit and switch[2] == amount:
check = True
if not check:
return {"status": "ERROR", "reason": f"Switch params wrong"}
lnurldevicepayment = await create_lnurldevicepayment(
deviceid=device.id,
payload=amount,
@ -129,7 +138,7 @@ async def lnurl_v1_params(
),
"minSendable": price_msat,
"maxSendable": price_msat,
"metadata": await device.lnurlpay_metadata(),
"metadata": device.lnurlpay_metadata,
}
if len(p) % 4 > 0:
p += "=" * (4 - (len(p) % 4))
@ -236,11 +245,17 @@ async def lnurl_callback(
if device.device == "switch":
payment_hash, payment_request = await create_invoice(
wallet_id=device.wallet,
amount=lnurldevicepayment.sats / 1000,
memo=device.title + "-" + lnurldevicepayment.id,
unhashed_description=(await device.lnurlpay_metadata()).encode("utf-8"),
extra={"tag": "Switch", "pin": lnurldevicepayment.pin,"amount": int(lnurldevicepayment.payload),"id": paymentid},
amount=int(lnurldevicepayment.sats / 1000),
memo=device.id + " PIN " + str(lnurldevicepayment.pin),
unhashed_description=device.lnurlpay_metadata.encode("utf-8"),
extra={
"tag": "Switch",
"pin": str(lnurldevicepayment.pin),
"amount": str(lnurldevicepayment.payload),
"id": paymentid
},
)
lnurldevicepayment = await update_lnurldevicepayment(
lnurldevicepayment_id=paymentid, payhash=payment_hash
)

View file

@ -1,6 +1,6 @@
import json
from sqlite3 import Row
from typing import Optional
from typing import Optional, List
from fastapi import Request
from lnurl import Lnurl
@ -9,7 +9,7 @@ from lnurl.models import LnurlPaySuccessAction, UrlAction # type: ignore
from lnurl.types import LnurlPayMetadata # type: ignore
from pydantic import BaseModel
from pydantic.main import BaseModel
from loguru import logger
class createLnurldevice(BaseModel):
title: str
@ -60,13 +60,28 @@ class lnurldevices(BaseModel):
def from_row(cls, row: Row) -> "lnurldevices":
return cls(**dict(row))
def lnurl(self, req: Request) -> Lnurl:
url = req.url_for("lnurldevice.lnurl_v1_params", device_id=self.id)
return lnurl_encode(url)
async def lnurlpay_metadata(self) -> LnurlPayMetadata:
@property
def lnurlpay_metadata(self) -> LnurlPayMetadata:
return LnurlPayMetadata(json.dumps([["text/plain", self.title]]))
def switches(self, req: Request) -> List:
switches = []
if self.profit > 0:
url = req.url_for("lnurldevice.lnurl_v1_params", device_id=self.id)
switches.append([str(self.pin), str(self.profit), str(self.amount), lnurl_encode(url + "?gpio=" + str(self.pin) + "&profit=" + str(self.profit) + "&amount=" + str(self.amount))])
if self.profit1 > 0:
url = req.url_for("lnurldevice.lnurl_v1_params", device_id=self.id)
switches.append([str(self.pin1), str(self.profit1), str(self.amount1), lnurl_encode(url + "?gpio=" + str(self.pin1) + "&profit=" + str(self.profit1) + "&amount=" + str(self.amount1))])
if self.profit2 > 0:
url = req.url_for("lnurldevice.lnurl_v1_params", device_id=self.id)
switches.append([str(self.pin2), str(self.profit2), str(self.amount2), lnurl_encode(url + "?gpio=" + str(self.pin2) + "&profit=" + str(self.profit2) + "&amount=" + str(self.amount2))])
if self.profit3 > 0:
url = req.url_for("lnurldevice.lnurl_v1_params", device_id=self.id)
switches.append([str(self.pin3), str(self.profit3), str(self.amount3), lnurl_encode(url + "?gpio=" + str(self.pin3) + "&profit=" + str(self.profit3) + "&amount=" + str(self.amount3))])
if self.profit4 > 0:
url = req.url_for("lnurldevice.lnurl_v1_params", device_id=self.id)
switches.append([str(self.pin4), str(self.profit4), str(self.amount4), lnurl_encode(url + "?gpio=" + str(self.pin4) + "&profit=" + str(self.profit4) + "&amount=" + str(self.amount4))])
return switches
class lnurldevicepayment(BaseModel):
id: str

View file

@ -36,5 +36,5 @@ async def on_invoice_paid(payment: Payment) -> None:
lnurldevicepayment = await update_lnurldevicepayment(
lnurldevicepayment_id=payment.extra.get("id"), payhash="used"
)
return await updater(lnurldevicepayment.deviceid, lnurldevicepayment.pin, lnurldevicepayment.amount)
return await updater(lnurldevicepayment.deviceid, lnurldevicepayment.pin, lnurldevicepayment.payload)
return

View file

@ -148,42 +148,6 @@
style="width: 700px; max-width: 80vw"
class="q-pa-lg q-pt-xl lnbits__dialog-card"
>
<q-tabs
v-model="tab"
dense
class="text-grey"
active-color="primary"
indicator-color="primary"
align="justify"
narrow-indicator
>
<q-tab name="mails" label="Mails" />
<q-tab name="alarms" label="Alarms" />
<q-tab name="movies" label="Movies" />
</q-tabs>
<q-separator />
<q-tab-panels v-model="tab" animated>
<q-tab-panel name="mails">
<div class="text-h6">Mails</div>
Lorem ipsum dolor sit amet consectetur adipisicing elit.
</q-tab-panel>
<q-tab-panel name="alarms">
<div class="text-h6">Alarms</div>
Lorem ipsum dolor sit amet consectetur adipisicing elit.
</q-tab-panel>
<q-tab-panel name="movies">
<div class="text-h6">Movies</div>
Lorem ipsum dolor sit amet consectetur adipisicing elit.
</q-tab-panel>
</q-tab-panels>
<div class="text-h6">LNURLDevice device string</div>
<center>
<q-btn
@ -494,28 +458,27 @@
<q-dialog v-model="qrCodeDialog.show" position="top">
<q-card v-if="qrCodeDialog.data" class="q-pa-lg lnbits__dialog-card">
<q-responsive :ratio="1" class="q-mx-xl q-mb-md">
<qrcode
:value="qrCodeDialog.data.url + '/?lightning=' + qrCodeDialog.data.lnurl"
:value="lnurlValue"
:options="{width: 800}"
class="rounded-borders"
></qrcode>
{% raw %}
</q-responsive>
<p style="word-break: break-all">
<strong>ID:</strong> {{ qrCodeDialog.data.id }}<br />
</p>
{% endraw %}
<div class="row q-mt-lg q-gutter-sm">
<q-btn
<q-btn
outline
color="grey"
@click="copyText(qrCodeDialog.data.lnurl, 'LNURL copied to clipboard!')"
class="q-ml-sm"
@click="copyText(lnurlValue, 'LNURL copied to clipboard!')"
>Copy LNURL</q-btn
>
<br/>
<div class="row q-mt-lg q-gutter-sm">
<q-btn v-for="switch_ in qrCodeDialog.data.switches" outline color="primary" :label="'Switch PIN:' + switch_[0]" @click="lnurlValueFetch(switch_[3])"></q-btn>
<q-btn v-close-popup flat color="grey" class="q-ml-auto">Close</q-btn>
</div>
</q-card>
</q-dialog>
</div>
@ -545,11 +508,13 @@
mixins: [windowMixin],
data: function () {
return {
tab: 'mails',
protocol: window.location.protocol,
location: window.location.hostname,
wslocation: window.location.hostname,
filter: '',
currency: 'USD',
lnurlValue: '',
switches: 0,
lnurldeviceLinks: [],
lnurldeviceLinksObj: [],
@ -599,12 +564,6 @@
label: 'device',
field: 'device'
},
{
name: 'profit',
align: 'left',
label: 'profit',
field: 'profit'
},
{
name: 'currency',
align: 'left',
@ -653,8 +612,12 @@
this.qrCodeDialog.data = _.clone(lnurldevice)
this.qrCodeDialog.data.url =
window.location.protocol + '//' + window.location.host
this.lnurlValueFetch(this.qrCodeDialog.data.switches[0][3])
this.qrCodeDialog.show = true
},
lnurlValueFetch: function (lnurl){
this.lnurlValue = lnurl
},
addSwitch: function () {
var self = this
self.switches = self.switches + 1
@ -719,7 +682,9 @@
.then(function (response) {
if (response.data) {
self.lnurldeviceLinks = response.data.map(maplnurldevice)
console.log("response.data")
console.log(response.data)
console.log("response.data")
}
})
.catch(function (error) {

View file

@ -39,10 +39,10 @@ async def api_lnurldevice_create_or_update(
):
if not lnurldevice_id:
lnurldevice = await create_lnurldevice(data)
return {**lnurldevice.dict(), **{"lnurl": lnurldevice.lnurl(req)}}
return {**lnurldevice.dict(), **{"switches": lnurldevice.switches(req)}}
else:
lnurldevice = await update_lnurldevice(data, lnurldevice_id=lnurldevice_id)
return {**lnurldevice.dict(), **{"lnurl": lnurldevice.lnurl(req)}}
return {**lnurldevice.dict(), **{"switches": lnurldevice.switches(req)}}
@lnurldevice_ext.get("/api/v1/lnurlpos")
@ -52,7 +52,7 @@ async def api_lnurldevices_retrieve(
wallet_ids = (await get_user(wallet.wallet.user)).wallet_ids
try:
return [
{**lnurldevice.dict(), **{"lnurl": lnurldevice.lnurl(req)}}
{**lnurldevice.dict(), **{"switches": lnurldevice.switches(req)}}
for lnurldevice in await get_lnurldevices(wallet_ids)
]
except:
@ -78,7 +78,7 @@ async def api_lnurldevice_retrieve(
)
if not lnurldevice.lnurl_toggle:
return {**lnurldevice.dict()}
return {**lnurldevice.dict(), **{"lnurl": lnurldevice.lnurl(req)}}
return {**lnurldevice.dict(), **{"switches": lnurldevice.switches(req)}}
@lnurldevice_ext.delete("/api/v1/lnurlpos/{lnurldevice_id}")