diff --git a/LNbits/__init__.py b/LNbits/__init__.py index 345ae1ac..e5c268f0 100644 --- a/LNbits/__init__.py +++ b/LNbits/__init__.py @@ -16,12 +16,10 @@ import time import json import bech32 - +from .db import Database, DEFAULT_PATH from .helpers import encrypt -# DATABASE = 'database.db' - INVOICE_KEY = "YOUR-LNTXBOT-INVOICE-KEY" # In the lntxbot bot on telegram type "/api" ADMIN_KEY = "YOUR-LNTXBOT-ADMIN-KEY" API_ENDPOINT = "YOUR-LNTXBOT-API-BASE-URL" @@ -29,9 +27,6 @@ API_ENDPOINT = "YOUR-LNTXBOT-API-BASE-URL" app = Flask(__name__) -DEFAULT_PATH = "database.sqlite3" - - def db_connect(db_path=DEFAULT_PATH): con = sqlite3.connect(db_path) return con @@ -45,58 +40,21 @@ def home(): @app.route("/deletewallet") def deletewallet(): - thewal = request.args.get("wal") - con = db_connect() - cur = con.cursor() - print(thewal) - cur.execute("select * from wallets WHERE hash = '" + str(thewal) + "'") - rowss = cur.fetchall() + with Database() as db: + rowss = db.fetchall("SELECT * FROM wallets WHERE hash = ?", (thewal,)) - if len(rowss) > 0: + if len(rowss) > 0: + db.execute("UPDATE wallets SET user = ? WHERE hash = ?", (f"del{rowss[0][4]}", rowss[0][0])) + db.execute("UPDATE wallets SET adminkey = ? WHERE hash = ?", (f"del{rowss[0][5]}", rowss[0][0])) + db.execute("UPDATE wallets SET inkey = ? WHERE hash = ?", (f"del{rowss[0][6]}", rowss[0][0])) + rowsss = db.fetchall("SELECT * FROM wallets WHERE user = ?", (rowss[0][4],)) - cur.close() - print(rowss) + if len(rowsss) > 0: + return render_template("deletewallet.html", theid=rowsss[0][4], thewal=rowsss[0][0]) - con = db_connect() - cur = con.cursor() - - cur.execute("UPDATE wallets SET user = '" + "del" + rowss[0][4] + "' WHERE hash = '" + rowss[0][0] + "'") - - con.commit() - cur.close() - - con = db_connect() - cur = con.cursor() - - cur.execute("UPDATE wallets SET adminkey = '" + "del" + rowss[0][5] + "' WHERE hash = '" + rowss[0][0] + "'") - - con.commit() - cur.close() - - con = db_connect() - cur = con.cursor() - - cur.execute("UPDATE wallets SET inkey = '" + "del" + rowss[0][6] + "' WHERE hash = '" + rowss[0][0] + "'") - - con.commit() - cur.close() - - con = db_connect() - cur = con.cursor() - print(thewal) - cur.execute("select * from wallets WHERE user = '" + rowss[0][4] + "'") - rowsss = cur.fetchall() - - if len(rowsss) > 0: - cur.close() - return render_template("deletewallet.html", theid=rowsss[0][4], thewal=rowsss[0][0]) - else: - return render_template("index.html") - - else: - return render_template("index.html") + return render_template("index.html") @app.route("/lnurlwallet") diff --git a/database.sqlite3 b/LNbits/data/database.sqlite3 similarity index 99% rename from database.sqlite3 rename to LNbits/data/database.sqlite3 index 12bc1151..f5a37d79 100644 Binary files a/database.sqlite3 and b/LNbits/data/database.sqlite3 differ diff --git a/LNbits/db.py b/LNbits/db.py new file mode 100644 index 00000000..cb126b70 --- /dev/null +++ b/LNbits/db.py @@ -0,0 +1,30 @@ +import os +import sqlite3 + + +LNBITS_PATH = os.path.dirname(os.path.realpath(__file__)) +DEFAULT_PATH = os.path.join(LNBITS_PATH, "data", "database.sqlite3") + + +class Database: + def __init__(self, db_path: str = DEFAULT_PATH): + self.path = db_path + self.connection = sqlite3.connect(db_path) + self.cursor = self.connection.cursor() + + def __enter__(self): + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + self.cursor.close() + self.connection.close() + + def fetchall(self, query: str, values: tuple) -> list: + """Given a query, return cursor.fetchall() rows.""" + self.cursor.execute(query, values) + return self.cursor.fetchall() + + def execute(self, query: str, values: tuple) -> None: + """Given a query, cursor.execute() it.""" + self.cursor.execute(query, values) + self.connection.commit()