ID: {{ qrCodeDialog.data.id }}
-
{% endraw %}
@@ -434,13 +433,15 @@
},
methods: {
openQrCodeDialog: function (lnurldevice_id) {
- var lnurldevice = _.findWhere(this.lnurldeviceLinks, {id: lnurldevice_id})
- console.log(lnurldevice)
- this.qrCodeDialog.data = _.clone(lnurldevice)
- this.qrCodeDialog.data.url =
- window.location.protocol + '//' + window.location.host
- this.qrCodeDialog.show = true
- },
+ var lnurldevice = _.findWhere(this.lnurldeviceLinks, {
+ id: lnurldevice_id
+ })
+ console.log(lnurldevice)
+ this.qrCodeDialog.data = _.clone(lnurldevice)
+ this.qrCodeDialog.data.url =
+ window.location.protocol + '//' + window.location.host
+ this.qrCodeDialog.show = true
+ },
cancellnurldevice: function (data) {
var self = this
self.formDialoglnurldevice.show = false
@@ -617,10 +618,7 @@
'//',
window.location.host
].join('')
- self.wslocation = [
- 'ws://',
- window.location.host
- ].join('')
+ self.wslocation = ['ws://', window.location.host].join('')
LNbits.api
.request('GET', '/api/v1/currencies')
.then(response => {
diff --git a/lnbits/wallets/__init__.py b/lnbits/wallets/__init__.py
index 41949652d..fa533566f 100644
--- a/lnbits/wallets/__init__.py
+++ b/lnbits/wallets/__init__.py
@@ -1,5 +1,6 @@
# flake8: noqa
+
from .cliche import ClicheWallet
from .cln import CoreLightningWallet # legacy .env support
from .cln import CoreLightningWallet as CLightningWallet
@@ -9,6 +10,7 @@ from .lnbits import LNbitsWallet
from .lndgrpc import LndWallet
from .lndrest import LndRestWallet
from .lnpay import LNPayWallet
+from .lntips import LnTipsWallet
from .lntxbot import LntxbotWallet
from .opennode import OpenNodeWallet
from .spark import SparkWallet
diff --git a/lnbits/wallets/lntips.py b/lnbits/wallets/lntips.py
new file mode 100644
index 000000000..54220c85a
--- /dev/null
+++ b/lnbits/wallets/lntips.py
@@ -0,0 +1,170 @@
+import asyncio
+import hashlib
+import json
+import time
+from os import getenv
+from typing import AsyncGenerator, Dict, Optional
+
+import httpx
+from loguru import logger
+
+from .base import (
+ InvoiceResponse,
+ PaymentResponse,
+ PaymentStatus,
+ StatusResponse,
+ Wallet,
+)
+
+
+class LnTipsWallet(Wallet):
+ def __init__(self):
+ endpoint = getenv("LNTIPS_API_ENDPOINT")
+ self.endpoint = endpoint[:-1] if endpoint.endswith("/") else endpoint
+
+ key = (
+ getenv("LNTIPS_API_KEY")
+ or getenv("LNTIPS_ADMIN_KEY")
+ or getenv("LNTIPS_INVOICE_KEY")
+ )
+ self.auth = {"Authorization": f"Basic {key}"}
+
+ async def status(self) -> StatusResponse:
+ async with httpx.AsyncClient() as client:
+ r = await client.get(
+ f"{self.endpoint}/api/v1/balance", headers=self.auth, timeout=40
+ )
+ try:
+ data = r.json()
+ except:
+ return StatusResponse(
+ f"Failed to connect to {self.endpoint}, got: '{r.text[:200]}...'", 0
+ )
+
+ if data.get("error"):
+ return StatusResponse(data["error"], 0)
+
+ return StatusResponse(None, data["balance"] * 1000)
+
+ async def create_invoice(
+ self,
+ amount: int,
+ memo: Optional[str] = None,
+ description_hash: Optional[bytes] = None,
+ unhashed_description: Optional[bytes] = None,
+ **kwargs,
+ ) -> InvoiceResponse:
+ data: Dict = {"amount": amount}
+ if description_hash:
+ data["description_hash"] = description_hash.hex()
+ elif unhashed_description:
+ data["description_hash"] = hashlib.sha256(unhashed_description).hexdigest()
+ else:
+ data["memo"] = memo or ""
+
+ async with httpx.AsyncClient() as client:
+ r = await client.post(
+ f"{self.endpoint}/api/v1/createinvoice",
+ headers=self.auth,
+ json=data,
+ timeout=40,
+ )
+
+ if r.is_error:
+ try:
+ data = r.json()
+ error_message = data["message"]
+ except:
+ error_message = r.text
+ pass
+
+ return InvoiceResponse(False, None, None, error_message)
+
+ data = r.json()
+ return InvoiceResponse(
+ True, data["payment_hash"], data["payment_request"], None
+ )
+
+ async def pay_invoice(self, bolt11: str, fee_limit_msat: int) -> PaymentResponse:
+ async with httpx.AsyncClient() as client:
+ r = await client.post(
+ f"{self.endpoint}/api/v1/payinvoice",
+ headers=self.auth,
+ json={"pay_req": bolt11},
+ timeout=None,
+ )
+ if r.is_error:
+ return PaymentResponse(False, None, 0, None, r.text)
+
+ if "error" in r.json():
+ try:
+ data = r.json()
+ error_message = data["error"]
+ except:
+ error_message = r.text
+ pass
+ return PaymentResponse(False, None, 0, None, error_message)
+
+ data = r.json()["details"]
+ checking_id = data["payment_hash"]
+ fee_msat = -data["fee"]
+ preimage = data["preimage"]
+ return PaymentResponse(True, checking_id, fee_msat, preimage, None)
+
+ async def get_invoice_status(self, checking_id: str) -> PaymentStatus:
+ async with httpx.AsyncClient() as client:
+ r = await client.post(
+ f"{self.endpoint}/api/v1/invoicestatus/{checking_id}",
+ headers=self.auth,
+ )
+
+ if r.is_error or len(r.text) == 0:
+ return PaymentStatus(None)
+
+ data = r.json()
+ return PaymentStatus(data["paid"])
+
+ async def get_payment_status(self, checking_id: str) -> PaymentStatus:
+ async with httpx.AsyncClient() as client:
+ r = await client.post(
+ url=f"{self.endpoint}/api/v1/paymentstatus/{checking_id}",
+ headers=self.auth,
+ )
+
+ if r.is_error:
+ return PaymentStatus(None)
+ data = r.json()
+
+ paid_to_status = {False: None, True: True}
+ return PaymentStatus(paid_to_status[data.get("paid")])
+
+ async def paid_invoices_stream(self) -> AsyncGenerator[str, None]:
+ last_connected = None
+ while True:
+ url = f"{self.endpoint}/api/v1/invoicestream"
+ try:
+ async with httpx.AsyncClient(timeout=None, headers=self.auth) as client:
+ last_connected = time.time()
+ async with client.stream("GET", url) as r:
+ async for line in r.aiter_lines():
+ try:
+ prefix = "data: "
+ if not line.startswith(prefix):
+ continue
+ data = line[len(prefix) :] # sse parsing
+ inv = json.loads(data)
+ if not inv.get("payment_hash"):
+ continue
+ except:
+ continue
+ yield inv["payment_hash"]
+ except Exception as e:
+ pass
+
+ # do not sleep if the connection was active for more than 10s
+ # since the backend is expected to drop the connection after 90s
+ if last_connected is None or time.time() - last_connected < 10:
+ logger.error(
+ f"lost connection to {self.endpoint}/api/v1/invoicestream, retrying in 5 seconds"
+ )
+ await asyncio.sleep(5)