From b6c4d676f6fba0ff1d8371c59e1501bab1fbb414 Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Sat, 1 Oct 2022 02:52:48 +0200 Subject: [PATCH] refactor deterministic key generation --- cashu/mint/ledger.py | 2 +- cashu/wallet/cli.py | 11 ++++++----- cashu/wallet/wallet.py | 13 +++++++++---- tests/test_wallet.py | 2 +- 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/cashu/mint/ledger.py b/cashu/mint/ledger.py index 1c4874f..5896aa3 100644 --- a/cashu/mint/ledger.py +++ b/cashu/mint/ledger.py @@ -239,7 +239,7 @@ class Ledger: raise Exception("split amount is higher than the total sum.") # verify that only unique proofs and outputs were used if not self._verify_no_duplicates(proofs, outputs): - raise Exception("duplicate proofs or promises.") + raise Exception("empty or duplicate proofs or promises.") # verify that outputs have the correct amount if not self._verify_outputs(total, amount, outputs): raise Exception("split of promises is not as expected.") diff --git a/cashu/wallet/cli.py b/cashu/wallet/cli.py index c9d5b77..a094aba 100755 --- a/cashu/wallet/cli.py +++ b/cashu/wallet/cli.py @@ -172,9 +172,11 @@ async def pending(ctx): wallet.load_mint() reserved_proofs = await get_reserved_proofs(wallet.db) if len(reserved_proofs): + print(f"--------------------------\n") sorted_proofs = sorted(reserved_proofs, key=itemgetter("send_id")) - grouped_proofs = groupby(sorted_proofs, key=itemgetter("send_id")) - for i, (key, value) in enumerate(grouped_proofs): + for i, (key, value) in enumerate( + groupby(sorted_proofs, key=itemgetter("send_id")) + ): grouped_proofs = list(value) token = await wallet.serialize_proofs(grouped_proofs) token_hidden_secret = await wallet.serialize_proofs( @@ -184,11 +186,10 @@ async def pending(ctx): int(grouped_proofs[0].time_reserved) ).strftime("%Y-%m-%d %H:%M:%S") print( - f"Amount: {sum([p['amount'] for p in grouped_proofs])} sat Sent: {reserved_date} ID: {key} #{i+1}/{len(grouped_proofs)}\n" + f"#{i} Amount: {sum([p['amount'] for p in grouped_proofs])} sat Time: {reserved_date} ID: {key}\n" ) print(f"With secret: {token}\n\nSecretless: {token_hidden_secret}\n") - if i < len(grouped_proofs) - 1: - print(f"--------------------------\n") + print(f"--------------------------\n") wallet.status() diff --git a/cashu/wallet/wallet.py b/cashu/wallet/wallet.py index e946855..21eb275 100644 --- a/cashu/wallet/wallet.py +++ b/cashu/wallet/wallet.py @@ -103,6 +103,11 @@ class LedgerAPI: if await secret_used(s, db=self.db): raise Exception(f"secret already used: {s}") + @staticmethod + def generate_deterministic_secrets(secret, n): + """`secret` is the base string that will be tweaked n times""" + return [f"{secret}_{i}" for i in range(n)] + async def mint(self, amounts, payment_hash=None): """Mints new coins and returns a proof of promise.""" secrets = [self._generate_secret() for s in range(len(amounts))] @@ -146,8 +151,9 @@ class LedgerAPI: secrets = [self._generate_secret() for _ in range(len(amounts))] else: logger.debug(f"Creating proofs with custom secret: {snd_secret}") - # TODO: serialize them here - snd_secrets = [f"{snd_secret}_{i}" for i in range(len(snd_outputs))] + snd_secrets = self.generate_deterministic_secrets( + snd_secret, len(snd_outputs) + ) assert len(snd_secrets) == len( snd_outputs ), "number of snd_secrets does not match number of ouptus." @@ -241,8 +247,7 @@ class Wallet(LedgerAPI): async def redeem(self, proofs: List[Proof], snd_secret: str = None): if snd_secret: logger.debug(f"Redeption secret: {snd_secret}") - # TODO: serialize them here - snd_secrets = [f"{snd_secret}_{i}" for i in range(len(proofs))] + snd_secrets = self.generate_deterministic_secrets(snd_secret, len(proofs)) assert len(proofs) == len(snd_secrets) # overload proofs with custom secrets for redemption for p, s in zip(proofs, snd_secrets): diff --git a/tests/test_wallet.py b/tests/test_wallet.py index 4f147ef..e90fdfa 100644 --- a/tests/test_wallet.py +++ b/tests/test_wallet.py @@ -135,7 +135,7 @@ async def run_test(): p.secret = "" await assert_err( wallet2.redeem(w1_snd_proofs_manipulated), - "Error: duplicate proofs or promises.", + "Error: empty or duplicate proofs or promises.", ) # redeem with wrong secret