mirror of
https://github.com/lnbits/lnbits.git
synced 2025-10-02 14:09:26 +02:00
Add backend: ln.tips (LightningTipBot) wallet (#921)
* add ln.tips * add wallet * revert lnbits * make format * .env.example * listener fix * reconnect fixed * make format * make format
This commit is contained in:
@@ -41,7 +41,7 @@ LNBITS_SITE_DESCRIPTION="Some description about your service, will display if ti
|
|||||||
LNBITS_THEME_OPTIONS="classic, bitcoin, flamingo, freedom, mint, autumn, monochrome, salvador"
|
LNBITS_THEME_OPTIONS="classic, bitcoin, flamingo, freedom, mint, autumn, monochrome, salvador"
|
||||||
# LNBITS_CUSTOM_LOGO="https://lnbits.com/assets/images/logo/logo.svg"
|
# LNBITS_CUSTOM_LOGO="https://lnbits.com/assets/images/logo/logo.svg"
|
||||||
|
|
||||||
# Choose from LNPayWallet, OpenNodeWallet, LntxbotWallet, ClicheWallet
|
# Choose from LNPayWallet, OpenNodeWallet, LntxbotWallet, ClicheWallet, LnTipsWallet
|
||||||
# LndRestWallet, CoreLightningWallet, LNbitsWallet, SparkWallet, FakeWallet, EclairWallet
|
# LndRestWallet, CoreLightningWallet, LNbitsWallet, SparkWallet, FakeWallet, EclairWallet
|
||||||
LNBITS_BACKEND_WALLET_CLASS=VoidWallet
|
LNBITS_BACKEND_WALLET_CLASS=VoidWallet
|
||||||
# VoidWallet is just a fallback that works without any actual Lightning capabilities,
|
# VoidWallet is just a fallback that works without any actual Lightning capabilities,
|
||||||
@@ -92,3 +92,8 @@ LNBITS_DENOMINATION=sats
|
|||||||
# EclairWallet
|
# EclairWallet
|
||||||
ECLAIR_URL=http://127.0.0.1:8283
|
ECLAIR_URL=http://127.0.0.1:8283
|
||||||
ECLAIR_PASS=eclairpw
|
ECLAIR_PASS=eclairpw
|
||||||
|
|
||||||
|
# LnTipsWallet
|
||||||
|
# Enter /api in LightningTipBot to get your key
|
||||||
|
LNTIPS_API_KEY=LNTIPS_ADMIN_KEY
|
||||||
|
LNTIPS_API_ENDPOINT=https://ln.tips
|
||||||
|
@@ -1,9 +1,10 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
|
|
||||||
from fastapi import APIRouter
|
from fastapi import APIRouter
|
||||||
|
|
||||||
from lnbits.db import Database
|
from lnbits.db import Database
|
||||||
from lnbits.tasks import catch_everything_and_restart
|
|
||||||
from lnbits.helpers import template_renderer
|
from lnbits.helpers import template_renderer
|
||||||
|
from lnbits.tasks import catch_everything_and_restart
|
||||||
|
|
||||||
db = Database("ext_lnurldevice")
|
db = Database("ext_lnurldevice")
|
||||||
|
|
||||||
@@ -14,8 +15,8 @@ def lnurldevice_renderer():
|
|||||||
return template_renderer(["lnbits/extensions/lnurldevice/templates"])
|
return template_renderer(["lnbits/extensions/lnurldevice/templates"])
|
||||||
|
|
||||||
|
|
||||||
from .tasks import wait_for_paid_invoices
|
|
||||||
from .lnurl import * # noqa
|
from .lnurl import * # noqa
|
||||||
|
from .tasks import wait_for_paid_invoices
|
||||||
from .views import * # noqa
|
from .views import * # noqa
|
||||||
from .views_api import * # noqa
|
from .views_api import * # noqa
|
||||||
|
|
||||||
|
@@ -4,19 +4,21 @@
|
|||||||
For LNURL based Points of Sale, ATMs, and relay devices<br />
|
For LNURL based Points of Sale, ATMs, and relay devices<br />
|
||||||
Use with: <br />
|
Use with: <br />
|
||||||
LNPoS
|
LNPoS
|
||||||
<a href="https://lnbits.github.io/lnpos"
|
<a href="https://lnbits.github.io/lnpos">
|
||||||
> https://lnbits.github.io/lnpos</a
|
https://lnbits.github.io/lnpos</a
|
||||||
><br />
|
><br />
|
||||||
bitcoinSwitch
|
bitcoinSwitch
|
||||||
<a href="https://github.com/lnbits/bitcoinSwitch"
|
<a href="https://github.com/lnbits/bitcoinSwitch">
|
||||||
> https://github.com/lnbits/bitcoinSwitch</a
|
https://github.com/lnbits/bitcoinSwitch</a
|
||||||
><br />
|
><br />
|
||||||
FOSSA
|
FOSSA
|
||||||
<a href="https://github.com/lnbits/fossa"
|
<a href="https://github.com/lnbits/fossa">
|
||||||
> https://github.com/lnbits/fossa</a
|
https://github.com/lnbits/fossa</a
|
||||||
><br />
|
><br />
|
||||||
<small>
|
<small>
|
||||||
Created by, <a href="https://github.com/benarc">Ben Arc</a>, <a href="https://github.com/blackcoffeexbt">BC</a>, <a href="https://github.com/motorina0">Vlad Stan</a></small
|
Created by, <a href="https://github.com/benarc">Ben Arc</a>,
|
||||||
|
<a href="https://github.com/blackcoffeexbt">BC</a>,
|
||||||
|
<a href="https://github.com/motorina0">Vlad Stan</a></small
|
||||||
>
|
>
|
||||||
</p>
|
</p>
|
||||||
</q-card-section>
|
</q-card-section>
|
||||||
|
@@ -94,17 +94,19 @@
|
|||||||
</q-td>
|
</q-td>
|
||||||
<q-td>
|
<q-td>
|
||||||
<q-btn
|
<q-btn
|
||||||
v-if="props.row.device == 'switch'"
|
v-if="props.row.device == 'switch'"
|
||||||
:disable="protocol == 'http:'"
|
:disable="protocol == 'http:'"
|
||||||
flat
|
flat
|
||||||
unelevated
|
unelevated
|
||||||
dense
|
dense
|
||||||
size="xs"
|
size="xs"
|
||||||
icon="visibility"
|
icon="visibility"
|
||||||
:color="($q.dark.isActive) ? 'grey-7' : 'grey-5'"
|
:color="($q.dark.isActive) ? 'grey-7' : 'grey-5'"
|
||||||
@click="openQrCodeDialog(props.row.id)"
|
@click="openQrCodeDialog(props.row.id)"
|
||||||
><q-tooltip v-if="protocol == 'http:'"> LNURLs only work over HTTPS </q-tooltip><q-tooltip v-else> view LNURL </q-tooltip></q-btn
|
><q-tooltip v-if="protocol == 'http:'">
|
||||||
>
|
LNURLs only work over HTTPS </q-tooltip
|
||||||
|
><q-tooltip v-else> view LNURL </q-tooltip></q-btn
|
||||||
|
>
|
||||||
</q-td>
|
</q-td>
|
||||||
<q-td
|
<q-td
|
||||||
v-for="col in props.cols"
|
v-for="col in props.cols"
|
||||||
@@ -148,33 +150,32 @@
|
|||||||
>
|
>
|
||||||
<div class="text-h6">LNURLDevice device string</div>
|
<div class="text-h6">LNURLDevice device string</div>
|
||||||
<center>
|
<center>
|
||||||
<q-btn
|
<q-btn
|
||||||
v-if="settingsDialog.data.device == 'switch'"
|
v-if="settingsDialog.data.device == 'switch'"
|
||||||
dense
|
dense
|
||||||
outline
|
outline
|
||||||
unelevated
|
unelevated
|
||||||
color="primary"
|
color="primary"
|
||||||
size="md"
|
size="md"
|
||||||
@click="copyText(wslocation + '/lnurldevice/ws/' + settingsDialog.data.id, 'Link copied to clipboard!')"
|
@click="copyText(wslocation + '/lnurldevice/ws/' + settingsDialog.data.id, 'Link copied to clipboard!')"
|
||||||
>{% raw
|
>{% raw %}{{wslocation}}/lnurldevice/ws/{{settingsDialog.data.id}}{%
|
||||||
%}{{wslocation}}/lnurldevice/ws/{{settingsDialog.data.id}}{% endraw
|
endraw %}<q-tooltip> Click to copy URL </q-tooltip>
|
||||||
%}<q-tooltip> Click to copy URL </q-tooltip>
|
</q-btn>
|
||||||
</q-btn>
|
<q-btn
|
||||||
<q-btn
|
v-else
|
||||||
v-else
|
dense
|
||||||
dense
|
outline
|
||||||
outline
|
unelevated
|
||||||
unelevated
|
color="primary"
|
||||||
color="primary"
|
size="md"
|
||||||
size="md"
|
@click="copyText(location + '/lnurldevice/api/v1/lnurl/' + settingsDialog.data.id + ',' +
|
||||||
@click="copyText(location + '/lnurldevice/api/v1/lnurl/' + settingsDialog.data.id + ',' +
|
|
||||||
settingsDialog.data.key + ',' + settingsDialog.data.currency, 'Link copied to clipboard!')"
|
settingsDialog.data.key + ',' + settingsDialog.data.currency, 'Link copied to clipboard!')"
|
||||||
>{% raw
|
>{% raw
|
||||||
%}{{location}}/lnurldevice/api/v1/lnurl/{{settingsDialog.data.id}},
|
%}{{location}}/lnurldevice/api/v1/lnurl/{{settingsDialog.data.id}},
|
||||||
{{settingsDialog.data.key}}, {{settingsDialog.data.currency}}{% endraw
|
{{settingsDialog.data.key}}, {{settingsDialog.data.currency}}{% endraw
|
||||||
%}<q-tooltip> Click to copy URL </q-tooltip>
|
%}<q-tooltip> Click to copy URL </q-tooltip>
|
||||||
</q-btn>
|
</q-btn>
|
||||||
</center>
|
</center>
|
||||||
<div class="text-subtitle2">
|
<div class="text-subtitle2">
|
||||||
<small> </small>
|
<small> </small>
|
||||||
</div>
|
</div>
|
||||||
@@ -229,29 +230,28 @@
|
|||||||
label="Profit margin (% added to invoices/deducted from faucets)"
|
label="Profit margin (% added to invoices/deducted from faucets)"
|
||||||
></q-input>
|
></q-input>
|
||||||
<div v-else>
|
<div v-else>
|
||||||
<q-input
|
<q-input
|
||||||
ref="setAmount"
|
ref="setAmount"
|
||||||
filled
|
filled
|
||||||
dense
|
dense
|
||||||
v-model.trim="formDialoglnurldevice.data.profit"
|
v-model.trim="formDialoglnurldevice.data.profit"
|
||||||
class="q-pb-md"
|
class="q-pb-md"
|
||||||
:label="'Amount (' + formDialoglnurldevice.data.currency + ') *'"
|
:label="'Amount (' + formDialoglnurldevice.data.currency + ') *'"
|
||||||
:mask="'#.##'"
|
:mask="'#.##'"
|
||||||
fill-mask="0"
|
fill-mask="0"
|
||||||
reverse-fill-mask
|
reverse-fill-mask
|
||||||
:step="'0.01'"
|
:step="'0.01'"
|
||||||
value="0.00"
|
value="0.00"
|
||||||
></q-input>
|
></q-input>
|
||||||
<q-input
|
<q-input
|
||||||
filled
|
filled
|
||||||
dense
|
dense
|
||||||
v-model.trim="formDialoglnurldevice.data.amount"
|
v-model.trim="formDialoglnurldevice.data.amount"
|
||||||
type="number"
|
type="number"
|
||||||
value="1000"
|
value="1000"
|
||||||
label="milesecs to turn Switch on for (1sec = 1000ms)"
|
label="milesecs to turn Switch on for (1sec = 1000ms)"
|
||||||
|
></q-input>
|
||||||
></q-input>
|
</div>
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="row q-mt-lg">
|
<div class="row q-mt-lg">
|
||||||
<q-btn
|
<q-btn
|
||||||
@@ -292,7 +292,6 @@
|
|||||||
</q-responsive>
|
</q-responsive>
|
||||||
<p style="word-break: break-all">
|
<p style="word-break: break-all">
|
||||||
<strong>ID:</strong> {{ qrCodeDialog.data.id }}<br />
|
<strong>ID:</strong> {{ qrCodeDialog.data.id }}<br />
|
||||||
|
|
||||||
</p>
|
</p>
|
||||||
{% endraw %}
|
{% endraw %}
|
||||||
<div class="row q-mt-lg q-gutter-sm">
|
<div class="row q-mt-lg q-gutter-sm">
|
||||||
@@ -434,13 +433,15 @@
|
|||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
openQrCodeDialog: function (lnurldevice_id) {
|
openQrCodeDialog: function (lnurldevice_id) {
|
||||||
var lnurldevice = _.findWhere(this.lnurldeviceLinks, {id: lnurldevice_id})
|
var lnurldevice = _.findWhere(this.lnurldeviceLinks, {
|
||||||
console.log(lnurldevice)
|
id: lnurldevice_id
|
||||||
this.qrCodeDialog.data = _.clone(lnurldevice)
|
})
|
||||||
this.qrCodeDialog.data.url =
|
console.log(lnurldevice)
|
||||||
window.location.protocol + '//' + window.location.host
|
this.qrCodeDialog.data = _.clone(lnurldevice)
|
||||||
this.qrCodeDialog.show = true
|
this.qrCodeDialog.data.url =
|
||||||
},
|
window.location.protocol + '//' + window.location.host
|
||||||
|
this.qrCodeDialog.show = true
|
||||||
|
},
|
||||||
cancellnurldevice: function (data) {
|
cancellnurldevice: function (data) {
|
||||||
var self = this
|
var self = this
|
||||||
self.formDialoglnurldevice.show = false
|
self.formDialoglnurldevice.show = false
|
||||||
@@ -617,10 +618,7 @@
|
|||||||
'//',
|
'//',
|
||||||
window.location.host
|
window.location.host
|
||||||
].join('')
|
].join('')
|
||||||
self.wslocation = [
|
self.wslocation = ['ws://', window.location.host].join('')
|
||||||
'ws://',
|
|
||||||
window.location.host
|
|
||||||
].join('')
|
|
||||||
LNbits.api
|
LNbits.api
|
||||||
.request('GET', '/api/v1/currencies')
|
.request('GET', '/api/v1/currencies')
|
||||||
.then(response => {
|
.then(response => {
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
# flake8: noqa
|
# flake8: noqa
|
||||||
|
|
||||||
|
|
||||||
from .cliche import ClicheWallet
|
from .cliche import ClicheWallet
|
||||||
from .cln import CoreLightningWallet # legacy .env support
|
from .cln import CoreLightningWallet # legacy .env support
|
||||||
from .cln import CoreLightningWallet as CLightningWallet
|
from .cln import CoreLightningWallet as CLightningWallet
|
||||||
@@ -9,6 +10,7 @@ from .lnbits import LNbitsWallet
|
|||||||
from .lndgrpc import LndWallet
|
from .lndgrpc import LndWallet
|
||||||
from .lndrest import LndRestWallet
|
from .lndrest import LndRestWallet
|
||||||
from .lnpay import LNPayWallet
|
from .lnpay import LNPayWallet
|
||||||
|
from .lntips import LnTipsWallet
|
||||||
from .lntxbot import LntxbotWallet
|
from .lntxbot import LntxbotWallet
|
||||||
from .opennode import OpenNodeWallet
|
from .opennode import OpenNodeWallet
|
||||||
from .spark import SparkWallet
|
from .spark import SparkWallet
|
||||||
|
170
lnbits/wallets/lntips.py
Normal file
170
lnbits/wallets/lntips.py
Normal file
@@ -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)
|
Reference in New Issue
Block a user