From d5e92a3f444eece8dd1653526a35d5fa247f5e0d Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Mon, 10 Oct 2022 22:08:11 +0200 Subject: [PATCH] keyset with version --- cashu/core/base.py | 4 ++++ cashu/mint/crud.py | 6 +++--- cashu/mint/ledger.py | 9 ++++++--- cashu/mint/migrations.py | 7 +++++++ 4 files changed, 20 insertions(+), 6 deletions(-) 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..a17cff6 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.") @@ -118,7 +118,10 @@ class Ledger: # backwards compatibility with old hash_to_curve # old clients do not send a version - if not context.get("client-version"): + if ( + not context.get("client-version") + or not self.keysets.keysets[proof.id].version + ): return legacy.verify_pre_0_3_3(secret_key, C, proof.secret) return b_dhke.verify(secret_key, C, proof.secret) 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")