2019-12-14 19:49:32 +01:00
|
|
|
import uuid
|
2019-12-14 12:21:17 +01:00
|
|
|
import os
|
2019-12-15 18:20:26 +01:00
|
|
|
import json
|
2019-12-13 17:56:19 +01:00
|
|
|
import requests
|
2019-12-13 22:51:41 +01:00
|
|
|
|
2019-12-14 19:49:32 +01:00
|
|
|
from flask import Flask, jsonify, render_template, request, redirect, url_for
|
2019-12-15 09:39:59 +01:00
|
|
|
from lnurl import Lnurl, LnurlWithdrawResponse
|
2019-12-13 17:56:19 +01:00
|
|
|
|
2019-12-14 02:59:35 +01:00
|
|
|
from . import bolt11
|
2019-12-13 23:41:39 +01:00
|
|
|
from .db import Database
|
2019-12-14 21:26:26 +01:00
|
|
|
from .helpers import megajson
|
|
|
|
from .settings import LNBITS_PATH, WALLET, DEFAULT_USER_WALLET_NAME
|
2019-12-13 17:59:40 +01:00
|
|
|
|
|
|
|
|
2019-12-13 17:56:19 +01:00
|
|
|
app = Flask(__name__)
|
2019-12-14 21:26:26 +01:00
|
|
|
app.jinja_env.filters["megajson"] = megajson
|
2019-12-13 17:56:19 +01:00
|
|
|
|
|
|
|
|
2019-12-14 00:34:19 +01:00
|
|
|
@app.before_first_request
|
|
|
|
def init():
|
|
|
|
with Database() as db:
|
2019-12-14 10:19:41 +01:00
|
|
|
with open(os.path.join(LNBITS_PATH, "data", "schema.sql")) as schemafile:
|
2019-12-16 17:23:00 +01:00
|
|
|
for stmt in schemafile.read().split(";\n\n"):
|
2019-12-14 00:34:19 +01:00
|
|
|
db.execute(stmt, [])
|
|
|
|
|
|
|
|
|
2019-12-13 17:56:19 +01:00
|
|
|
@app.route("/")
|
|
|
|
def home():
|
|
|
|
return render_template("index.html")
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/deletewallet")
|
|
|
|
def deletewallet():
|
2019-12-15 09:18:06 +01:00
|
|
|
user_id = request.args.get("usr")
|
|
|
|
wallet_id = request.args.get("wal")
|
2019-12-13 17:56:19 +01:00
|
|
|
|
2019-12-13 18:14:25 +01:00
|
|
|
with Database() as db:
|
2019-12-14 19:49:32 +01:00
|
|
|
db.execute(
|
|
|
|
"""
|
|
|
|
UPDATE wallets AS w SET
|
|
|
|
user = 'del:' || w.user,
|
|
|
|
adminkey = 'del:' || w.adminkey,
|
|
|
|
inkey = 'del:' || w.inkey
|
|
|
|
WHERE id = ? AND user = ?
|
|
|
|
""",
|
2019-12-15 09:18:06 +01:00
|
|
|
(wallet_id, user_id),
|
2019-12-14 19:49:32 +01:00
|
|
|
)
|
2019-12-13 17:56:19 +01:00
|
|
|
|
2019-12-15 09:18:06 +01:00
|
|
|
next_wallet = db.fetchone("SELECT id FROM wallets WHERE user = ?", (user_id,))
|
|
|
|
|
2019-12-14 19:49:32 +01:00
|
|
|
if next_wallet:
|
2019-12-15 09:18:06 +01:00
|
|
|
return redirect(url_for("wallet", usr=user_id, wal=next_wallet[0]))
|
2019-12-13 17:56:19 +01:00
|
|
|
|
2019-12-15 09:18:06 +01:00
|
|
|
return redirect(url_for("home"))
|
2019-12-13 17:56:19 +01:00
|
|
|
|
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
|
|
|
|
|
|
|
@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:
|
|
|
|
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)
|
|
|
|
if data.get('status') == 'ERROR':
|
|
|
|
return redirect(url_for('home'))
|
|
|
|
|
|
|
|
withdraw_res = LnurlWithdrawResponse(**data)
|
|
|
|
|
|
|
|
invoice = WALLET.create_invoice(withdraw_res.max_sats, 'lnbits lnurl funding').json()
|
2019-12-14 12:21:17 +01:00
|
|
|
payment_hash = invoice["payment_hash"]
|
|
|
|
|
2019-12-14 19:49:32 +01:00
|
|
|
r = requests.get(
|
2019-12-14 12:21:17 +01:00
|
|
|
withdraw_res.callback.base,
|
|
|
|
params={**withdraw_res.callback.query_params, **{"k1": withdraw_res.k1, "pr": invoice["pay_req"]}},
|
|
|
|
)
|
2019-12-15 18:20:26 +01:00
|
|
|
if not r.ok:
|
|
|
|
return redirect(url_for('home'))
|
|
|
|
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):
|
2019-12-14 12:21:17 +01:00
|
|
|
r = WALLET.get_invoice_status(payment_hash)
|
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
|
|
|
|
2019-12-14 12:21:17 +01:00
|
|
|
with Database() 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, ?)",
|
|
|
|
(payment_hash, withdraw_res.max_sats * 1000, wallet_id, "lnbits lnurl funding",),
|
|
|
|
)
|
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
|
|
|
|
|
|
|
|
|
|
|
@app.route("/wallet")
|
|
|
|
def wallet():
|
2019-12-14 19:49:32 +01:00
|
|
|
usr = request.args.get("usr")
|
|
|
|
wallet_id = request.args.get("wal")
|
2019-12-15 19:15:13 +01:00
|
|
|
wallet_name = request.args.get("nme")
|
2019-12-14 19:49:32 +01:00
|
|
|
|
|
|
|
# just usr: return a the first user wallet or create one if none found
|
|
|
|
# usr and wallet_id: return that wallet or create it if it doesn't exist
|
|
|
|
# usr, wallet_id and wallet_name: same as above, but use the specified name
|
|
|
|
# usr and wallet_name: generate a wallet_id and create
|
|
|
|
# wallet_id and wallet_name: create a user, then move an existing wallet or create
|
|
|
|
# just wallet_name: create a user, then generate a wallet_id and create
|
|
|
|
# nothing: create everything
|
2019-12-13 17:56:19 +01:00
|
|
|
|
2019-12-13 18:41:22 +01:00
|
|
|
with Database() as db:
|
2019-12-14 19:49:32 +01:00
|
|
|
# ensure this user exists
|
|
|
|
# -------------------------------
|
|
|
|
|
|
|
|
if not usr:
|
|
|
|
usr = uuid.uuid4().hex
|
|
|
|
return redirect(url_for("wallet", usr=usr, wal=wallet_id, nme=wallet_name))
|
2019-12-13 18:41:22 +01:00
|
|
|
|
|
|
|
db.execute(
|
2019-12-14 19:49:32 +01:00
|
|
|
"""
|
2019-12-15 18:20:26 +01:00
|
|
|
INSERT OR IGNORE INTO accounts (id) VALUES (?)
|
2019-12-14 19:49:32 +01:00
|
|
|
""",
|
|
|
|
(usr,),
|
2019-12-13 18:41:22 +01:00
|
|
|
)
|
|
|
|
|
2019-12-14 19:49:32 +01:00
|
|
|
user_wallets = db.fetchall("SELECT * FROM wallets WHERE user = ?", (usr,))
|
|
|
|
|
|
|
|
if not wallet_id:
|
2019-12-15 19:15:13 +01:00
|
|
|
if user_wallets and not wallet_name:
|
|
|
|
# fetch the first wallet from this user
|
|
|
|
# -------------------------------------
|
2019-12-14 19:49:32 +01:00
|
|
|
wallet_id = user_wallets[0]["id"]
|
|
|
|
else:
|
2019-12-15 19:15:13 +01:00
|
|
|
# create for this user
|
|
|
|
# --------------------
|
|
|
|
wallet_name = wallet_name or DEFAULT_USER_WALLET_NAME
|
2019-12-14 19:49:32 +01:00
|
|
|
wallet_id = uuid.uuid4().hex
|
|
|
|
db.execute(
|
|
|
|
"""
|
|
|
|
INSERT INTO wallets (id, name, user, adminkey, inkey)
|
|
|
|
VALUES (?, ?, ?, ?, ?)
|
|
|
|
""",
|
|
|
|
(wallet_id, wallet_name, usr, uuid.uuid4().hex, uuid.uuid4().hex),
|
|
|
|
)
|
|
|
|
|
|
|
|
return redirect(url_for("wallet", usr=usr, wal=wallet_id, nme=wallet_name))
|
|
|
|
|
|
|
|
# if wallet_id is given, try to move it to this user or create
|
|
|
|
# ------------------------------------------------------------
|
|
|
|
db.execute(
|
|
|
|
"""
|
2019-12-15 19:15:13 +01:00
|
|
|
INSERT OR REPLACE INTO wallets (id, user, name, adminkey, inkey)
|
|
|
|
VALUES (?, ?,
|
|
|
|
coalesce((SELECT name FROM wallets WHERE id = ?), ?),
|
2019-12-15 18:20:26 +01:00
|
|
|
coalesce((SELECT adminkey FROM wallets WHERE id = ?), ?),
|
|
|
|
coalesce((SELECT inkey FROM wallets WHERE id = ?), ?)
|
|
|
|
)
|
2019-12-14 19:49:32 +01:00
|
|
|
""",
|
2019-12-15 19:15:13 +01:00
|
|
|
(wallet_id, usr,
|
|
|
|
wallet_id, wallet_name or DEFAULT_USER_WALLET_NAME,
|
2019-12-15 18:20:26 +01:00
|
|
|
wallet_id, uuid.uuid4().hex,
|
|
|
|
wallet_id, uuid.uuid4().hex),
|
2019-12-14 19:49:32 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
# finally, get the wallet with balance and transactions
|
|
|
|
# -----------------------------------------------------
|
|
|
|
|
|
|
|
wallet = db.fetchone(
|
|
|
|
"""
|
|
|
|
SELECT
|
|
|
|
coalesce(
|
|
|
|
(SELECT balance/1000 FROM balances WHERE wallet = wallets.id),
|
|
|
|
0
|
|
|
|
) AS balance,
|
2019-12-14 21:26:26 +01:00
|
|
|
*
|
2019-12-14 19:49:32 +01:00
|
|
|
FROM wallets
|
|
|
|
WHERE user = ? AND id = ?
|
|
|
|
""",
|
|
|
|
(usr, wallet_id),
|
|
|
|
)
|
|
|
|
|
2019-12-16 17:23:00 +01:00
|
|
|
transactions = db.fetchall("SELECT * FROM apipayments WHERE wallet = ? AND pending = 0", (wallet_id,))
|
2019-12-14 19:49:32 +01:00
|
|
|
|
2019-12-14 04:11:41 +01:00
|
|
|
return render_template(
|
2019-12-14 19:49:32 +01:00
|
|
|
"wallet.html", user_wallets=user_wallets, wallet=wallet, user=usr, transactions=transactions,
|
2019-12-14 04:11:41 +01:00
|
|
|
)
|
2019-12-13 18:41:22 +01:00
|
|
|
|
2019-12-13 17:56:19 +01:00
|
|
|
|
|
|
|
@app.route("/v1/invoices", methods=["GET", "POST"])
|
|
|
|
def api_invoices():
|
2019-12-13 19:36:46 +01:00
|
|
|
if request.headers["Content-Type"] != "application/json":
|
2019-12-14 04:11:41 +01:00
|
|
|
return jsonify({"ERROR": "MUST BE JSON"}), 400
|
2019-12-13 17:56:19 +01:00
|
|
|
|
2019-12-13 19:36:46 +01:00
|
|
|
postedjson = request.json
|
2019-12-13 17:56:19 +01:00
|
|
|
|
2019-12-13 19:36:46 +01:00
|
|
|
if "value" not in postedjson:
|
2019-12-14 04:11:41 +01:00
|
|
|
return jsonify({"ERROR": "NO VALUE"}), 400
|
2019-12-13 17:56:19 +01:00
|
|
|
|
2019-12-13 19:36:46 +01:00
|
|
|
if not postedjson["value"].isdigit():
|
2019-12-14 04:11:41 +01:00
|
|
|
return jsonify({"ERROR": "VALUE MUST BE A NUMBER"}), 400
|
|
|
|
|
2019-12-14 10:19:41 +01:00
|
|
|
if int(postedjson["value"]) < 0:
|
2019-12-14 04:11:41 +01:00
|
|
|
return jsonify({"ERROR": "AMOUNTLESS INVOICES NOT SUPPORTED"}), 400
|
2019-12-13 19:36:46 +01:00
|
|
|
|
|
|
|
if "memo" not in postedjson:
|
2019-12-14 04:11:41 +01:00
|
|
|
return jsonify({"ERROR": "NO MEMO"}), 400
|
2019-12-13 19:36:46 +01:00
|
|
|
|
|
|
|
with Database() as db:
|
2019-12-14 19:49:32 +01:00
|
|
|
wallet = db.fetchone(
|
|
|
|
"SELECT id FROM wallets WHERE inkey = ? OR adminkey = ?",
|
2019-12-14 02:59:35 +01:00
|
|
|
(request.headers["Grpc-Metadata-macaroon"], request.headers["Grpc-Metadata-macaroon"],),
|
|
|
|
)
|
2019-12-13 19:36:46 +01:00
|
|
|
|
2019-12-14 19:49:32 +01:00
|
|
|
if not wallet:
|
2019-12-13 19:36:46 +01:00
|
|
|
return jsonify({"ERROR": "NO KEY"}), 200
|
|
|
|
|
2019-12-14 10:13:27 +01:00
|
|
|
r = WALLET.create_invoice(postedjson["value"], postedjson["memo"])
|
2019-12-15 18:20:26 +01:00
|
|
|
if not r.ok or r.json().get('error'):
|
|
|
|
return jsonify({"ERROR": "UNEXPECTED BACKEND ERROR"}), 500
|
|
|
|
|
2019-12-13 19:36:46 +01:00
|
|
|
data = r.json()
|
|
|
|
|
|
|
|
pay_req = data["pay_req"]
|
|
|
|
payment_hash = data["payment_hash"]
|
2019-12-14 19:49:32 +01:00
|
|
|
amount_msat = int(postedjson["value"]) * 1000
|
2019-12-13 19:36:46 +01:00
|
|
|
|
|
|
|
db.execute(
|
2019-12-15 18:20:26 +01:00
|
|
|
"INSERT INTO apipayments (payhash, amount, wallet, pending, memo) VALUES (?, ?, ?, 1, ?)",
|
2019-12-14 19:49:32 +01:00
|
|
|
(payment_hash, amount_msat, wallet["id"], postedjson["memo"],),
|
2019-12-13 19:36:46 +01:00
|
|
|
)
|
|
|
|
|
2019-12-13 22:45:03 +01:00
|
|
|
return jsonify({"pay_req": pay_req, "payment_hash": payment_hash}), 200
|
2019-12-13 17:56:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
@app.route("/v1/channels/transactions", methods=["GET", "POST"])
|
|
|
|
def api_transactions():
|
2019-12-13 22:45:03 +01:00
|
|
|
if request.headers["Content-Type"] != "application/json":
|
|
|
|
return jsonify({"ERROR": "MUST BE JSON"}), 200
|
2019-12-13 17:56:19 +01:00
|
|
|
|
2019-12-14 02:59:35 +01:00
|
|
|
data = request.json
|
2019-12-13 17:56:19 +01:00
|
|
|
|
2019-12-14 02:59:35 +01:00
|
|
|
if "payment_request" not in data:
|
2019-12-13 17:56:19 +01:00
|
|
|
return jsonify({"ERROR": "NO PAY REQ"}), 200
|
|
|
|
|
2019-12-13 22:45:03 +01:00
|
|
|
with Database() as db:
|
2019-12-14 19:49:32 +01:00
|
|
|
wallet = db.fetchone("SELECT id FROM wallets WHERE adminkey = ?", (request.headers["Grpc-Metadata-macaroon"],))
|
2019-12-13 22:45:03 +01:00
|
|
|
|
2019-12-14 19:49:32 +01:00
|
|
|
if not wallet:
|
2019-12-13 22:45:03 +01:00
|
|
|
return jsonify({"ERROR": "BAD AUTH"}), 200
|
|
|
|
|
2019-12-14 02:59:35 +01:00
|
|
|
# decode the invoice
|
|
|
|
invoice = bolt11.decode(data["payment_request"])
|
2019-12-14 10:19:41 +01:00
|
|
|
if invoice.amount_msat == 0:
|
2019-12-14 04:11:41 +01:00
|
|
|
return jsonify({"ERROR": "AMOUNTLESS INVOICES NOT SUPPORTED"}), 400
|
2019-12-13 22:45:03 +01:00
|
|
|
|
2019-12-14 02:59:35 +01:00
|
|
|
# insert the payment
|
2019-12-13 22:45:03 +01:00
|
|
|
db.execute(
|
2019-12-15 18:20:26 +01:00
|
|
|
"INSERT INTO apipayments (payhash, amount, fee, wallet, pending, memo) VALUES (?, ?, ?, ?, 1, ?)",
|
2019-12-13 22:45:03 +01:00
|
|
|
(
|
2019-12-14 02:59:35 +01:00
|
|
|
invoice.payment_hash,
|
|
|
|
-int(invoice.amount_msat),
|
|
|
|
-int(invoice.amount_msat * 0.01),
|
2019-12-14 19:49:32 +01:00
|
|
|
wallet["id"],
|
2019-12-14 02:59:35 +01:00
|
|
|
invoice.description,
|
2019-12-13 22:45:03 +01:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2019-12-14 02:59:35 +01:00
|
|
|
# check balance
|
2019-12-14 19:49:32 +01:00
|
|
|
balance = db.fetchone("SELECT balance/1000 FROM balances WHERE wallet = ?", (wallet["id"],))[0]
|
2019-12-14 02:59:35 +01:00
|
|
|
if balance < 0:
|
2019-12-16 19:24:06 +01:00
|
|
|
db.execute("DELETE FROM apipayments WHERE payhash = ? AND wallet = ?", (invoice.payment_hash, wallet["id"]))
|
2019-12-14 02:59:35 +01:00
|
|
|
return jsonify({"ERROR": "INSUFFICIENT BALANCE"}), 403
|
|
|
|
|
2019-12-16 19:24:06 +01:00
|
|
|
# check if the invoice is an internal one
|
|
|
|
if db.fetchone("SELECT count(*) FROM apipayments WHERE payhash = ?", (invoice.payment_hash,))[0] == 2:
|
|
|
|
# internal. mark both sides as fulfilled.
|
|
|
|
db.execute("UPDATE apipayments SET pending = 0, fee = 0 WHERE payhash = ?", (invoice.payment_hash,))
|
|
|
|
else:
|
|
|
|
# actually send the payment
|
|
|
|
r = WALLET.pay_invoice(data["payment_request"])
|
|
|
|
if not r.ok or r.json().get('error'):
|
|
|
|
return jsonify({"ERROR": "UNEXPECTED PAYMENT ERROR"}), 500
|
|
|
|
|
|
|
|
data = r.json()
|
|
|
|
if r.ok and "error" in data:
|
|
|
|
# payment didn't went through, delete it here
|
|
|
|
# (these guarantees specific to lntxbot)
|
|
|
|
db.execute("DELETE FROM apipayments WHERE payhash = ?", (invoice.payment_hash,))
|
|
|
|
return jsonify({"PAID": "FALSE"}), 200
|
|
|
|
|
|
|
|
# payment went through, not pending anymore, save actual fees
|
|
|
|
db.execute(
|
|
|
|
"UPDATE apipayments SET pending = 0, fee = ? WHERE payhash = ? AND wallet = ?",
|
|
|
|
(data["fee_msat"], invoice.payment_hash, wallet['id']),
|
|
|
|
)
|
2019-12-13 22:45:03 +01:00
|
|
|
|
|
|
|
return jsonify({"PAID": "TRUE"}), 200
|
2019-12-13 17:56:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
@app.route("/v1/invoice/<payhash>", methods=["GET"])
|
|
|
|
def api_checkinvoice(payhash):
|
2019-12-13 23:22:49 +01:00
|
|
|
if request.headers["Content-Type"] != "application/json":
|
|
|
|
return jsonify({"ERROR": "MUST BE JSON"}), 200
|
2019-12-13 17:56:19 +01:00
|
|
|
|
2019-12-13 23:22:49 +01:00
|
|
|
with Database() as db:
|
2019-12-14 19:49:32 +01:00
|
|
|
payment = db.fetchone(
|
2019-12-14 02:59:35 +01:00
|
|
|
"""
|
|
|
|
SELECT pending FROM apipayments
|
2019-12-14 19:49:32 +01:00
|
|
|
INNER JOIN wallets AS w ON apipayments.wallet = w.id
|
2019-12-14 02:59:35 +01:00
|
|
|
WHERE payhash = ?
|
2019-12-14 04:11:41 +01:00
|
|
|
AND (w.adminkey = ? OR w.inkey = ?)
|
2019-12-14 02:59:35 +01:00
|
|
|
""",
|
|
|
|
(payhash, request.headers["Grpc-Metadata-macaroon"], request.headers["Grpc-Metadata-macaroon"]),
|
|
|
|
)
|
2019-12-13 17:56:19 +01:00
|
|
|
|
2019-12-14 19:49:32 +01:00
|
|
|
if not payment:
|
2019-12-14 02:59:35 +01:00
|
|
|
return jsonify({"ERROR": "NO INVOICE"}), 404
|
2019-12-13 17:56:19 +01:00
|
|
|
|
2019-12-14 19:49:32 +01:00
|
|
|
if not payment["pending"]: # pending
|
2019-12-13 23:22:49 +01:00
|
|
|
return jsonify({"PAID": "TRUE"}), 200
|
2019-12-13 17:56:19 +01:00
|
|
|
|
2019-12-14 10:13:27 +01:00
|
|
|
r = WALLET.get_invoice_status(payhash)
|
2019-12-15 18:20:26 +01:00
|
|
|
if not r.ok or r.json().get('error'):
|
2019-12-13 23:22:49 +01:00
|
|
|
return jsonify({"PAID": "FALSE"}), 400
|
2019-12-13 17:56:19 +01:00
|
|
|
|
2019-12-14 02:59:35 +01:00
|
|
|
data = r.json()
|
|
|
|
if "preimage" not in data or not data["preimage"]:
|
|
|
|
return jsonify({"PAID": "FALSE"}), 400
|
2019-12-13 17:56:19 +01:00
|
|
|
|
2019-12-15 18:20:26 +01:00
|
|
|
db.execute("UPDATE apipayments SET pending = 0 WHERE payhash = ?", (payhash,))
|
2019-12-14 02:59:35 +01:00
|
|
|
return jsonify({"PAID": "TRUE"}), 200
|
2019-12-15 21:24:37 +01:00
|
|
|
|
|
|
|
@app.route("/v1/checkpending", methods=["POST"])
|
|
|
|
def api_checkpending():
|
|
|
|
with Database() as db:
|
|
|
|
for pendingtx in db.fetchall("""
|
|
|
|
SELECT
|
|
|
|
payhash,
|
|
|
|
CASE
|
|
|
|
WHEN amount < 0 THEN 'send'
|
|
|
|
ELSE 'recv'
|
|
|
|
END AS kind
|
|
|
|
FROM apipayments
|
|
|
|
INNER JOIN wallets ON apipayments.wallet = wallets.id
|
|
|
|
WHERE time > strftime('%s', 'now') - 86400
|
|
|
|
AND pending = 1
|
|
|
|
AND (adminkey = ? OR inkey = ?)
|
|
|
|
""", (request.headers["Grpc-Metadata-macaroon"], request.headers["Grpc-Metadata-macaroon"])):
|
|
|
|
payhash = pendingtx['payhash']
|
|
|
|
kind = pendingtx['kind']
|
|
|
|
|
|
|
|
if kind == 'send':
|
|
|
|
status = WALLET.get_final_payment_status(payhash)
|
|
|
|
if status == 'complete':
|
|
|
|
db.execute("UPDATE apipayments SET pending = 0 WHERE payhash = ?", (payhash,))
|
|
|
|
elif status == 'failed':
|
|
|
|
db.execute("DELETE FROM apipayments WHERE payhash = ?", (payhash,))
|
|
|
|
|
|
|
|
elif kind == 'recv':
|
|
|
|
if WALLET.is_invoice_paid(payhash):
|
|
|
|
db.execute("UPDATE apipayments SET pending = 0 WHERE payhash = ?", (payhash,))
|
|
|
|
return ''
|