From 7343d1e0a0c613649bbd5360c4c46c7a8a1666a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dni=20=E2=9A=A1?= Date: Thu, 24 Aug 2023 12:58:10 +0200 Subject: [PATCH] [REFACTOR] proper create_invoice responses for descriptions (#1863) * [REFACTOR] proper create_invoice responses for descriptions return a exception per error it is description hash or unhashed_description keep types * Update lnbits/core/views/api.py Co-authored-by: michael1011 * Update lnbits/core/views/api.py Co-authored-by: michael1011 --------- Co-authored-by: michael1011 --- lnbits/core/views/api.py | 45 ++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/lnbits/core/views/api.py b/lnbits/core/views/api.py index 241c8d085..1eddf470b 100644 --- a/lnbits/core/views/api.py +++ b/lnbits/core/views/api.py @@ -187,30 +187,29 @@ async def api_payments_paginated( async def api_payments_create_invoice(data: CreateInvoice, wallet: Wallet): - extra = data.extra or {} + data.extra = data.extra or {} + description_hash = b"" + unhashed_description = b"" + memo = data.memo or settings.lnbits_site_title if data.description_hash or data.unhashed_description: - try: - description_hash = ( - bytes.fromhex(data.description_hash) if data.description_hash else b"" - ) - unhashed_description = ( - bytes.fromhex(data.unhashed_description) - if data.unhashed_description - else b"" - ) - except ValueError: - raise HTTPException( - status_code=HTTPStatus.BAD_REQUEST, - detail=( - "'description_hash' and 'unhashed_description' " - "must be a valid hex strings" - ), - ) + if data.description_hash: + try: + description_hash = bytes.fromhex(data.description_hash) + except ValueError: + raise HTTPException( + status_code=HTTPStatus.BAD_REQUEST, + detail="'description_hash' must be a valid hex string", + ) + if data.unhashed_description: + try: + unhashed_description = bytes.fromhex(data.unhashed_description) + except ValueError: + raise HTTPException( + status_code=HTTPStatus.BAD_REQUEST, + detail="'unhashed_description' must be a valid hex string", + ) + # do not save memo if description_hash or unhashed_description is set memo = "" - else: - description_hash = b"" - unhashed_description = b"" - memo = data.memo or settings.lnbits_site_title if data.unit == "sat": amount = int(data.amount) @@ -218,7 +217,7 @@ async def api_payments_create_invoice(data: CreateInvoice, wallet: Wallet): assert data.unit is not None, "unit not set" price_in_sats = await fiat_amount_as_satoshis(data.amount, data.unit) amount = price_in_sats - extra.update({"fiat_amount": data.amount, "fiat_currency": data.unit}) + data.extra.update({"fiat_amount": data.amount, "fiat_currency": data.unit}) async with db.connect() as conn: try: