From 7609f7ea5b0cc81bf6e36f2b015bd8a8545c21b5 Mon Sep 17 00:00:00 2001 From: Vlad Stan Date: Fri, 7 Oct 2022 14:51:06 +0300 Subject: [PATCH] fix: don't even try to refactor --- lnbits/extensions/cashu/crud.py | 5 +++++ lnbits/extensions/cashu/mint.py | 4 ++-- lnbits/extensions/cashu/mint_helper.py | 20 ++++++++++---------- lnbits/extensions/cashu/views_api.py | 3 +++ 4 files changed, 20 insertions(+), 12 deletions(-) diff --git a/lnbits/extensions/cashu/crud.py b/lnbits/extensions/cashu/crud.py index 39af1f8c..698a8089 100644 --- a/lnbits/extensions/cashu/crud.py +++ b/lnbits/extensions/cashu/crud.py @@ -93,6 +93,11 @@ async def delete_cashu(cashu_id) -> None: ########################################## +async def store_promises(amounts: List[int], B_s: List[str], C_s: List[str], cashu_id: str): + for amount, B_, C_ in zip(amounts, B_s, C_s): + await store_promise(amount, B_, C_, cashu_id) + + async def store_promise(amount: int, B_: str, C_: str, cashu_id: str): promise_id = urlsafe_short_hash() diff --git a/lnbits/extensions/cashu/mint.py b/lnbits/extensions/cashu/mint.py index b1bdd042..1be07b91 100644 --- a/lnbits/extensions/cashu/mint.py +++ b/lnbits/extensions/cashu/mint.py @@ -3,7 +3,7 @@ from typing import List from .core.b_dhke import step2_bob from .core.base import BlindedSignature from .core.secp import PublicKey -from .mint_helper import derive_key, derive_keys, derive_pubkeys +from .mint_helper import derive_keys, derive_pubkeys # todo: extract const MAX_ORDER = 64 @@ -36,6 +36,6 @@ async def generate_promises( async def generate_promise(master_prvkey: str, amount: int, B_: PublicKey): """Generates a promise for given amount and returns a pair (amount, C').""" - secret_key = derive_key(master_prvkey, amount) # Get the correct key + secret_key = derive_keys(master_prvkey)[amount] # Get the correct key C_ = step2_bob(B_, secret_key) return BlindedSignature(amount=amount, C_=C_.serialize().hex()) diff --git a/lnbits/extensions/cashu/mint_helper.py b/lnbits/extensions/cashu/mint_helper.py index 50227733..cfb3b7d7 100644 --- a/lnbits/extensions/cashu/mint_helper.py +++ b/lnbits/extensions/cashu/mint_helper.py @@ -9,18 +9,18 @@ MAX_ORDER = 64 def derive_keys(master_key: str): """Deterministic derivation of keys for 2^n values.""" - return {2**i: derive_key(master_key, i) for i in range(MAX_ORDER)} + return { + 2 + ** i: PrivateKey( + hashlib.sha256((str(master_key) + str(i)).encode("utf-8")) + .hexdigest() + .encode("utf-8")[:32], + raw=True, + ) + for i in range(MAX_ORDER) + } -def derive_key(master_key: str, i: int): - """Deterministic derivation of keys for a particular value.""" - return PrivateKey( - hashlib.sha256((str(master_key) + str(i)).encode("utf-8")) - .hexdigest() - .encode("utf-8")[:32], - raw=True, - ) - def derive_pubkeys(keys: List[PrivateKey]): return {amt: keys[amt].pubkey for amt in [2**i for i in range(MAX_ORDER)]} diff --git a/lnbits/extensions/cashu/views_api.py b/lnbits/extensions/cashu/views_api.py index 93902356..fb4cefb0 100644 --- a/lnbits/extensions/cashu/views_api.py +++ b/lnbits/extensions/cashu/views_api.py @@ -300,6 +300,9 @@ async def mint_coins( try: promises = await generate_promises(cashu.prvkey, amounts, B_s) + for amount, B_, p in zip(amounts, B_s, promises): + await store_promise(amount, B_.serialize().hex(), p.C_, cashu_id) + # store_promises(amounts, B_s, C_s, cashu_id) # await store_promise(amount, B_=B_.serialize().hex(), C_=C_.serialize().hex(), cashu_id) return promises except Exception as exc: