From cfab668e544627a96fb0023c226318aaab9bbb12 Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Wed, 18 Oct 2023 15:18:07 +0200 Subject: [PATCH 1/2] Mint: Allow 0-valued amounts for blank outputs (#348) * allow 0 amount for blank outputs * fix precommit as well --- .pre-commit-config.yaml | 10 +++++----- cashu/mint/ledger.py | 4 ++-- cashu/mint/verification.py | 6 ++---- cashu/wallet/wallet.py | 2 +- 4 files changed, 10 insertions(+), 12 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 00795a80..0bd751a5 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -21,8 +21,8 @@ repos: hooks: - id: ruff args: [--fix, --exit-non-zero-on-fix] - - repo: https://github.com/pre-commit/mirrors-mypy - rev: v1.6.0 - hooks: - - id: mypy - args: [--ignore-missing] + # - repo: https://github.com/pre-commit/mirrors-mypy + # rev: v1.6.0 + # hooks: + # - id: mypy + # args: [--ignore-missing] diff --git a/cashu/mint/ledger.py b/cashu/mint/ledger.py index a00d8259..179baad9 100644 --- a/cashu/mint/ledger.py +++ b/cashu/mint/ledger.py @@ -457,8 +457,8 @@ async def melt( f" {total_provided}, needed: {invoice_amount + fees_sat}" ) - # verify spending inputs, outputs, and spending conditions - await self.verify_inputs_and_outputs(proofs, outputs) + # verify spending inputs and their spending conditions + await self.verify_inputs_and_outputs(proofs) if settings.lightning: logger.trace(f"paying lightning invoice {invoice}") diff --git a/cashu/mint/verification.py b/cashu/mint/verification.py index 434dca78..a50ba5ee 100644 --- a/cashu/mint/verification.py +++ b/cashu/mint/verification.py @@ -139,10 +139,8 @@ def _verify_no_duplicate_outputs(self, outputs: List[BlindedMessage]) -> bool: return True def _verify_amount(self, amount: int) -> int: - """Any amount used should be a positive integer not larger than 2^MAX_ORDER.""" - valid = ( - isinstance(amount, int) and amount > 0 and amount < 2**settings.max_order - ) + """Any amount used should be positive and not larger than 2^MAX_ORDER.""" + valid = amount > 0 and amount < 2**settings.max_order logger.trace(f"Verifying amount {amount} is valid: {valid}") if not valid: raise NotAllowedError("invalid amount: " + str(amount)) diff --git a/cashu/wallet/wallet.py b/cashu/wallet/wallet.py index f6c7b178..3c3f7ba8 100644 --- a/cashu/wallet/wallet.py +++ b/cashu/wallet/wallet.py @@ -797,7 +797,7 @@ async def pay_lightning( # amount of fees we overpaid. n_return_outputs = calculate_number_of_blank_outputs(fee_reserve_sat) secrets, rs, derivation_paths = await self.generate_n_secrets(n_return_outputs) - outputs, rs = self._construct_outputs(n_return_outputs * [1], secrets, rs) + outputs, rs = self._construct_outputs(n_return_outputs * [0], secrets, rs) status = await super().pay_lightning(proofs, invoice, outputs) From 8a4813aee674423e1adee555079bd5b5253601fd Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Wed, 18 Oct 2023 16:18:57 +0200 Subject: [PATCH 2/2] Zero amount invoices: throw error before attempting a payment (#349) * Zero amount invoices: throw error before attempting a payment * better naming of fee variables during melt --- cashu/mint/ledger.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/cashu/mint/ledger.py b/cashu/mint/ledger.py index 179baad9..8b1b8042 100644 --- a/cashu/mint/ledger.py +++ b/cashu/mint/ledger.py @@ -445,16 +445,19 @@ async def melt( # verify amounts total_provided = sum_proofs(proofs) invoice_obj = bolt11.decode(invoice) + assert invoice_obj.amount_msat, "invoice has no amount." invoice_amount = math.ceil(invoice_obj.amount_msat / 1000) if settings.mint_max_peg_out and invoice_amount > settings.mint_max_peg_out: raise NotAllowedError( f"Maximum melt amount is {settings.mint_max_peg_out} sat." ) - fees_sat = await self.get_melt_fees(invoice) + reserve_fees_sat = await self.get_melt_fees(invoice) # verify overspending attempt - assert total_provided >= invoice_amount + fees_sat, TransactionError( + assert ( + total_provided >= invoice_amount + reserve_fees_sat + ), TransactionError( "provided proofs not enough for Lightning payment. Provided:" - f" {total_provided}, needed: {invoice_amount + fees_sat}" + f" {total_provided}, needed: {invoice_amount + reserve_fees_sat}" ) # verify spending inputs and their spending conditions @@ -462,16 +465,17 @@ async def melt( if settings.lightning: logger.trace(f"paying lightning invoice {invoice}") - status, preimage, fee_msat = await self._pay_lightning_invoice( - invoice, fees_sat * 1000 + status, preimage, paid_fee_msat = await self._pay_lightning_invoice( + invoice, reserve_fees_sat * 1000 ) preimage = preimage or "" logger.trace("paid lightning invoice") else: - status, preimage, fee_msat = True, "preimage", 0 + status, preimage, paid_fee_msat = True, "preimage", 0 logger.debug( - f"Melt status: {status}: preimage: {preimage}, fee_msat: {fee_msat}" + f"Melt status: {status}: preimage: {preimage}, fee_msat:" + f" {paid_fee_msat}" ) if not status: @@ -482,11 +486,11 @@ async def melt( # prepare change to compensate wallet for overpaid fees return_promises: List[BlindedSignature] = [] - if outputs and fee_msat is not None: + if outputs and paid_fee_msat is not None: return_promises = await self._generate_change_promises( total_provided=total_provided, invoice_amount=invoice_amount, - ln_fee_msat=fee_msat, + ln_fee_msat=paid_fee_msat, outputs=outputs, )