Skip to content

Instantly share code, notes, and snippets.

@ilyar
Last active May 7, 2023 10:56
Show Gist options
  • Save ilyar/f8119138b114bb25b7d50255e5f3c82d to your computer and use it in GitHub Desktop.
Save ilyar/f8119138b114bb25b7d50255e5f3c82d to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
'use strict'
const { createHash } = require('crypto')
function slugSha256(data) {
return Number(`0x${createHash('sha256').update(data).digest('hex').slice(0, 8)}`)
}
function functionHash(signature) {
let hash = slugSha256(signature.replaceAll(' ', ''))
// FIXME for constructor, cell, bytes see https://gist.github.com/ilyar/484360bdb5464a6f252bd8f8ec10aaff
return signature.includes('constructor') ? hash ^ 2 ** 31 : hash
}
function functionID(signature) {
return `0x${functionHash(signature + 'v2').toString(16)}`
}
console.log(functionID(process.argv[2]))
@ilyar
Copy link
Author

ilyar commented May 7, 2023

$ node es.js "main()(string)"
0xb0992770
$ node es.js "redeem(uint256, string, uint256)()"
0x58160fa0
$ bash es.js "constructor()()"
0x68b55f3f

Also see

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment