From cef4244bfc8ab437d4ffbc06cc33e428c6dc0bf1 Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Thu, 15 Dec 2022 00:33:24 +0100 Subject: [PATCH] update to cashu 0.6.0 --- lnbits/extensions/cashu/views_api.py | 63 +++++++++++++++------------- poetry.lock | 8 ++-- pyproject.toml | 2 +- requirements.txt | 2 +- 4 files changed, 39 insertions(+), 36 deletions(-) diff --git a/lnbits/extensions/cashu/views_api.py b/lnbits/extensions/cashu/views_api.py index 38cb0bba..2d10f3f0 100644 --- a/lnbits/extensions/cashu/views_api.py +++ b/lnbits/extensions/cashu/views_api.py @@ -221,7 +221,7 @@ async def mint_coins( status: PaymentStatus = await check_transaction_status(cashu.wallet, payment_hash) - if status.paid != True: + if LIGHTNING and status.paid != True: raise HTTPException( status_code=HTTPStatus.PAYMENT_REQUIRED, detail="Invoice not paid." ) @@ -265,47 +265,50 @@ async def melt_coins( detail="Error: Tokens are from another mint.", ) - assert all([ledger._verify_proof(p) for p in proofs]), HTTPException( - status_code=HTTPStatus.BAD_REQUEST, - detail="Could not verify proofs.", - ) - - total_provided = sum([p["amount"] for p in proofs]) - invoice_obj = bolt11.decode(invoice) - amount = math.ceil(invoice_obj.amount_msat / 1000) - - internal_checking_id = await check_internal(invoice_obj.payment_hash) - - if not internal_checking_id: - fees_msat = fee_reserve(invoice_obj.amount_msat) - else: - fees_msat = 0 - assert total_provided >= amount + math.ceil(fees_msat / 1000), Exception( - f"Provided proofs ({total_provided} sats) not enough for Lightning payment ({amount + fees_msat} sats)." - ) - logger.debug(f"Cashu: Initiating payment of {total_provided} sats") - await pay_invoice( - wallet_id=cashu.wallet, - payment_request=invoice, - description=f"Pay cashu invoice", - extra={"tag": "cashu", "cashu_name": cashu.name}, - ) + # set proofs as pending + await ledger._set_proofs_pending(proofs) try: + ledger._verify_proofs(proofs) + + total_provided = sum([p["amount"] for p in proofs]) + invoice_obj = bolt11.decode(invoice) + amount = math.ceil(invoice_obj.amount_msat / 1000) + + internal_checking_id = await check_internal(invoice_obj.payment_hash) + + if not internal_checking_id: + fees_msat = fee_reserve(invoice_obj.amount_msat) + else: + fees_msat = 0 + assert total_provided >= amount + math.ceil(fees_msat / 1000), Exception( + f"Provided proofs ({total_provided} sats) not enough for Lightning payment ({amount + fees_msat} sats)." + ) + logger.debug(f"Cashu: Initiating payment of {total_provided} sats") + await pay_invoice( + wallet_id=cashu.wallet, + payment_request=invoice, + description=f"Pay cashu invoice", + extra={"tag": "cashu", "cashu_name": cashu.name}, + ) + logger.debug( f"Cashu: Wallet {cashu.wallet} checking PaymentStatus of {invoice_obj.payment_hash}" ) status: PaymentStatus = await check_transaction_status( cashu.wallet, invoice_obj.payment_hash ) - logger.debug(f"Cashu: Got status.paid: {status.paid}") if status.paid == True: logger.debug("Cashu: Payment successful, invalidating proofs") await ledger._invalidate_proofs(proofs) except Exception as e: - logger.error(e) - logger.error("Cashu: Error in payment status check, invalidating proofs") - await ledger._invalidate_proofs(proofs) + raise HTTPException( + status_code=HTTPStatus.BAD_REQUEST, + detail=f"Cashu: {str(e)}", + ) + finally: + # delete proofs from pending list + await ledger._unset_proofs_pending(proofs) return GetMeltResponse(paid=status.paid, preimage=status.preimage) diff --git a/poetry.lock b/poetry.lock index 5c546ff1..8fe8329e 100644 --- a/poetry.lock +++ b/poetry.lock @@ -123,7 +123,7 @@ uvloop = ["uvloop (>=0.15.2)"] [[package]] name = "cashu" -version = "0.5.5" +version = "0.6.0" description = "Ecash wallet and mint with Bitcoin Lightning support" category = "main" optional = false @@ -1144,7 +1144,7 @@ testing = ["flake8 (<5)", "func-timeout", "jaraco.functools", "jaraco.itertools" [metadata] lock-version = "1.1" python-versions = "^3.10 | ^3.9 | ^3.8 | ^3.7" -content-hash = "53a18d7695f02e9ad24dc7d0863b5ae815c18f2f390ef20d7166a54b202642ff" +content-hash = "7f75ca0b067a11f19520dc2121f0789e16738b573a8da84ba3838ed8a466a6e1" [metadata.files] aiofiles = [ @@ -1208,8 +1208,8 @@ black = [ {file = "black-22.10.0.tar.gz", hash = "sha256:f513588da599943e0cde4e32cc9879e825d58720d6557062d1098c5ad80080e1"}, ] cashu = [ - {file = "cashu-0.5.5-py3-none-any.whl", hash = "sha256:c1d707479b852e503acca5ed53aa341b1880cd6bd70369488ec002d647970c9b"}, - {file = "cashu-0.5.5.tar.gz", hash = "sha256:cc0349d3b6d9a2428cb575fee6280b20074ca9c20a1e2e9c68729a73c01f5f9d"}, + {file = "cashu-0.6.0-py3-none-any.whl", hash = "sha256:54096af145643aab45943b235f95a3357b0ec697835c1411e66523049ffb81f6"}, + {file = "cashu-0.6.0.tar.gz", hash = "sha256:503a90c4ca8d25d0b2c3f78a11b163c32902a726ea5b58e5337dc00eca8e96ad"}, ] Cerberus = [ {file = "Cerberus-1.3.4.tar.gz", hash = "sha256:d1b21b3954b2498d9a79edf16b3170a3ac1021df88d197dc2ce5928ba519237c"}, diff --git a/pyproject.toml b/pyproject.toml index 350bc292..3e45400d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -64,7 +64,7 @@ protobuf = "^4.21.6" Cerberus = "^1.3.4" async-timeout = "^4.0.2" pyln-client = "0.11.1" -cashu = "0.5.5" +cashu = "^0.6.0" [tool.poetry.dev-dependencies] diff --git a/requirements.txt b/requirements.txt index d5471455..da2044d7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -7,7 +7,7 @@ attrs==22.1.0 ; python_version >= "3.7" and python_version < "4.0" base58==2.1.1 ; python_version >= "3.7" and python_version < "4.0" bech32==1.2.0 ; python_version >= "3.7" and python_version < "4.0" bitstring==3.1.9 ; python_version >= "3.7" and python_version < "4.0" -cashu==0.5.5 ; python_version >= "3.7" and python_version < "4.0" +cashu==0.6.0 ; python_version >= "3.7" and python_version < "4.0" cerberus==1.3.4 ; python_version >= "3.7" and python_version < "4.0" certifi==2022.9.24 ; python_version >= "3.7" and python_version < "4.0" cffi==1.15.1 ; python_version >= "3.7" and python_version < "4.0"