Skip to content

Instantly share code, notes, and snippets.

@ryley-o
Created September 4, 2024 21:51
Show Gist options
  • Save ryley-o/f837e2db101636e1231f0d2f1b44c895 to your computer and use it in GitHub Desktop.
Save ryley-o/f837e2db101636e1231f0d2f1b44c895 to your computer and use it in GitHub Desktop.
Convert from hash seed to token hash, js + viem
import { keccak256, encodeAbiParameters, parseAbiParameters } from "viem";
export function hashSeedToTokenHashViem(seedAbiEncoded) {
return keccak256(seedAbiEncoded);
}
// hash seed is arbitrary and can be any bytes12 value except 0x0
const hashSeed = "0x123456789012345678901234";
// encode hash seed in abi format
// @dev in practice, this is right padded w/ 0s to 32 bytes
const hashSeedAbiEncoded = encodeAbiParameters(parseAbiParameters("bytes12"), [
hashSeed,
]);
console.log("hashSeed: ", hashSeed);
console.log("hashSeedAbiEncoded: ", hashSeedAbiEncoded);
// convert from abi encoded hash seed to token hash
const tokenHashViem = hashSeedToTokenHashViem(hashSeedAbiEncoded);
console.log("tokenHash: ", tokenHashViem);
// expected value from solidity testing in remix
const expectedValue =
"0x3d3e41593eda9eafe3d198fae22878a82189b2216eff154e1571cd72463759a7";
if (tokenHashViem !== expectedValue) {
throw new Error("hashSeedToTokenHashViem failed");
}
console.log("output matches expected value");
@ryley-o
Copy link
Author

ryley-o commented Sep 4, 2024

this can be boiled simplified into a single line, but is expanded here for clarity + verification checks

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