Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
298 lines (266 loc) Β· 10.4 KB
/
Copy pathserver.js
File metadata and controls
298 lines (266 loc) Β· 10.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
import express from 'express';
import cors from 'cors';
import { readFileSync, writeFileSync, existsSync } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
const __dirname = dirname(fileURLToPath(import.meta.url));
const app = express();
const PORT = process.env.PORT || 3456;
app.use(cors());
app.use(express.json());
app.use(express.static(join(__dirname, 'public')));
// ββ Config ββββββββββββββββββββββββββββββββββββββββββββββ
const MIMO_API_KEY = process.env.MIMO_API_KEY || 'demo';
const MIMO_BASE_URL = process.env.MIMO_BASE_URL || 'https://api.xiaomimimo.com/v1';
const ETHERSCAN_API_KEY = process.env.ETHERSCAN_API_KEY || '';
const DB_FILE = join(__dirname, 'data.json');
// ββ Database (JSON file) ββββββββββββββββββββββββββββββββ
function loadDB() {
if (!existsSync(DB_FILE)) {
return { wallets: [], alerts: [], analyses: [] };
}
return JSON.parse(readFileSync(DB_FILE, 'utf-8'));
}
function saveDB(db) {
writeFileSync(DB_FILE, JSON.stringify(db, null, 2));
}
// ββ Etherscan fetch βββββββββββββββββββββββββββββββββββββ
async function fetchTransactions(address, limit = 10) {
if (!ETHERSCAN_API_KEY) {
// Return mock data if no API key
return generateMockTransactions(address, limit);
}
try {
const url = `https://api.etherscan.io/api?module=account&action=txlist&address=${address}&startblock=0&endblock=99999999&page=1&offset=${limit}&sort=desc&apikey=${ETHERSCAN_API_KEY}`;
const res = await fetch(url);
const data = await res.json();
if (data.status === '1' && data.result) {
return data.result.map(tx => ({
hash: tx.hash,
from: tx.from,
to: tx.to,
value: (parseInt(tx.value) / 1e18).toFixed(6),
gasUsed: tx.gasUsed,
gasPrice: tx.gasPrice,
timeStamp: new Date(parseInt(tx.timeStamp) * 1000).toISOString(),
isError: tx.isError === '1',
method: tx.functionName?.split('(')[0] || 'transfer',
status: tx.isError === '1' ? 'failed' : 'success'
}));
}
return generateMockTransactions(address, limit);
} catch (e) {
console.error('Etherscan fetch error:', e.message);
return generateMockTransactions(address, limit);
}
}
function generateMockTransactions(address, limit) {
const methods = ['swap', 'transfer', 'approve', 'mint', 'bridge', 'delegate', 'stake'];
const tokens = ['ETH', 'USDC', 'USDT', 'WETH', 'ARB', 'OP'];
const txs = [];
for (let i = 0; i < limit; i++) {
const isOut = Math.random() > 0.5;
txs.push({
hash: `0x${Array.from({length: 64}, () => Math.floor(Math.random()*16).toString(16)).join('')}`,
from: isOut ? address : `0x${Array.from({length: 40}, () => Math.floor(Math.random()*16).toString(16)).join('')}`,
to: isOut ? `0x${Array.from({length: 40}, () => Math.floor(Math.random()*16).toString(16)).join('')}` : address,
value: (Math.random() * 5).toFixed(6),
gasUsed: String(Math.floor(21000 + Math.random() * 200000)),
gasPrice: String(Math.floor(10 + Math.random() * 100)),
timeStamp: new Date(Date.now() - Math.random() * 86400000 * 7).toISOString(),
isError: Math.random() < 0.05,
method: methods[Math.floor(Math.random() * methods.length)],
status: Math.random() < 0.05 ? 'failed' : 'success'
});
}
return txs.sort((a, b) => new Date(b.timeStamp) - new Date(a.timeStamp));
}
// ββ MiMo AI Analysis ββββββββββββββββββββββββββββββββββββ
async function analyzeWithMiMo(walletAddress, transactions) {
const txSummary = transactions.slice(0, 5).map(tx =>
`${tx.method} | ${tx.value} ETH | ${tx.from.slice(0,8)}...β${tx.to.slice(0,8)}... | ${tx.status}`
).join('\n');
const prompt = `You are a blockchain security analyst. Analyze this EVM wallet activity and provide a concise report.
Wallet: ${walletAddress}
Recent Transactions:
${txSummary}
Provide:
1. Risk Level: LOW/MEDIUM/HIGH/CRITICAL
2. Activity Summary (1-2 sentences)
3. Suspicious Patterns (if any)
4. Recommendations
Keep it concise and actionable. Format as JSON:
{"risk_level":"...","summary":"...","patterns":["..."],"recommendations":["..."]}`;
try {
const res = await fetch(`${MIMO_BASE_URL}/chat/completions`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${MIMO_API_KEY}`
},
body: JSON.stringify({
model: 'MiMo-V2.5',
messages: [{ role: 'user', content: prompt }],
temperature: 0.3,
max_tokens: 500
})
});
if (!res.ok) {
return generateFallbackAnalysis(walletAddress, transactions);
}
const data = await res.json();
const content = data.choices?.[0]?.message?.content || '';
try {
const jsonMatch = content.match(/\{[\s\S]*\}/);
if (jsonMatch) return JSON.parse(jsonMatch[0]);
} catch {}
return generateFallbackAnalysis(walletAddress, transactions);
} catch (e) {
console.error('MiMo API error:', e.message);
return generateFallbackAnalysis(walletAddress, transactions);
}
}
function generateFallbackAnalysis(address, txs) {
const failedCount = txs.filter(t => t.status === 'failed').length;
const riskLevel = failedCount > 2 ? 'HIGH' : failedCount > 0 ? 'MEDIUM' : 'LOW';
return {
risk_level: riskLevel,
summary: `Wallet ${address.slice(0,10)}... has ${txs.length} recent transactions with ${failedCount} failures.`,
patterns: failedCount > 0 ? [`${failedCount} failed transactions detected`] : ['No suspicious patterns'],
recommendations: ['Monitor for unusual activity', 'Set up alerts for large transfers']
};
}
// ββ Alert Detection βββββββββββββββββββββββββββββββββββββ
function detectAlerts(address, txs) {
const alerts = [];
for (const tx of txs) {
if (parseFloat(tx.value) > 1.0) {
alerts.push({
type: 'large_transfer',
severity: 'warning',
message: `Large transfer: ${tx.value} ETH`,
txHash: tx.hash,
address,
timestamp: tx.timeStamp
});
}
if (tx.status === 'failed') {
alerts.push({
type: 'failed_tx',
severity: 'info',
message: `Failed transaction: ${tx.method}`,
txHash: tx.hash,
address,
timestamp: tx.timeStamp
});
}
if (tx.method === 'approve') {
alerts.push({
type: 'token_approval',
severity: 'warning',
message: `Token approval: ${tx.to.slice(0,10)}...`,
txHash: tx.hash,
address,
timestamp: tx.timeStamp
});
}
}
return alerts;
}
// ββ API Routes ββββββββββββββββββββββββββββββββββββββββββ
// Get all wallets
app.get('/api/wallets', (req, res) => {
const db = loadDB();
res.json(db.wallets);
});
// Add wallet
app.post('/api/wallets', (req, res) => {
const { address, label, chain } = req.body;
if (!address) return res.status(400).json({ error: 'Address required' });
const db = loadDB();
const exists = db.wallets.find(w => w.address.toLowerCase() === address.toLowerCase());
if (exists) return res.status(409).json({ error: 'Wallet already tracked' });
const wallet = {
id: Date.now().toString(36),
address: address.trim(),
label: label || `Wallet ${db.wallets.length + 1}`,
chain: chain || 'ethereum',
addedAt: new Date().toISOString(),
lastChecked: null,
riskLevel: 'UNKNOWN'
};
db.wallets.push(wallet);
saveDB(db);
res.json(wallet);
});
// Remove wallet
app.delete('/api/wallets/:id', (req, res) => {
const db = loadDB();
db.wallets = db.wallets.filter(w => w.id !== req.params.id);
saveDB(db);
res.json({ success: true });
});
// Get transactions for wallet
app.get('/api/wallets/:address/transactions', async (req, res) => {
const limit = parseInt(req.query.limit) || 10;
const txs = await fetchTransactions(req.params.address, limit);
res.json(txs);
});
// Analyze wallet with MiMo AI
app.post('/api/wallets/:address/analyze', async (req, res) => {
const address = req.params.address;
const txs = await fetchTransactions(address, 10);
const analysis = await analyzeWithMiMo(address, txs);
const alerts = detectAlerts(address, txs);
// Save analysis
const db = loadDB();
const walletIdx = db.wallets.findIndex(w => w.address.toLowerCase() === address.toLowerCase());
if (walletIdx >= 0) {
db.wallets[walletIdx].lastChecked = new Date().toISOString();
db.wallets[walletIdx].riskLevel = analysis.risk_level;
}
db.analyses.push({ address, analysis, timestamp: new Date().toISOString() });
db.alerts.push(...alerts);
if (db.analyses.length > 100) db.analyses = db.analyses.slice(-100);
if (db.alerts.length > 200) db.alerts = db.alerts.slice(-200);
saveDB(db);
res.json({ analysis, alerts, transactions: txs.slice(0, 5) });
});
// Get alerts
app.get('/api/alerts', (req, res) => {
const db = loadDB();
const limit = parseInt(req.query.limit) || 20;
res.json(db.alerts.slice(-limit).reverse());
});
// Dashboard stats
app.get('/api/stats', async (req, res) => {
const db = loadDB();
const stats = {
totalWallets: db.wallets.length,
totalAlerts: db.alerts.length,
riskBreakdown: {
LOW: db.wallets.filter(w => w.riskLevel === 'LOW').length,
MEDIUM: db.wallets.filter(w => w.riskLevel === 'MEDIUM').length,
HIGH: db.wallets.filter(w => w.riskLevel === 'HIGH').length,
CRITICAL: db.wallets.filter(w => w.riskLevel === 'CRITICAL').length,
UNKNOWN: db.wallets.filter(w => w.riskLevel === 'UNKNOWN' || !w.riskLevel).length
},
recentAlerts: db.alerts.slice(-5).reverse(),
analysesCount: db.analyses.length
};
res.json(stats);
});
// Health check
app.get('/api/health', (req, res) => {
res.json({
status: 'ok',
version: '1.0.0',
ai: MIMO_API_KEY !== 'demo' ? 'MiMo V2.5' : 'fallback',
uptime: process.uptime()
});
});
app.listen(PORT, '0.0.0.0', () => {
console.log(`\n π¦ MiMo Wallet Tracker running on http://localhost:${PORT}\n`);
console.log(` AI Engine: ${MIMO_API_KEY !== 'demo' ? 'MiMo V2.5 β
' : 'Fallback (set MIMO_API_KEY)'}`);
console.log(` Etherscan: ${ETHERSCAN_API_KEY ? 'Connected β
' : 'Mock data (set ETHERSCAN_API_KEY)\n'}`);
});