mirror of
https://github.com/lnbits/lnbits-legend.git
synced 2024-11-20 02:28:10 +01:00
LNDHub loading, prob not working
This commit is contained in:
parent
0c6cd94154
commit
08bc771fbe
6
lnbits/extensions/lndhub/README.md
Normal file
6
lnbits/extensions/lndhub/README.md
Normal file
@ -0,0 +1,6 @@
|
||||
<h1>lndhub Extension</h1>
|
||||
<h2>*connect to your lnbits wallet from BlueWallet or Zeus*</h2>
|
||||
|
||||
Lndhub has nothing to do with lnd, it is just the name of the HTTP/JSON protocol https://bluewallet.io/ uses to talk to their Lightning custodian server at https://lndhub.io/.
|
||||
|
||||
Despite not having been planned to this, Lndhub because somewhat a standard for custodian wallet communication when https://t.me/lntxbot and https://zeusln.app/ implemented the same interface. And with this extension LNBits joins the same club.
|
25
lnbits/extensions/lndhub/__init__.py
Normal file
25
lnbits/extensions/lndhub/__init__.py
Normal file
@ -0,0 +1,25 @@
|
||||
import asyncio
|
||||
from fastapi import APIRouter, FastAPI
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from starlette.routing import Mount
|
||||
from lnbits.db import Database
|
||||
from lnbits.helpers import template_renderer
|
||||
from lnbits.tasks import catch_everything_and_restart
|
||||
|
||||
db = Database("ext_lndhub")
|
||||
|
||||
lndhub_ext: APIRouter = APIRouter(prefix="/lndhub", tags=["lndhub"])
|
||||
|
||||
|
||||
def lndhub_renderer():
|
||||
return template_renderer(
|
||||
[
|
||||
"lnbits/extensions/lndhub/templates",
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
from .views_api import * # noqa
|
||||
from .views import * # noqa
|
||||
from .utils import * # noqa
|
||||
from .decorators import * # noqa
|
6
lnbits/extensions/lndhub/config.json
Normal file
6
lnbits/extensions/lndhub/config.json
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"name": "LndHub",
|
||||
"short_description": "Access lnbits from BlueWallet or Zeus",
|
||||
"icon": "navigation",
|
||||
"contributors": ["fiatjaf"]
|
||||
}
|
33
lnbits/extensions/lndhub/decorators.py
Normal file
33
lnbits/extensions/lndhub/decorators.py
Normal file
@ -0,0 +1,33 @@
|
||||
from base64 import b64decode
|
||||
from functools import wraps
|
||||
|
||||
from lnbits.core.crud import get_wallet_for_key
|
||||
from fastapi import Request
|
||||
from http import HTTPStatus
|
||||
from starlette.exceptions import HTTPException
|
||||
from starlette.responses import HTMLResponse, JSONResponse # type: ignore
|
||||
|
||||
|
||||
def check_wallet(requires_admin=False):
|
||||
def wrap(view):
|
||||
@wraps(view)
|
||||
async def wrapped_view(request: Request, **kwargs):
|
||||
token = request.headers["Authorization"].split("Bearer ")[1]
|
||||
key_type, key = b64decode(token).decode("utf-8").split(":")
|
||||
|
||||
if requires_admin and key_type != "admin":
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.FORBIDDEN,
|
||||
detail="insufficient permissions",
|
||||
)
|
||||
g.wallet = await get_wallet_for_key(key, key_type)
|
||||
if not g.wallet:
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.FORBIDDEN,
|
||||
detail="insufficient permissions",
|
||||
)
|
||||
return await view(**kwargs)
|
||||
|
||||
return wrapped_view
|
||||
|
||||
return wrap
|
2
lnbits/extensions/lndhub/migrations.py
Normal file
2
lnbits/extensions/lndhub/migrations.py
Normal file
@ -0,0 +1,2 @@
|
||||
async def migrate():
|
||||
pass
|
35
lnbits/extensions/lndhub/templates/lndhub/_instructions.html
Normal file
35
lnbits/extensions/lndhub/templates/lndhub/_instructions.html
Normal file
@ -0,0 +1,35 @@
|
||||
<q-expansion-item
|
||||
group="extras"
|
||||
icon="info"
|
||||
label="Instructions"
|
||||
default-opened
|
||||
>
|
||||
<q-card>
|
||||
<q-card-section>
|
||||
To access an LNbits wallet from a mobile phone,
|
||||
<ol>
|
||||
<li>
|
||||
Install either <a href="https://zeusln.app">Zeus</a> or
|
||||
<a href="https://bluewallet.io/">BlueWallet</a>;
|
||||
</li>
|
||||
<li>
|
||||
Go to <code>Add a wallet / Import wallet</code> on BlueWallet or
|
||||
<code>Settings / Add a new node</code> on Zeus.
|
||||
</li>
|
||||
<li>Select the desired wallet on this page;</li>
|
||||
<li>Scan one of the two QR codes from the mobile wallet.</li>
|
||||
</ol>
|
||||
<ul>
|
||||
<li>
|
||||
<em>Invoice</em> URLs mean the mobile wallet will only have the
|
||||
authorization to read your payments and invoices and generate new
|
||||
invoices.
|
||||
</li>
|
||||
<li>
|
||||
<em>Admin</em> URLs mean the mobile wallet will be able to pay
|
||||
invoices..
|
||||
</li>
|
||||
</ul>
|
||||
</q-card-section>
|
||||
</q-card>
|
||||
</q-expansion-item>
|
19
lnbits/extensions/lndhub/templates/lndhub/_lndhub.html
Normal file
19
lnbits/extensions/lndhub/templates/lndhub/_lndhub.html
Normal file
@ -0,0 +1,19 @@
|
||||
<q-expansion-item group="extras" icon="info" label="LndHub info">
|
||||
<q-card>
|
||||
<q-card-section>
|
||||
<p>
|
||||
LndHub is a protocol invented by
|
||||
<a href="https://bluewallet.io/">BlueWallet</a> that allows mobile
|
||||
wallets to query payments and balances, generate invoices and make
|
||||
payments from accounts that exist on a server. The protocol is a
|
||||
collection of HTTP endpoints exposed through the internet.
|
||||
</p>
|
||||
<p>
|
||||
For a wallet that supports it, reading a QR code that contains the URL
|
||||
along with secret access credentials should enable access. Currently it
|
||||
is supported by <a href="https://zeusln.app">Zeus</a> and
|
||||
<a href="https://bluewallet.io/">BlueWallet</a>.
|
||||
</p>
|
||||
</q-card-section>
|
||||
</q-card>
|
||||
</q-expansion-item>
|
94
lnbits/extensions/lndhub/templates/lndhub/index.html
Normal file
94
lnbits/extensions/lndhub/templates/lndhub/index.html
Normal file
@ -0,0 +1,94 @@
|
||||
{% extends "base.html" %} {% from "macros.jinja" import window_vars with context
|
||||
%} {% block page %} {% raw %}
|
||||
<div class="row q-col-gutter-md">
|
||||
<div class="col-12 col-md-8 col-lg-7 q-gutter-y-md">
|
||||
<div class="row q-col-gutter-md">
|
||||
<q-card
|
||||
v-for="type in ['invoice', 'admin']"
|
||||
v-bind:key="type"
|
||||
class="q-pa-sm q-ma-sm col-5"
|
||||
>
|
||||
<q-card-section class="q-pa-none">
|
||||
<div class="text-center">
|
||||
<a :href="selectedWallet[type]">
|
||||
<q-responsive :ratio="1" class="q-mx-sm">
|
||||
<qrcode
|
||||
:value="selectedWallet[type]"
|
||||
:options="{width: 800}"
|
||||
class="rounded-borders"
|
||||
></qrcode>
|
||||
</q-responsive>
|
||||
</a>
|
||||
</div>
|
||||
<div class="row q-mt-lg items-center justify-center">
|
||||
<q-btn
|
||||
outline
|
||||
color="grey"
|
||||
@click="copyText(selectedWallet[type])"
|
||||
class="text-center q-mb-md"
|
||||
>Copy LndHub {{type}} URL</q-btn
|
||||
>
|
||||
</div>
|
||||
</q-card-section>
|
||||
</q-card>
|
||||
</div>
|
||||
|
||||
<q-card class="q-pa-lg q-pt-xl lnbits__dialog-card">
|
||||
<q-form class="q-gutter-md">
|
||||
<q-select
|
||||
filled
|
||||
dense
|
||||
v-model="selectedWallet"
|
||||
:options="wallets"
|
||||
label="Select wallet:"
|
||||
>
|
||||
</q-select>
|
||||
</q-form>
|
||||
</q-card>
|
||||
</div>
|
||||
|
||||
{% endraw %}
|
||||
|
||||
<div class="col-12 col-md-4 col-lg-5 q-gutter-y-md">
|
||||
<q-card>
|
||||
<q-card-section>
|
||||
<h6 class="text-subtitle1 q-my-none">
|
||||
{{SITE_TITLE}} LndHub extension
|
||||
</h6>
|
||||
</q-card-section>
|
||||
<q-card-section class="q-pa-none">
|
||||
<q-separator></q-separator>
|
||||
<q-list>
|
||||
{% include "lndhub/_instructions.html" %}
|
||||
<q-separator></q-separator>
|
||||
{% include "lndhub/_lndhub.html" %}
|
||||
</q-list>
|
||||
</q-card-section>
|
||||
</q-card>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{% endblock %} {% block scripts %} {{ window_vars(user) }}
|
||||
<script>
|
||||
Vue.component(VueQrcode.name, VueQrcode)
|
||||
|
||||
new Vue({
|
||||
el: '#vue',
|
||||
mixins: [windowMixin],
|
||||
data: function () {
|
||||
var wallets = JSON.parse('{{ user.wallets | tojson }}')
|
||||
.map(LNbits.map.wallet)
|
||||
.map(wallet => ({
|
||||
label: wallet.name,
|
||||
admin: `lndhub://admin:${wallet.adminkey}@${location.protocol}//${location.host}/lndhub/ext/`,
|
||||
invoice: `lndhub://invoice:${wallet.inkey}@${location.protocol}//${location.host}/lndhub/ext/`
|
||||
}))
|
||||
|
||||
return {
|
||||
wallets: wallets,
|
||||
selectedWallet: wallets[0]
|
||||
}
|
||||
}
|
||||
})
|
||||
</script>
|
||||
{% endblock %}
|
21
lnbits/extensions/lndhub/utils.py
Normal file
21
lnbits/extensions/lndhub/utils.py
Normal file
@ -0,0 +1,21 @@
|
||||
from binascii import unhexlify
|
||||
|
||||
from lnbits.bolt11 import Invoice
|
||||
|
||||
|
||||
def to_buffer(payment_hash: str):
|
||||
return {"type": "Buffer", "data": [b for b in unhexlify(payment_hash)]}
|
||||
|
||||
|
||||
def decoded_as_lndhub(invoice: Invoice):
|
||||
return {
|
||||
"destination": invoice.payee,
|
||||
"payment_hash": invoice.payment_hash,
|
||||
"num_satoshis": invoice.amount_msat / 1000,
|
||||
"timestamp": str(invoice.date),
|
||||
"expiry": str(invoice.expiry),
|
||||
"description": invoice.description,
|
||||
"fallback_addr": "",
|
||||
"cltv_expiry": invoice.min_final_cltv_expiry,
|
||||
"route_hints": "",
|
||||
}
|
12
lnbits/extensions/lndhub/views.py
Normal file
12
lnbits/extensions/lndhub/views.py
Normal file
@ -0,0 +1,12 @@
|
||||
from lnbits.decorators import check_user_exists
|
||||
from . import lndhub_ext, lndhub_renderer
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi.params import Depends
|
||||
from lnbits.core.models import User
|
||||
|
||||
|
||||
@lndhub_ext.get("/")
|
||||
async def lndhub_index(request: Request, user: User = Depends(check_user_exists)):
|
||||
return lndhub_renderer().TemplateResponse(
|
||||
"lndhub/index.html", {"request": request, "user": user.dict()}
|
||||
)
|
218
lnbits/extensions/lndhub/views_api.py
Normal file
218
lnbits/extensions/lndhub/views_api.py
Normal file
@ -0,0 +1,218 @@
|
||||
import time
|
||||
from base64 import urlsafe_b64encode
|
||||
|
||||
from lnbits.core.services import pay_invoice, create_invoice
|
||||
from lnbits.core.crud import get_payments, delete_expired_invoices
|
||||
from lnbits.decorators import api_validate_post_request, WalletTypeInfo, get_key_type
|
||||
from lnbits.settings import WALLET
|
||||
from lnbits import bolt11
|
||||
|
||||
from . import lndhub_ext
|
||||
from .decorators import check_wallet
|
||||
from .utils import to_buffer, decoded_as_lndhub
|
||||
from http import HTTPStatus
|
||||
from starlette.exceptions import HTTPException
|
||||
from starlette.responses import HTMLResponse, JSONResponse # type: ignore
|
||||
from fastapi.params import Depends
|
||||
from fastapi.param_functions import Query
|
||||
|
||||
|
||||
@lndhub_ext.get("/ext/getinfo")
|
||||
async def lndhub_getinfo():
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.UNAUTHORIZED,
|
||||
detail="bad auth",
|
||||
)
|
||||
|
||||
|
||||
@lndhub_ext.post("/ext/auth")
|
||||
async def lndhub_auth(
|
||||
login: str = Query(None),
|
||||
password: str = Query(None),
|
||||
refresh_token: str = Query(None),
|
||||
):
|
||||
token = (
|
||||
refresh_token
|
||||
if refresh_token
|
||||
else urlsafe_b64encode((login + ":" + password).encode("utf-8")).decode("ascii")
|
||||
)
|
||||
return {"refresh_token": token, "access_token": token}
|
||||
|
||||
|
||||
@lndhub_ext.post("/ext/addinvoice")
|
||||
async def lndhub_addinvoice(
|
||||
wallet: WalletTypeInfo = Depends(get_key_type),
|
||||
amt: str = Query(None),
|
||||
memo: str = Query(None),
|
||||
preimage: str = Query(None),
|
||||
):
|
||||
try:
|
||||
_, pr = await create_invoice(
|
||||
wallet_id=wallet.wallet.id,
|
||||
amount=int(amt),
|
||||
memo=memo,
|
||||
extra={"tag": "lndhub"},
|
||||
)
|
||||
except:
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.NOT_FOUND,
|
||||
detail="Failed to create invoice",
|
||||
)
|
||||
|
||||
invoice = bolt11.decode(pr)
|
||||
return {
|
||||
"pay_req": pr,
|
||||
"payment_request": pr,
|
||||
"add_index": "500",
|
||||
"r_hash": to_buffer(invoice.payment_hash),
|
||||
"hash": invoice.payment_hash,
|
||||
}
|
||||
|
||||
|
||||
@lndhub_ext.post("/ext/payinvoice")
|
||||
async def lndhub_payinvoice(
|
||||
wallet: WalletTypeInfo = Depends(get_key_type), invoice: str = Query(None)
|
||||
):
|
||||
try:
|
||||
await pay_invoice(
|
||||
wallet_id=wallet.wallet.id,
|
||||
payment_request=invoice,
|
||||
extra={"tag": "lndhub"},
|
||||
)
|
||||
except:
|
||||
raise HTTPException(
|
||||
status_code=HTTPStatus.NOT_FOUND,
|
||||
detail="FPayment failed",
|
||||
)
|
||||
|
||||
invoice: bolt11.Invoice = bolt11.decode(invoice)
|
||||
return {
|
||||
"payment_error": "",
|
||||
"payment_preimage": "0" * 64,
|
||||
"route": {},
|
||||
"payment_hash": invoice.payment_hash,
|
||||
"decoded": decoded_as_lndhub(invoice),
|
||||
"fee_msat": 0,
|
||||
"type": "paid_invoice",
|
||||
"fee": 0,
|
||||
"value": invoice.amount_msat / 1000,
|
||||
"timestamp": int(time.time()),
|
||||
"memo": invoice.description,
|
||||
}
|
||||
|
||||
|
||||
@lndhub_ext.get("/ext/balance")
|
||||
@check_wallet()
|
||||
async def lndhub_balance(
|
||||
wallet: WalletTypeInfo = Depends(get_key_type),
|
||||
):
|
||||
return {"BTC": {"AvailableBalance": wallet.wallet.balance}}
|
||||
|
||||
|
||||
@lndhub_ext.route("/ext/gettxs", methods=["GET"])
|
||||
@check_wallet()
|
||||
async def lndhub_gettxs(
|
||||
wallet: WalletTypeInfo = Depends(get_key_type), limit: int = Query(0, ge=0, lt=200)
|
||||
):
|
||||
for payment in await get_payments(
|
||||
wallet_id=wallet.wallet.id,
|
||||
complete=False,
|
||||
pending=True,
|
||||
outgoing=True,
|
||||
incoming=False,
|
||||
exclude_uncheckable=True,
|
||||
):
|
||||
await payment.set_pending(
|
||||
(await WALLET.get_payment_status(payment.checking_id)).pending
|
||||
)
|
||||
return [
|
||||
{
|
||||
"payment_preimage": payment.preimage,
|
||||
"payment_hash": payment.payment_hash,
|
||||
"fee_msat": payment.fee * 1000,
|
||||
"type": "paid_invoice",
|
||||
"fee": payment.fee,
|
||||
"value": int(payment.amount / 1000),
|
||||
"timestamp": payment.time,
|
||||
"memo": payment.memo if not payment.pending else "Payment in transition",
|
||||
}
|
||||
for payment in reversed(
|
||||
(
|
||||
await get_payments(
|
||||
wallet_id=wallet.wallet.id,
|
||||
pending=True,
|
||||
complete=True,
|
||||
outgoing=True,
|
||||
incoming=False,
|
||||
)
|
||||
)[:limit]
|
||||
)
|
||||
]
|
||||
|
||||
|
||||
@lndhub_ext.route("/ext/getuserinvoices", methods=["GET"])
|
||||
async def lndhub_getuserinvoices(
|
||||
wallet: WalletTypeInfo = Depends(get_key_type), limit: int = Query(0, ge=0, lt=200)
|
||||
):
|
||||
await delete_expired_invoices()
|
||||
for invoice in await get_payments(
|
||||
wallet_id=wallet.wallet.id,
|
||||
complete=False,
|
||||
pending=True,
|
||||
outgoing=False,
|
||||
incoming=True,
|
||||
exclude_uncheckable=True,
|
||||
):
|
||||
await invoice.set_pending(
|
||||
(await WALLET.get_invoice_status(invoice.checking_id)).pending
|
||||
)
|
||||
return [
|
||||
{
|
||||
"r_hash": to_buffer(invoice.payment_hash),
|
||||
"payment_request": invoice.bolt11,
|
||||
"add_index": "500",
|
||||
"description": invoice.memo,
|
||||
"payment_hash": invoice.payment_hash,
|
||||
"ispaid": not invoice.pending,
|
||||
"amt": int(invoice.amount / 1000),
|
||||
"expire_time": int(time.time() + 1800),
|
||||
"timestamp": invoice.time,
|
||||
"type": "user_invoice",
|
||||
}
|
||||
for invoice in reversed(
|
||||
(
|
||||
await get_payments(
|
||||
wallet_id=wallet.wallet.id,
|
||||
pending=True,
|
||||
complete=True,
|
||||
incoming=True,
|
||||
outgoing=False,
|
||||
)
|
||||
)[:limit]
|
||||
)
|
||||
]
|
||||
|
||||
|
||||
@lndhub_ext.get("/ext/getbtc")
|
||||
async def lndhub_getbtc(wallet: WalletTypeInfo = Depends(get_key_type)):
|
||||
"load an address for incoming onchain btc"
|
||||
return []
|
||||
|
||||
|
||||
@lndhub_ext.get("/ext/getpending")
|
||||
@check_wallet()
|
||||
async def lndhub_getpending():
|
||||
"pending onchain transactions"
|
||||
return []
|
||||
|
||||
|
||||
@lndhub_ext.get("/ext/decodeinvoice")
|
||||
async def lndhub_decodeinvoice(invoice: str = Query(None)):
|
||||
inv = bolt11.decode(invoice)
|
||||
return decoded_as_lndhub(inv)
|
||||
|
||||
|
||||
@lndhub_ext.get("/ext/checkrouteinvoice")
|
||||
async def lndhub_checkrouteinvoice():
|
||||
"not implemented on canonical lndhub"
|
||||
pass
|
Loading…
Reference in New Issue
Block a user