Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
callebtc committed Jan 24, 2025
1 parent 86c9934 commit 1f61924
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 32 deletions.
27 changes: 22 additions & 5 deletions cashu/wallet/auth/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from loguru import logger

from cashu.core.helpers import sum_proofs
from cashu.core.mint_info import MintInfo

from ...core.base import Proof
Expand All @@ -18,7 +19,7 @@ class WalletAuth(Wallet):
oidc_discovery_url: str
oidc_client: OpenIDClient
wallet_db: Database
auth_flow: AuthorizationFlow = AuthorizationFlow.AUTHORIZATION_CODE
auth_flow: AuthorizationFlow
username: str | None
password: str | None
# API prefix for all requests
Expand Down Expand Up @@ -71,12 +72,10 @@ async def with_db(cls, *args, **kwargs) -> "WalletAuth":

url: str = kwargs.get("url", "")
db = kwargs.get("db", "")
name = kwargs.get("name", "auth")
kwargs["name"] = kwargs.get("name", "auth")
name = kwargs["name"]
username = kwargs.get("username")
password = kwargs.get("password")
wallet_db_name = kwargs.get("wallet_db")
if not wallet_db_name:
raise Exception("Wallet db location is required.")
wallet_db = Database(name, db)

# run migrations etc
Expand Down Expand Up @@ -141,6 +140,24 @@ async def init_wallet(self, mint_info: MintInfo) -> bool:
# Store the access and refresh tokens in the database
await self.store_clear_auth_token()

# mint auth tokens if necessary
MIN_BALANCE = self.mint_info.bat_max_mint

if self.available_balance < MIN_BALANCE:
logger.debug(
f"Balance too low. Minting {self.unit.str(MIN_BALANCE)} auth tokens."
)
try:
new_proofs = await self.mint_blind_auth_proofs()
logger.debug(
f"Minted {self.unit.str(sum_proofs(new_proofs))} blind auth proofs."
)
except Exception as e:
logger.error(f"Error minting auth proofs: {str(e)}")

if not self.proofs:
raise Exception("No auth proofs available.")

return True

async def store_username_password(self) -> None:
Expand Down
28 changes: 1 addition & 27 deletions cashu/wallet/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,10 @@ async def wrapper(*args, **kwargs):
ctx = args[0] # Assuming the first argument is 'ctx'
wallet: Wallet = ctx.obj["WALLET"]
db_location = wallet.db.db_location
wallet_db_name = wallet.db.name

auth_wallet = await WalletAuth.with_db(
url=ctx.obj["HOST"],
db=db_location,
name="auth",
unit=Unit.auth.name,
wallet_db=wallet_db_name,
)

requires_auth = await auth_wallet.init_wallet(wallet.mint_info)
Expand All @@ -110,29 +106,10 @@ async def wrapper(*args, **kwargs):
logger.debug("Mint does not require clear auth.")
return await func(*args, **kwargs)

MIN_BALANCE = wallet.mint_info.bat_max_mint

# Check balance and mint new auth proofs if necessary
if auth_wallet.available_balance < MIN_BALANCE:
logger.debug(
f"Balance too low. Minting {auth_wallet.unit.str(MIN_BALANCE)} auth tokens."
)
try:
new_proofs = await auth_wallet.mint_blind_auth_proofs()
logger.debug(
f"Minted {auth_wallet.unit.str(sum_proofs(new_proofs))} blind auth proofs."
)
except Exception as e:
logger.error(f"Error minting auth proofs: {str(e)}")

if not auth_wallet.proofs:
logger.error("Error initializing auth wallet.")
return

# Pass auth_db and auth_keyset_id to the wallet object
wallet.auth_db = auth_wallet.db
wallet.auth_keyset_id = auth_wallet.keyset_id
# pass the mint_info so it doesn't need to be re-fetched
# pass the mint_info so the wallet doesn't need to re-fetch it
wallet.mint_info = auth_wallet.mint_info

# Pass the auth_wallet to context
Expand Down Expand Up @@ -1296,9 +1273,6 @@ async def auth(ctx: Context, force: bool, mint: bool, password: bool):
auth_wallet = await WalletAuth.with_db(
url=ctx.obj["HOST"],
db=wallet.db.db_location,
name="auth",
unit=Unit.auth.name,
wallet_db=wallet.db.name,
username=username,
password=password_str,
)
Expand Down

0 comments on commit 1f61924

Please sign in to comment.