-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
256 lines (215 loc) · 6.77 KB
/
index.html
File metadata and controls
256 lines (215 loc) · 6.77 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Get functions from a smart contract's runtime bytecode.">
<meta property="og:title" content="Function Extractor">
<meta property="og:description" content="Get functions from a smart contract's runtime bytecode.">
<meta property="og:type" content="website">
<meta property="og:url" content="https://function-extractor.ardis.lu">
<link rel="icon" href="data:,">
<title>Function Extractor</title>
<style>
body {
background: hsl(216deg 50% 93%);
font-family: monospace;
inline-size: min(1440px, 100% - 16px);
margin-inline: auto;
}
header {
margin-block-end: 32px;
}
h1 {
margin: 0;
font-size: 2rem;
}
ul {
margin: 0;
padding: 0;
list-style-type: none;
display: flex;
flex-wrap: wrap;
}
li:not(:last-child)::after {
content: '|';
content: '|' / '';
padding: 8px;
}
a,
input,
textarea {
font-size: 1rem;
}
a {
border: 1px solid transparent;
border-radius: 4px;
transition: border 400ms;
}
form {
display: flex;
flex-direction: column;
}
label {
font-size: 1.25rem;
font-weight: 700;
}
label:not(:first-child) {
margin-block-start: 16px;
}
label[for="functions"],
#functions {
display: none;
}
textarea:not(:invalid)~:is(label[for="functions"], #functions) {
display: unset;
}
input,
textarea {
font-family: inherit;
padding: 6px;
border-radius: 4px;
border: 1px solid hsl(216deg 50% 80%);
transition: border 400ms;
}
textarea {
align-self: center;
min-inline-size: calc(100% - 16px);
max-inline-size: calc(100vi - 32px);
min-block-size: 10rem;
}
:is(a, input, textarea):is(:hover, :focus-within) {
border: 1px solid hsl(216deg 50% 65%);
transition: border 200ms;
}
:is(input, textarea):focus {
outline: 1px solid hsl(216deg 50% 50%);
}
output {
overflow: auto;
}
table {
border-collapse: collapse;
font-size: 1rem;
}
tr {
transition: background-color 400ms;
}
tr:hover,
tr:focus-within {
background: hsl(222deg 50% 90%);
transition: background-color 200ms;
}
th,
td {
line-height: 2;
padding-block: 8px;
padding-inline: 4px 16px;
}
</style>
</head>
<body>
<header>
<h1>Function Extractor</h1>
<ul>
<li><a href="https://github.com/ardislu/function-extractor">ardislu/function-extractor</a></li>
<li><a href="https://openchain.xyz/">openchain.xyz</a></li>
<li><a href="https://github.com/cdump/evmole">cdump/evmole</a></li>
</ul>
</header>
<main>
<form>
<label for="rpc">RPC</label>
<input type="url" name="rpc" id="rpc" placeholder="https://ethereum-rpc.publicnode.com" value="https://ethereum-rpc.publicnode.com">
<label for="contract">Contract address</label>
<input name="contract" id="contract" placeholder="0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48" value="0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48" autocomplete="off" data-1p-ignore data-lpignore="true" data-form-type="other" data-bwignore>
<label for="bytecode">Runtime bytecode</label>
<textarea name="bytecode" id="bytecode" placeholder="0x60606040..." required></textarea>
<label for="functions">Extracted functions</label>
<output name="functions" id="functions">
<table>
<thead>
<tr>
<th>Selector</th>
<th>Name</th>
</tr>
</thead>
<tbody></tbody>
</table>
</output>
</form>
</main>
<script type="module">
const form = document.querySelector('form');
const rpcInput = document.querySelector('#rpc');
const contractInput = document.querySelector('#contract');
const bytecodeInput = document.querySelector('#bytecode');
const tableBody = document.querySelector('tbody');
// https://twitter.com/high_byte/status/1606634118808223747
function getFunctionSelectors(bytecode) {
return [...bytecode.matchAll(/63([0-9a-f]{8})1461([0-9a-f]{4})57/ig)].map(g => `0x${g[1]}`);
}
async function getSelectorNames(selectors) {
const endpoint = 'https://api.openchain.xyz/signature-database/v1/lookup';
const params = new URLSearchParams({ function: selectors.join(',') });
const obj = await fetch(`${endpoint}?${params}`).then(r => r.json());
const out = {};
for (const [key, arr] of Object.entries(obj.result.function)) {
out[key] = arr?.[0].name ?? 'No name found.';
}
return out;
}
function debounce(fn, wait) {
let timeoutId;
return () => {
clearTimeout(timeoutId);
timeoutId = setTimeout(fn, wait);
}
}
contractInput.addEventListener('input', debounce(async () => {
// Do nothing if either RPC input or contract input are invalid
try { new URL(rpcInput.value); } catch { return; }
if (!/(0x)?[0-9a-f]{40}/i.test(contractInput.value)) { return; }
const bytecode = await fetch(rpcInput.value, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
jsonrpc: '2.0',
id: 1,
method: 'eth_getCode',
params: [contractInput.value, 'latest']
})
})
.then(r => r.json())
.then(obj => obj.result);
bytecodeInput.value = bytecode;
form.requestSubmit();
}, 250));
bytecodeInput.addEventListener('change', debounce(() => {
form.requestSubmit();
}, 250));
form.addEventListener('submit', async e => {
e.preventDefault();
const selectors = getFunctionSelectors(bytecodeInput.value);
const names = await getSelectorNames(selectors);
tableBody.innerHTML = '';
for (const [selector, name] of Object.entries(names)) {
tableBody.insertAdjacentHTML('beforeend', `<tr><td>${selector}</td><td>${name}</td></tr>`);
}
});
// Fix selecting trailing element on triple (or quad) click, resulting in a trailing tab or newline
// when copying text from the table.
addEventListener('click', e => {
if (e.detail >= 3 && tableBody.contains(e.target)) {
const selection = getSelection();
const range = selection.getRangeAt(0);
const textNode = e.target.firstChild;
range.setStart(textNode, 0);
range.setEnd(textNode, textNode.length);
};
});
// Set initial bytecode from prepopulated contract
contractInput.dispatchEvent(new Event('input'));
</script>
</body>
</html>