-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.js
37 lines (34 loc) · 1.14 KB
/
worker.js
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
// From noble-hashes v1.4.0
import { keccak256 } from '/keccak256.js';
const encoder = new TextEncoder();
function validate(functionName, minimum) {
const byteArray = keccak256(encoder.encode(functionName)).slice(0, 4);
for (let i = 0; i < minimum; i++) {
if (byteArray[i] === 0) {
if (i + 1 === minimum) {
const hash = `0x${[...byteArray].map(b => b.toString(16).padStart(2, '0')).join('')}`;
return hash;
}
}
else {
return false;
}
}
}
function calculateOptimizedName(name, parametersTypes, seed, minimum) {
let counter = BigInt(seed);
let functionName = `${name}_${counter}(${parametersTypes})`;
let hash = validate(functionName, minimum);
while (!hash) {
counter++;
functionName = `${name}_${counter.toString(36)}(${parametersTypes})`;
hash = validate(functionName, minimum);
}
return { functionName, hash, counter };
}
addEventListener('message', e => {
const { name, parametersTypes, seed = 0, minimum } = e.data;
const { functionName, hash, counter } = calculateOptimizedName(name, parametersTypes, seed, minimum);
postMessage({ functionName, hash, counter });
close();
});