2020-02-19 20:52:34 +01:00
|
|
|
import importlib
|
2019-12-15 18:20:26 +01:00
|
|
|
import json
|
2019-12-13 17:56:19 +01:00
|
|
|
import requests
|
2020-01-31 21:07:05 +01:00
|
|
|
import uuid
|
2019-12-13 22:51:41 +01:00
|
|
|
|
2020-03-07 22:27:00 +01:00
|
|
|
from flask import Flask, redirect, render_template, request, url_for
|
2020-03-04 23:11:15 +01:00
|
|
|
from flask_assets import Environment, Bundle
|
|
|
|
from flask_compress import Compress
|
2020-01-31 21:07:05 +01:00
|
|
|
from flask_talisman import Talisman
|
|
|
|
from lnurl import Lnurl, LnurlWithdrawResponse
|
2019-12-13 17:56:19 +01:00
|
|
|
|
2020-01-31 21:07:05 +01:00
|
|
|
from .core import core_app
|
2020-02-10 13:29:20 +01:00
|
|
|
from .db import init_databases, open_db
|
2020-02-18 20:27:04 +01:00
|
|
|
from .helpers import ExtensionManager, megajson
|
2020-03-07 22:27:00 +01:00
|
|
|
from .settings import WALLET, DEFAULT_USER_WALLET_NAME
|
2019-12-13 17:59:40 +01:00
|
|
|
|
|
|
|
|
2019-12-13 17:56:19 +01:00
|
|
|
app = Flask(__name__)
|
2020-02-19 20:52:34 +01:00
|
|
|
valid_extensions = [ext for ext in ExtensionManager().extensions if ext.is_valid]
|
|
|
|
|
|
|
|
|
|
|
|
# optimization & security
|
|
|
|
# -----------------------
|
|
|
|
|
2020-03-04 23:11:15 +01:00
|
|
|
Compress(app)
|
2020-02-10 13:29:20 +01:00
|
|
|
Talisman(
|
|
|
|
app,
|
|
|
|
content_security_policy={
|
|
|
|
"default-src": [
|
|
|
|
"'self'",
|
|
|
|
"'unsafe-eval'",
|
|
|
|
"'unsafe-inline'",
|
|
|
|
"cdnjs.cloudflare.com",
|
|
|
|
"code.ionicframework.com",
|
|
|
|
"code.jquery.com",
|
|
|
|
"fonts.googleapis.com",
|
|
|
|
"fonts.gstatic.com",
|
|
|
|
"maxcdn.bootstrapcdn.com",
|
2020-03-04 23:11:15 +01:00
|
|
|
"github.com",
|
|
|
|
"avatars2.githubusercontent.com",
|
2020-02-10 13:29:20 +01:00
|
|
|
]
|
|
|
|
},
|
|
|
|
)
|
2020-01-31 21:07:05 +01:00
|
|
|
|
2020-02-19 20:52:34 +01:00
|
|
|
|
|
|
|
# blueprints / extensions
|
|
|
|
# -----------------------
|
|
|
|
|
|
|
|
app.register_blueprint(core_app)
|
|
|
|
|
|
|
|
for ext in valid_extensions:
|
|
|
|
try:
|
|
|
|
ext_module = importlib.import_module(f"lnbits.extensions.{ext.code}")
|
|
|
|
app.register_blueprint(getattr(ext_module, f"{ext.code}_ext"), url_prefix=f"/{ext.code}")
|
|
|
|
except Exception:
|
|
|
|
raise ImportError(f"Please make sure that the extension `{ext.code}` follows conventions.")
|
|
|
|
|
|
|
|
|
2020-01-31 21:07:05 +01:00
|
|
|
# filters
|
2020-02-19 20:52:34 +01:00
|
|
|
# -------
|
|
|
|
|
2020-03-04 23:11:15 +01:00
|
|
|
app.jinja_env.globals["DEBUG"] = app.config["DEBUG"]
|
2020-02-19 20:52:34 +01:00
|
|
|
app.jinja_env.globals["EXTENSIONS"] = valid_extensions
|
2019-12-14 21:26:26 +01:00
|
|
|
app.jinja_env.filters["megajson"] = megajson
|
2019-12-13 17:56:19 +01:00
|
|
|
|
2020-01-31 21:07:05 +01:00
|
|
|
|
2020-03-04 23:11:15 +01:00
|
|
|
# assets
|
|
|
|
# ------
|
|
|
|
|
|
|
|
assets = Environment(app)
|
|
|
|
assets.url = app.static_url_path
|
|
|
|
assets.register("base_css", Bundle("scss/base.scss", filters="pyscss", output="css/base.css"))
|
|
|
|
|
|
|
|
|
2020-02-19 20:52:34 +01:00
|
|
|
# init
|
|
|
|
# ----
|
2019-12-13 17:56:19 +01:00
|
|
|
|
2020-03-04 23:11:15 +01:00
|
|
|
|
2019-12-14 00:34:19 +01:00
|
|
|
@app.before_first_request
|
|
|
|
def init():
|
2020-02-10 13:29:20 +01:00
|
|
|
init_databases()
|
2019-12-14 00:34:19 +01:00
|
|
|
|
2019-12-13 17:56:19 +01:00
|
|
|
|
2020-02-19 20:52:34 +01:00
|
|
|
# vvvvvvvvvvvvvvvvvvvvvvvvvvv
|
|
|
|
# move the rest to `core_app`
|
|
|
|
# vvvvvvvvvvvvvvvvvvvvvvvvvvv
|
|
|
|
# vvvvvvvvvvvvvvvvvvvvvvvvvvv
|
|
|
|
|
|
|
|
|
2019-12-17 16:31:35 +01:00
|
|
|
@app.route("/lnurl")
|
|
|
|
def lnurl():
|
|
|
|
lnurl = request.args.get("lightning")
|
|
|
|
return render_template("lnurl.html", lnurl=lnurl)
|
2019-12-13 17:56:19 +01:00
|
|
|
|
2019-12-18 14:52:49 +01:00
|
|
|
|
2019-12-13 17:56:19 +01:00
|
|
|
@app.route("/lnurlwallet")
|
|
|
|
def lnurlwallet():
|
2019-12-15 09:39:59 +01:00
|
|
|
lnurl = Lnurl(request.args.get("lightning"))
|
|
|
|
r = requests.get(lnurl.url)
|
2019-12-15 18:20:26 +01:00
|
|
|
if not r.ok:
|
2019-12-18 14:52:49 +01:00
|
|
|
return redirect(url_for("home"))
|
2019-12-15 09:39:59 +01:00
|
|
|
|
2019-12-15 18:20:26 +01:00
|
|
|
data = json.loads(r.text)
|
2019-12-18 14:52:49 +01:00
|
|
|
if data.get("status") == "ERROR":
|
|
|
|
return redirect(url_for("home"))
|
2019-12-15 18:20:26 +01:00
|
|
|
|
|
|
|
withdraw_res = LnurlWithdrawResponse(**data)
|
|
|
|
|
2020-01-10 21:26:42 +01:00
|
|
|
_, pay_hash, pay_req = WALLET.create_invoice(withdraw_res.max_sats, "LNbits lnurl funding")
|
2019-12-14 12:21:17 +01:00
|
|
|
|
2019-12-14 19:49:32 +01:00
|
|
|
r = requests.get(
|
2019-12-14 12:21:17 +01:00
|
|
|
withdraw_res.callback.base,
|
2020-01-10 21:26:42 +01:00
|
|
|
params={**withdraw_res.callback.query_params, **{"k1": withdraw_res.k1, "pr": pay_req}},
|
2019-12-14 12:21:17 +01:00
|
|
|
)
|
2020-01-10 21:26:42 +01:00
|
|
|
|
2019-12-15 18:20:26 +01:00
|
|
|
if not r.ok:
|
2019-12-18 14:52:49 +01:00
|
|
|
return redirect(url_for("home"))
|
2019-12-15 18:20:26 +01:00
|
|
|
data = json.loads(r.text)
|
2019-12-13 17:56:19 +01:00
|
|
|
|
2019-12-15 18:20:26 +01:00
|
|
|
for i in range(10):
|
2020-01-10 21:26:42 +01:00
|
|
|
r = WALLET.get_invoice_status(pay_hash).raw_response
|
2019-12-15 18:20:26 +01:00
|
|
|
if not r.ok:
|
|
|
|
continue
|
|
|
|
|
2019-12-14 12:21:17 +01:00
|
|
|
data = r.json()
|
2019-12-15 18:20:26 +01:00
|
|
|
break
|
2019-12-13 17:56:19 +01:00
|
|
|
|
2020-01-31 21:07:05 +01:00
|
|
|
with open_db() as db:
|
2019-12-15 09:39:59 +01:00
|
|
|
wallet_id = uuid.uuid4().hex
|
|
|
|
user_id = uuid.uuid4().hex
|
|
|
|
wallet_name = DEFAULT_USER_WALLET_NAME
|
2019-12-14 19:49:32 +01:00
|
|
|
adminkey = uuid.uuid4().hex
|
|
|
|
inkey = uuid.uuid4().hex
|
2019-12-13 17:56:19 +01:00
|
|
|
|
2019-12-15 09:39:59 +01:00
|
|
|
db.execute("INSERT INTO accounts (id) VALUES (?)", (user_id,))
|
2019-12-14 12:21:17 +01:00
|
|
|
db.execute(
|
2019-12-14 19:49:32 +01:00
|
|
|
"INSERT INTO wallets (id, name, user, adminkey, inkey) VALUES (?, ?, ?, ?, ?)",
|
2019-12-15 09:39:59 +01:00
|
|
|
(wallet_id, wallet_name, user_id, adminkey, inkey),
|
2019-12-13 17:56:19 +01:00
|
|
|
)
|
2019-12-15 18:20:26 +01:00
|
|
|
db.execute(
|
|
|
|
"INSERT INTO apipayments (payhash, amount, wallet, pending, memo) VALUES (?, ?, ?, 0, ?)",
|
2020-01-10 21:26:42 +01:00
|
|
|
(pay_hash, withdraw_res.max_sats * 1000, wallet_id, "LNbits lnurl funding",),
|
2019-12-15 18:20:26 +01:00
|
|
|
)
|
2019-12-14 12:21:17 +01:00
|
|
|
|
2019-12-15 09:39:59 +01:00
|
|
|
return redirect(url_for("wallet", usr=user_id, wal=wallet_id))
|
2019-12-13 17:56:19 +01:00
|
|
|
|
|
|
|
|
2020-03-04 23:11:15 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
app.run()
|