-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_wallet_creation.py
More file actions
181 lines (145 loc) Β· 5.22 KB
/
test_wallet_creation.py
File metadata and controls
181 lines (145 loc) Β· 5.22 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env python3
"""
Test script to verify wallet creation functionality
"""
import sys
import os
def test_dependencies():
"""Test if all required dependencies are available"""
print("π Testing dependencies...")
# Test Web3
try:
from web3 import Web3
print("β
Web3 available")
except ImportError as e:
print(f"β Web3 not available: {e}")
return False
# Test Account
try:
from eth_account import Account
print("β
eth_account available")
except ImportError as e:
print(f"β eth_account not available: {e}")
return False
# Test Cryptography
try:
from cryptography.fernet import Fernet
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
import base64
print("β
cryptography available")
except ImportError as e:
print(f"β cryptography not available: {e}")
return False
return True
def test_wallet_creation():
"""Test wallet creation functionality"""
print("\nπ Testing wallet creation...")
try:
from eth_account import Account
from cryptography.fernet import Fernet
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
import base64
import os
# Test account creation
account = Account.create()
private_key = account.key.hex()
wallet_address = account.address
print(f"β
Account created successfully")
print(f"π Address: {wallet_address}")
print(f"π Private key length: {len(private_key)} characters")
# Test encryption
password = "TestPassword123!"
salt = os.urandom(16)
# Generate encryption key
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=100000,
)
key = base64.urlsafe_b64encode(kdf.derive(password.encode()))
# Encrypt private key
f = Fernet(key)
encrypted_key = f.encrypt(private_key.encode())
print(f"β
Private key encrypted successfully")
print(f"π Encrypted key length: {len(encrypted_key)} bytes")
# Test decryption
decrypted_key = f.decrypt(encrypted_key)
decrypted_private_key = decrypted_key.decode()
if decrypted_private_key == private_key:
print("β
Private key decrypted successfully")
else:
print("β Private key decryption failed")
return False
return True
except Exception as e:
print(f"β Wallet creation test failed: {e}")
return False
def test_database():
"""Test database functionality"""
print("\nποΈ Testing database...")
try:
import sqlite3
# Create test database
test_db = 'test_wallet.db'
conn = sqlite3.connect(test_db)
cursor = conn.cursor()
# Create tables
cursor.execute('''
CREATE TABLE IF NOT EXISTS connected_wallets (
id INTEGER PRIMARY KEY AUTOINCREMENT,
chat_id INTEGER NOT NULL,
wallet_address TEXT NOT NULL,
wallet_name TEXT,
is_active BOOLEAN DEFAULT 1,
date_connected TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
UNIQUE(chat_id, wallet_address)
)
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS wallet_keys (
id INTEGER PRIMARY KEY AUTOINCREMENT,
chat_id INTEGER NOT NULL,
wallet_address TEXT NOT NULL,
encrypted_private_key TEXT NOT NULL,
salt TEXT NOT NULL,
date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
UNIQUE(chat_id, wallet_address)
)
''')
conn.commit()
conn.close()
print("β
Database tables created successfully")
# Clean up test database
os.remove(test_db)
print("β
Test database cleaned up")
return True
except Exception as e:
print(f"β Database test failed: {e}")
return False
def main():
"""Run all tests"""
print("π§ͺ TradeSeer Wallet Creation Test Suite")
print("=" * 50)
# Test dependencies
deps_ok = test_dependencies()
if not deps_ok:
print("\nβ Dependencies test failed. Please install missing packages:")
print("pip install web3 eth-account cryptography")
return False
# Test wallet creation
wallet_ok = test_wallet_creation()
# Test database
db_ok = test_database()
print("\n" + "=" * 50)
if deps_ok and wallet_ok and db_ok:
print("π All tests passed! Wallet creation should work properly.")
return True
else:
print("β Some tests failed. Please check the errors above.")
return False
if __name__ == "__main__":
success = main()
sys.exit(0 if success else 1)