From 5154303328c4baf66359eaebbdc9fbc2be865726 Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Sat, 7 Oct 2023 22:33:09 +0200 Subject: [PATCH] cleanup --- cashu/mint/crud.py | 6 ++++++ cashu/mint/ledger.py | 27 +++++++++++++++----------- cashu/wallet/crud.py | 46 ++++++++++++++++++++++---------------------- 3 files changed, 45 insertions(+), 34 deletions(-) diff --git a/cashu/mint/crud.py b/cashu/mint/crud.py index b4f80e7c..0a444f43 100644 --- a/cashu/mint/crud.py +++ b/cashu/mint/crud.py @@ -62,6 +62,12 @@ async def update_lightning_invoice(self, *args, **kwags) -> None: class LedgerCrudSqlite(LedgerCrud): + """Implementation of LedgerCrud for sqlite. + + Args: + LedgerCrud (ABC): Abstract base class for LedgerCrud. + """ + async def store_promise( self, *, diff --git a/cashu/mint/ledger.py b/cashu/mint/ledger.py index de010a35..562dfcdd 100644 --- a/cashu/mint/ledger.py +++ b/cashu/mint/ledger.py @@ -48,14 +48,14 @@ def __init__( seed: str, lightning: Wallet, derivation_path="", - crud=LedgerCrudSqlite, + crud=LedgerCrudSqlite(), ): self.secrets_used: Set[str] = set() self.master_key = seed self.derivation_path = derivation_path self.db = db - self.crud = crud() + self.crud = crud self.lightning = lightning self.pubkey = derive_pubkey(self.master_key) self.keysets = MintKeysets([]) @@ -109,7 +109,8 @@ async def load_keyset(self, derivation_path, autosave=True) -> MintKeyset: self.keysets.keysets[keyset.id_deprecated] = copy.copy(keyset) self.keysets.keysets[keyset.id_deprecated].id = keyset.id_deprecated # END BACKWARDS COMPATIBILITY < 0.14.0 - logger.debug(f"Loaded keyset {keyset.id}.") + + logger.debug(f"Loaded keyset {keyset.id}") return keyset async def init_keysets(self, autosave=True) -> None: @@ -121,9 +122,10 @@ async def init_keysets(self, autosave=True) -> None: generated from `self.derivation_path`. Defaults to True. """ # load all past keysets from db - logger.trace("crud: loading keysets") tmp_keysets: List[MintKeyset] = await self.crud.get_keyset(db=self.db) - logger.trace(f"crud: loaded {len(tmp_keysets)} keysets") + logger.debug( + f"Loaded {len(tmp_keysets)} keysets from database. Generating keys..." + ) # add keysets from db to current keysets for k in tmp_keysets: if k.id and k.id not in self.keysets.keysets: @@ -134,20 +136,23 @@ async def init_keysets(self, autosave=True) -> None: # we already generated the keys for this keyset if v.id and v.public_keys and len(v.public_keys): continue - logger.debug(f"Generating keys for keyset {v.id}") + logger.trace(f"Generating keys for keyset {v.id}") v.generate_keys(self.master_key) - # BEGIN BACKWARDS COMPATIBILITY < 0.14.0 - # we store all keysets also by their deprecated id - logger.debug(f"Loading deprecated keyset {v.id_deprecated} (new: {v.id})") + # BEGIN BACKWARDS COMPATIBILITY < 0.14.0 + keyset_ids = list(self.keysets.keysets.values()) + for v in keyset_ids: + # we duplicate all keysets also by their deprecated id + logger.trace(f"Loading deprecated keyset {v.id_deprecated} (new: {v.id})") self.keysets.keysets[v.id_deprecated] = v - # END BACKWARDS COMPATIBILITY < 0.14.0 + # END BACKWARDS COMPATIBILITY < 0.14.0 - logger.debug( + logger.info( f"Initialized {len(self.keysets.keysets)} keysets from the database." ) # load the current keyset self.keyset = await self.load_keyset(self.derivation_path, autosave) + logger.info(f"Current keyset: {self.keyset.id}") def get_keyset(self, keyset_id: Optional[str] = None) -> Dict[int, str]: """Returns a dictionary of hex public keys of a specific keyset for each supported amount""" diff --git a/cashu/wallet/crud.py b/cashu/wallet/crud.py index fcdfab1a..f85a2bd1 100644 --- a/cashu/wallet/crud.py +++ b/cashu/wallet/crud.py @@ -264,29 +264,29 @@ async def store_lightning_invoice( ) -async def get_lightning_invoice( - db: Database, - hash: str = "", - conn: Optional[Connection] = None, -) -> Invoice: - clauses = [] - values: List[Any] = [] - if hash: - clauses.append("hash = ?") - values.append(hash) - - where = "" - if clauses: - where = f"WHERE {' AND '.join(clauses)}" - - row = await (conn or db).fetchone( - f""" - SELECT * from invoices - {where} - """, - tuple(values), - ) - return Invoice(**row) +# async def get_lightning_invoice( +# db: Database, +# hash: str = "", +# conn: Optional[Connection] = None, +# ) -> Invoice: +# clauses = [] +# values: List[Any] = [] +# if hash: +# clauses.append("hash = ?") +# values.append(hash) + +# where = "" +# if clauses: +# where = f"WHERE {' AND '.join(clauses)}" + +# row = await (conn or db).fetchone( +# f""" +# SELECT * from invoices +# {where} +# """, +# tuple(values), +# ) +# return Invoice(**row) async def get_lightning_invoices(