Skip to content

Instantly share code, notes, and snippets.

@ilyar
Last active May 7, 2023 10:56
Show Gist options
  • Save ilyar/cc31d37e4091287cb13a4e9bc12df5d1 to your computer and use it in GitHub Desktop.
Save ilyar/cc31d37e4091287cb13a4e9bc12df5d1 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ts-node
import { createHash } from 'crypto'
function slugSha256(data: string): number {
return Number(`0x${createHash('sha256').update(data).digest('hex').slice(0, 8)}`)
}
function functionHash(signature: string): number {
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: string): string {
return `0x${functionHash(signature + 'v2').toString(16)}`
}
console.log(functionID(process.argv[2]))
@ilyar
Copy link
Author

ilyar commented May 7, 2023

Also see

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