From 8de3bda191d0ebc671d6b5fa9c861bacfa2805b1 Mon Sep 17 00:00:00 2001 From: GitHappens2Me Date: Sun, 25 May 2025 14:51:53 +0200 Subject: [PATCH] identified problem with cashu tokens from different mints --- router/auth.py | 2 +- router/cashu.py | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/router/auth.py b/router/auth.py index 7dfd4b8e..32428ded 100644 --- a/router/auth.py +++ b/router/auth.py @@ -38,7 +38,7 @@ async def validate_bearer_key(bearer_key: str, session: AsyncSession) -> ApiKey: if exsisting_key := await session.get(ApiKey, hashed_key): return exsisting_key new_key = ApiKey(hashed_key=hashed_key, balance=0) - await credit_balance(bearer_key, new_key, session) + await credit_balance(bearer_key, new_key, session) #TODO: see cashu.py "_initialize_wallet" await session.refresh(new_key) return new_key except Exception as e: diff --git a/router/cashu.py b/router/cashu.py index 6ff940fe..6d49f4af 100644 --- a/router/cashu.py +++ b/router/cashu.py @@ -13,11 +13,13 @@ MINIMUM_PAYOUT = int(os.environ.get("MINIMUM_PAYOUT", 10)) DEVS_DONATION_RATE = 0 # 0.021 # 2.1% WALLET = None - +#TODO +# This causes problems when users send tokens from other mints +# WALLET is already set so it returns the specified wallet, but this wallet does not know the keyset of the token async def _initialize_wallet(mint_url: str | None = None) -> Wallet: """Initializes and loads a Cashu wallet.""" global WALLET - if WALLET is not None: + if WALLET is not None and WALLET.mint_url == mint_url: # only return existing wallet if the mint_url matches the one of current WALLET return WALLET if mint_url is None: mint_url = MINT @@ -121,6 +123,7 @@ async def pay_out(session: AsyncSession) -> None: async def credit_balance(cashu_token: str, key: ApiKey, session: AsyncSession) -> int: token_obj: Token = deserialize_token_from_string(cashu_token) + token_mint = await _initialize_wallet(token_obj.mint) wallet: Wallet = await _initialize_wallet(token_obj.mint) amount_msats = await _handle_token_receive(wallet, token_obj) key.balance += amount_msats