diff --git a/cashu/core/base.py b/cashu/core/base.py index d8b3e9f..afc7335 100644 --- a/cashu/core/base.py +++ b/cashu/core/base.py @@ -6,13 +6,9 @@ from typing import Any, Dict, List, Optional, TypedDict, Union from loguru import logger from pydantic import BaseModel -from ..core.crypto import ( - derive_keys, - derive_keys_backwards_compatible_0_11_insecure, - derive_keyset_id, - derive_pubkeys, -) +from ..core.crypto import derive_keys, derive_keyset_id, derive_pubkeys from ..core.secp import PrivateKey, PublicKey +from .legacy import derive_keys_backwards_compatible_insecure_pre_0_12 # ------- PROOFS ------- @@ -299,7 +295,7 @@ class MintKeyset: ): backwards_compatibility_pre_0_12 = True # WARNING: Broken key derivation for backwards compatibility with < 0.12 - self.private_keys = derive_keys_backwards_compatible_0_11_insecure( + self.private_keys = derive_keys_backwards_compatible_insecure_pre_0_12( seed, self.derivation_path ) else: diff --git a/cashu/core/crypto.py b/cashu/core/crypto.py index 44cd8f5..4f4384c 100644 --- a/cashu/core/crypto.py +++ b/cashu/core/crypto.py @@ -31,24 +31,6 @@ def derive_keys(master_key: str, derivation_path: str = ""): } -def derive_keys_backwards_compatible_0_11_insecure( - master_key: str, derivation_path: str = "" -): - """ - WARNING: Broken key derivation for backwards compatibility with 0.11. - """ - return { - 2 - ** i: PrivateKey( - hashlib.sha256((master_key + derivation_path + str(i)).encode("utf-8")) - .hexdigest() - .encode("utf-8")[:32], - raw=True, - ) - for i in range(settings.max_order) - } - - def derive_pubkey(master_key: str): return PrivateKey( hashlib.sha256((master_key).encode("utf-8")).digest()[:32], diff --git a/cashu/core/legacy.py b/cashu/core/legacy.py index 7434bdf..75186cc 100644 --- a/cashu/core/legacy.py +++ b/cashu/core/legacy.py @@ -1,6 +1,8 @@ import hashlib -from secp256k1 import PublicKey +from secp256k1 import PrivateKey, PublicKey + +from ..core.settings import settings def hash_to_point_pre_0_3_3(secret_msg): @@ -13,7 +15,7 @@ def hash_to_point_pre_0_3_3(secret_msg): point = None msg = secret_msg while point is None: - _hash = hashlib.sha256(msg).hexdigest().encode("utf-8") + _hash = hashlib.sha256(msg).hexdigest().encode("utf-8") # type: ignore try: # We construct compressed pub which has x coordinate encoded with even y _hash = list(_hash[:33]) # take the 33 bytes and get a list of bytes @@ -28,4 +30,22 @@ def hash_to_point_pre_0_3_3(secret_msg): def verify_pre_0_3_3(a, C, secret_msg): Y = hash_to_point_pre_0_3_3(secret_msg.encode("utf-8")) - return C == Y.mult(a) + return C == Y.mult(a) # type: ignore + + +def derive_keys_backwards_compatible_insecure_pre_0_12( + master_key: str, derivation_path: str = "" +): + """ + WARNING: Broken key derivation for backwards compatibility with 0.11. + """ + return { + 2 + ** i: PrivateKey( + hashlib.sha256((master_key + derivation_path + str(i)).encode("utf-8")) + .hexdigest() + .encode("utf-8")[:32], + raw=True, + ) + for i in range(settings.max_order) + } diff --git a/cashu/core/secp.py b/cashu/core/secp.py index 3341643..d0bb0e1 100644 --- a/cashu/core/secp.py +++ b/cashu/core/secp.py @@ -21,7 +21,7 @@ class PublicKeyExt(PublicKey): def __sub__(self, pubkey2): if isinstance(pubkey2, PublicKey): - return self + (-pubkey2) + return self + (-pubkey2) # type: ignore else: raise TypeError("Can't add pubkey and %s" % pubkey2.__class__) @@ -34,19 +34,20 @@ class PublicKeyExt(PublicKey): def __eq__(self, pubkey2): if isinstance(pubkey2, PublicKey): seq1 = self.to_data() - seq2 = pubkey2.to_data() + seq2 = pubkey2.to_data() # type: ignore return seq1 == seq2 else: raise TypeError("Can't compare pubkey and %s" % pubkey2.__class__) def to_data(self): + assert self.public_key return [self.public_key.data[i] for i in range(64)] # Horrible monkeypatching -PublicKey.__add__ = PublicKeyExt.__add__ -PublicKey.__neg__ = PublicKeyExt.__neg__ -PublicKey.__sub__ = PublicKeyExt.__sub__ -PublicKey.mult = PublicKeyExt.mult -PublicKey.__eq__ = PublicKeyExt.__eq__ -PublicKey.to_data = PublicKeyExt.to_data +PublicKey.__add__ = PublicKeyExt.__add__ # type: ignore +PublicKey.__neg__ = PublicKeyExt.__neg__ # type: ignore +PublicKey.__sub__ = PublicKeyExt.__sub__ # type: ignore +PublicKey.mult = PublicKeyExt.mult # type: ignore +PublicKey.__eq__ = PublicKeyExt.__eq__ # type: ignore +PublicKey.to_data = PublicKeyExt.to_data # type: ignore diff --git a/tests/test_mint.py b/tests/test_mint.py index 534d2b1..dcc5487 100644 --- a/tests/test_mint.py +++ b/tests/test_mint.py @@ -12,6 +12,7 @@ import os from cashu.core.db import Database from cashu.core.settings import settings +from cashu.lightning.fake import FakeWallet from cashu.mint import migrations from cashu.mint.ledger import Ledger @@ -46,7 +47,7 @@ async def ledger(): db=Database("test", "data/mint"), seed="TEST_PRIVATE_KEY", derivation_path="0/0/0/0", - lightning=None, + lightning=FakeWallet(), ) await start_mint_init(ledger) yield ledger