diff --git a/aitbc/config.py b/aitbc/config.py index b96b8525..e1c1d490 100644 --- a/aitbc/config.py +++ b/aitbc/config.py @@ -41,6 +41,14 @@ class BaseAITBCConfig(BaseSettings): default="%(asctime)s - %(name)s - %(levelname)s - %(message)s", description="Log format string" ) + + def __init__(self, **kwargs): + """Initialize configuration with logging""" + super().__init__(**kwargs) + logger.info(f"Loading configuration for {self.app_name} v{self.app_version} in {self.environment} environment") + logger.debug(f"Data directory: {self.data_dir}") + logger.debug(f"Config directory: {self.config_dir}") + logger.debug(f"Log directory: {self.log_dir}") class AITBCConfig(BaseAITBCConfig): diff --git a/aitbc/crypto.py b/aitbc/crypto.py index 5385b91b..49e112fc 100644 --- a/aitbc/crypto.py +++ b/aitbc/crypto.py @@ -100,12 +100,15 @@ def encrypt_private_key(private_key: str, password: str) -> str: def decrypt_private_key(encrypted_key: str, password: str) -> str: """Decrypt private key using Fernet symmetric encryption""" try: - # Decode combined data + # Decode combined salt + encrypted data combined = base64.urlsafe_b64decode(encrypted_key.encode('utf-8')) + + # Extract salt (first 16 bytes) and encrypted data (remaining bytes) salt = combined[:16] encrypted_data = combined[16:] - # Derive key from password + # Derive same encryption key from password using stored salt + # Must use identical parameters as encryption for successful decryption password_bytes = password.encode('utf-8') kdf = PBKDF2HMAC( algorithm=hashes.SHA256(), @@ -115,7 +118,7 @@ def decrypt_private_key(encrypted_key: str, password: str) -> str: ) key = base64.urlsafe_b64encode(kdf.derive(password_bytes)) - # Decrypt private key + # Decrypt private key using derived key fernet = Fernet(key) decrypted_key = fernet.decrypt(encrypted_data) return decrypted_key.decode('utf-8') diff --git a/aitbc/events.py b/aitbc/events.py index 121c1dc9..a2490c14 100644 --- a/aitbc/events.py +++ b/aitbc/events.py @@ -261,7 +261,9 @@ class EventRouter: await handler(event) else: handler(event) + self.logger.debug(f"Routed event {event.event_type} to handler") return True except Exception as e: - print(f"Error in routed handler: {e}") + self.logger.error(f"Routed handler failed for {event.event_type}: {e}") + self.logger.debug(f"No matching route found for event {event.event_type}") return False