Skip to content

Instantly share code, notes, and snippets.

@fzn0x
Last active November 5, 2022 01:50
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 fzn0x/b9a921c378ede2f5a57e14c042757738 to your computer and use it in GitHub Desktop.
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!
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