Merge branch origin/codex/add-wallet-handling-to-registration into main

This commit is contained in:
Alexa Amundson
2025-11-16 01:51:25 -06:00
6 changed files with 188 additions and 13 deletions

View File

@@ -6,6 +6,7 @@ from sqlalchemy import select
from app.database import get_db
from app.models.user import User
from app.models.blockchain import Wallet
from app.schemas.user import UserCreate, UserResponse, Token, UserLogin
from app.auth import (
verify_password,
@@ -39,7 +40,7 @@ async def register(user_data: UserCreate, db: AsyncSession = Depends(get_db)):
)
# Generate wallet
wallet_address, private_key = BlockchainService.generate_wallet_address()
wallet_address, private_key, public_key = BlockchainService.generate_wallet_address()
try:
encrypted_private_key = wallet_crypto.encrypt(private_key)
@@ -62,6 +63,19 @@ async def register(user_data: UserCreate, db: AsyncSession = Depends(get_db)):
)
db.add(user)
await db.flush()
wallet = Wallet(
user_id=user.id,
address=wallet_address,
private_key=private_key,
public_key=public_key,
balance=user.balance,
label="Primary Wallet",
is_primary=True,
)
db.add(wallet)
await db.commit()
await db.refresh(user)
@@ -124,3 +138,10 @@ async def get_current_user_info(
async def logout():
"""Logout (client should delete token)"""
return {"message": "Successfully logged out"}
async def get_current_user(
current_user: User = Depends(get_current_active_user)
) -> User:
"""Reusable dependency that returns the authenticated user."""
return current_user

View File

@@ -138,22 +138,37 @@ async def get_miner_stats(
)
wallet = wallet_result.scalar_one_or_none()
if not wallet:
return MinerStats(
blocks_mined=0,
roadcoins_earned=0.0,
current_hashrate_mhs=miner_state.hashrate_mhs if miner_state.is_mining else 0.0,
average_hashrate_mhs=0.0,
total_shares=miner_state.shares_submitted,
accepted_shares=miner_state.shares_accepted,
rejected_shares=miner_state.shares_submitted - miner_state.shares_accepted,
last_block_time=None,
mining_since=miner_state.started_at,
)
miner_filter = Block.miner_address == wallet.address
# Count blocks mined by this user
blocks_count_result = await db.execute(
select(func.count(Block.id)).filter(Block.miner == wallet.address if wallet else None)
select(func.count(Block.id)).filter(miner_filter)
)
blocks_mined = blocks_count_result.scalar() or 0
# Sum rewards earned
rewards_result = await db.execute(
select(func.sum(Block.reward)).filter(Block.miner == wallet.address if wallet else None)
select(func.sum(Block.reward)).filter(miner_filter)
)
roadcoins_earned = rewards_result.scalar() or 0.0
# Get last block mined
last_block_result = await db.execute(
select(Block)
.filter(Block.miner == wallet.address if wallet else None)
.filter(miner_filter)
.order_by(desc(Block.timestamp))
.limit(1)
)
@@ -197,7 +212,7 @@ async def get_recent_blocks(
# Get recent blocks
blocks_result = await db.execute(
select(Block)
.filter(Block.miner == wallet.address)
.filter(Block.miner_address == wallet.address)
.order_by(desc(Block.timestamp))
.limit(limit)
)