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,
),
)
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:
row = db.fetchone("SELECT * FROM withdraw_link WHERE id = ?", (link_id,))
link = []
for item in row:
link.append(item)
tohash = row["id"] + row["unique_hash"] + str(num)
link.append(shortuuid.uuid(name=tohash))
link = []
for item in row:
link.append(item)
link.append(num)
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:
row = db.fetchone("SELECT * FROM withdraw_link WHERE unique_hash = ?", (unique_hash,))
link = []
for item in row:
link.append(item)
if not num:
link.append("")
return WithdrawLink._make(link)
tohash = row["id"] + row["unique_hash"] + str(num)
link.append(shortuuid.uuid(name=tohash))
link.append(num)
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 sqlite3 import Row
from typing import NamedTuple
import shortuuid # type: ignore
from lnbits.settings import FORCE_HTTPS
@ -20,13 +20,13 @@ class WithdrawLink(NamedTuple):
open_time: int
used: int
usescsv: str
multihash: str
number: int
@classmethod
def from_row(cls, row: Row) -> "WithdrawLink":
data = dict(row)
data["is_unique"] = bool(data["is_unique"])
data["multihash"] = ""
data["number"] = 0
return cls(**data)
@property
@ -36,9 +36,11 @@ class WithdrawLink(NamedTuple):
@property
def lnurl(self) -> Lnurl:
scheme = "https" if FORCE_HTTPS else None
print(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:
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
url = url_for("withdraw.api_lnurl_callback", unique_hash=self.unique_hash, _external=True, _scheme=scheme)
print(url)
return LnurlWithdrawResponse(
callback=url,
k1=self.k1,

View File

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

View File

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

View File

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

View File

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

View File

@ -16,10 +16,8 @@ def index():
@withdraw_ext.route("/<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, len(link.usescsv.split(","))) or abort(HTTPStatus.NOT_FOUND, "Withdraw link does not exist.")
return render_template("withdraw/display.html", link=link)
link = get_withdraw_link(link_id, 0) or abort(HTTPStatus.NOT_FOUND, "Withdraw link does not exist.")
return render_template("withdraw/display.html", link=link, unique=True)
@withdraw_ext.route("/print/<link_id>")
@ -28,9 +26,11 @@ def print_qr(link_id):
if link.uses == 0:
return render_template("withdraw/print_qr.html", link=link, unique=False)
links = []
count = 0
for x in link.usescsv.split(","):
linkk = get_withdraw_link(link_id, x) or abort(HTTPStatus.NOT_FOUND, "Withdraw link does not exist.")
links.append(linkk)
linkk = get_withdraw_link(link_id, count) or abort(HTTPStatus.NOT_FOUND, "Withdraw link does not exist.")
links.append(str(linkk.lnurl))
count = count + 1
page_link = list(chunks(links, 2))
linked = list(chunks(page_link, 5))
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"])
@api_check_wallet_key("invoice")
def api_link_retrieve(link_id):
link = get_withdraw_link(link_id)
link = get_withdraw_link(link_id, 0)
if not link:
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`."}),
HTTPStatus.BAD_REQUEST,
)
if (g.data["max_withdrawable"] * g.data["uses"] * 1000) > g.wallet.balance_msat:
return jsonify({"message": "Insufficient balance."}), HTTPStatus.FORBIDDEN
usescsv = ""
for i in range(g.data["uses"]):
if g.data["is_unique"]:
usescsv += "," + str(i + 1)
@ -86,18 +83,14 @@ def api_link_create_or_update(link_id=None):
usescsv = usescsv[1:]
if link_id:
link = get_withdraw_link(link_id)
link = get_withdraw_link(link_id, 0)
if not link:
return jsonify({"message": "Withdraw link does not exist."}), HTTPStatus.NOT_FOUND
if link.wallet != g.wallet.id:
return jsonify({"message": "Not your withdraw link."}), HTTPStatus.FORBIDDEN
link = update_withdraw_link(link_id, **g.data, usescsv=usescsv, used=0)
else:
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