forked from Vero-protocol/vero-guardian-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiffEngine.tsx
More file actions
111 lines (98 loc) · 4.41 KB
/
Copy pathDiffEngine.tsx
File metadata and controls
111 lines (98 loc) · 4.41 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
import React, { useState, useMemo, useEffect } from 'react';
import { GitCompare, AlertTriangle, CheckCircle } from 'lucide-react';
export interface DiffEngineProps {
onChainAbi: string;
repoAbi: string;
onChainBytecode: string;
repoBytecode: string;
}
interface DiffResult {
hasDrift: boolean;
abiMatch: boolean;
bytecodeMatch: boolean;
sanitizedOnChainAbi: string;
sanitizedRepoAbi: string;
}
/**
* Sanitizes the string for off-chain comparison to ensure it's safe.
*/
const sanitizeForComparison = (input: string): string => {
if (!input) return '';
// Strip all HTML tags, including <script>. Re-applies the pattern until the
// string stops changing so nested/overlapping tags (e.g. "<<script>script>")
// can't survive a single pass.
let previous: string;
let sanitized = input;
do {
previous = sanitized;
sanitized = sanitized.replace(/<[^>]*>/g, '');
} while (sanitized !== previous);
return sanitized.trim();
};
export default function DiffEngine({
onChainAbi,
repoAbi,
onChainBytecode,
repoBytecode,
}: DiffEngineProps): React.ReactElement {
const [result, setResult] = useState<DiffResult | null>(null);
useEffect(() => {
// Performance optimized via local state processing
const processDiff = () => {
const sanitizedOnChainAbi = sanitizeForComparison(onChainAbi);
const sanitizedRepoAbi = sanitizeForComparison(repoAbi);
const abiMatch = sanitizedOnChainAbi === sanitizedRepoAbi;
const bytecodeMatch = onChainBytecode.trim() === repoBytecode.trim();
setResult({
hasDrift: !abiMatch || !bytecodeMatch,
abiMatch,
bytecodeMatch,
sanitizedOnChainAbi,
sanitizedRepoAbi,
});
};
processDiff();
}, [onChainAbi, repoAbi, onChainBytecode, repoBytecode]);
if (!result) return <div>Loading diff engine...</div>;
return (
<div className="p-4 rounded-xl border border-slate-200 dark:border-slate-700 bg-white dark:bg-slate-900 shadow-sm">
<div className="flex items-center gap-2 mb-4">
<GitCompare className="w-5 h-5 text-indigo-600 dark:text-indigo-400" />
<h3 className="text-lg font-semibold text-slate-900 dark:text-white">State Diff Engine</h3>
</div>
<div className="mb-6">
{result.hasDrift ? (
<div className="flex items-center gap-2 p-3 bg-red-50 dark:bg-red-900/20 text-red-700 dark:text-red-400 rounded-lg">
<AlertTriangle className="w-5 h-5" />
<span className="font-medium">State drift identified. Auditor-ready report generated.</span>
</div>
) : (
<div className="flex items-center gap-2 p-3 bg-emerald-50 dark:bg-emerald-900/20 text-emerald-700 dark:text-emerald-400 rounded-lg">
<CheckCircle className="w-5 h-5" />
<span className="font-medium">On-chain state matches repository version perfectly.</span>
</div>
)}
</div>
<div className="space-y-4">
<div className="p-4 bg-slate-50 dark:bg-slate-800 rounded-lg border border-slate-100 dark:border-slate-700">
<h4 className="font-medium text-slate-700 dark:text-slate-300 mb-2">ABI Comparison</h4>
<div className="flex items-center gap-2">
<span className={`px-2 py-1 text-xs font-semibold rounded-full ${result.abiMatch ? 'bg-emerald-100 text-emerald-700 dark:bg-emerald-900/30 dark:text-emerald-400' : 'bg-red-100 text-red-700 dark:bg-red-900/30 dark:text-red-400'}`}>
{result.abiMatch ? 'Match' : 'Mismatch'}
</span>
{!result.abiMatch && <span className="text-sm text-slate-500">Drift detected in ABI definition.</span>}
</div>
</div>
<div className="p-4 bg-slate-50 dark:bg-slate-800 rounded-lg border border-slate-100 dark:border-slate-700">
<h4 className="font-medium text-slate-700 dark:text-slate-300 mb-2">Bytecode Comparison</h4>
<div className="flex items-center gap-2">
<span className={`px-2 py-1 text-xs font-semibold rounded-full ${result.bytecodeMatch ? 'bg-emerald-100 text-emerald-700 dark:bg-emerald-900/30 dark:text-emerald-400' : 'bg-red-100 text-red-700 dark:bg-red-900/30 dark:text-red-400'}`}>
{result.bytecodeMatch ? 'Match' : 'Mismatch'}
</span>
{!result.bytecodeMatch && <span className="text-sm text-slate-500">Drift detected in compiled bytecode.</span>}
</div>
</div>
</div>
</div>
);
}