Fixed lnurlw bugs

This commit is contained in:
arcbtc
2020-08-19 08:49:52 +01:00
parent e01de0eae5
commit 6cf488ea4f
8 changed files with 32 additions and 39 deletions

View File

@@ -55,31 +55,26 @@ def create_withdraw_link(
usescsv, usescsv,
), ),
) )
return get_withdraw_link(link_id, uses) return get_withdraw_link(link_id, 0)
def get_withdraw_link(link_id: str, num=None) -> Optional[WithdrawLink]: def get_withdraw_link(link_id: str, num=0) -> Optional[WithdrawLink]:
with open_ext_db("withdraw") as db: with open_ext_db("withdraw") as db:
row = db.fetchone("SELECT * FROM withdraw_link WHERE id = ?", (link_id,)) row = db.fetchone("SELECT * FROM withdraw_link WHERE id = ?", (link_id,))
link = [] link = []
for item in row: for item in row:
link.append(item) link.append(item)
tohash = row["id"] + row["unique_hash"] + str(num) link.append(num)
link.append(shortuuid.uuid(name=tohash))
return WithdrawLink._make(link) return WithdrawLink._make(link)
def get_withdraw_link_by_hash(unique_hash: str, num=None) -> Optional[WithdrawLink]: def get_withdraw_link_by_hash(unique_hash: str, num=0) -> Optional[WithdrawLink]:
with open_ext_db("withdraw") as db: with open_ext_db("withdraw") as db:
row = db.fetchone("SELECT * FROM withdraw_link WHERE unique_hash = ?", (unique_hash,)) row = db.fetchone("SELECT * FROM withdraw_link WHERE unique_hash = ?", (unique_hash,))
link = [] link = []
for item in row: for item in row:
link.append(item) link.append(item)
if not num: link.append(num)
link.append("")
return WithdrawLink._make(link)
tohash = row["id"] + row["unique_hash"] + str(num)
link.append(shortuuid.uuid(name=tohash))
return WithdrawLink._make(link) return WithdrawLink._make(link)

View File

@@ -2,7 +2,7 @@ from flask import url_for
from lnurl import Lnurl, LnurlWithdrawResponse, encode as lnurl_encode from lnurl import Lnurl, LnurlWithdrawResponse, encode as lnurl_encode
from sqlite3 import Row from sqlite3 import Row
from typing import NamedTuple from typing import NamedTuple
import shortuuid # type: ignore
from lnbits.settings import FORCE_HTTPS from lnbits.settings import FORCE_HTTPS
@@ -20,13 +20,13 @@ class WithdrawLink(NamedTuple):
open_time: int open_time: int
used: int used: int
usescsv: str usescsv: str
multihash: str number: int
@classmethod @classmethod
def from_row(cls, row: Row) -> "WithdrawLink": def from_row(cls, row: Row) -> "WithdrawLink":
data = dict(row) data = dict(row)
data["is_unique"] = bool(data["is_unique"]) data["is_unique"] = bool(data["is_unique"])
data["multihash"] = "" data["number"] = 0
return cls(**data) return cls(**data)
@property @property
@@ -36,9 +36,11 @@ class WithdrawLink(NamedTuple):
@property @property
def lnurl(self) -> Lnurl: def lnurl(self) -> Lnurl:
scheme = "https" if FORCE_HTTPS else None scheme = "https" if FORCE_HTTPS else None
print(self.is_unique)
if self.is_unique: if self.is_unique:
url = url_for("withdraw.api_lnurl_multi_response", unique_hash=self.unique_hash, id_unique_hash=self.multihash, _external=True, _scheme=scheme) usescssv = self.usescsv.split(",")
tohash = self.id + self.unique_hash + usescssv[self.number]
multihash = shortuuid.uuid(name=tohash)
url = url_for("withdraw.api_lnurl_multi_response", unique_hash=self.unique_hash, id_unique_hash=multihash, _external=True, _scheme=scheme)
else: else:
url = url_for("withdraw.api_lnurl_response", unique_hash=self.unique_hash, _external=True, _scheme=scheme) url = url_for("withdraw.api_lnurl_response", unique_hash=self.unique_hash, _external=True, _scheme=scheme)
@@ -49,7 +51,6 @@ class WithdrawLink(NamedTuple):
scheme = "https" if FORCE_HTTPS else None scheme = "https" if FORCE_HTTPS else None
url = url_for("withdraw.api_lnurl_callback", unique_hash=self.unique_hash, _external=True, _scheme=scheme) url = url_for("withdraw.api_lnurl_callback", unique_hash=self.unique_hash, _external=True, _scheme=scheme)
print(url)
return LnurlWithdrawResponse( return LnurlWithdrawResponse(
callback=url, callback=url,
k1=self.k1, k1=self.k1,

View File

@@ -113,7 +113,10 @@ new Vue({
}, },
openQrCodeDialog: function (linkId) { openQrCodeDialog: function (linkId) {
var link = _.findWhere(this.withdrawLinks, {id: linkId}) var link = _.findWhere(this.withdrawLinks, {id: linkId})
this.qrCodeDialog.data = _.clone(link) this.qrCodeDialog.data = _.clone(link)
console.log(this.qrCodeDialog.data)
this.qrCodeDialog.data.url = window.location.hostname
this.qrCodeDialog.show = true this.qrCodeDialog.show = true
}, },
openUpdateDialog: function (linkId) { openUpdateDialog: function (linkId) {

View File

@@ -7,10 +7,10 @@
{% if link.is_spent %} {% if link.is_spent %}
<q-badge color="red" class="q-mb-md">Withdraw is spent.</q-badge> <q-badge color="red" class="q-mb-md">Withdraw is spent.</q-badge>
{% endif %} {% endif %}
<a href="lightning:{{ link.lnurl }}"> <a href="{{ link.lnurl }}">
<q-responsive :ratio="1" class="q-mx-md"> <q-responsive :ratio="1" class="q-mx-md">
<qrcode <qrcode
value="{{ link.lnurl }}" :value="this.theurl+'/?lightning={{link.lnurl }}'"
:options="{width: 800}" :options="{width: 800}"
class="rounded-borders" class="rounded-borders"
></qrcode> ></qrcode>
@@ -18,6 +18,7 @@
</a> </a>
</div> </div>
<div class="row q-mt-lg"> <div class="row q-mt-lg">
<q-btn outline color="grey" @click="copyText('{{ link.lnurl }}')" <q-btn outline color="grey" @click="copyText('{{ link.lnurl }}')"
>Copy LNURL</q-btn >Copy LNURL</q-btn
> >
@@ -55,7 +56,7 @@
mixins: [windowMixin], mixins: [windowMixin],
data: function () { data: function () {
return { return {
theurl: location.host, theurl: location.protocol + '//' + location.host
} }
}, },

View File

@@ -293,7 +293,7 @@
{% raw %} {% raw %}
<q-responsive :ratio="1" class="q-mx-xl q-mb-md"> <q-responsive :ratio="1" class="q-mx-xl q-mb-md">
<qrcode <qrcode
:value="qrCodeDialog.data.lnurl" :value="qrCodeDialog.data.url + '/?lightning=' + qrCodeDialog.data.lnurl"
:options="{width: 800}" :options="{width: 800}"
class="rounded-borders" class="rounded-borders"
></qrcode> ></qrcode>

View File

@@ -12,7 +12,7 @@
<td style="width: 105mm;"> <td style="width: 105mm;">
<center> <center>
<qrcode <qrcode
:value="theurl + '/lnurlwallet?lightning={{one.lnurl}}'" :value="theurl + '/?lightning={{one}}'"
:options="{width: 150}" :options="{width: 150}"
></qrcode> ></qrcode>
</center> </center>

View File

@@ -16,10 +16,8 @@ def index():
@withdraw_ext.route("/<link_id>") @withdraw_ext.route("/<link_id>")
def display(link_id): def display(link_id):
link = get_withdraw_link(link_id) or abort(HTTPStatus.NOT_FOUND, "Withdraw link does not exist.") link = get_withdraw_link(link_id, 0) or abort(HTTPStatus.NOT_FOUND, "Withdraw link does not exist.")
link = get_withdraw_link(link_id, len(link.usescsv.split(","))) or abort(HTTPStatus.NOT_FOUND, "Withdraw link does not exist.") return render_template("withdraw/display.html", link=link, unique=True)
return render_template("withdraw/display.html", link=link)
@withdraw_ext.route("/print/<link_id>") @withdraw_ext.route("/print/<link_id>")
@@ -28,9 +26,11 @@ def print_qr(link_id):
if link.uses == 0: if link.uses == 0:
return render_template("withdraw/print_qr.html", link=link, unique=False) return render_template("withdraw/print_qr.html", link=link, unique=False)
links = [] links = []
count = 0
for x in link.usescsv.split(","): for x in link.usescsv.split(","):
linkk = get_withdraw_link(link_id, x) or abort(HTTPStatus.NOT_FOUND, "Withdraw link does not exist.") linkk = get_withdraw_link(link_id, count) or abort(HTTPStatus.NOT_FOUND, "Withdraw link does not exist.")
links.append(linkk) links.append(str(linkk.lnurl))
count = count + 1
page_link = list(chunks(links, 2)) page_link = list(chunks(links, 2))
linked = list(chunks(page_link, 5)) linked = list(chunks(page_link, 5))
return render_template("withdraw/print_qr.html", link=linked, unique=True) return render_template("withdraw/print_qr.html", link=linked, unique=True)

View File

@@ -42,7 +42,7 @@ def api_links():
@withdraw_ext.route("/api/v1/links/<link_id>", methods=["GET"]) @withdraw_ext.route("/api/v1/links/<link_id>", methods=["GET"])
@api_check_wallet_key("invoice") @api_check_wallet_key("invoice")
def api_link_retrieve(link_id): def api_link_retrieve(link_id):
link = get_withdraw_link(link_id) link = get_withdraw_link(link_id, 0)
if not link: if not link:
return jsonify({"message": "Withdraw link does not exist."}), HTTPStatus.NOT_FOUND return jsonify({"message": "Withdraw link does not exist."}), HTTPStatus.NOT_FOUND
@@ -72,12 +72,9 @@ def api_link_create_or_update(link_id=None):
jsonify({"message": "`max_withdrawable` needs to be at least `min_withdrawable`."}), jsonify({"message": "`max_withdrawable` needs to be at least `min_withdrawable`."}),
HTTPStatus.BAD_REQUEST, HTTPStatus.BAD_REQUEST,
) )
if (g.data["max_withdrawable"] * g.data["uses"] * 1000) > g.wallet.balance_msat: if (g.data["max_withdrawable"] * g.data["uses"] * 1000) > g.wallet.balance_msat:
return jsonify({"message": "Insufficient balance."}), HTTPStatus.FORBIDDEN return jsonify({"message": "Insufficient balance."}), HTTPStatus.FORBIDDEN
usescsv = "" usescsv = ""
for i in range(g.data["uses"]): for i in range(g.data["uses"]):
if g.data["is_unique"]: if g.data["is_unique"]:
usescsv += "," + str(i + 1) usescsv += "," + str(i + 1)
@@ -86,18 +83,14 @@ def api_link_create_or_update(link_id=None):
usescsv = usescsv[1:] usescsv = usescsv[1:]
if link_id: if link_id:
link = get_withdraw_link(link_id) link = get_withdraw_link(link_id, 0)
if not link: if not link:
return jsonify({"message": "Withdraw link does not exist."}), HTTPStatus.NOT_FOUND return jsonify({"message": "Withdraw link does not exist."}), HTTPStatus.NOT_FOUND
if link.wallet != g.wallet.id: if link.wallet != g.wallet.id:
return jsonify({"message": "Not your withdraw link."}), HTTPStatus.FORBIDDEN return jsonify({"message": "Not your withdraw link."}), HTTPStatus.FORBIDDEN
link = update_withdraw_link(link_id, **g.data, usescsv=usescsv, used=0) link = update_withdraw_link(link_id, **g.data, usescsv=usescsv, used=0)
else: else:
link = create_withdraw_link(wallet_id=g.wallet.id, **g.data, usescsv=usescsv) link = create_withdraw_link(wallet_id=g.wallet.id, **g.data, usescsv=usescsv)
return jsonify({**link._asdict(), **{"lnurl": link.lnurl}}), HTTPStatus.OK if link_id else HTTPStatus.CREATED return jsonify({**link._asdict(), **{"lnurl": link.lnurl}}), HTTPStatus.OK if link_id else HTTPStatus.CREATED