Skip to content

Commit

Permalink
load keys from db if available
Browse files Browse the repository at this point in the history
  • Loading branch information
callebtc committed Oct 7, 2023
1 parent edd3d31 commit 09e7d52
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cashu/wallet/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ async def get_keyset(
mint_url: str = "",
db: Optional[Database] = None,
conn: Optional[Connection] = None,
):
) -> WalletKeyset | None:
clauses = []
values: List[Any] = []
clauses.append("active = ?")
Expand Down
24 changes: 18 additions & 6 deletions cashu/wallet/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,21 @@ async def _load_mint_keys(self, keyset_id: Optional[str] = None) -> None:
self.url
), "Ledger not initialized correctly: mint URL not specified yet. "

keyset_local: Union[WalletKeyset, None] = None
if keyset_id:
# get requested keyset
# check if current keyset is in db
keyset_local = await get_keyset(keyset_id, db=self.db)
if keyset_local:
logger.debug(f"Found keyset {keyset_id} in database.")
else:
logger.debug(
f"Cannot find keyset {keyset_id} in database. Loading keyset from"
" mint."
)
keyset = keyset_local

if keyset_local is None and keyset_id:
# get requested keyset from mint
keyset = await self._get_keys_of_keyset(self.url, keyset_id)
else:
# get current keyset
Expand All @@ -165,20 +178,19 @@ async def _load_mint_keys(self, keyset_id: Optional[str] = None) -> None:
assert keyset.id
assert len(keyset.public_keys) > 0, "did not receive keys from mint."

if keyset_id != keyset.id:
if keyset_id and keyset_id != keyset.id:
# NOTE: Because of the upcoming change of how to calculate keyset ids
# with version 0.14.0, we overwrite the calculated keyset id with the
# requested one. This is a temporary fix and should be removed once all
# ecash is transitioned to 0.14.0.
logger.debug(
"Keyset ID mismatch. This can happen due to a version upgrade."
f"Keyset ID mismatch: {keyset_id} != {keyset.id}. This can happen due"
" to a version upgrade."
)
keyset.id = keyset_id or keyset.id

# check if current keyset is in db
keyset_local: Optional[WalletKeyset] = await get_keyset(keyset.id, db=self.db)
# if not, store it
if keyset_local is None:
if keyset_id and keyset_local is None:
logger.debug(f"Storing new mint keyset: {keyset.id}")
await store_keyset(keyset=keyset, db=self.db)

Expand Down

0 comments on commit 09e7d52

Please sign in to comment.