Created
September 4, 2024 21:51
-
-
Save ryley-o/f837e2db101636e1231f0d2f1b44c895 to your computer and use it in GitHub Desktop.
Convert from hash seed to token hash, js + viem
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 { 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"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this can be boiled simplified into a single line, but is expanded here for clarity + verification checks