This repository was archived by the owner on Jan 11, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthentication_manager.py
More file actions
54 lines (51 loc) · 2.33 KB
/
authentication_manager.py
File metadata and controls
54 lines (51 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import uuid
import json
import asyncio
from typing import List
from logger import Logger
class AuthenticationManager:
def __init__(self, ws_manager):
self.logger = Logger("AuthenticationManager")
self.ws_manager = ws_manager
self.authenticated_addresses: List[str] = []
self.auth_timeout = 30
self.logger.info("AuthenticationManager initialized")
async def authenticate_wallet(self, wallet_address: str):
try:
masked_address = f"{wallet_address[:6]}...{wallet_address[-4:]}"
self.logger.info(f"Authenticating wallet: {masked_address}")
if wallet_address in self.authenticated_addresses:
self.logger.info(f"Already authenticated wallet: {masked_address}")
return True
# 发送认证请求
auth_request = {
"method": "walletAuth/authenticateWallet",
"args": {
"walletAddress": wallet_address,
"referred_by_id": "",
"telegramId": None
},
"requestId": str(uuid.uuid4())
}
await self.ws_manager.send(json.dumps(auth_request), auth_request["requestId"])
# 等待认证响应
response_queue = self.ws_manager.register_listener(auth_request["requestId"])
try:
# Get the response with timeout
response = await asyncio.wait_for(response_queue.get(), timeout=self.auth_timeout)
if response and response.get("code") == 200:
self.authenticated_addresses.append(wallet_address)
self.logger.success(f"Wallet authenticated successfully: {masked_address}")
return True
else:
self.logger.error(f"Wallet authentication failed: {response}")
return False
except asyncio.TimeoutError:
self.logger.error(f"Authentication timeout for wallet: {masked_address}")
return False
finally:
# Always unregister the listener
self.ws_manager.unregister_listener(auth_request["requestId"])
except Exception as e:
self.logger.error(f"Error during wallet authentication: {str(e)}")
return False