-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinitial_data.py
More file actions
434 lines (361 loc) Β· 16.4 KB
/
initial_data.py
File metadata and controls
434 lines (361 loc) Β· 16.4 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# initialize_contract.py - Script to populate contract with initial data
import hashlib
import json
import time
from datetime import datetime
from typing import List, Dict
import requests
from web3 import Web3
from eth_account import Account
import os
class ContractInitializer:
def __init__(self, contract_address: str, private_key: str = None):
self.contract_address = contract_address
# Flow Testnet configuration
self.rpc_url = "https://testnet.evm.nodes.onflow.org"
self.chain_id = 545
# Initialize Web3
self.w3 = Web3(Web3.HTTPProvider(self.rpc_url))
if private_key:
self.account = Account.from_key(private_key)
else:
self.account = None
print(f"π Connected to Flow EVM Testnet")
print(f"π‘ Contract: {contract_address}")
print(f"π Connected: {self.w3.is_connected()}")
def create_sample_repositories(self) -> List[Dict]:
"""Create sample repository data for testing"""
sample_repos = [
{
"github_url": "https://github.com/onflow/flow-go",
"name": "flow-go",
"description": "Flow blockchain implementation in Go",
"language": "Go",
"license_type": "Apache-2.0",
"owner": "onflow"
},
{
"github_url": "https://github.com/onflow/cadence",
"name": "cadence",
"description": "Cadence smart contract language",
"language": "Go",
"license_type": "Apache-2.0",
"owner": "onflow"
},
{
"github_url": "https://github.com/microsoft/vscode",
"name": "vscode",
"description": "Visual Studio Code editor",
"language": "TypeScript",
"license_type": "MIT",
"owner": "microsoft"
},
{
"github_url": "https://github.com/facebook/react",
"name": "react",
"description": "React JavaScript library",
"language": "JavaScript",
"license_type": "MIT",
"owner": "facebook"
},
{
"github_url": "https://github.com/ethereum/solidity",
"name": "solidity",
"description": "Solidity smart contract language",
"language": "C++",
"license_type": "GPL-3.0",
"owner": "ethereum"
}
]
# Process each repository
processed_repos = []
for repo in sample_repos:
# Generate fingerprint data
fingerprint_data = {
'name': repo['name'],
'description': repo['description'],
'language': repo['language'],
'github_url': repo['github_url'],
'created_at': datetime.now().isoformat(),
}
# Generate hashes
repo_hash = hashlib.sha256(
json.dumps(fingerprint_data, sort_keys=True).encode()
).hexdigest()
fingerprint = hashlib.sha256(
f"{repo['owner']}/{repo['name']}{repo['github_url']}".encode()
).hexdigest()
# Generate key features
key_features = [
f"{repo['language']} implementation",
f"{repo['name']} core functionality",
f"Open source {repo['license_type']} licensed",
f"Maintained by {repo['owner']}"
]
processed_repo = {
**repo,
'repo_hash': repo_hash,
'fingerprint': fingerprint,
'key_features': key_features,
'processed_at': datetime.now().isoformat()
}
processed_repos.append(processed_repo)
return processed_repos
def simulate_contract_registration(self, repo_data: Dict) -> Dict:
"""Simulate registering repository on blockchain"""
print(f"π Simulating contract registration for: {repo_data['name']}")
# Simulate transaction
tx_hash = f"0x{hashlib.sha256(f'{repo_data["github_url"]}{time.time()}'.encode()).hexdigest()}"
# Simulate gas usage
gas_used = 150000 + len(repo_data['key_features']) * 5000
result = {
'success': True,
'tx_hash': tx_hash,
'contract_address': self.contract_address,
'gas_used': gas_used,
'network': 'Flow Testnet',
'block_number': int(time.time()) % 1000000, # Simulate block number
'registered_at': datetime.now().isoformat()
}
print(f" β
Transaction: {tx_hash[:16]}...")
print(f" β½ Gas Used: {gas_used:,}")
return result
def create_sample_violations(self, repositories: List[Dict]) -> List[Dict]:
"""Create sample violation data"""
sample_violations = [
{
"original_repo": repositories[0], # flow-go
"violating_url": "https://github.com/fake-user/flow-copy",
"similarity_score": 0.85,
"reason": "Copied core Flow blockchain implementation"
},
{
"original_repo": repositories[2], # vscode
"violating_url": "https://github.com/clone-user/code-editor",
"similarity_score": 0.72,
"reason": "Similar editor functionality and UI patterns"
},
{
"original_repo": repositories[3], # react
"violating_url": "https://github.com/copy-cat/react-clone",
"similarity_score": 0.91,
"reason": "Direct copy of React component system"
}
]
processed_violations = []
for i, violation in enumerate(sample_violations):
# Generate evidence hash
evidence = {
'violating_url': violation['violating_url'],
'similarity_score': violation['similarity_score'],
'reason': violation['reason'],
'detected_at': datetime.now().isoformat()
}
evidence_hash = hashlib.sha256(
json.dumps(evidence, sort_keys=True).encode()
).hexdigest()
# Simulate blockchain transaction
tx_hash = f"0x{hashlib.sha256(f'{violation["violating_url"]}{time.time()}'.encode()).hexdigest()}"
processed_violation = {
'violation_id': i + 1,
'original_repo_id': repositories.index(violation['original_repo']) + 1,
'original_repo_name': violation['original_repo']['name'],
'violating_url': violation['violating_url'],
'similarity_score': violation['similarity_score'],
'reason': violation['reason'],
'evidence_hash': evidence_hash,
'tx_hash': tx_hash,
'status': 'pending',
'reported_at': datetime.now().isoformat()
}
processed_violations.append(processed_violation)
return processed_violations
def initialize_contract_data(self) -> Dict:
"""Initialize contract with sample data"""
print("π Initializing contract with sample data...")
print("="*60)
# Step 1: Create sample repositories
print("π Creating sample repositories...")
repositories = self.create_sample_repositories()
# Step 2: Register repositories
print(f"\nπ Registering {len(repositories)} repositories...")
registered_repos = []
for i, repo in enumerate(repositories):
repo_id = i + 1
registration_result = self.simulate_contract_registration(repo)
# Combine repo data with registration result
full_repo_data = {
'repo_id': repo_id,
**repo,
**registration_result
}
registered_repos.append(full_repo_data)
time.sleep(1) # Simulate transaction delay
# Step 3: Create sample violations
print(f"\nβ οΈ Creating sample violations...")
violations = self.create_sample_violations(repositories)
for violation in violations:
print(f" π Violation: {violation['violating_url']}")
print(f" Similarity: {violation['similarity_score']:.1%}")
print(f" Reason: {violation['reason']}")
# Step 4: Generate summary
summary = {
'initialization_completed_at': datetime.now().isoformat(),
'contract_address': self.contract_address,
'network': 'Flow Testnet',
'repositories_registered': len(registered_repos),
'violations_created': len(violations),
'total_gas_used': sum(repo.get('gas_used', 0) for repo in registered_repos),
'repositories': registered_repos,
'violations': violations
}
return summary
def save_initialization_data(self, data: Dict, filename: str = "contract_initialization.json"):
"""Save initialization data to file"""
try:
with open(filename, 'w') as f:
json.dump(data, f, indent=2, default=str)
print(f"πΎ Initialization data saved to: {filename}")
return True
except Exception as e:
print(f"β Failed to save data: {e}")
return False
class AgentDataSeeder:
"""Seeds the agent's in-memory storage with initial data"""
def __init__(self, agent_url: str = "http://localhost:8000"):
self.agent_url = agent_url
def check_agent_health(self) -> bool:
"""Check if agent is running and healthy"""
try:
response = requests.get(f"{self.agent_url}/")
if response.status_code == 200:
data = response.json()
return data.get('agent_ready', False)
return False
except Exception as e:
print(f"β Agent health check failed: {e}")
return False
def seed_repositories(self, repositories: List[Dict]) -> List[Dict]:
"""Seed agent with repository data"""
print(f"π± Seeding agent with {len(repositories)} repositories...")
seeded_repos = []
for repo in repositories:
try:
# Register repository with agent
response = requests.post(
f"{self.agent_url}/register-repository",
json={
"github_url": repo['github_url'],
"license_type": repo['license_type'],
"description": repo['description']
}
)
if response.status_code == 200:
result = response.json()
if result.get('success'):
print(f" β
{repo['name']}: Registered with ID {result.get('repo_id')}")
seeded_repos.append({**repo, 'agent_repo_id': result.get('repo_id')})
else:
print(f" β {repo['name']}: Registration failed")
else:
print(f" β {repo['name']}: HTTP {response.status_code}")
except Exception as e:
print(f" β {repo['name']}: Error - {e}")
time.sleep(2) # Avoid overwhelming the agent
return seeded_repos
def trigger_security_audits(self, repositories: List[Dict]) -> List[Dict]:
"""Trigger security audits for repositories"""
print(f"π Triggering security audits...")
audit_results = []
for repo in repositories[:3]: # Limit to first 3 to avoid long wait
try:
print(f" π Auditing {repo['name']}...")
response = requests.post(
f"{self.agent_url}/security-audit",
json={
"github_url": repo['github_url'],
"audit_type": "comprehensive"
}
)
if response.status_code == 200:
result = response.json()
if result.get('success'):
print(f" β
Audit completed - ID: {result.get('audit_id')}")
audit_results.append(result)
else:
print(f" β Audit failed")
else:
print(f" β HTTP {response.status_code}")
except Exception as e:
print(f" β Error: {e}")
time.sleep(3) # Security audits take time
return audit_results
def main():
"""Main initialization function"""
# Configuration
contract_address = os.getenv('CONTRACT_ADDRESS', '0x5fa19b4a48C20202055c8a6fdf16688633617D50')
private_key = os.getenv('PRIVATE_KEY')
agent_url = os.getenv('AGENT_URL', 'http://localhost:8000')
print("π GitHub Protection Agent Initialization Script")
print("="*60)
print(f"π‘ Contract: {contract_address}")
print(f"π€ Agent URL: {agent_url}")
print(f"π Private Key: {'Set' if private_key else 'Not Set'}")
print()
# Step 1: Initialize contract data
print("Step 1: Initializing contract data...")
initializer = ContractInitializer(contract_address, private_key)
try:
init_data = initializer.initialize_contract_data()
print(f"β
Contract initialization completed!")
print(f" Repositories: {init_data['repositories_registered']}")
print(f" Violations: {init_data['violations_created']}")
print(f" Gas Used: {init_data['total_gas_used']:,}")
# Save initialization data
initializer.save_initialization_data(init_data)
except Exception as e:
print(f"β Contract initialization failed: {e}")
return False
# Step 2: Check if agent is running
print(f"\nStep 2: Checking agent status...")
seeder = AgentDataSeeder(agent_url)
if not seeder.check_agent_health():
print("β Agent is not running or not ready!")
print(" Please start the agent first:")
print(" python enhanced_fastapi_server.py")
# Ask user if they want to continue with just contract init
user_input = input("\nContinue with contract initialization only? (y/n): ")
if user_input.lower() != 'y':
return False
print("β
Contract initialization completed. Start agent and run seeding later.")
return True
print("β
Agent is healthy and ready!")
# Step 3: Seed agent with repository data
print(f"\nStep 3: Seeding agent with data...")
try:
seeded_repos = seeder.seed_repositories(init_data['repositories'])
print(f"β
Seeded {len(seeded_repos)} repositories into agent")
# Step 4: Trigger some security audits
print(f"\nStep 4: Triggering security audits...")
audit_results = seeder.trigger_security_audits(seeded_repos)
print(f"β
Completed {len(audit_results)} security audits")
except Exception as e:
print(f"β Agent seeding failed: {e}")
return False
# Final summary
print("\n" + "="*60)
print("π INITIALIZATION COMPLETE!")
print("="*60)
print(f"β
Contract initialized with sample data")
print(f"β
Agent seeded with {len(seeded_repos)} repositories")
print(f"β
{len(audit_results)} security audits completed")
print(f"β
Ready for testing!")
print()
print("Next steps:")
print("1. Test the agent: python test_agent.py quick")
print("2. View API docs: http://localhost:8000/docs")
print("3. Check system stats: http://localhost:8000/stats")
return True
if __name__ == "__main__":
success = main()
exit(0 if success else 1)