wallet: invalidate proofs without checking with mint (#122)

This commit is contained in:
calle
2023-03-04 22:02:24 +01:00
committed by GitHub
parent 23f2b58430
commit e696e19479
2 changed files with 36 additions and 10 deletions

View File

@@ -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 ----------

View File

@@ -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)