Files
gogo2/NN/exchanges/mexc/debug/test_mexc_signature_debug.py
Dobromir Popov c651ae585a mexc debug files
2025-07-14 12:32:06 +03:00

140 lines
4.4 KiB
Python

#!/usr/bin/env python3
"""
Debug MEXC Signature Generation
Tests signature generation against known working examples
"""
import os
import sys
import time
import hmac
import hashlib
import logging
from pathlib import Path
# Add project root to path
project_root = Path(__file__).parent
sys.path.insert(0, str(project_root))
# Enable debug logging
logging.basicConfig(level=logging.DEBUG)
def test_signature_generation():
"""Test signature generation with known parameters"""
print("MEXC Signature Generation Debug")
print("=" * 50)
# Get API credentials
api_key = os.getenv('MEXC_API_KEY', '')
api_secret = os.getenv('MEXC_SECRET_KEY', '')
if not api_key or not api_secret:
print("❌ No MEXC API credentials found")
return False
# Import the interface
from NN.exchanges.mexc_interface import MEXCInterface
mexc = MEXCInterface(api_key=api_key, api_secret=api_secret, test_mode=False)
# Test 1: Manual signature generation (working method from examples)
print("\n1. Manual signature generation (working method):")
timestamp = str(int(time.time() * 1000))
# Parameters in exact order from working example
params_string = f"timestamp={timestamp}&recvWindow=5000"
print(f"Params string: {params_string}")
signature_manual = hmac.new(
api_secret.encode('utf-8'),
params_string.encode('utf-8'),
hashlib.sha256
).hexdigest()
print(f"Manual signature: {signature_manual}")
# Test 2: Interface signature generation
print("\n2. Interface signature generation:")
params_dict = {
'timestamp': timestamp,
'recvWindow': '5000'
}
signature_interface = mexc._generate_signature(params_dict)
print(f"Interface signature: {signature_interface}")
# Compare
if signature_manual == signature_interface:
print("✅ Signatures match!")
else:
print("❌ Signatures don't match")
print("This indicates a problem with the signature generation method")
# Test 3: Try account request with manual signature
print("\n3. Testing account request with manual method:")
import requests
url = f"https://api.mexc.com/api/v3/account"
headers = {
'X-MEXC-APIKEY': api_key
}
params = {
'timestamp': timestamp,
'recvWindow': '5000',
'signature': signature_manual
}
print(f"Making request to: {url}")
print(f"Headers: {headers}")
print(f"Params: {params}")
try:
response = requests.get(url, headers=headers, params=params, timeout=10)
print(f"Response status: {response.status_code}")
print(f"Response: {response.text}")
if response.status_code == 200:
print("✅ Manual method works!")
return True
else:
print("❌ Manual method failed")
# Test 4: Try different parameter ordering
print("\n4. Testing different parameter orderings:")
# Try alphabetical ordering (current implementation)
params_alpha = sorted(params_dict.items())
params_alpha_string = '&'.join([f"{k}={v}" for k, v in params_alpha])
print(f"Alphabetical: {params_alpha_string}")
# Try the exact order from Postman collection
params_postman_string = f"recvWindow=5000&timestamp={timestamp}"
print(f"Postman order: {params_postman_string}")
sig_alpha = hmac.new(api_secret.encode('utf-8'), params_alpha_string.encode('utf-8'), hashlib.sha256).hexdigest()
sig_postman = hmac.new(api_secret.encode('utf-8'), params_postman_string.encode('utf-8'), hashlib.sha256).hexdigest()
print(f"Alpha signature: {sig_alpha}")
print(f"Postman signature: {sig_postman}")
# Test with postman order
params_test = {
'timestamp': timestamp,
'recvWindow': '5000',
'signature': sig_postman
}
response2 = requests.get(url, headers=headers, params=params_test, timeout=10)
print(f"Postman order response: {response2.status_code} - {response2.text}")
except Exception as e:
print(f"Request failed: {e}")
return False
return False
if __name__ == "__main__":
test_signature_generation()