From 80e7b9639d37afb0ddf4c93272f52378ec0eb9e0 Mon Sep 17 00:00:00 2001 From: Vlad Stan Date: Wed, 31 Jul 2024 14:36:42 +0300 Subject: [PATCH] feat: filter response fields for `/api/v1/payments/decode` (#2612) * feat: filter response fields * chore: `make format` * chore: comment * Update lnbits/helpers.py Co-authored-by: Pavol Rusnak * Update lnbits/helpers.py Co-authored-by: Pavol Rusnak * chore: code format --------- Co-authored-by: Pavol Rusnak --- lnbits/core/models.py | 1 + lnbits/core/views/payment_api.py | 5 +++-- lnbits/helpers.py | 7 +++++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lnbits/core/models.py b/lnbits/core/models.py index 59301cd65..0829709c9 100644 --- a/lnbits/core/models.py +++ b/lnbits/core/models.py @@ -356,6 +356,7 @@ class Callback(BaseModel): class DecodePayment(BaseModel): data: str + filter_fields: Optional[list[str]] = [] class CreateLnurl(BaseModel): diff --git a/lnbits/core/views/payment_api.py b/lnbits/core/views/payment_api.py index c12a5eb4c..6b932caa3 100644 --- a/lnbits/core/views/payment_api.py +++ b/lnbits/core/views/payment_api.py @@ -40,7 +40,7 @@ from lnbits.decorators import ( require_admin_key, require_invoice_key, ) -from lnbits.helpers import generate_filter_params_openapi +from lnbits.helpers import filter_dict_keys, generate_filter_params_openapi from lnbits.lnurl import decode as lnurl_decode from lnbits.settings import settings from lnbits.utils.exchange_rates import fiat_amount_as_satoshis @@ -433,7 +433,8 @@ async def api_payments_decode(data: DecodePayment) -> JSONResponse: return JSONResponse({"domain": url}) else: invoice = bolt11.decode(payment_str) - return JSONResponse(invoice.data) + filtered_data = filter_dict_keys(invoice.data, data.filter_fields) + return JSONResponse(filtered_data) except Exception as exc: return JSONResponse( {"message": f"Failed to decode: {exc!s}"}, diff --git a/lnbits/helpers.py b/lnbits/helpers.py index 1d4e74fe0..fef7f3746 100644 --- a/lnbits/helpers.py +++ b/lnbits/helpers.py @@ -223,3 +223,10 @@ def decrypt_internal_message(m: Optional[str] = None) -> Optional[str]: if not m: return None return AESCipher(key=settings.auth_secret_key).decrypt(m) + + +def filter_dict_keys(data: dict, filter_keys: Optional[list[str]]) -> dict: + if not filter_keys: + # return shallow clone of the dict even if there are no filters + return {**data} + return {key: data[key] for key in filter_keys if key in data}