Spaces:
Sleeping
Sleeping
xyizko-SkeletonUpload
Browse filesSkeleton Application Guide
README.md
CHANGED
@@ -1,12 +1,14 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
|
4 |
-
|
5 |
-
|
|
|
6 |
sdk: gradio
|
7 |
sdk_version: 5.33.0
|
8 |
app_file: app.py
|
9 |
pinned: false
|
|
|
|
|
|
|
10 |
---
|
11 |
-
|
12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: Xyizko-Web3SecurityAuditor
|
3 |
+
short_description: Smart Contract Code Security Auditor
|
4 |
+
emoji: π€
|
5 |
+
colorFrom: green
|
6 |
+
colorTo: indigo
|
7 |
sdk: gradio
|
8 |
sdk_version: 5.33.0
|
9 |
app_file: app.py
|
10 |
pinned: false
|
11 |
+
tags:
|
12 |
+
- mcp-server-track
|
13 |
+
- agent-demo-track
|
14 |
---
|
|
|
|
app.py
ADDED
@@ -0,0 +1,324 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
# /////////////////////////////////////////////
|
3 |
+
# Xyizko - MCH Hackathon 2025 Skeleton
|
4 |
+
# - This will be updated continuously as progress thought he project
|
5 |
+
# /////////////////////////////////////////////
|
6 |
+
|
7 |
+
# --- Imports Section ---
|
8 |
+
|
9 |
+
import datetime
|
10 |
+
|
11 |
+
import gradio as gr
|
12 |
+
|
13 |
+
# --- Demo UI Component Functions ---
|
14 |
+
|
15 |
+
# App Introduction Function
|
16 |
+
|
17 |
+
|
18 |
+
def app_intro():
|
19 |
+
intro_text = """
|
20 |
+
# Xyizko - MCH Hackathon 2025 Smart Contract Analyzer
|
21 |
+
1. This gradio app serves as a skeleton for the MCH Hackathon 2025 project.
|
22 |
+
2. As components are developed they will be added to this app.
|
23 |
+
|
24 |
+
# App Description
|
25 |
+
**AI-Powered Smart Contract Security Analysis Platform**
|
26 |
+
|
27 |
+
Our platform leverages advanced AI agents to analyze smart contracts for vulnerabilities,
|
28 |
+
anti-patterns, and security issues. Upload your contract code and get comprehensive
|
29 |
+
security analysis powered by Hugging Face models.
|
30 |
+
"""
|
31 |
+
|
32 |
+
part3 = """
|
33 |
+
## Development Progress
|
34 |
+
> Changelog - Thu Jun 5 10:31:00 PM UTC 2025
|
35 |
+
1. β
Testing agents and Gradio UI Interaction
|
36 |
+
2. Testing MCP Server Implementations
|
37 |
+
3. β
Smart Contract Upload & Analysis System
|
38 |
+
4. AI-Powered Vulnerability Detection
|
39 |
+
5. β
Downloadable Analysis Reports
|
40 |
+
"""
|
41 |
+
|
42 |
+
gr.Markdown(intro_text)
|
43 |
+
gr.Markdown(
|
44 |
+
value="""
|
45 |
+
```mermaid
|
46 |
+
flowchart LR
|
47 |
+
A[Smart Contract Code] -->|Upload| B(AI Agents)
|
48 |
+
B --> C{Agent Executions}
|
49 |
+
C -->|Agent 1| D[Vulnerability Detection]
|
50 |
+
C -->|Agent 2| E[Anti-Pattern Analysis]
|
51 |
+
C -->|Agent 3| F[Code Quality Review]
|
52 |
+
D --> G[Comprehensive Report]
|
53 |
+
E --> G
|
54 |
+
F --> G
|
55 |
+
G --> H[Downloadable MD Report]
|
56 |
+
```
|
57 |
+
""")
|
58 |
+
gr.Markdown(part3)
|
59 |
+
|
60 |
+
# Smart Contract Analysis Tab
|
61 |
+
|
62 |
+
|
63 |
+
def upload_tab():
|
64 |
+
upload_description = """
|
65 |
+
# Smart Contract Vulnerability Analysis
|
66 |
+
> Upload your smart contract code as a text file and analyze it for potential vulnerabilities using AI agents powered by Hugging Face models.
|
67 |
+
|
68 |
+
## How it works:
|
69 |
+
1. **Upload**: Select your smart contract (.txt, .sol files)
|
70 |
+
2. **Configure**: Enter your Hugging Face API token
|
71 |
+
3. **Analyze**: AI agents will scan for common vulnerabilities
|
72 |
+
4. **Download**: Get detailed results in markdown format
|
73 |
+
"""
|
74 |
+
|
75 |
+
analysis_info = """
|
76 |
+
## Analysis Components
|
77 |
+
- **π Vulnerability Detection**: Identifies common smart contract security issues (reentrancy, overflow, etc.)
|
78 |
+
- **π Code Quality Assessment**: Reviews code structure and best practices
|
79 |
+
- **β οΈ Risk Assessment**: Categorizes findings by severity level (High/Medium/Low)
|
80 |
+
- **π Anti-Pattern Detection**: Finds problematic coding patterns
|
81 |
+
"""
|
82 |
+
|
83 |
+
def analyze_contract(file, hf_token, progress=gr.Progress()):
|
84 |
+
if file is None:
|
85 |
+
return "β No file uploaded.", "", gr.update(visible=False)
|
86 |
+
|
87 |
+
if not hf_token.strip():
|
88 |
+
return "β Please provide a valid Hugging Face API token.", "", gr.update(visible=False)
|
89 |
+
|
90 |
+
try:
|
91 |
+
progress(0.1, desc="π Reading contract file...")
|
92 |
+
|
93 |
+
# Read file content
|
94 |
+
if hasattr(file, 'read'):
|
95 |
+
content = file.read().decode("utf-8")
|
96 |
+
else:
|
97 |
+
with open(file.name, 'r', encoding='utf-8') as f:
|
98 |
+
content = f.read()
|
99 |
+
|
100 |
+
progress(0.3, desc="π€ Initializing AI agents...")
|
101 |
+
|
102 |
+
# Initialize Hugging Face API
|
103 |
+
try:
|
104 |
+
from huggingface_hub import InferenceClient
|
105 |
+
client = InferenceClient(token=hf_token)
|
106 |
+
api_available = True
|
107 |
+
except ImportError:
|
108 |
+
api_available = False
|
109 |
+
|
110 |
+
progress(0.5, desc="π Analyzing contract for vulnerabilities...")
|
111 |
+
|
112 |
+
# Create analysis prompt
|
113 |
+
analysis_prompt = f"""
|
114 |
+
Analyze the following smart contract code for security vulnerabilities and provide a detailed report:
|
115 |
+
|
116 |
+
CONTRACT CODE:
|
117 |
+
{content[:4000]}
|
118 |
+
|
119 |
+
Please identify:
|
120 |
+
1. Security vulnerabilities (reentrancy, overflow, access control, etc.)
|
121 |
+
2. Code quality issues
|
122 |
+
3. Best practice violations
|
123 |
+
4. Risk level for each finding (High/Medium/Low)
|
124 |
+
|
125 |
+
Format your response as a structured analysis with clear sections.
|
126 |
+
"""
|
127 |
+
|
128 |
+
progress(0.7, desc="π§ Running AI analysis...")
|
129 |
+
|
130 |
+
if api_available and hf_token.strip():
|
131 |
+
try:
|
132 |
+
# Try to use HF API for analysis
|
133 |
+
response = client.text_generation(
|
134 |
+
prompt=analysis_prompt,
|
135 |
+
model="microsoft/DialoGPT-large",
|
136 |
+
max_new_tokens=1000,
|
137 |
+
temperature=0.3
|
138 |
+
)
|
139 |
+
ai_analysis = response if isinstance(
|
140 |
+
response, str) else str(response)
|
141 |
+
except Exception as model_error:
|
142 |
+
ai_analysis = f"AI API Error: {str(model_error)}"
|
143 |
+
else:
|
144 |
+
ai_analysis = "AI API not available - performing basic analysis"
|
145 |
+
|
146 |
+
progress(0.8, desc="π Generating comprehensive report...")
|
147 |
+
|
148 |
+
# Perform basic code analysis
|
149 |
+
lines = content.splitlines()
|
150 |
+
function_count = content.count('function')
|
151 |
+
modifier_count = content.count('modifier')
|
152 |
+
pragma_count = content.count('pragma')
|
153 |
+
|
154 |
+
# Basic vulnerability checks
|
155 |
+
vulnerabilities = []
|
156 |
+
if 'call.value' in content:
|
157 |
+
vulnerabilities.append(
|
158 |
+
"β οΈ **HIGH**: Potential reentrancy vulnerability detected (call.value usage)")
|
159 |
+
if 'tx.origin' in content:
|
160 |
+
vulnerabilities.append(
|
161 |
+
"β οΈ **MEDIUM**: tx.origin usage detected (use msg.sender instead)")
|
162 |
+
if 'block.timestamp' in content or 'now' in content:
|
163 |
+
vulnerabilities.append(
|
164 |
+
"β οΈ **LOW**: Timestamp dependence detected")
|
165 |
+
if not any(word in content.lower() for word in ['require', 'assert', 'revert']):
|
166 |
+
vulnerabilities.append(
|
167 |
+
"β οΈ **MEDIUM**: No input validation detected")
|
168 |
+
|
169 |
+
# Create comprehensive analysis
|
170 |
+
analysis_result = f"""
|
171 |
+
# π‘οΈ Smart Contract Security Analysis
|
172 |
+
|
173 |
+
## π Code Statistics
|
174 |
+
- **Lines of Code**: {len(lines)}
|
175 |
+
- **Functions**: {function_count}
|
176 |
+
- **Modifiers**: {modifier_count}
|
177 |
+
- **Pragma Statements**: {pragma_count}
|
178 |
+
|
179 |
+
## π Vulnerability Assessment
|
180 |
+
{chr(10).join(vulnerabilities) if vulnerabilities else "β
No obvious vulnerabilities detected in basic scan"}
|
181 |
+
|
182 |
+
## π€ AI Analysis Results
|
183 |
+
{ai_analysis}
|
184 |
+
|
185 |
+
## π Code Quality Review
|
186 |
+
- File successfully parsed and analyzed
|
187 |
+
- {'β
Uses Solidity pragma' if pragma_count > 0 else 'β Missing pragma statement'}
|
188 |
+
- {'β
Contains function modifiers' if modifier_count > 0 else 'β οΈ No custom modifiers found'}
|
189 |
+
- {'β
Has function definitions' if function_count > 0 else 'β No functions detected'}
|
190 |
+
|
191 |
+
## π― Recommendations
|
192 |
+
1. **Security**: Implement comprehensive access control mechanisms
|
193 |
+
2. **Testing**: Add unit tests for all critical functions
|
194 |
+
3. **Validation**: Ensure proper input validation on all public functions
|
195 |
+
4. **Upgrades**: Use latest Solidity compiler version
|
196 |
+
5. **Audit**: Consider professional security audit before deployment
|
197 |
+
|
198 |
+
## β‘ Next Steps
|
199 |
+
- Review and address identified vulnerabilities
|
200 |
+
- Implement recommended security measures
|
201 |
+
- Consider formal verification for critical functions
|
202 |
+
- Test thoroughly on testnets before mainnet deployment
|
203 |
+
"""
|
204 |
+
|
205 |
+
progress(0.9, desc="πΎ Creating downloadable report...")
|
206 |
+
|
207 |
+
# Create downloadable markdown report
|
208 |
+
timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
209 |
+
report_content = f"""# Smart Contract Security Analysis Report
|
210 |
+
**Generated on**: {datetime.datetime.now().strftime("%Y-%m-%d at %H:%M:%S")}
|
211 |
+
**Analyzer**: Xyizko Smart Contract Security Platform
|
212 |
+
|
213 |
+
## π Contract Overview
|
214 |
+
**File Size**: {len(content)} characters
|
215 |
+
**Analysis Type**: Automated Security Scan
|
216 |
+
|
217 |
+
### Code Preview
|
218 |
+
```solidity
|
219 |
+
{content[:500]}...
|
220 |
+
```
|
221 |
+
|
222 |
+
{analysis_result}
|
223 |
+
|
224 |
+
---
|
225 |
+
## π¬ Technical Details
|
226 |
+
- **Analysis Engine**: AI-Powered Multi-Agent System
|
227 |
+
- **Models Used**: Hugging Face Transformers
|
228 |
+
- **Scan Type**: Comprehensive Security Assessment
|
229 |
+
|
230 |
+
## βοΈ Disclaimer
|
231 |
+
This automated analysis is for informational purposes only. Always conduct thorough manual reviews and professional audits before deploying smart contracts to production environments.
|
232 |
+
|
233 |
+
---
|
234 |
+
*Report generated by Xyizko Smart Contract Analyzer - MCH Hackathon 2025*
|
235 |
+
"""
|
236 |
+
|
237 |
+
progress(1.0, desc="β
Analysis complete!")
|
238 |
+
|
239 |
+
# Create downloadable file
|
240 |
+
report_filename = f"contract_security_analysis_{timestamp}.md"
|
241 |
+
try:
|
242 |
+
with open(report_filename, 'w', encoding='utf-8') as f:
|
243 |
+
f.write(report_content)
|
244 |
+
|
245 |
+
return (
|
246 |
+
"β
Analysis completed successfully! Review the results below and download the full report.",
|
247 |
+
analysis_result,
|
248 |
+
gr.update(visible=True, value=report_filename)
|
249 |
+
)
|
250 |
+
except Exception:
|
251 |
+
return (
|
252 |
+
"β
Analysis completed! (Note: Could not create download file)",
|
253 |
+
analysis_result,
|
254 |
+
gr.update(visible=False)
|
255 |
+
)
|
256 |
+
|
257 |
+
except Exception as e:
|
258 |
+
return f"β Error during analysis: {str(e)}", "", gr.update(visible=False)
|
259 |
+
|
260 |
+
with gr.Column():
|
261 |
+
gr.Markdown(upload_description)
|
262 |
+
gr.Markdown(analysis_info)
|
263 |
+
|
264 |
+
with gr.Row():
|
265 |
+
with gr.Column(scale=2):
|
266 |
+
file_input = gr.File(
|
267 |
+
label="π Upload Smart Contract",
|
268 |
+
file_types=[".txt", ".sol"],
|
269 |
+
height=120
|
270 |
+
)
|
271 |
+
hf_token_input = gr.Textbox(
|
272 |
+
label="π Hugging Face API Token",
|
273 |
+
placeholder="hf_xxxxxxxxxxxxxxxxxxxxxxxxx",
|
274 |
+
type="password",
|
275 |
+
info="Get your token from https://huggingface.co/settings/tokens"
|
276 |
+
)
|
277 |
+
|
278 |
+
with gr.Column(scale=1):
|
279 |
+
analyze_btn = gr.Button(
|
280 |
+
"π Start Analysis",
|
281 |
+
variant="primary",
|
282 |
+
size="lg",
|
283 |
+
scale=2
|
284 |
+
)
|
285 |
+
|
286 |
+
gr.Markdown("---")
|
287 |
+
|
288 |
+
status_output = gr.Textbox(
|
289 |
+
label="π Analysis Status",
|
290 |
+
lines=2,
|
291 |
+
interactive=False,
|
292 |
+
show_copy_button=True
|
293 |
+
)
|
294 |
+
|
295 |
+
analysis_output = gr.Textbox(
|
296 |
+
label="π Detailed Analysis Results",
|
297 |
+
lines=20,
|
298 |
+
max_lines=25,
|
299 |
+
show_copy_button=True
|
300 |
+
)
|
301 |
+
|
302 |
+
download_file = gr.File(
|
303 |
+
label="π₯ Download Complete Report (Markdown)",
|
304 |
+
visible=False
|
305 |
+
)
|
306 |
+
|
307 |
+
analyze_btn.click(
|
308 |
+
fn=analyze_contract,
|
309 |
+
inputs=[file_input, hf_token_input],
|
310 |
+
outputs=[status_output, analysis_output, download_file]
|
311 |
+
)
|
312 |
+
|
313 |
+
|
314 |
+
# --- Main Demo UI Function ---
|
315 |
+
with gr.Blocks(theme=gr.themes.Ocean(), title="Xyizko - Smart Contract Analyzer") as demo:
|
316 |
+
|
317 |
+
with gr.Tab("π Introduction"):
|
318 |
+
app_intro()
|
319 |
+
|
320 |
+
with gr.Tab("π Contract Analyzer"):
|
321 |
+
upload_tab()
|
322 |
+
|
323 |
+
if __name__ == "__main__":
|
324 |
+
demo.launch()
|