mirror of
https://github.com/aljazceru/nutshell.git
synced 2025-12-23 11:44:19 +01:00
refactor deterministic key generation
This commit is contained in:
@@ -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.")
|
||||
|
||||
@@ -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,10 +186,9 @@ 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")
|
||||
wallet.status()
|
||||
|
||||
|
||||
@@ -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):
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user