-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
560 lines (490 loc) · 19.5 KB
/
index.html
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
<!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="A utility to look up the Ethereum hexadecimal address for every ENS name in a list.">
<meta property="og:title" content="ens2hex">
<meta property="og:description" content="A utility to look up the Ethereum hexadecimal address for every ENS name in a list.">
<meta property="og:type" content="website">
<meta property="og:url" content="https://ens2hex.ardis.lu/">
<link rel="canonical" href="https://ens2hex.ardis.lu/">
<link rel="icon" href="data:,">
<link rel="modulepreload" href="/keccak256.js">
<title>ens2hex</title>
<style>
:root {
--primary-darker: hsl(16deg 30% 40%);
--primary: hsl(16deg 10% 60%);
--primary-lighter: hsl(16deg 40% 85%);
--primary-lightest: hsl(16deg 10% 90%);
--secondary: hsl(90deg 40% 35%);
--secondary-lighter: hsl(90deg 30% 80%);
}
body {
inline-size: min(100% - 16px, 720px);
margin-inline: auto;
font-family: -apple-system, BlinkMacSystemFont, avenir next, avenir, segoe ui, helvetica neue, helvetica, Cantarell, Ubuntu, roboto, noto, arial, sans-serif;
background: var(--primary-lightest);
}
header {
display: flex;
justify-content: center;
}
code {
background: var(--primary-lighter);
border-radius: 3px;
font-family: Menlo, Consolas, Monaco, Liberation Mono, Lucida Console, monospace;
padding: 0 3px;
}
a {
display: inline-block;
line-height: 1;
color: var(--secondary);
font-weight: 700;
text-decoration: none;
border-radius: 4px;
transition: background-color 500ms;
}
a:hover,
a:focus {
background: var(--secondary-lighter);
transition: background-color 150ms;
}
textarea,
input {
font-family: inherit;
font-size: 1rem;
border: none;
border-radius: 4px;
padding: 8px;
}
textarea {
resize: vertical;
min-block-size: 10ch;
}
.about,
.lookup,
form {
display: flex;
flex-direction: column;
gap: 8px;
margin-block-end: 24px;
}
.about h1,
.lookup h1,
form p {
margin: 0;
}
details {
border: 2px solid var(--secondary-lighter);
border-radius: 2px;
}
summary {
background: var(--secondary-lighter);
padding: 8px 16px;
cursor: pointer;
filter: brightness(0.95);
transition: filter 500ms;
}
summary:hover,
summary:focus {
filter: brightness(1);
transition: filter 150ms;
}
details .details-body {
padding: 8px 16px;
}
.upload-box {
display: flex;
flex-direction: column;
align-items: center;
padding: 48px 16px;
border: 2px dashed var(--primary);
border-radius: 12px;
transition: border-color 400ms;
}
.upload-box:hover,
.upload-box[data-dragover] {
border-color: var(--primary-darker);
transition: border-color 200ms;
}
.upload-box:hover,
.upload-box label:hover {
cursor: pointer;
}
.upload-box:has(input:focus) {
outline: -webkit-focus-ring-color auto 2px;
}
.upload-box input {
opacity: 0;
width: 0.1px;
height: 0.1px;
}
.inline-input-container {
display: flex;
flex-wrap: wrap;
align-items: baseline;
gap: 8px;
}
.inline-input-container input {
flex-grow: 1;
}
#selected-file {
block-size: 1rem;
color: var(--secondary);
font-weight: 500;
}
#fetch {
inline-size: fit-content;
color: inherit;
background: var(--secondary-lighter);
font-weight: 700;
padding: 16px;
border: 1px solid var(--secondary);
border-radius: 12px;
filter: brightness(0.95);
transition: filter 500ms;
}
#fetch:hover,
#fetch:focus {
cursor: pointer;
filter: brightness(1);
transition: filter 150ms;
}
[data-submitting] #fetch {
cursor: not-allowed;
filter: brightness(0.9);
}
.inline-spinner {
display: none;
box-sizing: border-box;
block-size: 1.5em;
inline-size: 1.5em;
border: 0.25em solid transparent;
border-block-start: 0.25em solid var(--secondary);
border-radius: 50%;
margin-inline-start: 8px;
transform: translateY(25%);
animation: spin 1s infinite;
}
@keyframes spin {
100% {
transform: translateY(25%) rotate(360deg);
}
}
output#table {
display: none;
}
#download {
cursor: pointer;
background: none;
border: none;
padding: 0;
transition: filter 400ms;
}
label[for="download"] {
cursor: pointer;
display: inline-block;
color: var(--secondary);
font-weight: 500;
transform: translateY(-5px);
}
#download:is(:hover, :focus) {
filter: drop-shadow(0 0 8px var(--secondary));
transition: filter 200ms;
}
#download svg {
inline-size: 24px;
block-size: 24px;
}
table {
inline-size: 100%;
border-collapse: collapse;
}
th,
td {
padding: 8px;
border-block: solid 1px var(--primary);
}
th {
background: var(--primary);
text-transform: uppercase;
}
th:first-child,
td:first-child {
border-inline-start: solid 1px var(--primary);
}
th:last-child,
td:last-child {
border-inline-end: solid 1px var(--primary);
}
tbody tr:nth-child(odd) {
background: var(--primary-lighter);
}
tbody tr:nth-child(even) {
background: var(--secondary-lighter);
}
</style>
</head>
<body>
<header>
<h1>ens2hex</h1>
</header>
<main>
<section class="about">
<h1>About this site</h1>
<details>
<summary>What is this?</summary>
<div class="details-body">
<p>This is a tool to get the <a href="https://ethereum.org">Ethereum</a> hexadecimal (<code>0x</code>) <a href="https://info.etherscan.com/what-is-an-ethereum-address/">address</a> for every <a href="https://ens.domains/"><abbr title="Ethereum Name Service">ENS</abbr></a> (<code>.eth</code>) name in a list.</p>
</div>
</details>
<details>
<summary>How does it work?</summary>
<div class="details-body">
<p>For each ENS name in the list, this tool will calculate the name's <code>namehash</code> according to <a href="https://ercs.ethereum.org/ERCS/erc-137">ERC-137</a>.</p>
<p>Then it will prepare a <a href="https://ethereum.org/en/developers/docs/apis/json-rpc/">JSON-RPC</a> request to query the <a href="https://www.multicall3.com/"><code>multicall3</code></a> smart contract, which in turn contains batched calls to the <a href="https://etherscan.io/address/0x4976fb03c32e5b8cfe2b6ccb31c09ba78ebaba41">ENS Public Resolver 2</a> and the <a href="https://etherscan.io/address/0x226159d592E2b063810a10Ebf6dcbADA94Ed68b8">Old ENS Public Resolver 2</a> smart contracts for the hexadecimal addresses.</p>
<p>Finally the tool sends the request to an <a href="https://ethereum.org/en/developers/docs/nodes-and-clients/">Ethereum node</a>, which executes the payload and provides a response.</p>
<p>Note that the <code>multicall3</code> smart contract was deployed in <a href="https://etherscan.io/tx/0x00d9fcb7848f6f6b0aae4fb709c133d69262b902156c85a473ef23faa60760bd">block number 14353601</a>, so <code>14353601</code> is the oldest block that can be queried.</p>
</div>
</details>
<details>
<summary>Credits</summary>
<div class="details-body">
<p>This tool uses a public Ethereum JSON-RPC node to query the ENS smart contract on the Ethereum blockchain. <a href="https://ethereumnodes.com/">Click here</a> or <a href="https://chainlist.org/chain/1">here</a> to see a list of some other public nodes you could use.</p>
<p>In the JavaScript, the only third-party code is the <code>keccak256</code> implementation used to hash the ENS names. The rest of the tool consists of about 200 lines of vanilla JavaScript to manipulate the names, prepare the JSON-RPC payload, and parse the response.</p>
<p>The <code>keccak256</code> implementation was bundled from Paul Miller's <a href="https://github.com/paulmillr/noble-hashes">noble-hashes</a> project.</p>
<p><a href="https://github.com/ardislu/ens2hex">Click here</a> to view this site's source code on GitHub.</p>
</div>
</details>
</section>
<section class="lookup">
<h1>ENS lookup</h1>
<form>
<label for="text">Copy a list of names here:</label>
<textarea id="text" name="text" rows="10"></textarea>
<p>Or upload a list of names:</p>
<div class="upload-box">
<label for="file">Drag and drop a file to upload or click to choose a file.</label>
<input id="file" type="file" name="file">
<p id="selected-file"></p>
</div>
<details>
<summary>Advanced options</summary>
<div class="details-body inline-input-container">
<label for="rpc">JSON-RPC node:</label>
<input type="url" name="rpc" id="rpc" value="https://ethereum-rpc.publicnode.com" required>
</div>
<div class="details-body inline-input-container">
<label for="block">Block number (empty value will default to "latest"):</label>
<input type="number" name="block" id="block" min="14353601" placeholder="21413499">
</div>
</details>
<div>
<button id="fetch">Fetch addresses</button>
<span class="inline-spinner"></span>
</div>
<output name="status" id="status"></output>
<output name="table" id="table">
<button id="download">
<svg role="img" aria-labelledby="download-label" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<title id="download-label">Download to CSV file</title>
<path d="M14.5 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7.5L14.5 2z"></path>
<polyline points="14 2 14 8 20 8"></polyline>
<path d="M12 18v-6"></path>
<path d="m9 15 3 3 3-3"></path>
</svg>
</button>
<label for="download">Download to CSV file</label>
<table>
<thead>
<tr>
<th>ENS name</th>
<th>Address</th>
</tr>
</thead>
</table>
</output>
</form>
</section>
</main>
<script type="module">
// noble-hashes v1.4.0
import { keccak256 } from '/keccak256.js';
// Implement drag and drop file upload
window.addEventListener('dragover', e => e.preventDefault());
window.addEventListener('drop', e => e.preventDefault());
const uploadBox = document.querySelector('.upload-box');
const input = document.querySelector('input');
uploadBox.addEventListener('dragenter', () => uploadBox.setAttribute('data-dragover', ''));
uploadBox.addEventListener('drop', e => {
input.files = e.dataTransfer.files;
input.dispatchEvent(new Event('change'));
uploadBox.dispatchEvent(new Event('dragleave'));
});
uploadBox.addEventListener('dragleave', () => uploadBox.removeAttribute('data-dragover'));
uploadBox.addEventListener('click', _ => input.click());
input.addEventListener('click', e => e.stopPropagation()); // Prevent 'click' from getting propagated to div (or else there will be two dialogs)
document.querySelector('label[for="file"]').addEventListener('click', e => e.stopPropagation());
input.addEventListener('change', e => {
const { name, size } = e.target.files[0] ?? {};
if (name === undefined) {
document.querySelector('#selected-file').textContent = '';
}
else {
document.querySelector('#selected-file').textContent = `${name} (${size}B)`;
}
})
// Implement namehash as defined in https://eips.ethereum.org/EIPS/eip-137
// Does NOT include the '0x' prefix in the return value so it can be easily passed into JSON-RPC
function namehash(name) {
// Special hardcoded output for the '' value defined in EIP-137
if (name === '') {
return '0000000000000000000000000000000000000000000000000000000000000000';
}
const encoder = new TextEncoder();
const nameByteArrays = [new Uint8Array(32).fill(0)]; // Byte array equivalent of the hardcoded '' output. MUST be the last value
nameByteArrays.unshift(...name.split('.').map(n => encoder.encode(n))); // Results in an array of encoded Uint8Array for each ENS name part
const hash = nameByteArrays.reduceRight((acc, v) => keccak256(Uint8Array.from([...acc, ...keccak256(v)])));
return [...hash].map(v => v.toString(16).padStart(2, '0')).join(''); // Convert byte array into 0-padded string. Must spread typed array into non-typed array to allow .map() to output strings
}
// Batch multiple "addr(bytes32)" calls to both "ENS Public Resolver 2" AND "Old ENS Public Resolver 2" using the multicall3 smart contract
async function multicall(rpc, names, block) {
if (block !== 'latest') {
block = `0x${block.toString(16)}`;
}
// Construct payload
const multiCallPrefix =
'0x82ad56cb' + // aggregate3((address,bool,bytes)[])
'20'.padStart(64, '0') + // Pointer to next calldata slot
names.length.toString(16).padStart(64, '0'); // Length of tuple
let multiCallTupleOffsets = '';
let multiCallTuples1 = '';
let multiCallTuples2 = '';
for (const [index, name] of names.entries()) {
// Each tuple takes exactly 6 32-byte slots, starting after these offsets themselves end
multiCallTupleOffsets += ((6 * 32 * index) + (names.length * 32)).toString(16).padStart(64, '0');
multiCallTuples1 +=
'4976fb03c32e5b8cfe2b6ccb31c09ba78ebaba41'.padStart(64, '0') + // ENS Public Resolver 2
'0'.repeat(64) + // Hardcode allowFailure = false
'60'.padStart(64, '0') + // Pointer to next calldata slot
'24'.padStart(64, '0') + // 0x24 == 36 bytes immediately follow
`3b3b57de${namehash(name)}`.padEnd(64 * 2, '0'); // Calldata for "addr(bytes32)", filling 2 slots
multiCallTuples2 +=
'226159d592E2b063810a10Ebf6dcbADA94Ed68b8'.padStart(64, '0') + // Old ENS Public Resolver 2
'0'.repeat(64) + // Hardcode allowFailure = false
'60'.padStart(64, '0') + // Pointer to next calldata slot
'24'.padStart(64, '0') + // 0x24 == 36 bytes immediately follow
`3b3b57de${namehash(name)}`.padEnd(64 * 2, '0'); // Calldata for "addr(bytes32)", filling 2 slots
}
const response = await fetch(rpc, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify([{
jsonrpc: '2.0',
method: 'eth_call',
params: [
{
to: '0xcA11bde05977b3631167028862bE2a173976CA11', // multicall3
data: multiCallPrefix + multiCallTupleOffsets + multiCallTuples1
},
block
],
id: 'new'
},
{
jsonrpc: '2.0',
method: 'eth_call',
params: [
{
to: '0xcA11bde05977b3631167028862bE2a173976CA11', // multicall3
data: multiCallPrefix + multiCallTupleOffsets + multiCallTuples2
},
block
],
id: 'old'
}]),
}).then(r => r.json());
const addresses = {};
for (const [index, name] of names.entries()) {
const prefixOffset = 64 * (2 + names.length); // First 2 slots encode struct offset and length, then 1 slot for the offset of each result
const payloadOffset = 64 * (3 + index * 4); // First 3 slots of each result is multicall overhead, actual result starts in slot 4
const totalOffset = prefixOffset + payloadOffset;
const addr1 = `0x${response[0].result.slice(totalOffset + 26, totalOffset + 66)}`; // Parse address from slot 4
const addr2 = `0x${response[1].result.slice(totalOffset + 26, totalOffset + 66)}`;
addresses[name] = addr1 === `0x${'0'.repeat(40)}` ? addr2 : addr1;
}
return addresses;
}
// ERC-55: https://ercs.ethereum.org/ERCS/erc-55
function toChecksumAddress(address) {
address = address.toLowerCase().replace('0x', '');
// Hash the address (treat it as UTF-8) and return as a hex string
const hash = [...keccak256(address)].map(v => v.toString(16).padStart(2, '0')).join('');
let ret = '0x';
for (let i = 0; i < 40; i++) {
if (parseInt(hash[i], 16) >= 8) {
ret += address[i].toUpperCase();
}
else {
ret += address[i];
}
}
return ret;
}
document.querySelector('form').addEventListener('submit', async event => {
event.preventDefault();
if (event.target.hasAttribute('data-submitting')) {
return;
}
event.target.setAttribute('data-submitting', '');
const spinner = document.querySelector('.inline-spinner');
spinner.style.setProperty('display', 'inline-block');
const status = event.target.elements['status'];
status.textContent = ''; // Clear previous status, if any
const formData = new FormData(event.target);
const block = formData.get('block') === '' ? 'latest' : Number(formData.get('block'));
const combined = `${formData.get('text')}\n${await formData.get('file').text()}`; // Concatenate textarea and file inputs
const parsed = new Set(combined.split('\n').map(c => c.split(',')).flat().map(e => e.trim().toLocaleLowerCase())); // Split on newline or comma, flatten, trim whitespace, lowercase, and de-duplicate
const cleaned = [...parsed].filter(name => name.slice(-4) === '.eth'); // Remove anything that does not end in '.eth'
if (cleaned.length === 0) {
status.textContent = 'No valid ENS names found.';
spinner.style.setProperty('display', 'none');
event.target.removeAttribute('data-submitting');
return;
}
const multicallResponse = await multicall(formData.get('rpc'), cleaned, block)
.catch(e => {
status.textContent = e;
return;
});
const table = document.querySelector('table');
table.querySelector('tbody')?.remove(); // Clear previous rows, if any
const body = table.createTBody();
for (const name of cleaned) {
const row = body.insertRow();
const ens = row.insertCell();
const address = row.insertCell();
ens.textContent = name;
const response = multicallResponse[name];
address.textContent = toChecksumAddress(response);
}
spinner.style.setProperty('display', 'none');
document.querySelector('output#table').style.display = 'block';
event.target.removeAttribute('data-submitting');
});
// Download to CSV
document.querySelector('#download').addEventListener('click', e => {
e.preventDefault(); // Prevents re-submit on the form
const table = document.querySelector('table');
const csv = table.innerText.replaceAll('\t', ',');
const file = new File([csv], `ENS_Addresses_${Date.now()}`, { type: 'text/csv' });
const a = document.createElement('a');
a.href = URL.createObjectURL(file);
a.download = file.name;
a.click();
});
</script>
</body>
</html>