Skip to content

Instantly share code, notes, and snippets.

@nventuro
Created May 4, 2020 17:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nventuro/e93ae87bc7db765d233d8a72f80dedb2 to your computer and use it in GitHub Desktop.
Save nventuro/e93ae87bc7db765d233d8a72f80dedb2 to your computer and use it in GitHub Desktop.
function findSelectors(bytecode) {
// We look for:
// * PUSH4 (0x63)
// * 8 bytes (the selector itself)
// * 2 optional bytes (a possible DUP, this is inserted when optimizations are enabled)
// * EQ (0x14)
// * One of:
// - PUSH1 (0x60) + 2 bytes (the jump destination)
// - PUSH2 (0x61) + 4 bytes (the jump destination)
// * JUMPI (0x57)
const re = /63(([a-f]|\d){8})(([a-f]|\d){2})?14((60([a-f]|\d){2})|(61([a-f]|\d){4}))57/g;
// Since we only accept jumps of up to 4 bytes, the bytecode must have less than 2*16 bytes.
if (bytecode.length / 2 >= 2**16) { // Division by two since each byte is two characters
throw new Error();
}
let match;
const selectors = [];
while ((match = re.exec(bytecode)) !== null) {
selectors.push(match[1]);
}
return selectors;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment