diff --git a/cashu/core/base.py b/cashu/core/base.py index 189335b..2c7a31d 100644 --- a/cashu/core/base.py +++ b/cashu/core/base.py @@ -238,6 +238,7 @@ class MintKeyset: valid_to: Union[str, None] = None first_seen: Union[str, None] = None active: bool = True + version: Union[str, None] = None def __init__( self, @@ -248,6 +249,7 @@ class MintKeyset: active=None, seed: Union[None, str] = None, derivation_path: str = None, + version: str = None, ): self.derivation_path = derivation_path self.id = id @@ -255,6 +257,7 @@ class MintKeyset: self.valid_to = valid_to self.first_seen = first_seen self.active = active + self.version = version # generate keys from seed if seed: self.generate_keys(seed) @@ -275,6 +278,7 @@ class MintKeyset: valid_to=row[3], first_seen=row[4], active=row[5], + version=row[6], ) def get_keybase(self): diff --git a/cashu/mint/crud.py b/cashu/mint/crud.py index 19d0f0c..fbb3bf3 100644 --- a/cashu/mint/crud.py +++ b/cashu/mint/crud.py @@ -114,7 +114,6 @@ async def update_lightning_invoice( async def store_keyset( keyset: MintKeyset, - mint_url: str = None, db: Database = None, conn: Optional[Connection] = None, ): @@ -122,8 +121,8 @@ async def store_keyset( await (conn or db).execute( """ INSERT INTO keysets - (id, derivation_path, valid_from, valid_to, first_seen, active) - VALUES (?, ?, ?, ?, ?, ?) + (id, derivation_path, valid_from, valid_to, first_seen, active, version) + VALUES (?, ?, ?, ?, ?, ?, ?) """, ( keyset.id, @@ -132,6 +131,7 @@ async def store_keyset( keyset.valid_to, keyset.first_seen, True, + keyset.version, ), ) diff --git a/cashu/mint/ledger.py b/cashu/mint/ledger.py index 2e2f0bb..8f6a885 100644 --- a/cashu/mint/ledger.py +++ b/cashu/mint/ledger.py @@ -24,7 +24,7 @@ from cashu.core.db import Database from cashu.core.helpers import fee_reserve, sum_proofs from cashu.core.script import verify_script from cashu.core.secp import PublicKey -from cashu.core.settings import LIGHTNING, MAX_ORDER +from cashu.core.settings import LIGHTNING, MAX_ORDER, VERSION from cashu.core.split import amount_split from cashu.lightning import WALLET from cashu.mint.crud import ( @@ -54,7 +54,7 @@ class Ledger: """Loads all past keysets and stores the active one if not already in db""" # generate current keyset from seed and current derivation path self.keyset = MintKeyset( - seed=self.master_key, derivation_path=self.derivation_path + seed=self.master_key, derivation_path=self.derivation_path, version=VERSION ) # check if current keyset is stored in db and store if not logger.debug(f"Loading keyset {self.keyset.id} from db.") @@ -117,9 +117,23 @@ class Ledger: C = PublicKey(bytes.fromhex(proof.C), raw=True) # backwards compatibility with old hash_to_curve - # old clients do not send a version - if not context.get("client-version"): - return legacy.verify_pre_0_3_3(secret_key, C, proof.secret) + logger.debug(f"Client version {context.get('client-version')}") + if self.keysets.keysets.get(proof.id): + logger.debug( + f"Token keyset: {self.keysets.keysets.get(proof.id)}, token version: {self.keysets.keysets[proof.id].version}" + ) + # if not context.get("client-version") or ( + # self.keysets.keysets.get(proof.id) + # and not self.keysets.keysets[proof.id].version + # ): + # return legacy.verify_pre_0_3_3(secret_key, C, proof.secret) + try: + ret = legacy.verify_pre_0_3_3(secret_key, C, proof.secret) + if ret: + return ret + except: + pass + return b_dhke.verify(secret_key, C, proof.secret) def _verify_script(self, idx: int, proof: Proof): diff --git a/cashu/mint/migrations.py b/cashu/mint/migrations.py index 15c8233..c58b3a0 100644 --- a/cashu/mint/migrations.py +++ b/cashu/mint/migrations.py @@ -118,3 +118,10 @@ async def m003_mint_keysets(db: Database): ); """ ) + + +async def m004_keysets_add_version(db: Database): + """ + Column that remembers with which version + """ + await db.execute("ALTER TABLE keysets ADD COLUMN version TEXT") diff --git a/requirements.txt b/requirements.txt index 8fc17f6..0065b77 100644 --- a/requirements.txt +++ b/requirements.txt @@ -12,7 +12,7 @@ environs==9.5.0 ; python_version >= "3.7" and python_version < "4.0" fastapi==0.83.0 ; python_version >= "3.7" and python_version < "4.0" h11==0.14.0 ; python_version >= "3.7" and python_version < "4.0" idna==3.4 ; python_version >= "3.7" and python_version < "4.0" -importlib-metadata==4.12.0 ; python_version >= "3.7" and python_version < "3.8" +importlib-metadata==5.0.0 ; python_version >= "3.7" and python_version < "3.8" iniconfig==1.1.1 ; python_version >= "3.7" and python_version < "4.0" loguru==0.6.0 ; python_version >= "3.7" and python_version < "4.0" marshmallow==3.18.0 ; python_version >= "3.7" and python_version < "4.0" @@ -34,6 +34,7 @@ six==1.16.0 ; python_version >= "3.7" and python_version < "4.0" sniffio==1.3.0 ; python_version >= "3.7" and python_version < "4.0" sqlalchemy-aio==0.17.0 ; python_version >= "3.7" and python_version < "4.0" sqlalchemy==1.3.24 ; python_version >= "3.7" and python_version < "4.0" +starlette-context==0.3.4 ; python_version >= "3.7" and python_version < "4.0" starlette==0.19.1 ; python_version >= "3.7" and python_version < "4.0" tomli==2.0.1 ; python_version >= "3.7" and python_version < "4.0" typing-extensions==4.3.0 ; python_version >= "3.7" and python_version < "4.0"