Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
callebtc committed Oct 7, 2023
1 parent 8ef94cc commit 5154303
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 34 deletions.
6 changes: 6 additions & 0 deletions cashu/mint/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
*,
Expand Down
27 changes: 16 additions & 11 deletions cashu/mint/ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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([])
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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"""
Expand Down
46 changes: 23 additions & 23 deletions cashu/wallet/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 5154303

Please sign in to comment.