From e696e19479025d729f56750e6e654a6892fc8c48 Mon Sep 17 00:00:00 2001 From: calle <93376500+callebtc@users.noreply.github.com> Date: Sat, 4 Mar 2023 22:02:24 +0100 Subject: [PATCH] wallet: invalidate proofs without checking with mint (#122) --- cashu/wallet/wallet.py | 30 ++++++++++++++++++++---------- tests/test_wallet.py | 16 ++++++++++++++++ 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/cashu/wallet/wallet.py b/cashu/wallet/wallet.py index bc71925..9087eaf 100644 --- a/cashu/wallet/wallet.py +++ b/cashu/wallet/wallet.py @@ -664,17 +664,27 @@ class Wallet(LedgerAPI): proof, reserved=reserved, send_id=uuid_str, db=self.db ) - async def invalidate(self, proofs): - """Invalidates all spendable tokens supplied in proofs.""" - spendables = await self.check_spendable(proofs) - invalidated_proofs = [] - for i, spendable in enumerate(spendables.spendable): - if not spendable: - invalidated_proofs.append(proofs[i]) - await invalidate_proof(proofs[i], db=self.db) - invalidate_secrets = [p["secret"] for p in invalidated_proofs] + async def invalidate(self, proofs: List[Proof], check_spendable=True): + """Invalidates all spendable tokens supplied in proofs. + + Args: + proofs (List[Proof]): Which proofs to delete + check_spendable (bool, optional): Asks the mint to check whether proofs are already spent before deleting them. Defaults to True. + """ + invalidated_proofs: List[Proof] = [] + if check_spendable: + spendables = await self.check_spendable(proofs) + for i, spendable in enumerate(spendables.spendable): + if not spendable: + invalidated_proofs.append(proofs[i]) + else: + invalidated_proofs = proofs + + for p in invalidated_proofs: + await invalidate_proof(p, db=self.db) + invalidate_secrets = [p.secret for p in invalidated_proofs] self.proofs = list( - filter(lambda p: p["secret"] not in invalidate_secrets, self.proofs) + filter(lambda p: p.secret not in invalidate_secrets, self.proofs) ) # ---------- TRANSACTION HELPERS ---------- diff --git a/tests/test_wallet.py b/tests/test_wallet.py index 010d0ea..6fb60d6 100644 --- a/tests/test_wallet.py +++ b/tests/test_wallet.py @@ -189,6 +189,22 @@ async def test_send_and_redeem(wallet1: Wallet, wallet2: Wallet): assert wallet1.available_balance == 32 +@pytest.mark.asyncio +async def test_invalidate_unspent_proofs(wallet1: Wallet): + """Try to invalidate proofs that have not been spent yet. Should not work!""" + await wallet1.mint(64) + await wallet1.invalidate(wallet1.proofs) + assert wallet1.balance == 64 + + +@pytest.mark.asyncio +async def test_invalidate_unspent_proofs_without_checking(wallet1: Wallet): + """Try to invalidate proofs that have not been spent yet but force no check.""" + await wallet1.mint(64) + await wallet1.invalidate(wallet1.proofs, check_spendable=False) + assert wallet1.balance == 0 + + @pytest.mark.asyncio async def test_split_invalid_amount(wallet1: Wallet): await wallet1.mint(64)