refactor: clean lnurlwallet view

This commit is contained in:
Eneko Illarramendi 2019-12-14 12:21:17 +01:00
parent 21a6958458
commit 80c43701b3
3 changed files with 35 additions and 80 deletions

View file

@ -1,5 +1,5 @@
import os
import lnurl import lnurl
import os
import requests import requests
from flask import Flask, jsonify, render_template, request from flask import Flask, jsonify, render_template, request
@ -56,100 +56,52 @@ def deletewallet():
@app.route("/lnurlwallet") @app.route("/lnurlwallet")
def lnurlwallet(): def lnurlwallet():
withdraw_res = lnurl.handle(request.args.get("lightning"))
invoice = WALLET.create_invoice(withdraw_res.max_sats).json()
payment_hash = invoice["payment_hash"]
# put in a function rrr = requests.get(
thestr = request.args.get("lightning") withdraw_res.callback.base,
lnurll = lnurl.decode(thestr) params={**withdraw_res.callback.query_params, **{"k1": withdraw_res.k1, "pr": invoice["pay_req"]}},
r = requests.get(url=lnurll) )
data = r.json()
callback = data["callback"]
maxwithdraw = data["maxWithdrawable"]
withdraw = int(maxwithdraw / 1000)
k1 = data["k1"]
# get invoice
rr = WALLET.create_invoice(withdraw)
dataa = rr.json()
# get callback
pay_req = dataa["pay_req"]
payment_hash = dataa["payment_hash"]
invurl = callback + "&k1=" + k1 + "&pr=" + pay_req
rrr = requests.get(url=invurl)
dataaa = rrr.json() dataaa = rrr.json()
print(dataaa) if dataaa["status"] != "OK":
print("poo") """TODO: show some kind of error?"""
return render_template("index.html")
if dataaa["status"] == "OK":
data = "" data = ""
while data == "": while data == "":
r = WALLET.get_invoice_status(payment_hash) r = WALLET.get_invoice_status(payment_hash)
data = r.json() data = r.json()
print(r.json())
with Database() as db:
adminkey = encrypt(payment_hash)[0:20] adminkey = encrypt(payment_hash)[0:20]
inkey = encrypt(adminkey)[0:20] inkey = encrypt(adminkey)[0:20]
thewal = encrypt(inkey)[0:20] thewal = encrypt(inkey)[0:20]
theid = encrypt(thewal)[0:20] theid = encrypt(thewal)[0:20]
thenme = "Bitcoin LN Wallet" thenme = "Bitcoin LN Wallet"
con = db_connect() db.execute("INSERT INTO accounts (userhash) VALUES (?)", (theid,))
cur = con.cursor()
cur.execute("INSERT INTO accounts (userhash) VALUES ('" + theid + "')")
con.commit()
cur.close()
con = db_connect()
cur = con.cursor()
adminkey = encrypt(theid) adminkey = encrypt(theid)
inkey = encrypt(adminkey) inkey = encrypt(adminkey)
cur.execute( db.execute(
"INSERT INTO wallets (hash, name, user, adminkey, inkey) VALUES ('" "INSERT INTO wallets (hash, name, user, adminkey, inkey) VALUES (?, ?, ?, ?, ?)",
+ thewal (thewal, thenme, theid, adminkey, inkey),
+ "',',0,"
+ str(withdraw)
+ "','0','"
+ thenme
+ "','"
+ theid
+ "','"
+ adminkey
+ "','"
+ inkey
+ "')"
) )
con.commit()
cur.close()
con = db_connect()
cur = con.cursor()
print(thewal)
cur.execute("select * from wallets WHERE user = '" + str(theid) + "'")
# rows = cur.fetchall()
con.commit()
cur.close()
return render_template( return render_template(
"lnurlwallet.html", "lnurlwallet.html",
len=len("1"), len=len("1"),
walnme=thenme, walnme=thenme,
walbal=str(withdraw), walbal=withdraw_res.max_sats,
theid=theid, theid=theid,
thewal=thewal, thewal=thewal,
adminkey=adminkey, adminkey=adminkey,
inkey=inkey, inkey=inkey,
) )
else:
return render_template("index.html")
@app.route("/wallet") @app.route("/wallet")
@ -348,7 +300,7 @@ def api_transactions():
return jsonify({"ERROR": "UNEXPECTED PAYMENT ERROR"}), 500 return jsonify({"ERROR": "UNEXPECTED PAYMENT ERROR"}), 500
data = r.json() data = r.json()
if r.ok and 'error' in data: if r.ok and "error" in data:
# payment didn't went through, delete it here # payment didn't went through, delete it here
# (these guarantees specific to lntxbot) # (these guarantees specific to lntxbot)
db.execute("DELETE FROM apipayments WHERE payhash = ?", (invoice.payment_hash,)) db.execute("DELETE FROM apipayments WHERE payhash = ?", (invoice.payment_hash,))

View file

@ -18,6 +18,7 @@ class Wallet(ABC):
def pay_invoice(self, bolt11: str) -> WalletResponse: def pay_invoice(self, bolt11: str) -> WalletResponse:
pass pass
@abstractmethod
def get_invoice_status(self, payment_hash: str) -> WalletResponse: def get_invoice_status(self, payment_hash: str) -> WalletResponse:
pass pass

View file

@ -7,8 +7,10 @@ flask==1.1.1
idna==2.8 idna==2.8
itsdangerous==1.1.0 itsdangerous==1.1.0
jinja2==2.10.3 jinja2==2.10.3
lnurl==0.1.1 lnurl==0.2.0
markupsafe==1.1.1 markupsafe==1.1.1
pydantic==1.2
requests==2.22.0 requests==2.22.0
typing-extensions==3.7.4.1
urllib3==1.25.7 urllib3==1.25.7
werkzeug==0.16.0 werkzeug==0.16.0