Last active
November 5, 2022 01:50
-
-
Save fzn0x/b9a921c378ede2f5a57e14c042757738 to your computer and use it in GitHub Desktop.
Get text signature from bytes signature, thanks https://www.4byte.directory/signatures for the free service!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fetch from "node-fetch"; | |
export default async function getSignatureMethods(hash) { | |
if (!hash) { | |
throw new Error("signature hash is required"); | |
} | |
if (!Buffer.isBuffer(hash)) { | |
hash = hash.replace(/^0x/, ""); | |
hash = Buffer.from(hash, "hex"); | |
} | |
if (hash.byteLength !== 4) { | |
throw new Error("Hash size must be 4 bytes"); | |
} | |
const url = `https://www.4byte.directory/api/v1/signatures/?hex_signature=0x${hash.toString( | |
"hex" | |
)}`; | |
const res = await fetch(url); | |
const results = (await res.json()).results; | |
return results.map((res) => res.text_signature); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment