From ce94fc8984e276dc813578c064a10ce00405157b Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Mon, 16 Dec 2019 16:23:00 +0000 Subject: [PATCH] fix schema so payments can be sent between lnbits wallets. --- LNbits/__init__.py | 9 ++++----- LNbits/bolt11.py | 4 ++-- LNbits/data/schema.sql | 6 ++++-- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/LNbits/__init__.py b/LNbits/__init__.py index d8517b03d..5deb10564 100644 --- a/LNbits/__init__.py +++ b/LNbits/__init__.py @@ -20,7 +20,7 @@ app.jinja_env.filters["megajson"] = megajson def init(): with Database() as db: with open(os.path.join(LNBITS_PATH, "data", "schema.sql")) as schemafile: - for stmt in schemafile.read().split("\n\n"): + for stmt in schemafile.read().split(";\n\n"): db.execute(stmt, []) @@ -75,7 +75,6 @@ def lnurlwallet(): params={**withdraw_res.callback.query_params, **{"k1": withdraw_res.k1, "pr": invoice["pay_req"]}}, ) if not r.ok: - print('error2', r.text) return redirect(url_for('home')) data = json.loads(r.text) @@ -192,7 +191,7 @@ def wallet(): (usr, wallet_id), ) - transactions = db.fetchall("SELECT * FROM apipayments WHERE wallet = ?", (wallet_id,)) + transactions = db.fetchall("SELECT * FROM apipayments WHERE wallet = ? AND pending = 0", (wallet_id,)) return render_template( "wallet.html", user_wallets=user_wallets, wallet=wallet, user=usr, transactions=transactions, @@ -297,8 +296,8 @@ def api_transactions(): # payment went through, not pending anymore, save actual fees db.execute( - "UPDATE apipayments SET pending = 0, fee = ? WHERE payhash = ?", - (data["fee_msat"], invoice.payment_hash,), + "UPDATE apipayments SET pending = 0, fee = ? WHERE payhash = ? AND wallet = ?", + (data["fee_msat"], invoice.payment_hash, wallet['id']), ) return jsonify({"PAID": "TRUE"}), 200 diff --git a/LNbits/bolt11.py b/LNbits/bolt11.py index a60ba7969..27c5307ef 100644 --- a/LNbits/bolt11.py +++ b/LNbits/bolt11.py @@ -49,9 +49,9 @@ def decode(pr: str) -> Invoice: if tag == "d": invoice.description = trim_to_bytes(tagdata).decode("utf-8") elif tag == "h" and data_length == 52: - invoice.description = str(hexlify(trim_to_bytes(tagdata))) + invoice.description = hexlify(trim_to_bytes(tagdata)).decode('ascii') elif tag == "p" and data_length == 52: - invoice.payment_hash = str(hexlify(trim_to_bytes(tagdata))) + invoice.payment_hash = hexlify(trim_to_bytes(tagdata)).decode('ascii') return invoice diff --git a/LNbits/data/schema.sql b/LNbits/data/schema.sql index e5d8bf51a..2c9991d75 100644 --- a/LNbits/data/schema.sql +++ b/LNbits/data/schema.sql @@ -13,13 +13,15 @@ CREATE TABLE IF NOT EXISTS wallets ( ); CREATE TABLE IF NOT EXISTS apipayments ( - payhash text PRIMARY KEY, + payhash text NOT NULL, amount integer NOT NULL, fee integer NOT NULL DEFAULT 0, wallet text NOT NULL, pending boolean NOT NULL, memo text, - time timestamp NOT NULL DEFAULT (strftime('%s', 'now')) + time timestamp NOT NULL DEFAULT (strftime('%s', 'now')), + + UNIQUE (wallet, payhash) ); CREATE VIEW IF NOT EXISTS balances AS