fix validation rules so description_hash works.

This commit is contained in:
fiatjaf 2020-06-12 22:46:40 -03:00
parent bc27293315
commit 24dc6e5415
6 changed files with 9 additions and 17 deletions

View file

@ -2,7 +2,6 @@
import bitstring import bitstring
import re import re
from binascii import hexlify
from bech32 import bech32_decode, CHARSET from bech32 import bech32_decode, CHARSET
@ -51,9 +50,9 @@ def decode(pr: str) -> Invoice:
if tag == "d": if tag == "d":
invoice.description = trim_to_bytes(tagdata).decode("utf-8") invoice.description = trim_to_bytes(tagdata).decode("utf-8")
elif tag == "h" and data_length == 52: elif tag == "h" and data_length == 52:
invoice.description = hexlify(trim_to_bytes(tagdata)).decode("ascii") invoice.description = trim_to_bytes(tagdata).hex()
elif tag == "p" and data_length == 52: elif tag == "p" and data_length == 52:
invoice.payment_hash = hexlify(trim_to_bytes(tagdata)).decode("ascii") invoice.payment_hash = trim_to_bytes(tagdata).hex()
return invoice return invoice

View file

@ -115,7 +115,7 @@ def get_wallet(wallet_id: str) -> Optional[Wallet]:
def get_wallet_for_key(key: str, key_type: str = "invoice") -> Optional[Wallet]: def get_wallet_for_key(key: str, key_type: str = "invoice") -> Optional[Wallet]:
with open_db() as db: with open_db() as db:
row = db.fetchone( row = db.fetchone(
f""" """
SELECT *, COALESCE((SELECT balance FROM balances WHERE wallet = wallets.id), 0) AS balance_msat SELECT *, COALESCE((SELECT balance FROM balances WHERE wallet = wallets.id), 0) AS balance_msat
FROM wallets FROM wallets
WHERE adminkey = ? OR inkey = ? WHERE adminkey = ? OR inkey = ?

View file

@ -28,8 +28,8 @@ def api_payments():
@api_validate_post_request( @api_validate_post_request(
schema={ schema={
"amount": {"type": "integer", "min": 1, "required": True}, "amount": {"type": "integer", "min": 1, "required": True},
"memo": {"type": "string", "empty": False, "required": False}, "memo": {"type": "string", "empty": False, "required": True, "excludes": "description_hash"},
"description_hash": {"type": "string", "empty": False, "required": False}, "description_hash": {"type": "string", "empty": False, "required": True, "excludes": "memo"},
} }
) )
def api_payments_create_invoice(): def api_payments_create_invoice():

View file

@ -36,7 +36,7 @@ def api_validate_post_request(*, schema: dict):
return jsonify({"message": "Content-Type must be `application/json`."}), HTTPStatus.BAD_REQUEST return jsonify({"message": "Content-Type must be `application/json`."}), HTTPStatus.BAD_REQUEST
v = Validator(schema) v = Validator(schema)
g.data = {key: (request.json[key] if key in request.json else None) for key in schema.keys()} g.data = {key: request.json[key] for key in schema.keys() if key in request.json}
if not v.validate(g.data): if not v.validate(g.data):
return jsonify({"message": f"Errors in request data: {v.errors}"}), HTTPStatus.BAD_REQUEST return jsonify({"message": f"Errors in request data: {v.errors}"}), HTTPStatus.BAD_REQUEST
@ -56,7 +56,7 @@ def check_user_exists(param: str = "usr"):
allowed_users = getenv("LNBITS_ALLOWED_USERS", "all") allowed_users = getenv("LNBITS_ALLOWED_USERS", "all")
if allowed_users != "all" and g.user.id not in allowed_users.split(","): if allowed_users != "all" and g.user.id not in allowed_users.split(","):
abort(HTTPStatus.UNAUTHORIZED, f"User not authorized.") abort(HTTPStatus.UNAUTHORIZED, "User not authorized.")
return view(**kwargs) return view(**kwargs)

View file

@ -1,6 +1,5 @@
from os import getenv from os import getenv
from requests import get, post from requests import get, post
from binascii import hexlify
from .base import InvoiceResponse, PaymentResponse, PaymentStatus, Wallet from .base import InvoiceResponse, PaymentResponse, PaymentStatus, Wallet
@ -17,12 +16,7 @@ class LNbitsWallet(Wallet):
r = post( r = post(
url=f"{self.endpoint}/api/v1/payments", url=f"{self.endpoint}/api/v1/payments",
headers=self.auth_invoice, headers=self.auth_invoice,
json={ json={"out": False, "amount": amount, "memo": memo, "description_hash": description_hash.hex(),},
"out": False,
"amount": amount,
"memo": memo,
"description_hash": hexlify(description_hash).decode("ascii"),
},
) )
ok, checking_id, payment_request, error_message = r.ok, None, None, None ok, checking_id, payment_request, error_message = r.ok, None, None, None

View file

@ -1,6 +1,5 @@
from os import getenv from os import getenv
from requests import post from requests import post
from binascii import hexlify
from .base import InvoiceResponse, PaymentResponse, PaymentStatus, Wallet from .base import InvoiceResponse, PaymentResponse, PaymentStatus, Wallet
@ -18,7 +17,7 @@ class LntxbotWallet(Wallet):
r = post( r = post(
url=f"{self.endpoint}/addinvoice", url=f"{self.endpoint}/addinvoice",
headers=self.auth_invoice, headers=self.auth_invoice,
json={"amt": str(amount), "memo": memo, "description_hash": hexlify(description_hash).decode("ascii")}, json={"amt": str(amount), "memo": memo, "description_hash": description_hash.hex()},
) )
ok, checking_id, payment_request, error_message = r.ok, None, None, None ok, checking_id, payment_request, error_message = r.ok, None, None, None