Skip to content

Commit

Permalink
skip_private_key -> skip_db_read
Browse files Browse the repository at this point in the history
  • Loading branch information
callebtc committed Nov 27, 2023
1 parent 015dd24 commit 9858c90
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 25 deletions.
4 changes: 2 additions & 2 deletions cashu/wallet/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,13 @@ async def cli(ctx: Context, host: str, walletname: str, unit: str, tests: bool):
# otherwise it will create a mnemonic and store it in the database
if ctx.invoked_subcommand == "restore":
wallet = await Wallet.with_db(
ctx.obj["HOST"], db_path, name=walletname, skip_private_key=True
ctx.obj["HOST"], db_path, name=walletname, skip_db_read=True
)
else:
# # we need to run the migrations before we load the wallet for the first time
# # otherwise the wallet will not be able to generate a new private key and store it
wallet = await Wallet.with_db(
ctx.obj["HOST"], db_path, name=walletname, skip_private_key=True
ctx.obj["HOST"], db_path, name=walletname, skip_db_read=True
)
# now with the migrations done, we can load the wallet and generate a new mnemonic if needed
wallet = await Wallet.with_db(ctx.obj["HOST"], db_path, name=walletname)
Expand Down
36 changes: 18 additions & 18 deletions cashu/wallet/migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,24 +139,24 @@ async def m007_nostr(db: Database):
"""
Stores timestamps of nostr operations.
"""
# async with db.connect() as conn:
# await conn.execute("""
# CREATE TABLE IF NOT EXISTS nostr (
# type TEXT NOT NULL,
# last TIMESTAMP DEFAULT NULL
# )
# """)
# await conn.execute(
# """
# INSERT INTO nostr
# (type, last)
# VALUES (?, ?)
# """,
# (
# "dm",
# None,
# ),
# )
async with db.connect() as conn:
await conn.execute("""
CREATE TABLE IF NOT EXISTS nostr (
type TEXT NOT NULL,
last TIMESTAMP DEFAULT NULL
)
""")
await conn.execute(
"""
INSERT INTO nostr
(type, last)
VALUES (?, ?)
""",
(
"dm",
None,
),
)


async def m008_keysets_add_public_keys(db: Database):
Expand Down
12 changes: 7 additions & 5 deletions cashu/wallet/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,27 +721,29 @@ async def with_db(
url: str,
db: str,
name: str = "no_name",
skip_private_key: bool = False,
skip_db_read: bool = False,
):
"""Initializes a wallet with a database and initializes the private key.
Args:
url (str): URL of the mint.
db (str): Path to the database.
name (str, optional): Name of the wallet. Defaults to "no_name".
skip_private_key (bool, optional): If true, the private key is not initialized. Defaults to False.
skip_db_read (bool, optional): If true, values from db like private key and
keysets are not loaded. Useful for running only migrations and returning.
Defaults to False.
Returns:
Wallet: Initialized wallet.
"""
logger.debug(f"Initializing wallet with database: {db}")
self = cls(url=url, db=db, name=name)
await self._migrate_database()
if not skip_private_key:
if not skip_db_read:
await self._init_private_key()
keysets_list = await get_keysets(mint_url=url, db=self.db)
self.keysets = {k.id: k for k in keysets_list}

keysets_list = await get_keysets(mint_url=url, db=self.db)
self.keysets = {k.id: k for k in keysets_list}
return self

async def _migrate_database(self):
Expand Down

0 comments on commit 9858c90

Please sign in to comment.